ViennaCL - The Vienna Computing Library: viennacl/linalg/spai.hpp Source File

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
38 #include "viennacl/linalg/detail/spai/spai_tag.hpp"
39 #include "viennacl/linalg/qr.hpp"
40 #include "viennacl/linalg/prod.hpp"
41 #include "viennacl/linalg/detail/spai/spai-dynamic.hpp"
42 #include "viennacl/linalg/detail/spai/spai-static.hpp"
43 #include "viennacl/linalg/detail/spai/sparse_vector.hpp"
44 #include "viennacl/linalg/detail/spai/block_matrix.hpp"
45 #include "viennacl/linalg/detail/spai/block_vector.hpp"
46 #include "viennacl/linalg/detail/spai/fspai.hpp"
47 #include "viennacl/linalg/detail/spai/spai.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
66 typedef viennacl::linalg::detail::spai::spai_tag spai_tag;
67 typedef viennacl::linalg::detail::spai::fspai_tag fspai_tag;
68
73 //UBLAS version
74 template<typename MatrixType>
75 class spai_precond
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()){
94 viennacl::linalg::detail::spai::sparse_transpose(A, At);
95 }else{
96 At = A;
97 }
98 pA = At;
99 viennacl::linalg::detail::spai::initPreconditioner(pA, spai_m_);
100 viennacl::linalg::detail::spai::computeSPAI(At, spai_m_, tag_);
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 {
126 typedef viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> MatrixType;
127 typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
128 typedef viennacl::vector<ScalarType> VectorType;
129 typedef viennacl::matrix<ScalarType> VCLDenseMatrixType;
130
131 typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
132 public:
133
138 spai_precond(const MatrixType& A,
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());
142 viennacl::linalg::opencl::kernels::spai<ScalarType>::init(ctx);
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()){
149 viennacl::linalg::detail::spai::sparse_transpose(ubls_A, ubls_At);
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
157 viennacl::linalg::detail::spai::initPreconditioner(ubls_At, ubls_spai_m);
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>
189 class fspai_precond
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;
194 typedef typename viennacl::matrix<ScalarType> VCLMatrixType;
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 {
239 typedef viennacl::compressed_matrix<ScalarType, MAT_ALIGNMENT> MatrixType;
240 typedef viennacl::vector<ScalarType> VectorType;
241 typedef viennacl::matrix<ScalarType> VCLDenseMatrixType;
242 typedef boost::numeric::ublas::compressed_matrix<ScalarType> UBLASSparseMatrixType;
243 typedef boost::numeric::ublas::vector<ScalarType> UBLASVectorType;
244 public:
245
250 fspai_precond(const MatrixType & A,
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
viennacl::compressed_matrix::size2
const vcl_size_t & size2() const
Returns the number of columns.
Definition: compressed_matrix.hpp:929
viennacl::compressed_matrix::size1
const vcl_size_t & size1() const
Returns the number of rows.
Definition: compressed_matrix.hpp:927
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
viennacl::linalg::detail::spai::sparse_transpose
void sparse_transpose(MatrixT const &A_in, MatrixT &A)
Transposition of sparse matrix.
Definition: spai.hpp:640
fspai.hpp
Implementation of FSPAI. Experimental.
viennacl::traits::size1
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
viennacl::linalg::detail::spai::computeSPAI
void computeSPAI(MatrixT const &A, MatrixT &M, spai_tag &tag)
Construction of SPAI preconditioner on CPU.
Definition: spai.hpp:686
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
spai.hpp
Main implementation of SPAI (not FSPAI). Experimental.
viennacl::linalg::detail::spai::spai_tag
A tag for SPAI.
Definition: spai_tag.hpp:64
sparse_vector.hpp
Implementation of a helper sparse vector class for SPAI. Experimental.
viennacl::linalg::spai_precond< viennacl::compressed_matrix< ScalarType, MAT_ALIGNMENT > >::apply
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:166
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::linalg::spai_precond::apply
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:108
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
viennacl::linalg::detail::spai::fspai_tag
A tag for FSPAI. Experimental.
Definition: fspai.hpp:71
block_matrix.hpp
Implementation of a bunch of (small) matrices on GPU. Experimental.
viennacl::linalg::fspai_precond
Implementation of the Factored SParse Approximate Inverse Algorithm for a generic, uBLAS-compatible matrix type.
Definition: spai.hpp:189
viennacl::linalg::spai_precond< viennacl::compressed_matrix< ScalarType, MAT_ALIGNMENT > >::spai_precond
spai_precond(const MatrixType &A, const spai_tag &tag)
Constructor.
Definition: spai.hpp:138
spai-static.hpp
Implementation of a static SPAI. Experimental.
viennacl::linalg::fspai_tag
viennacl::linalg::detail::spai::fspai_tag fspai_tag
Definition: spai.hpp:67
viennacl::linalg::detail::spai::spai_tag::getIsRight
bool getIsRight() const
Definition: spai_tag.hpp:94
block_vector.hpp
Implementation of a bunch of vectors on GPU. Experimental.
viennacl::linalg::spai_precond
Implementation of the SParse Approximate Inverse Algorithm for a generic, uBLAS-compatible matrix typ...
Definition: spai.hpp:75
qr.hpp
Provides a QR factorization using a block-based approach.
spai_tag.hpp
Implementation of the spai tag holding SPAI configuration parameters. Experimental.
viennacl::linalg::spai_precond::spai_precond
spai_precond(const MatrixType &A, const spai_tag &tag)
Constructor.
Definition: spai.hpp:84
viennacl::vector< ScalarType >
viennacl::linalg::spai_precond::ScalarType
MatrixType::value_type ScalarType
Definition: spai.hpp:78
viennacl::linalg::fspai_precond::fspai_precond
fspai_precond(const MatrixType &A, const fspai_tag &tag)
Constructor.
Definition: spai.hpp:201
viennacl::linalg::spai_precond::VectorType
boost::numeric::ublas::vector< ScalarType > VectorType
Definition: spai.hpp:79
viennacl::traits::context
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
viennacl::linalg::fspai_precond< viennacl::compressed_matrix< ScalarType, MAT_ALIGNMENT > >::fspai_precond
fspai_precond(const MatrixType &A, const fspai_tag &tag)
Constructor.
Definition: spai.hpp:250
viennacl::copy
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) ...
Definition: circulant_matrix.hpp:150
viennacl::linalg::fspai_precond< viennacl::compressed_matrix< ScalarType, MAT_ALIGNMENT > >::apply
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:275
viennacl::linalg::opencl::kernels::spai::init
static void init(viennacl::ocl::context &ctx)
Definition: spai.hpp:594
viennacl::linalg::detail::spai::computeFSPAI
void computeFSPAI(MatrixT const &A, MatrixT const &PatternA, MatrixT &L, MatrixT &L_trans, fspai_tag)
Definition: fspai.hpp:313
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
viennacl::linalg::fspai_precond::apply
void apply(VectorType &vec) const
Application of current preconditioner, multiplication on the right-hand side vector.
Definition: spai.hpp:211
viennacl::linalg::detail::spai::initPreconditioner
void initPreconditioner(SparseMatrixT const &A, SparseMatrixT &M)
Initialize preconditioner with sparcity pattern = p(A)
Definition: spai-static.hpp:154
spai-dynamic.hpp
Implementation of a dynamic SPAI. Provides the routines for automatic pattern updates Experimental...
viennacl::linalg::spai_tag
viennacl::linalg::detail::spai::spai_tag spai_tag
Definition: spai.hpp:66