1 #ifndef VIENNACL_LINALG_CG_HPP_
2 #define VIENNACL_LINALG_CG_HPP_
61 unsigned int iters()
const {
return iters_taken_; }
62 void iters(
unsigned int i)
const { iters_taken_ = i; }
65 double error()
const {
return last_error_; }
67 void error(
double e)
const { last_error_ = e; }
72 unsigned int iterations_;
75 mutable unsigned int iters_taken_;
76 mutable double last_error_;
89 template <
typename MatrixType,
typename VectorType>
96 VectorType result = rhs;
99 VectorType residual = rhs;
101 VectorType tmp = rhs;
104 CPU_ScalarType alpha;
105 CPU_ScalarType new_ip_rr = 0;
107 CPU_ScalarType norm_rhs = std::sqrt(ip_rr);
120 residual -= alpha * tmp;
123 if (new_ip_rr / norm_rhs < tag.
tolerance())
125 new_ip_rr *= new_ip_rr;
127 beta = new_ip_rr / ip_rr;
130 p = residual + beta * p;
134 tag.
error(std::sqrt(new_ip_rr) / norm_rhs);
139 template <
typename MatrixType,
typename VectorType>
142 return solve(matrix, rhs, tag);
155 template <
typename MatrixType,
typename VectorType,
typename PreconditionerType>
156 VectorType
solve(
const MatrixType &
matrix, VectorType
const & rhs,
cg_tag const & tag, PreconditionerType
const & precond)
161 VectorType result = rhs;
164 VectorType residual = rhs;
165 VectorType tmp = rhs;
172 CPU_ScalarType alpha;
173 CPU_ScalarType new_ip_rr = 0;
175 CPU_ScalarType norm_rhs_squared = ip_rr;
176 CPU_ScalarType new_ipp_rr_over_norm_rhs;
178 if (norm_rhs_squared == 0)
189 residual -= alpha * tmp;
194 new_ipp_rr_over_norm_rhs = new_ip_rr / norm_rhs_squared;
198 beta = new_ip_rr / ip_rr;
205 tag.
error(std::sqrt(std::fabs(new_ip_rr / norm_rhs_squared)));
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
void error(double e) const
Sets the estimated relative error at the end of the solver run.
Definition: cg.hpp:67
double tolerance() const
Returns the relative tolerance.
Definition: cg.hpp:56
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
Generic size and resize functionality for different vector and matrix types.
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
cg_tag(double tol=1e-8, unsigned int max_iterations=300)
The constructor.
Definition: cg.hpp:53
A dense matrix class.
Definition: forwards.h:293
void clear(VectorType &vec)
Generic routine for setting all entries of a vector to zero. This is the version for non-ViennaCL obj...
Definition: clear.hpp:57
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:89
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.
Definition: cpu_ram.hpp:29
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
A tag class representing the use of no preconditioner.
Definition: forwards.h:727
Implementations of incomplete factorization preconditioners. Convenience header file.
void iters(unsigned int i) const
Definition: cg.hpp:62
unsigned int iters() const
Return the number of solver iterations:
Definition: cg.hpp:61
T::value_type type
Definition: result_of.hpp:230
Generic clear functionality for different vector and matrix types.
unsigned int max_iterations() const
Returns the maximum number of iterations.
Definition: cg.hpp:58
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:276
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
Definition: cg.hpp:45
double error() const
Returns the estimated relative error at the end of the solver run.
Definition: cg.hpp:65
VectorType solve(const MatrixType &matrix, VectorType const &rhs, bicgstab_tag const &tag)
Implementation of the stabilized Bi-conjugate gradient solver.
Definition: bicgstab.hpp:92
A collection of compile time type deductions.