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.

314 lines
9.3 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2012 Yves Renard, Julien Pommier
  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. /**@file gmm_MUMPS_interface.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>,
  28. @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
  29. @date December 8, 2005.
  30. @brief Interface with MUMPS (LU direct solver for sparse matrices).
  31. */
  32. #if defined(GMM_USES_MUMPS) || defined(HAVE_DMUMPS_C_H)
  33. #ifndef GMM_MUMPS_INTERFACE_H
  34. #define GMM_MUMPS_INTERFACE_H
  35. #include "gmm_kernel.h"
  36. extern "C" {
  37. #include <smumps_c.h>
  38. #undef F_INT
  39. #undef F_DOUBLE
  40. #undef F_DOUBLE2
  41. #include <dmumps_c.h>
  42. #undef F_INT
  43. #undef F_DOUBLE
  44. #undef F_DOUBLE2
  45. #include <cmumps_c.h>
  46. #undef F_INT
  47. #undef F_DOUBLE
  48. #undef F_DOUBLE2
  49. #include <zmumps_c.h>
  50. #undef F_INT
  51. #undef F_DOUBLE
  52. #undef F_DOUBLE2
  53. }
  54. namespace gmm {
  55. template <typename T> struct ij_sparse_matrix {
  56. std::vector<int> irn;
  57. std::vector<int> jcn;
  58. std::vector<T> a;
  59. template <typename L> void store(const L& l, size_type i) {
  60. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  61. ite = vect_const_end(l);
  62. for (; it != ite; ++it)
  63. { irn.push_back((int)i + 1); jcn.push_back((int)it.index() + 1); a.push_back(*it); }
  64. }
  65. template <typename L> void build_from(const L& l, row_major) {
  66. for (size_type i = 0; i < mat_nrows(l); ++i)
  67. store(mat_const_row(l, i), i);
  68. }
  69. template <typename L> void build_from(const L& l, col_major) {
  70. for (size_type i = 0; i < mat_ncols(l); ++i)
  71. store(mat_const_col(l, i), i);
  72. irn.swap(jcn);
  73. }
  74. template <typename L> ij_sparse_matrix(const L& A) {
  75. size_type nz = nnz(A);
  76. irn.reserve(nz); jcn.reserve(nz); a.reserve(nz);
  77. build_from(A, typename principal_orientation_type<typename
  78. linalg_traits<L>::sub_orientation>::potype());
  79. }
  80. };
  81. /* ********************************************************************* */
  82. /* MUMPS solve interface */
  83. /* ********************************************************************* */
  84. template <typename T> struct mumps_interf {};
  85. template <> struct mumps_interf<float> {
  86. typedef SMUMPS_STRUC_C MUMPS_STRUC_C;
  87. typedef float value_type;
  88. static void mumps_c(MUMPS_STRUC_C &id) { smumps_c(&id); }
  89. };
  90. template <> struct mumps_interf<double> {
  91. typedef DMUMPS_STRUC_C MUMPS_STRUC_C;
  92. typedef double value_type;
  93. static void mumps_c(MUMPS_STRUC_C &id) { dmumps_c(&id); }
  94. };
  95. template <> struct mumps_interf<std::complex<float> > {
  96. typedef CMUMPS_STRUC_C MUMPS_STRUC_C;
  97. typedef mumps_complex value_type;
  98. static void mumps_c(MUMPS_STRUC_C &id) { cmumps_c(&id); }
  99. };
  100. template <> struct mumps_interf<std::complex<double> > {
  101. typedef ZMUMPS_STRUC_C MUMPS_STRUC_C;
  102. typedef mumps_double_complex value_type;
  103. static void mumps_c(MUMPS_STRUC_C &id) { zmumps_c(&id); }
  104. };
  105. /** MUMPS solve interface
  106. * Works only with sparse or skyline matrices
  107. */
  108. template <typename MAT, typename VECTX, typename VECTB>
  109. bool MUMPS_solve(const MAT &A, const VECTX &X_, const VECTB &B) {
  110. VECTX &X = const_cast<VECTX &>(X_);
  111. typedef typename linalg_traits<MAT>::value_type T;
  112. typedef typename mumps_interf<T>::value_type MUMPS_T;
  113. GMM_ASSERT2(gmm::mat_nrows(A) == gmm::mat_ncols(A), "Non square matrix");
  114. std::vector<T> rhs(gmm::vect_size(B)); gmm::copy(B, rhs);
  115. ij_sparse_matrix<T> AA(A);
  116. const int JOB_INIT = -1;
  117. const int JOB_END = -2;
  118. const int USE_COMM_WORLD = -987654;
  119. typename mumps_interf<T>::MUMPS_STRUC_C id;
  120. #ifdef GMM_USES_MPI
  121. int rank;
  122. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  123. #endif
  124. id.job = JOB_INIT;
  125. id.par = 1;
  126. id.sym = 0;
  127. id.comm_fortran = USE_COMM_WORLD;
  128. mumps_interf<T>::mumps_c(id);
  129. #ifdef GMM_USES_MPI
  130. if (rank == 0) {
  131. #endif
  132. id.n = (int)gmm::mat_nrows(A);
  133. id.nz = (int)AA.irn.size();
  134. id.irn = &(AA.irn[0]);
  135. id.jcn = &(AA.jcn[0]);
  136. id.a = (MUMPS_T*)(&(AA.a[0]));
  137. id.rhs = (MUMPS_T*)(&(rhs[0]));
  138. #ifdef GMM_USES_MPI
  139. }
  140. #endif
  141. #define ICNTL(I) icntl[(I)-1]
  142. id.ICNTL(1) = -1; // output stream for error messages
  143. id.ICNTL(2) = -1; // output stream for other messages
  144. id.ICNTL(3) = -1; // output stream for global information
  145. id.ICNTL(4) = 0; // verbosity level
  146. id.ICNTL(14) += 80; /* small boost to the workspace size as we have encountered some problem
  147. who did not fit in the default settings of mumps..
  148. by default, ICNTL(14) = 15 or 20
  149. */
  150. //cout << "ICNTL(14): " << id.ICNTL(14) << "\n";
  151. // id.ICNTL(22) = 1; /* enables out-of-core support */
  152. id.job = 6;
  153. mumps_interf<T>::mumps_c(id);
  154. bool ok = mumps_error_check(id);
  155. id.job = JOB_END;
  156. mumps_interf<T>::mumps_c(id);
  157. gmm::copy(rhs, X);
  158. return ok;
  159. #undef ICNTL
  160. }
  161. /** MUMPS solve interface for distributed matrices
  162. * Works only with sparse or skyline matrices
  163. */
  164. template <typename MAT, typename VECTX, typename VECTB>
  165. bool MUMPS_distributed_matrix_solve(const MAT &A, const VECTX &X_,
  166. const VECTB &B) {
  167. VECTX &X = const_cast<VECTX &>(X_);
  168. typedef typename linalg_traits<MAT>::value_type T;
  169. typedef typename mumps_interf<T>::value_type MUMPS_T;
  170. GMM_ASSERT2(gmm::mat_nrows(A) == gmm::mat_ncols(A), "Non-square matrix");
  171. std::vector<T> rhs(gmm::vect_size(B)); gmm::copy(B, rhs);
  172. ij_sparse_matrix<T> AA(A);
  173. const int JOB_INIT = -1;
  174. const int JOB_END = -2;
  175. const int USE_COMM_WORLD = -987654;
  176. typename mumps_interf<T>::MUMPS_STRUC_C id;
  177. #ifdef GMM_USES_MPI
  178. int rank;
  179. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  180. #endif
  181. id.job = JOB_INIT;
  182. id.par = 1;
  183. id.sym = 0;
  184. id.comm_fortran = USE_COMM_WORLD;
  185. mumps_interf<T>::mumps_c(id);
  186. id.n = gmm::mat_nrows(A);
  187. id.nz_loc = AA.irn.size();
  188. id.irn_loc = &(AA.irn[0]);
  189. id.jcn_loc = &(AA.jcn[0]);
  190. id.a_loc = (MUMPS_T*)(&(AA.a[0]));
  191. #ifdef GMM_USES_MPI
  192. if (rank == 0) {
  193. #endif
  194. id.rhs = (MUMPS_T*)(&(rhs[0]));
  195. #ifdef GMM_USES_MPI
  196. }
  197. #endif
  198. #define ICNTL(I) icntl[(I)-1]
  199. id.ICNTL(1) = -1; // output stream for error messages
  200. id.ICNTL(2) = 6; // id.ICNTL(2) = -1; // output stream for other messages
  201. id.ICNTL(3) = 6; // id.ICNTL(3) = -1; // output stream for global information
  202. id.ICNTL(4) = 2; // verbosity level
  203. id.ICNTL(5) = 0; // assembled input matrix (default)
  204. id.ICNTL(18) = 3; // strategy for distributed input matrix
  205. id.job = 6;
  206. mumps_interf<T>::mumps_c(id);
  207. bool ok = mumps_error_check(id);
  208. id.job = JOB_END;
  209. mumps_interf<T>::mumps_c(id);
  210. #ifdef GMM_USES_MPI
  211. MPI_Bcast(&(rhs[0]),id.n,gmm::mpi_type(T()),0,MPI_COMM_WORLD);
  212. #endif
  213. gmm::copy(rhs, X);
  214. return ok;
  215. #undef ICNTL
  216. }
  217. template <typename MUMPS_STRUCT>
  218. static inline bool mumps_error_check(MUMPS_STRUCT &id) {
  219. #define INFO(I) info[(I)-1]
  220. if (id.INFO(1) < 0) {
  221. switch (id.INFO(1)) {
  222. case -2:
  223. GMM_ASSERT1(false, "Solve with MUMPS failed: NZ = " << id.INFO(2)
  224. << " is out of range");
  225. case -6 : case -10 :
  226. GMM_WARNING1("Solve with MUMPS failed: matrix is singular");
  227. return false;
  228. case -9:
  229. GMM_ASSERT1(false, "Solve with MUMPS failed: error "
  230. << id.INFO(1) << ", increase ICNTL(14)");
  231. case -13 :
  232. GMM_ASSERT1(false, "Solve with MUMPS failed: not enough memory");
  233. default :
  234. GMM_ASSERT1(false, "Solve with MUMPS failed with error "
  235. << id.INFO(1));
  236. }
  237. }
  238. return true;
  239. #undef INFO
  240. }
  241. }
  242. #endif // GMM_MUMPS_INTERFACE_H
  243. #endif // GMM_USES_MUMPS