/usr/include/viennacl/linalg/ichol.hpp is in libviennacl-dev 1.7.1+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #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
============================================================================= */
/** @file viennacl/linalg/ichol.hpp
@brief Implementations of incomplete Cholesky factorization preconditioners with static nonzero pattern.
*/
#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
{
/** @brief A tag for incomplete Cholesky factorization with static pattern (ILU0)
*/
class ichol0_tag {};
/** @brief Implementation of a ILU-preconditioner with static pattern. Optimized version for CSR matrices.
*
* Refer to Chih-Jen Lin and Jorge J. Moré, Incomplete Cholesky Factorizations with Limited Memory, SIAM J. Sci. Comput., 21(1), 24–45
* for one of many descriptions of incomplete Cholesky Factorizations
*
* @param A The input matrix in CSR format
* // param tag An ichol0_tag in order to dispatch among several other preconditioners.
*/
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;
}
}
}
}
}
}
/** @brief Incomplete Cholesky preconditioner class with static pattern (ICHOL0), can be supplied to solve()-routines
*/
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;
};
/** @brief ILU0 preconditioner class, can be supplied to solve()-routines.
*
* Specialization for compressed_matrix
*/
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
|