All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
row_scaling.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_ROW_SCALING_HPP_
#define VIENNACL_LINALG_ROW_SCALING_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 <map>
namespace viennacl
{
namespace linalg
{
class row_scaling_tag
{
public:
row_scaling_tag(unsigned int p = 2) : norm_(p) {}
unsigned int norm() const { return norm_; }
private:
unsigned int norm_;
};
namespace detail
{
template<typename T>
struct row_scaling_for_viennacl
{
enum { value = false };
};
template<typename ScalarType, unsigned int ALIGNMENT>
struct row_scaling_for_viennacl< viennacl::compressed_matrix<ScalarType, ALIGNMENT> >
{
enum { value = true };
};
template<typename ScalarType, unsigned int ALIGNMENT>
struct row_scaling_for_viennacl< viennacl::coordinate_matrix<ScalarType, ALIGNMENT> >
{
enum { value = true };
};
}
template<typename MatrixType,
bool is_viennacl = detail::row_scaling_for_viennacl<MatrixType>::value >
class row_scaling
{
typedef typename MatrixType::value_type ScalarType;
public:
row_scaling(MatrixType const & mat, row_scaling_tag const & tag) : diag_M(viennacl::traits::size1(mat))
{
assert(mat.size1() == mat.size2() && bool("Size mismatch"));
init(mat, tag);
}
void init(MatrixType const & mat, row_scaling_tag const & tag)
{
diag_M.resize(mat.size1()); //resize without preserving values
for (typename MatrixType::const_iterator1 row_it = mat.begin1();
row_it != mat.end1();
++row_it)
{
for (typename MatrixType::const_iterator2 col_it = row_it.begin();
col_it != row_it.end();
++col_it)
{
if (tag.norm() == 0)
diag_M[col_it.index1()] = std::max<ScalarType>(diag_M[col_it.index1()], std::fabs(*col_it));
else if (tag.norm() == 1)
diag_M[col_it.index1()] += std::fabs(*col_it);
else if (tag.norm() == 2)
diag_M[col_it.index1()] += (*col_it) * (*col_it);
}
if (!diag_M[row_it.index1()])
throw zero_on_diagonal_exception("ViennaCL: Zero row encountered while setting up row scaling preconditioner!");
if (tag.norm() == 2)
diag_M[row_it.index1()] = std::sqrt(diag_M[row_it.index1()]);
}
}
template<typename VectorType>
void apply(VectorType & vec) const
{
assert(vec.size() == diag_M.size() && bool("Size mismatch"));
for (vcl_size_t i=0; i<vec.size(); ++i)
vec[i] /= diag_M[i];
}
private:
std::vector<ScalarType> diag_M;
};
template<typename MatrixType>
class row_scaling< MatrixType, true>
{
typedef typename viennacl::result_of::cpu_value_type<typename MatrixType::value_type>::type ScalarType;
public:
row_scaling(MatrixType const & mat, row_scaling_tag const & tag) : diag_M(mat.size1(), viennacl::traits::context(mat))
{
init(mat, tag);
}
void init(MatrixType const & mat, row_scaling_tag const & tag)
{
switch (tag.norm())
{
case 0:
detail::row_info(mat, diag_M, detail::SPARSE_ROW_NORM_INF);
break;
case 1:
detail::row_info(mat, diag_M, detail::SPARSE_ROW_NORM_1);
break;
case 2:
detail::row_info(mat, diag_M, detail::SPARSE_ROW_NORM_2);
break;
default:
throw unknown_norm_exception("Unknown norm when initializing row_scaling preconditioner!");
}
}
template<unsigned int ALIGNMENT>
void apply(viennacl::vector<ScalarType, ALIGNMENT> & vec) const
{
assert(viennacl::traits::size(diag_M) == viennacl::traits::size(vec) && bool("Size mismatch"));
vec = element_div(vec, diag_M);
}
private:
viennacl::vector<ScalarType> diag_M;
};
}
}
#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::detail::SPARSE_ROW_NORM_2
Definition: forwards.h:841
viennacl::linalg::row_scaling::row_scaling
row_scaling(MatrixType const &mat, row_scaling_tag const &tag)
Constructor for the preconditioner.
Definition: row_scaling.hpp:97
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::row_scaling_tag::norm
unsigned int norm() const
Returns the index p of the l^p-norm (0 ... ||x||_sup, 1... sum(abs(x)), 2... sqrt(sum(x_i^2))). Currently only p=0, p=1, and p=2 supported.
Definition: row_scaling.hpp:50
viennacl::linalg::row_scaling
Jacobi-type preconditioner class, can be supplied to solve()-routines. This is a diagonal preconditio...
Definition: row_scaling.hpp:87
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::linalg::detail::SPARSE_ROW_NORM_INF
Definition: forwards.h:839
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::row_scaling::init
void init(MatrixType const &mat, row_scaling_tag const &tag)
Definition: row_scaling.hpp:103
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::linalg::row_scaling_tag
A tag for a row scaling preconditioner which merely normalizes the equation system such that each row...
Definition: row_scaling.hpp:40
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
viennacl::linalg::row_scaling< MatrixType, true >::row_scaling
row_scaling(MatrixType const &mat, row_scaling_tag const &tag)
Constructor for the preconditioner.
Definition: row_scaling.hpp:161
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::linalg::row_scaling::apply
void apply(VectorType &vec) const
Apply to res = b - Ax, i.e. row applied vec (right hand side),.
Definition: row_scaling.hpp:133
viennacl::linalg::detail::SPARSE_ROW_NORM_1
Definition: forwards.h:840
viennacl::unknown_norm_exception
Definition: forwards.h:603
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
viennacl::linalg::row_scaling_tag::row_scaling_tag
row_scaling_tag(unsigned int p=2)
Constructor.
Definition: row_scaling.hpp:47
viennacl::linalg::row_scaling< MatrixType, true >::apply
void apply(viennacl::vector< ScalarType, ALIGNMENT > &vec) const
Definition: row_scaling.hpp:185
viennacl::linalg::row_scaling< MatrixType, true >::init
void init(MatrixType const &mat, row_scaling_tag const &tag)
Definition: row_scaling.hpp:166