ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spai.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_SPAI_HPP
2 #define VIENNACL_LINALG_SPAI_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2016, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
28 #include <utility>
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <algorithm>
33 #include <vector>
34 #include <math.h>
35 #include <map>
36 
37 // ViennaCL includes
39 #include "viennacl/linalg/qr.hpp"
40 #include "viennacl/linalg/prod.hpp"
48 
49 //boost includes
50 #include "boost/numeric/ublas/vector.hpp"
51 #include "boost/numeric/ublas/matrix.hpp"
52 #include "boost/numeric/ublas/matrix_proxy.hpp"
53 #include "boost/numeric/ublas/vector_proxy.hpp"
54 #include "boost/numeric/ublas/storage.hpp"
55 #include "boost/numeric/ublas/io.hpp"
56 #include "boost/numeric/ublas/lu.hpp"
57 #include "boost/numeric/ublas/triangular.hpp"
58 #include "boost/numeric/ublas/matrix_expression.hpp"
59 
60 
61 namespace viennacl
62 {
63  namespace linalg
64  {
65 
68 
73  //UBLAS version
74  template<typename MatrixType>
76  {
77  public:
78  typedef typename MatrixType::value_type ScalarType;
79  typedef typename boost::numeric::ublas::vector<ScalarType> VectorType;
84  spai_precond(const MatrixType& A,
85  const spai_tag& tag): tag_(tag){
86 
87  //VCLMatrixType vcl_Ap((unsigned int)A.size2(), (unsigned int)A.size1()), vcl_A((unsigned int)A.size1(), (unsigned int)A.size2()),
88  //vcl_At((unsigned int)A.size1(), (unsigned int)A.size2());
89  //UBLASDenseMatrixType dA = A;
90  MatrixType pA(A.size1(), A.size2());
91  MatrixType At;
92  //std::cout<<A<<std::endl;
93  if (!tag_.getIsRight()){
95  }else{
96  At = A;
97  }
98  pA = At;
101  //(At, pA, tag_.getIsRight(), tag_.getIsStatic(), (ScalarType)_tag.getResidualNormThreshold(), (unsigned int)_tag.getIterationLimit(),
102  //_spai_m);
103 
104  }
108  void apply(VectorType& vec) const {
109  vec = viennacl::linalg::prod(spai_m_, vec);
110  }
111  private:
112  // variables
113  spai_tag tag_;
114  // result of SPAI
115  MatrixType spai_m_;
116  };
117 
118  //VIENNACL version
123  template<typename ScalarType, unsigned int MAT_ALIGNMENT>
124  class spai_precond< viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> >
125  {
127  typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
130 
131  typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
132  public:
133 
139  const spai_tag& tag): tag_(tag), spai_m_(viennacl::traits::context(A))
140  {
141  viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());
143 
144  MatrixType At(A.size1(), A.size2(), viennacl::context(ctx));
145  UBLASSparseMatrixType ubls_A(A.size1(), A.size2()), ubls_spai_m;
146  UBLASSparseMatrixType ubls_At;
147  viennacl::copy(A, ubls_A);
148  if (!tag_.getIsRight()){
150  }
151  else{
152  ubls_At = ubls_A;
153  }
154  //current pattern is A
155  //pA = ubls_At;
156  //execute SPAI with ublas matrix types
158  viennacl::copy(ubls_At, At);
159  viennacl::linalg::detail::spai::computeSPAI(At, ubls_At, ubls_spai_m, spai_m_, tag_);
160  //viennacl::copy(ubls_spai_m, spai_m_);
161  tmp_.resize(A.size1(), viennacl::traits::context(A), false);
162  }
166  void apply(VectorType& vec) const {
167  tmp_ = viennacl::linalg::prod(spai_m_, vec);
168  vec = tmp_;
169  }
170  private:
171  // variables
172  spai_tag tag_;
173  // result of SPAI
174  MatrixType spai_m_;
175  mutable VectorType tmp_;
176  };
177 
178 
179  //
180  // FSPAI
181  //
182 
187  //UBLAS version
188  template<typename MatrixType>
190  {
191  typedef typename MatrixType::value_type ScalarType;
192  typedef typename boost::numeric::ublas::vector<ScalarType> VectorType;
193  typedef typename boost::numeric::ublas::matrix<ScalarType> UBLASDenseMatrixType;
195  public:
196 
201  fspai_precond(const MatrixType& A,
202  const fspai_tag& tag): tag_(tag)
203  {
204  MatrixType pA = A;
205  viennacl::linalg::detail::spai::computeFSPAI(A, pA, L, L_trans, tag_);
206  }
207 
211  void apply(VectorType& vec) const
212  {
213  VectorType temp = viennacl::linalg::prod(L_trans, vec);
214  vec = viennacl::linalg::prod(L, temp);
215  }
216 
217  private:
218  // variables
219  const fspai_tag & tag_;
220  // result of SPAI
221  MatrixType L;
222  MatrixType L_trans;
223  };
224 
225 
226 
227 
228 
229  //
230  // ViennaCL version
231  //
236  template<typename ScalarType, unsigned int MAT_ALIGNMENT>
237  class fspai_precond< viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> >
238  {
242  typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
243  typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
244  public:
245 
251  const fspai_tag & tag) : tag_(tag), L(viennacl::traits::context(A)), L_trans(viennacl::traits::context(A)), temp_apply_vec_(A.size1(), viennacl::traits::context(A))
252  {
253  //UBLASSparseMatrixType ubls_A;
254  UBLASSparseMatrixType ublas_A(A.size1(), A.size2());
255  UBLASSparseMatrixType pA(A.size1(), A.size2());
256  UBLASSparseMatrixType ublas_L(A.size1(), A.size2());
257  UBLASSparseMatrixType ublas_L_trans(A.size1(), A.size2());
258  viennacl::copy(A, ublas_A);
259  //viennacl::copy(ubls_A, vcl_A);
260  //vcl_At = viennacl::linalg::prod(vcl_A, vcl_A);
261  //vcl_pA = viennacl::linalg::prod(vcl_A, vcl_At);
262  //viennacl::copy(vcl_pA, pA);
263  pA = ublas_A;
264  //execute SPAI with ublas matrix types
265  viennacl::linalg::detail::spai::computeFSPAI(ublas_A, pA, ublas_L, ublas_L_trans, tag_);
266  //copy back to GPU
267  viennacl::copy(ublas_L, L);
268  viennacl::copy(ublas_L_trans, L_trans);
269  }
270 
271 
275  void apply(VectorType& vec) const
276  {
277  temp_apply_vec_ = viennacl::linalg::prod(L_trans, vec);
278  vec = viennacl::linalg::prod(L, temp_apply_vec_);
279  }
280 
281  private:
282  // variables
283  const fspai_tag & tag_;
284  MatrixType L;
285  MatrixType L_trans;
286  mutable VectorType temp_apply_vec_;
287  };
288 
289 
290  }
291 }
292 #endif
const vcl_size_t & size2() const
Returns the number of columns.
const vcl_size_t & size1() const
Returns the number of rows.
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
void sparse_transpose(MatrixT const &A_in, MatrixT &A)
Transposition of sparse matrix.
Definition: spai.hpp:640
Implementation of FSPAI. Experimental.
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
void computeSPAI(MatrixT const &A, MatrixT &M, spai_tag &tag)
Construction of SPAI preconditioner on CPU.
Definition: spai.hpp:686
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
A dense matrix class.
Definition: forwards.h:375
Main implementation of SPAI (not FSPAI). Experimental.
Implementation of a helper sparse vector class for SPAI. Experimental.
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:166
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:108
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
A tag for FSPAI. Experimental.
Definition: fspai.hpp:71
Implementation of a bunch of (small) matrices on GPU. Experimental.
Implementation of the Factored SParse Approximate Inverse Algorithm for a generic, uBLAS-compatible matrix type.
Definition: spai.hpp:189
spai_precond(const MatrixType &A, const spai_tag &tag)
Constructor.
Definition: spai.hpp:138
Implementation of a static SPAI. Experimental.
viennacl::linalg::detail::spai::fspai_tag fspai_tag
Definition: spai.hpp:67
Implementation of a bunch of vectors on GPU. Experimental.
Implementation of the SParse Approximate Inverse Algorithm for a generic, uBLAS-compatible matrix typ...
Definition: spai.hpp:75
Provides a QR factorization using a block-based approach.
Implementation of the spai tag holding SPAI configuration parameters. Experimental.
spai_precond(const MatrixType &A, const spai_tag &tag)
Constructor.
Definition: spai.hpp:84
MatrixType::value_type ScalarType
Definition: spai.hpp:78
fspai_precond(const MatrixType &A, const fspai_tag &tag)
Constructor.
Definition: spai.hpp:201
boost::numeric::ublas::vector< ScalarType > VectorType
Definition: spai.hpp:79
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
fspai_precond(const MatrixType &A, const fspai_tag &tag)
Constructor.
Definition: spai.hpp:250
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:275
static void init(viennacl::ocl::context &ctx)
Definition: spai.hpp:594
void computeFSPAI(MatrixT const &A, MatrixT const &PatternA, MatrixT &L, MatrixT &L_trans, fspai_tag)
Definition: fspai.hpp:313
A sparse square matrix in compressed sparse rows format.
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:211
void initPreconditioner(SparseMatrixT const &A, SparseMatrixT &M)
Initialize preconditioner with sparcity pattern = p(A)
Implementation of a dynamic SPAI. Provides the routines for automatic pattern updates Experimental...
viennacl::linalg::detail::spai::spai_tag spai_tag
Definition: spai.hpp:66