All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ichol.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_ICHOL_HPP_
#define VIENNACL_LINALG_ICHOL_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 <iostream>
#include "viennacl/forwards.h"
#include "viennacl/tools/tools.hpp"
#include "viennacl/compressed_matrix.hpp"
#include "viennacl/linalg/host_based/common.hpp"
#include <map>
namespace viennacl
{
namespace linalg
{
class ichol0_tag {};
template<typename NumericT>
void precondition(viennacl::compressed_matrix<NumericT> & A, ichol0_tag const & /* tag */)
{
assert( (viennacl::traits::context(A).memory_type() == viennacl::MAIN_MEMORY) && bool("System matrix must reside in main memory for ICHOL0") );
NumericT * elements = viennacl::linalg::host_based::detail::extract_raw_pointer<NumericT>(A.handle());
unsigned int const * row_buffer = viennacl::linalg::host_based::detail::extract_raw_pointer<unsigned int>(A.handle1());
unsigned int const * col_buffer = viennacl::linalg::host_based::detail::extract_raw_pointer<unsigned int>(A.handle2());
//std::cout << A.size1() << std::endl;
for (vcl_size_t i=0; i<A.size1(); ++i)
{
unsigned int row_i_begin = row_buffer[i];
unsigned int row_i_end = row_buffer[i+1];
// get a_ii:
NumericT a_ii = 0;
for (unsigned int buf_index_aii = row_i_begin; buf_index_aii < row_i_end; ++buf_index_aii)
{
if (col_buffer[buf_index_aii] == i)
{
a_ii = std::sqrt(elements[buf_index_aii]);
elements[buf_index_aii] = a_ii;
break;
}
}
// Now scale column/row i, i.e. A(k, i) /= A(i, i)
for (unsigned int buf_index_aii = row_i_begin; buf_index_aii < row_i_end; ++buf_index_aii)
{
if (col_buffer[buf_index_aii] > i)
elements[buf_index_aii] /= a_ii;
}
// Now compute A(k, j) -= A(k, i) * A(j, i) for all nonzero k, j in column i:
for (unsigned int buf_index_j = row_i_begin; buf_index_j < row_i_end; ++buf_index_j)
{
unsigned int j = col_buffer[buf_index_j];
if (j <= i)
continue;
NumericT a_ji = elements[buf_index_j];
for (unsigned int buf_index_k = row_i_begin; buf_index_k < row_i_end; ++buf_index_k)
{
unsigned int k = col_buffer[buf_index_k];
if (k < j)
continue;
NumericT a_ki = elements[buf_index_k];
//Now check whether A(k, j) is in nonzero pattern:
unsigned int row_j_begin = row_buffer[j];
unsigned int row_j_end = row_buffer[j+1];
for (unsigned int buf_index_kj = row_j_begin; buf_index_kj < row_j_end; ++buf_index_kj)
{
if (col_buffer[buf_index_kj] == k)
{
elements[buf_index_kj] -= a_ki * a_ji;
break;
}
}
}
}
}
}
template<typename MatrixT>
class ichol0_precond
{
typedef typename MatrixT::value_type NumericType;
public:
ichol0_precond(MatrixT const & mat, ichol0_tag const & tag) : tag_(tag), LLT(mat.size1(), mat.size2(), viennacl::context(viennacl::MAIN_MEMORY))
{
//initialize preconditioner:
//std::cout << "Start CPU precond" << std::endl;
init(mat);
//std::cout << "End CPU precond" << std::endl;
}
template<typename VectorT>
void apply(VectorT & vec) const
{
unsigned int const * row_buffer = viennacl::linalg::host_based::detail::extract_raw_pointer<unsigned int>(LLT.handle1());
unsigned int const * col_buffer = viennacl::linalg::host_based::detail::extract_raw_pointer<unsigned int>(LLT.handle2());
NumericType const * elements = viennacl::linalg::host_based::detail::extract_raw_pointer<NumericType>(LLT.handle());
// Note: L is stored in a column-oriented fashion, i.e. transposed w.r.t. the row-oriented layout. Thus, the factorization A = L L^T holds L in the upper triangular part of A.
viennacl::linalg::host_based::detail::csr_trans_inplace_solve<NumericType>(row_buffer, col_buffer, elements, vec, LLT.size2(), lower_tag());
viennacl::linalg::host_based::detail::csr_inplace_solve<NumericType>(row_buffer, col_buffer, elements, vec, LLT.size2(), upper_tag());
}
private:
void init(MatrixT const & mat)
{
viennacl::context host_ctx(viennacl::MAIN_MEMORY);
viennacl::switch_memory_context(LLT, host_ctx);
viennacl::copy(mat, LLT);
viennacl::linalg::precondition(LLT, tag_);
}
ichol0_tag const & tag_;
viennacl::compressed_matrix<NumericType> LLT;
};
template<typename NumericT, unsigned int AlignmentV>
class ichol0_precond< compressed_matrix<NumericT, AlignmentV> >
{
typedef compressed_matrix<NumericT, AlignmentV> MatrixType;
public:
ichol0_precond(MatrixType const & mat, ichol0_tag const & tag) : tag_(tag), LLT(mat.size1(), mat.size2(), viennacl::traits::context(mat))
{
//initialize preconditioner:
//std::cout << "Start GPU precond" << std::endl;
init(mat);
//std::cout << "End GPU precond" << std::endl;
}
void apply(vector<NumericT> & vec) const
{
if (viennacl::traits::context(vec).memory_type() != viennacl::MAIN_MEMORY)
{
viennacl::context host_ctx(viennacl::MAIN_MEMORY);
viennacl::context old_ctx = viennacl::traits::context(vec);
viennacl::switch_memory_context(vec, host_ctx);
viennacl::linalg::inplace_solve(trans(LLT), vec, lower_tag());
viennacl::linalg::inplace_solve( LLT , vec, upper_tag());
viennacl::switch_memory_context(vec, old_ctx);
}
else //apply ILU0 directly:
{
// Note: L is stored in a column-oriented fashion, i.e. transposed w.r.t. the row-oriented layout. Thus, the factorization A = L L^T holds L in the upper triangular part of A.
viennacl::linalg::inplace_solve(trans(LLT), vec, lower_tag());
viennacl::linalg::inplace_solve( LLT , vec, upper_tag());
}
}
private:
void init(MatrixType const & mat)
{
viennacl::context host_ctx(viennacl::MAIN_MEMORY);
viennacl::switch_memory_context(LLT, host_ctx);
LLT = mat;
viennacl::linalg::precondition(LLT, tag_);
}
ichol0_tag const & tag_;
viennacl::compressed_matrix<NumericT> LLT;
};
}
}
#endif
viennacl::compressed_matrix::size2
const vcl_size_t & size2() const
Returns the number of columns.
Definition: compressed_matrix.hpp:929
viennacl::linalg::inplace_solve
void inplace_solve(const matrix_base< NumericT > &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
Definition: direct_solve.hpp:217
viennacl::linalg::ichol0_precond
Incomplete Cholesky preconditioner class with static pattern (ICHOL0), can be supplied to solve()-rou...
Definition: ichol.hpp:127
viennacl::linalg::ichol0_tag
A tag for incomplete Cholesky factorization with static pattern (ILU0)
Definition: ichol.hpp:43
viennacl::compressed_matrix::size1
const vcl_size_t & size1() const
Returns the number of rows.
Definition: compressed_matrix.hpp:927
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
viennacl::linalg::lower_tag
A tag class representing a lower triangular matrix.
Definition: forwards.h:849
viennacl::linalg::precondition
void precondition(viennacl::compressed_matrix< NumericT > &A, ilu0_tag const &)
Implementation of a ILU-preconditioner with static pattern. Optimized version for CSR matrices...
Definition: ilu0.hpp:78
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::linalg::ichol0_precond< compressed_matrix< NumericT, AlignmentV > >::apply
void apply(vector< NumericT > &vec) const
Definition: ichol.hpp:185
viennacl::traits::size2
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:201
viennacl::compressed_matrix::handle
const handle_type & handle() const
Returns the OpenCL handle to the matrix entry array.
Definition: compressed_matrix.hpp:942
viennacl::compressed_matrix::handle1
const handle_type & handle1() const
Returns the OpenCL handle to the row index array.
Definition: compressed_matrix.hpp:936
NumericT
float NumericT
Definition: bisect.cpp:40
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::upper_tag
A tag class representing an upper triangular matrix.
Definition: forwards.h:854
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::compressed_matrix::handle2
const handle_type & handle2() const
Returns the OpenCL handle to the column index array.
Definition: compressed_matrix.hpp:938
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::linalg::ichol0_precond::ichol0_precond
ichol0_precond(MatrixT const &mat, ichol0_tag const &tag)
Definition: ichol.hpp:132
common.hpp
Common routines for single-threaded or OpenMP-enabled execution on CPU.
viennacl::linalg::trans
void trans(const matrix_expression< const matrix_base< NumericT, SizeT, DistanceT >, const matrix_base< NumericT, SizeT, DistanceT >, op_trans > &proxy, matrix_base< NumericT > &temp_trans)
Definition: matrix_operations.hpp:83
viennacl::vector< NumericT >
viennacl::MAIN_MEMORY
Definition: forwards.h:348
viennacl::linalg::ichol0_precond::apply
void apply(VectorT &vec) const
Definition: ichol.hpp:141
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::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::compressed_matrix< NumericT >
viennacl::linalg::ichol0_precond< compressed_matrix< NumericT, AlignmentV > >::ichol0_precond
ichol0_precond(MatrixType const &mat, ichol0_tag const &tag)
Definition: ichol.hpp:177
viennacl::switch_memory_context
void switch_memory_context(T &obj, viennacl::context new_ctx)
Generic convenience routine for migrating data of an object to a new memory domain.
Definition: memory.hpp:622