1 #ifndef VIENNACL_LINALG_CG_HPP_
2 #define VIENNACL_LINALG_CG_HPP_
64 unsigned int iters()
const {
return iters_taken_; }
65 void iters(
unsigned int i)
const { iters_taken_ = i; }
68 double error()
const {
return last_error_; }
70 void error(
double e)
const { last_error_ = e; }
75 unsigned int iterations_;
78 mutable unsigned int iters_taken_;
79 mutable double last_error_;
86 template<
typename VectorT,
typename PreconditionerT>
90 VectorT &
get() {
return z_; }
95 template<
typename VectorT>
98 z_handler(VectorT & residual) : presidual_(&residual){ }
99 VectorT &
get() {
return *presidual_; }
101 VectorT * presidual_;
116 template<
typename MatrixT,
typename NumericT>
131 std::vector<NumericT> host_inner_prod_buffer(inner_prod_buffer.
size());
133 difference_type buffer_offset_per_vector =
static_cast<difference_type
>(buffer_size_per_vector);
137 if (!norm_rhs_squared)
140 NumericT inner_prod_rr = norm_rhs_squared;
143 NumericT inner_prod_ApAp = 0;
144 NumericT inner_prod_pAp = 0;
156 inner_prod_rr = std::accumulate(host_inner_prod_buffer.begin(), host_inner_prod_buffer.begin() + buffer_offset_per_vector, NumericT(0));
157 inner_prod_ApAp = std::accumulate(host_inner_prod_buffer.begin() + buffer_offset_per_vector, host_inner_prod_buffer.begin() + 2 * buffer_offset_per_vector, NumericT(0));
158 inner_prod_pAp = std::accumulate(host_inner_prod_buffer.begin() + 2 * buffer_offset_per_vector, host_inner_prod_buffer.begin() + 3 * buffer_offset_per_vector, NumericT(0));
163 alpha = inner_prod_rr / inner_prod_pAp;
164 beta = (alpha*alpha*inner_prod_ApAp - inner_prod_rr) / inner_prod_rr;
168 tag.
error(std::sqrt(std::fabs(inner_prod_rr) / norm_rhs_squared));
183 template<
typename MatrixT,
typename VectorT,
typename PreconditionerT>
184 VectorT
solve(MatrixT
const &
matrix, VectorT
const & rhs,
cg_tag const & tag, PreconditionerT
const & precond)
189 VectorT result = rhs;
192 VectorT residual = rhs;
195 VectorT & z = zhandler.
get();
201 CPU_NumericType alpha;
202 CPU_NumericType new_ip_rr = 0;
203 CPU_NumericType beta;
204 CPU_NumericType norm_rhs_squared = ip_rr;
205 CPU_NumericType new_ipp_rr_over_norm_rhs;
207 if (norm_rhs_squared <= 0)
218 residual -= alpha * tmp;
227 new_ipp_rr_over_norm_rhs = new_ip_rr / norm_rhs_squared;
231 beta = new_ip_rr / ip_rr;
238 tag.
error(std::sqrt(std::fabs(new_ip_rr / norm_rhs_squared)));
243 template<
typename MatrixT,
typename VectorT>
T norm_2(std::vector< T, A > const &v1)
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
cg_tag(double tol=1e-8, unsigned int max_iterations=300)
The constructor.
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...
double tolerance() const
Returns the relative tolerance.
handles the no_precond case at minimal overhead
void clear(VectorType &vec)
Generic routine for setting all entries of a vector to zero. This is the version for non-ViennaCL obj...
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)
void pipelined_cg_vector_update(vector_base< NumericT > &result, NumericT alpha, vector_base< NumericT > &p, vector_base< NumericT > &r, vector_base< NumericT > const &Ap, NumericT beta, vector_base< NumericT > &inner_prod_buffer)
Performs a joint vector update operation needed for an efficient pipelined CG algorithm.
unsigned int iters() const
Return the number of solver iterations:
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
void pipelined_cg_prod(MatrixT const &A, vector_base< NumericT > const &p, vector_base< NumericT > &Ap, vector_base< NumericT > &inner_prod_buffer)
Performs a joint vector update operation needed for an efficient pipelined CG algorithm.
unsigned int max_iterations() const
Returns the maximum number of iterations.
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
A tag class representing the use of no preconditioner.
Implementations of incomplete factorization preconditioners. Convenience header file.
base_type::difference_type difference_type
z_handler(VectorT &residual)
Generic clear functionality for different vector and matrix types.
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
double error() const
Returns the estimated relative error at the end of the solver run.
z_handler(VectorT &residual)
Implementations of specialized routines for the iterative solvers.
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
void iters(unsigned int i) const
void error(double e) const
Sets the estimated relative error at the end of the solver run.
size_type size() const
Returns the length of the vector (cf. std::vector)
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
A collection of compile time type deductions.
viennacl::vector< NumericT > solve(MatrixT const &A, viennacl::vector_base< NumericT > const &rhs, bicgstab_tag const &tag, viennacl::linalg::no_precond)
Implementation of a pipelined stabilized Bi-conjugate gradient solver.
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)