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.

355 lines
11 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2017 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. #define ICNTL(I) icntl[(I)-1]
  56. #define INFO(I) info[(I)-1]
  57. #define INFOG(I) infog[(I)-1]
  58. #define RINFOG(I) rinfog[(I)-1]
  59. template <typename T> struct ij_sparse_matrix {
  60. std::vector<int> irn;
  61. std::vector<int> jcn;
  62. std::vector<T> a;
  63. bool sym;
  64. template <typename L> void store(const L& l, size_type i) {
  65. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  66. ite = vect_const_end(l);
  67. for (; it != ite; ++it) {
  68. int ir = (int)i + 1, jc = (int)it.index() + 1;
  69. if (*it != T(0) && (!sym || ir >= jc))
  70. { irn.push_back(ir); jcn.push_back(jc); a.push_back(*it); }
  71. }
  72. }
  73. template <typename L> void build_from(const L& l, row_major) {
  74. for (size_type i = 0; i < mat_nrows(l); ++i)
  75. store(mat_const_row(l, i), i);
  76. }
  77. template <typename L> void build_from(const L& l, col_major) {
  78. for (size_type i = 0; i < mat_ncols(l); ++i)
  79. store(mat_const_col(l, i), i);
  80. irn.swap(jcn);
  81. }
  82. template <typename L> ij_sparse_matrix(const L& A, bool sym_) {
  83. size_type nz = nnz(A);
  84. sym = sym_;
  85. irn.reserve(nz); jcn.reserve(nz); a.reserve(nz);
  86. build_from(A, typename principal_orientation_type<typename
  87. linalg_traits<L>::sub_orientation>::potype());
  88. }
  89. };
  90. /* ********************************************************************* */
  91. /* MUMPS solve interface */
  92. /* ********************************************************************* */
  93. template <typename T> struct mumps_interf {};
  94. template <> struct mumps_interf<float> {
  95. typedef SMUMPS_STRUC_C MUMPS_STRUC_C;
  96. typedef float value_type;
  97. static void mumps_c(MUMPS_STRUC_C &id) { smumps_c(&id); }
  98. };
  99. template <> struct mumps_interf<double> {
  100. typedef DMUMPS_STRUC_C MUMPS_STRUC_C;
  101. typedef double value_type;
  102. static void mumps_c(MUMPS_STRUC_C &id) { dmumps_c(&id); }
  103. };
  104. template <> struct mumps_interf<std::complex<float> > {
  105. typedef CMUMPS_STRUC_C MUMPS_STRUC_C;
  106. typedef mumps_complex value_type;
  107. static void mumps_c(MUMPS_STRUC_C &id) { cmumps_c(&id); }
  108. };
  109. template <> struct mumps_interf<std::complex<double> > {
  110. typedef ZMUMPS_STRUC_C MUMPS_STRUC_C;
  111. typedef mumps_double_complex value_type;
  112. static void mumps_c(MUMPS_STRUC_C &id) { zmumps_c(&id); }
  113. };
  114. template <typename MUMPS_STRUCT>
  115. static inline bool mumps_error_check(MUMPS_STRUCT &id) {
  116. if (id.INFO(1) < 0) {
  117. switch (id.INFO(1)) {
  118. case -2:
  119. GMM_ASSERT1(false, "Solve with MUMPS failed: NZ = " << id.INFO(2)
  120. << " is out of range");
  121. case -6 : case -10 :
  122. GMM_WARNING1("Solve with MUMPS failed: matrix is singular");
  123. return false;
  124. case -9:
  125. GMM_ASSERT1(false, "Solve with MUMPS failed: error "
  126. << id.INFO(1) << ", increase ICNTL(14)");
  127. case -13 :
  128. GMM_ASSERT1(false, "Solve with MUMPS failed: not enough memory");
  129. default :
  130. GMM_ASSERT1(false, "Solve with MUMPS failed with error "
  131. << id.INFO(1));
  132. }
  133. }
  134. return true;
  135. }
  136. /** MUMPS solve interface
  137. * Works only with sparse or skyline matrices
  138. */
  139. template <typename MAT, typename VECTX, typename VECTB>
  140. bool MUMPS_solve(const MAT &A, const VECTX &X_, const VECTB &B,
  141. bool sym = false, bool distributed = false) {
  142. VECTX &X = const_cast<VECTX &>(X_);
  143. typedef typename linalg_traits<MAT>::value_type T;
  144. typedef typename mumps_interf<T>::value_type MUMPS_T;
  145. GMM_ASSERT2(gmm::mat_nrows(A) == gmm::mat_ncols(A), "Non-square matrix");
  146. std::vector<T> rhs(gmm::vect_size(B)); gmm::copy(B, rhs);
  147. ij_sparse_matrix<T> AA(A, sym);
  148. const int JOB_INIT = -1;
  149. const int JOB_END = -2;
  150. const int USE_COMM_WORLD = -987654;
  151. typename mumps_interf<T>::MUMPS_STRUC_C id;
  152. int rank(0);
  153. #ifdef GMM_USES_MPI
  154. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  155. #endif
  156. id.job = JOB_INIT;
  157. id.par = 1;
  158. id.sym = sym ? 2 : 0;
  159. id.comm_fortran = USE_COMM_WORLD;
  160. mumps_interf<T>::mumps_c(id);
  161. if (rank == 0 || distributed) {
  162. id.n = int(gmm::mat_nrows(A));
  163. if (distributed) {
  164. id.nz_loc = int(AA.irn.size());
  165. id.irn_loc = &(AA.irn[0]);
  166. id.jcn_loc = &(AA.jcn[0]);
  167. id.a_loc = (MUMPS_T*)(&(AA.a[0]));
  168. } else {
  169. id.nz = int(AA.irn.size());
  170. id.irn = &(AA.irn[0]);
  171. id.jcn = &(AA.jcn[0]);
  172. id.a = (MUMPS_T*)(&(AA.a[0]));
  173. }
  174. if (rank == 0)
  175. id.rhs = (MUMPS_T*)(&(rhs[0]));
  176. }
  177. id.ICNTL(1) = -1; // output stream for error messages
  178. id.ICNTL(2) = -1; // output stream for other messages
  179. id.ICNTL(3) = -1; // output stream for global information
  180. id.ICNTL(4) = 0; // verbosity level
  181. if (distributed)
  182. id.ICNTL(5) = 0; // assembled input matrix (default)
  183. id.ICNTL(14) += 80; /* small boost to the workspace size as we have encountered some problem
  184. who did not fit in the default settings of mumps..
  185. by default, ICNTL(14) = 15 or 20
  186. */
  187. //cout << "ICNTL(14): " << id.ICNTL(14) << "\n";
  188. if (distributed)
  189. id.ICNTL(18) = 3; // strategy for distributed input matrix
  190. // id.ICNTL(22) = 1; /* enables out-of-core support */
  191. id.job = 6;
  192. mumps_interf<T>::mumps_c(id);
  193. bool ok = mumps_error_check(id);
  194. id.job = JOB_END;
  195. mumps_interf<T>::mumps_c(id);
  196. #ifdef GMM_USES_MPI
  197. MPI_Bcast(&(rhs[0]),id.n,gmm::mpi_type(T()),0,MPI_COMM_WORLD);
  198. #endif
  199. gmm::copy(rhs, X);
  200. return ok;
  201. }
  202. /** MUMPS solve interface for distributed matrices
  203. * Works only with sparse or skyline matrices
  204. */
  205. template <typename MAT, typename VECTX, typename VECTB>
  206. bool MUMPS_distributed_matrix_solve(const MAT &A, const VECTX &X_,
  207. const VECTB &B, bool sym = false) {
  208. return MUMPS_solve(A, X_, B, sym, true);
  209. }
  210. template<typename T>
  211. inline T real_or_complex(std::complex<T> a) { return a.real(); }
  212. template<typename T>
  213. inline T real_or_complex(T &a) { return a; }
  214. /** Evaluate matrix determinant with MUMPS
  215. * Works only with sparse or skyline matrices
  216. */
  217. template <typename MAT, typename T = typename linalg_traits<MAT>::value_type>
  218. T MUMPS_determinant(const MAT &A, int &exponent,
  219. bool sym = false, bool distributed = false) {
  220. exponent = 0;
  221. typedef typename mumps_interf<T>::value_type MUMPS_T;
  222. typedef typename number_traits<T>::magnitude_type R;
  223. GMM_ASSERT2(gmm::mat_nrows(A) == gmm::mat_ncols(A), "Non-square matrix");
  224. ij_sparse_matrix<T> AA(A, sym);
  225. const int JOB_INIT = -1;
  226. const int JOB_END = -2;
  227. const int USE_COMM_WORLD = -987654;
  228. typename mumps_interf<T>::MUMPS_STRUC_C id;
  229. int rank(0);
  230. #ifdef GMM_USES_MPI
  231. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  232. #endif
  233. id.job = JOB_INIT;
  234. id.par = 1;
  235. id.sym = sym ? 2 : 0;
  236. id.comm_fortran = USE_COMM_WORLD;
  237. mumps_interf<T>::mumps_c(id);
  238. if (rank == 0 || distributed) {
  239. id.n = int(gmm::mat_nrows(A));
  240. if (distributed) {
  241. id.nz_loc = int(AA.irn.size());
  242. id.irn_loc = &(AA.irn[0]);
  243. id.jcn_loc = &(AA.jcn[0]);
  244. id.a_loc = (MUMPS_T*)(&(AA.a[0]));
  245. } else {
  246. id.nz = int(AA.irn.size());
  247. id.irn = &(AA.irn[0]);
  248. id.jcn = &(AA.jcn[0]);
  249. id.a = (MUMPS_T*)(&(AA.a[0]));
  250. }
  251. }
  252. id.ICNTL(1) = -1; // output stream for error messages
  253. id.ICNTL(2) = -1; // output stream for other messages
  254. id.ICNTL(3) = -1; // output stream for global information
  255. id.ICNTL(4) = 0; // verbosity level
  256. if (distributed)
  257. id.ICNTL(5) = 0; // assembled input matrix (default)
  258. // id.ICNTL(14) += 80; // small boost to the workspace size
  259. if (distributed)
  260. id.ICNTL(18) = 3; // strategy for distributed input matrix
  261. id.ICNTL(31) = 1; // only factorization, no solution to follow
  262. id.ICNTL(33) = 1; // request determinant calculation
  263. id.job = 4; // abalysis (job=1) + factorization (job=2)
  264. mumps_interf<T>::mumps_c(id);
  265. mumps_error_check(id);
  266. T det = real_or_complex(std::complex<R>(id.RINFOG(12),id.RINFOG(13)));
  267. exponent = id.INFOG(34);
  268. id.job = JOB_END;
  269. mumps_interf<T>::mumps_c(id);
  270. return det;
  271. }
  272. #undef ICNTL
  273. #undef INFO
  274. #undef INFOG
  275. #undef RINFOG
  276. }
  277. #endif // GMM_MUMPS_INTERFACE_H
  278. #endif // GMM_USES_MUMPS