ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chow_patel_ilu.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_DETAIL_CHOW_PATEL_ILU_HPP_
2 #define VIENNACL_LINALG_DETAIL_CHOW_PATEL_ILU_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2016, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
27 #include <vector>
28 #include <cmath>
29 #include <iostream>
30 #include "viennacl/forwards.h"
31 #include "viennacl/tools/tools.hpp"
34 #include "viennacl/linalg/prod.hpp"
36 
37 namespace viennacl
38 {
39 namespace linalg
40 {
41 
45 {
46 public:
52  chow_patel_tag(vcl_size_t num_sweeps = 3, vcl_size_t num_jacobi_iters = 2) : sweeps_(num_sweeps), jacobi_iters_(num_jacobi_iters) {}
53 
55  vcl_size_t sweeps() const { return sweeps_; }
57  void sweeps(vcl_size_t num) { sweeps_ = num; }
58 
60  vcl_size_t jacobi_iters() const { return jacobi_iters_; }
62  void jacobi_iters(vcl_size_t num) { jacobi_iters_ = num; }
63 
64 private:
65  vcl_size_t sweeps_;
66  vcl_size_t jacobi_iters_;
67 };
68 
69 namespace detail
70 {
76  template<typename NumericT>
81  chow_patel_tag const & tag)
82  {
83  // make sure L and U have correct dimensions:
84  L.resize(A.size1(), A.size2(), false);
85 
86  // initialize L and U from values in A:
88 
89  // diagonally scale values from A in L:
91 
93  viennacl::backend::memory_copy(L.handle(), aij_L.handle(), 0, 0, sizeof(NumericT) * L.nnz());
94 
95  // run sweeps:
96  for (vcl_size_t i=0; i<tag.sweeps(); ++i)
98 
99  // transpose L to obtain L_trans:
101 
102  // form (I - D_L^{-1}L) and (I - D_U^{-1} U), with U := L_trans
105  }
106 
107 
109  template<typename NumericT>
115  chow_patel_tag const & tag)
116  {
117  // make sure L and U have correct dimensions:
118  L.resize(A.size1(), A.size2(), false);
119  U.resize(A.size1(), A.size2(), false);
120 
121  // initialize L and U from values in A:
123 
124  // diagonally scale values from A in L and U:
126 
127  // transpose storage layout of U from CSR to CSC via transposition
130 
131  // keep entries of a_ij for the sweeps
133  viennacl::vector<NumericT> aij_U_trans(U_trans.nnz(), viennacl::traits::context(A));
134 
135  viennacl::backend::memory_copy( L.handle(), aij_L.handle(), 0, 0, sizeof(NumericT) * L.nnz());
136  viennacl::backend::memory_copy(U_trans.handle(), aij_U_trans.handle(), 0, 0, sizeof(NumericT) * U_trans.nnz());
137 
138  // run sweeps:
139  for (vcl_size_t i=0; i<tag.sweeps(); ++i)
140  viennacl::linalg::ilu_chow_patel_sweep(L, aij_L, U_trans, aij_U_trans);
141 
142  // transpose U_trans back:
144 
145  // form (I - D_L^{-1}L) and (I - D_U^{-1} U)
148  }
149 
150 }
151 
152 
153 
154 
157 template<typename MatrixT>
159 {
160  // only works with compressed_matrix!
161  typedef typename MatrixT::CHOW_PATEL_ICC_ONLY_WORKS_WITH_COMPRESSED_MATRIX error_type;
162 };
163 
164 
169 template<typename NumericT, unsigned int AlignmentV>
170 class chow_patel_icc_precond< viennacl::compressed_matrix<NumericT, AlignmentV> >
171 {
172 
173 public:
175  : tag_(tag),
176  L_(0, 0, 0, viennacl::traits::context(A)),
177  diag_L_(A.size1(), viennacl::traits::context(A)),
178  L_trans_(0, 0, 0, viennacl::traits::context(A)),
179  x_k_(A.size1(), viennacl::traits::context(A)),
180  b_(A.size1(), viennacl::traits::context(A))
181  {
182  viennacl::linalg::detail::precondition(A, L_, diag_L_, L_trans_, tag_);
183  }
184 
189  template<typename VectorT>
190  void apply(VectorT & vec) const
191  {
192  //
193  // y = L^{-1} b through Jacobi iteration y_{k+1} = (I - D^{-1}L)y_k + D^{-1}x
194  //
195  b_ = viennacl::linalg::element_div(vec, diag_L_);
196  x_k_ = b_;
197  for (unsigned int i=0; i<tag_.jacobi_iters(); ++i)
198  {
199  vec = viennacl::linalg::prod(L_, x_k_);
200  x_k_ = vec + b_;
201  }
202 
203  //
204  // x = U^{-1} y through Jacobi iteration x_{k+1} = (I - D^{-1}L^T)x_k + D^{-1}b
205  //
206  b_ = viennacl::linalg::element_div(x_k_, diag_L_);
207  x_k_ = b_; // x_1 if x_0 \equiv 0
208  for (unsigned int i=0; i<tag_.jacobi_iters(); ++i)
209  {
210  vec = viennacl::linalg::prod(L_trans_, x_k_);
211  x_k_ = vec + b_;
212  }
213 
214  // return result:
215  vec = x_k_;
216  }
217 
218 private:
219  chow_patel_tag tag_;
223 
224  mutable viennacl::vector<NumericT> x_k_;
225  mutable viennacl::vector<NumericT> b_;
226 };
227 
228 
229 
230 
231 
232 
235 template<typename MatrixT>
237 {
238  // only works with compressed_matrix!
239  typedef typename MatrixT::CHOW_PATEL_ILU_ONLY_WORKS_WITH_COMPRESSED_MATRIX error_type;
240 };
241 
242 
247 template<typename NumericT, unsigned int AlignmentV>
248 class chow_patel_ilu_precond< viennacl::compressed_matrix<NumericT, AlignmentV> >
249 {
250 
251 public:
253  : tag_(tag),
254  L_(0, 0, 0, viennacl::traits::context(A)),
255  diag_L_(A.size1(), viennacl::traits::context(A)),
256  U_(0, 0, 0, viennacl::traits::context(A)),
257  diag_U_(A.size1(), viennacl::traits::context(A)),
258  x_k_(A.size1(), viennacl::traits::context(A)),
259  b_(A.size1(), viennacl::traits::context(A))
260  {
261  viennacl::linalg::detail::precondition(A, L_, diag_L_, U_, diag_U_, tag_);
262  }
263 
268  template<typename VectorT>
269  void apply(VectorT & vec) const
270  {
271  //
272  // y = L^{-1} b through Jacobi iteration y_{k+1} = (I - D^{-1}L)y_k + D^{-1}x
273  //
274  b_ = viennacl::linalg::element_div(vec, diag_L_);
275  x_k_ = b_;
276  for (unsigned int i=0; i<tag_.jacobi_iters(); ++i)
277  {
278  vec = viennacl::linalg::prod(L_, x_k_);
279  x_k_ = vec + b_;
280  }
281 
282  //
283  // x = U^{-1} y through Jacobi iteration x_{k+1} = (I - D^{-1}U)x_k + D^{-1}b
284  //
285  b_ = viennacl::linalg::element_div(x_k_, diag_U_);
286  x_k_ = b_; // x_1 if x_0 \equiv 0
287  for (unsigned int i=0; i<tag_.jacobi_iters(); ++i)
288  {
289  vec = viennacl::linalg::prod(U_, x_k_);
290  x_k_ = vec + b_;
291  }
292 
293  // return result:
294  vec = x_k_;
295  }
296 
297 private:
298  chow_patel_tag tag_;
303 
304  mutable viennacl::vector<NumericT> x_k_;
305  mutable viennacl::vector<NumericT> b_;
306 };
307 
308 
309 } // namespace linalg
310 } // namespace viennacl
311 
312 
313 #endif
314 
315 
316 
const vcl_size_t & size2() const
Returns the number of columns.
void apply(VectorT &vec) const
Preconditioner application: LL^Tx = b, computed via Ly = b, L^Tx = y using Jacobi iterations...
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)
A tag for incomplete LU and incomplete Cholesky factorization with static pattern (Parallel-ILU0...
void jacobi_iters(vcl_size_t num)
Sets the number of Jacobi iterations for each triangular 'solve' when applying the preconditioner to ...
const vcl_size_t & size1() const
Returns the number of rows.
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Various little tools used here and there in ViennaCL.
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
void extract_L(compressed_matrix< NumericT > const &A, compressed_matrix< NumericT > &L)
Extracts the lower triangular part L from A.
void ilu_chow_patel_sweep(compressed_matrix< NumericT > &L, vector< NumericT > const &aij_L, compressed_matrix< NumericT > &U_trans, vector< NumericT > const &aij_U_trans)
Performs one nonlinear relaxation step in the Chow-Patel-ILU (cf. Algorithm 2 in paper) ...
This file provides the forward declarations for the main types used within ViennaCL.
Parallel Chow-Patel ILU preconditioner class, can be supplied to solve()-routines.
void sweeps(vcl_size_t num)
Sets the number of sweeps (i.e. number of nonlinear iterations) in the solver setup stage...
const handle_type & handle() const
Returns the OpenCL handle to the matrix entry array.
const vcl_size_t & nnz() const
Returns the number of nonzero entries.
float NumericT
Definition: bisect.cpp:40
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
Implementations of specialized routines for the Chow-Patel parallel ILU preconditioner.
vcl_size_t sweeps() const
Returns the number of sweeps (i.e. number of nonlinear iterations) in the solver setup stage...
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
chow_patel_tag(vcl_size_t num_sweeps=3, vcl_size_t num_jacobi_iters=2)
Constructor allowing to set the number of sweeps and Jacobi iterations.
void icc_scale(compressed_matrix< NumericT > const &A, compressed_matrix< NumericT > &L)
Scales the values extracted from A such that A' = DAD has unit diagonal. Updates values from A in L a...
void ilu_scale(compressed_matrix< NumericT > const &A, compressed_matrix< NumericT > &L, compressed_matrix< NumericT > &U)
Scales the values extracted from A such that A' = DAD has unit diagonal. Updates values from A in L a...
void extract_LU(compressed_matrix< NumericT > const &A, compressed_matrix< NumericT > &L, compressed_matrix< NumericT > &U)
Extracts the lower triangular part L and the upper triangular part U from A.
chow_patel_icc_precond(viennacl::compressed_matrix< NumericT, AlignmentV > const &A, chow_patel_tag const &tag)
std::size_t vcl_size_t
Definition: forwards.h:75
void ilu_form_neumann_matrix(compressed_matrix< NumericT > &R, vector< NumericT > &diag_R)
Extracts the lower triangular part L and the upper triangular part U from A.
void ilu_transpose(compressed_matrix< NumericT > const &A, compressed_matrix< NumericT > &B)
Transposition B <- A^T, where the aij-vector is permuted in the same way as the value array in A when...
void memory_copy(mem_handle const &src_buffer, mem_handle &dst_buffer, vcl_size_t src_offset, vcl_size_t dst_offset, vcl_size_t bytes_to_copy)
Copies 'bytes_to_copy' bytes from address 'src_buffer + src_offset' to memory starting at address 'ds...
Definition: memory.hpp:140
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
void icc_chow_patel_sweep(compressed_matrix< NumericT > &L, vector< NumericT > &aij_L)
Performs one nonlinear relaxation step in the Chow-Patel-ICC (cf. Algorithm 3 in paper, but for L rather than U)
Common routines used within ILU-type preconditioners.
void precondition(viennacl::compressed_matrix< NumericT > const &A, viennacl::compressed_matrix< NumericT > &L, viennacl::vector< NumericT > &diag_L, viennacl::compressed_matrix< NumericT > &L_trans, chow_patel_tag const &tag)
Implementation of the parallel ICC0 factorization, Algorithm 3 in Chow-Patel paper.
void resize(vcl_size_t new_size1, vcl_size_t new_size2, bool preserve=true)
Resize the matrix.
Parallel Chow-Patel ILU preconditioner class, can be supplied to solve()-routines.
Main interface routines for memory management.
void apply(VectorT &vec) const
Preconditioner application: LUx = b, computed via Ly = b, Ux = y using Jacobi iterations.
vcl_size_t jacobi_iters() const
Returns the number of Jacobi iterations (i.e. applications of x_{k+1} = (I - D^{-1}R)x_k + D^{-1} b) ...
chow_patel_ilu_precond(viennacl::compressed_matrix< NumericT, AlignmentV > const &A, chow_patel_tag const &tag)