All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jacobi_precond.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_JACOBI_PRECOND_HPP_
#define VIENNACL_LINALG_JACOBI_PRECOND_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 "viennacl/forwards.h"
#include "viennacl/vector.hpp"
#include "viennacl/compressed_matrix.hpp"
#include "viennacl/tools/tools.hpp"
#include "viennacl/linalg/sparse_matrix_operations.hpp"
#include "viennacl/linalg/row_scaling.hpp"
#include <map>
namespace viennacl
{
namespace linalg
{
class jacobi_tag {};
template<typename MatrixT,
bool is_viennacl = detail::row_scaling_for_viennacl<MatrixT>::value >
class jacobi_precond
{
typedef typename MatrixT::value_type NumericType;
public:
jacobi_precond(MatrixT const & mat, jacobi_tag const &) : diag_A_(viennacl::traits::size1(mat))
{
init(mat);
}
void init(MatrixT const & mat)
{
diag_A_.resize(viennacl::traits::size1(mat)); //resize without preserving values
for (typename MatrixT::const_iterator1 row_it = mat.begin1();
row_it != mat.end1();
++row_it)
{
bool diag_found = false;
for (typename MatrixT::const_iterator2 col_it = row_it.begin();
col_it != row_it.end();
++col_it)
{
if (col_it.index1() == col_it.index2())
{
diag_A_[col_it.index1()] = *col_it;
diag_found = true;
}
}
if (!diag_found)
throw zero_on_diagonal_exception("ViennaCL: Zero in diagonal encountered while setting up Jacobi preconditioner!");
}
}
template<typename VectorT>
void apply(VectorT & vec) const
{
assert(viennacl::traits::size(diag_A_) == viennacl::traits::size(vec) && bool("Size mismatch"));
for (vcl_size_t i=0; i<diag_A_.size(); ++i)
vec[i] /= diag_A_[i];
}
private:
std::vector<NumericType> diag_A_;
};
template<typename MatrixT>
class jacobi_precond<MatrixT, true>
{
typedef typename viennacl::result_of::cpu_value_type<typename MatrixT::value_type>::type NumericType;
public:
jacobi_precond(MatrixT const & mat, jacobi_tag const &) : diag_A_(mat.size1(), viennacl::traits::context(mat))
{
init(mat);
}
void init(MatrixT const & mat)
{
detail::row_info(mat, diag_A_, detail::SPARSE_ROW_DIAGONAL);
}
template<unsigned int AlignmentV>
void apply(viennacl::vector<NumericType, AlignmentV> & vec) const
{
assert(viennacl::traits::size(diag_A_) == viennacl::traits::size(vec) && bool("Size mismatch"));
vec = element_div(vec, diag_A_);
}
private:
viennacl::vector<NumericType> diag_A_;
};
}
}
#endif
viennacl::linalg::element_div
viennacl::vector_expression< const vector_base< T >, const vector_base< T >, op_element_binary< op_div > > element_div(vector_base< T > const &v1, vector_base< T > const &v2)
viennacl::zero_on_diagonal_exception
Definition: forwards.h:597
viennacl::linalg::jacobi_precond
Jacobi preconditioner class, can be supplied to solve()-routines. Generic version for non-ViennaCL ma...
Definition: jacobi_precond.hpp:50
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::traits::size1
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::linalg::jacobi_tag
A tag for a jacobi preconditioner.
Definition: jacobi_precond.hpp:43
viennacl::linalg::jacobi_precond< MatrixT, true >::apply
void apply(viennacl::vector< NumericType, AlignmentV > &vec) const
Definition: jacobi_precond.hpp:122
viennacl::linalg::jacobi_precond::init
void init(MatrixT const &mat)
Definition: jacobi_precond.hpp:60
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::is_viennacl
Meta function which checks whether a tag is tag_viennacl.
Definition: tag_of.hpp:375
viennacl::linalg::jacobi_precond< MatrixT, true >::init
void init(MatrixT const &mat)
Definition: jacobi_precond.hpp:115
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::linalg::jacobi_precond::jacobi_precond
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:55
compressed_matrix.hpp
Implementation of the compressed_matrix class.
sparse_matrix_operations.hpp
Implementations of operations using sparse matrices.
viennacl::linalg::jacobi_precond::apply
void apply(VectorT &vec) const
Apply to res = b - Ax, i.e. jacobi applied vec (right hand side),.
Definition: jacobi_precond.hpp:87
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector
Definition: forwards.h:266
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
row_scaling.hpp
A row normalization preconditioner is implemented here.
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::linalg::detail::SPARSE_ROW_DIAGONAL
Definition: forwards.h:842
viennacl::linalg::jacobi_precond< MatrixT, true >::jacobi_precond
jacobi_precond(MatrixT const &mat, jacobi_tag const &)
Definition: jacobi_precond.hpp:109
viennacl::linalg::detail::row_info
viennacl::enable_if< viennacl::is_any_sparse_matrix< SparseMatrixType >::value >::type row_info(SparseMatrixType const &mat, vector< SCALARTYPE, VEC_ALIGNMENT > &vec, row_info_types info_selector)
Definition: sparse_matrix_operations.hpp:50