ViennaCL - The Vienna Computing Library: viennacl/linalg/jacobi_precond.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
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-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
25 #include <vector>
26 #include <cmath>
27 #include "viennacl/forwards.h"
28 #include "viennacl/vector.hpp"
29 #include "viennacl/compressed_matrix.hpp"
30 #include "viennacl/tools/tools.hpp"
31 #include "viennacl/linalg/sparse_matrix_operations.hpp"
32 #include "viennacl/linalg/row_scaling.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 >
50 class jacobi_precond
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 {
106 typedef typename viennacl::result_of::cpu_value_type<typename MatrixT::value_type>::type NumericType;
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 {
117 detail::row_info(mat, diag_A_, detail::SPARSE_ROW_DIAGONAL);
118 }
119
120
121 template<unsigned int AlignmentV>
122 void apply(viennacl::vector<NumericType, AlignmentV> & vec) const
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:
129 viennacl::vector<NumericType> diag_A_;
130 };
131
132 }
133 }
134
135
136
137
138 #endif
139
140
141
viennacl::linalg::element_div
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)
viennacl::zero_on_diagonal_exception
Definition: forwards.h:597
viennacl::linalg::jacobi_precond
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Definition: jacobi_precond.hpp:50
tools.hpp
Various little tools used here and there in ViennaCL.
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
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::linalg::jacobi_tag
A tag for a jacobi preconditioner.
Definition: jacobi_precond.hpp:43
viennacl::linalg::jacobi_precond< MatrixT, true >::apply
void apply(viennacl::vector< NumericType, AlignmentV > &vec) const
Definition: jacobi_precond.hpp:122
viennacl::linalg::jacobi_precond::init
void init(MatrixT const &mat)
Definition: jacobi_precond.hpp:60
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::is_viennacl
Meta function which checks whether a tag is tag_viennacl.
Definition: tag_of.hpp:375
viennacl::linalg::jacobi_precond< MatrixT, true >::init
void init(MatrixT const &mat)
Definition: jacobi_precond.hpp:115
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
viennacl::linalg::jacobi_precond::jacobi_precond
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:55
compressed_matrix.hpp
Implementation of the compressed_matrix class.
sparse_matrix_operations.hpp
Implementations of operations using sparse matrices.
viennacl::linalg::jacobi_precond::apply
void apply(VectorT &vec) const
Apply to res = b - Ax, i.e. jacobi applied vec (right hand side),.
Definition: jacobi_precond.hpp:87
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector
Definition: forwards.h:266
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
row_scaling.hpp
A row normalization preconditioner is implemented here.
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::linalg::detail::SPARSE_ROW_DIAGONAL
Definition: forwards.h:842
viennacl::linalg::jacobi_precond< MatrixT, true >::jacobi_precond
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:109
viennacl::linalg::detail::row_info
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