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_;
119 template<
typename MatrixT,
typename NumericT>
134 std::vector<NumericT> host_inner_prod_buffer(inner_prod_buffer.
size());
136 difference_type buffer_offset_per_vector =
static_cast<difference_type
>(buffer_size_per_vector);
140 if (!norm_rhs_squared)
143 NumericT inner_prod_rr = norm_rhs_squared;
146 NumericT inner_prod_ApAp = 0;
147 NumericT inner_prod_pAp = 0;
159 inner_prod_rr = std::accumulate(host_inner_prod_buffer.begin(), host_inner_prod_buffer.begin() + buffer_offset_per_vector, NumericT(0));
160 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));
161 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));
166 alpha = inner_prod_rr / inner_prod_pAp;
167 beta = (alpha*alpha*inner_prod_ApAp - inner_prod_rr) / inner_prod_rr;
171 tag.
error(std::sqrt(std::fabs(inner_prod_rr) / norm_rhs_squared));
180 template<
typename NumericT>
190 template<
typename NumericT>
200 template<
typename NumericT>
210 template<
typename NumericT>
222 template<
typename NumericT>
232 template<
typename NumericT>
242 template<
typename NumericT>
252 template<
typename NumericT>
264 template<
typename NumericT>
274 template<
typename NumericT>
284 template<
typename NumericT>
294 template<
typename NumericT>
308 template<
typename NumericT>
318 template<
typename NumericT>
328 template<
typename NumericT>
338 template<
typename NumericT>
351 template<
typename NumericT>
361 template<
typename NumericT>
371 template<
typename NumericT>
381 template<
typename NumericT>
407 template<
typename MatrixT,
typename VectorT,
typename PreconditionerT>
408 VectorT
solve(MatrixT
const &
matrix, VectorT
const & rhs,
cg_tag const & tag, PreconditionerT
const & precond)
413 VectorT result = rhs;
416 VectorT residual = rhs;
419 VectorT & z = zhandler.
get();
425 CPU_NumericType alpha;
426 CPU_NumericType new_ip_rr = 0;
427 CPU_NumericType beta;
428 CPU_NumericType norm_rhs_squared = ip_rr;
429 CPU_NumericType new_ipp_rr_over_norm_rhs;
431 if (norm_rhs_squared <= 0)
442 residual -= alpha * tmp;
451 new_ipp_rr_over_norm_rhs = new_ip_rr / norm_rhs_squared;
455 beta = new_ip_rr / ip_rr;
462 tag.
error(std::sqrt(std::fabs(new_ip_rr / norm_rhs_squared)));
467 template<
typename MatrixT,
typename VectorT>
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
T norm_2(std::vector< T, A > const &v1)
viennacl::vector< NumericT > pipelined_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.
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.
viennacl::vector< NumericT > solve(viennacl::compressed_matrix< NumericT > const &A, viennacl::vector_base< NumericT > const &rhs, bicgstab_tag const &tag, viennacl::linalg::no_precond)
Overload for the pipelined BiCGStab implementation for the ViennaCL sparse matrix types...
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)
Class for representing non-strided subvectors of a bigger vector x.
Sparse matrix class using the ELLPACK format for storing the nonzeros.
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
Sparse matrix class using the sliced ELLPACK with parameters C, .
Class for representing strided subvectors of a bigger vector x.
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.
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)