Actual source code: imumps.c
1: /*
2: Provides an interface to the MUMPS sparse solver
3: */
4: #include <petscpkg_version.h>
5: #include <petscsf.h>
6: #include <../src/mat/impls/aij/mpi/mpiaij.h>
7: #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
8: #include <../src/mat/impls/sell/mpi/mpisell.h>
9: #include <petsc/private/vecimpl.h>
11: #define MUMPS_MANUALS "(see users manual https://mumps-solver.org/index.php?page=doc \"Error and warning diagnostics\")"
13: EXTERN_C_BEGIN
14: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
15: #include <cmumps_c.h>
16: #include <zmumps_c.h>
17: #include <smumps_c.h>
18: #include <dmumps_c.h>
19: #else
20: #if PetscDefined(USE_COMPLEX)
21: #if PetscDefined(USE_REAL_SINGLE)
22: #include <cmumps_c.h>
23: #define MUMPS_c cmumps_c
24: #define MumpsScalar CMUMPS_COMPLEX
25: #else
26: #include <zmumps_c.h>
27: #define MUMPS_c zmumps_c
28: #define MumpsScalar ZMUMPS_COMPLEX
29: #endif
30: #else
31: #if PetscDefined(USE_REAL_SINGLE)
32: #include <smumps_c.h>
33: #define MUMPS_c smumps_c
34: #define MumpsScalar SMUMPS_REAL
35: #else
36: #include <dmumps_c.h>
37: #define MUMPS_c dmumps_c
38: #define MumpsScalar DMUMPS_REAL
39: #endif
40: #endif
41: #endif
42: #if PetscDefined(USE_COMPLEX)
43: #if PetscDefined(USE_REAL_SINGLE)
44: #define MUMPS_STRUC_C CMUMPS_STRUC_C
45: #else
46: #define MUMPS_STRUC_C ZMUMPS_STRUC_C
47: #endif
48: #else
49: #if PetscDefined(USE_REAL_SINGLE)
50: #define MUMPS_STRUC_C SMUMPS_STRUC_C
51: #else
52: #define MUMPS_STRUC_C DMUMPS_STRUC_C
53: #endif
54: #endif
55: EXTERN_C_END
57: #define JOB_INIT -1
58: #define JOB_NULL 0
59: #define JOB_FACTSYMBOLIC 1
60: #define JOB_FACTNUMERIC 2
61: #define JOB_SOLVE 3
62: #define JOB_END -2
64: /* MUMPS uses MUMPS_INT for nonzero indices such as irn/jcn, irn_loc/jcn_loc and uses int64_t for
65: number of nonzeros such as nnz, nnz_loc. We typedef MUMPS_INT to PetscMUMPSInt to follow the
66: naming convention in PetscMPIInt, PetscBLASInt etc.
67: */
68: typedef MUMPS_INT PetscMUMPSInt;
70: #if PETSC_PKG_MUMPS_VERSION_GE(5, 3, 0)
71: #if defined(MUMPS_INTSIZE64) /* MUMPS_INTSIZE64 is in MUMPS headers if it is built in full 64-bit mode, therefore the macro is more reliable */
72: #error "PETSc has not been tested with full 64-bit MUMPS and we choose to error out"
73: #endif
74: #else
75: #if defined(INTSIZE64) /* INTSIZE64 is a command line macro one used to build MUMPS in full 64-bit mode */
76: #error "PETSc has not been tested with full 64-bit MUMPS and we choose to error out"
77: #endif
78: #endif
80: #define MPIU_MUMPSINT MPI_INT
81: #define PETSC_MUMPS_INT_MAX 2147483647
82: #define PETSC_MUMPS_INT_MIN -2147483648
84: /* Cast PetscInt to PetscMUMPSInt. Usually there is no overflow since <a> is row/col indices or some small integers*/
85: static inline PetscErrorCode PetscMUMPSIntCast(PetscCount a, PetscMUMPSInt *b)
86: {
87: PetscFunctionBegin;
88: PetscAssert(!PetscDefined(USE_64BIT_INDICES) || (a <= PETSC_MUMPS_INT_MAX && a >= PETSC_MUMPS_INT_MIN), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "PetscInt too long for PetscMUMPSInt");
89: *b = (PetscMUMPSInt)a;
90: PetscFunctionReturn(PETSC_SUCCESS);
91: }
93: /* Put these utility routines here since they are only used in this file */
94: static inline PetscErrorCode PetscOptionsMUMPSInt_Private(PetscOptionItems PetscOptionsObject, const char opt[], const char text[], const char man[], PetscMUMPSInt currentvalue, PetscMUMPSInt *value, PetscBool *set, PetscMUMPSInt lb, PetscMUMPSInt ub)
95: {
96: PetscInt myval;
97: PetscBool myset;
99: PetscFunctionBegin;
100: /* PetscInt's size should be always >= PetscMUMPSInt's. It is safe to call PetscOptionsInt_Private to read a PetscMUMPSInt */
101: PetscCall(PetscOptionsInt_Private(PetscOptionsObject, opt, text, man, (PetscInt)currentvalue, &myval, &myset, lb, ub));
102: if (myset) PetscCall(PetscMUMPSIntCast(myval, value));
103: if (set) *set = myset;
104: PetscFunctionReturn(PETSC_SUCCESS);
105: }
106: #define PetscOptionsMUMPSInt(a, b, c, d, e, f) PetscOptionsMUMPSInt_Private(PetscOptionsObject, a, b, c, d, e, f, PETSC_MUMPS_INT_MIN, PETSC_MUMPS_INT_MAX)
108: // An abstract type for specific MUMPS types {S,D,C,Z}MUMPS_STRUC_C.
109: //
110: // With the abstract (outer) type, we can write shared code. We call MUMPS through a type-to-be-determined inner field within the abstract type.
111: // Before/after calling MUMPS, we need to copy in/out fields between the outer and the inner, which seems expensive. But note that the large fixed size
112: // arrays within the types are directly linked. At the end, we only need to copy ~20 integers/pointers, which is doable. See PreMumpsCall()/PostMumpsCall().
113: //
114: // Not all fields in the specific types are exposed in the abstract type. We only need those used by the PETSc/MUMPS interface.
115: // Notably, DMUMPS_COMPLEX* and DMUMPS_REAL* fields are now declared as void *. Their type will be determined by the the actual precision to be used.
116: // Also note that we added some *_len fields not in specific types to track sizes of those MumpsScalar buffers.
117: typedef struct {
118: PetscPrecision precision; // precision used by MUMPS
119: void *internal_id; // the data structure passed to MUMPS, whose actual type {S,D,C,Z}MUMPS_STRUC_C is to be decided by precision and PETSc's use of complex
121: // aliased fields from internal_id, so that we can use XMUMPS_STRUC_C to write shared code across different precisions.
122: MUMPS_INT sym, par, job;
123: MUMPS_INT comm_fortran; /* Fortran communicator */
124: MUMPS_INT *icntl;
125: void *cntl; // MumpsReal, fixed size array
126: MUMPS_INT n;
127: MUMPS_INT nblk;
129: /* Assembled entry */
130: MUMPS_INT8 nnz;
131: MUMPS_INT *irn;
132: MUMPS_INT *jcn;
133: void *a; // MumpsScalar, centralized input
134: PetscCount a_len;
136: /* Distributed entry */
137: MUMPS_INT8 nnz_loc;
138: MUMPS_INT *irn_loc;
139: MUMPS_INT *jcn_loc;
140: void *a_loc; // MumpsScalar, distributed input
141: PetscCount a_loc_len;
143: /* Matrix by blocks */
144: MUMPS_INT *blkptr;
145: MUMPS_INT *blkvar;
147: /* Ordering, if given by user */
148: MUMPS_INT *perm_in;
150: /* RHS, solution, ouptput data and statistics */
151: void *rhs, *redrhs, *rhs_sparse, *sol_loc, *rhs_loc; // MumpsScalar buffers
152: PetscCount rhs_len, redrhs_len, rhs_sparse_len, sol_loc_len, rhs_loc_len; // length of buffers (in MumpsScalar) IF allocated in a different precision than PetscScalar
154: MUMPS_INT *irhs_sparse, *irhs_ptr, *isol_loc, *irhs_loc;
155: MUMPS_INT nrhs, lrhs, lredrhs, nz_rhs, lsol_loc, nloc_rhs, lrhs_loc;
156: // MUMPS_INT nsol_loc; // introduced in MUMPS-5.7, but PETSc doesn't use it; would cause compile errors with the widely used 5.6. If you add it, must also update PreMumpsCall() and guard this with #if PETSC_PKG_MUMPS_VERSION_GE(5, 7, 0)
157: MUMPS_INT schur_lld;
158: MUMPS_INT *info, *infog; // fixed size array
159: void *rinfo, *rinfog; // MumpsReal, fixed size array
161: /* Null space */
162: MUMPS_INT *pivnul_list; // allocated by MUMPS!
163: MUMPS_INT *mapping; // allocated by MUMPS!
165: /* Schur */
166: MUMPS_INT size_schur;
167: MUMPS_INT *listvar_schur;
168: void *schur; // MumpsScalar
169: PetscCount schur_len;
171: /* For out-of-core */
172: char *ooc_tmpdir; // fixed size array
173: char *ooc_prefix; // fixed size array
174: } XMUMPS_STRUC_C;
176: // Note: fixed-size arrays are allocated by MUMPS; redirect them to the outer struct
177: #define AllocateInternalID(MUMPS_STRUC_T, outer) \
178: do { \
179: MUMPS_STRUC_T *inner; \
180: PetscCall(PetscNew(&inner)); \
181: outer->icntl = inner->icntl; \
182: outer->cntl = inner->cntl; \
183: outer->info = inner->info; \
184: outer->infog = inner->infog; \
185: outer->rinfo = inner->rinfo; \
186: outer->rinfog = inner->rinfog; \
187: outer->ooc_tmpdir = inner->ooc_tmpdir; \
188: outer->ooc_prefix = inner->ooc_prefix; \
189: /* the three field should never change after init */ \
190: inner->comm_fortran = outer->comm_fortran; \
191: inner->par = outer->par; \
192: inner->sym = outer->sym; \
193: outer->internal_id = inner; \
194: } while (0)
196: // Allocate the internal [SDCZ]MUMPS_STRUC_C ID data structure in the given , and link fields of the outer and the inner
197: static inline PetscErrorCode MatMumpsAllocateInternalID(XMUMPS_STRUC_C *outer, PetscPrecision precision)
198: {
199: PetscFunctionBegin;
200: outer->precision = precision;
201: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
202: #if PetscDefined(USE_COMPLEX)
203: if (precision == PETSC_PRECISION_SINGLE) AllocateInternalID(CMUMPS_STRUC_C, outer);
204: else AllocateInternalID(ZMUMPS_STRUC_C, outer);
205: #else
206: if (precision == PETSC_PRECISION_SINGLE) AllocateInternalID(SMUMPS_STRUC_C, outer);
207: else AllocateInternalID(DMUMPS_STRUC_C, outer);
208: #endif
209: #else
210: AllocateInternalID(MUMPS_STRUC_C, outer);
211: #endif
212: PetscFunctionReturn(PETSC_SUCCESS);
213: }
215: #define FreeInternalIDFields(MUMPS_STRUC_T, outer) \
216: do { \
217: MUMPS_STRUC_T *inner = (MUMPS_STRUC_T *)(outer)->internal_id; \
218: PetscCall(PetscFree(inner->a)); \
219: PetscCall(PetscFree(inner->a_loc)); \
220: PetscCall(PetscFree(inner->rhs)); \
221: PetscCall(PetscFree(inner->rhs_sparse)); \
222: PetscCall(PetscFree(inner->rhs_loc)); \
223: PetscCall(PetscFree(inner->sol_loc)); \
224: } while (0)
226: static inline PetscErrorCode MatMumpsFreeInternalID(XMUMPS_STRUC_C *outer)
227: {
228: PetscFunctionBegin;
229: if (outer->internal_id) { // sometimes, the inner is never created before we destroy the outer
230: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
231: const PetscPrecision mumps_precision = outer->precision;
232: if (mumps_precision != PETSC_SCALAR_PRECISION) { // Free internal buffers if we used mixed precision
233: #if PetscDefined(USE_COMPLEX)
234: if (mumps_precision == PETSC_PRECISION_SINGLE) FreeInternalIDFields(CMUMPS_STRUC_C, outer);
235: else FreeInternalIDFields(ZMUMPS_STRUC_C, outer);
236: #else
237: if (mumps_precision == PETSC_PRECISION_SINGLE) FreeInternalIDFields(SMUMPS_STRUC_C, outer);
238: else FreeInternalIDFields(DMUMPS_STRUC_C, outer);
239: #endif
240: }
241: #endif
242: PetscCall(PetscFree(outer->internal_id));
243: }
244: PetscFunctionReturn(PETSC_SUCCESS);
245: }
247: // Make a companion MumpsScalar array (with a given PetscScalar array), to hold at least MumpsScalars in the given and return the address at .
248: // indicates if we need to convert PetscScalars to MumpsScalars after allocating the MumpsScalar array.
249: // (For brevity, we use for array address and for its length in MumpsScalar, though in code they should be <*ma> and <*m>)
250: // If already points to a buffer/array, on input should be its length. Note the buffer might be freed if it is not big enough for this request.
251: //
252: // The returned array is a companion, so how it is created depends on if PetscScalar and MumpsScalar are the same.
253: // 1) If they are different, a separate array will be made and its length and address will be provided at and on output.
254: // 2) Otherwise, will be returned in , and will be zero on output.
255: //
256: //
257: // Input parameters:
258: // + convert - whether to do PetscScalar to MumpsScalar conversion
259: // . n - length of the PetscScalar array
260: // . pa - [n]], points to the PetscScalar array
261: // . precision - precision of MumpsScalar
262: // . m - on input, length of an existing MumpsScalar array if any, otherwise *m is just zero.
263: // - ma - on input, an existing MumpsScalar array if any.
264: //
265: // Output parameters:
266: // + m - length of the MumpsScalar buffer at if MumpsScalar is different from PetscScalar, otherwise 0
267: // . ma - the MumpsScalar array, which could be an alias of when the two types are the same.
268: //
269: // Note:
270: // New memory, if allocated, is done via PetscMalloc1(), and is owned by caller.
271: static PetscErrorCode MatMumpsMakeMumpsScalarArray(PetscBool convert, PetscCount n, const PetscScalar *pa, PetscPrecision precision, PetscCount *m, void **ma)
272: {
273: PetscFunctionBegin;
274: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
275: const PetscPrecision mumps_precision = precision;
276: PetscCheck(precision == PETSC_PRECISION_SINGLE || precision == PETSC_PRECISION_DOUBLE, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unsupported precicison (%d). Must be single or double", (int)precision);
277: #if PetscDefined(USE_COMPLEX)
278: if (mumps_precision != PETSC_SCALAR_PRECISION) {
279: if (mumps_precision == PETSC_PRECISION_SINGLE) {
280: if (*m < n) {
281: PetscCall(PetscFree(*ma));
282: PetscCall(PetscMalloc1(n, (CMUMPS_COMPLEX **)ma));
283: *m = n;
284: }
285: if (convert) {
286: CMUMPS_COMPLEX *b = *(CMUMPS_COMPLEX **)ma;
287: for (PetscCount i = 0; i < n; i++) {
288: b[i].r = PetscRealPart(pa[i]);
289: b[i].i = PetscImaginaryPart(pa[i]);
290: }
291: }
292: } else {
293: if (*m < n) {
294: PetscCall(PetscFree(*ma));
295: PetscCall(PetscMalloc1(n, (ZMUMPS_COMPLEX **)ma));
296: *m = n;
297: }
298: if (convert) {
299: ZMUMPS_COMPLEX *b = *(ZMUMPS_COMPLEX **)ma;
300: for (PetscCount i = 0; i < n; i++) {
301: b[i].r = PetscRealPart(pa[i]);
302: b[i].i = PetscImaginaryPart(pa[i]);
303: }
304: }
305: }
306: }
307: #else
308: if (mumps_precision != PETSC_SCALAR_PRECISION) {
309: if (mumps_precision == PETSC_PRECISION_SINGLE) {
310: if (*m < n) {
311: PetscCall(PetscFree(*ma));
312: PetscCall(PetscMalloc1(n, (SMUMPS_REAL **)ma));
313: *m = n;
314: }
315: if (convert) {
316: SMUMPS_REAL *b = *(SMUMPS_REAL **)ma;
317: for (PetscCount i = 0; i < n; i++) b[i] = pa[i];
318: }
319: } else {
320: if (*m < n) {
321: PetscCall(PetscFree(*ma));
322: PetscCall(PetscMalloc1(n, (DMUMPS_REAL **)ma));
323: *m = n;
324: }
325: if (convert) {
326: DMUMPS_REAL *b = *(DMUMPS_REAL **)ma;
327: for (PetscCount i = 0; i < n; i++) b[i] = pa[i];
328: }
329: }
330: }
331: #endif
332: else
333: #endif
334: {
335: if (*m != 0) PetscCall(PetscFree(*ma)); // free existing buffer if any
336: *ma = (void *)pa; // same precision, make them alias
337: *m = 0;
338: }
339: PetscFunctionReturn(PETSC_SUCCESS);
340: }
342: // Cast a MumpsScalar array in to a PetscScalar array at address .
343: //
344: // 1) If the two types are different, cast array elements.
345: // 2) Otherwise, this works as a memcpy; of course, if the two addresses are equal, it is a no-op.
346: static PetscErrorCode MatMumpsCastMumpsScalarArray(PetscCount n, PetscPrecision mumps_precision, const void *ma, PetscScalar *pa)
347: {
348: PetscFunctionBegin;
349: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
350: if (mumps_precision != PETSC_SCALAR_PRECISION) {
351: #if PetscDefined(USE_COMPLEX)
352: if (mumps_precision == PETSC_PRECISION_SINGLE) {
353: PetscReal *a = (PetscReal *)pa;
354: const SMUMPS_REAL *b = (const SMUMPS_REAL *)ma;
355: for (PetscCount i = 0; i < 2 * n; i++) a[i] = b[i];
356: } else {
357: PetscReal *a = (PetscReal *)pa;
358: const DMUMPS_REAL *b = (const DMUMPS_REAL *)ma;
359: for (PetscCount i = 0; i < 2 * n; i++) a[i] = b[i];
360: }
361: #else
362: if (mumps_precision == PETSC_PRECISION_SINGLE) {
363: const SMUMPS_REAL *b = (const SMUMPS_REAL *)ma;
364: for (PetscCount i = 0; i < n; i++) pa[i] = b[i];
365: } else {
366: const DMUMPS_REAL *b = (const DMUMPS_REAL *)ma;
367: for (PetscCount i = 0; i < n; i++) pa[i] = b[i];
368: }
369: #endif
370: } else
371: #endif
372: PetscCall(PetscArraycpy((PetscScalar *)pa, (PetscScalar *)ma, n));
373: PetscFunctionReturn(PETSC_SUCCESS);
374: }
376: // Cast a PetscScalar array to a MumpsScalar array in the given at address .
377: //
378: // 1) If the two types are different, cast array elements.
379: // 2) Otherwise, this works as a memcpy; of course, if the two addresses are equal, it is a no-op.
380: static PetscErrorCode MatMumpsCastPetscScalarArray(PetscCount n, const PetscScalar *pa, PetscPrecision mumps_precision, const void *ma)
381: {
382: PetscFunctionBegin;
383: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
384: if (mumps_precision != PETSC_SCALAR_PRECISION) {
385: #if PetscDefined(USE_COMPLEX)
386: if (mumps_precision == PETSC_PRECISION_SINGLE) {
387: CMUMPS_COMPLEX *b = (CMUMPS_COMPLEX *)ma;
388: for (PetscCount i = 0; i < n; i++) {
389: b[i].r = PetscRealPart(pa[i]);
390: b[i].i = PetscImaginaryPart(pa[i]);
391: }
392: } else {
393: ZMUMPS_COMPLEX *b = (ZMUMPS_COMPLEX *)ma;
394: for (PetscCount i = 0; i < n; i++) {
395: b[i].r = PetscRealPart(pa[i]);
396: b[i].i = PetscImaginaryPart(pa[i]);
397: }
398: }
399: #else
400: if (mumps_precision == PETSC_PRECISION_SINGLE) {
401: SMUMPS_REAL *b = (SMUMPS_REAL *)ma;
402: for (PetscCount i = 0; i < n; i++) b[i] = pa[i];
403: } else {
404: DMUMPS_REAL *b = (DMUMPS_REAL *)ma;
405: for (PetscCount i = 0; i < n; i++) b[i] = pa[i];
406: }
407: #endif
408: } else
409: #endif
410: PetscCall(PetscArraycpy((PetscScalar *)ma, (PetscScalar *)pa, n));
411: PetscFunctionReturn(PETSC_SUCCESS);
412: }
414: static inline MPI_Datatype MPIU_MUMPSREAL(const XMUMPS_STRUC_C *id)
415: {
416: return id->precision == PETSC_PRECISION_DOUBLE ? MPI_DOUBLE : MPI_FLOAT;
417: }
419: #define PreMumpsCall(inner, outer, mumpsscalar) \
420: do { \
421: inner->job = outer->job; \
422: inner->n = outer->n; \
423: inner->nblk = outer->nblk; \
424: inner->nnz = outer->nnz; \
425: inner->irn = outer->irn; \
426: inner->jcn = outer->jcn; \
427: inner->a = (mumpsscalar *)outer->a; \
428: inner->nnz_loc = outer->nnz_loc; \
429: inner->irn_loc = outer->irn_loc; \
430: inner->jcn_loc = outer->jcn_loc; \
431: inner->a_loc = (mumpsscalar *)outer->a_loc; \
432: inner->blkptr = outer->blkptr; \
433: inner->blkvar = outer->blkvar; \
434: inner->perm_in = outer->perm_in; \
435: inner->rhs = (mumpsscalar *)outer->rhs; \
436: inner->redrhs = (mumpsscalar *)outer->redrhs; \
437: inner->rhs_sparse = (mumpsscalar *)outer->rhs_sparse; \
438: inner->sol_loc = (mumpsscalar *)outer->sol_loc; \
439: inner->rhs_loc = (mumpsscalar *)outer->rhs_loc; \
440: inner->irhs_sparse = outer->irhs_sparse; \
441: inner->irhs_ptr = outer->irhs_ptr; \
442: inner->isol_loc = outer->isol_loc; \
443: inner->irhs_loc = outer->irhs_loc; \
444: inner->nrhs = outer->nrhs; \
445: inner->lrhs = outer->lrhs; \
446: inner->lredrhs = outer->lredrhs; \
447: inner->nz_rhs = outer->nz_rhs; \
448: inner->lsol_loc = outer->lsol_loc; \
449: inner->nloc_rhs = outer->nloc_rhs; \
450: inner->lrhs_loc = outer->lrhs_loc; \
451: inner->schur_lld = outer->schur_lld; \
452: inner->size_schur = outer->size_schur; \
453: inner->listvar_schur = outer->listvar_schur; \
454: inner->schur = (mumpsscalar *)outer->schur; \
455: } while (0)
457: #define PostMumpsCall(inner, outer) \
458: do { \
459: outer->pivnul_list = inner->pivnul_list; \
460: outer->mapping = inner->mapping; \
461: } while (0)
463: // Entry for PETSc to call mumps
464: static inline PetscErrorCode PetscCallMumps_Private(XMUMPS_STRUC_C *outer)
465: {
466: PetscFunctionBegin;
467: #if PetscDefined(HAVE_MUMPS_MIXED_PRECISION)
468: #if PetscDefined(USE_COMPLEX)
469: if (outer->precision == PETSC_PRECISION_SINGLE) {
470: CMUMPS_STRUC_C *inner = (CMUMPS_STRUC_C *)outer->internal_id;
471: PreMumpsCall(inner, outer, CMUMPS_COMPLEX);
472: PetscCallExternalVoid("cmumps_c", cmumps_c(inner));
473: PostMumpsCall(inner, outer);
474: } else {
475: ZMUMPS_STRUC_C *inner = (ZMUMPS_STRUC_C *)outer->internal_id;
476: PreMumpsCall(inner, outer, ZMUMPS_COMPLEX);
477: PetscCallExternalVoid("zmumps_c", zmumps_c(inner));
478: PostMumpsCall(inner, outer);
479: }
480: #else
481: if (outer->precision == PETSC_PRECISION_SINGLE) {
482: SMUMPS_STRUC_C *inner = (SMUMPS_STRUC_C *)outer->internal_id;
483: PreMumpsCall(inner, outer, SMUMPS_REAL);
484: PetscCallExternalVoid("smumps_c", smumps_c(inner));
485: PostMumpsCall(inner, outer);
486: } else {
487: DMUMPS_STRUC_C *inner = (DMUMPS_STRUC_C *)outer->internal_id;
488: PreMumpsCall(inner, outer, DMUMPS_REAL);
489: PetscCallExternalVoid("dmumps_c", dmumps_c(inner));
490: PostMumpsCall(inner, outer);
491: }
492: #endif
493: #else
494: MUMPS_STRUC_C *inner = (MUMPS_STRUC_C *)outer->internal_id;
495: PreMumpsCall(inner, outer, MumpsScalar);
496: PetscCallExternalVoid(PetscStringize(MUMPS_c), MUMPS_c(inner));
497: PostMumpsCall(inner, outer);
498: #endif
499: PetscFunctionReturn(PETSC_SUCCESS);
500: }
502: /* macros s.t. indices match MUMPS documentation */
503: #define ICNTL(I) icntl[(I) - 1]
504: #define INFOG(I) infog[(I) - 1]
505: #define INFO(I) info[(I) - 1]
507: // Get a value from a MumpsScalar array, which is the field in the struct of MUMPS_STRUC_C. The value is convertible to PetscScalar. Note no minus 1 on I!
508: #if PetscDefined(USE_COMPLEX)
509: #define ID_FIELD_GET(ID, F, I) ((ID).precision == PETSC_PRECISION_SINGLE ? ((CMUMPS_COMPLEX *)(ID).F)[I].r + PETSC_i * ((CMUMPS_COMPLEX *)(ID).F)[I].i : ((ZMUMPS_COMPLEX *)(ID).F)[I].r + PETSC_i * ((ZMUMPS_COMPLEX *)(ID).F)[I].i)
510: #else
511: #define ID_FIELD_GET(ID, F, I) ((ID).precision == PETSC_PRECISION_SINGLE ? ((float *)(ID).F)[I] : ((double *)(ID).F)[I])
512: #endif
514: // Get a value from MumpsReal arrays. The value is convertible to PetscReal.
515: #define ID_CNTL_GET(ID, I) ((ID).precision == PETSC_PRECISION_SINGLE ? ((float *)(ID).cntl)[(I) - 1] : ((double *)(ID).cntl)[(I) - 1])
516: #define ID_RINFOG_GET(ID, I) ((ID).precision == PETSC_PRECISION_SINGLE ? ((float *)(ID).rinfog)[(I) - 1] : ((double *)(ID).rinfog)[(I) - 1])
517: #define ID_RINFO_GET(ID, I) ((ID).precision == PETSC_PRECISION_SINGLE ? ((float *)(ID).rinfo)[(I) - 1] : ((double *)(ID).rinfo)[(I) - 1])
519: // Set the I-th entry of the MumpsReal array id.cntl[] with a PetscReal
520: #define ID_CNTL_SET(ID, I, VAL) \
521: do { \
522: if ((ID).precision == PETSC_PRECISION_SINGLE) ((float *)(ID).cntl)[(I) - 1] = (VAL); \
523: else ((double *)(ID).cntl)[(I) - 1] = (VAL); \
524: } while (0)
526: /* if using PETSc OpenMP support, we only call MUMPS on master ranks. Before/after the call, we change/restore CPUs the master ranks can run on */
527: #if PetscDefined(HAVE_OPENMP_SUPPORT)
528: #define PetscMUMPS_c(mumps) \
529: do { \
530: if (mumps->use_petsc_omp_support) { \
531: if (mumps->is_omp_master) { \
532: PetscCall(PetscOmpCtrlOmpRegionOnMasterBegin(mumps->omp_ctrl)); \
533: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); \
534: PetscCall(PetscCallMumps_Private(&mumps->id)); \
535: PetscCall(PetscFPTrapPop()); \
536: PetscCall(PetscOmpCtrlOmpRegionOnMasterEnd(mumps->omp_ctrl)); \
537: } \
538: PetscCall(PetscOmpCtrlBarrier(mumps->omp_ctrl)); \
539: /* Global info is same on all processes so we Bcast it within omp_comm. Local info is specific \
540: to processes, so we only Bcast info[1], an error code and leave others (since they do not have \
541: an easy translation between omp_comm and petsc_comm). See MUMPS-5.1.2 manual p82. \
542: omp_comm is a small shared memory communicator, hence doing multiple Bcast as shown below is OK. \
543: */ \
544: MUMPS_STRUC_C tmp; /* All MUMPS_STRUC_C types have same lengths on these info arrays */ \
545: PetscCallMPI(MPI_Bcast(mumps->id.infog, PETSC_STATIC_ARRAY_LENGTH(tmp.infog), MPIU_MUMPSINT, 0, mumps->omp_comm)); \
546: PetscCallMPI(MPI_Bcast(mumps->id.info, PETSC_STATIC_ARRAY_LENGTH(tmp.info), MPIU_MUMPSINT, 0, mumps->omp_comm)); \
547: PetscCallMPI(MPI_Bcast(mumps->id.rinfog, PETSC_STATIC_ARRAY_LENGTH(tmp.rinfog), MPIU_MUMPSREAL(&mumps->id), 0, mumps->omp_comm)); \
548: PetscCallMPI(MPI_Bcast(mumps->id.rinfo, PETSC_STATIC_ARRAY_LENGTH(tmp.rinfo), MPIU_MUMPSREAL(&mumps->id), 0, mumps->omp_comm)); \
549: } else { \
550: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); \
551: PetscCall(PetscCallMumps_Private(&mumps->id)); \
552: PetscCall(PetscFPTrapPop()); \
553: } \
554: } while (0)
555: #else
556: #define PetscMUMPS_c(mumps) \
557: do { \
558: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); \
559: PetscCall(PetscCallMumps_Private(&mumps->id)); \
560: PetscCall(PetscFPTrapPop()); \
561: } while (0)
562: #endif
564: typedef struct Mat_MUMPS Mat_MUMPS;
565: struct Mat_MUMPS {
566: XMUMPS_STRUC_C id;
568: MatStructure matstruc;
569: PetscMPIInt myid, petsc_size;
570: PetscMUMPSInt *irn, *jcn; /* the (i,j,v) triplets passed to mumps. */
571: PetscScalar *val, *val_alloc; /* For some matrices, we can directly access their data array without a buffer. For others, we need a buffer. So comes val_alloc. */
572: PetscCount nnz; /* number of nonzeros. The type is called selective 64-bit in mumps */
573: PetscMUMPSInt sym;
574: MPI_Comm mumps_comm;
575: PetscMUMPSInt *ICNTL_pre;
576: PetscReal *CNTL_pre;
577: PetscMUMPSInt ICNTL9_pre; /* check if ICNTL(9) is changed from previous MatSolve */
578: VecScatter scat_rhs, scat_sol; /* used by MatSolve() */
579: PetscMUMPSInt ICNTL20; /* use centralized (0) or distributed (10) dense RHS */
580: PetscMUMPSInt ICNTL26;
581: PetscMUMPSInt lrhs_loc, nloc_rhs, *irhs_loc;
582: #if PetscDefined(HAVE_OPENMP_SUPPORT)
583: PetscInt *rhs_nrow, max_nrhs;
584: PetscMPIInt *rhs_recvcounts, *rhs_disps;
585: PetscScalar *rhs_loc, *rhs_recvbuf;
586: #endif
587: Vec b_seq, x_seq;
588: PetscInt ninfo, *info; /* which INFO to display */
589: PetscInt sizeredrhs;
590: PetscScalar *schur_sol;
591: PetscInt schur_sizesol;
592: PetscScalar *redrhs; // buffer in PetscScalar in case MumpsScalar is in a different precision
593: PetscMUMPSInt *ia_alloc, *ja_alloc; /* work arrays used for the CSR struct for sparse rhs */
594: PetscCount cur_ilen, cur_jlen; /* current len of ia_alloc[], ja_alloc[] */
595: PetscErrorCode (*ConvertToTriples)(Mat, PetscInt, MatReuse, Mat_MUMPS *);
597: /* Support for MATNEST */
598: PetscErrorCode (**nest_convert_to_triples)(Mat, PetscInt, MatReuse, Mat_MUMPS *);
599: PetscCount *nest_vals_start;
600: PetscScalar *nest_vals;
602: /* stuff used by petsc/mumps OpenMP support*/
603: PetscBool use_petsc_omp_support;
604: PetscOmpCtrl omp_ctrl; /* an OpenMP controller that blocked processes will release their CPU (MPI_Barrier does not have this guarantee) */
605: MPI_Comm petsc_comm, omp_comm; /* petsc_comm is PETSc matrix's comm */
606: PetscCount *recvcount; /* a collection of nnz on omp_master */
607: PetscMPIInt tag, omp_comm_size;
608: PetscBool is_omp_master; /* is this rank the master of omp_comm */
609: MPI_Request *reqs;
610: };
612: /* Cast a 1-based CSR represented by (nrow, ia, ja) of type PetscInt to a CSR of type PetscMUMPSInt.
613: Here, nrow is number of rows, ia[] is row pointer and ja[] is column indices.
614: */
615: static PetscErrorCode PetscMUMPSIntCSRCast(PETSC_UNUSED Mat_MUMPS *mumps, PetscInt nrow, PetscInt *ia, PetscInt *ja, PetscMUMPSInt **ia_mumps, PetscMUMPSInt **ja_mumps, PetscMUMPSInt *nnz_mumps)
616: {
617: PetscInt nnz = ia[nrow] - 1; /* mumps uses 1-based indices. Uses PetscInt instead of PetscCount since mumps only uses PetscMUMPSInt for rhs */
619: PetscFunctionBegin;
620: #if PetscDefined(USE_64BIT_INDICES)
621: {
622: if (nrow + 1 > mumps->cur_ilen) { /* realloc ia_alloc/ja_alloc to fit ia/ja */
623: PetscCall(PetscFree(mumps->ia_alloc));
624: PetscCall(PetscMalloc1(nrow + 1, &mumps->ia_alloc));
625: mumps->cur_ilen = nrow + 1;
626: }
627: if (nnz > mumps->cur_jlen) {
628: PetscCall(PetscFree(mumps->ja_alloc));
629: PetscCall(PetscMalloc1(nnz, &mumps->ja_alloc));
630: mumps->cur_jlen = nnz;
631: }
632: for (PetscInt i = 0; i < nrow + 1; i++) PetscCall(PetscMUMPSIntCast(ia[i], &mumps->ia_alloc[i]));
633: for (PetscInt i = 0; i < nnz; i++) PetscCall(PetscMUMPSIntCast(ja[i], &mumps->ja_alloc[i]));
634: *ia_mumps = mumps->ia_alloc;
635: *ja_mumps = mumps->ja_alloc;
636: }
637: #else
638: *ia_mumps = ia;
639: *ja_mumps = ja;
640: #endif
641: PetscCall(PetscMUMPSIntCast(nnz, nnz_mumps));
642: PetscFunctionReturn(PETSC_SUCCESS);
643: }
645: static PetscErrorCode MatMumpsEnsureSchurArray_Private(Mat F)
646: {
647: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
649: PetscFunctionBegin;
650: if (F->schur && !mumps->id.schur) {
651: const PetscScalar *array;
652: PetscCount size = mumps->id.size_schur;
654: PetscCall(MatDenseGetArrayRead(F->schur, &array));
655: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_FALSE, size * size, array, mumps->id.precision, &mumps->id.schur_len, &mumps->id.schur));
656: PetscCall(MatDenseRestoreArrayRead(F->schur, &array));
657: }
658: PetscFunctionReturn(PETSC_SUCCESS);
659: }
661: static PetscErrorCode MatMumpsResetSchur_Private(Mat F)
662: {
663: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
665: PetscFunctionBegin;
666: PetscCall(PetscFree(mumps->id.listvar_schur));
667: PetscCall(PetscFree(mumps->schur_sol));
668: if (mumps->redrhs != mumps->id.redrhs) PetscCall(PetscFree(mumps->id.redrhs));
669: else mumps->id.redrhs = NULL;
670: PetscCall(PetscFree(mumps->redrhs));
671: mumps->id.redrhs_len = 0;
672: mumps->id.schur_len = 0;
673: mumps->id.lredrhs = 0;
674: mumps->sizeredrhs = 0;
675: mumps->id.size_schur = 0;
676: mumps->id.schur_lld = 0;
677: if (mumps->id.internal_id) mumps->id.ICNTL(19) = 0; // sometimes, the inner id is yet built
678: if (F->schur) {
679: const PetscScalar *array;
681: PetscCall(MatDenseGetArrayRead(F->schur, &array));
682: if (array != mumps->id.schur) PetscCall(PetscFree(mumps->id.schur));
683: else mumps->id.schur = NULL;
684: PetscCall(MatDenseRestoreArrayRead(F->schur, &array));
685: }
686: PetscCall(MatDestroy(&F->schur));
687: if (mumps->id.icntl) mumps->id.ICNTL(26) = 0;
688: else mumps->ICNTL26 = 0;
689: PetscFunctionReturn(PETSC_SUCCESS);
690: }
692: /* solve with rhs in mumps->id.redrhs and return in the same location */
693: static PetscErrorCode MatMumpsSolveSchur_Private(Mat F)
694: {
695: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
696: Mat S, B, X; // solve S*X = B; all three matrices are dense
697: MatFactorSchurStatus schurstatus;
698: PetscInt sizesol;
699: const PetscScalar *xarray;
701: PetscFunctionBegin;
702: PetscCall(MatFactorFactorizeSchurComplement(F));
703: PetscCall(MatFactorGetSchurComplement(F, &S, &schurstatus));
704: PetscCall(MatMumpsCastMumpsScalarArray(mumps->sizeredrhs, mumps->id.precision, mumps->id.redrhs, mumps->redrhs));
706: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, mumps->id.size_schur, mumps->id.nrhs, mumps->redrhs, &B));
707: PetscCall(MatSetType(B, ((PetscObject)S)->type_name));
708: #if PetscDefined(HAVE_VIENNACL) || PetscDefined(HAVE_CUDA)
709: PetscCall(MatBindToCPU(B, S->boundtocpu));
710: #endif
711: switch (schurstatus) {
712: case MAT_FACTOR_SCHUR_FACTORED:
713: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, mumps->id.size_schur, mumps->id.nrhs, mumps->redrhs, &X));
714: PetscCall(MatSetType(X, ((PetscObject)S)->type_name));
715: #if PetscDefined(HAVE_VIENNACL) || PetscDefined(HAVE_CUDA)
716: PetscCall(MatBindToCPU(X, S->boundtocpu));
717: #endif
718: if (!mumps->id.ICNTL(9)) { /* transpose solve */
719: PetscCall(MatMatSolveTranspose(S, B, X));
720: } else {
721: PetscCall(MatMatSolve(S, B, X));
722: }
723: break;
724: case MAT_FACTOR_SCHUR_INVERTED:
725: sizesol = mumps->id.nrhs * mumps->id.size_schur;
726: if (!mumps->schur_sol || sizesol > mumps->schur_sizesol) {
727: PetscCall(PetscFree(mumps->schur_sol));
728: PetscCall(PetscMalloc1(sizesol, &mumps->schur_sol));
729: mumps->schur_sizesol = sizesol;
730: }
731: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, mumps->id.size_schur, mumps->id.nrhs, mumps->schur_sol, &X));
732: PetscCall(MatSetType(X, ((PetscObject)S)->type_name));
733: #if PetscDefined(HAVE_VIENNACL) || PetscDefined(HAVE_CUDA)
734: PetscCall(MatBindToCPU(X, S->boundtocpu));
735: #endif
736: PetscCall(MatProductCreateWithMat(S, B, NULL, X));
737: if (!mumps->id.ICNTL(9)) { /* transpose solve */
738: PetscCall(MatProductSetType(X, MATPRODUCT_AtB));
739: } else {
740: PetscCall(MatProductSetType(X, MATPRODUCT_AB));
741: }
742: PetscCall(MatProductSetFromOptions(X));
743: PetscCall(MatProductSymbolic(X));
744: PetscCall(MatProductNumeric(X));
746: PetscCall(MatCopy(X, B, SAME_NONZERO_PATTERN));
747: break;
748: default:
749: SETERRQ(PetscObjectComm((PetscObject)F), PETSC_ERR_SUP, "Unhandled MatFactorSchurStatus %d", F->schur_status);
750: }
751: // MUST get the array from X (not B), though they share the same host array. We can only guarantee X has the correct data on device.
752: PetscCall(MatDenseGetArrayRead(X, &xarray)); // xarray should be mumps->redrhs, but using MatDenseGetArrayRead is safer with GPUs.
753: PetscCall(MatMumpsCastPetscScalarArray(mumps->sizeredrhs, xarray, mumps->id.precision, mumps->id.redrhs));
754: PetscCall(MatDenseRestoreArrayRead(X, &xarray));
755: PetscCall(MatFactorRestoreSchurComplement(F, &S, schurstatus));
756: PetscCall(MatDestroy(&B));
757: PetscCall(MatDestroy(&X));
758: PetscFunctionReturn(PETSC_SUCCESS);
759: }
761: static PetscErrorCode MatMumpsHandleSchur_Private(Mat F, PetscBool expansion)
762: {
763: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
765: PetscFunctionBegin;
766: if (!mumps->id.ICNTL(19)) { /* do nothing when Schur complement has not been computed */
767: PetscFunctionReturn(PETSC_SUCCESS);
768: }
769: if (!expansion) { /* prepare for the condensation step */
770: PetscInt sizeredrhs = mumps->id.nrhs * mumps->id.size_schur;
771: /* allocate MUMPS internal array to store reduced right-hand sides */
772: if (!mumps->id.redrhs || sizeredrhs > mumps->sizeredrhs) {
773: mumps->id.lredrhs = mumps->id.size_schur;
774: mumps->sizeredrhs = mumps->id.nrhs * mumps->id.lredrhs;
775: if (mumps->id.redrhs_len) PetscCall(PetscFree(mumps->id.redrhs));
776: PetscCall(PetscFree(mumps->redrhs));
777: PetscCall(PetscMalloc1(mumps->sizeredrhs, &mumps->redrhs));
778: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_FALSE, mumps->sizeredrhs, mumps->redrhs, mumps->id.precision, &mumps->id.redrhs_len, &mumps->id.redrhs));
779: }
780: } else { /* prepare for the expansion step */
781: PetscCall(MatMumpsSolveSchur_Private(F)); /* solve Schur complement, put solution in id.redrhs (this has to be done by the MUMPS user, so basically us) */
782: mumps->id.ICNTL(26) = 2; /* expansion phase */
783: PetscMUMPS_c(mumps);
784: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in solve: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
785: /* restore defaults */
786: mumps->id.ICNTL(26) = -1;
787: /* free MUMPS internal array for redrhs if we have solved for multiple rhs in order to save memory space */
788: if (mumps->id.nrhs > 1) {
789: if (mumps->redrhs != mumps->id.redrhs) PetscCall(PetscFree(mumps->id.redrhs));
790: else mumps->id.redrhs = NULL;
791: PetscCall(PetscFree(mumps->redrhs));
792: mumps->id.redrhs_len = 0;
793: mumps->id.lredrhs = 0;
794: mumps->sizeredrhs = 0;
795: }
796: }
797: PetscFunctionReturn(PETSC_SUCCESS);
798: }
800: /*
801: MatConvertToTriples_A_B - convert PETSc matrix to triples: row[nz], col[nz], val[nz]
803: input:
804: A - matrix in aij,baij or sbaij format
805: shift - 0: C style output triple; 1: Fortran style output triple.
806: reuse - MAT_INITIAL_MATRIX: spaces are allocated and values are set for the triple
807: MAT_REUSE_MATRIX: only the values in v array are updated
808: output:
809: nnz - dim of r, c, and v (number of local nonzero entries of A)
810: r, c, v - row and col index, matrix values (matrix triples)
812: The returned values r, c, and sometimes v are obtained in a single PetscMalloc(). Then in MatDestroy_MUMPS() it is
813: freed with PetscFree(mumps->irn); This is not ideal code, the fact that v is ONLY sometimes part of mumps->irn means
814: that the PetscMalloc() cannot easily be replaced with a PetscMalloc3().
816: */
818: static PetscErrorCode MatConvertToTriples_seqaij_seqaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
819: {
820: const PetscScalar *av;
821: const PetscInt *ai, *aj, *ajj, M = A->rmap->n;
822: PetscCount nz, rnz, k;
823: PetscMUMPSInt *row, *col;
824: Mat_SeqAIJ *aa = (Mat_SeqAIJ *)A->data;
826: PetscFunctionBegin;
827: PetscCall(MatSeqAIJGetArrayRead(A, &av));
828: if (reuse == MAT_INITIAL_MATRIX) {
829: nz = aa->nz;
830: ai = aa->i;
831: aj = aa->j;
832: PetscCall(PetscMalloc2(nz, &row, nz, &col));
833: for (PetscCount i = k = 0; i < M; i++) {
834: rnz = ai[i + 1] - ai[i];
835: ajj = aj + ai[i];
836: for (PetscCount j = 0; j < rnz; j++) {
837: PetscCall(PetscMUMPSIntCast(i + shift, &row[k]));
838: PetscCall(PetscMUMPSIntCast(ajj[j] + shift, &col[k]));
839: k++;
840: }
841: }
842: mumps->val = (PetscScalar *)av;
843: mumps->irn = row;
844: mumps->jcn = col;
845: mumps->nnz = nz;
846: } else if (mumps->nest_vals) PetscCall(PetscArraycpy(mumps->val, av, aa->nz)); /* MatConvertToTriples_nest_xaij() allocates mumps->val outside of MatConvertToTriples_seqaij_seqaij(), so one needs to copy the memory */
847: else mumps->val = (PetscScalar *)av; /* in the default case, mumps->val is never allocated, one just needs to update the mumps->val pointer */
848: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
849: PetscFunctionReturn(PETSC_SUCCESS);
850: }
852: static PetscErrorCode MatConvertToTriples_seqsell_seqaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
853: {
854: PetscCount nz, i, j, k, r;
855: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
856: PetscMUMPSInt *row, *col;
858: PetscFunctionBegin;
859: nz = a->sliidx[a->totalslices];
860: if (reuse == MAT_INITIAL_MATRIX) {
861: PetscCall(PetscMalloc2(nz, &row, nz, &col));
862: for (i = k = 0; i < a->totalslices; i++) {
863: for (j = a->sliidx[i], r = 0; j < a->sliidx[i + 1]; j++, r = ((r + 1) & 0x07)) PetscCall(PetscMUMPSIntCast(8 * i + r + shift, &row[k++]));
864: }
865: for (i = 0; i < nz; i++) PetscCall(PetscMUMPSIntCast(a->colidx[i] + shift, &col[i]));
866: mumps->irn = row;
867: mumps->jcn = col;
868: mumps->nnz = nz;
869: mumps->val = a->val;
870: } else if (mumps->nest_vals) PetscCall(PetscArraycpy(mumps->val, a->val, nz)); /* MatConvertToTriples_nest_xaij() allocates mumps->val outside of MatConvertToTriples_seqsell_seqaij(), so one needs to copy the memory */
871: else mumps->val = a->val; /* in the default case, mumps->val is never allocated, one just needs to update the mumps->val pointer */
872: PetscFunctionReturn(PETSC_SUCCESS);
873: }
875: static PetscErrorCode MatConvertToTriples_seqbaij_seqaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
876: {
877: Mat_SeqBAIJ *aa = (Mat_SeqBAIJ *)A->data;
878: const PetscInt *ai, *aj, *ajj, bs2 = aa->bs2;
879: PetscCount M, nz = bs2 * aa->nz, idx = 0, rnz, i, j, k, m;
880: PetscInt bs;
881: PetscMUMPSInt *row, *col;
883: PetscFunctionBegin;
884: if (reuse == MAT_INITIAL_MATRIX) {
885: PetscCall(MatGetBlockSize(A, &bs));
886: M = A->rmap->N / bs;
887: ai = aa->i;
888: aj = aa->j;
889: PetscCall(PetscMalloc2(nz, &row, nz, &col));
890: for (i = 0; i < M; i++) {
891: ajj = aj + ai[i];
892: rnz = ai[i + 1] - ai[i];
893: for (k = 0; k < rnz; k++) {
894: for (j = 0; j < bs; j++) {
895: for (m = 0; m < bs; m++) {
896: PetscCall(PetscMUMPSIntCast(i * bs + m + shift, &row[idx]));
897: PetscCall(PetscMUMPSIntCast(bs * ajj[k] + j + shift, &col[idx]));
898: idx++;
899: }
900: }
901: }
902: }
903: mumps->irn = row;
904: mumps->jcn = col;
905: mumps->nnz = nz;
906: mumps->val = aa->a;
907: } else if (mumps->nest_vals) PetscCall(PetscArraycpy(mumps->val, aa->a, nz)); /* MatConvertToTriples_nest_xaij() allocates mumps->val outside of MatConvertToTriples_seqbaij_seqaij(), so one needs to copy the memory */
908: else mumps->val = aa->a; /* in the default case, mumps->val is never allocated, one just needs to update the mumps->val pointer */
909: PetscFunctionReturn(PETSC_SUCCESS);
910: }
912: static PetscErrorCode MatConvertToTriples_seqsbaij_seqsbaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
913: {
914: const PetscInt *ai, *aj, *ajj;
915: PetscInt bs;
916: PetscCount nz, rnz, i, j, k, m;
917: PetscMUMPSInt *row, *col;
918: PetscScalar *val;
919: Mat_SeqSBAIJ *aa = (Mat_SeqSBAIJ *)A->data;
920: const PetscInt bs2 = aa->bs2, mbs = aa->mbs;
921: PetscBool isset, hermitian;
923: PetscFunctionBegin;
924: if (PetscDefined(USE_COMPLEX)) {
925: PetscCall(MatIsHermitianKnown(A, &isset, &hermitian));
926: PetscCheck(!isset || !hermitian, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MUMPS does not support Hermitian symmetric matrices for Choleksy");
927: }
928: ai = aa->i;
929: aj = aa->j;
930: PetscCall(MatGetBlockSize(A, &bs));
931: if (reuse == MAT_INITIAL_MATRIX) {
932: const PetscCount alloc_size = aa->nz * bs2;
934: PetscCall(PetscMalloc2(alloc_size, &row, alloc_size, &col));
935: if (bs > 1) {
936: PetscCall(PetscMalloc1(alloc_size, &mumps->val_alloc));
937: mumps->val = mumps->val_alloc;
938: } else {
939: mumps->val = aa->a;
940: }
941: mumps->irn = row;
942: mumps->jcn = col;
943: } else {
944: row = mumps->irn;
945: col = mumps->jcn;
946: }
947: val = mumps->val;
949: nz = 0;
950: if (bs > 1) {
951: for (i = 0; i < mbs; i++) {
952: rnz = ai[i + 1] - ai[i];
953: ajj = aj + ai[i];
954: for (j = 0; j < rnz; j++) {
955: for (k = 0; k < bs; k++) {
956: for (m = 0; m < bs; m++) {
957: if (ajj[j] > i || k >= m) {
958: if (reuse == MAT_INITIAL_MATRIX) {
959: PetscCall(PetscMUMPSIntCast(i * bs + m + shift, &row[nz]));
960: PetscCall(PetscMUMPSIntCast(ajj[j] * bs + k + shift, &col[nz]));
961: }
962: val[nz++] = aa->a[(ai[i] + j) * bs2 + m + k * bs];
963: }
964: }
965: }
966: }
967: }
968: } else if (reuse == MAT_INITIAL_MATRIX) {
969: for (i = 0; i < mbs; i++) {
970: rnz = ai[i + 1] - ai[i];
971: ajj = aj + ai[i];
972: for (j = 0; j < rnz; j++) {
973: PetscCall(PetscMUMPSIntCast(i + shift, &row[nz]));
974: PetscCall(PetscMUMPSIntCast(ajj[j] + shift, &col[nz]));
975: nz++;
976: }
977: }
978: PetscCheck(nz == aa->nz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Different numbers of nonzeros %" PetscCount_FMT " != %" PetscInt_FMT, nz, aa->nz);
979: } else if (mumps->nest_vals)
980: PetscCall(PetscArraycpy(mumps->val, aa->a, aa->nz)); /* bs == 1 and MAT_REUSE_MATRIX, MatConvertToTriples_nest_xaij() allocates mumps->val outside of MatConvertToTriples_seqsbaij_seqsbaij(), so one needs to copy the memory */
981: else mumps->val = aa->a; /* in the default case, mumps->val is never allocated, one just needs to update the mumps->val pointer */
982: if (reuse == MAT_INITIAL_MATRIX) mumps->nnz = nz;
983: PetscFunctionReturn(PETSC_SUCCESS);
984: }
986: static PetscErrorCode MatConvertToTriples_seqaij_seqsbaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
987: {
988: const PetscInt *ai, *aj, *ajj, *adiag, M = A->rmap->n;
989: PetscCount nz, rnz, i, j;
990: const PetscScalar *av, *v1;
991: PetscScalar *val;
992: PetscMUMPSInt *row, *col;
993: Mat_SeqAIJ *aa = (Mat_SeqAIJ *)A->data;
994: PetscBool diagDense;
995: PetscBool hermitian, isset;
997: PetscFunctionBegin;
998: if (PetscDefined(USE_COMPLEX)) {
999: PetscCall(MatIsHermitianKnown(A, &isset, &hermitian));
1000: PetscCheck(!isset || !hermitian, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MUMPS does not support Hermitian symmetric matrices for Choleksy");
1001: }
1002: PetscCall(MatSeqAIJGetArrayRead(A, &av));
1003: ai = aa->i;
1004: aj = aa->j;
1005: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &adiag, &diagDense));
1006: if (reuse == MAT_INITIAL_MATRIX) {
1007: /* count nz in the upper triangular part of A */
1008: nz = 0;
1009: if (!diagDense) {
1010: for (i = 0; i < M; i++) {
1011: if (PetscUnlikely(adiag[i] >= ai[i + 1])) {
1012: for (j = ai[i]; j < ai[i + 1]; j++) {
1013: if (aj[j] < i) continue;
1014: nz++;
1015: }
1016: } else {
1017: nz += ai[i + 1] - adiag[i];
1018: }
1019: }
1020: } else {
1021: for (i = 0; i < M; i++) nz += ai[i + 1] - adiag[i];
1022: }
1023: PetscCall(PetscMalloc2(nz, &row, nz, &col));
1024: PetscCall(PetscMalloc1(nz, &val));
1025: mumps->nnz = nz;
1026: mumps->irn = row;
1027: mumps->jcn = col;
1028: mumps->val = mumps->val_alloc = val;
1030: nz = 0;
1031: if (!diagDense) {
1032: for (i = 0; i < M; i++) {
1033: if (PetscUnlikely(adiag[i] >= ai[i + 1])) {
1034: for (j = ai[i]; j < ai[i + 1]; j++) {
1035: if (aj[j] < i) continue;
1036: PetscCall(PetscMUMPSIntCast(i + shift, &row[nz]));
1037: PetscCall(PetscMUMPSIntCast(aj[j] + shift, &col[nz]));
1038: val[nz] = av[j];
1039: nz++;
1040: }
1041: } else {
1042: rnz = ai[i + 1] - adiag[i];
1043: ajj = aj + adiag[i];
1044: v1 = av + adiag[i];
1045: for (j = 0; j < rnz; j++) {
1046: PetscCall(PetscMUMPSIntCast(i + shift, &row[nz]));
1047: PetscCall(PetscMUMPSIntCast(ajj[j] + shift, &col[nz]));
1048: val[nz++] = v1[j];
1049: }
1050: }
1051: }
1052: } else {
1053: for (i = 0; i < M; i++) {
1054: rnz = ai[i + 1] - adiag[i];
1055: ajj = aj + adiag[i];
1056: v1 = av + adiag[i];
1057: for (j = 0; j < rnz; j++) {
1058: PetscCall(PetscMUMPSIntCast(i + shift, &row[nz]));
1059: PetscCall(PetscMUMPSIntCast(ajj[j] + shift, &col[nz]));
1060: val[nz++] = v1[j];
1061: }
1062: }
1063: }
1064: } else {
1065: nz = 0;
1066: val = mumps->val;
1067: if (!diagDense) {
1068: for (i = 0; i < M; i++) {
1069: if (PetscUnlikely(adiag[i] >= ai[i + 1])) {
1070: for (j = ai[i]; j < ai[i + 1]; j++) {
1071: if (aj[j] < i) continue;
1072: val[nz++] = av[j];
1073: }
1074: } else {
1075: rnz = ai[i + 1] - adiag[i];
1076: v1 = av + adiag[i];
1077: for (j = 0; j < rnz; j++) val[nz++] = v1[j];
1078: }
1079: }
1080: } else {
1081: for (i = 0; i < M; i++) {
1082: rnz = ai[i + 1] - adiag[i];
1083: v1 = av + adiag[i];
1084: for (j = 0; j < rnz; j++) val[nz++] = v1[j];
1085: }
1086: }
1087: }
1088: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
1089: PetscFunctionReturn(PETSC_SUCCESS);
1090: }
1092: static PetscErrorCode MatConvertToTriples_mpisbaij_mpisbaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1093: {
1094: const PetscInt *ai, *aj, *bi, *bj, *garray, *ajj, *bjj;
1095: PetscInt bs;
1096: PetscCount rstart, nz, i, j, k, m, jj, irow, countA, countB;
1097: PetscMUMPSInt *row, *col;
1098: const PetscScalar *av, *bv, *v1, *v2;
1099: PetscScalar *val;
1100: Mat_MPISBAIJ *mat = (Mat_MPISBAIJ *)A->data;
1101: Mat_SeqSBAIJ *aa = (Mat_SeqSBAIJ *)mat->A->data;
1102: Mat_SeqBAIJ *bb = (Mat_SeqBAIJ *)mat->B->data;
1103: const PetscInt bs2 = aa->bs2, mbs = aa->mbs;
1104: PetscBool hermitian, isset;
1106: PetscFunctionBegin;
1107: if (PetscDefined(USE_COMPLEX)) {
1108: PetscCall(MatIsHermitianKnown(A, &isset, &hermitian));
1109: PetscCheck(!isset || !hermitian, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MUMPS does not support Hermitian symmetric matrices for Choleksy");
1110: }
1111: PetscCall(MatGetBlockSize(A, &bs));
1112: rstart = A->rmap->rstart;
1113: ai = aa->i;
1114: aj = aa->j;
1115: bi = bb->i;
1116: bj = bb->j;
1117: av = aa->a;
1118: bv = bb->a;
1120: garray = mat->garray;
1122: if (reuse == MAT_INITIAL_MATRIX) {
1123: nz = (aa->nz + bb->nz) * bs2; /* just a conservative estimate */
1124: PetscCall(PetscMalloc2(nz, &row, nz, &col));
1125: PetscCall(PetscMalloc1(nz, &val));
1126: /* can not decide the exact mumps->nnz now because of the SBAIJ */
1127: mumps->irn = row;
1128: mumps->jcn = col;
1129: mumps->val = mumps->val_alloc = val;
1130: } else {
1131: val = mumps->val;
1132: }
1134: jj = 0;
1135: irow = rstart;
1136: for (i = 0; i < mbs; i++) {
1137: ajj = aj + ai[i]; /* ptr to the beginning of this row */
1138: countA = ai[i + 1] - ai[i];
1139: countB = bi[i + 1] - bi[i];
1140: bjj = bj + bi[i];
1141: v1 = av + ai[i] * bs2;
1142: v2 = bv + bi[i] * bs2;
1144: if (bs > 1) {
1145: /* A-part */
1146: for (j = 0; j < countA; j++) {
1147: for (k = 0; k < bs; k++) {
1148: for (m = 0; m < bs; m++) {
1149: if (rstart + ajj[j] * bs > irow || k >= m) {
1150: if (reuse == MAT_INITIAL_MATRIX) {
1151: PetscCall(PetscMUMPSIntCast(irow + m + shift, &row[jj]));
1152: PetscCall(PetscMUMPSIntCast(rstart + ajj[j] * bs + k + shift, &col[jj]));
1153: }
1154: val[jj++] = v1[j * bs2 + m + k * bs];
1155: }
1156: }
1157: }
1158: }
1160: /* B-part */
1161: for (j = 0; j < countB; j++) {
1162: for (k = 0; k < bs; k++) {
1163: for (m = 0; m < bs; m++) {
1164: if (reuse == MAT_INITIAL_MATRIX) {
1165: PetscCall(PetscMUMPSIntCast(irow + m + shift, &row[jj]));
1166: PetscCall(PetscMUMPSIntCast(garray[bjj[j]] * bs + k + shift, &col[jj]));
1167: }
1168: val[jj++] = v2[j * bs2 + m + k * bs];
1169: }
1170: }
1171: }
1172: } else {
1173: /* A-part */
1174: for (j = 0; j < countA; j++) {
1175: if (reuse == MAT_INITIAL_MATRIX) {
1176: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1177: PetscCall(PetscMUMPSIntCast(rstart + ajj[j] + shift, &col[jj]));
1178: }
1179: val[jj++] = v1[j];
1180: }
1182: /* B-part */
1183: for (j = 0; j < countB; j++) {
1184: if (reuse == MAT_INITIAL_MATRIX) {
1185: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1186: PetscCall(PetscMUMPSIntCast(garray[bjj[j]] + shift, &col[jj]));
1187: }
1188: val[jj++] = v2[j];
1189: }
1190: }
1191: irow += bs;
1192: }
1193: if (reuse == MAT_INITIAL_MATRIX) mumps->nnz = jj;
1194: PetscFunctionReturn(PETSC_SUCCESS);
1195: }
1197: static PetscErrorCode MatConvertToTriples_mpiaij_mpiaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1198: {
1199: const PetscInt *ai, *aj, *bi, *bj, *garray, m = A->rmap->n, *ajj, *bjj;
1200: PetscCount rstart, cstart, nz, i, j, jj, irow, countA, countB;
1201: PetscMUMPSInt *row, *col;
1202: const PetscScalar *av, *bv, *v1, *v2;
1203: PetscScalar *val;
1204: Mat Ad, Ao;
1205: Mat_SeqAIJ *aa;
1206: Mat_SeqAIJ *bb;
1208: PetscFunctionBegin;
1209: PetscCall(MatMPIAIJGetSeqAIJ(A, &Ad, &Ao, &garray));
1210: PetscCall(MatSeqAIJGetArrayRead(Ad, &av));
1211: PetscCall(MatSeqAIJGetArrayRead(Ao, &bv));
1213: aa = (Mat_SeqAIJ *)Ad->data;
1214: bb = (Mat_SeqAIJ *)Ao->data;
1215: ai = aa->i;
1216: aj = aa->j;
1217: bi = bb->i;
1218: bj = bb->j;
1220: rstart = A->rmap->rstart;
1221: cstart = A->cmap->rstart;
1223: if (reuse == MAT_INITIAL_MATRIX) {
1224: nz = (PetscCount)aa->nz + bb->nz; /* make sure the sum won't overflow PetscInt */
1225: PetscCall(PetscMalloc2(nz, &row, nz, &col));
1226: PetscCall(PetscMalloc1(nz, &val));
1227: mumps->nnz = nz;
1228: mumps->irn = row;
1229: mumps->jcn = col;
1230: mumps->val = mumps->val_alloc = val;
1231: } else {
1232: val = mumps->val;
1233: }
1235: jj = 0;
1236: irow = rstart;
1237: for (i = 0; i < m; i++) {
1238: ajj = aj + ai[i]; /* ptr to the beginning of this row */
1239: countA = ai[i + 1] - ai[i];
1240: countB = bi[i + 1] - bi[i];
1241: bjj = bj + bi[i];
1242: v1 = av + ai[i];
1243: v2 = bv + bi[i];
1245: /* A-part */
1246: for (j = 0; j < countA; j++) {
1247: if (reuse == MAT_INITIAL_MATRIX) {
1248: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1249: PetscCall(PetscMUMPSIntCast(cstart + ajj[j] + shift, &col[jj]));
1250: }
1251: val[jj++] = v1[j];
1252: }
1254: /* B-part */
1255: for (j = 0; j < countB; j++) {
1256: if (reuse == MAT_INITIAL_MATRIX) {
1257: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1258: PetscCall(PetscMUMPSIntCast(garray[bjj[j]] + shift, &col[jj]));
1259: }
1260: val[jj++] = v2[j];
1261: }
1262: irow++;
1263: }
1264: PetscCall(MatSeqAIJRestoreArrayRead(Ad, &av));
1265: PetscCall(MatSeqAIJRestoreArrayRead(Ao, &bv));
1266: PetscFunctionReturn(PETSC_SUCCESS);
1267: }
1269: static PetscErrorCode MatConvertToTriples_mpibaij_mpiaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1270: {
1271: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ *)A->data;
1272: Mat_SeqBAIJ *aa = (Mat_SeqBAIJ *)mat->A->data;
1273: Mat_SeqBAIJ *bb = (Mat_SeqBAIJ *)mat->B->data;
1274: const PetscInt *ai = aa->i, *bi = bb->i, *aj = aa->j, *bj = bb->j, *ajj, *bjj;
1275: const PetscInt *garray = mat->garray, mbs = mat->mbs, rstart = A->rmap->rstart, cstart = A->cmap->rstart;
1276: const PetscInt bs2 = mat->bs2;
1277: PetscInt bs;
1278: PetscCount nz, i, j, k, n, jj, irow, countA, countB, idx;
1279: PetscMUMPSInt *row, *col;
1280: const PetscScalar *av = aa->a, *bv = bb->a, *v1, *v2;
1281: PetscScalar *val;
1283: PetscFunctionBegin;
1284: PetscCall(MatGetBlockSize(A, &bs));
1285: if (reuse == MAT_INITIAL_MATRIX) {
1286: nz = bs2 * (aa->nz + bb->nz);
1287: PetscCall(PetscMalloc2(nz, &row, nz, &col));
1288: PetscCall(PetscMalloc1(nz, &val));
1289: mumps->nnz = nz;
1290: mumps->irn = row;
1291: mumps->jcn = col;
1292: mumps->val = mumps->val_alloc = val;
1293: } else {
1294: val = mumps->val;
1295: }
1297: jj = 0;
1298: irow = rstart;
1299: for (i = 0; i < mbs; i++) {
1300: countA = ai[i + 1] - ai[i];
1301: countB = bi[i + 1] - bi[i];
1302: ajj = aj + ai[i];
1303: bjj = bj + bi[i];
1304: v1 = av + bs2 * ai[i];
1305: v2 = bv + bs2 * bi[i];
1307: idx = 0;
1308: /* A-part */
1309: for (k = 0; k < countA; k++) {
1310: for (j = 0; j < bs; j++) {
1311: for (n = 0; n < bs; n++) {
1312: if (reuse == MAT_INITIAL_MATRIX) {
1313: PetscCall(PetscMUMPSIntCast(irow + n + shift, &row[jj]));
1314: PetscCall(PetscMUMPSIntCast(cstart + bs * ajj[k] + j + shift, &col[jj]));
1315: }
1316: val[jj++] = v1[idx++];
1317: }
1318: }
1319: }
1321: idx = 0;
1322: /* B-part */
1323: for (k = 0; k < countB; k++) {
1324: for (j = 0; j < bs; j++) {
1325: for (n = 0; n < bs; n++) {
1326: if (reuse == MAT_INITIAL_MATRIX) {
1327: PetscCall(PetscMUMPSIntCast(irow + n + shift, &row[jj]));
1328: PetscCall(PetscMUMPSIntCast(bs * garray[bjj[k]] + j + shift, &col[jj]));
1329: }
1330: val[jj++] = v2[idx++];
1331: }
1332: }
1333: }
1334: irow += bs;
1335: }
1336: PetscFunctionReturn(PETSC_SUCCESS);
1337: }
1339: static PetscErrorCode MatConvertToTriples_mpiaij_mpisbaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1340: {
1341: const PetscInt *ai, *aj, *adiag, *bi, *bj, *garray, m = A->rmap->n, *ajj, *bjj;
1342: PetscCount rstart, nz, nza, nzb, i, j, jj, irow, countA, countB;
1343: PetscMUMPSInt *row, *col;
1344: const PetscScalar *av, *bv, *v1, *v2;
1345: PetscScalar *val;
1346: Mat Ad, Ao;
1347: Mat_SeqAIJ *aa;
1348: Mat_SeqAIJ *bb;
1349: PetscBool hermitian, isset;
1351: PetscFunctionBegin;
1352: if (PetscDefined(USE_COMPLEX)) {
1353: PetscCall(MatIsHermitianKnown(A, &isset, &hermitian));
1354: PetscCheck(!isset || !hermitian, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MUMPS does not support Hermitian symmetric matrices for Choleksy");
1355: }
1356: PetscCall(MatMPIAIJGetSeqAIJ(A, &Ad, &Ao, &garray));
1357: PetscCall(MatSeqAIJGetArrayRead(Ad, &av));
1358: PetscCall(MatSeqAIJGetArrayRead(Ao, &bv));
1360: aa = (Mat_SeqAIJ *)Ad->data;
1361: bb = (Mat_SeqAIJ *)Ao->data;
1362: ai = aa->i;
1363: aj = aa->j;
1364: bi = bb->i;
1365: bj = bb->j;
1366: PetscCall(MatGetDiagonalMarkers_SeqAIJ(Ad, &adiag, NULL));
1367: rstart = A->rmap->rstart;
1369: if (reuse == MAT_INITIAL_MATRIX) {
1370: nza = 0; /* num of upper triangular entries in mat->A, including diagonals */
1371: nzb = 0; /* num of upper triangular entries in mat->B */
1372: for (i = 0; i < m; i++) {
1373: nza += (ai[i + 1] - adiag[i]);
1374: countB = bi[i + 1] - bi[i];
1375: bjj = bj + bi[i];
1376: for (j = 0; j < countB; j++) {
1377: if (garray[bjj[j]] > rstart) nzb++;
1378: }
1379: }
1381: nz = nza + nzb; /* total nz of upper triangular part of mat */
1382: PetscCall(PetscMalloc2(nz, &row, nz, &col));
1383: PetscCall(PetscMalloc1(nz, &val));
1384: mumps->nnz = nz;
1385: mumps->irn = row;
1386: mumps->jcn = col;
1387: mumps->val = mumps->val_alloc = val;
1388: } else {
1389: val = mumps->val;
1390: }
1392: jj = 0;
1393: irow = rstart;
1394: for (i = 0; i < m; i++) {
1395: ajj = aj + adiag[i]; /* ptr to the beginning of the diagonal of this row */
1396: v1 = av + adiag[i];
1397: countA = ai[i + 1] - adiag[i];
1398: countB = bi[i + 1] - bi[i];
1399: bjj = bj + bi[i];
1400: v2 = bv + bi[i];
1402: /* A-part */
1403: for (j = 0; j < countA; j++) {
1404: if (reuse == MAT_INITIAL_MATRIX) {
1405: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1406: PetscCall(PetscMUMPSIntCast(rstart + ajj[j] + shift, &col[jj]));
1407: }
1408: val[jj++] = v1[j];
1409: }
1411: /* B-part */
1412: for (j = 0; j < countB; j++) {
1413: if (garray[bjj[j]] > rstart) {
1414: if (reuse == MAT_INITIAL_MATRIX) {
1415: PetscCall(PetscMUMPSIntCast(irow + shift, &row[jj]));
1416: PetscCall(PetscMUMPSIntCast(garray[bjj[j]] + shift, &col[jj]));
1417: }
1418: val[jj++] = v2[j];
1419: }
1420: }
1421: irow++;
1422: }
1423: PetscCall(MatSeqAIJRestoreArrayRead(Ad, &av));
1424: PetscCall(MatSeqAIJRestoreArrayRead(Ao, &bv));
1425: PetscFunctionReturn(PETSC_SUCCESS);
1426: }
1428: static PetscErrorCode MatConvertToTriples_diagonal_xaij(Mat A, PETSC_UNUSED PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1429: {
1430: const PetscScalar *av;
1431: const PetscInt M = A->rmap->n;
1432: PetscCount i;
1433: PetscMUMPSInt *row, *col;
1434: Vec v;
1436: PetscFunctionBegin;
1437: PetscCall(MatDiagonalGetDiagonal(A, &v));
1438: PetscCall(VecGetArrayRead(v, &av));
1439: if (reuse == MAT_INITIAL_MATRIX) {
1440: PetscCall(PetscMalloc2(M, &row, M, &col));
1441: for (i = 0; i < M; i++) {
1442: PetscCall(PetscMUMPSIntCast(i + A->rmap->rstart, &row[i]));
1443: col[i] = row[i];
1444: }
1445: mumps->val = (PetscScalar *)av;
1446: mumps->irn = row;
1447: mumps->jcn = col;
1448: mumps->nnz = M;
1449: } else if (mumps->nest_vals) PetscCall(PetscArraycpy(mumps->val, av, M)); /* MatConvertToTriples_nest_xaij() allocates mumps->val outside of MatConvertToTriples_diagonal_xaij(), so one needs to copy the memory */
1450: else mumps->val = (PetscScalar *)av; /* in the default case, mumps->val is never allocated, one just needs to update the mumps->val pointer */
1451: PetscCall(VecRestoreArrayRead(v, &av));
1452: PetscFunctionReturn(PETSC_SUCCESS);
1453: }
1455: static PetscErrorCode MatConvertToTriples_dense_xaij(Mat A, PETSC_UNUSED PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1456: {
1457: PetscScalar *v;
1458: const PetscInt m = A->rmap->n, N = A->cmap->N;
1459: PetscInt lda;
1460: PetscCount i, j;
1461: PetscMUMPSInt *row, *col;
1463: PetscFunctionBegin;
1464: PetscCall(MatDenseGetArray(A, &v));
1465: PetscCall(MatDenseGetLDA(A, &lda));
1466: if (reuse == MAT_INITIAL_MATRIX) {
1467: PetscCall(PetscMalloc2(m * N, &row, m * N, &col));
1468: for (i = 0; i < m; i++) {
1469: col[i] = 0;
1470: PetscCall(PetscMUMPSIntCast(i + A->rmap->rstart, &row[i]));
1471: }
1472: for (j = 1; j < N; j++) {
1473: for (i = 0; i < m; i++) PetscCall(PetscMUMPSIntCast(j, col + i + m * j));
1474: PetscCall(PetscArraycpy(row + m * j, row + m * (j - 1), m));
1475: }
1476: if (lda == m) mumps->val = v;
1477: else {
1478: PetscCall(PetscMalloc1(m * N, &mumps->val));
1479: mumps->val_alloc = mumps->val;
1480: for (j = 0; j < N; j++) PetscCall(PetscArraycpy(mumps->val + m * j, v + lda * j, m));
1481: }
1482: mumps->irn = row;
1483: mumps->jcn = col;
1484: mumps->nnz = m * N;
1485: } else {
1486: if (lda == m && !mumps->nest_vals) mumps->val = v;
1487: else {
1488: for (j = 0; j < N; j++) PetscCall(PetscArraycpy(mumps->val + m * j, v + lda * j, m));
1489: }
1490: }
1491: PetscCall(MatDenseRestoreArray(A, &v));
1492: PetscFunctionReturn(PETSC_SUCCESS);
1493: }
1495: // If the input Mat (sub) is either MATTRANSPOSEVIRTUAL or MATHERMITIANTRANSPOSEVIRTUAL, this function gets the parent Mat until it is not a
1496: // MATTRANSPOSEVIRTUAL or MATHERMITIANTRANSPOSEVIRTUAL itself and returns the appropriate shift, scaling, and whether the parent Mat should be conjugated
1497: // and its rows and columns permuted
1498: // TODO FIXME: this should not be in this file and should instead be refactored where the same logic applies, e.g., MatAXPY_Dense_Nest()
1499: static PetscErrorCode MatGetTranspose_TransposeVirtual(Mat *sub, PetscBool *conjugate, PetscScalar *vshift, PetscScalar *vscale, PetscBool *swap)
1500: {
1501: Mat A;
1502: PetscScalar s[2];
1503: PetscBool isTrans, isHTrans, compare;
1505: PetscFunctionBegin;
1506: do {
1507: PetscCall(PetscObjectTypeCompare((PetscObject)*sub, MATTRANSPOSEVIRTUAL, &isTrans));
1508: if (isTrans) {
1509: PetscCall(MatTransposeGetMat(*sub, &A));
1510: isHTrans = PETSC_FALSE;
1511: } else {
1512: PetscCall(PetscObjectTypeCompare((PetscObject)*sub, MATHERMITIANTRANSPOSEVIRTUAL, &isHTrans));
1513: if (isHTrans) PetscCall(MatHermitianTransposeGetMat(*sub, &A));
1514: }
1515: compare = (PetscBool)(isTrans || isHTrans);
1516: if (compare) {
1517: if (vshift && vscale) {
1518: PetscCall(MatShellGetScalingShifts(*sub, s, s + 1, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
1519: if (!*conjugate) {
1520: *vshift += s[0] * *vscale;
1521: *vscale *= s[1];
1522: } else {
1523: *vshift += PetscConj(s[0]) * *vscale;
1524: *vscale *= PetscConj(s[1]);
1525: }
1526: }
1527: if (swap) *swap = (PetscBool)!*swap;
1528: if (isHTrans && conjugate) *conjugate = (PetscBool)!*conjugate;
1529: *sub = A;
1530: }
1531: } while (compare);
1532: PetscFunctionReturn(PETSC_SUCCESS);
1533: }
1535: static PetscErrorCode MatConvertToTriples_nest_xaij(Mat A, PetscInt shift, MatReuse reuse, Mat_MUMPS *mumps)
1536: {
1537: Mat **mats;
1538: PetscInt nr, nc;
1539: PetscBool chol = mumps->sym ? PETSC_TRUE : PETSC_FALSE;
1541: PetscFunctionBegin;
1542: PetscCall(MatNestGetSubMats(A, &nr, &nc, &mats));
1543: if (reuse == MAT_INITIAL_MATRIX) {
1544: PetscMUMPSInt *irns, *jcns;
1545: PetscScalar *vals;
1546: PetscCount totnnz, cumnnz, maxnnz;
1547: PetscInt *pjcns_w, Mbs = 0;
1548: IS *rows, *cols;
1549: PetscInt **rows_idx, **cols_idx;
1551: cumnnz = 0;
1552: maxnnz = 0;
1553: PetscCall(PetscMalloc2(nr * nc + 1, &mumps->nest_vals_start, nr * nc, &mumps->nest_convert_to_triples));
1554: for (PetscInt r = 0; r < nr; r++) {
1555: for (PetscInt c = 0; c < nc; c++) {
1556: Mat sub = mats[r][c];
1558: mumps->nest_convert_to_triples[r * nc + c] = NULL;
1559: if (chol && c < r) continue; /* skip lower-triangular block for Cholesky */
1560: if (sub) {
1561: PetscErrorCode (*convert_to_triples)(Mat, PetscInt, MatReuse, Mat_MUMPS *) = NULL;
1562: PetscBool isSeqAIJ, isMPIAIJ, isSeqBAIJ, isMPIBAIJ, isSeqSBAIJ, isMPISBAIJ, isDiag, isDense;
1563: MatInfo info;
1565: PetscCall(MatGetTranspose_TransposeVirtual(&sub, NULL, NULL, NULL, NULL));
1566: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQAIJ, &isSeqAIJ));
1567: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPIAIJ, &isMPIAIJ));
1568: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQBAIJ, &isSeqBAIJ));
1569: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPIBAIJ, &isMPIBAIJ));
1570: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQSBAIJ, &isSeqSBAIJ));
1571: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPISBAIJ, &isMPISBAIJ));
1572: PetscCall(PetscObjectTypeCompare((PetscObject)sub, MATDIAGONAL, &isDiag));
1573: PetscCall(PetscObjectTypeCompareAny((PetscObject)sub, &isDense, MATSEQDENSE, MATMPIDENSE, NULL));
1575: if (chol) {
1576: if (r == c) {
1577: if (isSeqAIJ) convert_to_triples = MatConvertToTriples_seqaij_seqsbaij;
1578: else if (isMPIAIJ) convert_to_triples = MatConvertToTriples_mpiaij_mpisbaij;
1579: else if (isSeqSBAIJ) convert_to_triples = MatConvertToTriples_seqsbaij_seqsbaij;
1580: else if (isMPISBAIJ) convert_to_triples = MatConvertToTriples_mpisbaij_mpisbaij;
1581: else if (isDiag) convert_to_triples = MatConvertToTriples_diagonal_xaij;
1582: else if (isDense) convert_to_triples = MatConvertToTriples_dense_xaij;
1583: } else {
1584: if (isSeqAIJ) convert_to_triples = MatConvertToTriples_seqaij_seqaij;
1585: else if (isMPIAIJ) convert_to_triples = MatConvertToTriples_mpiaij_mpiaij;
1586: else if (isSeqBAIJ) convert_to_triples = MatConvertToTriples_seqbaij_seqaij;
1587: else if (isMPIBAIJ) convert_to_triples = MatConvertToTriples_mpibaij_mpiaij;
1588: else if (isDiag) convert_to_triples = MatConvertToTriples_diagonal_xaij;
1589: else if (isDense) convert_to_triples = MatConvertToTriples_dense_xaij;
1590: }
1591: } else {
1592: if (isSeqAIJ) convert_to_triples = MatConvertToTriples_seqaij_seqaij;
1593: else if (isMPIAIJ) convert_to_triples = MatConvertToTriples_mpiaij_mpiaij;
1594: else if (isSeqBAIJ) convert_to_triples = MatConvertToTriples_seqbaij_seqaij;
1595: else if (isMPIBAIJ) convert_to_triples = MatConvertToTriples_mpibaij_mpiaij;
1596: else if (isDiag) convert_to_triples = MatConvertToTriples_diagonal_xaij;
1597: else if (isDense) convert_to_triples = MatConvertToTriples_dense_xaij;
1598: }
1599: PetscCheck(convert_to_triples, PetscObjectComm((PetscObject)sub), PETSC_ERR_SUP, "Not for block of type %s", ((PetscObject)sub)->type_name);
1600: mumps->nest_convert_to_triples[r * nc + c] = convert_to_triples;
1601: PetscCall(MatGetInfo(sub, MAT_LOCAL, &info));
1602: cumnnz += (PetscCount)info.nz_used; /* can be overestimated for Cholesky */
1603: maxnnz = PetscMax(maxnnz, info.nz_used);
1604: }
1605: }
1606: }
1608: /* Allocate total COO */
1609: totnnz = cumnnz;
1610: PetscCall(PetscMalloc2(totnnz, &irns, totnnz, &jcns));
1611: PetscCall(PetscMalloc1(totnnz, &vals));
1613: /* Handle rows and column maps
1614: We directly map rows and use an SF for the columns */
1615: PetscCall(PetscMalloc4(nr, &rows, nc, &cols, nr, &rows_idx, nc, &cols_idx));
1616: PetscCall(MatNestGetISs(A, rows, cols));
1617: for (PetscInt r = 0; r < nr; r++) PetscCall(ISGetIndices(rows[r], (const PetscInt **)&rows_idx[r]));
1618: for (PetscInt c = 0; c < nc; c++) PetscCall(ISGetIndices(cols[c], (const PetscInt **)&cols_idx[c]));
1619: if (PetscDefined(USE_64BIT_INDICES)) PetscCall(PetscMalloc1(maxnnz, &pjcns_w));
1620: else (void)maxnnz;
1622: cumnnz = 0;
1623: for (PetscInt r = 0; r < nr; r++) {
1624: for (PetscInt c = 0; c < nc; c++) {
1625: Mat sub = mats[r][c];
1626: const PetscInt *ridx = rows_idx[r];
1627: const PetscInt *cidx = cols_idx[c];
1628: PetscScalar vscale = 1.0, vshift = 0.0;
1629: PetscInt rst, size, bs;
1630: PetscSF csf;
1631: PetscBool conjugate = PETSC_FALSE, swap = PETSC_FALSE;
1632: PetscLayout cmap;
1633: PetscInt innz;
1635: mumps->nest_vals_start[r * nc + c] = cumnnz;
1636: if (c == r) {
1637: PetscCall(ISGetSize(rows[r], &size));
1638: if (!mumps->nest_convert_to_triples[r * nc + c]) {
1639: for (PetscInt c = 0; c < nc && !sub; ++c) sub = mats[r][c]; // diagonal Mat is NULL, so start over from the beginning of the current row
1640: }
1641: PetscCall(MatGetBlockSize(sub, &bs));
1642: Mbs += size / bs;
1643: }
1644: if (!mumps->nest_convert_to_triples[r * nc + c]) continue;
1646: /* Extract inner blocks if needed */
1647: PetscCall(MatGetTranspose_TransposeVirtual(&sub, &conjugate, &vshift, &vscale, &swap));
1648: PetscCheck(vshift == 0.0, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Nonzero shift in parent MatShell");
1650: /* Get column layout to map off-process columns */
1651: PetscCall(MatGetLayouts(sub, NULL, &cmap));
1653: /* Get row start to map on-process rows */
1654: PetscCall(MatGetOwnershipRange(sub, &rst, NULL));
1656: /* Directly use the mumps datastructure and use C ordering for now */
1657: PetscCall((*mumps->nest_convert_to_triples[r * nc + c])(sub, 0, MAT_INITIAL_MATRIX, mumps));
1659: /* Swap the role of rows and columns indices for transposed blocks
1660: since we need values with global final ordering */
1661: if (swap) {
1662: cidx = rows_idx[r];
1663: ridx = cols_idx[c];
1664: }
1666: /* Communicate column indices
1667: This could have been done with a single SF but it would have complicated the code a lot.
1668: But since we do it only once, we pay the price of setting up an SF for each block */
1669: if (PetscDefined(USE_64BIT_INDICES)) {
1670: for (PetscInt k = 0; k < mumps->nnz; k++) pjcns_w[k] = mumps->jcn[k];
1671: } else pjcns_w = (PetscInt *)mumps->jcn; /* This cast is needed only to silence warnings for 64bit integers builds */
1672: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &csf));
1673: PetscCall(PetscIntCast(mumps->nnz, &innz));
1674: PetscCall(PetscSFSetGraphLayout(csf, cmap, innz, NULL, PETSC_OWN_POINTER, pjcns_w));
1675: PetscCall(PetscSFBcastBegin(csf, MPIU_INT, cidx, pjcns_w, MPI_REPLACE));
1676: PetscCall(PetscSFBcastEnd(csf, MPIU_INT, cidx, pjcns_w, MPI_REPLACE));
1677: PetscCall(PetscSFDestroy(&csf));
1679: /* Import indices: use direct map for rows and mapped indices for columns */
1680: if (swap) {
1681: for (PetscInt k = 0; k < mumps->nnz; k++) {
1682: PetscCall(PetscMUMPSIntCast(ridx[mumps->irn[k] - rst] + shift, &jcns[cumnnz + k]));
1683: PetscCall(PetscMUMPSIntCast(pjcns_w[k] + shift, &irns[cumnnz + k]));
1684: }
1685: } else {
1686: for (PetscInt k = 0; k < mumps->nnz; k++) {
1687: PetscCall(PetscMUMPSIntCast(ridx[mumps->irn[k] - rst] + shift, &irns[cumnnz + k]));
1688: PetscCall(PetscMUMPSIntCast(pjcns_w[k] + shift, &jcns[cumnnz + k]));
1689: }
1690: }
1692: /* Import values to full COO */
1693: if (conjugate) { /* conjugate the entries */
1694: PetscScalar *v = vals + cumnnz;
1695: for (PetscInt k = 0; k < mumps->nnz; k++) v[k] = vscale * PetscConj(mumps->val[k]);
1696: } else if (vscale != 1.0) {
1697: PetscScalar *v = vals + cumnnz;
1698: for (PetscInt k = 0; k < mumps->nnz; k++) v[k] = vscale * mumps->val[k];
1699: } else PetscCall(PetscArraycpy(vals + cumnnz, mumps->val, mumps->nnz));
1701: /* Shift new starting point and sanity check */
1702: cumnnz += mumps->nnz;
1703: PetscCheck(cumnnz <= totnnz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected number of nonzeros %" PetscCount_FMT " != %" PetscCount_FMT, cumnnz, totnnz);
1705: /* Free scratch memory */
1706: PetscCall(PetscFree2(mumps->irn, mumps->jcn));
1707: PetscCall(PetscFree(mumps->val_alloc));
1708: mumps->val = NULL;
1709: mumps->nnz = 0;
1710: }
1711: }
1712: if (mumps->id.ICNTL(15) == 1) {
1713: if (Mbs != A->rmap->N) {
1714: PetscMPIInt rank, size;
1716: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
1717: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
1718: if (rank == 0) {
1719: PetscInt shift = 0;
1721: PetscCall(PetscMUMPSIntCast(Mbs, &mumps->id.nblk));
1722: PetscCall(PetscFree(mumps->id.blkptr));
1723: PetscCall(PetscMalloc1(Mbs + 1, &mumps->id.blkptr));
1724: mumps->id.blkptr[0] = 1;
1725: for (PetscInt i = 0; i < size; ++i) {
1726: for (PetscInt r = 0; r < nr; r++) {
1727: Mat sub = mats[r][r];
1728: const PetscInt *ranges;
1729: PetscInt bs;
1731: for (PetscInt c = 0; c < nc && !sub; ++c) sub = mats[r][c]; // diagonal Mat is NULL, so start over from the beginning of the current row
1732: PetscCall(MatGetOwnershipRanges(sub, &ranges));
1733: PetscCall(MatGetBlockSize(sub, &bs));
1734: for (PetscInt j = 0, start = mumps->id.blkptr[shift] + bs; j < ranges[i + 1] - ranges[i]; j += bs) PetscCall(PetscMUMPSIntCast(start + j, mumps->id.blkptr + shift + j / bs + 1));
1735: shift += (ranges[i + 1] - ranges[i]) / bs;
1736: }
1737: }
1738: }
1739: } else mumps->id.ICNTL(15) = 0;
1740: }
1741: if (PetscDefined(USE_64BIT_INDICES)) PetscCall(PetscFree(pjcns_w));
1742: for (PetscInt r = 0; r < nr; r++) PetscCall(ISRestoreIndices(rows[r], (const PetscInt **)&rows_idx[r]));
1743: for (PetscInt c = 0; c < nc; c++) PetscCall(ISRestoreIndices(cols[c], (const PetscInt **)&cols_idx[c]));
1744: PetscCall(PetscFree4(rows, cols, rows_idx, cols_idx));
1745: if (!chol) PetscCheck(cumnnz == totnnz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Different number of nonzeros %" PetscCount_FMT " != %" PetscCount_FMT, cumnnz, totnnz);
1746: mumps->nest_vals_start[nr * nc] = cumnnz;
1748: /* Set pointers for final MUMPS data structure */
1749: mumps->nest_vals = vals;
1750: mumps->val_alloc = NULL; /* do not use val_alloc since it may be reallocated with the OMP callpath */
1751: mumps->val = vals;
1752: mumps->irn = irns;
1753: mumps->jcn = jcns;
1754: mumps->nnz = cumnnz;
1755: } else {
1756: PetscScalar *oval = mumps->nest_vals;
1757: for (PetscInt r = 0; r < nr; r++) {
1758: for (PetscInt c = 0; c < nc; c++) {
1759: PetscBool conjugate = PETSC_FALSE;
1760: Mat sub = mats[r][c];
1761: PetscScalar vscale = 1.0, vshift = 0.0;
1762: PetscInt midx = r * nc + c;
1764: if (!mumps->nest_convert_to_triples[midx]) continue;
1765: PetscCall(MatGetTranspose_TransposeVirtual(&sub, &conjugate, &vshift, &vscale, NULL));
1766: PetscCheck(vshift == 0.0, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Nonzero shift in parent MatShell");
1767: mumps->val = oval + mumps->nest_vals_start[midx];
1768: PetscCall((*mumps->nest_convert_to_triples[midx])(sub, shift, MAT_REUSE_MATRIX, mumps));
1769: if (conjugate) {
1770: PetscCount nnz = mumps->nest_vals_start[midx + 1] - mumps->nest_vals_start[midx];
1771: for (PetscCount k = 0; k < nnz; k++) mumps->val[k] = vscale * PetscConj(mumps->val[k]);
1772: } else if (vscale != 1.0) {
1773: PetscCount nnz = mumps->nest_vals_start[midx + 1] - mumps->nest_vals_start[midx];
1774: for (PetscCount k = 0; k < nnz; k++) mumps->val[k] *= vscale;
1775: }
1776: }
1777: }
1778: mumps->val = oval;
1779: }
1780: PetscFunctionReturn(PETSC_SUCCESS);
1781: }
1783: static PetscErrorCode MatDestroy_MUMPS(Mat F)
1784: {
1785: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
1787: PetscFunctionBegin;
1788: PetscCall(MatMumpsResetSchur_Private(F));
1789: PetscCall(PetscFree(mumps->id.isol_loc));
1790: PetscCall(VecScatterDestroy(&mumps->scat_rhs));
1791: PetscCall(VecScatterDestroy(&mumps->scat_sol));
1792: PetscCall(VecDestroy(&mumps->b_seq));
1793: PetscCall(VecDestroy(&mumps->x_seq));
1794: PetscCall(PetscFree(mumps->id.perm_in));
1795: PetscCall(PetscFree(mumps->id.blkvar));
1796: PetscCall(PetscFree(mumps->id.blkptr));
1797: PetscCall(PetscFree2(mumps->irn, mumps->jcn));
1798: PetscCall(PetscFree(mumps->val_alloc));
1799: PetscCall(PetscFree(mumps->info));
1800: PetscCall(PetscFree(mumps->ICNTL_pre));
1801: PetscCall(PetscFree(mumps->CNTL_pre));
1802: if (mumps->id.job != JOB_NULL) { /* cannot call PetscMUMPS_c() if JOB_INIT has never been called for this instance */
1803: mumps->id.job = JOB_END;
1804: PetscMUMPS_c(mumps);
1805: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in termination: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
1806: if (mumps->mumps_comm != MPI_COMM_NULL) {
1807: if (PetscDefined(HAVE_OPENMP_SUPPORT) && mumps->use_petsc_omp_support) PetscCallMPI(MPI_Comm_free(&mumps->mumps_comm));
1808: else PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)F), &mumps->mumps_comm));
1809: }
1810: }
1811: PetscCall(MatMumpsFreeInternalID(&mumps->id));
1812: #if PetscDefined(HAVE_OPENMP_SUPPORT)
1813: if (mumps->use_petsc_omp_support) {
1814: PetscCall(PetscOmpCtrlDestroy(&mumps->omp_ctrl));
1815: PetscCall(PetscFree2(mumps->rhs_loc, mumps->rhs_recvbuf));
1816: PetscCall(PetscFree3(mumps->rhs_nrow, mumps->rhs_recvcounts, mumps->rhs_disps));
1817: }
1818: #endif
1819: PetscCall(PetscFree(mumps->ia_alloc));
1820: PetscCall(PetscFree(mumps->ja_alloc));
1821: PetscCall(PetscFree(mumps->recvcount));
1822: PetscCall(PetscFree(mumps->reqs));
1823: PetscCall(PetscFree(mumps->irhs_loc));
1824: PetscCall(PetscFree2(mumps->nest_vals_start, mumps->nest_convert_to_triples));
1825: PetscCall(PetscFree(mumps->nest_vals));
1826: PetscCall(PetscFree(F->data));
1828: /* clear composed functions */
1829: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatFactorGetSolverType_C", NULL));
1830: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatFactorSetSchurIS_C", NULL));
1831: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatFactorCreateSchurComplement_C", NULL));
1832: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsSetIcntl_C", NULL));
1833: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetIcntl_C", NULL));
1834: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsSetCntl_C", NULL));
1835: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetCntl_C", NULL));
1836: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetInfo_C", NULL));
1837: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetInfog_C", NULL));
1838: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetRinfo_C", NULL));
1839: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetRinfog_C", NULL));
1840: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetNullPivots_C", NULL));
1841: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetInverse_C", NULL));
1842: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsGetInverseTranspose_C", NULL));
1843: PetscCall(PetscObjectComposeFunction((PetscObject)F, "MatMumpsSetBlk_C", NULL));
1844: PetscFunctionReturn(PETSC_SUCCESS);
1845: }
1847: /* Set up the distributed RHS info for MUMPS. <nrhs> is the number of RHS. <array> points to start of RHS on the local processor. */
1848: static PetscErrorCode MatMumpsSetUpDistRHSInfo(Mat A, PetscInt nrhs, const PetscScalar *array)
1849: {
1850: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
1851: const PetscMPIInt ompsize = mumps->omp_comm_size;
1852: PetscInt i, m, M, rstart;
1854: PetscFunctionBegin;
1855: PetscCall(MatGetSize(A, &M, NULL));
1856: PetscCall(MatGetLocalSize(A, &m, NULL));
1857: PetscCheck(M <= PETSC_MUMPS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "PetscInt too long for PetscMUMPSInt");
1858: if (ompsize == 1) {
1859: if (!mumps->irhs_loc) {
1860: mumps->nloc_rhs = (PetscMUMPSInt)m;
1861: PetscCall(PetscMalloc1(m, &mumps->irhs_loc));
1862: PetscCall(MatGetOwnershipRange(A, &rstart, NULL));
1863: for (i = 0; i < m; i++) PetscCall(PetscMUMPSIntCast(rstart + i + 1, &mumps->irhs_loc[i])); /* use 1-based indices */
1864: }
1865: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, m * nrhs, array, mumps->id.precision, &mumps->id.rhs_loc_len, &mumps->id.rhs_loc));
1866: } else {
1867: #if PetscDefined(HAVE_OPENMP_SUPPORT)
1868: const PetscInt *ranges;
1869: PetscMPIInt j, k, sendcount, *petsc_ranks, *omp_ranks;
1870: MPI_Group petsc_group, omp_group;
1871: PetscScalar *recvbuf = NULL;
1873: if (mumps->is_omp_master) {
1874: /* Lazily initialize the omp stuff for distributed rhs */
1875: if (!mumps->irhs_loc) {
1876: PetscCall(PetscMalloc2(ompsize, &omp_ranks, ompsize, &petsc_ranks));
1877: PetscCall(PetscMalloc3(ompsize, &mumps->rhs_nrow, ompsize, &mumps->rhs_recvcounts, ompsize, &mumps->rhs_disps));
1878: PetscCallMPI(MPI_Comm_group(mumps->petsc_comm, &petsc_group));
1879: PetscCallMPI(MPI_Comm_group(mumps->omp_comm, &omp_group));
1880: for (j = 0; j < ompsize; j++) omp_ranks[j] = j;
1881: PetscCallMPI(MPI_Group_translate_ranks(omp_group, ompsize, omp_ranks, petsc_group, petsc_ranks));
1883: /* Populate mumps->irhs_loc[], rhs_nrow[] */
1884: mumps->nloc_rhs = 0;
1885: PetscCall(MatGetOwnershipRanges(A, &ranges));
1886: for (j = 0; j < ompsize; j++) {
1887: mumps->rhs_nrow[j] = ranges[petsc_ranks[j] + 1] - ranges[petsc_ranks[j]];
1888: mumps->nloc_rhs += mumps->rhs_nrow[j];
1889: }
1890: PetscCall(PetscMalloc1(mumps->nloc_rhs, &mumps->irhs_loc));
1891: for (j = k = 0; j < ompsize; j++) {
1892: for (i = ranges[petsc_ranks[j]]; i < ranges[petsc_ranks[j] + 1]; i++, k++) PetscCall(PetscMUMPSIntCast(i + 1, &mumps->irhs_loc[k])); /* uses 1-based indices */
1893: }
1895: PetscCall(PetscFree2(omp_ranks, petsc_ranks));
1896: PetscCallMPI(MPI_Group_free(&petsc_group));
1897: PetscCallMPI(MPI_Group_free(&omp_group));
1898: }
1900: /* Realloc buffers when current nrhs is bigger than what we have met */
1901: if (nrhs > mumps->max_nrhs) {
1902: PetscCall(PetscFree2(mumps->rhs_loc, mumps->rhs_recvbuf));
1903: PetscCall(PetscMalloc2(mumps->nloc_rhs * nrhs, &mumps->rhs_loc, mumps->nloc_rhs * nrhs, &mumps->rhs_recvbuf));
1904: mumps->max_nrhs = nrhs;
1905: }
1907: /* Setup recvcounts[], disps[], recvbuf on omp rank 0 for the upcoming MPI_Gatherv */
1908: for (j = 0; j < ompsize; j++) PetscCall(PetscMPIIntCast(mumps->rhs_nrow[j] * nrhs, &mumps->rhs_recvcounts[j]));
1909: mumps->rhs_disps[0] = 0;
1910: for (j = 1; j < ompsize; j++) {
1911: mumps->rhs_disps[j] = mumps->rhs_disps[j - 1] + mumps->rhs_recvcounts[j - 1];
1912: PetscCheck(mumps->rhs_disps[j] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "PetscMPIInt overflow!");
1913: }
1914: recvbuf = (nrhs == 1) ? mumps->rhs_loc : mumps->rhs_recvbuf; /* Directly use rhs_loc[] as recvbuf. Single rhs is common in Ax=b */
1915: }
1917: PetscCall(PetscMPIIntCast(m * nrhs, &sendcount));
1918: PetscCallMPI(MPI_Gatherv(array, sendcount, MPIU_SCALAR, recvbuf, mumps->rhs_recvcounts, mumps->rhs_disps, MPIU_SCALAR, 0, mumps->omp_comm));
1920: if (mumps->is_omp_master) {
1921: if (nrhs > 1) { /* Copy & re-arrange data from rhs_recvbuf[] to mumps->rhs_loc[] only when there are multiple rhs */
1922: PetscScalar *dst, *dstbase = mumps->rhs_loc;
1923: for (j = 0; j < ompsize; j++) {
1924: const PetscScalar *src = mumps->rhs_recvbuf + mumps->rhs_disps[j];
1925: dst = dstbase;
1926: for (i = 0; i < nrhs; i++) {
1927: PetscCall(PetscArraycpy(dst, src, mumps->rhs_nrow[j]));
1928: src += mumps->rhs_nrow[j];
1929: dst += mumps->nloc_rhs;
1930: }
1931: dstbase += mumps->rhs_nrow[j];
1932: }
1933: }
1934: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nloc_rhs * nrhs, mumps->rhs_loc, mumps->id.precision, &mumps->id.rhs_loc_len, &mumps->id.rhs_loc));
1935: }
1936: #endif /* PETSC_HAVE_OPENMP_SUPPORT */
1937: }
1938: mumps->id.nrhs = (PetscMUMPSInt)nrhs;
1939: mumps->id.nloc_rhs = (PetscMUMPSInt)mumps->nloc_rhs;
1940: mumps->id.lrhs_loc = mumps->nloc_rhs;
1941: mumps->id.irhs_loc = mumps->irhs_loc;
1942: PetscFunctionReturn(PETSC_SUCCESS);
1943: }
1945: static PetscErrorCode MatSolve_MUMPS(Mat A, Vec b, Vec x)
1946: {
1947: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
1948: const PetscScalar *barray = NULL;
1949: PetscScalar *array;
1950: IS is_iden, is_petsc;
1951: PetscBool second_solve = PETSC_FALSE;
1952: static PetscBool cite1 = PETSC_FALSE, cite2 = PETSC_FALSE;
1954: PetscFunctionBegin;
1955: PetscCall(PetscCitationsRegister("@article{MUMPS01,\n author = {P.~R. Amestoy and I.~S. Duff and J.-Y. L'Excellent and J. Koster},\n title = {A fully asynchronous multifrontal solver using distributed dynamic scheduling},\n journal = {SIAM "
1956: "Journal on Matrix Analysis and Applications},\n volume = {23},\n number = {1},\n pages = {15--41},\n year = {2001}\n}\n",
1957: &cite1));
1958: PetscCall(PetscCitationsRegister("@article{MUMPS02,\n author = {P.~R. Amestoy and A. Guermouche and J.-Y. L'Excellent and S. Pralet},\n title = {Hybrid scheduling for the parallel solution of linear systems},\n journal = {Parallel "
1959: "Computing},\n volume = {32},\n number = {2},\n pages = {136--156},\n year = {2006}\n}\n",
1960: &cite2));
1962: PetscCall(VecFlag(x, A->factorerrortype));
1963: if (A->factorerrortype) {
1964: PetscCall(PetscInfo(A, "MatSolve is called with singular matrix factor, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
1965: PetscFunctionReturn(PETSC_SUCCESS);
1966: }
1968: mumps->id.nrhs = 1;
1969: if (mumps->petsc_size > 1) {
1970: if (mumps->ICNTL20 == 10) {
1971: mumps->id.ICNTL(20) = 10; /* dense distributed RHS, need to set rhs_loc[], irhs_loc[] */
1972: PetscCall(VecGetArrayRead(b, &barray));
1973: PetscCall(MatMumpsSetUpDistRHSInfo(A, 1, barray));
1974: } else {
1975: mumps->id.ICNTL(20) = 0; /* dense centralized RHS; Scatter b into a sequential b_seq vector*/
1976: PetscCall(VecScatterBegin(mumps->scat_rhs, b, mumps->b_seq, INSERT_VALUES, SCATTER_FORWARD));
1977: PetscCall(VecScatterEnd(mumps->scat_rhs, b, mumps->b_seq, INSERT_VALUES, SCATTER_FORWARD));
1978: if (!mumps->myid) {
1979: PetscCall(VecGetArray(mumps->b_seq, &array));
1980: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->b_seq->map->n, array, mumps->id.precision, &mumps->id.rhs_len, &mumps->id.rhs));
1981: }
1982: }
1983: } else { /* petsc_size == 1, use MUMPS's dense centralized RHS feature, so that we don't need to bother with isol_loc[] to get the solution */
1984: mumps->id.ICNTL(20) = 0;
1985: PetscCall(VecCopy(b, x));
1986: PetscCall(VecGetArray(x, &array));
1987: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, x->map->n, array, mumps->id.precision, &mumps->id.rhs_len, &mumps->id.rhs));
1988: }
1990: /*
1991: handle condensation step of Schur complement (if any)
1992: We set by default ICNTL(26) == -1 when Schur indices have been provided by the user.
1993: According to MUMPS (5.0.0) manual, any value should be harmful during the factorization phase
1994: Unless the user provides a valid value for ICNTL(26), MatSolve and MatMatSolve routines solve the full system.
1995: This requires an extra call to PetscMUMPS_c and the computation of the factors for S
1996: */
1997: if (mumps->id.size_schur > 0) {
1998: PetscCheck(mumps->petsc_size <= 1, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Parallel Schur complements not yet supported from PETSc");
1999: if (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2) {
2000: second_solve = PETSC_TRUE;
2001: PetscCall(MatMumpsHandleSchur_Private(A, PETSC_FALSE)); // allocate id.redrhs
2002: mumps->id.ICNTL(26) = 1; /* condensation phase */
2003: } else if (mumps->id.ICNTL(26) == 1) PetscCall(MatMumpsHandleSchur_Private(A, PETSC_FALSE));
2004: }
2006: mumps->id.job = JOB_SOLVE;
2007: PetscMUMPS_c(mumps); // reduced solve, put solution in id.redrhs
2008: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in solve: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2010: /* handle expansion step of Schur complement (if any) */
2011: if (second_solve) PetscCall(MatMumpsHandleSchur_Private(A, PETSC_TRUE));
2012: else if (mumps->id.ICNTL(26) == 1) { // condense the right hand side
2013: PetscCall(MatMumpsSolveSchur_Private(A));
2014: for (PetscInt i = 0; i < mumps->id.size_schur; ++i) array[mumps->id.listvar_schur[i] - 1] = ID_FIELD_GET(mumps->id, redrhs, i);
2015: }
2017: if (mumps->petsc_size > 1) { /* convert mumps distributed solution to PETSc mpi x */
2018: if (mumps->scat_sol && mumps->ICNTL9_pre != mumps->id.ICNTL(9)) {
2019: /* when id.ICNTL(9) changes, the contents of ilsol_loc may change (not its size, lsol_loc), recreates scat_sol */
2020: PetscCall(VecScatterDestroy(&mumps->scat_sol));
2021: }
2022: if (!mumps->scat_sol) { /* create scatter scat_sol */
2023: PetscInt *isol2_loc = NULL;
2024: PetscCall(ISCreateStride(PETSC_COMM_SELF, mumps->id.lsol_loc, 0, 1, &is_iden)); /* from */
2025: PetscCall(PetscMalloc1(mumps->id.lsol_loc, &isol2_loc));
2026: for (PetscInt i = 0; i < mumps->id.lsol_loc; i++) isol2_loc[i] = mumps->id.isol_loc[i] - 1; /* change Fortran style to C style */
2027: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, mumps->id.lsol_loc, isol2_loc, PETSC_OWN_POINTER, &is_petsc)); /* to */
2028: PetscCall(VecScatterCreate(mumps->x_seq, is_iden, x, is_petsc, &mumps->scat_sol));
2029: PetscCall(ISDestroy(&is_iden));
2030: PetscCall(ISDestroy(&is_petsc));
2031: mumps->ICNTL9_pre = mumps->id.ICNTL(9); /* save current value of id.ICNTL(9) */
2032: }
2034: PetscScalar *xarray;
2035: PetscCall(VecGetArray(mumps->x_seq, &xarray));
2036: PetscCall(MatMumpsCastMumpsScalarArray(mumps->id.lsol_loc, mumps->id.precision, mumps->id.sol_loc, xarray));
2037: PetscCall(VecRestoreArray(mumps->x_seq, &xarray));
2038: PetscCall(VecScatterBegin(mumps->scat_sol, mumps->x_seq, x, INSERT_VALUES, SCATTER_FORWARD));
2039: PetscCall(VecScatterEnd(mumps->scat_sol, mumps->x_seq, x, INSERT_VALUES, SCATTER_FORWARD));
2041: if (mumps->ICNTL20 == 10) { // distributed RHS
2042: PetscCall(VecRestoreArrayRead(b, &barray));
2043: } else if (!mumps->myid) { // centralized RHS
2044: PetscCall(VecRestoreArray(mumps->b_seq, &array));
2045: }
2046: } else {
2047: // id.rhs has the solution in mumps precision
2048: PetscCall(MatMumpsCastMumpsScalarArray(x->map->n, mumps->id.precision, mumps->id.rhs, array));
2049: PetscCall(VecRestoreArray(x, &array));
2050: }
2052: PetscCall(PetscLogFlops(2.0 * PetscMax(0, (mumps->id.INFO(28) >= 0 ? mumps->id.INFO(28) : -1000000 * mumps->id.INFO(28)) - A->cmap->n)));
2053: PetscFunctionReturn(PETSC_SUCCESS);
2054: }
2056: static PetscErrorCode MatSolveTranspose_MUMPS(Mat A, Vec b, Vec x)
2057: {
2058: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
2059: const PetscMUMPSInt value = mumps->id.ICNTL(9);
2061: PetscFunctionBegin;
2062: mumps->id.ICNTL(9) = 0;
2063: PetscCall(MatSolve_MUMPS(A, b, x));
2064: mumps->id.ICNTL(9) = value;
2065: PetscFunctionReturn(PETSC_SUCCESS);
2066: }
2068: static PetscErrorCode MatMatSolve_MUMPS(Mat A, Mat B, Mat X)
2069: {
2070: Mat Bt = NULL;
2071: PetscBool denseX, denseB, flg, flgT;
2072: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
2073: PetscInt i, nrhs, M, nrhsM;
2074: PetscScalar *array;
2075: const PetscScalar *barray;
2076: PetscInt lsol_loc, nlsol_loc, *idxx, iidx = 0;
2077: PetscMUMPSInt *isol_loc, *isol_loc_save;
2078: PetscScalar *sol_loc;
2079: void *sol_loc_save;
2080: PetscCount sol_loc_len_save;
2081: IS is_to, is_from;
2082: PetscInt k, proc, j, m, myrstart;
2083: const PetscInt *rstart;
2084: Vec v_mpi, msol_loc;
2085: VecScatter scat_sol;
2086: Vec b_seq;
2087: VecScatter scat_rhs;
2088: PetscScalar *aa;
2089: PetscInt spnr, *ia, *ja;
2090: Mat_MPIAIJ *b = NULL;
2092: PetscFunctionBegin;
2093: PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &denseX, MATSEQDENSE, MATMPIDENSE, NULL));
2094: PetscCheck(denseX, PetscObjectComm((PetscObject)X), PETSC_ERR_ARG_WRONG, "Matrix X must be MATDENSE matrix");
2096: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &denseB, MATSEQDENSE, MATMPIDENSE, NULL));
2098: if (denseB) {
2099: PetscCheck(B->rmap->n == X->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Matrix B and X must have same row distribution");
2100: mumps->id.ICNTL(20) = 0; /* dense RHS */
2101: } else { /* sparse B */
2102: PetscCheck(X != B, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_IDN, "X and B must be different matrices");
2103: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATTRANSPOSEVIRTUAL, &flgT));
2104: PetscCheck(flgT, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONG, "Matrix B must be MATTRANSPOSEVIRTUAL matrix");
2105: PetscCall(MatShellGetScalingShifts(B, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
2106: /* input B is transpose of actual RHS matrix,
2107: because mumps requires sparse compressed COLUMN storage! See MatMatTransposeSolve_MUMPS() */
2108: PetscCall(MatTransposeGetMat(B, &Bt));
2109: mumps->id.ICNTL(20) = 1; /* sparse RHS */
2110: }
2112: PetscCall(MatGetSize(B, &M, &nrhs));
2113: PetscCall(PetscIntMultError(nrhs, M, &nrhsM));
2114: mumps->id.nrhs = (PetscMUMPSInt)nrhs;
2115: mumps->id.lrhs = (PetscMUMPSInt)M;
2117: if (mumps->petsc_size == 1) { // handle this easy case specially and return early
2118: PetscScalar *aa;
2119: PetscInt spnr, *ia, *ja;
2120: PetscBool second_solve = PETSC_FALSE;
2122: PetscCall(MatDenseGetArray(X, &array));
2123: if (denseB) {
2124: /* copy B to X */
2125: PetscCall(MatDenseGetArrayRead(B, &barray));
2126: PetscCall(PetscArraycpy(array, barray, nrhsM));
2127: PetscCall(MatDenseRestoreArrayRead(B, &barray));
2128: } else { /* sparse B */
2129: PetscCall(MatSeqAIJGetArray(Bt, &aa));
2130: PetscCall(MatGetRowIJ(Bt, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
2131: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot get IJ structure");
2132: PetscCall(PetscMUMPSIntCSRCast(mumps, spnr, ia, ja, &mumps->id.irhs_ptr, &mumps->id.irhs_sparse, &mumps->id.nz_rhs));
2133: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->id.nz_rhs, aa, mumps->id.precision, &mumps->id.rhs_sparse_len, &mumps->id.rhs_sparse));
2134: }
2135: PetscCall(MatMumpsMakeMumpsScalarArray(denseB, nrhsM, array, mumps->id.precision, &mumps->id.rhs_len, &mumps->id.rhs));
2137: /* handle condensation step of Schur complement (if any) */
2138: if (mumps->id.size_schur > 0) {
2139: if (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2) {
2140: second_solve = PETSC_TRUE;
2141: PetscCall(MatMumpsHandleSchur_Private(A, PETSC_FALSE)); // allocate id.redrhs
2142: mumps->id.ICNTL(26) = 1; /* condensation phase, i.e, to solve id.redrhs */
2143: } else if (mumps->id.ICNTL(26) == 1) PetscCall(MatMumpsHandleSchur_Private(A, PETSC_FALSE));
2144: }
2146: mumps->id.job = JOB_SOLVE;
2147: PetscMUMPS_c(mumps);
2148: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in solve: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2150: /* handle expansion step of Schur complement (if any) */
2151: if (second_solve) PetscCall(MatMumpsHandleSchur_Private(A, PETSC_TRUE));
2152: else if (mumps->id.ICNTL(26) == 1) { // condense the right hand side
2153: PetscCall(MatMumpsSolveSchur_Private(A));
2154: for (j = 0; j < nrhs; ++j)
2155: for (i = 0; i < mumps->id.size_schur; ++i) array[mumps->id.listvar_schur[i] - 1 + j * M] = ID_FIELD_GET(mumps->id, redrhs, i + j * mumps->id.lredrhs);
2156: }
2158: if (!denseB) { /* sparse B, restore ia, ja */
2159: PetscCall(MatSeqAIJRestoreArray(Bt, &aa));
2160: PetscCall(MatRestoreRowIJ(Bt, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
2161: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot restore IJ structure");
2162: }
2164: // no matter dense B or sparse B, solution is in id.rhs; convert it to array of X.
2165: PetscCall(MatMumpsCastMumpsScalarArray(nrhsM, mumps->id.precision, mumps->id.rhs, array));
2166: PetscCall(MatDenseRestoreArray(X, &array));
2167: PetscFunctionReturn(PETSC_SUCCESS);
2168: }
2170: /* parallel case: MUMPS requires rhs B to be centralized on the host! */
2171: PetscCheck(!mumps->id.ICNTL(19), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Parallel Schur complements not yet supported from PETSc");
2173: /* create msol_loc to hold mumps local solution */
2174: isol_loc_save = mumps->id.isol_loc; /* save these, as we want to reuse them in MatSolve() */
2175: sol_loc_save = mumps->id.sol_loc;
2176: sol_loc_len_save = mumps->id.sol_loc_len;
2177: mumps->id.isol_loc = NULL; // an init state
2178: mumps->id.sol_loc = NULL;
2179: mumps->id.sol_loc_len = 0;
2181: lsol_loc = mumps->id.lsol_loc;
2182: PetscCall(PetscIntMultError(nrhs, lsol_loc, &nlsol_loc)); /* length of sol_loc */
2183: PetscCall(PetscMalloc2(nlsol_loc, &sol_loc, lsol_loc, &isol_loc));
2184: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_FALSE, nlsol_loc, sol_loc, mumps->id.precision, &mumps->id.sol_loc_len, &mumps->id.sol_loc));
2185: mumps->id.isol_loc = isol_loc;
2187: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nlsol_loc, (PetscScalar *)sol_loc, &msol_loc));
2189: if (denseB) {
2190: if (mumps->ICNTL20 == 10) {
2191: mumps->id.ICNTL(20) = 10; /* dense distributed RHS */
2192: PetscCall(MatDenseGetArrayRead(B, &barray));
2193: PetscCall(MatMumpsSetUpDistRHSInfo(A, nrhs, barray)); // put barray to rhs_loc
2194: PetscCall(MatDenseRestoreArrayRead(B, &barray));
2195: PetscCall(MatGetLocalSize(B, &m, NULL));
2196: PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)B), 1, nrhs * m, nrhsM, NULL, &v_mpi)); // will scatter the solution to v_mpi, which wraps X
2197: } else {
2198: mumps->id.ICNTL(20) = 0; /* dense centralized RHS */
2199: /* TODO: Because of non-contiguous indices, the created vecscatter scat_rhs is not done in MPI_Gather, resulting in
2200: very inefficient communication. An optimization is to use VecScatterCreateToZero to gather B to rank 0. Then on rank
2201: 0, re-arrange B into desired order, which is a local operation.
2202: */
2204: /* scatter v_mpi to b_seq because MUMPS before 5.3.0 only supports centralized rhs */
2205: /* wrap dense rhs matrix B into a vector v_mpi */
2206: PetscCall(MatGetLocalSize(B, &m, NULL));
2207: PetscCall(MatDenseGetArrayRead(B, &barray));
2208: PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)B), 1, nrhs * m, nrhsM, barray, &v_mpi));
2209: PetscCall(MatDenseRestoreArrayRead(B, &barray));
2211: /* scatter v_mpi to b_seq in proc[0]. With ICNTL(20) = 0, MUMPS requires rhs to be centralized on the host! */
2212: if (!mumps->myid) {
2213: PetscInt *idx;
2214: /* idx: maps from k-th index of v_mpi to (i,j)-th global entry of B */
2215: PetscCall(PetscMalloc1(nrhsM, &idx));
2216: PetscCall(MatGetOwnershipRanges(B, &rstart));
2217: for (proc = 0, k = 0; proc < mumps->petsc_size; proc++) {
2218: for (j = 0; j < nrhs; j++) {
2219: for (i = rstart[proc]; i < rstart[proc + 1]; i++) idx[k++] = j * M + i;
2220: }
2221: }
2223: PetscCall(VecCreateSeq(PETSC_COMM_SELF, nrhsM, &b_seq));
2224: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nrhsM, idx, PETSC_OWN_POINTER, &is_to));
2225: PetscCall(ISCreateStride(PETSC_COMM_SELF, nrhsM, 0, 1, &is_from));
2226: } else {
2227: PetscCall(VecCreateSeq(PETSC_COMM_SELF, 0, &b_seq));
2228: PetscCall(ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &is_to));
2229: PetscCall(ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &is_from));
2230: }
2232: PetscCall(VecScatterCreate(v_mpi, is_from, b_seq, is_to, &scat_rhs));
2233: PetscCall(VecScatterBegin(scat_rhs, v_mpi, b_seq, INSERT_VALUES, SCATTER_FORWARD));
2234: PetscCall(ISDestroy(&is_to));
2235: PetscCall(ISDestroy(&is_from));
2236: PetscCall(VecScatterEnd(scat_rhs, v_mpi, b_seq, INSERT_VALUES, SCATTER_FORWARD));
2238: if (!mumps->myid) { /* define rhs on the host */
2239: PetscCall(VecGetArrayRead(b_seq, &barray));
2240: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, nrhsM, barray, mumps->id.precision, &mumps->id.rhs_len, &mumps->id.rhs));
2241: PetscCall(VecRestoreArrayRead(b_seq, &barray));
2242: }
2243: }
2244: } else { /* sparse B */
2245: b = (Mat_MPIAIJ *)Bt->data;
2247: /* wrap dense X into a vector v_mpi */
2248: PetscCall(MatGetLocalSize(X, &m, NULL));
2249: PetscCall(MatDenseGetArrayRead(X, &barray));
2250: PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)X), 1, nrhs * m, nrhsM, barray, &v_mpi));
2251: PetscCall(MatDenseRestoreArrayRead(X, &barray));
2253: if (!mumps->myid) {
2254: PetscCall(MatSeqAIJGetArray(b->A, &aa));
2255: PetscCall(MatGetRowIJ(b->A, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
2256: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot get IJ structure");
2257: PetscCall(PetscMUMPSIntCSRCast(mumps, spnr, ia, ja, &mumps->id.irhs_ptr, &mumps->id.irhs_sparse, &mumps->id.nz_rhs));
2258: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, ((Mat_SeqAIJ *)b->A->data)->nz, aa, mumps->id.precision, &mumps->id.rhs_sparse_len, &mumps->id.rhs_sparse));
2259: } else {
2260: mumps->id.irhs_ptr = NULL;
2261: mumps->id.irhs_sparse = NULL;
2262: mumps->id.nz_rhs = 0;
2263: if (mumps->id.rhs_sparse_len) {
2264: PetscCall(PetscFree(mumps->id.rhs_sparse));
2265: mumps->id.rhs_sparse_len = 0;
2266: }
2267: }
2268: }
2270: /* solve phase */
2271: mumps->id.job = JOB_SOLVE;
2272: PetscMUMPS_c(mumps);
2273: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in solve: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2275: /* scatter mumps distributed solution to PETSc vector v_mpi, which shares local arrays with solution matrix X */
2276: PetscCall(MatDenseGetArray(X, &array));
2277: PetscCall(VecPlaceArray(v_mpi, array));
2279: /* create scatter scat_sol */
2280: PetscCall(MatGetOwnershipRanges(X, &rstart));
2281: /* iidx: index for scatter mumps solution to PETSc X */
2283: PetscCall(ISCreateStride(PETSC_COMM_SELF, nlsol_loc, 0, 1, &is_from));
2284: PetscCall(PetscMalloc1(nlsol_loc, &idxx));
2285: for (i = 0; i < lsol_loc; i++) {
2286: isol_loc[i] -= 1; /* change Fortran style to C style. isol_loc[i+j*lsol_loc] contains x[isol_loc[i]] in j-th vector */
2288: for (proc = 0; proc < mumps->petsc_size; proc++) {
2289: if (isol_loc[i] >= rstart[proc] && isol_loc[i] < rstart[proc + 1]) {
2290: myrstart = rstart[proc];
2291: k = isol_loc[i] - myrstart; /* local index on 1st column of PETSc vector X */
2292: iidx = k + myrstart * nrhs; /* maps mumps isol_loc[i] to PETSc index in X */
2293: m = rstart[proc + 1] - rstart[proc]; /* rows of X for this proc */
2294: break;
2295: }
2296: }
2298: for (j = 0; j < nrhs; j++) idxx[i + j * lsol_loc] = iidx + j * m;
2299: }
2300: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nlsol_loc, idxx, PETSC_COPY_VALUES, &is_to));
2301: PetscCall(MatMumpsCastMumpsScalarArray(nlsol_loc, mumps->id.precision, mumps->id.sol_loc, sol_loc)); // Vec msol_loc is created with sol_loc[]
2302: PetscCall(VecScatterCreate(msol_loc, is_from, v_mpi, is_to, &scat_sol));
2303: PetscCall(VecScatterBegin(scat_sol, msol_loc, v_mpi, INSERT_VALUES, SCATTER_FORWARD));
2304: PetscCall(ISDestroy(&is_from));
2305: PetscCall(ISDestroy(&is_to));
2306: PetscCall(VecScatterEnd(scat_sol, msol_loc, v_mpi, INSERT_VALUES, SCATTER_FORWARD));
2307: PetscCall(MatDenseRestoreArray(X, &array));
2309: if (mumps->id.sol_loc_len) { // in case we allocated intermediate buffers
2310: mumps->id.sol_loc_len = 0;
2311: PetscCall(PetscFree(mumps->id.sol_loc));
2312: }
2314: // restore old values
2315: mumps->id.sol_loc = sol_loc_save;
2316: mumps->id.sol_loc_len = sol_loc_len_save;
2317: mumps->id.isol_loc = isol_loc_save;
2319: PetscCall(PetscFree2(sol_loc, isol_loc));
2320: PetscCall(PetscFree(idxx));
2321: PetscCall(VecDestroy(&msol_loc));
2322: PetscCall(VecDestroy(&v_mpi));
2323: if (!denseB) {
2324: if (!mumps->myid) {
2325: b = (Mat_MPIAIJ *)Bt->data;
2326: PetscCall(MatSeqAIJRestoreArray(b->A, &aa));
2327: PetscCall(MatRestoreRowIJ(b->A, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
2328: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot restore IJ structure");
2329: }
2330: } else {
2331: if (mumps->ICNTL20 == 0) {
2332: PetscCall(VecDestroy(&b_seq));
2333: PetscCall(VecScatterDestroy(&scat_rhs));
2334: }
2335: }
2336: PetscCall(VecScatterDestroy(&scat_sol));
2337: PetscCall(PetscLogFlops(nrhs * PetscMax(0, 2.0 * (mumps->id.INFO(28) >= 0 ? mumps->id.INFO(28) : -1000000 * mumps->id.INFO(28)) - A->cmap->n)));
2338: PetscFunctionReturn(PETSC_SUCCESS);
2339: }
2341: static PetscErrorCode MatMatSolveTranspose_MUMPS(Mat A, Mat B, Mat X)
2342: {
2343: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
2344: const PetscMUMPSInt value = mumps->id.ICNTL(9);
2346: PetscFunctionBegin;
2347: mumps->id.ICNTL(9) = 0;
2348: PetscCall(MatMatSolve_MUMPS(A, B, X));
2349: mumps->id.ICNTL(9) = value;
2350: PetscFunctionReturn(PETSC_SUCCESS);
2351: }
2353: static PetscErrorCode MatMatTransposeSolve_MUMPS(Mat A, Mat Bt, Mat X)
2354: {
2355: PetscBool flg;
2356: Mat B;
2358: PetscFunctionBegin;
2359: PetscCall(PetscObjectTypeCompareAny((PetscObject)Bt, &flg, MATSEQAIJ, MATMPIAIJ, NULL));
2360: PetscCheck(flg, PetscObjectComm((PetscObject)Bt), PETSC_ERR_ARG_WRONG, "Matrix Bt must be MATAIJ matrix");
2362: /* Create B=Bt^T that uses Bt's data structure */
2363: PetscCall(MatCreateTranspose(Bt, &B));
2365: PetscCall(MatMatSolve_MUMPS(A, B, X));
2366: PetscCall(MatDestroy(&B));
2367: PetscFunctionReturn(PETSC_SUCCESS);
2368: }
2370: #if !PetscDefined(USE_COMPLEX)
2371: /*
2372: input:
2373: F: numeric factor
2374: output:
2375: nneg: total number of negative pivots
2376: nzero: total number of zero pivots
2377: npos: (global dimension of F) - nneg - nzero
2378: */
2379: static PetscErrorCode MatGetInertia_SBAIJMUMPS(Mat F, PetscInt *nneg, PetscInt *nzero, PetscInt *npos)
2380: {
2381: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
2382: PetscMPIInt size;
2384: PetscFunctionBegin;
2385: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)F), &size));
2386: /* MUMPS 4.3.1 calls ScaLAPACK when ICNTL(13)=0 (default), which does not offer the possibility to compute the inertia of a dense matrix. Set ICNTL(13)=1 to skip ScaLAPACK */
2387: PetscCheck(size <= 1 || mumps->id.ICNTL(13) == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "ICNTL(13)=%d. -mat_mumps_icntl_13 must be set as 1 for correct global matrix inertia", mumps->id.INFOG(13));
2389: if (nneg) *nneg = mumps->id.INFOG(12);
2390: if (nzero || npos) {
2391: PetscCheck(mumps->id.ICNTL(24) == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "-mat_mumps_icntl_24 must be set as 1 for null pivot row detection");
2392: if (nzero) *nzero = mumps->id.INFOG(28);
2393: if (npos) *npos = F->rmap->N - (mumps->id.INFOG(12) + mumps->id.INFOG(28));
2394: }
2395: PetscFunctionReturn(PETSC_SUCCESS);
2396: }
2397: #endif
2399: static PetscErrorCode MatMumpsGatherNonzerosOnMaster(MatReuse reuse, Mat_MUMPS *mumps)
2400: {
2401: PetscMPIInt nreqs;
2402: PetscMUMPSInt *irn, *jcn;
2403: PetscMPIInt count;
2404: PetscCount totnnz, remain;
2405: const PetscInt osize = mumps->omp_comm_size;
2406: PetscScalar *val;
2408: PetscFunctionBegin;
2409: if (osize > 1) {
2410: if (reuse == MAT_INITIAL_MATRIX) {
2411: /* master first gathers counts of nonzeros to receive */
2412: if (mumps->is_omp_master) PetscCall(PetscMalloc1(osize, &mumps->recvcount));
2413: PetscCallMPI(MPI_Gather(&mumps->nnz, 1, MPIU_INT64, mumps->recvcount, 1, MPIU_INT64, 0 /*master*/, mumps->omp_comm));
2415: /* Then each computes number of send/recvs */
2416: if (mumps->is_omp_master) {
2417: /* Start from 1 since self communication is not done in MPI */
2418: nreqs = 0;
2419: for (PetscMPIInt i = 1; i < osize; i++) nreqs += (mumps->recvcount[i] + PETSC_MPI_INT_MAX - 1) / PETSC_MPI_INT_MAX;
2420: } else {
2421: nreqs = (PetscMPIInt)(((mumps->nnz + PETSC_MPI_INT_MAX - 1) / PETSC_MPI_INT_MAX));
2422: }
2423: PetscCall(PetscMalloc1(nreqs * 3, &mumps->reqs)); /* Triple the requests since we send irn, jcn and val separately */
2425: /* The following code is doing a very simple thing: omp_master rank gathers irn/jcn/val from others.
2426: MPI_Gatherv would be enough if it supports big counts > 2^31-1. Since it does not, and mumps->nnz
2427: might be a prime number > 2^31-1, we have to slice the message. Note omp_comm_size
2428: is very small, the current approach should have no extra overhead compared to MPI_Gatherv.
2429: */
2430: nreqs = 0; /* counter for actual send/recvs */
2431: if (mumps->is_omp_master) {
2432: totnnz = 0;
2434: for (PetscMPIInt i = 0; i < osize; i++) totnnz += mumps->recvcount[i]; /* totnnz = sum of nnz over omp_comm */
2435: PetscCall(PetscMalloc2(totnnz, &irn, totnnz, &jcn));
2436: PetscCall(PetscMalloc1(totnnz, &val));
2438: /* Self communication */
2439: PetscCall(PetscArraycpy(irn, mumps->irn, mumps->nnz));
2440: PetscCall(PetscArraycpy(jcn, mumps->jcn, mumps->nnz));
2441: PetscCall(PetscArraycpy(val, mumps->val, mumps->nnz));
2443: /* Replace mumps->irn/jcn etc on master with the newly allocated bigger arrays */
2444: PetscCall(PetscFree2(mumps->irn, mumps->jcn));
2445: PetscCall(PetscFree(mumps->val_alloc));
2446: mumps->nnz = totnnz;
2447: mumps->irn = irn;
2448: mumps->jcn = jcn;
2449: mumps->val = mumps->val_alloc = val;
2451: irn += mumps->recvcount[0]; /* recvcount[0] is old mumps->nnz on omp rank 0 */
2452: jcn += mumps->recvcount[0];
2453: val += mumps->recvcount[0];
2455: /* Remote communication */
2456: for (PetscMPIInt i = 1; i < osize; i++) {
2457: count = (PetscMPIInt)PetscMin(mumps->recvcount[i], (PetscMPIInt)PETSC_MPI_INT_MAX);
2458: remain = mumps->recvcount[i] - count;
2459: while (count > 0) {
2460: PetscCallMPI(MPIU_Irecv(irn, count, MPIU_MUMPSINT, i, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2461: PetscCallMPI(MPIU_Irecv(jcn, count, MPIU_MUMPSINT, i, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2462: PetscCallMPI(MPIU_Irecv(val, count, MPIU_SCALAR, i, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2463: irn += count;
2464: jcn += count;
2465: val += count;
2466: count = (PetscMPIInt)PetscMin(remain, (PetscMPIInt)PETSC_MPI_INT_MAX);
2467: remain -= count;
2468: }
2469: }
2470: } else {
2471: irn = mumps->irn;
2472: jcn = mumps->jcn;
2473: val = mumps->val;
2474: count = (PetscMPIInt)PetscMin(mumps->nnz, (PetscMPIInt)PETSC_MPI_INT_MAX);
2475: remain = mumps->nnz - count;
2476: while (count > 0) {
2477: PetscCallMPI(MPIU_Isend(irn, count, MPIU_MUMPSINT, 0, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2478: PetscCallMPI(MPIU_Isend(jcn, count, MPIU_MUMPSINT, 0, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2479: PetscCallMPI(MPIU_Isend(val, count, MPIU_SCALAR, 0, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2480: irn += count;
2481: jcn += count;
2482: val += count;
2483: count = (PetscMPIInt)PetscMin(remain, (PetscMPIInt)PETSC_MPI_INT_MAX);
2484: remain -= count;
2485: }
2486: }
2487: } else {
2488: nreqs = 0;
2489: if (mumps->is_omp_master) {
2490: val = mumps->val + mumps->recvcount[0];
2491: for (PetscMPIInt i = 1; i < osize; i++) { /* Remote communication only since self data is already in place */
2492: count = (PetscMPIInt)PetscMin(mumps->recvcount[i], (PetscMPIInt)PETSC_MPI_INT_MAX);
2493: remain = mumps->recvcount[i] - count;
2494: while (count > 0) {
2495: PetscCallMPI(MPIU_Irecv(val, count, MPIU_SCALAR, i, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2496: val += count;
2497: count = (PetscMPIInt)PetscMin(remain, (PetscMPIInt)PETSC_MPI_INT_MAX);
2498: remain -= count;
2499: }
2500: }
2501: } else {
2502: val = mumps->val;
2503: count = (PetscMPIInt)PetscMin(mumps->nnz, (PetscMPIInt)PETSC_MPI_INT_MAX);
2504: remain = mumps->nnz - count;
2505: while (count > 0) {
2506: PetscCallMPI(MPIU_Isend(val, count, MPIU_SCALAR, 0, mumps->tag, mumps->omp_comm, &mumps->reqs[nreqs++]));
2507: val += count;
2508: count = (PetscMPIInt)PetscMin(remain, (PetscMPIInt)PETSC_MPI_INT_MAX);
2509: remain -= count;
2510: }
2511: }
2512: }
2513: PetscCallMPI(MPI_Waitall(nreqs, mumps->reqs, MPI_STATUSES_IGNORE));
2514: mumps->tag++; /* It is totally fine for above send/recvs to share one mpi tag */
2515: }
2516: PetscFunctionReturn(PETSC_SUCCESS);
2517: }
2519: static PetscErrorCode MatFactorNumeric_MUMPS(Mat F, Mat A, PETSC_UNUSED const MatFactorInfo *info)
2520: {
2521: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
2523: PetscFunctionBegin;
2524: if (mumps->id.INFOG(1) < 0 && !(mumps->id.INFOG(1) == -16 && mumps->id.INFOG(1) == 0)) {
2525: if (mumps->id.INFOG(1) == -6) PetscCall(PetscInfo(A, "MatFactorNumeric is called with singular matrix structure, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2526: PetscCall(PetscInfo(A, "MatFactorNumeric is called after analysis phase fails, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2527: PetscFunctionReturn(PETSC_SUCCESS);
2528: }
2530: PetscCall((*mumps->ConvertToTriples)(A, 1, MAT_REUSE_MATRIX, mumps));
2531: PetscCall(MatMumpsGatherNonzerosOnMaster(MAT_REUSE_MATRIX, mumps));
2532: PetscCall(MatMumpsEnsureSchurArray_Private(F));
2534: /* numerical factorization phase */
2535: mumps->id.job = JOB_FACTNUMERIC;
2536: if (!mumps->id.ICNTL(18)) { /* A is centralized */
2537: if (!mumps->myid) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_len, &mumps->id.a));
2538: } else {
2539: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_loc_len, &mumps->id.a_loc));
2540: }
2542: if (mumps->id.ICNTL(22)) PetscCall(PetscStrncpy(mumps->id.ooc_prefix, ((PetscObject)F)->prefix, sizeof(((MUMPS_STRUC_C *)NULL)->ooc_prefix)));
2543: if (A->rmap->N && A->cmap->N) PetscMUMPS_c(mumps);
2544: if (mumps->id.INFOG(1) < 0) {
2545: PetscCheck(!A->erroriffailure, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in numerical factorization: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2546: if (mumps->id.INFOG(1) == -10) {
2547: PetscCall(PetscInfo(F, "MUMPS error in numerical factorization: matrix is numerically singular, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2548: F->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
2549: } else if (mumps->id.INFOG(1) == -13) {
2550: PetscCall(PetscInfo(F, "MUMPS error in numerical factorization: INFOG(1)=%d, cannot allocate required memory %d megabytes\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2551: F->factorerrortype = MAT_FACTOR_OUTMEMORY;
2552: } else if (mumps->id.INFOG(1) == -8 || mumps->id.INFOG(1) == -9 || (-16 < mumps->id.INFOG(1) && mumps->id.INFOG(1) < -10)) {
2553: PetscCall(PetscInfo(F, "MUMPS error in numerical factorization: INFOG(1)=%d, INFO(2)=%d, problem with work array\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2554: F->factorerrortype = MAT_FACTOR_OUTMEMORY;
2555: } else {
2556: PetscCall(PetscInfo(F, "MUMPS error in numerical factorization: INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2557: F->factorerrortype = MAT_FACTOR_OTHER;
2558: }
2559: }
2560: PetscCheck(mumps->myid || mumps->id.ICNTL(16) <= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in numerical factorization: ICNTL(16)=%d " MUMPS_MANUALS, mumps->id.INFOG(16));
2562: F->assembled = PETSC_TRUE;
2564: if (F->schur) { /* reset Schur status to unfactored */
2565: #if PetscDefined(HAVE_CUDA)
2566: F->schur->offloadmask = PETSC_OFFLOAD_CPU;
2567: #endif
2568: PetscScalar *array;
2569: PetscCall(MatDenseGetArray(F->schur, &array));
2570: PetscCall(MatMumpsCastMumpsScalarArray(mumps->id.size_schur * mumps->id.size_schur, mumps->id.precision, mumps->id.schur, array));
2571: PetscCall(MatDenseRestoreArray(F->schur, &array));
2572: if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
2573: mumps->id.ICNTL(19) = 2;
2574: PetscCall(MatTranspose(F->schur, MAT_INPLACE_MATRIX, &F->schur));
2575: }
2576: PetscCall(MatFactorRestoreSchurComplement(F, NULL, MAT_FACTOR_SCHUR_UNFACTORED));
2577: }
2579: /* just to be sure that ICNTL(19) value returned by a call from MatMumpsGetIcntl is always consistent */
2580: if (!mumps->sym && mumps->id.ICNTL(19) && mumps->id.ICNTL(19) != 1) mumps->id.ICNTL(19) = 3;
2582: if (!mumps->is_omp_master) mumps->id.INFO(23) = 0;
2583: // MUMPS userguide: ISOL_loc should be allocated by the user between the factorization and the
2584: // solve phases. On exit from the solve phase, ISOL_loc(i) contains the index of the variables for
2585: // which the solution (in SOL_loc) is available on the local processor.
2586: // If successive calls to the solve phase (JOB= 3) are performed for a given matrix, ISOL_loc will
2587: // normally have the same contents for each of these calls. The only exception is the case of
2588: // unsymmetric matrices (SYM=1) when the transpose option is changed (see ICNTL(9)) and non
2589: // symmetric row/column exchanges (see ICNTL(6)) have occurred before the solve phase.
2590: if (mumps->petsc_size > 1) {
2591: PetscInt lsol_loc;
2592: PetscScalar *array;
2594: /* distributed solution; Create x_seq=sol_loc for repeated use */
2595: if (mumps->x_seq) {
2596: PetscCall(VecScatterDestroy(&mumps->scat_sol));
2597: PetscCall(PetscFree(mumps->id.isol_loc));
2598: PetscCall(VecDestroy(&mumps->x_seq));
2599: }
2600: lsol_loc = mumps->id.INFO(23); /* length of sol_loc */
2601: PetscCall(PetscMalloc1(lsol_loc, &mumps->id.isol_loc));
2602: PetscCall(VecCreateSeq(PETSC_COMM_SELF, lsol_loc, &mumps->x_seq));
2603: PetscCall(VecGetArray(mumps->x_seq, &array));
2604: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_FALSE, lsol_loc, array, mumps->id.precision, &mumps->id.sol_loc_len, &mumps->id.sol_loc));
2605: PetscCall(VecRestoreArray(mumps->x_seq, &array));
2606: mumps->id.lsol_loc = (PetscMUMPSInt)lsol_loc;
2607: }
2608: PetscCall(PetscLogFlops((double)ID_RINFO_GET(mumps->id, 2)));
2609: PetscFunctionReturn(PETSC_SUCCESS);
2610: }
2612: /* Sets MUMPS options from the options database */
2613: static PetscErrorCode MatSetFromOptions_MUMPS(Mat F, Mat A)
2614: {
2615: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
2616: PetscReal cntl;
2617: PetscMUMPSInt icntl = 0, size, *listvar_schur;
2618: PetscInt info[80], i, ninfo = 80, rbs, cbs;
2619: PetscBool flg = PETSC_FALSE;
2620: PetscBool schur = mumps->id.icntl ? (PetscBool)(mumps->id.ICNTL(26) == -1) : (PetscBool)(mumps->ICNTL26 == -1);
2621: void *arr;
2623: PetscFunctionBegin;
2624: PetscOptionsBegin(PetscObjectComm((PetscObject)F), ((PetscObject)F)->prefix, "MUMPS Options", "Mat");
2625: if (mumps->id.job == JOB_NULL) { /* MatSetFromOptions_MUMPS() has never been called before */
2626: PetscPrecision precision = PetscDefined(USE_REAL_SINGLE) ? PETSC_PRECISION_SINGLE : PETSC_PRECISION_DOUBLE;
2627: PetscInt nthreads = 0;
2628: PetscInt nCNTL_pre = mumps->CNTL_pre ? mumps->CNTL_pre[0] : 0;
2629: PetscInt nICNTL_pre = mumps->ICNTL_pre ? mumps->ICNTL_pre[0] : 0;
2630: PetscMUMPSInt nblk, *blkvar, *blkptr;
2632: mumps->petsc_comm = PetscObjectComm((PetscObject)A);
2633: PetscCallMPI(MPI_Comm_size(mumps->petsc_comm, &mumps->petsc_size));
2634: PetscCallMPI(MPI_Comm_rank(mumps->petsc_comm, &mumps->myid)); /* "if (!myid)" still works even if mumps_comm is different */
2636: PetscCall(PetscOptionsName("-mat_mumps_use_omp_threads", "Convert MPI processes into OpenMP threads", "None", &mumps->use_petsc_omp_support));
2637: if (mumps->use_petsc_omp_support) nthreads = -1; /* -1 will let PetscOmpCtrlCreate() guess a proper value when user did not supply one */
2638: /* do not use PetscOptionsInt() so that the option -mat_mumps_use_omp_threads is not displayed twice in the help */
2639: PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)F)->prefix, "-mat_mumps_use_omp_threads", &nthreads, NULL));
2640: if (mumps->use_petsc_omp_support) {
2641: PetscCheck(!schur, PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot use -%smat_mumps_use_omp_threads with the Schur complement feature", ((PetscObject)F)->prefix ? ((PetscObject)F)->prefix : "");
2642: #if PetscDefined(HAVE_OPENMP_SUPPORT)
2643: PetscCall(PetscOmpCtrlCreate(mumps->petsc_comm, nthreads, &mumps->omp_ctrl));
2644: PetscCall(PetscOmpCtrlGetOmpComms(mumps->omp_ctrl, &mumps->omp_comm, &mumps->mumps_comm, &mumps->is_omp_master));
2645: #else
2646: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "The system does not have PETSc OpenMP support but you added the -%smat_mumps_use_omp_threads option. Configure PETSc with --with-openmp --download-hwloc (or --with-hwloc) to enable it, see more in MATSOLVERMUMPS manual",
2647: ((PetscObject)F)->prefix ? ((PetscObject)F)->prefix : "");
2648: #endif
2649: } else {
2650: mumps->omp_comm = PETSC_COMM_SELF;
2651: mumps->mumps_comm = mumps->petsc_comm;
2652: mumps->is_omp_master = PETSC_TRUE;
2653: }
2654: PetscCallMPI(MPI_Comm_size(mumps->omp_comm, &mumps->omp_comm_size));
2655: mumps->reqs = NULL;
2656: mumps->tag = 0;
2658: if (mumps->mumps_comm != MPI_COMM_NULL) {
2659: if (PetscDefined(HAVE_OPENMP_SUPPORT) && mumps->use_petsc_omp_support) {
2660: /* It looks like MUMPS does not dup the input comm. Dup a new comm for MUMPS to avoid any tag mismatches. */
2661: MPI_Comm comm;
2662: PetscCallMPI(MPI_Comm_dup(mumps->mumps_comm, &comm));
2663: mumps->mumps_comm = comm;
2664: } else PetscCall(PetscCommGetComm(mumps->petsc_comm, &mumps->mumps_comm));
2665: }
2667: mumps->id.comm_fortran = MPI_Comm_c2f(mumps->mumps_comm);
2668: mumps->id.job = JOB_INIT;
2669: mumps->id.par = 1; /* host participates factorizaton and solve */
2670: mumps->id.sym = mumps->sym;
2672: size = mumps->id.size_schur;
2673: arr = mumps->id.schur;
2674: listvar_schur = mumps->id.listvar_schur;
2675: nblk = mumps->id.nblk;
2676: blkvar = mumps->id.blkvar;
2677: blkptr = mumps->id.blkptr;
2678: if (PetscDefined(USE_DEBUG)) {
2679: for (PetscInt i = 0; i < size; i++)
2680: PetscCheck(listvar_schur[i] - 1 >= 0 && listvar_schur[i] - 1 < A->rmap->N, PETSC_COMM_SELF, PETSC_ERR_USER, "Invalid Schur index at position %" PetscInt_FMT "! %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", i, (PetscInt)listvar_schur[i] - 1,
2681: A->rmap->N);
2682: }
2684: PetscCall(PetscOptionsEnum("-pc_precision", "Precision used by MUMPS", "MATSOLVERMUMPS", PetscPrecisionTypes, (PetscEnum)precision, (PetscEnum *)&precision, NULL));
2685: PetscCheck(precision == PETSC_PRECISION_SINGLE || precision == PETSC_PRECISION_DOUBLE, PetscObjectComm((PetscObject)F), PETSC_ERR_SUP, "MUMPS does not support %s precision", PetscPrecisionTypes[precision]);
2686: PetscCheck(precision == PETSC_SCALAR_PRECISION || PetscDefined(HAVE_MUMPS_MIXED_PRECISION), PetscObjectComm((PetscObject)F), PETSC_ERR_USER, "Your MUMPS library does not support mixed precision, but which is needed with your specified PetscScalar");
2687: PetscCall(MatMumpsAllocateInternalID(&mumps->id, precision));
2689: PetscMUMPS_c(mumps);
2690: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2692: /* set PETSc-MUMPS default options - override MUMPS default */
2693: mumps->id.ICNTL(3) = 0;
2694: mumps->id.ICNTL(4) = 0;
2695: if (mumps->petsc_size == 1) {
2696: mumps->id.ICNTL(18) = 0; /* centralized assembled matrix input */
2697: mumps->id.ICNTL(7) = 7; /* automatic choice of ordering done by the package */
2698: } else {
2699: mumps->id.ICNTL(18) = 3; /* distributed assembled matrix input */
2700: mumps->id.ICNTL(21) = 1; /* distributed solution */
2701: }
2702: if (nblk && blkptr) {
2703: mumps->id.ICNTL(15) = 1;
2704: mumps->id.nblk = nblk;
2705: mumps->id.blkvar = blkvar;
2706: mumps->id.blkptr = blkptr;
2707: } else mumps->id.ICNTL(15) = 0;
2709: /* restore cached ICNTL and CNTL values */
2710: for (icntl = 0; icntl < nICNTL_pre; ++icntl) mumps->id.ICNTL(mumps->ICNTL_pre[1 + 2 * icntl]) = mumps->ICNTL_pre[2 + 2 * icntl];
2711: for (icntl = 0; icntl < nCNTL_pre; ++icntl) ID_CNTL_SET(mumps->id, (PetscInt)mumps->CNTL_pre[1 + 2 * icntl], mumps->CNTL_pre[2 + 2 * icntl]);
2713: PetscCall(PetscFree(mumps->ICNTL_pre));
2714: PetscCall(PetscFree(mumps->CNTL_pre));
2716: if (schur) {
2717: mumps->id.size_schur = size;
2718: mumps->id.schur_lld = size;
2719: mumps->id.schur = arr;
2720: mumps->id.listvar_schur = listvar_schur;
2721: if (mumps->petsc_size > 1) {
2722: PetscBool gs; /* gs is false if any rank other than root has non-empty IS */
2724: mumps->id.ICNTL(19) = 1; /* MUMPS returns Schur centralized on the host */
2725: gs = mumps->myid ? (mumps->id.size_schur ? PETSC_FALSE : PETSC_TRUE) : PETSC_TRUE; /* always true on root; false on others if their size != 0 */
2726: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &gs, 1, MPI_C_BOOL, MPI_LAND, mumps->petsc_comm));
2727: PetscCheck(gs, PETSC_COMM_SELF, PETSC_ERR_SUP, "MUMPS distributed parallel Schur complements not yet supported from PETSc");
2728: } else {
2729: if (F->factortype == MAT_FACTOR_LU) {
2730: mumps->id.ICNTL(19) = 3; /* MUMPS returns full matrix */
2731: } else {
2732: mumps->id.ICNTL(19) = 2; /* MUMPS returns lower triangular part */
2733: }
2734: }
2735: mumps->id.ICNTL(26) = -1;
2736: }
2738: /* copy MUMPS default control values from master to slaves. Although slaves do not call MUMPS, they may access these values in code.
2739: For example, ICNTL(9) is initialized to 1 by MUMPS and slaves check ICNTL(9) in MatSolve_MUMPS.
2740: */
2741: PetscCallMPI(MPI_Bcast(mumps->id.icntl, 40, MPI_INT, 0, mumps->omp_comm));
2742: PetscCallMPI(MPI_Bcast(mumps->id.cntl, 15, MPIU_MUMPSREAL(&mumps->id), 0, mumps->omp_comm));
2744: mumps->scat_rhs = NULL;
2745: mumps->scat_sol = NULL;
2746: }
2747: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_1", "ICNTL(1): output stream for error messages", "None", mumps->id.ICNTL(1), &icntl, &flg));
2748: if (flg) mumps->id.ICNTL(1) = icntl;
2749: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_2", "ICNTL(2): output stream for diagnostic printing, statistics, and warning", "None", mumps->id.ICNTL(2), &icntl, &flg));
2750: if (flg) mumps->id.ICNTL(2) = icntl;
2751: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_3", "ICNTL(3): output stream for global information, collected on the host", "None", mumps->id.ICNTL(3), &icntl, &flg));
2752: if (flg) mumps->id.ICNTL(3) = icntl;
2754: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_4", "ICNTL(4): level of printing (0 to 4)", "None", mumps->id.ICNTL(4), &icntl, &flg));
2755: if (flg) mumps->id.ICNTL(4) = icntl;
2756: if (mumps->id.ICNTL(4) || PetscLogPrintInfo) mumps->id.ICNTL(3) = 6; /* resume MUMPS default id.ICNTL(3) = 6 */
2758: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_6", "ICNTL(6): permutes to a zero-free diagonal and/or scale the matrix (0 to 7)", "None", mumps->id.ICNTL(6), &icntl, &flg));
2759: if (flg) mumps->id.ICNTL(6) = icntl;
2761: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_7", "ICNTL(7): computes a symmetric permutation in sequential analysis. 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto(default)", "None", mumps->id.ICNTL(7), &icntl, &flg));
2762: if (flg) {
2763: PetscCheck(icntl != 1 && icntl >= 0 && icntl <= 7, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Valid values are 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto");
2764: mumps->id.ICNTL(7) = icntl;
2765: }
2767: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_8", "ICNTL(8): scaling strategy (-2 to 8 or 77)", "None", mumps->id.ICNTL(8), &mumps->id.ICNTL(8), NULL));
2768: /* PetscCall(PetscOptionsInt("-mat_mumps_icntl_9","ICNTL(9): computes the solution using A or A^T","None",mumps->id.ICNTL(9),&mumps->id.ICNTL(9),NULL)); handled by MatSolveTranspose_MUMPS() */
2769: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_10", "ICNTL(10): max num of refinements", "None", mumps->id.ICNTL(10), &mumps->id.ICNTL(10), NULL));
2770: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_11", "ICNTL(11): statistics related to an error analysis (via -ksp_view)", "None", mumps->id.ICNTL(11), &mumps->id.ICNTL(11), NULL));
2771: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_12", "ICNTL(12): an ordering strategy for symmetric matrices (0 to 3)", "None", mumps->id.ICNTL(12), &mumps->id.ICNTL(12), NULL));
2772: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_13", "ICNTL(13): parallelism of the root node (enable ScaLAPACK) and its splitting", "None", mumps->id.ICNTL(13), &mumps->id.ICNTL(13), NULL));
2773: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_14", "ICNTL(14): percentage increase in the estimated working space", "None", mumps->id.ICNTL(14), &mumps->id.ICNTL(14), NULL));
2774: PetscCall(MatGetBlockSizes(A, &rbs, &cbs));
2775: if (rbs == cbs && rbs > 1) mumps->id.ICNTL(15) = (PetscMUMPSInt)-rbs;
2776: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_15", "ICNTL(15): compression of the input matrix resulting from a block format", "None", mumps->id.ICNTL(15), &mumps->id.ICNTL(15), &flg));
2777: if (flg) {
2778: if (mumps->id.ICNTL(15) < 0) PetscCheck((-mumps->id.ICNTL(15) % cbs == 0) && (-mumps->id.ICNTL(15) % rbs == 0), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The opposite of -mat_mumps_icntl_15 must be a multiple of the column and row blocksizes");
2779: else if (mumps->id.ICNTL(15) > 0) {
2780: const PetscInt *bsizes;
2781: PetscInt nblocks, p, *blkptr = NULL;
2782: PetscMPIInt *recvcounts, *displs, n;
2783: PetscMPIInt rank, size = 0;
2785: PetscCall(MatGetVariableBlockSizes(A, &nblocks, &bsizes));
2786: flg = PETSC_TRUE;
2787: for (p = 0; p < nblocks; ++p) {
2788: if (bsizes[p] > 1) break;
2789: }
2790: if (p == nblocks) flg = PETSC_FALSE;
2791: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &flg, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)A)));
2792: if (flg) { // if at least one process supplies variable block sizes and they are not all set to 1
2793: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
2794: if (rank == 0) PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
2795: PetscCall(PetscCalloc2(size, &recvcounts, size + 1, &displs));
2796: PetscCall(PetscMPIIntCast(nblocks, &n));
2797: PetscCallMPI(MPI_Gather(&n, 1, MPI_INT, recvcounts, 1, MPI_INT, 0, PetscObjectComm((PetscObject)A)));
2798: for (PetscInt p = 0; p < size; ++p) displs[p + 1] = displs[p] + recvcounts[p];
2799: PetscCall(PetscMalloc1(displs[size] + 1, &blkptr));
2800: PetscCallMPI(MPI_Bcast(displs + size, 1, MPIU_INT, 0, PetscObjectComm((PetscObject)A)));
2801: PetscCallMPI(MPI_Gatherv(bsizes, n, MPIU_INT, blkptr + 1, recvcounts, displs, MPIU_INT, 0, PetscObjectComm((PetscObject)A)));
2802: if (rank == 0) {
2803: blkptr[0] = 1;
2804: for (PetscInt p = 0; p < n; ++p) blkptr[p + 1] += blkptr[p];
2805: PetscCall(MatMumpsSetBlk(F, displs[size], NULL, blkptr));
2806: }
2807: PetscCall(PetscFree2(recvcounts, displs));
2808: PetscCall(PetscFree(blkptr));
2809: }
2810: }
2811: }
2812: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_19", "ICNTL(19): computes the Schur complement", "None", mumps->id.ICNTL(19), &mumps->id.ICNTL(19), NULL));
2813: if (mumps->id.ICNTL(19) <= 0 || mumps->id.ICNTL(19) > 3) { /* reset any Schur data (if any) */
2814: PetscCall(MatMumpsResetSchur_Private(F));
2815: }
2817: /* Two MPICH Fortran MPI_IN_PLACE binding bugs prevented the use of 'mpich + mumps'. One happened with "mpi4py + mpich + mumps",
2818: and was reported by Firedrake. See https://bitbucket.org/mpi4py/mpi4py/issues/162/mpi4py-initialization-breaks-fortran
2819: and a petsc-maint mailing list thread with subject 'MUMPS segfaults in parallel because of ...'
2820: This bug was fixed by https://github.com/pmodels/mpich/pull/4149. But the fix brought a new bug,
2821: see https://github.com/pmodels/mpich/issues/5589. This bug was fixed by https://github.com/pmodels/mpich/pull/5590.
2822: In short, we could not use distributed RHS until with MPICH v4.0b1 or we enabled a workaround in mumps-5.6.2+
2823: */
2824: mumps->ICNTL20 = 10; /* Distributed dense RHS, by default */
2825: #if PETSC_PKG_MUMPS_VERSION_LT(5, 3, 0) || (PetscDefined(HAVE_MPICH) && MPICH_NUMVERSION < 40000101) || PetscDefined(HAVE_MSMPI)
2826: mumps->ICNTL20 = 0; /* Centralized dense RHS, if need be */
2827: #endif
2828: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_20", "ICNTL(20): give mumps centralized (0) or distributed (10) dense right-hand sides", "None", mumps->ICNTL20, &mumps->ICNTL20, &flg));
2829: PetscCheck(!flg || mumps->ICNTL20 == 10 || mumps->ICNTL20 == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "ICNTL(20)=%d is not supported by the PETSc/MUMPS interface. Allowed values are 0, 10", (int)mumps->ICNTL20);
2830: #if PETSC_PKG_MUMPS_VERSION_LT(5, 3, 0)
2831: PetscCheck(!flg || mumps->ICNTL20 != 10, PETSC_COMM_SELF, PETSC_ERR_SUP, "ICNTL(20)=10 is not supported before MUMPS-5.3.0");
2832: #endif
2833: /* PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_21","ICNTL(21): the distribution (centralized or distributed) of the solution vectors","None",mumps->id.ICNTL(21),&mumps->id.ICNTL(21),NULL)); we only use distributed solution vector */
2835: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_22", "ICNTL(22): in-core/out-of-core factorization and solve (0 or 1)", "None", mumps->id.ICNTL(22), &mumps->id.ICNTL(22), &flg));
2836: if (flg && mumps->id.ICNTL(22) != 1) mumps->id.ICNTL(22) = 0; // MUMPS treats values other than 1 as 0. Normalize it so we can safely use 'if (mumps->id.ICNTL(22))'
2837: if (mumps->id.ICNTL(22)) {
2838: // MUMPS will use the /tmp directory if -mat_mumps_ooc_tmpdir is not set by user.
2839: // We don't provide option -mat_mumps_ooc_prefix, as we use F's prefix as OOC_PREFIX, which is set later during MatFactorNumeric_MUMPS() to also handle cases where users enable OOC via MatMumpsSetIcntl().
2840: PetscCall(PetscOptionsString("-mat_mumps_ooc_tmpdir", "Out of core directory", "None", mumps->id.ooc_tmpdir, mumps->id.ooc_tmpdir, sizeof(((MUMPS_STRUC_C *)NULL)->ooc_tmpdir), NULL));
2841: }
2842: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_23", "ICNTL(23): max size of the working memory (MB) that can allocate per processor", "None", mumps->id.ICNTL(23), &mumps->id.ICNTL(23), NULL));
2843: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_24", "ICNTL(24): detection of null pivot rows (0 or 1)", "None", mumps->id.ICNTL(24), &mumps->id.ICNTL(24), NULL));
2844: if (mumps->id.ICNTL(24)) mumps->id.ICNTL(13) = 1; /* turn-off ScaLAPACK to help with the correct detection of null pivots */
2846: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_25", "ICNTL(25): computes a solution of a deficient matrix and a null space basis", "None", mumps->id.ICNTL(25), &mumps->id.ICNTL(25), NULL));
2847: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_26", "ICNTL(26): drives the solution phase if a Schur complement matrix", "None", mumps->id.ICNTL(26), &mumps->id.ICNTL(26), NULL));
2848: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_27", "ICNTL(27): controls the blocking size for multiple right-hand sides", "None", mumps->id.ICNTL(27), &mumps->id.ICNTL(27), NULL));
2849: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_28", "ICNTL(28): use 1 for sequential analysis and ICNTL(7) ordering, or 2 for parallel analysis and ICNTL(29) ordering", "None", mumps->id.ICNTL(28), &mumps->id.ICNTL(28), NULL));
2850: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_29", "ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis", "None", mumps->id.ICNTL(29), &mumps->id.ICNTL(29), NULL));
2851: /* PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_30","ICNTL(30): compute user-specified set of entries in inv(A)","None",mumps->id.ICNTL(30),&mumps->id.ICNTL(30),NULL)); */ /* call MatMumpsGetInverse() directly */
2852: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_31", "ICNTL(31): indicates which factors may be discarded during factorization", "None", mumps->id.ICNTL(31), &mumps->id.ICNTL(31), NULL));
2853: /* PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_32","ICNTL(32): performs the forward elimination of the right-hand sides during factorization","None",mumps->id.ICNTL(32),&mumps->id.ICNTL(32),NULL)); -- not supported by PETSc API */
2854: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_33", "ICNTL(33): compute determinant", "None", mumps->id.ICNTL(33), &mumps->id.ICNTL(33), NULL));
2855: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_35", "ICNTL(35): activates Block Low Rank (BLR) based factorization", "None", mumps->id.ICNTL(35), &mumps->id.ICNTL(35), NULL));
2856: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_36", "ICNTL(36): choice of BLR factorization variant", "None", mumps->id.ICNTL(36), &mumps->id.ICNTL(36), NULL));
2857: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_37", "ICNTL(37): compression of the contribution blocks (CB)", "None", mumps->id.ICNTL(37), &mumps->id.ICNTL(37), NULL));
2858: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_38", "ICNTL(38): estimated compression rate of LU factors with BLR", "None", mumps->id.ICNTL(38), &mumps->id.ICNTL(38), NULL));
2859: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_40", "ICNTL(40): adaptive BLR precision feature", "None", mumps->id.ICNTL(40), &mumps->id.ICNTL(40), NULL));
2860: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_47", "ICNTL(47): single precision factorization in a double precision instance", "None", mumps->id.ICNTL(47), &mumps->id.ICNTL(47), NULL));
2861: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_48", "ICNTL(48): multithreading with tree parallelism", "None", mumps->id.ICNTL(48), &mumps->id.ICNTL(48), NULL));
2862: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_49", "ICNTL(49): compact workarray at the end of factorization phase", "None", mumps->id.ICNTL(49), &mumps->id.ICNTL(49), NULL));
2863: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_56", "ICNTL(56): postponing and rank-revealing factorization", "None", mumps->id.ICNTL(56), &mumps->id.ICNTL(56), NULL));
2864: PetscCall(PetscOptionsMUMPSInt("-mat_mumps_icntl_58", "ICNTL(58): defines options for symbolic factorization", "None", mumps->id.ICNTL(58), &mumps->id.ICNTL(58), NULL));
2866: PetscCall(PetscOptionsReal("-mat_mumps_cntl_1", "CNTL(1): relative pivoting threshold", "None", (PetscReal)ID_CNTL_GET(mumps->id, 1), &cntl, &flg));
2867: if (flg) ID_CNTL_SET(mumps->id, 1, cntl);
2868: PetscCall(PetscOptionsReal("-mat_mumps_cntl_2", "CNTL(2): stopping criterion of refinement", "None", (PetscReal)ID_CNTL_GET(mumps->id, 2), &cntl, &flg));
2869: if (flg) ID_CNTL_SET(mumps->id, 2, cntl);
2870: PetscCall(PetscOptionsReal("-mat_mumps_cntl_3", "CNTL(3): absolute pivoting threshold", "None", (PetscReal)ID_CNTL_GET(mumps->id, 3), &cntl, &flg));
2871: if (flg) ID_CNTL_SET(mumps->id, 3, cntl);
2872: PetscCall(PetscOptionsReal("-mat_mumps_cntl_4", "CNTL(4): value for static pivoting", "None", (PetscReal)ID_CNTL_GET(mumps->id, 4), &cntl, &flg));
2873: if (flg) ID_CNTL_SET(mumps->id, 4, cntl);
2874: PetscCall(PetscOptionsReal("-mat_mumps_cntl_5", "CNTL(5): fixation for null pivots", "None", (PetscReal)ID_CNTL_GET(mumps->id, 5), &cntl, &flg));
2875: if (flg) ID_CNTL_SET(mumps->id, 5, cntl);
2876: PetscCall(PetscOptionsReal("-mat_mumps_cntl_7", "CNTL(7): dropping parameter used during BLR", "None", (PetscReal)ID_CNTL_GET(mumps->id, 7), &cntl, &flg));
2877: if (flg) ID_CNTL_SET(mumps->id, 7, cntl);
2879: PetscCall(PetscOptionsIntArray("-mat_mumps_view_info", "request INFO local to each processor", "", info, &ninfo, NULL));
2880: if (ninfo) {
2881: PetscCheck(ninfo <= 80, PETSC_COMM_SELF, PETSC_ERR_USER, "number of INFO %" PetscInt_FMT " must <= 80", ninfo);
2882: PetscCall(PetscMalloc1(ninfo, &mumps->info));
2883: mumps->ninfo = ninfo;
2884: for (i = 0; i < ninfo; i++) {
2885: PetscCheck(info[i] >= 0 && info[i] <= 80, PETSC_COMM_SELF, PETSC_ERR_USER, "index of INFO %" PetscInt_FMT " must between 1 and 80", ninfo);
2886: mumps->info[i] = info[i];
2887: }
2888: }
2889: PetscOptionsEnd();
2890: PetscFunctionReturn(PETSC_SUCCESS);
2891: }
2893: static PetscErrorCode MatFactorSymbolic_MUMPS_ReportIfError(Mat F, Mat A, PETSC_UNUSED const MatFactorInfo *info, Mat_MUMPS *mumps)
2894: {
2895: PetscFunctionBegin;
2896: if (mumps->id.INFOG(1) < 0) {
2897: PetscCheck(!A->erroriffailure, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in analysis: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
2898: if (mumps->id.INFOG(1) == -6) {
2899: PetscCall(PetscInfo(F, "MUMPS error in analysis: matrix is singular, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2900: F->factorerrortype = MAT_FACTOR_STRUCT_ZEROPIVOT;
2901: } else if (mumps->id.INFOG(1) == -5 || mumps->id.INFOG(1) == -7) {
2902: PetscCall(PetscInfo(F, "MUMPS error in analysis: problem with work array, INFOG(1)=%d, INFO(2)=%d\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2903: F->factorerrortype = MAT_FACTOR_OUTMEMORY;
2904: } else {
2905: PetscCall(PetscInfo(F, "MUMPS error in analysis: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS "\n", mumps->id.INFOG(1), mumps->id.INFO(2)));
2906: F->factorerrortype = MAT_FACTOR_OTHER;
2907: }
2908: }
2909: if (!mumps->id.n) F->factorerrortype = MAT_FACTOR_NOERROR;
2910: PetscFunctionReturn(PETSC_SUCCESS);
2911: }
2913: static PetscErrorCode MatLUFactorSymbolic_AIJMUMPS(Mat F, Mat A, IS r, PETSC_UNUSED IS c, const MatFactorInfo *info)
2914: {
2915: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
2916: Vec b;
2917: const PetscInt M = A->rmap->N;
2919: PetscFunctionBegin;
2920: if (mumps->matstruc == SAME_NONZERO_PATTERN) {
2921: /* F is assembled by a previous call of MatLUFactorSymbolic_AIJMUMPS() */
2922: PetscFunctionReturn(PETSC_SUCCESS);
2923: }
2925: /* Set MUMPS options from the options database */
2926: PetscCall(MatSetFromOptions_MUMPS(F, A));
2928: PetscCall((*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps));
2929: PetscCall(MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX, mumps));
2930: PetscCall(MatMumpsEnsureSchurArray_Private(F));
2932: /* analysis phase */
2933: mumps->id.job = JOB_FACTSYMBOLIC;
2934: PetscCall(PetscMUMPSIntCast(M, &mumps->id.n));
2935: switch (mumps->id.ICNTL(18)) {
2936: case 0: /* centralized assembled matrix input */
2937: if (!mumps->myid) {
2938: mumps->id.nnz = mumps->nnz;
2939: mumps->id.irn = mumps->irn;
2940: mumps->id.jcn = mumps->jcn;
2941: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_len, &mumps->id.a));
2942: if (r && mumps->id.ICNTL(7) == 7 && !F->schur) {
2943: mumps->id.ICNTL(7) = 1;
2944: if (!mumps->myid) {
2945: const PetscInt *idx;
2947: PetscCall(PetscMalloc1(M, &mumps->id.perm_in));
2948: PetscCall(ISGetIndices(r, &idx));
2949: for (PetscInt i = 0; i < M; i++) PetscCall(PetscMUMPSIntCast(idx[i] + 1, &mumps->id.perm_in[i])); /* perm_in[]: start from 1, not 0! */
2950: PetscCall(ISRestoreIndices(r, &idx));
2951: }
2952: }
2953: }
2954: break;
2955: case 3: /* distributed assembled matrix input (size>1) */
2956: mumps->id.nnz_loc = mumps->nnz;
2957: mumps->id.irn_loc = mumps->irn;
2958: mumps->id.jcn_loc = mumps->jcn;
2959: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_loc_len, &mumps->id.a_loc));
2960: if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
2961: PetscCall(MatCreateVecs(A, NULL, &b));
2962: PetscCall(VecScatterCreateToZero(b, &mumps->scat_rhs, &mumps->b_seq));
2963: PetscCall(VecDestroy(&b));
2964: }
2965: break;
2966: }
2967: if (A->rmap->N && A->cmap->N) {
2968: PetscMUMPS_c(mumps);
2969: PetscCall(MatFactorSymbolic_MUMPS_ReportIfError(F, A, info, mumps));
2970: }
2971: F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
2972: F->ops->solve = MatSolve_MUMPS;
2973: F->ops->solvetranspose = MatSolveTranspose_MUMPS;
2974: F->ops->matsolve = MatMatSolve_MUMPS;
2975: F->ops->mattransposesolve = MatMatTransposeSolve_MUMPS;
2976: F->ops->matsolvetranspose = MatMatSolveTranspose_MUMPS;
2978: mumps->matstruc = SAME_NONZERO_PATTERN;
2979: PetscFunctionReturn(PETSC_SUCCESS);
2980: }
2982: /* Note the PETSc r and c permutations are ignored */
2983: static PetscErrorCode MatLUFactorSymbolic_BAIJMUMPS(Mat F, Mat A, PETSC_UNUSED IS r, PETSC_UNUSED IS c, const MatFactorInfo *info)
2984: {
2985: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
2986: Vec b;
2987: const PetscInt M = A->rmap->N;
2989: PetscFunctionBegin;
2990: if (mumps->matstruc == SAME_NONZERO_PATTERN) {
2991: /* F is assembled by a previous call of MatLUFactorSymbolic_BAIJMUMPS() */
2992: PetscFunctionReturn(PETSC_SUCCESS);
2993: }
2995: /* Set MUMPS options from the options database */
2996: PetscCall(MatSetFromOptions_MUMPS(F, A));
2998: PetscCall((*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps));
2999: PetscCall(MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX, mumps));
3000: PetscCall(MatMumpsEnsureSchurArray_Private(F));
3002: /* analysis phase */
3003: mumps->id.job = JOB_FACTSYMBOLIC;
3004: PetscCall(PetscMUMPSIntCast(M, &mumps->id.n));
3005: switch (mumps->id.ICNTL(18)) {
3006: case 0: /* centralized assembled matrix input */
3007: if (!mumps->myid) {
3008: mumps->id.nnz = mumps->nnz;
3009: mumps->id.irn = mumps->irn;
3010: mumps->id.jcn = mumps->jcn;
3011: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_len, &mumps->id.a));
3012: }
3013: break;
3014: case 3: /* distributed assembled matrix input (size>1) */
3015: mumps->id.nnz_loc = mumps->nnz;
3016: mumps->id.irn_loc = mumps->irn;
3017: mumps->id.jcn_loc = mumps->jcn;
3018: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_loc_len, &mumps->id.a_loc));
3019: if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
3020: PetscCall(MatCreateVecs(A, NULL, &b));
3021: PetscCall(VecScatterCreateToZero(b, &mumps->scat_rhs, &mumps->b_seq));
3022: PetscCall(VecDestroy(&b));
3023: }
3024: break;
3025: }
3026: if (A->rmap->N && A->cmap->N) {
3027: PetscMUMPS_c(mumps);
3028: PetscCall(MatFactorSymbolic_MUMPS_ReportIfError(F, A, info, mumps));
3029: }
3030: F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
3031: F->ops->solve = MatSolve_MUMPS;
3032: F->ops->solvetranspose = MatSolveTranspose_MUMPS;
3033: F->ops->matsolve = MatMatSolve_MUMPS;
3034: F->ops->mattransposesolve = MatMatTransposeSolve_MUMPS;
3035: F->ops->matsolvetranspose = MatMatSolveTranspose_MUMPS;
3037: mumps->matstruc = SAME_NONZERO_PATTERN;
3038: PetscFunctionReturn(PETSC_SUCCESS);
3039: }
3041: /* Note the PETSc r permutation and factor info are ignored */
3042: static PetscErrorCode MatCholeskyFactorSymbolic_MUMPS(Mat F, Mat A, PETSC_UNUSED IS r, const MatFactorInfo *info)
3043: {
3044: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3045: Vec b;
3046: const PetscInt M = A->rmap->N;
3048: PetscFunctionBegin;
3049: if (mumps->matstruc == SAME_NONZERO_PATTERN) {
3050: /* F is assembled by a previous call of MatCholeskyFactorSymbolic_MUMPS() */
3051: PetscFunctionReturn(PETSC_SUCCESS);
3052: }
3054: /* Set MUMPS options from the options database */
3055: PetscCall(MatSetFromOptions_MUMPS(F, A));
3057: PetscCall((*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps));
3058: PetscCall(MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX, mumps));
3059: PetscCall(MatMumpsEnsureSchurArray_Private(F));
3061: /* analysis phase */
3062: mumps->id.job = JOB_FACTSYMBOLIC;
3063: PetscCall(PetscMUMPSIntCast(M, &mumps->id.n));
3064: switch (mumps->id.ICNTL(18)) {
3065: case 0: /* centralized assembled matrix input */
3066: if (!mumps->myid) {
3067: mumps->id.nnz = mumps->nnz;
3068: mumps->id.irn = mumps->irn;
3069: mumps->id.jcn = mumps->jcn;
3070: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_len, &mumps->id.a));
3071: }
3072: break;
3073: case 3: /* distributed assembled matrix input (size>1) */
3074: mumps->id.nnz_loc = mumps->nnz;
3075: mumps->id.irn_loc = mumps->irn;
3076: mumps->id.jcn_loc = mumps->jcn;
3077: if (1 < mumps->id.ICNTL(6) && mumps->id.ICNTL(6) < 7) PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, mumps->nnz, mumps->val, mumps->id.precision, &mumps->id.a_loc_len, &mumps->id.a_loc));
3078: if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
3079: PetscCall(MatCreateVecs(A, NULL, &b));
3080: PetscCall(VecScatterCreateToZero(b, &mumps->scat_rhs, &mumps->b_seq));
3081: PetscCall(VecDestroy(&b));
3082: }
3083: break;
3084: }
3085: if (A->rmap->N && A->cmap->N) {
3086: PetscMUMPS_c(mumps);
3087: PetscCall(MatFactorSymbolic_MUMPS_ReportIfError(F, A, info, mumps));
3088: }
3089: F->ops->choleskyfactornumeric = MatFactorNumeric_MUMPS;
3090: F->ops->solve = MatSolve_MUMPS;
3091: F->ops->solvetranspose = MatSolve_MUMPS;
3092: F->ops->matsolve = MatMatSolve_MUMPS;
3093: F->ops->mattransposesolve = MatMatTransposeSolve_MUMPS;
3094: F->ops->matsolvetranspose = MatMatSolveTranspose_MUMPS;
3095: #if PetscDefined(USE_COMPLEX)
3096: F->ops->getinertia = NULL;
3097: #else
3098: F->ops->getinertia = MatGetInertia_SBAIJMUMPS;
3099: #endif
3101: mumps->matstruc = SAME_NONZERO_PATTERN;
3102: PetscFunctionReturn(PETSC_SUCCESS);
3103: }
3105: static PetscErrorCode MatView_MUMPS(Mat A, PetscViewer viewer)
3106: {
3107: PetscBool isascii;
3108: PetscViewerFormat format;
3109: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
3111: PetscFunctionBegin;
3112: /* check if matrix is mumps type */
3113: if (A->ops->solve != MatSolve_MUMPS) PetscFunctionReturn(PETSC_SUCCESS);
3115: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
3116: if (isascii) {
3117: PetscCall(PetscViewerGetFormat(viewer, &format));
3118: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
3119: PetscCall(PetscViewerASCIIPrintf(viewer, "MUMPS run parameters:\n"));
3120: PetscCall(PetscViewerASCIIPrintf(viewer, " precision: %s\n", PetscPrecisionTypes[mumps->id.precision]));
3121: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
3122: PetscCall(PetscViewerASCIIPrintf(viewer, " SYM (matrix type): %d\n", mumps->id.sym));
3123: PetscCall(PetscViewerASCIIPrintf(viewer, " PAR (host participation): %d\n", mumps->id.par));
3124: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(1) (output for error): %d\n", mumps->id.ICNTL(1)));
3125: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(2) (output of diagnostic msg): %d\n", mumps->id.ICNTL(2)));
3126: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(3) (output for global info): %d\n", mumps->id.ICNTL(3)));
3127: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(4) (level of printing): %d\n", mumps->id.ICNTL(4)));
3128: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(5) (input mat struct): %d\n", mumps->id.ICNTL(5)));
3129: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(6) (matrix prescaling): %d\n", mumps->id.ICNTL(6)));
3130: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(7) (sequential matrix ordering):%d\n", mumps->id.ICNTL(7)));
3131: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(8) (scaling strategy): %d\n", mumps->id.ICNTL(8)));
3132: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(10) (max num of refinements): %d\n", mumps->id.ICNTL(10)));
3133: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(11) (error analysis): %d\n", mumps->id.ICNTL(11)));
3134: if (mumps->id.ICNTL(11) > 0) {
3135: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(4) (inf norm of input mat): %g\n", (double)ID_RINFOG_GET(mumps->id, 4)));
3136: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(5) (inf norm of solution): %g\n", (double)ID_RINFOG_GET(mumps->id, 5)));
3137: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(6) (inf norm of residual): %g\n", (double)ID_RINFOG_GET(mumps->id, 6)));
3138: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(7),RINFOG(8) (backward error est): %g, %g\n", (double)ID_RINFOG_GET(mumps->id, 7), (double)ID_RINFOG_GET(mumps->id, 8)));
3139: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(9) (error estimate): %g\n", (double)ID_RINFOG_GET(mumps->id, 9)));
3140: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(10),RINFOG(11)(condition numbers): %g, %g\n", (double)ID_RINFOG_GET(mumps->id, 10), (double)ID_RINFOG_GET(mumps->id, 11)));
3141: }
3142: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(12) (efficiency control): %d\n", mumps->id.ICNTL(12)));
3143: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(13) (sequential factorization of the root node): %d\n", mumps->id.ICNTL(13)));
3144: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(14) (percentage of estimated workspace increase): %d\n", mumps->id.ICNTL(14)));
3145: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(15) (compression of the input matrix): %d\n", mumps->id.ICNTL(15)));
3146: /* ICNTL(15-17) not used */
3147: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(18) (input mat struct): %d\n", mumps->id.ICNTL(18)));
3148: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(19) (Schur complement info): %d\n", mumps->id.ICNTL(19)));
3149: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(20) (RHS sparse pattern): %d\n", mumps->id.ICNTL(20)));
3150: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(21) (solution struct): %d\n", mumps->id.ICNTL(21)));
3151: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(22) (in-core/out-of-core facility): %d\n", mumps->id.ICNTL(22)));
3152: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(23) (max size of memory allocated locally): %d\n", mumps->id.ICNTL(23)));
3154: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(24) (detection of null pivot rows): %d\n", mumps->id.ICNTL(24)));
3155: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(25) (computation of a null space basis): %d\n", mumps->id.ICNTL(25)));
3156: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(26) (Schur options for RHS or solution): %d\n", mumps->id.ICNTL(26)));
3157: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(27) (blocking size for multiple RHS): %d\n", mumps->id.ICNTL(27)));
3158: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(28) (use parallel or sequential ordering): %d\n", mumps->id.ICNTL(28)));
3159: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(29) (parallel ordering): %d\n", mumps->id.ICNTL(29)));
3161: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(30) (user-specified set of entries in inv(A)): %d\n", mumps->id.ICNTL(30)));
3162: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(31) (factors is discarded in the solve phase): %d\n", mumps->id.ICNTL(31)));
3163: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(33) (compute determinant): %d\n", mumps->id.ICNTL(33)));
3164: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(35) (activate BLR based factorization): %d\n", mumps->id.ICNTL(35)));
3165: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(36) (choice of BLR factorization variant): %d\n", mumps->id.ICNTL(36)));
3166: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(37) (compression of the contribution blocks): %d\n", mumps->id.ICNTL(37)));
3167: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(38) (estimated compression rate of LU factors): %d\n", mumps->id.ICNTL(38)));
3168: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(40) (adaptive BLR precision feature): %d\n", mumps->id.ICNTL(40)));
3169: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(47) (single precision factorization in a double precision instance): %d\n", mumps->id.ICNTL(47)));
3170: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(48) (multithreading with tree parallelism): %d\n", mumps->id.ICNTL(48)));
3171: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(49) (compact workarray at the end of factorization phase):%d\n", mumps->id.ICNTL(49)));
3172: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(56) (postponing and rank-revealing factorization):%d\n", mumps->id.ICNTL(56)));
3173: PetscCall(PetscViewerASCIIPrintf(viewer, " ICNTL(58) (options for symbolic factorization): %d\n", mumps->id.ICNTL(58)));
3175: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(1) (relative pivoting threshold): %g\n", (double)ID_CNTL_GET(mumps->id, 1)));
3176: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(2) (stopping criterion of refinement): %g\n", (double)ID_CNTL_GET(mumps->id, 2)));
3177: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(3) (absolute pivoting threshold): %g\n", (double)ID_CNTL_GET(mumps->id, 3)));
3178: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(4) (value of static pivoting): %g\n", (double)ID_CNTL_GET(mumps->id, 4)));
3179: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(5) (fixation for null pivots): %g\n", (double)ID_CNTL_GET(mumps->id, 5)));
3180: PetscCall(PetscViewerASCIIPrintf(viewer, " CNTL(7) (dropping parameter for BLR): %g\n", (double)ID_CNTL_GET(mumps->id, 7)));
3182: /* information local to each processor */
3183: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFO(1) (local estimated flops for the elimination after analysis):\n"));
3184: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
3185: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %g\n", mumps->myid, (double)ID_RINFO_GET(mumps->id, 1)));
3186: PetscCall(PetscViewerFlush(viewer));
3187: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFO(2) (local estimated flops for the assembly after factorization):\n"));
3188: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %g\n", mumps->myid, (double)ID_RINFO_GET(mumps->id, 2)));
3189: PetscCall(PetscViewerFlush(viewer));
3190: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFO(3) (local estimated flops for the elimination after factorization):\n"));
3191: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %g\n", mumps->myid, (double)ID_RINFO_GET(mumps->id, 3)));
3192: PetscCall(PetscViewerFlush(viewer));
3194: PetscCall(PetscViewerASCIIPrintf(viewer, " INFO(15) (estimated size of (in MB) MUMPS internal data for running numerical factorization):\n"));
3195: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %d\n", mumps->myid, mumps->id.INFO(15)));
3196: PetscCall(PetscViewerFlush(viewer));
3198: PetscCall(PetscViewerASCIIPrintf(viewer, " INFO(16) (size of (in MB) MUMPS internal data used during numerical factorization):\n"));
3199: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %d\n", mumps->myid, mumps->id.INFO(16)));
3200: PetscCall(PetscViewerFlush(viewer));
3202: PetscCall(PetscViewerASCIIPrintf(viewer, " INFO(23) (num of pivots eliminated on this processor after factorization):\n"));
3203: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %d\n", mumps->myid, mumps->id.INFO(23)));
3204: PetscCall(PetscViewerFlush(viewer));
3206: if (mumps->ninfo && mumps->ninfo <= 80) {
3207: for (PetscInt i = 0; i < mumps->ninfo; i++) {
3208: PetscCall(PetscViewerASCIIPrintf(viewer, " INFO(%" PetscInt_FMT "):\n", mumps->info[i]));
3209: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] %d\n", mumps->myid, mumps->id.INFO(mumps->info[i])));
3210: PetscCall(PetscViewerFlush(viewer));
3211: }
3212: }
3213: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
3214: } else PetscCall(PetscViewerASCIIPrintf(viewer, " Use -%sksp_view ::ascii_info_detail to display information for all processes\n", ((PetscObject)A)->prefix ? ((PetscObject)A)->prefix : ""));
3216: if (mumps->myid == 0) { /* information from the host */
3217: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(1) (global estimated flops for the elimination after analysis): %g\n", (double)ID_RINFOG_GET(mumps->id, 1)));
3218: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(2) (global estimated flops for the assembly after factorization): %g\n", (double)ID_RINFOG_GET(mumps->id, 2)));
3219: PetscCall(PetscViewerASCIIPrintf(viewer, " RINFOG(3) (global estimated flops for the elimination after factorization): %g\n", (double)ID_RINFOG_GET(mumps->id, 3)));
3220: PetscCall(PetscViewerASCIIPrintf(viewer, " (RINFOG(12) RINFOG(13))*2^INFOG(34) (determinant): (%g,%g)*(2^%d)\n", (double)ID_RINFOG_GET(mumps->id, 12), (double)ID_RINFOG_GET(mumps->id, 13), mumps->id.INFOG(34)));
3222: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(3) (estimated real workspace for factors on all processors after analysis): %d\n", mumps->id.INFOG(3)));
3223: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(4) (estimated integer workspace for factors on all processors after analysis): %d\n", mumps->id.INFOG(4)));
3224: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(5) (estimated maximum front size in the complete tree): %d\n", mumps->id.INFOG(5)));
3225: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(6) (number of nodes in the complete tree): %d\n", mumps->id.INFOG(6)));
3226: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(7) (ordering option effectively used after analysis): %d\n", mumps->id.INFOG(7)));
3227: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(8) (structural symmetry in percent of the permuted matrix after analysis): %d\n", mumps->id.INFOG(8)));
3228: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(9) (total real/complex workspace to store the matrix factors after factorization): %d\n", mumps->id.INFOG(9)));
3229: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(10) (total integer space store the matrix factors after factorization): %d\n", mumps->id.INFOG(10)));
3230: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(11) (order of largest frontal matrix after factorization): %d\n", mumps->id.INFOG(11)));
3231: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(12) (number of off-diagonal pivots): %d\n", mumps->id.INFOG(12)));
3232: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(13) (number of delayed pivots after factorization): %d\n", mumps->id.INFOG(13)));
3233: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(14) (number of memory compress after factorization): %d\n", mumps->id.INFOG(14)));
3234: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(15) (number of steps of iterative refinement after solution): %d\n", mumps->id.INFOG(15)));
3235: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(16) (estimated size (in MB) of all MUMPS internal data for factorization after analysis: value on the most memory consuming processor): %d\n", mumps->id.INFOG(16)));
3236: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(17) (estimated size of all MUMPS internal data for factorization after analysis: sum over all processors): %d\n", mumps->id.INFOG(17)));
3237: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(18) (size of all MUMPS internal data allocated during factorization: value on the most memory consuming processor): %d\n", mumps->id.INFOG(18)));
3238: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(19) (size of all MUMPS internal data allocated during factorization: sum over all processors): %d\n", mumps->id.INFOG(19)));
3239: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(20) (estimated number of entries in the factors): %d\n", mumps->id.INFOG(20)));
3240: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(21) (size in MB of memory effectively used during factorization - value on the most memory consuming processor): %d\n", mumps->id.INFOG(21)));
3241: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(22) (size in MB of memory effectively used during factorization - sum over all processors): %d\n", mumps->id.INFOG(22)));
3242: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(23) (after analysis: value of ICNTL(6) effectively used): %d\n", mumps->id.INFOG(23)));
3243: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(24) (after analysis: value of ICNTL(12) effectively used): %d\n", mumps->id.INFOG(24)));
3244: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(25) (after factorization: number of pivots modified by static pivoting): %d\n", mumps->id.INFOG(25)));
3245: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(28) (after factorization: number of null pivots encountered): %d\n", mumps->id.INFOG(28)));
3246: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(29) (after factorization: effective number of entries in the factors (sum over all processors)): %d\n", mumps->id.INFOG(29)));
3247: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(30, 31) (after solution: size in Mbytes of memory used during solution phase): %d, %d\n", mumps->id.INFOG(30), mumps->id.INFOG(31)));
3248: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(32) (after analysis: type of analysis done): %d\n", mumps->id.INFOG(32)));
3249: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(33) (value used for ICNTL(8)): %d\n", mumps->id.INFOG(33)));
3250: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(34) (exponent of the determinant if determinant is requested): %d\n", mumps->id.INFOG(34)));
3251: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(35) (after factorization: number of entries taking into account BLR factor compression - sum over all processors): %d\n", mumps->id.INFOG(35)));
3252: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(36) (after analysis: estimated size of all MUMPS internal data for running BLR in-core - value on the most memory consuming processor): %d\n", mumps->id.INFOG(36)));
3253: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(37) (after analysis: estimated size of all MUMPS internal data for running BLR in-core - sum over all processors): %d\n", mumps->id.INFOG(37)));
3254: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(38) (after analysis: estimated size of all MUMPS internal data for running BLR out-of-core - value on the most memory consuming processor): %d\n", mumps->id.INFOG(38)));
3255: PetscCall(PetscViewerASCIIPrintf(viewer, " INFOG(39) (after analysis: estimated size of all MUMPS internal data for running BLR out-of-core - sum over all processors): %d\n", mumps->id.INFOG(39)));
3256: }
3257: }
3258: }
3259: PetscFunctionReturn(PETSC_SUCCESS);
3260: }
3262: static PetscErrorCode MatGetInfo_MUMPS(Mat A, PETSC_UNUSED MatInfoType flag, MatInfo *info)
3263: {
3264: Mat_MUMPS *mumps = (Mat_MUMPS *)A->data;
3266: PetscFunctionBegin;
3267: info->block_size = 1.0;
3268: info->nz_allocated = mumps->id.INFOG(20) >= 0 ? mumps->id.INFOG(20) : -1000000 * mumps->id.INFOG(20);
3269: info->nz_used = mumps->id.INFOG(20) >= 0 ? mumps->id.INFOG(20) : -1000000 * mumps->id.INFOG(20);
3270: info->nz_unneeded = 0.0;
3271: info->assemblies = 0.0;
3272: info->mallocs = 0.0;
3273: info->memory = 0.0;
3274: info->fill_ratio_given = 0;
3275: info->fill_ratio_needed = 0;
3276: info->factor_mallocs = 0;
3277: PetscFunctionReturn(PETSC_SUCCESS);
3278: }
3280: static PetscErrorCode MatFactorSetSchurIS_MUMPS(Mat F, IS is)
3281: {
3282: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3283: const PetscInt *idxs;
3284: PetscInt size, i;
3286: PetscFunctionBegin;
3287: PetscCall(MatMumpsResetSchur_Private(F));
3288: if (!is) PetscFunctionReturn(PETSC_SUCCESS);
3289: /* Schur complement matrix */
3290: PetscCall(ISGetLocalSize(is, &size));
3291: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, size, size, NULL, &F->schur));
3292: // don't allocate mumps->id.schur[] now as its precision is yet to be known
3293: PetscCall(PetscMUMPSIntCast(size, &mumps->id.size_schur));
3294: PetscCall(PetscMUMPSIntCast(size, &mumps->id.schur_lld));
3295: if (mumps->sym == 1) PetscCall(MatSetOption(F->schur, MAT_SPD, PETSC_TRUE));
3297: /* MUMPS expects Fortran style indices */
3298: PetscCall(PetscMalloc1(size, &mumps->id.listvar_schur));
3299: PetscCall(ISGetIndices(is, &idxs));
3300: for (i = 0; i < size; i++) PetscCall(PetscMUMPSIntCast(idxs[i] + 1, &mumps->id.listvar_schur[i]));
3301: PetscCall(ISRestoreIndices(is, &idxs));
3302: /* set a special value of ICNTL (not handled my MUMPS) to be used in the solve phase by PETSc */
3303: if (mumps->id.icntl) mumps->id.ICNTL(26) = -1;
3304: else mumps->ICNTL26 = -1;
3305: PetscFunctionReturn(PETSC_SUCCESS);
3306: }
3308: static PetscErrorCode MatFactorCreateSchurComplement_MUMPS(Mat F, Mat *S)
3309: {
3310: Mat St;
3311: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3312: PetscScalar *array;
3313: PetscInt i, j, N = mumps->id.size_schur;
3315: PetscFunctionBegin;
3316: PetscCheck(mumps->id.ICNTL(19), PetscObjectComm((PetscObject)F), PETSC_ERR_ORDER, "Schur complement mode not selected! Call MatFactorSetSchurIS() to enable it");
3317: PetscCall(MatCreate(PETSC_COMM_SELF, &St));
3318: PetscCall(MatSetSizes(St, PETSC_DECIDE, PETSC_DECIDE, mumps->id.size_schur, mumps->id.size_schur));
3319: PetscCall(MatSetType(St, MATDENSE));
3320: PetscCall(MatSetUp(St));
3321: PetscCall(MatDenseGetArray(St, &array));
3322: PetscCall(MatMumpsEnsureSchurArray_Private(F));
3323: if (!mumps->sym) { /* MUMPS always returns a full matrix */
3324: if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
3325: for (i = 0; i < N; i++) {
3326: for (j = 0; j < N; j++) array[j * N + i] = ID_FIELD_GET(mumps->id, schur, i * N + j);
3327: }
3328: } else { /* stored by columns */
3329: PetscCall(MatMumpsCastMumpsScalarArray(N * N, mumps->id.precision, mumps->id.schur, array));
3330: }
3331: } else { /* either full or lower-triangular (not packed) */
3332: if (mumps->id.ICNTL(19) == 2) { /* lower triangular stored by columns */
3333: for (i = 0; i < N; i++) {
3334: for (j = i; j < N; j++) array[i * N + j] = array[j * N + i] = ID_FIELD_GET(mumps->id, schur, i * N + j);
3335: }
3336: } else if (mumps->id.ICNTL(19) == 3) { /* full matrix */
3337: PetscCall(MatMumpsCastMumpsScalarArray(N * N, mumps->id.precision, mumps->id.schur, array));
3338: } else { /* ICNTL(19) == 1 lower triangular stored by rows */
3339: for (i = 0; i < N; i++) {
3340: for (j = 0; j < i + 1; j++) array[i * N + j] = array[j * N + i] = ID_FIELD_GET(mumps->id, schur, i * N + j);
3341: }
3342: }
3343: }
3344: PetscCall(MatDenseRestoreArray(St, &array));
3345: *S = St;
3346: PetscFunctionReturn(PETSC_SUCCESS);
3347: }
3349: static PetscErrorCode MatMumpsSetIcntl_MUMPS(Mat F, PetscInt icntl, PetscInt ival)
3350: {
3351: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3353: PetscFunctionBegin;
3354: PetscCheck((icntl >= 1 && icntl <= 38) || icntl == 40 || icntl == 47 || icntl == 48 || icntl == 49 || icntl == 56 || icntl == 58, PetscObjectComm((PetscObject)F), PETSC_ERR_ARG_WRONG, "Unsupported ICNTL value %" PetscInt_FMT, icntl);
3355: if (mumps->id.job == JOB_NULL) { /* need to cache icntl and ival since PetscMUMPS_c() has never been called */
3356: PetscMUMPSInt i, nICNTL_pre = mumps->ICNTL_pre ? mumps->ICNTL_pre[0] : 0; /* number of already cached ICNTL */
3357: for (i = 0; i < nICNTL_pre; ++i)
3358: if (mumps->ICNTL_pre[1 + 2 * i] == icntl) break; /* is this ICNTL already cached? */
3359: if (i == nICNTL_pre) { /* not already cached */
3360: if (i > 0) PetscCall(PetscRealloc(sizeof(PetscMUMPSInt) * (2 * nICNTL_pre + 3), &mumps->ICNTL_pre));
3361: else PetscCall(PetscCalloc(sizeof(PetscMUMPSInt) * 3, &mumps->ICNTL_pre));
3362: mumps->ICNTL_pre[0]++;
3363: }
3364: mumps->ICNTL_pre[1 + 2 * i] = (PetscMUMPSInt)icntl;
3365: PetscCall(PetscMUMPSIntCast(ival, mumps->ICNTL_pre + 2 + 2 * i));
3366: } else PetscCall(PetscMUMPSIntCast(ival, &mumps->id.ICNTL(icntl)));
3367: PetscFunctionReturn(PETSC_SUCCESS);
3368: }
3370: static PetscErrorCode MatMumpsGetIcntl_MUMPS(Mat F, PetscInt icntl, PetscInt *ival)
3371: {
3372: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3374: PetscFunctionBegin;
3375: PetscCheck((icntl >= 1 && icntl <= 38) || icntl == 40 || icntl == 47 || icntl == 48 || icntl == 49 || icntl == 56 || icntl == 58, PetscObjectComm((PetscObject)F), PETSC_ERR_ARG_WRONG, "Unsupported ICNTL value %" PetscInt_FMT, icntl);
3376: if (mumps->id.job == JOB_NULL) {
3377: PetscInt i, nICNTL_pre = mumps->ICNTL_pre ? mumps->ICNTL_pre[0] : 0;
3378: *ival = 0;
3379: for (i = 0; i < nICNTL_pre; ++i) {
3380: if (mumps->ICNTL_pre[1 + 2 * i] == icntl) *ival = mumps->ICNTL_pre[2 + 2 * i];
3381: }
3382: } else *ival = mumps->id.ICNTL(icntl);
3383: PetscFunctionReturn(PETSC_SUCCESS);
3384: }
3386: static PetscErrorCode MatMumpsSetCntl_MUMPS(Mat F, PetscInt icntl, PetscReal val)
3387: {
3388: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3390: PetscFunctionBegin;
3391: PetscCheck(icntl >= 1 && icntl <= 7, PetscObjectComm((PetscObject)F), PETSC_ERR_ARG_WRONG, "Unsupported CNTL value %" PetscInt_FMT, icntl);
3392: if (mumps->id.job == JOB_NULL) {
3393: PetscInt i, nCNTL_pre = mumps->CNTL_pre ? mumps->CNTL_pre[0] : 0;
3394: for (i = 0; i < nCNTL_pre; ++i)
3395: if (mumps->CNTL_pre[1 + 2 * i] == icntl) break;
3396: if (i == nCNTL_pre) {
3397: if (i > 0) PetscCall(PetscRealloc(sizeof(PetscReal) * (2 * nCNTL_pre + 3), &mumps->CNTL_pre));
3398: else PetscCall(PetscCalloc(sizeof(PetscReal) * 3, &mumps->CNTL_pre));
3399: mumps->CNTL_pre[0]++;
3400: }
3401: mumps->CNTL_pre[1 + 2 * i] = icntl;
3402: mumps->CNTL_pre[2 + 2 * i] = val;
3403: } else ID_CNTL_SET(mumps->id, icntl, val);
3404: PetscFunctionReturn(PETSC_SUCCESS);
3405: }
3407: static PetscErrorCode MatMumpsGetCntl_MUMPS(Mat F, PetscInt icntl, PetscReal *val)
3408: {
3409: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3411: PetscFunctionBegin;
3412: PetscCheck(icntl >= 1 && icntl <= 7, PetscObjectComm((PetscObject)F), PETSC_ERR_ARG_WRONG, "Unsupported CNTL value %" PetscInt_FMT, icntl);
3413: if (mumps->id.job == JOB_NULL) {
3414: PetscInt i, nCNTL_pre = mumps->CNTL_pre ? mumps->CNTL_pre[0] : 0;
3415: *val = 0.0;
3416: for (i = 0; i < nCNTL_pre; ++i) {
3417: if (mumps->CNTL_pre[1 + 2 * i] == icntl) *val = mumps->CNTL_pre[2 + 2 * i];
3418: }
3419: } else *val = ID_CNTL_GET(mumps->id, icntl);
3420: PetscFunctionReturn(PETSC_SUCCESS);
3421: }
3423: /*@C
3424: MatMumpsSetOocTmpDir - Set MUMPS out-of-core `OOC_TMPDIR` <https://mumps-solver.org/index.php?page=doc>
3426: Logically Collective
3428: Input Parameters:
3429: + F - the factored matrix obtained by calling `MatGetFactor()` with a `MatSolverType` of `MATSOLVERMUMPS` and a `MatFactorType` of `MAT_FACTOR_LU` or `MAT_FACTOR_CHOLESKY`.
3430: - tmpdir - temporary directory for out-of-core facility.
3432: Level: beginner
3434: Note:
3435: To make it effective, this routine must be called before the numeric factorization, i.e., `PCSetUp()`.
3436: If `ooc_tmpdir` is not set, MUMPS will also check the environment variable `MUMPS_OOC_TMPDIR`. But if neither was defined, it will use /tmp by default.
3438: .seealso: [](ch_matrices), `Mat`, `MatGetFactor()`, `MatMumpsGetOocTmpDir`, `MatMumpsSetIcntl()`, `MatMumpsGetIcntl()`, `MatMumpsSetCntl()`, `MatMumpsGetInfo()`, `MatMumpsGetInfog()`, `MatMumpsGetRinfo()`, `MatMumpsGetRinfog()`
3439: @*/
3440: PetscErrorCode MatMumpsSetOocTmpDir(Mat F, const char *tmpdir)
3441: {
3442: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3444: PetscFunctionBegin;
3447: PetscCall(PetscStrncpy(mumps->id.ooc_tmpdir, tmpdir, sizeof(((MUMPS_STRUC_C *)NULL)->ooc_tmpdir)));
3448: PetscFunctionReturn(PETSC_SUCCESS);
3449: }
3451: /*@C
3452: MatMumpsGetOocTmpDir - Get MUMPS out-of-core `OOC_TMPDIR` <https://mumps-solver.org/index.php?page=doc>
3454: Logically Collective
3456: Input Parameter:
3457: . F - the factored matrix obtained by calling `MatGetFactor()` with a `MatSolverType` of `MATSOLVERMUMPS` and a `MatFactorType` of `MAT_FACTOR_LU` or `MAT_FACTOR_CHOLESKY`.
3459: Output Parameter:
3460: . tmpdir - temporary directory for out-of-core facility.
3462: Level: beginner
3464: Note:
3465: The returned string is read-only and user should not try to change it.
3467: .seealso: [](ch_matrices), `Mat`, `MatGetFactor()`, `MatMumpsSetOocTmpDir`, `MatMumpsSetIcntl()`, `MatMumpsGetIcntl()`, `MatMumpsSetCntl()`, `MatMumpsGetInfo()`, `MatMumpsGetInfog()`, `MatMumpsGetRinfo()`, `MatMumpsGetRinfog()`
3468: @*/
3469: PetscErrorCode MatMumpsGetOocTmpDir(Mat F, const char *tmpdir[])
3470: {
3471: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3473: PetscFunctionBegin;
3476: if (tmpdir) *tmpdir = mumps->id.ooc_tmpdir;
3477: PetscFunctionReturn(PETSC_SUCCESS);
3478: }
3480: static PetscErrorCode MatMumpsGetInfo_MUMPS(Mat F, PetscInt icntl, PetscInt *info)
3481: {
3482: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3484: PetscFunctionBegin;
3485: *info = mumps->id.INFO(icntl);
3486: PetscFunctionReturn(PETSC_SUCCESS);
3487: }
3489: static PetscErrorCode MatMumpsGetInfog_MUMPS(Mat F, PetscInt icntl, PetscInt *infog)
3490: {
3491: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3493: PetscFunctionBegin;
3494: *infog = mumps->id.INFOG(icntl);
3495: PetscFunctionReturn(PETSC_SUCCESS);
3496: }
3498: static PetscErrorCode MatMumpsGetRinfo_MUMPS(Mat F, PetscInt icntl, PetscReal *rinfo)
3499: {
3500: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3502: PetscFunctionBegin;
3503: *rinfo = ID_RINFO_GET(mumps->id, icntl);
3504: PetscFunctionReturn(PETSC_SUCCESS);
3505: }
3507: static PetscErrorCode MatMumpsGetRinfog_MUMPS(Mat F, PetscInt icntl, PetscReal *rinfog)
3508: {
3509: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3511: PetscFunctionBegin;
3512: *rinfog = ID_RINFOG_GET(mumps->id, icntl);
3513: PetscFunctionReturn(PETSC_SUCCESS);
3514: }
3516: static PetscErrorCode MatMumpsGetNullPivots_MUMPS(Mat F, PetscInt *size, PetscInt **array)
3517: {
3518: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3520: PetscFunctionBegin;
3521: PetscCheck(mumps->id.ICNTL(24) == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "-mat_mumps_icntl_24 must be set as 1 for null pivot row detection");
3522: *size = 0;
3523: *array = NULL;
3524: if (!mumps->myid) {
3525: *size = mumps->id.INFOG(28);
3526: PetscCall(PetscMalloc1(*size, array));
3527: for (int i = 0; i < *size; i++) (*array)[i] = mumps->id.pivnul_list[i] - 1;
3528: }
3529: PetscFunctionReturn(PETSC_SUCCESS);
3530: }
3532: static PetscErrorCode MatMumpsGetInverse_MUMPS(Mat F, Mat spRHS)
3533: {
3534: Mat Bt = NULL, Btseq = NULL;
3535: PetscBool flg;
3536: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3537: PetscScalar *aa;
3538: PetscInt spnr, *ia, *ja, M, nrhs;
3540: PetscFunctionBegin;
3541: PetscAssertPointer(spRHS, 2);
3542: PetscCall(PetscObjectTypeCompare((PetscObject)spRHS, MATTRANSPOSEVIRTUAL, &flg));
3543: PetscCheck(flg, PetscObjectComm((PetscObject)spRHS), PETSC_ERR_ARG_WRONG, "Matrix spRHS must be type MATTRANSPOSEVIRTUAL matrix");
3544: PetscCall(MatShellGetScalingShifts(spRHS, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
3545: PetscCall(MatTransposeGetMat(spRHS, &Bt));
3547: PetscCall(MatMumpsSetIcntl(F, 30, 1));
3549: if (mumps->petsc_size > 1) {
3550: Mat_MPIAIJ *b = (Mat_MPIAIJ *)Bt->data;
3551: Btseq = b->A;
3552: } else {
3553: Btseq = Bt;
3554: }
3556: PetscCall(MatGetSize(spRHS, &M, &nrhs));
3557: mumps->id.nrhs = (PetscMUMPSInt)nrhs;
3558: PetscCall(PetscMUMPSIntCast(M, &mumps->id.lrhs));
3559: mumps->id.rhs = NULL;
3561: if (!mumps->myid) {
3562: PetscCall(MatSeqAIJGetArray(Btseq, &aa));
3563: PetscCall(MatGetRowIJ(Btseq, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
3564: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot get IJ structure");
3565: PetscCall(PetscMUMPSIntCSRCast(mumps, spnr, ia, ja, &mumps->id.irhs_ptr, &mumps->id.irhs_sparse, &mumps->id.nz_rhs));
3566: PetscCall(MatMumpsMakeMumpsScalarArray(PETSC_TRUE, ((Mat_SeqAIJ *)Btseq->data)->nz, aa, mumps->id.precision, &mumps->id.rhs_sparse_len, &mumps->id.rhs_sparse));
3567: } else {
3568: mumps->id.irhs_ptr = NULL;
3569: mumps->id.irhs_sparse = NULL;
3570: mumps->id.nz_rhs = 0;
3571: if (mumps->id.rhs_sparse_len) {
3572: PetscCall(PetscFree(mumps->id.rhs_sparse));
3573: mumps->id.rhs_sparse_len = 0;
3574: }
3575: }
3576: mumps->id.ICNTL(20) = 1; /* rhs is sparse */
3577: mumps->id.ICNTL(21) = 0; /* solution is in assembled centralized format */
3579: /* solve phase */
3580: mumps->id.job = JOB_SOLVE;
3581: PetscMUMPS_c(mumps);
3582: PetscCheck(mumps->id.INFOG(1) >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "MUMPS error in solve: INFOG(1)=%d, INFO(2)=%d " MUMPS_MANUALS, mumps->id.INFOG(1), mumps->id.INFO(2));
3584: if (!mumps->myid) {
3585: PetscCall(MatSeqAIJRestoreArray(Btseq, &aa));
3586: PetscCall(MatRestoreRowIJ(Btseq, 1, PETSC_FALSE, PETSC_FALSE, &spnr, (const PetscInt **)&ia, (const PetscInt **)&ja, &flg));
3587: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot restore IJ structure");
3588: }
3589: PetscFunctionReturn(PETSC_SUCCESS);
3590: }
3592: static PetscErrorCode MatMumpsGetInverseTranspose_MUMPS(Mat F, Mat spRHST)
3593: {
3594: Mat spRHS;
3596: PetscFunctionBegin;
3597: PetscCall(MatCreateTranspose(spRHST, &spRHS));
3598: PetscCall(MatMumpsGetInverse_MUMPS(F, spRHS));
3599: PetscCall(MatDestroy(&spRHS));
3600: PetscFunctionReturn(PETSC_SUCCESS);
3601: }
3603: static PetscErrorCode MatMumpsSetBlk_MUMPS(Mat F, PetscInt nblk, const PetscInt blkvar[], const PetscInt blkptr[])
3604: {
3605: Mat_MUMPS *mumps = (Mat_MUMPS *)F->data;
3607: PetscFunctionBegin;
3608: if (nblk) {
3609: PetscAssertPointer(blkptr, 4);
3610: PetscCall(PetscMUMPSIntCast(nblk, &mumps->id.nblk));
3611: PetscCall(PetscFree(mumps->id.blkptr));
3612: PetscCall(PetscMalloc1(nblk + 1, &mumps->id.blkptr));
3613: for (PetscInt i = 0; i < nblk + 1; ++i) PetscCall(PetscMUMPSIntCast(blkptr[i], mumps->id.blkptr + i));
3614: // mumps->id.icntl[] might have not been allocated, which is done in MatSetFromOptions_MUMPS(). So we don't assign ICNTL(15).
3615: // We use id.nblk and id.blkptr to know what values to set to ICNTL(15) in MatSetFromOptions_MUMPS().
3616: // mumps->id.ICNTL(15) = 1;
3617: if (blkvar) {
3618: PetscCall(PetscFree(mumps->id.blkvar));
3619: PetscCall(PetscMalloc1(F->rmap->N, &mumps->id.blkvar));
3620: for (PetscInt i = 0; i < F->rmap->N; ++i) PetscCall(PetscMUMPSIntCast(blkvar[i], mumps->id.blkvar + i));
3621: }
3622: } else {
3623: PetscCall(PetscFree(mumps->id.blkptr));
3624: PetscCall(PetscFree(mumps->id.blkvar));
3625: // mumps->id.ICNTL(15) = 0;
3626: mumps->id.nblk = 0;
3627: }
3628: PetscFunctionReturn(PETSC_SUCCESS);
3629: }
3631: /*MC
3632: MATSOLVERMUMPS - A matrix type providing direct solvers (LU and Cholesky) for
3633: MPI distributed and sequential matrices via the external package MUMPS <https://mumps-solver.org/index.php?page=doc>
3635: Works with `MATAIJ` and `MATSBAIJ` matrices
3637: Use ./configure --download-mumps --download-scalapack --download-parmetis --download-metis --download-ptscotch to have PETSc installed with MUMPS
3639: Use ./configure --with-openmp --download-hwloc (or --with-hwloc) to enable running MUMPS in MPI+OpenMP hybrid mode and non-MUMPS in flat-MPI mode.
3640: See details below.
3642: Use `-pc_type cholesky` or `lu` `-pc_factor_mat_solver_type mumps` to use this direct solver
3644: Options Database Keys:
3645: + -mat_mumps_icntl_1 - ICNTL(1): output stream for error messages
3646: . -mat_mumps_icntl_2 - ICNTL(2): output stream for diagnostic printing, statistics, and warning
3647: . -mat_mumps_icntl_3 - ICNTL(3): output stream for global information, collected on the host
3648: . -mat_mumps_icntl_4 - ICNTL(4): level of printing (0 to 4)
3649: . -mat_mumps_icntl_6 - ICNTL(6): permutes to a zero-free diagonal and/or scale the matrix (0 to 7)
3650: . -mat_mumps_icntl_7 - ICNTL(7): computes a symmetric permutation in sequential analysis, 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto
3651: Use -pc_factor_mat_ordering_type type to have PETSc perform the ordering (sequential only)
3652: . -mat_mumps_icntl_8 - ICNTL(8): scaling strategy (-2 to 8 or 77)
3653: . -mat_mumps_icntl_10 - ICNTL(10): max num of refinements
3654: . -mat_mumps_icntl_11 - ICNTL(11): statistics related to an error analysis (via -ksp_view)
3655: . -mat_mumps_icntl_12 - ICNTL(12): an ordering strategy for symmetric matrices (0 to 3)
3656: . -mat_mumps_icntl_13 - ICNTL(13): parallelism of the root node (enable ScaLAPACK) and its splitting
3657: . -mat_mumps_icntl_14 - ICNTL(14): percentage increase in the estimated working space
3658: . -mat_mumps_icntl_15 - ICNTL(15): compression of the input matrix resulting from a block format
3659: . -mat_mumps_icntl_19 - ICNTL(19): computes the Schur complement
3660: . -mat_mumps_icntl_20 - ICNTL(20): give MUMPS centralized (0) or distributed (10) dense RHS
3661: . -mat_mumps_icntl_22 - ICNTL(22): in-core/out-of-core factorization and solve (0 or 1)
3662: . -mat_mumps_icntl_23 - ICNTL(23): max size of the working memory (MB) that can allocate per processor
3663: . -mat_mumps_icntl_24 - ICNTL(24): detection of null pivot rows (0 or 1)
3664: . -mat_mumps_icntl_25 - ICNTL(25): compute a solution of a deficient matrix and a null space basis
3665: . -mat_mumps_icntl_26 - ICNTL(26): drives the solution phase if a Schur complement matrix
3666: . -mat_mumps_icntl_28 - ICNTL(28): use 1 for sequential analysis and ICNTL(7) ordering, or 2 for parallel analysis and ICNTL(29) ordering
3667: . -mat_mumps_icntl_29 - ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis
3668: . -mat_mumps_icntl_30 - ICNTL(30): compute user-specified set of entries in inv(A)
3669: . -mat_mumps_icntl_31 - ICNTL(31): indicates which factors may be discarded during factorization
3670: . -mat_mumps_icntl_33 - ICNTL(33): compute determinant
3671: . -mat_mumps_icntl_35 - ICNTL(35): level of activation of BLR (Block Low-Rank) feature
3672: . -mat_mumps_icntl_36 - ICNTL(36): controls the choice of BLR factorization variant
3673: . -mat_mumps_icntl_37 - ICNTL(37): compression of the contribution blocks (CB)
3674: . -mat_mumps_icntl_38 - ICNTL(38): sets the estimated compression rate of LU factors with BLR
3675: . -mat_mumps_icntl_40 - ICNTL(40): adaptive BLR precision feature
3676: . -mat_mumps_icntl_47 - ICNTL(47): single precision factorization in a double precision instance
3677: . -mat_mumps_icntl_48 - ICNTL(48): multithreading with tree parallelism
3678: . -mat_mumps_icntl_49 - ICNTL(49): compact workarray at the end of factorization phase
3679: . -mat_mumps_icntl_58 - ICNTL(58): options for symbolic factorization
3680: . -mat_mumps_cntl_1 - CNTL(1): relative pivoting threshold
3681: . -mat_mumps_cntl_2 - CNTL(2): stopping criterion of refinement
3682: . -mat_mumps_cntl_3 - CNTL(3): absolute pivoting threshold
3683: . -mat_mumps_cntl_4 - CNTL(4): value for static pivoting
3684: . -mat_mumps_cntl_5 - CNTL(5): fixation for null pivots
3685: . -mat_mumps_cntl_7 - CNTL(7): precision of the dropping parameter used during BLR factorization
3686: - -mat_mumps_use_omp_threads m - run MUMPS in MPI+OpenMP hybrid mode as if omp_set_num_threads(m) is called before calling MUMPS.
3687: Default might be the number of cores per CPU package (socket) as reported by hwloc and suggested by the MUMPS manual.
3689: Level: beginner
3691: Notes:
3692: MUMPS Cholesky does not handle (complex) Hermitian matrices (see User's Guide at <https://mumps-solver.org/index.php?page=doc>) so using it will
3693: error if the matrix is Hermitian.
3695: When used within a `KSP`/`PC` solve the options are prefixed with that of the `PC`. Otherwise one can set the options prefix by calling
3696: `MatSetOptionsPrefixFactor()` on the matrix from which the factor was obtained or `MatSetOptionsPrefix()` on the factor matrix.
3698: When a MUMPS factorization fails inside a KSP solve, for example with a `KSP_DIVERGED_PC_FAILED`, one can find the MUMPS information about
3699: the failure with
3700: .vb
3701: KSPGetPC(ksp,&pc);
3702: PCFactorGetMatrix(pc,&mat);
3703: MatMumpsGetInfo(mat,....);
3704: MatMumpsGetInfog(mat,....); etc.
3705: .ve
3706: Or run with `-ksp_error_if_not_converged` and the program will be stopped and the information printed in the error message.
3708: MUMPS provides 64-bit integer support in two build modes:
3709: full 64-bit: here MUMPS is built with C preprocessing flag -DINTSIZE64 and Fortran compiler option -i8, -fdefault-integer-8 or equivalent, and
3710: requires all dependent libraries MPI, ScaLAPACK, LAPACK and BLAS built the same way with 64-bit integers (for example ILP64 Intel MKL and MPI).
3712: selective 64-bit: with the default MUMPS build, 64-bit integers have been introduced where needed. In compressed sparse row (CSR) storage of matrices,
3713: MUMPS stores column indices in 32-bit, but row offsets in 64-bit, so you can have a huge number of non-zeros, but must have less than 2^31 rows and
3714: columns. This can lead to significant memory and performance gains with respect to a full 64-bit integer MUMPS version. This requires a regular (32-bit
3715: integer) build of all dependent libraries MPI, ScaLAPACK, LAPACK and BLAS.
3717: With --download-mumps=1, PETSc always build MUMPS in selective 64-bit mode, which can be used by both --with-64-bit-indices=0/1 variants of PETSc.
3719: Two modes to run MUMPS/PETSc with OpenMP
3720: .vb
3721: Set `OMP_NUM_THREADS` and run with fewer MPI ranks than cores. For example, if you want to have 16 OpenMP
3722: threads per rank, then you may use "export `OMP_NUM_THREADS` = 16 && mpiexec -n 4 ./test".
3723: .ve
3725: .vb
3726: `-mat_mumps_use_omp_threads` [m] and run your code with as many MPI ranks as the number of cores. For example,
3727: if a compute node has 32 cores and you run on two nodes, you may use "mpiexec -n 64 ./test -mat_mumps_use_omp_threads 16"
3728: .ve
3730: To run MUMPS in MPI+OpenMP hybrid mode (i.e., enable multithreading in MUMPS), but still run the non-MUMPS part
3731: (i.e., PETSc part) of your code in the so-called flat-MPI (aka pure-MPI) mode, you need to configure PETSc with `--with-openmp` `--download-hwloc`
3732: (or `--with-hwloc`), and have an MPI that supports MPI-3.0's process shared memory (which is usually available). Since MUMPS calls BLAS
3733: libraries, to really get performance, you should have multithreaded BLAS libraries such as Intel MKL, AMD ACML, Cray libSci or OpenBLAS
3734: (PETSc will automatically try to utilized a threaded BLAS if `--with-openmp` is provided).
3736: If you run your code through a job submission system, there are caveats in MPI rank mapping. We use MPI_Comm_split_type() to obtain MPI
3737: processes on each compute node. Listing the processes in rank ascending order, we split processes on a node into consecutive groups of
3738: size m and create a communicator called omp_comm for each group. Rank 0 in an omp_comm is called the master rank, and others in the omp_comm
3739: are called slave ranks (or slaves). Only master ranks are seen to MUMPS and slaves are not. We will free CPUs assigned to slaves (might be set
3740: by CPU binding policies in job scripts) and make the CPUs available to the master so that OMP threads spawned by MUMPS can run on the CPUs.
3741: In a multi-socket compute node, MPI rank mapping is an issue. Still use the above example and suppose your compute node has two sockets,
3742: if you interleave MPI ranks on the two sockets, in other words, even ranks are placed on socket 0, and odd ranks are on socket 1, and bind
3743: MPI ranks to cores, then with `-mat_mumps_use_omp_threads` 16, a master rank (and threads it spawns) will use half cores in socket 0, and half
3744: cores in socket 1, that definitely hurts locality. On the other hand, if you map MPI ranks consecutively on the two sockets, then the
3745: problem will not happen. Therefore, when you use `-mat_mumps_use_omp_threads`, you need to keep an eye on your MPI rank mapping and CPU binding.
3746: For example, with the Slurm job scheduler, one can use srun `--cpu-bind`=verbose -m block:block to map consecutive MPI ranks to sockets and
3747: examine the mapping result.
3749: PETSc does not control thread binding in MUMPS. So to get best performance, one still has to set `OMP_PROC_BIND` and `OMP_PLACES` in job scripts,
3750: for example, export `OMP_PLACES`=threads and export `OMP_PROC_BIND`=spread. One does not need to export `OMP_NUM_THREADS`=m in job scripts as PETSc
3751: calls `omp_set_num_threads`(m) internally before calling MUMPS.
3753: See {cite}`heroux2011bi` and {cite}`gutierrez2017accommodating`
3755: .seealso: [](ch_matrices), `Mat`, `PCFactorSetMatSolverType()`, `MatSolverType`, `MatMumpsSetIcntl()`, `MatMumpsGetIcntl()`, `MatMumpsSetCntl()`, `MatMumpsGetCntl()`, `MatMumpsGetInfo()`, `MatMumpsGetInfog()`, `MatMumpsGetRinfo()`, `MatMumpsGetRinfog()`, `MatMumpsSetBlk()`, `KSPGetPC()`, `PCFactorGetMatrix()`
3756: M*/
3758: static PetscErrorCode MatFactorGetSolverType_mumps(PETSC_UNUSED Mat A, MatSolverType *type)
3759: {
3760: PetscFunctionBegin;
3761: *type = MATSOLVERMUMPS;
3762: PetscFunctionReturn(PETSC_SUCCESS);
3763: }
3765: /* MatGetFactor for Seq and MPI AIJ matrices */
3766: static PetscErrorCode MatGetFactor_aij_mumps(Mat A, MatFactorType ftype, Mat *F)
3767: {
3768: Mat B;
3769: Mat_MUMPS *mumps;
3770: PetscBool isSeqAIJ, isDiag, isDense;
3771: PetscMPIInt size;
3773: PetscFunctionBegin;
3774: if (PetscDefined(USE_COMPLEX) && ftype == MAT_FACTOR_CHOLESKY && A->hermitian == PETSC_BOOL3_TRUE && A->symmetric != PETSC_BOOL3_TRUE) {
3775: PetscCall(PetscInfo(A, "Hermitian MAT_FACTOR_CHOLESKY is not supported. Use MAT_FACTOR_LU instead.\n"));
3776: *F = NULL;
3777: PetscFunctionReturn(PETSC_SUCCESS);
3778: }
3779: /* Create the factorization matrix */
3780: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATSEQAIJ, &isSeqAIJ));
3781: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATDIAGONAL, &isDiag));
3782: PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &isDense, MATSEQDENSE, MATMPIDENSE, NULL));
3783: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
3784: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
3785: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &((PetscObject)B)->type_name));
3786: PetscCall(MatSetUp(B));
3788: PetscCall(PetscNew(&mumps));
3790: B->ops->view = MatView_MUMPS;
3791: B->ops->getinfo = MatGetInfo_MUMPS;
3793: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_mumps));
3794: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorSetSchurIS_C", MatFactorSetSchurIS_MUMPS));
3795: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorCreateSchurComplement_C", MatFactorCreateSchurComplement_MUMPS));
3796: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetIcntl_C", MatMumpsSetIcntl_MUMPS));
3797: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetIcntl_C", MatMumpsGetIcntl_MUMPS));
3798: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetCntl_C", MatMumpsSetCntl_MUMPS));
3799: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetCntl_C", MatMumpsGetCntl_MUMPS));
3800: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfo_C", MatMumpsGetInfo_MUMPS));
3801: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfog_C", MatMumpsGetInfog_MUMPS));
3802: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfo_C", MatMumpsGetRinfo_MUMPS));
3803: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfog_C", MatMumpsGetRinfog_MUMPS));
3804: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetNullPivots_C", MatMumpsGetNullPivots_MUMPS));
3805: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverse_C", MatMumpsGetInverse_MUMPS));
3806: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverseTranspose_C", MatMumpsGetInverseTranspose_MUMPS));
3807: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetBlk_C", MatMumpsSetBlk_MUMPS));
3809: if (ftype == MAT_FACTOR_LU) {
3810: B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
3811: B->factortype = MAT_FACTOR_LU;
3812: if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqaij;
3813: else if (isDiag) mumps->ConvertToTriples = MatConvertToTriples_diagonal_xaij;
3814: else if (isDense) mumps->ConvertToTriples = MatConvertToTriples_dense_xaij;
3815: else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpiaij;
3816: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_LU]));
3817: mumps->sym = 0;
3818: } else {
3819: B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
3820: B->factortype = MAT_FACTOR_CHOLESKY;
3821: if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqsbaij;
3822: else if (isDiag) mumps->ConvertToTriples = MatConvertToTriples_diagonal_xaij;
3823: else if (isDense) mumps->ConvertToTriples = MatConvertToTriples_dense_xaij;
3824: else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpisbaij;
3825: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_CHOLESKY]));
3826: if (PetscDefined(USE_COMPLEX)) mumps->sym = 2;
3827: else if (A->spd == PETSC_BOOL3_TRUE) mumps->sym = 1;
3828: else mumps->sym = 2;
3829: }
3831: /* set solvertype */
3832: PetscCall(PetscFree(B->solvertype));
3833: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &B->solvertype));
3834: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
3835: if (size == 1) {
3836: /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
3837: B->canuseordering = PETSC_TRUE;
3838: }
3839: B->ops->destroy = MatDestroy_MUMPS;
3840: B->data = (void *)mumps;
3842: *F = B;
3843: mumps->id.job = JOB_NULL;
3844: mumps->ICNTL_pre = NULL;
3845: mumps->CNTL_pre = NULL;
3846: mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
3847: PetscFunctionReturn(PETSC_SUCCESS);
3848: }
3850: /* MatGetFactor for Seq and MPI SBAIJ matrices */
3851: static PetscErrorCode MatGetFactor_sbaij_mumps(Mat A, PETSC_UNUSED MatFactorType ftype, Mat *F)
3852: {
3853: Mat B;
3854: Mat_MUMPS *mumps;
3855: PetscBool isSeqSBAIJ;
3856: PetscMPIInt size;
3858: PetscFunctionBegin;
3859: if (PetscDefined(USE_COMPLEX) && ftype == MAT_FACTOR_CHOLESKY && A->hermitian == PETSC_BOOL3_TRUE && A->symmetric != PETSC_BOOL3_TRUE) {
3860: PetscCall(PetscInfo(A, "Hermitian MAT_FACTOR_CHOLESKY is not supported. Use MAT_FACTOR_LU instead.\n"));
3861: *F = NULL;
3862: PetscFunctionReturn(PETSC_SUCCESS);
3863: }
3864: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
3865: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
3866: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &((PetscObject)B)->type_name));
3867: PetscCall(MatSetUp(B));
3869: PetscCall(PetscNew(&mumps));
3870: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQSBAIJ, &isSeqSBAIJ));
3871: if (isSeqSBAIJ) {
3872: mumps->ConvertToTriples = MatConvertToTriples_seqsbaij_seqsbaij;
3873: } else {
3874: mumps->ConvertToTriples = MatConvertToTriples_mpisbaij_mpisbaij;
3875: }
3877: B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
3878: B->ops->view = MatView_MUMPS;
3879: B->ops->getinfo = MatGetInfo_MUMPS;
3881: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_mumps));
3882: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorSetSchurIS_C", MatFactorSetSchurIS_MUMPS));
3883: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorCreateSchurComplement_C", MatFactorCreateSchurComplement_MUMPS));
3884: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetIcntl_C", MatMumpsSetIcntl_MUMPS));
3885: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetIcntl_C", MatMumpsGetIcntl_MUMPS));
3886: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetCntl_C", MatMumpsSetCntl_MUMPS));
3887: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetCntl_C", MatMumpsGetCntl_MUMPS));
3888: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfo_C", MatMumpsGetInfo_MUMPS));
3889: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfog_C", MatMumpsGetInfog_MUMPS));
3890: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfo_C", MatMumpsGetRinfo_MUMPS));
3891: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfog_C", MatMumpsGetRinfog_MUMPS));
3892: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetNullPivots_C", MatMumpsGetNullPivots_MUMPS));
3893: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverse_C", MatMumpsGetInverse_MUMPS));
3894: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverseTranspose_C", MatMumpsGetInverseTranspose_MUMPS));
3895: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetBlk_C", MatMumpsSetBlk_MUMPS));
3897: B->factortype = MAT_FACTOR_CHOLESKY;
3898: if (PetscDefined(USE_COMPLEX)) mumps->sym = 2;
3899: else if (A->spd == PETSC_BOOL3_TRUE) mumps->sym = 1;
3900: else mumps->sym = 2;
3902: /* set solvertype */
3903: PetscCall(PetscFree(B->solvertype));
3904: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &B->solvertype));
3905: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
3906: if (size == 1) {
3907: /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
3908: B->canuseordering = PETSC_TRUE;
3909: }
3910: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_CHOLESKY]));
3911: B->ops->destroy = MatDestroy_MUMPS;
3912: B->data = (void *)mumps;
3914: *F = B;
3915: mumps->id.job = JOB_NULL;
3916: mumps->ICNTL_pre = NULL;
3917: mumps->CNTL_pre = NULL;
3918: mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
3919: PetscFunctionReturn(PETSC_SUCCESS);
3920: }
3922: static PetscErrorCode MatGetFactor_baij_mumps(Mat A, MatFactorType ftype, Mat *F)
3923: {
3924: Mat B;
3925: Mat_MUMPS *mumps;
3926: PetscBool isSeqBAIJ;
3927: PetscMPIInt size;
3929: PetscFunctionBegin;
3930: /* Create the factorization matrix */
3931: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQBAIJ, &isSeqBAIJ));
3932: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
3933: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
3934: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &((PetscObject)B)->type_name));
3935: PetscCall(MatSetUp(B));
3937: PetscCall(PetscNew(&mumps));
3938: PetscCheck(ftype == MAT_FACTOR_LU, PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot use PETSc BAIJ matrices with MUMPS Cholesky, use SBAIJ or AIJ matrix instead");
3939: B->ops->lufactorsymbolic = MatLUFactorSymbolic_BAIJMUMPS;
3940: B->factortype = MAT_FACTOR_LU;
3941: if (isSeqBAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqbaij_seqaij;
3942: else mumps->ConvertToTriples = MatConvertToTriples_mpibaij_mpiaij;
3943: mumps->sym = 0;
3944: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_LU]));
3946: B->ops->view = MatView_MUMPS;
3947: B->ops->getinfo = MatGetInfo_MUMPS;
3949: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_mumps));
3950: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorSetSchurIS_C", MatFactorSetSchurIS_MUMPS));
3951: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorCreateSchurComplement_C", MatFactorCreateSchurComplement_MUMPS));
3952: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetIcntl_C", MatMumpsSetIcntl_MUMPS));
3953: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetIcntl_C", MatMumpsGetIcntl_MUMPS));
3954: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetCntl_C", MatMumpsSetCntl_MUMPS));
3955: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetCntl_C", MatMumpsGetCntl_MUMPS));
3956: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfo_C", MatMumpsGetInfo_MUMPS));
3957: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfog_C", MatMumpsGetInfog_MUMPS));
3958: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfo_C", MatMumpsGetRinfo_MUMPS));
3959: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfog_C", MatMumpsGetRinfog_MUMPS));
3960: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetNullPivots_C", MatMumpsGetNullPivots_MUMPS));
3961: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverse_C", MatMumpsGetInverse_MUMPS));
3962: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverseTranspose_C", MatMumpsGetInverseTranspose_MUMPS));
3963: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetBlk_C", MatMumpsSetBlk_MUMPS));
3965: /* set solvertype */
3966: PetscCall(PetscFree(B->solvertype));
3967: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &B->solvertype));
3968: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
3969: if (size == 1) {
3970: /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
3971: B->canuseordering = PETSC_TRUE;
3972: }
3973: B->ops->destroy = MatDestroy_MUMPS;
3974: B->data = (void *)mumps;
3976: *F = B;
3977: mumps->id.job = JOB_NULL;
3978: mumps->ICNTL_pre = NULL;
3979: mumps->CNTL_pre = NULL;
3980: mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
3981: PetscFunctionReturn(PETSC_SUCCESS);
3982: }
3984: /* MatGetFactor for Seq and MPI SELL matrices */
3985: static PetscErrorCode MatGetFactor_sell_mumps(Mat A, MatFactorType ftype, Mat *F)
3986: {
3987: Mat B;
3988: Mat_MUMPS *mumps;
3989: PetscBool isSeqSELL;
3990: PetscMPIInt size;
3992: PetscFunctionBegin;
3993: /* Create the factorization matrix */
3994: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQSELL, &isSeqSELL));
3995: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
3996: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
3997: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &((PetscObject)B)->type_name));
3998: PetscCall(MatSetUp(B));
4000: PetscCall(PetscNew(&mumps));
4002: B->ops->view = MatView_MUMPS;
4003: B->ops->getinfo = MatGetInfo_MUMPS;
4005: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_mumps));
4006: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorSetSchurIS_C", MatFactorSetSchurIS_MUMPS));
4007: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorCreateSchurComplement_C", MatFactorCreateSchurComplement_MUMPS));
4008: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetIcntl_C", MatMumpsSetIcntl_MUMPS));
4009: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetIcntl_C", MatMumpsGetIcntl_MUMPS));
4010: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetCntl_C", MatMumpsSetCntl_MUMPS));
4011: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetCntl_C", MatMumpsGetCntl_MUMPS));
4012: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfo_C", MatMumpsGetInfo_MUMPS));
4013: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfog_C", MatMumpsGetInfog_MUMPS));
4014: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfo_C", MatMumpsGetRinfo_MUMPS));
4015: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfog_C", MatMumpsGetRinfog_MUMPS));
4016: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetNullPivots_C", MatMumpsGetNullPivots_MUMPS));
4018: PetscCheck(ftype == MAT_FACTOR_LU, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "To be implemented");
4019: B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
4020: B->factortype = MAT_FACTOR_LU;
4021: PetscCheck(isSeqSELL, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "To be implemented");
4022: mumps->ConvertToTriples = MatConvertToTriples_seqsell_seqaij;
4023: mumps->sym = 0;
4024: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_LU]));
4026: /* set solvertype */
4027: PetscCall(PetscFree(B->solvertype));
4028: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &B->solvertype));
4029: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
4030: if (size == 1) {
4031: /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
4032: B->canuseordering = PETSC_TRUE;
4033: }
4034: B->ops->destroy = MatDestroy_MUMPS;
4035: B->data = (void *)mumps;
4037: *F = B;
4038: mumps->id.job = JOB_NULL;
4039: mumps->ICNTL_pre = NULL;
4040: mumps->CNTL_pre = NULL;
4041: mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
4042: PetscFunctionReturn(PETSC_SUCCESS);
4043: }
4045: /* MatGetFactor for MATNEST matrices */
4046: static PetscErrorCode MatGetFactor_nest_mumps(Mat A, MatFactorType ftype, Mat *F)
4047: {
4048: Mat B, **mats;
4049: Mat_MUMPS *mumps;
4050: PetscInt nr, nc;
4051: PetscMPIInt size;
4052: PetscBool flg = PETSC_TRUE;
4054: PetscFunctionBegin;
4055: if (PetscDefined(USE_COMPLEX) && ftype == MAT_FACTOR_CHOLESKY && A->hermitian == PETSC_BOOL3_TRUE && A->symmetric != PETSC_BOOL3_TRUE) {
4056: PetscCall(PetscInfo(A, "Hermitian MAT_FACTOR_CHOLESKY is not supported. Use MAT_FACTOR_LU instead.\n"));
4057: *F = NULL;
4058: PetscFunctionReturn(PETSC_SUCCESS);
4059: }
4061: /* Return if some condition is not satisfied */
4062: *F = NULL;
4063: PetscCall(MatNestGetSubMats(A, &nr, &nc, &mats));
4064: if (ftype == MAT_FACTOR_CHOLESKY) {
4065: IS *rows, *cols;
4066: PetscInt *m, *M;
4068: PetscCheck(nr == nc, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MAT_FACTOR_CHOLESKY not supported for nest sizes %" PetscInt_FMT " != %" PetscInt_FMT ". Use MAT_FACTOR_LU.", nr, nc);
4069: PetscCall(PetscMalloc2(nr, &rows, nc, &cols));
4070: PetscCall(MatNestGetISs(A, rows, cols));
4071: for (PetscInt r = 0; flg && r < nr; r++) PetscCall(ISEqualUnsorted(rows[r], cols[r], &flg));
4072: if (!flg) {
4073: PetscCall(PetscFree2(rows, cols));
4074: PetscCall(PetscInfo(A, "MAT_FACTOR_CHOLESKY not supported for unequal row and column maps. Use MAT_FACTOR_LU.\n"));
4075: PetscFunctionReturn(PETSC_SUCCESS);
4076: }
4077: PetscCall(PetscMalloc2(nr, &m, nr, &M));
4078: for (PetscInt r = 0; r < nr; r++) PetscCall(ISGetMinMax(rows[r], &m[r], &M[r]));
4079: for (PetscInt r = 0; flg && r < nr; r++)
4080: for (PetscInt k = r + 1; flg && k < nr; k++)
4081: if ((m[k] <= m[r] && m[r] <= M[k]) || (m[k] <= M[r] && M[r] <= M[k])) flg = PETSC_FALSE;
4082: PetscCall(PetscFree2(m, M));
4083: PetscCall(PetscFree2(rows, cols));
4084: if (!flg) {
4085: PetscCall(PetscInfo(A, "MAT_FACTOR_CHOLESKY not supported for intersecting row maps. Use MAT_FACTOR_LU.\n"));
4086: PetscFunctionReturn(PETSC_SUCCESS);
4087: }
4088: }
4090: for (PetscInt r = 0; r < nr; r++) {
4091: for (PetscInt c = 0; c < nc; c++) {
4092: Mat sub = mats[r][c];
4093: PetscBool isSeqAIJ, isMPIAIJ, isSeqBAIJ, isMPIBAIJ, isSeqSBAIJ, isMPISBAIJ, isDiag, isDense;
4095: if (!sub || (ftype == MAT_FACTOR_CHOLESKY && c < r)) continue;
4096: PetscCall(MatGetTranspose_TransposeVirtual(&sub, NULL, NULL, NULL, NULL));
4097: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQAIJ, &isSeqAIJ));
4098: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPIAIJ, &isMPIAIJ));
4099: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQBAIJ, &isSeqBAIJ));
4100: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPIBAIJ, &isMPIBAIJ));
4101: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATSEQSBAIJ, &isSeqSBAIJ));
4102: PetscCall(PetscObjectBaseTypeCompare((PetscObject)sub, MATMPISBAIJ, &isMPISBAIJ));
4103: PetscCall(PetscObjectTypeCompare((PetscObject)sub, MATDIAGONAL, &isDiag));
4104: PetscCall(PetscObjectTypeCompareAny((PetscObject)sub, &isDense, MATSEQDENSE, MATMPIDENSE, NULL));
4105: if (ftype == MAT_FACTOR_CHOLESKY) {
4106: if (r == c) {
4107: if (!isSeqAIJ && !isMPIAIJ && !isSeqBAIJ && !isMPIBAIJ && !isSeqSBAIJ && !isMPISBAIJ && !isDiag && !isDense) {
4108: PetscCall(PetscInfo(sub, "MAT_FACTOR_CHOLESKY not supported for diagonal block of type %s.\n", ((PetscObject)sub)->type_name));
4109: flg = PETSC_FALSE;
4110: }
4111: } else if (!isSeqAIJ && !isMPIAIJ && !isSeqBAIJ && !isMPIBAIJ && !isDiag && !isDense) {
4112: PetscCall(PetscInfo(sub, "MAT_FACTOR_CHOLESKY not supported for off-diagonal block of type %s.\n", ((PetscObject)sub)->type_name));
4113: flg = PETSC_FALSE;
4114: }
4115: } else if (!isSeqAIJ && !isMPIAIJ && !isSeqBAIJ && !isMPIBAIJ && !isDiag && !isDense) {
4116: PetscCall(PetscInfo(sub, "MAT_FACTOR_LU not supported for block of type %s.\n", ((PetscObject)sub)->type_name));
4117: flg = PETSC_FALSE;
4118: }
4119: }
4120: }
4121: if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
4123: /* Create the factorization matrix */
4124: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
4125: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
4126: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &((PetscObject)B)->type_name));
4127: PetscCall(MatSetUp(B));
4129: PetscCall(PetscNew(&mumps));
4131: B->ops->view = MatView_MUMPS;
4132: B->ops->getinfo = MatGetInfo_MUMPS;
4134: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_mumps));
4135: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorSetSchurIS_C", MatFactorSetSchurIS_MUMPS));
4136: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorCreateSchurComplement_C", MatFactorCreateSchurComplement_MUMPS));
4137: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetIcntl_C", MatMumpsSetIcntl_MUMPS));
4138: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetIcntl_C", MatMumpsGetIcntl_MUMPS));
4139: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetCntl_C", MatMumpsSetCntl_MUMPS));
4140: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetCntl_C", MatMumpsGetCntl_MUMPS));
4141: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfo_C", MatMumpsGetInfo_MUMPS));
4142: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInfog_C", MatMumpsGetInfog_MUMPS));
4143: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfo_C", MatMumpsGetRinfo_MUMPS));
4144: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetRinfog_C", MatMumpsGetRinfog_MUMPS));
4145: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetNullPivots_C", MatMumpsGetNullPivots_MUMPS));
4146: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverse_C", MatMumpsGetInverse_MUMPS));
4147: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsGetInverseTranspose_C", MatMumpsGetInverseTranspose_MUMPS));
4148: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMumpsSetBlk_C", MatMumpsSetBlk_MUMPS));
4150: if (ftype == MAT_FACTOR_LU) {
4151: B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
4152: B->factortype = MAT_FACTOR_LU;
4153: mumps->sym = 0;
4154: } else {
4155: B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
4156: B->factortype = MAT_FACTOR_CHOLESKY;
4157: if (PetscDefined(USE_COMPLEX)) mumps->sym = 2;
4158: else if (A->spd == PETSC_BOOL3_TRUE) mumps->sym = 1;
4159: else mumps->sym = 2;
4160: }
4161: mumps->ConvertToTriples = MatConvertToTriples_nest_xaij;
4162: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[ftype]));
4164: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
4165: if (size == 1) {
4166: /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
4167: B->canuseordering = PETSC_TRUE;
4168: }
4170: /* set solvertype */
4171: PetscCall(PetscFree(B->solvertype));
4172: PetscCall(PetscStrallocpy(MATSOLVERMUMPS, &B->solvertype));
4173: B->ops->destroy = MatDestroy_MUMPS;
4174: B->data = (void *)mumps;
4176: *F = B;
4177: mumps->id.job = JOB_NULL;
4178: mumps->ICNTL_pre = NULL;
4179: mumps->CNTL_pre = NULL;
4180: mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
4181: PetscFunctionReturn(PETSC_SUCCESS);
4182: }
4184: PETSC_INTERN PetscErrorCode MatSolverTypeRegister_MUMPS(void)
4185: {
4186: PetscFunctionBegin;
4187: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIAIJ, MAT_FACTOR_LU, MatGetFactor_aij_mumps));
4188: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_aij_mumps));
4189: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIBAIJ, MAT_FACTOR_LU, MatGetFactor_baij_mumps));
4190: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIBAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_baij_mumps));
4191: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPISBAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_sbaij_mumps));
4192: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQAIJ, MAT_FACTOR_LU, MatGetFactor_aij_mumps));
4193: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_aij_mumps));
4194: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQBAIJ, MAT_FACTOR_LU, MatGetFactor_baij_mumps));
4195: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQBAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_baij_mumps));
4196: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQSBAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_sbaij_mumps));
4197: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQSELL, MAT_FACTOR_LU, MatGetFactor_sell_mumps));
4198: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATDIAGONAL, MAT_FACTOR_LU, MatGetFactor_aij_mumps));
4199: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATDIAGONAL, MAT_FACTOR_CHOLESKY, MatGetFactor_aij_mumps));
4200: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQDENSE, MAT_FACTOR_LU, MatGetFactor_aij_mumps));
4201: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATSEQDENSE, MAT_FACTOR_CHOLESKY, MatGetFactor_aij_mumps));
4202: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIDENSE, MAT_FACTOR_LU, MatGetFactor_aij_mumps));
4203: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATMPIDENSE, MAT_FACTOR_CHOLESKY, MatGetFactor_aij_mumps));
4204: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATNEST, MAT_FACTOR_LU, MatGetFactor_nest_mumps));
4205: PetscCall(MatSolverTypeRegister(MATSOLVERMUMPS, MATNEST, MAT_FACTOR_CHOLESKY, MatGetFactor_nest_mumps));
4206: PetscFunctionReturn(PETSC_SUCCESS);
4207: }