You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
10 KiB

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