1 #ifndef VIENNACL_LINALG_DETAIL_SPAI_FSPAI_HPP
2 #define VIENNACL_LINALG_DETAIL_SPAI_FSPAI_HPP
31 #include "boost/numeric/ublas/vector.hpp"
32 #include "boost/numeric/ublas/matrix.hpp"
33 #include "boost/numeric/ublas/matrix_proxy.hpp"
34 #include "boost/numeric/ublas/vector_proxy.hpp"
35 #include "boost/numeric/ublas/storage.hpp"
36 #include "boost/numeric/ublas/io.hpp"
37 #include "boost/numeric/ublas/lu.hpp"
38 #include "boost/numeric/ublas/triangular.hpp"
39 #include "boost/numeric/ublas/matrix_expression.hpp"
82 double residual_norm_threshold = 1e-3,
83 unsigned int iteration_limit = 5,
84 bool is_static =
false,
85 bool is_right =
false)
86 : residual_norm_threshold_(residual_norm_threshold),
87 iteration_limit_(iteration_limit),
88 is_static_(is_static),
89 is_right_(is_right) {}
97 if (residual_norm_threshold > 0)
98 residual_norm_threshold_ = residual_norm_threshold;
102 if (iteration_limit > 0)
103 iteration_limit_ = iteration_limit;
105 inline void setIsRight(
bool is_right) { is_right_ = is_right; }
106 inline void setIsStatic(
bool is_static) { is_static_ = is_static; }
109 double residual_norm_threshold_;
110 unsigned long iteration_limit_;
120 template<
typename MatrixT,
typename NumericT>
123 STL_A.resize(A.size1());
124 for (
typename MatrixT::const_iterator1 row_it = A.begin1();
128 for (
typename MatrixT::const_iterator2 col_it = row_it.begin();
129 col_it != row_it.end();
132 if (col_it.index1() >= col_it.index2())
133 STL_A[col_it.index1()][
static_cast<unsigned int>(col_it.index2())] = *col_it;
144 template<
typename MatrixT>
145 void generateJ(MatrixT
const & A, std::vector<std::vector<vcl_size_t> > & J)
147 for (
typename MatrixT::const_iterator1 row_it = A.begin1();
151 for (
typename MatrixT::const_iterator2 col_it = row_it.begin();
152 col_it != row_it.end();
155 if (col_it.index1() > col_it.index2())
157 J[col_it.index2()].push_back(col_it.index1());
158 J[col_it.index1()].push_back(col_it.index2());
171 template<
typename NumericT,
typename MatrixT,
typename VectorT>
172 void fill_blocks(std::vector< std::map<unsigned int, NumericT> > & A,
173 std::vector<MatrixT> & blocks,
174 std::vector<std::vector<vcl_size_t> >
const & J,
175 std::vector<VectorT> & Y)
179 std::vector<vcl_size_t>
const & Jk = J[k];
181 MatrixT & block_k = blocks[k];
183 yk.resize(Jk.size());
184 block_k.resize(Jk.size(), Jk.size());
190 std::map<unsigned int, NumericT> & A_row = A[row_index];
193 yk[i] = A_row[
static_cast<unsigned int>(k)];
198 if (col_index <= row_index && A_row.find(static_cast<unsigned int>(col_index)) != A_row.end())
199 block_k(i, j) = A_row[
static_cast<unsigned int>(col_index)];
209 template<
typename MatrixT>
216 std::cout <<
"k: " << k << std::endl;
217 std::cout <<
"A(k,k): " << A(k,k) << std::endl;
220 assert(A(k,k) > 0 &&
bool(
"Matrix not positive definite in Cholesky factorization."));
222 A(k,k) = std::sqrt(A(k,k));
228 A(i,j) -= A(i,k) * A(j,k);
237 template<
typename MatrixT,
typename VectorT>
244 b[i] -= L(i,j) * b[j];
252 b[i] -= L(k,i) * b[k];
265 template<
typename MatrixT,
typename VectorT>
269 std::vector<VectorT> & Y,
270 std::vector<std::vector<vcl_size_t> > & J)
272 typedef typename VectorT::value_type NumericType;
273 typedef std::vector<std::map<unsigned int, NumericType> > STLSparseMatrixType;
275 STLSparseMatrixType L_temp(A.size1());
279 std::vector<vcl_size_t>
const & Jk = J[k];
280 VectorT
const & yk = Y[k];
283 NumericType Lkk = A(k,k);
285 Lkk -= A(Jk[i],k) * yk[i];
287 Lkk = NumericType(1) / std::sqrt(Lkk);
288 L_temp[k][
static_cast<unsigned int>(k)] = Lkk;
294 L_temp[Jk[i]][
static_cast<unsigned int>(k)] = -Lkk * yk[i];
295 L_trans(k, Jk[i]) = -Lkk * yk[i];
302 for (
typename std::map<unsigned int, NumericType>::const_iterator it = L_temp[i].begin();
303 it != L_temp[i].end();
305 L(i, it->first) = it->second;
312 template<
typename MatrixT>
314 MatrixT
const & PatternA,
319 typedef typename MatrixT::value_type
NumericT;
320 typedef boost::numeric::ublas::matrix<NumericT> DenseMatrixType;
321 typedef std::vector<std::map<unsigned int, NumericT> > SparseMatrixType;
327 std::vector<std::vector<NumericT> > y_k(A.size1());
328 SparseMatrixType STL_A(A.size1());
336 std::vector<std::vector<vcl_size_t> > J(A.size1());
343 std::vector<DenseMatrixType> subblocks_A(A.size1());
351 for (
vcl_size_t i=0; i<subblocks_A.size(); ++i)
370 if (subblocks_A[i].
size1() > 0)
386 L.resize(A.size1(), A.size2(),
false);
387 L.reserve(A.nnz(),
false);
388 L_trans.resize(A.size1(), A.size2(),
false);
389 L_trans.reserve(A.nnz(),
false);
Implementations of dense matrix related operations including matrix-vector products.
void cholesky_decompose(MatrixT &A)
void sym_sparse_matrix_to_stl(MatrixT const &A, std::vector< std::map< unsigned int, NumericT > > &STL_A)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Implementation of the dense matrix class.
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
void cholesky_solve(MatrixT const &L, VectorT &b)
A tag for FSPAI. Experimental.
void setIterationLimit(unsigned long iteration_limit)
Implementations of incomplete factorization preconditioners. Convenience header file.
Implementation of the compressed_matrix class.
double getResidualNormThreshold() const
Implementations of operations using sparse matrices.
unsigned long getIterationLimit() const
The conjugate gradient method is implemented here.
void computeL(MatrixT const &A, MatrixT &L, MatrixT &L_trans, std::vector< VectorT > &Y, std::vector< std::vector< vcl_size_t > > &J)
void setIsStatic(bool is_static)
void generateJ(MatrixT const &A, std::vector< std::vector< vcl_size_t > > &J)
void computeFSPAI(MatrixT const &A, MatrixT const &PatternA, MatrixT &L, MatrixT &L_trans, fspai_tag)
void fill_blocks(std::vector< std::map< unsigned int, NumericT > > &A, std::vector< MatrixT > &blocks, std::vector< std::vector< vcl_size_t > > const &J, std::vector< VectorT > &Y)
void setIsRight(bool is_right)
Implementation of the ViennaCL scalar class.
fspai_tag(double residual_norm_threshold=1e-3, unsigned int iteration_limit=5, bool is_static=false, bool is_right=false)
Constructor.
void setResidualNormThreshold(double residual_norm_threshold)