All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gmres.hpp
Go to the documentation of this file.
#ifndef VIENNACL_GMRES_HPP_
#define VIENNACL_GMRES_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 <cmath>
#include <limits>
#include "viennacl/forwards.h"
#include "viennacl/tools/tools.hpp"
#include "viennacl/linalg/norm_2.hpp"
#include "viennacl/linalg/prod.hpp"
#include "viennacl/linalg/inner_prod.hpp"
#include "viennacl/traits/clear.hpp"
#include "viennacl/traits/size.hpp"
#include "viennacl/traits/context.hpp"
#include "viennacl/meta/result_of.hpp"
#include "viennacl/linalg/iterative_operations.hpp"
#include "viennacl/vector_proxy.hpp"
namespace viennacl
{
namespace linalg
{
class gmres_tag //generalized minimum residual
{
public:
gmres_tag(double tol = 1e-10, unsigned int max_iterations = 300, unsigned int krylov_dim = 20)
: tol_(tol), abs_tol_(0), iterations_(max_iterations), krylov_dim_(krylov_dim), iters_taken_(0) {}
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 krylov_dim() const { return krylov_dim_; }
unsigned int max_restarts() const
{
unsigned int ret = iterations_ / krylov_dim_;
if (ret > 0 && (ret * krylov_dim_ == iterations_) )
return ret - 1;
return ret;
}
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_;
unsigned int krylov_dim_;
//return values from solver
mutable unsigned int iters_taken_;
mutable double last_error_;
};
namespace detail
{
template<typename SrcVectorT, typename DestVectorT>
void gmres_copy_helper(SrcVectorT const & src, DestVectorT & dest, vcl_size_t len, vcl_size_t start = 0)
{
for (vcl_size_t i=0; i<len; ++i)
dest[start+i] = src[start+i];
}
template<typename NumericT, typename DestVectorT>
void gmres_copy_helper(viennacl::vector<NumericT> const & src, DestVectorT & dest, vcl_size_t len, vcl_size_t start = 0)
{
typedef typename viennacl::vector<NumericT>::difference_type difference_type;
viennacl::copy( src.begin() + static_cast<difference_type>(start),
src.begin() + static_cast<difference_type>(start + len),
dest.begin() + static_cast<difference_type>(start));
}
template<typename VectorT, typename NumericT>
void gmres_setup_householder_vector(VectorT const & input_vec, VectorT & hh_vec, NumericT & beta, NumericT & mu, vcl_size_t j)
{
NumericT input_j = input_vec(j);
// copy entries from input vector to householder vector:
detail::gmres_copy_helper(input_vec, hh_vec, viennacl::traits::size(hh_vec) - (j+1), j+1);
NumericT sigma = viennacl::linalg::norm_2(hh_vec);
sigma *= sigma;
if (sigma <= 0)
{
beta = 0;
mu = input_j;
}
else
{
mu = std::sqrt(sigma + input_j*input_j);
NumericT hh_vec_0 = (input_j <= 0) ? (input_j - mu) : (-sigma / (input_j + mu));
beta = NumericT(2) * hh_vec_0 * hh_vec_0 / (sigma + hh_vec_0 * hh_vec_0);
//divide hh_vec by its diagonal element hh_vec_0
hh_vec /= hh_vec_0;
hh_vec[j] = NumericT(1);
}
}
// Apply (I - beta h h^T) to x (Householder reflection with Householder vector h)
template<typename VectorT, typename NumericT>
void gmres_householder_reflect(VectorT & x, VectorT const & h, NumericT beta)
{
NumericT hT_in_x = viennacl::linalg::inner_prod(h, x);
x -= (beta * hT_in_x) * h;
}
template <typename MatrixType, typename ScalarType>
viennacl::vector<ScalarType> pipelined_solve(MatrixType const & A,
viennacl::vector<ScalarType> const & rhs,
gmres_tag const & tag,
viennacl::linalg::no_precond,
bool (*monitor)(viennacl::vector<ScalarType> const &, ScalarType, void*) = NULL,
void *monitor_data = NULL)
{
viennacl::vector<ScalarType> residual(rhs);
viennacl::vector<ScalarType> result = viennacl::zero_vector<ScalarType>(rhs.size(), viennacl::traits::context(rhs));
viennacl::vector<ScalarType> device_krylov_basis(rhs.internal_size() * tag.krylov_dim(), viennacl::traits::context(rhs)); // not using viennacl::matrix here because of spurious padding in column number
viennacl::vector<ScalarType> device_buffer_R(tag.krylov_dim()*tag.krylov_dim(), viennacl::traits::context(rhs));
std::vector<ScalarType> host_buffer_R(device_buffer_R.size());
vcl_size_t buffer_size_per_vector = 128;
vcl_size_t num_buffer_chunks = 3;
viennacl::vector<ScalarType> device_inner_prod_buffer = viennacl::zero_vector<ScalarType>(num_buffer_chunks*buffer_size_per_vector, viennacl::traits::context(rhs)); // temporary buffer
viennacl::vector<ScalarType> device_r_dot_vk_buffer = viennacl::zero_vector<ScalarType>(buffer_size_per_vector * tag.krylov_dim(), viennacl::traits::context(rhs)); // holds result of first reduction stage for <r, v_k> on device
viennacl::vector<ScalarType> device_vi_in_vk_buffer = viennacl::zero_vector<ScalarType>(buffer_size_per_vector * tag.krylov_dim(), viennacl::traits::context(rhs)); // holds <v_i, v_k> for i=0..k-1 on device
viennacl::vector<ScalarType> device_values_xi_k = viennacl::zero_vector<ScalarType>(tag.krylov_dim(), viennacl::traits::context(rhs)); // holds values \xi_k = <r, v_k> on device
std::vector<ScalarType> host_r_dot_vk_buffer(device_r_dot_vk_buffer.size());
std::vector<ScalarType> host_values_xi_k(tag.krylov_dim());
std::vector<ScalarType> host_values_eta_k_buffer(tag.krylov_dim());
std::vector<ScalarType> host_update_coefficients(tag.krylov_dim());
ScalarType norm_rhs = viennacl::linalg::norm_2(residual);
ScalarType rho_0 = norm_rhs;
ScalarType rho = ScalarType(1);
tag.iters(0);
for (unsigned int restart_count = 0; restart_count <= tag.max_restarts(); ++restart_count)
{
//
// prepare restart:
//
if (restart_count > 0)
{
// compute new residual without introducing a temporary for A*x:
residual = viennacl::linalg::prod(A, result);
residual = rhs - residual;
rho_0 = viennacl::linalg::norm_2(residual);
}
if (rho_0 <= ScalarType(tag.abs_tolerance())) // trivial right hand side?
break;
residual /= rho_0;
rho = ScalarType(1);
// check for convergence:
if (rho_0 / norm_rhs < tag.tolerance() || rho_0 < tag.abs_tolerance())
break;
//
// minimize in Krylov basis:
//
vcl_size_t k = 0;
for (k = 0; k < static_cast<vcl_size_t>(tag.krylov_dim()); ++k)
{
if (k == 0)
{
// compute v0 = A*r and perform first reduction stage for ||v0||
viennacl::vector_range<viennacl::vector<ScalarType> > v0(device_krylov_basis, viennacl::range(0, rhs.size()));
viennacl::linalg::pipelined_gmres_prod(A, residual, v0, device_inner_prod_buffer);
// Normalize v_1 and compute first reduction stage for <r, v_0> in device_r_dot_vk_buffer:
viennacl::linalg::pipelined_gmres_normalize_vk(v0, residual,
device_buffer_R, k*tag.krylov_dim() + k,
device_inner_prod_buffer, device_r_dot_vk_buffer,
buffer_size_per_vector, k*buffer_size_per_vector);
}
else
{
// compute v0 = A*r and perform first reduction stage for ||v0||
viennacl::vector_range<viennacl::vector<ScalarType> > vk (device_krylov_basis, viennacl::range( k *rhs.internal_size(), k *rhs.internal_size() + rhs.size()));
viennacl::vector_range<viennacl::vector<ScalarType> > vk_minus_1(device_krylov_basis, viennacl::range((k-1)*rhs.internal_size(), (k-1)*rhs.internal_size() + rhs.size()));
viennacl::linalg::pipelined_gmres_prod(A, vk_minus_1, vk, device_inner_prod_buffer);
//
// Gram-Schmidt, stage 1: compute first reduction stage of <v_i, v_k>
//
viennacl::linalg::pipelined_gmres_gram_schmidt_stage1(device_krylov_basis, rhs.size(), rhs.internal_size(), k, device_vi_in_vk_buffer, buffer_size_per_vector);
//
// Gram-Schmidt, stage 2: compute second reduction stage of <v_i, v_k> and use that to compute v_k -= sum_i <v_i, v_k> v_i.
// Store <v_i, v_k> in R-matrix and compute first reduction stage for ||v_k||
//
viennacl::linalg::pipelined_gmres_gram_schmidt_stage2(device_krylov_basis, rhs.size(), rhs.internal_size(), k,
device_vi_in_vk_buffer,
device_buffer_R, tag.krylov_dim(),
device_inner_prod_buffer, buffer_size_per_vector);
//
// Normalize v_k and compute first reduction stage for <r, v_k> in device_r_dot_vk_buffer:
//
viennacl::linalg::pipelined_gmres_normalize_vk(vk, residual,
device_buffer_R, k*tag.krylov_dim() + k,
device_inner_prod_buffer, device_r_dot_vk_buffer,
buffer_size_per_vector, k*buffer_size_per_vector);
}
}
//
// Run reduction to obtain the values \xi_k = <r, v_k>.
// Note that unlike Algorithm 2.1 in Walker: "A Simpler GMRES", we do not update the residual
//
viennacl::fast_copy(device_r_dot_vk_buffer.begin(), device_r_dot_vk_buffer.end(), host_r_dot_vk_buffer.begin());
for (std::size_t i=0; i<k; ++i)
{
host_values_xi_k[i] = ScalarType(0);
for (std::size_t j=0; j<buffer_size_per_vector; ++j)
host_values_xi_k[i] += host_r_dot_vk_buffer[i*buffer_size_per_vector + j];
}
//
// Bring values in R back to host:
//
viennacl::fast_copy(device_buffer_R.begin(), device_buffer_R.end(), host_buffer_R.begin());
//
// Check for premature convergence: If the diagonal element drops too far below the first norm, we're done and restrict the Krylov size accordingly.
//
vcl_size_t full_krylov_dim = k; //needed for proper access to R
for (std::size_t i=0; i<k; ++i)
{
if (std::fabs(host_buffer_R[i + i*k]) < tag.tolerance() * host_buffer_R[0])
{
k = i;
break;
}
}
// Compute error estimator:
for (std::size_t i=0; i<k; ++i)
{
tag.iters( tag.iters() + 1 ); //increase iteration counter
// check for accumulation of round-off errors for poorly conditioned systems
if (host_values_xi_k[i] >= rho || host_values_xi_k[i] <= -rho)
{
k = i;
break; // restrict Krylov space at this point. No gain from using additional basis vectors, since orthogonality is lost.
}
// update error estimator
rho *= std::sin( std::acos(host_values_xi_k[i] / rho) );
}
//
// Solve minimization problem:
//
host_values_eta_k_buffer = host_values_xi_k;
for (int i2=static_cast<int>(k)-1; i2>-1; --i2)
{
vcl_size_t i = static_cast<vcl_size_t>(i2);
for (vcl_size_t j=static_cast<vcl_size_t>(i)+1; j<k; ++j)
host_values_eta_k_buffer[i] -= host_buffer_R[i + j*full_krylov_dim] * host_values_eta_k_buffer[j];
host_values_eta_k_buffer[i] /= host_buffer_R[i + i*full_krylov_dim];
}
//
// Update x += rho * z with z = \eta_0 * residual + sum_{i=0}^{k-1} \eta_{i+1} v_i
// Note that we have not updated the residual yet, hence this slightly modified as compared to the form given in Algorithm 2.1 in Walker: "A Simpler GMRES"
//
for (vcl_size_t i=0; i<k; ++i)
host_update_coefficients[i] = rho_0 * host_values_eta_k_buffer[i];
viennacl::fast_copy(host_update_coefficients.begin(), host_update_coefficients.end(), device_values_xi_k.begin()); //reuse device_values_xi_k_buffer here for simplicity
viennacl::linalg::pipelined_gmres_update_result(result, residual,
device_krylov_basis, rhs.size(), rhs.internal_size(),
device_values_xi_k, k);
tag.error( std::fabs(rho*rho_0 / norm_rhs) );
if (monitor && monitor(result, std::fabs(rho*rho_0 / norm_rhs), monitor_data))
break;
}
return result;
}
template<typename NumericT>
viennacl::vector<NumericT> solve_impl(viennacl::compressed_matrix<NumericT> const & A,
viennacl::vector<NumericT> const & rhs,
gmres_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,
gmres_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,
gmres_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,
gmres_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,
gmres_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,
gmres_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;
unsigned int problem_size = static_cast<unsigned int>(viennacl::traits::size(rhs));
VectorT result = rhs;
viennacl::traits::clear(result);
vcl_size_t krylov_dim = static_cast<vcl_size_t>(tag.krylov_dim());
if (problem_size < krylov_dim)
krylov_dim = problem_size; //A Krylov space larger than the matrix would lead to seg-faults (mathematically, error is certain to be zero already)
VectorT res = rhs;
VectorT v_k_tilde = rhs;
VectorT v_k_tilde_temp = rhs;
std::vector< std::vector<CPU_NumericType> > R(krylov_dim, std::vector<CPU_NumericType>(tag.krylov_dim()));
std::vector<CPU_NumericType> projection_rhs(krylov_dim);
std::vector<VectorT> householder_reflectors(krylov_dim, rhs);
std::vector<CPU_NumericType> betas(krylov_dim);
CPU_NumericType norm_rhs = viennacl::linalg::norm_2(rhs);
if (norm_rhs <= tag.abs_tolerance()) //solution is zero if RHS norm is zero
return result;
tag.iters(0);
for (unsigned int it = 0; it <= tag.max_restarts(); ++it)
{
//
// (Re-)Initialize residual: r = b - A*x (without temporary for the result of A*x)
//
res = viennacl::linalg::prod(matrix, result); //initial guess zero
res = rhs - res;
precond.apply(res);
CPU_NumericType rho_0 = viennacl::linalg::norm_2(res);
//
// Check for premature convergence
//
if (rho_0 / norm_rhs < tag.tolerance() || rho_0 < tag.abs_tolerance()) // norm_rhs is known to be nonzero here
{
tag.error(rho_0 / norm_rhs);
return result;
}
//
// Normalize residual and set 'rho' to 1 as requested in 'A Simpler GMRES' by Walker and Zhou.
//
res /= rho_0;
CPU_NumericType rho = static_cast<CPU_NumericType>(1.0);
//
// Iterate up until maximal Krylove space dimension is reached:
//
vcl_size_t k = 0;
for (k = 0; k < krylov_dim; ++k)
{
tag.iters( tag.iters() + 1 ); //increase iteration counter
// prepare storage:
viennacl::traits::clear(R[k]);
viennacl::traits::clear(householder_reflectors[k]);
//compute v_k = A * v_{k-1} via Householder matrices
if (k == 0)
{
v_k_tilde = viennacl::linalg::prod(matrix, res);
precond.apply(v_k_tilde);
}
else
{
viennacl::traits::clear(v_k_tilde);
v_k_tilde[k-1] = CPU_NumericType(1);
//Householder rotations, part 1: Compute P_1 * P_2 * ... * P_{k-1} * e_{k-1}
for (int i = static_cast<int>(k)-1; i > -1; --i)
detail::gmres_householder_reflect(v_k_tilde, householder_reflectors[vcl_size_t(i)], betas[vcl_size_t(i)]);
v_k_tilde_temp = viennacl::linalg::prod(matrix, v_k_tilde);
precond.apply(v_k_tilde_temp);
v_k_tilde = v_k_tilde_temp;
//Householder rotations, part 2: Compute P_{k-1} * ... * P_{1} * v_k_tilde
for (vcl_size_t i = 0; i < k; ++i)
detail::gmres_householder_reflect(v_k_tilde, householder_reflectors[i], betas[i]);
}
//
// Compute Householder reflection for v_k_tilde such that all entries below k-th entry are zero:
//
CPU_NumericType rho_k_k = 0;
detail::gmres_setup_householder_vector(v_k_tilde, householder_reflectors[k], betas[k], rho_k_k, k);
//
// copy first k entries from v_k_tilde to R[k] in order to fill k-th column with result of
// P_k * v_k_tilde = (v[0], ... , v[k-1], norm(v), 0, 0, ...) =: (rho_{1,k}, rho_{2,k}, ..., rho_{k,k}, 0, ..., 0);
//
detail::gmres_copy_helper(v_k_tilde, R[k], k);
R[k][k] = rho_k_k;
//
// Update residual: r = P_k r
// Set zeta_k = r[k] including machine precision considerations: mathematically we have |r[k]| <= rho
// Set rho *= sin(acos(r[k] / rho))
//
detail::gmres_householder_reflect(res, householder_reflectors[k], betas[k]);
if (res[k] > rho) //machine precision reached
res[k] = rho;
if (res[k] < -rho) //machine precision reached
res[k] = -rho;
projection_rhs[k] = res[k];
rho *= std::sin( std::acos(projection_rhs[k] / rho) );
if (std::fabs(rho * rho_0 / norm_rhs) < tag.tolerance()) // Residual is sufficiently reduced, stop here
{
tag.error( std::fabs(rho*rho_0 / norm_rhs) );
++k;
break;
}
} // for k
//
// Triangular solver stage:
//
for (int i2=static_cast<int>(k)-1; i2>-1; --i2)
{
vcl_size_t i = static_cast<vcl_size_t>(i2);
for (vcl_size_t j=i+1; j<k; ++j)
projection_rhs[i] -= R[j][i] * projection_rhs[j]; //R is transposed
projection_rhs[i] /= R[i][i];
}
//
// Note: 'projection_rhs' now holds the solution (eta_1, ..., eta_k)
//
res *= projection_rhs[0];
if (k > 0)
{
for (unsigned int i = 0; i < k-1; ++i)
res[i] += projection_rhs[i+1];
}
//
// Form z inplace in 'res' by applying P_1 * ... * P_{k}
//
for (int i=static_cast<int>(k)-1; i>=0; --i)
detail::gmres_householder_reflect(res, householder_reflectors[vcl_size_t(i)], betas[vcl_size_t(i)]);
res *= rho_0;
result += res; // x += rho_0 * z in the paper
//
// Check for convergence:
//
tag.error(std::fabs(rho*rho_0 / norm_rhs));
if (monitor && monitor(result, std::fabs(rho*rho_0 / norm_rhs), monitor_data))
break;
if ( tag.error() < tag.tolerance() )
return result;
}
return result;
}
}
template<typename MatrixT, typename VectorT, typename PreconditionerT>
VectorT solve(MatrixT const & matrix, VectorT const & rhs, gmres_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, gmres_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 & A, VectorT const & rhs, gmres_tag const & tag)
{
return solve(A, rhs, tag, no_precond());
}
template<typename VectorT>
class gmres_solver
{
public:
typedef typename viennacl::result_of::cpu_value_type<VectorT>::type numeric_type;
gmres_solver(gmres_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;
}
gmres_tag const & tag() const { return tag_; }
private:
gmres_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::gmres_tag::max_restarts
unsigned int max_restarts() const
Returns the maximum number of GMRES restarts.
Definition: gmres.hpp:74
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::gmres_solver::numeric_type
viennacl::result_of::cpu_value_type< VectorT >::type numeric_type
Definition: gmres.hpp:681
viennacl::linalg::gmres_tag::max_iterations
unsigned int max_iterations() const
Returns the maximum number of iterations.
Definition: gmres.hpp:70
size.hpp
Generic size and resize functionality for different vector and matrix types.
viennacl::linalg::gmres_tag::iters
unsigned int iters() const
Return the number of solver iterations:
Definition: gmres.hpp:83
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
viennacl::linalg::gmres_tag::abs_tolerance
void abs_tolerance(double new_tol)
Sets the absolute tolerance.
Definition: gmres.hpp:67
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::linalg::pipelined_gmres_gram_schmidt_stage1
void pipelined_gmres_gram_schmidt_stage1(vector_base< T > const &device_krylov_basis, vcl_size_t v_k_size, vcl_size_t v_k_internal_size, vcl_size_t k, vector_base< T > &vi_in_vk_buffer, vcl_size_t buffer_chunk_size)
Computes the first reduction stage for multiple inner products <v_i, v_k>, i=0..k-1.
Definition: iterative_operations.hpp:286
viennacl::linalg::gmres_solver::operator()
VectorT operator()(MatrixT const &A, VectorT const &b, PreconditionerT const &precond) const
Definition: gmres.hpp:686
viennacl::linalg::pipelined_gmres_update_result
void pipelined_gmres_update_result(vector_base< T > &result, vector_base< T > const &residual, vector_base< T > const &krylov_basis, vcl_size_t v_k_size, vcl_size_t v_k_internal_size, vector_base< T > const &coefficients, vcl_size_t k)
Computes x += eta_0 r + sum_{i=1}^{k-1} eta_i v_{i-1}.
Definition: iterative_operations.hpp:356
viennacl::linalg::pipelined_gmres_gram_schmidt_stage2
void pipelined_gmres_gram_schmidt_stage2(vector_base< T > &device_krylov_basis, vcl_size_t v_k_size, vcl_size_t v_k_internal_size, vcl_size_t k, vector_base< T > const &vi_in_vk_buffer, vector_base< T > &R_buffer, vcl_size_t krylov_dim, vector_base< T > &inner_prod_buffer, vcl_size_t buffer_chunk_size)
Computes the second reduction stage for multiple inner products <v_i, v_k>, i=0..k-1, then updates v_k -= <v_i, v_k> v_i and computes the first reduction stage for ||v_k||.
Definition: iterative_operations.hpp:321
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::gmres_tag::abs_tolerance
double abs_tolerance() const
Returns the absolute tolerance.
Definition: gmres.hpp:65
viennacl::linalg::gmres_tag::iters
void iters(unsigned int i) const
Set the number of solver iterations (should only be modified by the solver)
Definition: gmres.hpp:85
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::gmres_tag::error
void error(double e) const
Sets the estimated relative error at the end of the solver run.
Definition: gmres.hpp:90
viennacl::linalg::gmres_solver
Definition: gmres.hpp:678
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::gmres_tag
A tag for the solver GMRES. Used for supplying solver parameters and for dispatching the solve() func...
Definition: gmres.hpp:49
viennacl::linalg::gmres_solver::set_initial_guess
void set_initial_guess(VectorT const &x)
Specifies an initial guess for the iterative solver.
Definition: gmres.hpp:709
viennacl::linalg::solve
VectorT solve(MatrixT const &matrix, VectorT const &rhs, bicgstab_tag const &tag, PreconditionerT const &precond)
Definition: bicgstab.hpp:496
viennacl::linalg::gmres_tag::gmres_tag
gmres_tag(double tol=1e-10, unsigned int max_iterations=300, unsigned int krylov_dim=20)
The constructor.
Definition: gmres.hpp:58
viennacl::linalg::gmres_solver::operator()
VectorT operator()(MatrixT const &A, VectorT const &b) const
Definition: gmres.hpp:700
NumericT
float NumericT
Definition: bisect.cpp:40
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::vector_range
Class for representing non-strided subvectors of a bigger vector x.
Definition: forwards.h:434
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
viennacl::linalg::pipelined_gmres_prod
void pipelined_gmres_prod(MatrixType const &A, vector_base< T > const &p, vector_base< T > &Ap, vector_base< T > &inner_prod_buffer)
Performs a joint vector update operation needed for an efficient pipelined GMRES algorithm.
Definition: iterative_operations.hpp:393
viennacl::vector::difference_type
base_type::difference_type difference_type
Definition: vector.hpp:957
viennacl::implicit_vector_base::size
vcl_size_t size() const
Definition: vector_def.hpp:47
viennacl::linalg::gmres_solver::tag
gmres_tag const & tag() const
Returns the solver tag containing basic configuration such as tolerances, etc.
Definition: gmres.hpp:725
viennacl::sliced_ell_matrix
Sparse matrix class using the sliced ELLPACK with parameters C, .
Definition: forwards.h:403
viennacl::traits::start
result_of::size_type< T >::type start(T const &obj)
Definition: start.hpp:44
context.hpp
Extracts the underlying context from objects.
viennacl::linalg::pipelined_gmres_normalize_vk
void pipelined_gmres_normalize_vk(vector_base< T > &v_k, vector_base< T > const &residual, vector_base< T > &R_buffer, vcl_size_t offset_in_R, vector_base< T > const &inner_prod_buffer, vector_base< T > &r_dot_vk_buffer, vcl_size_t buffer_chunk_size, vcl_size_t buffer_chunk_offset)
Performs a vector normalization needed for an efficient pipelined GMRES algorithm.
Definition: iterative_operations.hpp:248
viennacl::linalg::detail::gmres_setup_householder_vector
void gmres_setup_householder_vector(VectorT const &input_vec, VectorT &hh_vec, NumericT &beta, NumericT &mu, vcl_size_t j)
Computes the householder vector 'hh_vec' which rotates 'input_vec' such that all entries below the j-...
Definition: gmres.hpp:131
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::gmres_solver::gmres_solver
gmres_solver(gmres_tag const &tag)
Definition: gmres.hpp:683
viennacl::linalg::detail::gmres_copy_helper
void gmres_copy_helper(SrcVectorT const &src, DestVectorT &dest, vcl_size_t len, vcl_size_t start=0)
Definition: gmres.hpp:107
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
clear.hpp
Generic clear functionality for different vector and matrix types.
viennacl::linalg::gmres_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: gmres.hpp:718
viennacl::vector< NumericT >
viennacl::linalg::gmres_tag::tolerance
double tolerance() const
Returns the relative tolerance.
Definition: gmres.hpp:62
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
vector_proxy.hpp
Proxy classes for vectors.
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::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::basic_range
A range class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:424
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::compressed_matrix< NumericT >
viennacl::linalg::gmres_tag::error
double error() const
Returns the estimated relative error at the end of the solver run.
Definition: gmres.hpp:88
viennacl::vector_base::internal_size
size_type internal_size() const
Returns the internal length of the vector, which is given by size() plus the extra memory due to padd...
Definition: vector_def.hpp:120
result_of.hpp
A collection of compile time type deductions.
viennacl::linalg::gmres_tag::krylov_dim
unsigned int krylov_dim() const
Returns the maximum dimension of the Krylov space before restart.
Definition: gmres.hpp:72
viennacl::linalg::detail::gmres_householder_reflect
void gmres_householder_reflect(VectorT &x, VectorT const &h, NumericT beta)
Definition: gmres.hpp:162
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)