ViennaCL - The Vienna Computing Library  1.5.2
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-2014, 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 PDF 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 MatrixType,
49  bool is_viennacl = detail::row_scaling_for_viennacl<MatrixType>::value >
51  {
52  typedef typename MatrixType::value_type ScalarType;
53 
54  public:
55  jacobi_precond(MatrixType const & mat, jacobi_tag const &) : diag_A(viennacl::traits::size1(mat))
56  {
57  init(mat);
58  }
59 
60  void init(MatrixType const & mat)
61  {
62  diag_A.resize(viennacl::traits::size1(mat)); //resize without preserving values
63 
64  for (typename MatrixType::const_iterator1 row_it = mat.begin1();
65  row_it != mat.end1();
66  ++row_it)
67  {
68  bool diag_found = false;
69  for (typename MatrixType::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 "ViennaCL: Zero in diagonal encountered while setting up Jacobi preconditioner!";
81  }
82  }
83 
84 
86  template <typename VectorType>
87  void apply(VectorType & 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<ScalarType> diag_A;
96  };
97 
98 
103  template <typename MatrixType>
104  class jacobi_precond< MatrixType, true>
105  {
107 
108  public:
109  jacobi_precond(MatrixType const & mat, jacobi_tag const &) : diag_A(mat.size1(), viennacl::traits::context(mat))
110  {
111  init(mat);
112  }
113 
114 
115  void init(MatrixType const & mat)
116  {
118  }
119 
120 
121  template <unsigned int ALIGNMENT>
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)
std::size_t vcl_size_t
Definition: forwards.h:58
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Definition: jacobi_precond.hpp:50
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:216
This file provides the forward declarations for the main types used within ViennaCL.
A tag for a jacobi preconditioner.
Definition: jacobi_precond.hpp:43
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:364
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:144
Implementation of the compressed_matrix class.
Implementations of operations using sparse matrices.
void init(MatrixType const &mat)
Definition: jacobi_precond.hpp:60
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
A row normalization preconditioner is implemented here.
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:276
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
void apply(viennacl::vector< ScalarType, ALIGNMENT > &vec) const
Definition: jacobi_precond.hpp:122
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)
Definition: sparse_matrix_operations.hpp:50
jacobi_precond(MatrixType const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:109
jacobi_precond(MatrixType const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:55
void apply(VectorType &vec) const
Apply to res = b - Ax, i.e. jacobi applied vec (right hand side),.
Definition: jacobi_precond.hpp:87
void init(MatrixType const &mat)
Definition: jacobi_precond.hpp:115