ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
jacobi_precond.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_JACOBI_PRECOND_HPP_
2 #define VIENNACL_LINALG_JACOBI_PRECOND_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, 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 
25 #include <vector>
26 #include <cmath>
27 #include "viennacl/forwards.h"
28 #include "viennacl/vector.hpp"
30 #include "viennacl/tools/tools.hpp"
33 
34 #include <map>
35 
36 namespace viennacl
37 {
38 namespace linalg
39 {
40 
43 class jacobi_tag {};
44 
45 
48 template<typename MatrixT,
49  bool is_viennacl = detail::row_scaling_for_viennacl<MatrixT>::value >
51 {
52  typedef typename MatrixT::value_type NumericType;
53 
54  public:
55  jacobi_precond(MatrixT const & mat, jacobi_tag const &) : diag_A_(viennacl::traits::size1(mat))
56  {
57  init(mat);
58  }
59 
60  void init(MatrixT const & mat)
61  {
62  diag_A_.resize(viennacl::traits::size1(mat)); //resize without preserving values
63 
64  for (typename MatrixT::const_iterator1 row_it = mat.begin1();
65  row_it != mat.end1();
66  ++row_it)
67  {
68  bool diag_found = false;
69  for (typename MatrixT::const_iterator2 col_it = row_it.begin();
70  col_it != row_it.end();
71  ++col_it)
72  {
73  if (col_it.index1() == col_it.index2())
74  {
75  diag_A_[col_it.index1()] = *col_it;
76  diag_found = true;
77  }
78  }
79  if (!diag_found)
80  throw zero_on_diagonal_exception("ViennaCL: Zero in diagonal encountered while setting up Jacobi preconditioner!");
81  }
82  }
83 
84 
86  template<typename VectorT>
87  void apply(VectorT & vec) const
88  {
89  assert(viennacl::traits::size(diag_A_) == viennacl::traits::size(vec) && bool("Size mismatch"));
90  for (vcl_size_t i=0; i<diag_A_.size(); ++i)
91  vec[i] /= diag_A_[i];
92  }
93 
94  private:
95  std::vector<NumericType> diag_A_;
96 };
97 
98 
103 template<typename MatrixT>
104 class jacobi_precond<MatrixT, true>
105 {
107 
108  public:
109  jacobi_precond(MatrixT const & mat, jacobi_tag const &) : diag_A_(mat.size1(), viennacl::traits::context(mat))
110  {
111  init(mat);
112  }
113 
114 
115  void init(MatrixT const & mat)
116  {
118  }
119 
120 
121  template<unsigned int AlignmentV>
123  {
124  assert(viennacl::traits::size(diag_A_) == viennacl::traits::size(vec) && bool("Size mismatch"));
125  vec = element_div(vec, diag_A_);
126  }
127 
128  private:
130 };
131 
132 }
133 }
134 
135 
136 
137 
138 #endif
139 
140 
141 
viennacl::vector_expression< const vector_base< T >, const vector_base< T >, op_element_binary< op_div > > element_div(vector_base< T > const &v1, vector_base< T > const &v2)
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Various little tools used here and there in ViennaCL.
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
This file provides the forward declarations for the main types used within ViennaCL.
A tag for a jacobi preconditioner.
void apply(viennacl::vector< NumericType, AlignmentV > &vec) const
void init(MatrixT const &mat)
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
Meta function which checks whether a tag is tag_viennacl.
Definition: tag_of.hpp:375
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:235
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
Implementation of the compressed_matrix class.
Implementations of operations using sparse matrices.
void apply(VectorT &vec) const
Apply to res = b - Ax, i.e. jacobi applied vec (right hand side),.
std::size_t vcl_size_t
Definition: forwards.h:75
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
A row normalization preconditioner is implemented here.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
viennacl::enable_if< viennacl::is_any_sparse_matrix< SparseMatrixType >::value >::type row_info(SparseMatrixType const &mat, vector< SCALARTYPE, VEC_ALIGNMENT > &vec, row_info_types info_selector)