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.

299 lines
10 KiB

  1. /*===========================================================================
  2. Copyright (C) 2007-2012 Yves Renard, Julien Pommier.
  3. This file is a part of GETFEM++
  4. Getfem++ is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version along with the GCC Runtime Library
  8. Exception either version 3.1 or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License and GCC Runtime Library Exception for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  16. ===========================================================================*/
  17. // SQUARED_MATRIX_PARAM;
  18. // DENSE_VECTOR_PARAM;
  19. // VECTOR_PARAM;
  20. // ENDPARAM;
  21. #include "gmm/gmm.h"
  22. using namespace std; // in order to test a using namespace std;
  23. using gmm::size_type;
  24. int print_debug = 0;
  25. size_type nb_fault_allowed = 200;
  26. size_type nb_fault = 0;
  27. double ratio_max = 0.0;
  28. struct la_stat {
  29. size_type nb_iter, nb_fault, nb_expe;
  30. la_stat(void) : nb_iter(0), nb_fault(0), nb_expe(0) {};
  31. };
  32. template <typename PS>
  33. la_stat ps_stat(const PS &, size_type nb_iter, bool fault) {
  34. static la_stat la;
  35. if (nb_iter != size_type(-1)) {
  36. la.nb_expe++;
  37. la.nb_iter += nb_iter;
  38. la.nb_fault += (fault) ? 1 : 0;
  39. }
  40. return la;
  41. }
  42. template <typename PS>
  43. void print_stat(const PS &ps, const char *name) {
  44. la_stat s = ps_stat(ps, size_type(-1), false);
  45. cout << name << "\t: " << s.nb_expe << " exp. with ";
  46. if (s.nb_fault > 1) cout << s.nb_fault << " faults";
  47. else if (s.nb_fault == 1) cout << "1 fault";
  48. else cout << "no fault";
  49. if (s.nb_expe != 0) {
  50. double ratio = double(s.nb_fault) / double(s.nb_expe);
  51. ratio_max = std::max(ratio, ratio_max);
  52. cout << ", ratio = " << ratio;
  53. cout << ", average nb iter = " << double(s.nb_iter) / double(s.nb_expe);
  54. }
  55. cout << endl;
  56. }
  57. struct LEAST_SQUARE_CG {
  58. template <typename MAT, typename VECT1, typename VECT2, typename PRECOND>
  59. void operator()(const MAT &m, VECT1 &v1, const VECT2 &v2, const PRECOND &,
  60. gmm::iteration &iter) const
  61. { gmm::least_squares_cg(m, v1, v2, iter); }
  62. };
  63. struct BICGSTAB {
  64. template <typename MAT, typename VECT1, typename VECT2, typename PRECOND>
  65. void operator()(const MAT &m, VECT1 &v1, const VECT2 &v2, const PRECOND &P,
  66. gmm::iteration &iter) const
  67. { gmm::bicgstab(m, v1, v2, P, iter); }
  68. };
  69. struct GMRES {
  70. template <typename MAT, typename VECT1, typename VECT2, typename PRECOND>
  71. void operator()(const MAT &m, VECT1 &v1, const VECT2 &v2, const PRECOND &P,
  72. gmm::iteration &iter) const
  73. { gmm::gmres(m, v1, v2, P, 50, iter); }
  74. };
  75. struct QMR {
  76. template <typename MAT, typename VECT1, typename VECT2, typename PRECOND>
  77. void operator()(const MAT &m, VECT1 &v1, const VECT2 &v2, const PRECOND &P,
  78. gmm::iteration &iter) const
  79. { gmm::qmr(m, v1, v2, P, iter); }
  80. };
  81. struct CG {
  82. template <typename MAT, typename VECT1, typename VECT2, typename PRECOND>
  83. void operator()(const MAT &m, VECT1 &v1, const VECT2 &v2, const PRECOND &P,
  84. gmm::iteration &iter) const
  85. { gmm::cg(m, v1, v2, P, iter); }
  86. };
  87. template <typename SOLVER, typename PRECOND, typename MAT, typename VECT1,
  88. typename VECT2, typename Rcond>
  89. void do_test(const SOLVER &solver, const MAT &m1, VECT1 &v1,
  90. VECT2 &v2, const PRECOND &P, Rcond cond) {
  91. typedef typename gmm::linalg_traits<MAT>::value_type T;
  92. typedef typename gmm::number_traits<T>::magnitude_type R;
  93. R prec = gmm::default_tol(R()), error(0);
  94. size_type m = gmm::mat_nrows(m1);
  95. std::vector<T> v3(m);
  96. gmm::iteration iter((double(prec*cond))*100.0, (print_debug > 2) ? 1:0,
  97. 50*m);
  98. for (int i = 0; i < 30; ++i) {
  99. iter.init();
  100. gmm::fill_random(v1);
  101. if (i >= 4) gmm::fill_random(v2);
  102. if (i == 29) gmm::clear(v1);
  103. solver(m1, v1, v2, P, iter);
  104. gmm::mult(m1, v1, gmm::scaled(v2, T(-1)), v3);
  105. error = gmm::vect_norm2(v3) / gmm::vect_norm2(v1);
  106. // if (error * R(0) != R(0))
  107. // GMM_ASSERT1(false, "Inconsistent error: " << error);
  108. if (error <= prec * cond * R(20000)) {
  109. ps_stat(solver, iter.get_iteration(), false);
  110. ps_stat(P, iter.get_iteration(), false);
  111. return;
  112. }
  113. }
  114. ps_stat(solver, iter.get_iteration(), true);
  115. ps_stat(P, iter.get_iteration(), true);
  116. ++nb_fault;
  117. if (nb_fault > nb_fault_allowed)
  118. GMM_ASSERT1(false, "Error too large: " << error);
  119. }
  120. template <typename MAT1, typename VECT1, typename VECT2>
  121. bool test_procedure(const MAT1 &m1_, const VECT1 &v1_, const VECT2 &v2_) {
  122. VECT1 &v1 = const_cast<VECT1 &>(v1_);
  123. VECT2 &v2 = const_cast<VECT2 &>(v2_);
  124. MAT1 &m1 = const_cast<MAT1 &>(m1_);
  125. typedef typename gmm::linalg_traits<MAT1>::value_type T;
  126. typedef typename gmm::number_traits<T>::magnitude_type R;
  127. R prec = gmm::default_tol(R());
  128. size_type m = gmm::mat_nrows(m1);
  129. if (m == 0) return true;
  130. static int nexpe = 0, effexpe = 0;
  131. ++nexpe;
  132. gmm::set_warning_level(0);
  133. gmm::clean(v1, 0.01);
  134. for (size_type i = 0; i < gmm::vect_size(v1); ++i)
  135. if (v1[i] != T(0) && gmm::abs(v1[i]) < R(1) / R(101))
  136. GMM_ASSERT1(false, "Error in clean");
  137. if (print_debug) {
  138. cout << "Begin experiment " << nexpe << "\n\nwith " << m1 << "\n\n";
  139. gmm::set_warning_level(3);
  140. }
  141. R det = gmm::abs(gmm::lu_det(m1)), cond = gmm::condest(m1);
  142. if (print_debug)
  143. cout << "condition number = " << cond << " det = " << det << endl;
  144. if (det == R(0) && cond < R(1) / prec && cond != R(0))
  145. GMM_ASSERT1(false, "Inconsistent condition number: " << cond);
  146. if (sqrt(prec) * cond >= R(1)/R(100) || det < sqrt(prec)*R(10)) return false;
  147. ++effexpe; cout << "."; cout.flush();
  148. gmm::identity_matrix P1;
  149. gmm::diagonal_precond<MAT1> P2(m1);
  150. gmm::mr_approx_inverse_precond<MAT1> P3(m1, 10, prec);
  151. gmm::ilu_precond<MAT1> P4(m1);
  152. gmm::ilut_precond<MAT1> P5(m1, 20, prec);
  153. gmm::ilutp_precond<MAT1> P5b(m1, 20, prec);
  154. R detmr = gmm::abs(gmm::lu_det(P3.approx_inverse()));
  155. if (sizeof(R) > 4 || m < 15) {
  156. if (print_debug) cout << "\nLeast square CG with no preconditionner\n";
  157. do_test(LEAST_SQUARE_CG(), m1, v1, v2, P1, cond);
  158. if (print_debug) cout << "\nBicgstab with no preconditionner\n";
  159. do_test(BICGSTAB(), m1, v1, v2, P1, cond);
  160. if (print_debug) cout << "\nBicgstab with diagonal preconditionner\n";
  161. do_test(BICGSTAB(), m1, v1, v2, P2, cond);
  162. if (detmr > prec * R(100)) {
  163. if (print_debug) cout << "\nBicgstab with mr preconditionner\n";
  164. do_test(BICGSTAB(), m1, v1, v2, P3, cond);
  165. }
  166. if (print_debug) cout << "\nBicgstab with ilu preconditionner\n";
  167. do_test(BICGSTAB(), m1, v1, v2, P4, cond);
  168. if (print_debug) cout << "\nBicgstab with ilut preconditionner\n";
  169. do_test(BICGSTAB(), m1, v1, v2, P5, cond);
  170. if (print_debug) cout << "\nBicgstab with ilutp preconditionner\n";
  171. do_test(BICGSTAB(), m1, v1, v2, P5b, cond);
  172. }
  173. if (print_debug) cout << "\nGmres with no preconditionner\n";
  174. do_test(GMRES(), m1, v1, v2, P1, cond);
  175. if (print_debug) cout << "\nGmres with diagonal preconditionner\n";
  176. do_test(GMRES(), m1, v1, v2, P2, cond);
  177. if (detmr > prec * R(100)) {
  178. if (print_debug) cout << "\nGmres with mr preconditionner\n";
  179. do_test(GMRES(), m1, v1, v2, P3, cond);
  180. }
  181. if (print_debug) cout << "\nGmres with ilu preconditionner\n";
  182. do_test(GMRES(), m1, v1, v2, P4, cond);
  183. if (print_debug) cout << "\nGmres with ilut preconditionner\n";
  184. do_test(GMRES(), m1, v1, v2, P5, cond);
  185. if (print_debug) cout << "\nGmres with ilutp preconditionner\n";
  186. do_test(GMRES(), m1, v1, v2, P5b, cond);
  187. if (sizeof(R) > 4 || m < 20) {
  188. if (print_debug) cout << "\nQmr with no preconditionner\n";
  189. do_test(QMR(), m1, v1, v2, P1, cond);
  190. if (print_debug) cout << "\nQmr with diagonal preconditionner\n";
  191. do_test(QMR(), m1, v1, v2, P2, cond);
  192. if (print_debug) cout << "\nQmr with ilu preconditionner\n";
  193. do_test(QMR(), m1, v1, v2, P4, cond);
  194. if (print_debug) cout << "\nQmr with ilut preconditionner\n";
  195. do_test(QMR(), m1, v1, v2, P5, cond);
  196. if (print_debug) cout << "\nQmr with ilutp preconditionner\n";
  197. do_test(QMR(), m1, v1, v2, P5b, cond);
  198. }
  199. gmm::dense_matrix<T> m2(m, m), m3(m, m);
  200. gmm::mult(gmm::conjugated(m1), m1, m2);
  201. gmm::copy(m1, m3);
  202. gmm::add(gmm::conjugated(m1), m3);
  203. gmm::copy(m2, m1);
  204. gmm::cholesky_precond<MAT1> P6(m1);
  205. gmm::choleskyt_precond<MAT1> P7(m1, 10, prec);
  206. if (!is_hermitian(m1, prec*R(100)))
  207. GMM_ASSERT1(false, "The matrix is not hermitian");
  208. if (print_debug) cout << "\nCG with no preconditionner\n";
  209. do_test(CG(), m1, v1, v2, P1, cond*cond);
  210. if (print_debug) cout << "\nCG with diagonal preconditionner\n";
  211. do_test(CG(), m1, v1, v2, P2, cond*cond);
  212. if (print_debug) cout << "\nCG with ildlt preconditionner\n";
  213. do_test(CG(), m1, v1, v2, P6, cond*cond);
  214. if (print_debug) cout << "\nCG with ildltt preconditionner\n";
  215. do_test(CG(), m1, v1, v2, P7, cond*cond);
  216. if (effexpe == 50) {
  217. cout << "\n\n" << effexpe << " effective experiments with ";
  218. if (nb_fault > 1) cout << nb_fault << " faults";
  219. else if (nb_fault == 1) cout << "1 fault";
  220. else cout << "no fault";
  221. cout << ", size = " << m << " base type : " << typeid(T).name() << endl;
  222. cout.precision(3);
  223. print_stat(LEAST_SQUARE_CG(), "solver least square cg");
  224. print_stat(BICGSTAB(), "solver bicgstab");
  225. print_stat(GMRES(), "solver gmres");
  226. print_stat(QMR(), "solver qmr");
  227. print_stat(CG(), "solver cg");
  228. print_stat(P1, "no precond");
  229. print_stat(P2, "diag precond");
  230. print_stat(P3, "mr precond");
  231. print_stat(P4, "ilu precond");
  232. print_stat(P5, "ilut precond");
  233. print_stat(P5b, "ilutp precond");
  234. print_stat(P6, "ildlt precond");
  235. print_stat(P7, "ildltt precond");
  236. if (ratio_max > 0.2) GMM_ASSERT1(false, "something wrong ..");
  237. return true;
  238. }
  239. return false;
  240. }