All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cg.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_CG_HPP_
#define VIENNACL_LINALG_CG_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include "viennacl/forwards.h"
#include "viennacl/tools/tools.hpp"
#include "viennacl/linalg/ilu.hpp"
#include "viennacl/linalg/prod.hpp"
#include "viennacl/linalg/inner_prod.hpp"
#include "viennacl/linalg/norm_2.hpp"
#include "viennacl/traits/clear.hpp"
#include "viennacl/traits/size.hpp"
#include "viennacl/meta/result_of.hpp"
#include "viennacl/linalg/iterative_operations.hpp"
namespace viennacl
{
namespace linalg
{
class cg_tag
{
public:
cg_tag(double tol = 1e-8, unsigned int max_iterations = 300) : tol_(tol), abs_tol_(0), iterations_(max_iterations) {}
double tolerance() const { return tol_; }
double abs_tolerance() const { return abs_tol_; }
void abs_tolerance(double new_tol) { if (new_tol >= 0) abs_tol_ = new_tol; }
unsigned int max_iterations() const { return iterations_; }
unsigned int iters() const { return iters_taken_; }
void iters(unsigned int i) const { iters_taken_ = i; }
double error() const { return last_error_; }
void error(double e) const { last_error_ = e; }
private:
double tol_;
double abs_tol_;
unsigned int iterations_;
//return values from solver
mutable unsigned int iters_taken_;
mutable double last_error_;
};
namespace detail
{
template<typename VectorT, typename PreconditionerT>
class z_handler{
public:
z_handler(VectorT & residual) : z_(residual){ }
VectorT & get() { return z_; }
private:
VectorT z_;
};
template<typename VectorT>
class z_handler<VectorT, viennacl::linalg::no_precond>{
public:
z_handler(VectorT & residual) : presidual_(&residual){ }
VectorT & get() { return *presidual_; }
private:
VectorT * presidual_;
};
}
namespace detail
{
//template<typename MatrixType, typename ScalarType>
template<typename MatrixT, typename NumericT>
viennacl::vector<NumericT> pipelined_solve(MatrixT const & A, //MatrixType const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
typedef typename viennacl::vector<NumericT>::difference_type difference_type;
viennacl::vector<NumericT> result(rhs);
viennacl::traits::clear(result);
viennacl::vector<NumericT> residual(rhs);
viennacl::vector<NumericT> p(rhs);
viennacl::vector<NumericT> Ap = viennacl::linalg::prod(A, p);
viennacl::vector<NumericT> inner_prod_buffer = viennacl::zero_vector<NumericT>(3*256, viennacl::traits::context(rhs)); // temporary buffer
std::vector<NumericT> host_inner_prod_buffer(inner_prod_buffer.size());
vcl_size_t buffer_size_per_vector = inner_prod_buffer.size() / 3;
difference_type buffer_offset_per_vector = static_cast<difference_type>(buffer_size_per_vector);
NumericT norm_rhs_squared = viennacl::linalg::norm_2(residual); norm_rhs_squared *= norm_rhs_squared;
if (norm_rhs_squared <= tag.abs_tolerance() * tag.abs_tolerance()) //check for early convergence of A*x = 0
return result;
NumericT inner_prod_rr = norm_rhs_squared;
NumericT alpha = inner_prod_rr / viennacl::linalg::inner_prod(p, Ap);
NumericT beta = viennacl::linalg::norm_2(Ap); beta = (alpha * alpha * beta * beta - inner_prod_rr) / inner_prod_rr;
NumericT inner_prod_ApAp = 0;
NumericT inner_prod_pAp = 0;
for (unsigned int i = 0; i < tag.max_iterations(); ++i)
{
tag.iters(i+1);
viennacl::linalg::pipelined_cg_vector_update(result, alpha, p, residual, Ap, beta, inner_prod_buffer);
viennacl::linalg::pipelined_cg_prod(A, p, Ap, inner_prod_buffer);
// bring back the partial results to the host:
viennacl::fast_copy(inner_prod_buffer.begin(), inner_prod_buffer.end(), host_inner_prod_buffer.begin());
inner_prod_rr = std::accumulate(host_inner_prod_buffer.begin(), host_inner_prod_buffer.begin() + buffer_offset_per_vector, NumericT(0));
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));
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));
if (monitor && monitor(result, std::sqrt(std::fabs(inner_prod_rr / norm_rhs_squared)), monitor_data))
break;
if (std::fabs(inner_prod_rr / norm_rhs_squared) < tag.tolerance() * tag.tolerance() || std::fabs(inner_prod_rr) < tag.abs_tolerance() * tag.abs_tolerance()) //squared norms involved here
break;
alpha = inner_prod_rr / inner_prod_pAp;
beta = (alpha*alpha*inner_prod_ApAp - inner_prod_rr) / inner_prod_rr;
}
//store last error estimate:
tag.error(std::sqrt(std::fabs(inner_prod_rr) / norm_rhs_squared));
return result;
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::compressed_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
return pipelined_solve(A, rhs, tag, viennacl::linalg::no_precond(), monitor, monitor_data);
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::coordinate_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
return detail::pipelined_solve(A, rhs, tag, viennacl::linalg::no_precond(), monitor, monitor_data);
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::ell_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
return detail::pipelined_solve(A, rhs, tag, viennacl::linalg::no_precond(), monitor, monitor_data);
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::sliced_ell_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
return detail::pipelined_solve(A, rhs, tag, viennacl::linalg::no_precond(), monitor, monitor_data);
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::hyb_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
cg_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<NumericT> const &, NumericT, void*) = NULL,
void *monitor_data = NULL)
{
return detail::pipelined_solve(A, rhs, tag, viennacl::linalg::no_precond(), monitor, monitor_data);
}
template<typename MatrixT, typename VectorT, typename PreconditionerT>
VectorT solve_impl(MatrixT const & matrix,
VectorT const & rhs,
cg_tag const & tag,
PreconditionerT const & precond,
bool (*monitor)(VectorT const &, typename viennacl::result_of::cpu_value_type<typename viennacl::result_of::value_type<VectorT>::type>::type, void*) = NULL,
void *monitor_data = NULL)
{
typedef typename viennacl::result_of::value_type<VectorT>::type NumericType;
typedef typename viennacl::result_of::cpu_value_type<NumericType>::type CPU_NumericType;
VectorT result = rhs;
viennacl::traits::clear(result);
VectorT residual = rhs;
VectorT tmp = rhs;
detail::z_handler<VectorT, PreconditionerT> zhandler(residual);
VectorT & z = zhandler.get();
precond.apply(z);
VectorT p = z;
CPU_NumericType ip_rr = viennacl::linalg::inner_prod(residual, z);
CPU_NumericType alpha;
CPU_NumericType new_ip_rr = 0;
CPU_NumericType beta;
CPU_NumericType norm_rhs_squared = ip_rr;
CPU_NumericType new_ipp_rr_over_norm_rhs;
if (norm_rhs_squared <= tag.abs_tolerance() * tag.abs_tolerance()) //solution is zero if RHS norm (squared) is zero
return result;
for (unsigned int i = 0; i < tag.max_iterations(); ++i)
{
tag.iters(i+1);
tmp = viennacl::linalg::prod(matrix, p);
alpha = ip_rr / viennacl::linalg::inner_prod(tmp, p);
result += alpha * p;
residual -= alpha * tmp;
z = residual;
precond.apply(z);
if (static_cast<VectorT*>(&residual)==static_cast<VectorT*>(&z))
new_ip_rr = std::pow(viennacl::linalg::norm_2(residual),2);
else
new_ip_rr = viennacl::linalg::inner_prod(residual, z);
new_ipp_rr_over_norm_rhs = new_ip_rr / norm_rhs_squared;
if (monitor && monitor(result, std::sqrt(std::fabs(new_ipp_rr_over_norm_rhs)), monitor_data))
break;
if (std::fabs(new_ipp_rr_over_norm_rhs) < tag.tolerance() * tag.tolerance() || std::fabs(new_ip_rr) < tag.abs_tolerance() * tag.abs_tolerance()) //squared norms involved here
break;
beta = new_ip_rr / ip_rr;
ip_rr = new_ip_rr;
p = z + beta*p;
}
//store last error estimate:
tag.error(std::sqrt(std::fabs(new_ip_rr / norm_rhs_squared)));
return result;
}
}
template<typename MatrixT, typename VectorT, typename PreconditionerT>
VectorT solve(MatrixT const & matrix, VectorT const & rhs, cg_tag const & tag, PreconditionerT const & precond)
{
return detail::solve_impl(matrix, rhs, tag, precond);
}
template<typename IndexT, typename NumericT, typename PreconditionerT>
std::vector<NumericT> solve(std::vector< std::map<IndexT, NumericT> > const & A, std::vector<NumericT> const & rhs, cg_tag const & tag, PreconditionerT const & precond)
{
viennacl::compressed_matrix<NumericT> vcl_A;
viennacl::copy(A, vcl_A);
viennacl::vector<NumericT> vcl_rhs(rhs.size());
viennacl::copy(rhs, vcl_rhs);
viennacl::vector<NumericT> vcl_result = solve(vcl_A, vcl_rhs, tag, precond);
std::vector<NumericT> result(vcl_result.size());
viennacl::copy(vcl_result, result);
return result;
}
template<typename MatrixT, typename VectorT>
VectorT solve(MatrixT const & matrix, VectorT const & rhs, cg_tag const & tag)
{
return solve(matrix, rhs, tag, viennacl::linalg::no_precond());
}
template<typename VectorT>
class cg_solver
{
public:
typedef typename viennacl::result_of::cpu_value_type<VectorT>::type numeric_type;
cg_solver(cg_tag const & tag) : tag_(tag), monitor_callback_(NULL), user_data_(NULL) {}
template<typename MatrixT, typename PreconditionerT>
VectorT operator()(MatrixT const & A, VectorT const & b, PreconditionerT const & precond) const
{
if (viennacl::traits::size(init_guess_) > 0) // take initial guess into account
{
VectorT mod_rhs = viennacl::linalg::prod(A, init_guess_);
mod_rhs = b - mod_rhs;
VectorT y = detail::solve_impl(A, mod_rhs, tag_, precond, monitor_callback_, user_data_);
return init_guess_ + y;
}
return detail::solve_impl(A, b, tag_, precond, monitor_callback_, user_data_);
}
template<typename MatrixT>
VectorT operator()(MatrixT const & A, VectorT const & b) const
{
return operator()(A, b, viennacl::linalg::no_precond());
}
void set_initial_guess(VectorT const & x) { init_guess_ = x; }
void set_monitor(bool (*monitor_fun)(VectorT const &, numeric_type, void *), void *user_data)
{
monitor_callback_ = monitor_fun;
user_data_ = user_data;
}
cg_tag const & tag() const { return tag_; }
private:
cg_tag tag_;
VectorT init_guess_;
bool (*monitor_callback_)(VectorT const &, numeric_type, void *);
void *user_data_;
};
}
}
#endif
viennacl::hyb_matrix
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
Definition: forwards.h:406
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
norm_2.hpp
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::linalg::cg_tag::cg_tag
cg_tag(double tol=1e-8, unsigned int max_iterations=300)
The constructor.
Definition: cg.hpp:56
size.hpp
Generic size and resize functionality for different vector and matrix types.
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::linalg::cg_tag::tolerance
double tolerance() const
Returns the relative tolerance.
Definition: cg.hpp:59
viennacl::linalg::detail::solve_impl
viennacl::vector< NumericT > solve_impl(viennacl::compressed_matrix< NumericT > const &A, viennacl::vector< NumericT > const &rhs, bicgstab_tag const &tag, viennacl::linalg::no_precond, bool(*monitor)(viennacl::vector< NumericT > const &, NumericT, void *)=NULL, void *monitor_data=NULL)
Overload for the pipelined CG implementation for the ViennaCL sparse matrix types.
Definition: bicgstab.hpp:219
viennacl::linalg::cg_solver
Definition: cg.hpp:380
viennacl::linalg::detail::z_handler
handles the no_precond case at minimal overhead
Definition: cg.hpp:94
viennacl::traits::clear
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:43
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::zero_vector
Definition: vector_def.hpp:93
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::inner_prod
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:100
viennacl::linalg::pipelined_cg_vector_update
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.
Definition: iterative_operations.hpp:59
viennacl::linalg::cg_tag::iters
unsigned int iters() const
Return the number of solver iterations:
Definition: cg.hpp:70
viennacl::result_of::value_type::type
T::value_type type
Definition: result_of.hpp:213
inner_prod.hpp
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
viennacl::linalg::solve
VectorT solve(MatrixT const &matrix, VectorT const &rhs, bicgstab_tag const &tag, PreconditionerT const &precond)
Definition: bicgstab.hpp:496
viennacl::linalg::pipelined_cg_prod
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.
Definition: iterative_operations.hpp:97
viennacl::linalg::cg_solver::operator()
VectorT operator()(MatrixT const &A, VectorT const &b) const
Definition: cg.hpp:402
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::linalg::cg_tag::max_iterations
unsigned int max_iterations() const
Returns the maximum number of iterations.
Definition: cg.hpp:67
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
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::ell_matrix
Sparse matrix class using the ELLPACK format for storing the nonzeros.
Definition: ell_matrix.hpp:53
viennacl::vector_base::begin
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:841
viennacl::linalg::no_precond
A tag class representing the use of no preconditioner.
Definition: forwards.h:873
ilu.hpp
Implementations of incomplete factorization preconditioners. Convenience header file.
viennacl::vector::difference_type
base_type::difference_type difference_type
Definition: vector.hpp:957
viennacl::sliced_ell_matrix
Sparse matrix class using the sliced ELLPACK with parameters C, .
Definition: forwards.h:403
viennacl::linalg::detail::z_handler< VectorT, viennacl::linalg::no_precond >::z_handler
z_handler(VectorT &residual)
Definition: cg.hpp:105
viennacl::linalg::detail::pipelined_solve
viennacl::vector< NumericT > pipelined_solve(MatrixT const &A, viennacl::vector_base< NumericT > const &rhs, bicgstab_tag const &tag, viennacl::linalg::no_precond, bool(*monitor)(viennacl::vector< NumericT > const &, NumericT, void *)=NULL, void *monitor_data=NULL)
Implementation of a pipelined stabilized Bi-conjugate gradient solver.
Definition: bicgstab.hpp:98
viennacl::linalg::detail::z_handler::get
VectorT & get()
Definition: cg.hpp:97
viennacl::linalg::cg_tag::abs_tolerance
double abs_tolerance() const
Returns the absolute tolerance.
Definition: cg.hpp:62
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::linalg::cg_solver::numeric_type
viennacl::result_of::cpu_value_type< VectorT >::type numeric_type
Definition: cg.hpp:383
clear.hpp
Generic clear functionality for different vector and matrix types.
viennacl::linalg::cg_solver::cg_solver
cg_solver(cg_tag const &tag)
Definition: cg.hpp:385
viennacl::vector< NumericT >
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
viennacl::linalg::cg_tag::error
double error() const
Returns the estimated relative error at the end of the solver run.
Definition: cg.hpp:74
viennacl::linalg::detail::z_handler::z_handler
z_handler(VectorT &residual)
Definition: cg.hpp:96
viennacl::linalg::cg_tag::abs_tolerance
void abs_tolerance(double new_tol)
Sets the absolute tolerance.
Definition: cg.hpp:64
iterative_operations.hpp
Implementations of specialized routines for the iterative solvers.
viennacl::traits::context
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
viennacl::linalg::cg_tag::iters
void iters(unsigned int i) const
Definition: cg.hpp:71
viennacl::linalg::cg_tag::error
void error(double e) const
Sets the estimated relative error at the end of the solver run.
Definition: cg.hpp:76
viennacl::result_of::cpu_value_type
Helper meta function for retrieving the main RAM-based value type. Particularly important to obtain T...
Definition: result_of.hpp:269
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
viennacl::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
viennacl::linalg::cg_tag
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
Definition: cg.hpp:48
viennacl::compressed_matrix< NumericT >
viennacl::linalg::cg_solver::set_monitor
void set_monitor(bool(*monitor_fun)(VectorT const &, numeric_type, void *), void *user_data)
Sets a monitor function pointer to be called in each iteration. Set to NULL to run without monitor...
Definition: cg.hpp:420
viennacl::vector_base::end
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
Definition: vector.hpp:848
viennacl::linalg::cg_solver::set_initial_guess
void set_initial_guess(VectorT const &x)
Specifies an initial guess for the iterative solver.
Definition: cg.hpp:411
result_of.hpp
A collection of compile time type deductions.
viennacl::linalg::cg_solver::tag
cg_tag const & tag() const
Returns the solver tag containing basic configuration such as tolerances, etc.
Definition: cg.hpp:427
viennacl::linalg::cg_solver::operator()
VectorT operator()(MatrixT const &A, VectorT const &b, PreconditionerT const &precond) const
Definition: cg.hpp:388
viennacl::coordinate_matrix
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...
Definition: coordinate_matrix.hpp:186
viennacl::fast_copy
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)