GetFEM  5.5
gmm_precond_ilut.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 // This file is a modified version of ilut.h from ITL.
32 // See http://osl.iu.edu/research/itl/
33 // Following the corresponding Copyright notice.
34 //===========================================================================
35 //
36 // Copyright (c) 1998-2020, University of Notre Dame. All rights reserved.
37 // Redistribution and use in source and binary forms, with or without
38 // modification, are permitted provided that the following conditions are met:
39 //
40 // * Redistributions of source code must retain the above copyright
41 // notice, this list of conditions and the following disclaimer.
42 // * Redistributions in binary form must reproduce the above copyright
43 // notice, this list of conditions and the following disclaimer in the
44 // documentation and/or other materials provided with the distribution.
45 // * Neither the name of the University of Notre Dame nor the
46 // names of its contributors may be used to endorse or promote products
47 // derived from this software without specific prior written permission.
48 //
49 // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
50 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
51 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
53 // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 //
61 //===========================================================================
62 
63 #ifndef GMM_PRECOND_ILUT_H
64 #define GMM_PRECOND_ILUT_H
65 
66 /**@file gmm_precond_ilut.h
67  @author Andrew Lumsdaine <[email protected]>, Lie-Quan Lee <[email protected]>
68  @date June 5, 2003.
69  @brief ILUT: Incomplete LU with threshold and K fill-in Preconditioner.
70 */
71 
72 /*
73  Performane comparing for SSOR, ILU and ILUT based on sherman 5 matrix
74  in Harwell-Boeing collection on Sun Ultra 30 UPA/PCI (UltraSPARC-II 296MHz)
75  Preconditioner & Factorization time & Number of Iteration \\ \hline
76  SSOR & 0.010577 & 41 \\
77  ILU & 0.019336 & 32 \\
78  ILUT with 0 fill-in and threshold of 1.0e-6 & 0.343612 & 23 \\
79  ILUT with 5 fill-in and threshold of 1.0e-6 & 0.343612 & 18 \\ \hline
80 */
81 
82 #include "gmm_precond.h"
83 
84 namespace gmm {
85 
86  template<typename T> struct elt_rsvector_value_less_ {
87  inline bool operator()(const elt_rsvector_<T>& a,
88  const elt_rsvector_<T>& b) const
89  { return (gmm::abs(a.e) > gmm::abs(b.e)); }
90  };
91 
92  /** Incomplete LU with threshold and K fill-in Preconditioner.
93 
94  The algorithm of ILUT(A, 0, 1.0e-6) is slower than ILU(A). If No
95  fill-in is arrowed, you can use ILU instead of ILUT.
96 
97  Notes: The idea under a concrete Preconditioner such as ilut is to
98  create a Preconditioner object to use in iterative methods.
99  */
100  template <typename Matrix>
101  class ilut_precond {
102  public :
103  typedef typename linalg_traits<Matrix>::value_type value_type;
106  typedef row_matrix<_rsvector> LU_Matrix;
107 
108  bool invert;
109  LU_Matrix L, U;
110 
111  protected:
112  size_type K;
113  double eps;
114 
115  template<typename M> void do_ilut(const M&, row_major);
116  void do_ilut(const Matrix&, col_major);
117 
118  public:
119  void build_with(const Matrix& A, int k_ = -1, double eps_ = double(-1)) {
120  if (k_ >= 0) K = k_;
121  if (eps_ >= double(0)) eps = eps_;
122  invert = false;
123  gmm::resize(L, mat_nrows(A), mat_ncols(A));
124  gmm::resize(U, mat_nrows(A), mat_ncols(A));
125  do_ilut(A, typename principal_orientation_type<typename
126  linalg_traits<Matrix>::sub_orientation>::potype());
127  }
128  ilut_precond(const Matrix& A, int k_, double eps_)
129  : L(mat_nrows(A), mat_ncols(A)), U(mat_nrows(A), mat_ncols(A)),
130  K(k_), eps(eps_) { build_with(A); }
131  ilut_precond(size_type k_, double eps_) : K(k_), eps(eps_) {}
132  ilut_precond(void) { K = 10; eps = 1E-7; }
133  size_type memsize() const {
134  return sizeof(*this) + (nnz(U)+nnz(L))*sizeof(value_type);
135  }
136  };
137 
138  template<typename Matrix> template<typename M>
139  void ilut_precond<Matrix>::do_ilut(const M& A, row_major) {
140  typedef value_type T;
141  typedef typename number_traits<T>::magnitude_type R;
142 
143  size_type n = mat_nrows(A);
144  if (n == 0) return;
145  std::vector<T> indiag(n);
146  _wsvector w(mat_ncols(A));
147  _rsvector ww(mat_ncols(A)), wL(mat_ncols(A)), wU(mat_ncols(A));
148  T tmp;
149  gmm::clear(U); gmm::clear(L);
150  R prec = default_tol(R());
151  R max_pivot = gmm::abs(A(0,0)) * prec;
152 
153  for (size_type i = 0; i < n; ++i) {
154  gmm::copy(mat_const_row(A, i), w);
155  double norm_row = gmm::vect_norm2(w);
156 
157  typename _wsvector::iterator wkold = w.end();
158  for (typename _wsvector::iterator wk = w.begin();
159  wk != w.end() && wk->first < i; ) {
160  size_type k = wk->first;
161  tmp = (wk->second) * indiag[k];
162  if (gmm::abs(tmp) < eps * norm_row) w.erase(k);
163  else { wk->second += tmp; gmm::add(scaled(mat_row(U, k), -tmp), w); }
164  if (wkold == w.end()) wk = w.begin(); else { wk = wkold; ++wk; }
165  if (wk != w.end() && wk->first == k)
166  { if (wkold == w.end()) wkold = w.begin(); else ++wkold; ++wk; }
167  }
168  tmp = w[i];
169 
170  if (gmm::abs(tmp) <= max_pivot) {
171  GMM_WARNING2("pivot " << i << " too small. try with ilutp ?");
172  w[i] = tmp = T(1);
173  }
174 
175  max_pivot = std::max(max_pivot, std::min(gmm::abs(tmp) * prec, R(1)));
176  indiag[i] = T(1) / tmp;
177  gmm::clean(w, eps * norm_row);
178  gmm::copy(w, ww);
179  std::sort(ww.begin(), ww.end(), elt_rsvector_value_less_<T>());
180  typename _rsvector::const_iterator wit = ww.begin(), wite = ww.end();
181 
182  size_type nnl = 0, nnu = 0;
183  wL.base_resize(K); wU.base_resize(K+1);
184  typename _rsvector::iterator witL = wL.begin(), witU = wU.begin();
185  for (; wit != wite; ++wit)
186  if (wit->c < i) { if (nnl < K) { *witL++ = *wit; ++nnl; } }
187  else { if (nnu < K || wit->c == i) { *witU++ = *wit; ++nnu; } }
188  wL.base_resize(nnl); wU.base_resize(nnu);
189  std::sort(wL.begin(), wL.end());
190  std::sort(wU.begin(), wU.end());
191  gmm::copy(wL, L.row(i));
192  gmm::copy(wU, U.row(i));
193  }
194 
195  }
196 
197  template<typename Matrix>
198  void ilut_precond<Matrix>::do_ilut(const Matrix& A, col_major) {
199  do_ilut(gmm::transposed(A), row_major());
200  invert = true;
201  }
202 
203  template <typename Matrix, typename V1, typename V2> inline
204  void mult(const ilut_precond<Matrix>& P, const V1 &v1, V2 &v2) {
205  gmm::copy(v1, v2);
206  if (P.invert) {
207  gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
208  gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
209  }
210  else {
211  gmm::lower_tri_solve(P.L, v2, true);
212  gmm::upper_tri_solve(P.U, v2, false);
213  }
214  }
215 
216  template <typename Matrix, typename V1, typename V2> inline
217  void transposed_mult(const ilut_precond<Matrix>& P,const V1 &v1,V2 &v2) {
218  gmm::copy(v1, v2);
219  if (P.invert) {
220  gmm::lower_tri_solve(P.L, v2, true);
221  gmm::upper_tri_solve(P.U, v2, false);
222  }
223  else {
224  gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
225  gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
226  }
227  }
228 
229  template <typename Matrix, typename V1, typename V2> inline
230  void left_mult(const ilut_precond<Matrix>& P, const V1 &v1, V2 &v2) {
231  copy(v1, v2);
232  if (P.invert) gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
233  else gmm::lower_tri_solve(P.L, v2, true);
234  }
235 
236  template <typename Matrix, typename V1, typename V2> inline
237  void right_mult(const ilut_precond<Matrix>& P, const V1 &v1, V2 &v2) {
238  copy(v1, v2);
239  if (P.invert) gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
240  else gmm::upper_tri_solve(P.U, v2, false);
241  }
242 
243  template <typename Matrix, typename V1, typename V2> inline
244  void transposed_left_mult(const ilut_precond<Matrix>& P, const V1 &v1,
245  V2 &v2) {
246  copy(v1, v2);
247  if (P.invert) gmm::upper_tri_solve(P.U, v2, false);
248  else gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
249  }
250 
251  template <typename Matrix, typename V1, typename V2> inline
252  void transposed_right_mult(const ilut_precond<Matrix>& P, const V1 &v1,
253  V2 &v2) {
254  copy(v1, v2);
255  if (P.invert) gmm::lower_tri_solve(P.L, v2, true);
256  else gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
257  }
258 
259 }
260 
261 #endif
262 
Incomplete LU with threshold and K fill-in Preconditioner.
sparse vector built upon std::vector.
Definition: gmm_vector.h:995
sparse vector built upon std::map.
Definition: gmm_vector.h:752
size_type nnz(const L &l)
count the number of non-zero entries of a vector or matrix.
Definition: gmm_blas.h:68
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
Definition: gmm_blas.h:556
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void resize(V &v, size_type n)
*‍/
Definition: gmm_blas.h:209
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
gmm preconditioners.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48