00001 #ifndef VIENNACL_LINALG_SPAI_HPP
00002 #define VIENNACL_LINALG_SPAI_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #include <utility>
00028 #include <iostream>
00029 #include <fstream>
00030 #include <string>
00031 #include <algorithm>
00032 #include <vector>
00033 #include <math.h>
00034 #include <map>
00035
00036
00037 #include "viennacl/linalg/detail/spai/spai_tag.hpp"
00038 #include "viennacl/linalg/qr.hpp"
00039 #include "viennacl/linalg/detail/spai/spai-dynamic.hpp"
00040 #include "viennacl/linalg/detail/spai/spai-static.hpp"
00041 #include "viennacl/linalg/detail/spai/sparse_vector.hpp"
00042 #include "viennacl/linalg/detail/spai/block_matrix.hpp"
00043 #include "viennacl/linalg/detail/spai/block_vector.hpp"
00044 #include "viennacl/linalg/detail/spai/fspai.hpp"
00045 #include "viennacl/linalg/detail/spai/spai.hpp"
00046
00047
00048 #include "boost/numeric/ublas/vector.hpp"
00049 #include "boost/numeric/ublas/matrix.hpp"
00050 #include "boost/numeric/ublas/matrix_proxy.hpp"
00051 #include "boost/numeric/ublas/vector_proxy.hpp"
00052 #include "boost/numeric/ublas/storage.hpp"
00053 #include "boost/numeric/ublas/io.hpp"
00054 #include "boost/numeric/ublas/lu.hpp"
00055 #include "boost/numeric/ublas/triangular.hpp"
00056 #include "boost/numeric/ublas/matrix_expression.hpp"
00057
00058
00059 #include "viennacl/linalg/prod.hpp"
00060 #include "viennacl/matrix.hpp"
00061 #include "viennacl/compressed_matrix.hpp"
00062 #include "viennacl/linalg/compressed_matrix_operations.hpp"
00063 #include "viennacl/linalg/matrix_operations.hpp"
00064 #include "viennacl/scalar.hpp"
00065 #include "viennacl/linalg/inner_prod.hpp"
00066 #include "viennacl/linalg/ilu.hpp"
00067 #include "viennacl/ocl/backend.hpp"
00068 #include "viennacl/linalg/kernels/spai_source.h"
00069 #include "viennacl/linalg/kernels/spai_kernels.h"
00070
00071
00072 namespace viennacl
00073 {
00074 namespace linalg
00075 {
00076
00077 typedef viennacl::linalg::detail::spai::spai_tag spai_tag;
00078 typedef viennacl::linalg::detail::spai::fspai_tag fspai_tag;
00079
00084
00085 template <typename MatrixType>
00086 class spai_precond
00087 {
00088 public:
00089 typedef typename MatrixType::value_type ScalarType;
00090 typedef typename boost::numeric::ublas::vector<ScalarType> VectorType;
00095 spai_precond(const MatrixType& A,
00096 const spai_tag& tag): _tag(tag){
00097
00098
00099
00100
00101 MatrixType pA(A.size1(), A.size2());
00102 MatrixType At;
00103
00104 if(!_tag.getIsRight()){
00105 viennacl::linalg::detail::spai::sparse_transpose(A, At);
00106 }else{
00107 At = A;
00108 }
00109 pA = At;
00110 viennacl::linalg::detail::spai::initPreconditioner(pA, _spai_m);
00111 viennacl::linalg::detail::spai::computeSPAI(At, _spai_m, _tag);
00112
00113
00114
00115 }
00119 void apply(VectorType& vec) const {
00120 vec = viennacl::linalg::prod(_spai_m, vec);
00121 }
00122 private:
00123
00124 spai_tag _tag;
00125
00126 MatrixType _spai_m;
00127 };
00128
00129
00130 template <typename ScalarType, unsigned int MAT_ALIGNMENT>
00131 class spai_precond< viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> >
00132 {
00133 typedef viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> MatrixType;
00134 typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
00135 typedef viennacl::vector<ScalarType> VectorType;
00136 typedef viennacl::matrix<ScalarType> VCLDenseMatrixType;
00137
00138 typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
00139 public:
00140
00145 spai_precond(const MatrixType& A,
00146 const spai_tag& tag): _tag(tag)
00147 {
00148 viennacl::linalg::kernels::spai<ScalarType, 1>::init();
00149
00150 MatrixType At(A.size1(), A.size2());
00151 UBLASSparseMatrixType ubls_A, ubls_spai_m;
00152 UBLASSparseMatrixType ubls_At;
00153 viennacl::copy(A, ubls_A);;
00154 if(!_tag.getIsRight()){
00155 viennacl::linalg::detail::spai::sparse_transpose(ubls_A, ubls_At);
00156 }
00157 else{
00158 ubls_At = ubls_A;
00159 }
00160
00161
00162
00163 viennacl::linalg::detail::spai::initPreconditioner(ubls_At, ubls_spai_m);
00164 viennacl::copy(ubls_At, At);
00165 viennacl::linalg::detail::spai::computeSPAI(At, ubls_At, ubls_spai_m, _spai_m, _tag);
00166
00167
00168 }
00172 void apply(VectorType& vec) const {
00173 vec = viennacl::linalg::prod(_spai_m, vec);
00174 }
00175 private:
00176
00177 spai_tag _tag;
00178
00179 MatrixType _spai_m;
00180 };
00181
00182
00183
00184
00185
00186
00191
00192 template <typename MatrixType>
00193 class fspai_precond
00194 {
00195 typedef typename MatrixType::value_type ScalarType;
00196 typedef typename boost::numeric::ublas::vector<ScalarType> VectorType;
00197 typedef typename boost::numeric::ublas::matrix<ScalarType> UBLASDenseMatrixType;
00198 typedef typename viennacl::matrix<ScalarType> VCLMatrixType;
00199 public:
00200
00205 fspai_precond(const MatrixType& A,
00206 const fspai_tag& tag): tag_(tag)
00207 {
00208 MatrixType pA = A;
00209 viennacl::linalg::detail::spai::computeFSPAI(A, pA, L, L_trans, tag_);
00210 }
00211
00215 void apply(VectorType& vec) const
00216 {
00217 VectorType temp = viennacl::linalg::prod(L_trans, vec);
00218 vec = viennacl::linalg::prod(L, temp);
00219 }
00220
00221 private:
00222
00223 const fspai_tag & tag_;
00224
00225 MatrixType L;
00226 MatrixType L_trans;
00227 };
00228
00229
00230
00231
00232
00233
00234
00235
00236 template <typename ScalarType, unsigned int MAT_ALIGNMENT>
00237 class fspai_precond< viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> >
00238 {
00239 typedef viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> MatrixType;
00240 typedef viennacl::vector<ScalarType> VectorType;
00241 typedef viennacl::matrix<ScalarType> VCLDenseMatrixType;
00242 typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
00243 typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
00244 public:
00245
00250 fspai_precond(const MatrixType & A,
00251 const fspai_tag & tag): tag_(tag){
00252
00253 UBLASSparseMatrixType ublas_A(A.size1(), A.size2());
00254 UBLASSparseMatrixType pA(A.size1(), A.size2());
00255 UBLASSparseMatrixType ublas_L(A.size1(), A.size2());
00256 UBLASSparseMatrixType ublas_L_trans(A.size1(), A.size2());
00257 viennacl::copy(A, ublas_A);
00258
00259
00260
00261
00262 pA = ublas_A;
00263
00264 viennacl::linalg::detail::spai::computeFSPAI(ublas_A, pA, ublas_L, ublas_L_trans, tag_);
00265
00266 viennacl::copy(ublas_L, L);
00267 viennacl::copy(ublas_L_trans, L_trans);
00268 }
00269
00270
00274 void apply(VectorType& vec) const
00275 {
00276 VectorType temp(vec.size());
00277 temp = viennacl::linalg::prod(L_trans, vec);
00278 vec = viennacl::linalg::prod(L, temp);
00279 }
00280
00281 private:
00282
00283 const fspai_tag & tag_;
00284 MatrixType L;
00285 MatrixType L_trans;
00286 };
00287
00288
00289 }
00290 }
00291 #endif