xref: /petsc/src/mat/impls/aij/mpi/mumps/mumps.c (revision 285fb4e2b69b3de46a0633bd0adc6a7f684caa1e)
1 
2 /*
3     Provides an interface to the MUMPS sparse solver
4 */
5 
6 #include <../src/mat/impls/aij/mpi/mpiaij.h> /*I  "petscmat.h"  I*/
7 #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
8 #include <../src/mat/impls/sell/mpi/mpisell.h>
9 
10 EXTERN_C_BEGIN
11 #if defined(PETSC_USE_COMPLEX)
12 #if defined(PETSC_USE_REAL_SINGLE)
13 #include <cmumps_c.h>
14 #else
15 #include <zmumps_c.h>
16 #endif
17 #else
18 #if defined(PETSC_USE_REAL_SINGLE)
19 #include <smumps_c.h>
20 #else
21 #include <dmumps_c.h>
22 #endif
23 #endif
24 EXTERN_C_END
25 #define JOB_INIT -1
26 #define JOB_FACTSYMBOLIC 1
27 #define JOB_FACTNUMERIC 2
28 #define JOB_SOLVE 3
29 #define JOB_END -2
30 
31 /* calls to MUMPS */
32 #if defined(PETSC_USE_COMPLEX)
33 #if defined(PETSC_USE_REAL_SINGLE)
34 #define PetscMUMPS_c cmumps_c
35 #else
36 #define PetscMUMPS_c zmumps_c
37 #endif
38 #else
39 #if defined(PETSC_USE_REAL_SINGLE)
40 #define PetscMUMPS_c smumps_c
41 #else
42 #define PetscMUMPS_c dmumps_c
43 #endif
44 #endif
45 
46 /* declare MumpsScalar */
47 #if defined(PETSC_USE_COMPLEX)
48 #if defined(PETSC_USE_REAL_SINGLE)
49 #define MumpsScalar mumps_complex
50 #else
51 #define MumpsScalar mumps_double_complex
52 #endif
53 #else
54 #define MumpsScalar PetscScalar
55 #endif
56 
57 /* macros s.t. indices match MUMPS documentation */
58 #define ICNTL(I) icntl[(I)-1]
59 #define CNTL(I) cntl[(I)-1]
60 #define INFOG(I) infog[(I)-1]
61 #define INFO(I) info[(I)-1]
62 #define RINFOG(I) rinfog[(I)-1]
63 #define RINFO(I) rinfo[(I)-1]
64 
65 typedef struct {
66 #if defined(PETSC_USE_COMPLEX)
67 #if defined(PETSC_USE_REAL_SINGLE)
68   CMUMPS_STRUC_C id;
69 #else
70   ZMUMPS_STRUC_C id;
71 #endif
72 #else
73 #if defined(PETSC_USE_REAL_SINGLE)
74   SMUMPS_STRUC_C id;
75 #else
76   DMUMPS_STRUC_C id;
77 #endif
78 #endif
79 
80   MatStructure matstruc;
81   PetscMPIInt  myid,size;
82   PetscInt     *irn,*jcn,nz,sym;
83   PetscScalar  *val;
84   MPI_Comm     comm_mumps;
85   PetscInt     ICNTL9_pre;           /* check if ICNTL(9) is changed from previous MatSolve */
86   VecScatter   scat_rhs, scat_sol;   /* used by MatSolve() */
87   Vec          b_seq,x_seq;
88   PetscInt     ninfo,*info;          /* display INFO */
89   PetscInt     sizeredrhs;
90   PetscScalar  *schur_sol;
91   PetscInt     schur_sizesol;
92 
93   PetscErrorCode (*ConvertToTriples)(Mat, int, MatReuse, int*, int**, int**, PetscScalar**);
94 } Mat_MUMPS;
95 
96 extern PetscErrorCode MatDuplicate_MUMPS(Mat,MatDuplicateOption,Mat*);
97 
98 static PetscErrorCode MatMumpsResetSchur_Private(Mat_MUMPS* mumps)
99 {
100   PetscErrorCode ierr;
101 
102   PetscFunctionBegin;
103   ierr = PetscFree2(mumps->id.listvar_schur,mumps->id.schur);CHKERRQ(ierr);
104   ierr = PetscFree(mumps->id.redrhs);CHKERRQ(ierr);
105   ierr = PetscFree(mumps->schur_sol);CHKERRQ(ierr);
106   mumps->id.size_schur = 0;
107   mumps->id.schur_lld  = 0;
108   mumps->id.ICNTL(19)  = 0;
109   PetscFunctionReturn(0);
110 }
111 
112 /* solve with rhs in mumps->id.redrhs and return in the same location */
113 static PetscErrorCode MatMumpsSolveSchur_Private(Mat F)
114 {
115   Mat_MUMPS            *mumps=(Mat_MUMPS*)F->data;
116   Mat                  S,B,X;
117   MatFactorSchurStatus schurstatus;
118   PetscInt             sizesol;
119   PetscErrorCode       ierr;
120 
121   PetscFunctionBegin;
122   ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr);
123   ierr = MatFactorGetSchurComplement(F,&S,&schurstatus);CHKERRQ(ierr);
124   ierr = MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,(PetscScalar*)mumps->id.redrhs,&B);CHKERRQ(ierr);
125   switch (schurstatus) {
126   case MAT_FACTOR_SCHUR_FACTORED:
127     ierr = MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,(PetscScalar*)mumps->id.redrhs,&X);CHKERRQ(ierr);
128     if (!mumps->id.ICNTL(9)) { /* transpose solve */
129       ierr = MatMatSolveTranspose(S,B,X);CHKERRQ(ierr);
130     } else {
131       ierr = MatMatSolve(S,B,X);CHKERRQ(ierr);
132     }
133     break;
134   case MAT_FACTOR_SCHUR_INVERTED:
135     sizesol = mumps->id.nrhs*mumps->id.size_schur;
136     if (!mumps->schur_sol || sizesol > mumps->schur_sizesol) {
137       ierr = PetscFree(mumps->schur_sol);CHKERRQ(ierr);
138       ierr = PetscMalloc1(sizesol,&mumps->schur_sol);CHKERRQ(ierr);
139       mumps->schur_sizesol = sizesol;
140     }
141     ierr = MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,mumps->schur_sol,&X);CHKERRQ(ierr);
142     if (!mumps->id.ICNTL(9)) { /* transpose solve */
143       ierr = MatTransposeMatMult(S,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&X);CHKERRQ(ierr);
144     } else {
145       ierr = MatMatMult(S,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&X);CHKERRQ(ierr);
146     }
147     ierr = MatCopy(X,B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
148     break;
149   default:
150     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
151     break;
152   }
153   ierr = MatFactorRestoreSchurComplement(F,&S,schurstatus);CHKERRQ(ierr);
154   ierr = MatDestroy(&B);CHKERRQ(ierr);
155   ierr = MatDestroy(&X);CHKERRQ(ierr);
156   PetscFunctionReturn(0);
157 }
158 
159 static PetscErrorCode MatMumpsHandleSchur_Private(Mat F, PetscBool expansion)
160 {
161   Mat_MUMPS     *mumps=(Mat_MUMPS*)F->data;
162   PetscErrorCode ierr;
163 
164   PetscFunctionBegin;
165   if (!mumps->id.ICNTL(19)) { /* do nothing when Schur complement has not been computed */
166     PetscFunctionReturn(0);
167   }
168   if (!expansion) { /* prepare for the condensation step */
169     PetscInt sizeredrhs = mumps->id.nrhs*mumps->id.size_schur;
170     /* allocate MUMPS internal array to store reduced right-hand sides */
171     if (!mumps->id.redrhs || sizeredrhs > mumps->sizeredrhs) {
172       ierr = PetscFree(mumps->id.redrhs);CHKERRQ(ierr);
173       mumps->id.lredrhs = mumps->id.size_schur;
174       ierr = PetscMalloc1(mumps->id.nrhs*mumps->id.lredrhs,&mumps->id.redrhs);CHKERRQ(ierr);
175       mumps->sizeredrhs = mumps->id.nrhs*mumps->id.lredrhs;
176     }
177     mumps->id.ICNTL(26) = 1; /* condensation phase */
178   } else { /* prepare for the expansion step */
179     /* solve Schur complement (this has to be done by the MUMPS user, so basically us) */
180     ierr = MatMumpsSolveSchur_Private(F);CHKERRQ(ierr);
181     mumps->id.ICNTL(26) = 2; /* expansion phase */
182     PetscMUMPS_c(&mumps->id);
183     if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in solve phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
184     /* restore defaults */
185     mumps->id.ICNTL(26) = -1;
186     /* free MUMPS internal array for redrhs if we have solved for multiple rhs in order to save memory space */
187     if (mumps->id.nrhs > 1) {
188       ierr = PetscFree(mumps->id.redrhs);CHKERRQ(ierr);
189       mumps->id.lredrhs = 0;
190       mumps->sizeredrhs = 0;
191     }
192   }
193   PetscFunctionReturn(0);
194 }
195 
196 /*
197   MatConvertToTriples_A_B - convert Petsc matrix to triples: row[nz], col[nz], val[nz]
198 
199   input:
200     A       - matrix in aij,baij or sbaij (bs=1) format
201     shift   - 0: C style output triple; 1: Fortran style output triple.
202     reuse   - MAT_INITIAL_MATRIX: spaces are allocated and values are set for the triple
203               MAT_REUSE_MATRIX:   only the values in v array are updated
204   output:
205     nnz     - dim of r, c, and v (number of local nonzero entries of A)
206     r, c, v - row and col index, matrix values (matrix triples)
207 
208   The returned values r, c, and sometimes v are obtained in a single PetscMalloc(). Then in MatDestroy_MUMPS() it is
209   freed with PetscFree(mumps->irn);  This is not ideal code, the fact that v is ONLY sometimes part of mumps->irn means
210   that the PetscMalloc() cannot easily be replaced with a PetscMalloc3().
211 
212  */
213 
214 PetscErrorCode MatConvertToTriples_seqaij_seqaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
215 {
216   const PetscInt *ai,*aj,*ajj,M=A->rmap->n;
217   PetscInt       nz,rnz,i,j;
218   PetscErrorCode ierr;
219   PetscInt       *row,*col;
220   Mat_SeqAIJ     *aa=(Mat_SeqAIJ*)A->data;
221 
222   PetscFunctionBegin;
223   *v=aa->a;
224   if (reuse == MAT_INITIAL_MATRIX) {
225     nz   = aa->nz;
226     ai   = aa->i;
227     aj   = aa->j;
228     *nnz = nz;
229     ierr = PetscMalloc1(2*nz, &row);CHKERRQ(ierr);
230     col  = row + nz;
231 
232     nz = 0;
233     for (i=0; i<M; i++) {
234       rnz = ai[i+1] - ai[i];
235       ajj = aj + ai[i];
236       for (j=0; j<rnz; j++) {
237         row[nz] = i+shift; col[nz++] = ajj[j] + shift;
238       }
239     }
240     *r = row; *c = col;
241   }
242   PetscFunctionReturn(0);
243 }
244 
245 PetscErrorCode MatConvertToTriples_seqsell_seqaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
246 {
247   Mat_SeqSELL *a=(Mat_SeqSELL*)A->data;
248   PetscInt    *ptr;
249 
250   PetscFunctionBegin;
251   *v = a->val;
252   if (reuse == MAT_INITIAL_MATRIX) {
253     PetscInt       nz,i,j,row;
254     PetscErrorCode ierr;
255 
256     nz   = a->sliidx[a->totalslices];
257     *nnz = nz;
258     ierr = PetscMalloc1(2*nz, &ptr);CHKERRQ(ierr);
259     *r   = ptr;
260     *c   = ptr + nz;
261 
262     for (i=0; i<a->totalslices; i++) {
263       for (j=a->sliidx[i],row=0; j<a->sliidx[i+1]; j++,row=((row+1)&0x07)) {
264         *ptr++ = 8*i + row + shift;
265       }
266     }
267     for (i=0;i<nz;i++) *ptr++ = a->colidx[i] + shift;
268   }
269   PetscFunctionReturn(0);
270 }
271 
272 PetscErrorCode MatConvertToTriples_seqbaij_seqaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
273 {
274   Mat_SeqBAIJ    *aa=(Mat_SeqBAIJ*)A->data;
275   const PetscInt *ai,*aj,*ajj,bs2 = aa->bs2;
276   PetscInt       bs,M,nz,idx=0,rnz,i,j,k,m;
277   PetscErrorCode ierr;
278   PetscInt       *row,*col;
279 
280   PetscFunctionBegin;
281   ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr);
282   M = A->rmap->N/bs;
283   *v = aa->a;
284   if (reuse == MAT_INITIAL_MATRIX) {
285     ai   = aa->i; aj = aa->j;
286     nz   = bs2*aa->nz;
287     *nnz = nz;
288     ierr = PetscMalloc1(2*nz, &row);CHKERRQ(ierr);
289     col  = row + nz;
290 
291     for (i=0; i<M; i++) {
292       ajj = aj + ai[i];
293       rnz = ai[i+1] - ai[i];
294       for (k=0; k<rnz; k++) {
295         for (j=0; j<bs; j++) {
296           for (m=0; m<bs; m++) {
297             row[idx]   = i*bs + m + shift;
298             col[idx++] = bs*(ajj[k]) + j + shift;
299           }
300         }
301       }
302     }
303     *r = row; *c = col;
304   }
305   PetscFunctionReturn(0);
306 }
307 
308 PetscErrorCode MatConvertToTriples_seqsbaij_seqsbaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
309 {
310   const PetscInt *ai, *aj,*ajj,M=A->rmap->n;
311   PetscInt       nz,rnz,i,j;
312   PetscErrorCode ierr;
313   PetscInt       *row,*col;
314   Mat_SeqSBAIJ   *aa=(Mat_SeqSBAIJ*)A->data;
315 
316   PetscFunctionBegin;
317   *v = aa->a;
318   if (reuse == MAT_INITIAL_MATRIX) {
319     nz   = aa->nz;
320     ai   = aa->i;
321     aj   = aa->j;
322     *v   = aa->a;
323     *nnz = nz;
324     ierr = PetscMalloc1(2*nz, &row);CHKERRQ(ierr);
325     col  = row + nz;
326 
327     nz = 0;
328     for (i=0; i<M; i++) {
329       rnz = ai[i+1] - ai[i];
330       ajj = aj + ai[i];
331       for (j=0; j<rnz; j++) {
332         row[nz] = i+shift; col[nz++] = ajj[j] + shift;
333       }
334     }
335     *r = row; *c = col;
336   }
337   PetscFunctionReturn(0);
338 }
339 
340 PetscErrorCode MatConvertToTriples_seqaij_seqsbaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
341 {
342   const PetscInt    *ai,*aj,*ajj,*adiag,M=A->rmap->n;
343   PetscInt          nz,rnz,i,j;
344   const PetscScalar *av,*v1;
345   PetscScalar       *val;
346   PetscErrorCode    ierr;
347   PetscInt          *row,*col;
348   Mat_SeqAIJ        *aa=(Mat_SeqAIJ*)A->data;
349   PetscBool         missing;
350 
351   PetscFunctionBegin;
352   ai    = aa->i; aj = aa->j; av = aa->a;
353   adiag = aa->diag;
354   ierr  = MatMissingDiagonal_SeqAIJ(A,&missing,&i);CHKERRQ(ierr);
355   if (reuse == MAT_INITIAL_MATRIX) {
356     /* count nz in the upper triangular part of A */
357     nz = 0;
358     if (missing) {
359       for (i=0; i<M; i++) {
360         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
361           for (j=ai[i];j<ai[i+1];j++) {
362             if (aj[j] < i) continue;
363             nz++;
364           }
365         } else {
366           nz += ai[i+1] - adiag[i];
367         }
368       }
369     } else {
370       for (i=0; i<M; i++) nz += ai[i+1] - adiag[i];
371     }
372     *nnz = nz;
373 
374     ierr = PetscMalloc((2*nz*sizeof(PetscInt)+nz*sizeof(PetscScalar)), &row);CHKERRQ(ierr);
375     col  = row + nz;
376     val  = (PetscScalar*)(col + nz);
377 
378     nz = 0;
379     if (missing) {
380       for (i=0; i<M; i++) {
381         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
382           for (j=ai[i];j<ai[i+1];j++) {
383             if (aj[j] < i) continue;
384             row[nz] = i+shift;
385             col[nz] = aj[j]+shift;
386             val[nz] = av[j];
387             nz++;
388           }
389         } else {
390           rnz = ai[i+1] - adiag[i];
391           ajj = aj + adiag[i];
392           v1  = av + adiag[i];
393           for (j=0; j<rnz; j++) {
394             row[nz] = i+shift; col[nz] = ajj[j] + shift; val[nz++] = v1[j];
395           }
396         }
397       }
398     } else {
399       for (i=0; i<M; i++) {
400         rnz = ai[i+1] - adiag[i];
401         ajj = aj + adiag[i];
402         v1  = av + adiag[i];
403         for (j=0; j<rnz; j++) {
404           row[nz] = i+shift; col[nz] = ajj[j] + shift; val[nz++] = v1[j];
405         }
406       }
407     }
408     *r = row; *c = col; *v = val;
409   } else {
410     nz = 0; val = *v;
411     if (missing) {
412       for (i=0; i <M; i++) {
413         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
414           for (j=ai[i];j<ai[i+1];j++) {
415             if (aj[j] < i) continue;
416             val[nz++] = av[j];
417           }
418         } else {
419           rnz = ai[i+1] - adiag[i];
420           v1  = av + adiag[i];
421           for (j=0; j<rnz; j++) {
422             val[nz++] = v1[j];
423           }
424         }
425       }
426     } else {
427       for (i=0; i <M; i++) {
428         rnz = ai[i+1] - adiag[i];
429         v1  = av + adiag[i];
430         for (j=0; j<rnz; j++) {
431           val[nz++] = v1[j];
432         }
433       }
434     }
435   }
436   PetscFunctionReturn(0);
437 }
438 
439 PetscErrorCode MatConvertToTriples_mpisbaij_mpisbaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
440 {
441   const PetscInt    *ai, *aj, *bi, *bj,*garray,m=A->rmap->n,*ajj,*bjj;
442   PetscErrorCode    ierr;
443   PetscInt          rstart,nz,i,j,jj,irow,countA,countB;
444   PetscInt          *row,*col;
445   const PetscScalar *av, *bv,*v1,*v2;
446   PetscScalar       *val;
447   Mat_MPISBAIJ      *mat = (Mat_MPISBAIJ*)A->data;
448   Mat_SeqSBAIJ      *aa  = (Mat_SeqSBAIJ*)(mat->A)->data;
449   Mat_SeqBAIJ       *bb  = (Mat_SeqBAIJ*)(mat->B)->data;
450 
451   PetscFunctionBegin;
452   ai=aa->i; aj=aa->j; bi=bb->i; bj=bb->j; rstart= A->rmap->rstart;
453   av=aa->a; bv=bb->a;
454 
455   garray = mat->garray;
456 
457   if (reuse == MAT_INITIAL_MATRIX) {
458     nz   = aa->nz + bb->nz;
459     *nnz = nz;
460     ierr = PetscMalloc((2*nz*sizeof(PetscInt)+nz*sizeof(PetscScalar)), &row);CHKERRQ(ierr);
461     col  = row + nz;
462     val  = (PetscScalar*)(col + nz);
463 
464     *r = row; *c = col; *v = val;
465   } else {
466     row = *r; col = *c; val = *v;
467   }
468 
469   jj = 0; irow = rstart;
470   for (i=0; i<m; i++) {
471     ajj    = aj + ai[i];                 /* ptr to the beginning of this row */
472     countA = ai[i+1] - ai[i];
473     countB = bi[i+1] - bi[i];
474     bjj    = bj + bi[i];
475     v1     = av + ai[i];
476     v2     = bv + bi[i];
477 
478     /* A-part */
479     for (j=0; j<countA; j++) {
480       if (reuse == MAT_INITIAL_MATRIX) {
481         row[jj] = irow + shift; col[jj] = rstart + ajj[j] + shift;
482       }
483       val[jj++] = v1[j];
484     }
485 
486     /* B-part */
487     for (j=0; j < countB; j++) {
488       if (reuse == MAT_INITIAL_MATRIX) {
489         row[jj] = irow + shift; col[jj] = garray[bjj[j]] + shift;
490       }
491       val[jj++] = v2[j];
492     }
493     irow++;
494   }
495   PetscFunctionReturn(0);
496 }
497 
498 PetscErrorCode MatConvertToTriples_mpiaij_mpiaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
499 {
500   const PetscInt    *ai, *aj, *bi, *bj,*garray,m=A->rmap->n,*ajj,*bjj;
501   PetscErrorCode    ierr;
502   PetscInt          rstart,nz,i,j,jj,irow,countA,countB;
503   PetscInt          *row,*col;
504   const PetscScalar *av, *bv,*v1,*v2;
505   PetscScalar       *val;
506   Mat_MPIAIJ        *mat = (Mat_MPIAIJ*)A->data;
507   Mat_SeqAIJ        *aa  = (Mat_SeqAIJ*)(mat->A)->data;
508   Mat_SeqAIJ        *bb  = (Mat_SeqAIJ*)(mat->B)->data;
509 
510   PetscFunctionBegin;
511   ai=aa->i; aj=aa->j; bi=bb->i; bj=bb->j; rstart= A->rmap->rstart;
512   av=aa->a; bv=bb->a;
513 
514   garray = mat->garray;
515 
516   if (reuse == MAT_INITIAL_MATRIX) {
517     nz   = aa->nz + bb->nz;
518     *nnz = nz;
519     ierr = PetscMalloc((2*nz*sizeof(PetscInt)+nz*sizeof(PetscScalar)), &row);CHKERRQ(ierr);
520     col  = row + nz;
521     val  = (PetscScalar*)(col + nz);
522 
523     *r = row; *c = col; *v = val;
524   } else {
525     row = *r; col = *c; val = *v;
526   }
527 
528   jj = 0; irow = rstart;
529   for (i=0; i<m; i++) {
530     ajj    = aj + ai[i];                 /* ptr to the beginning of this row */
531     countA = ai[i+1] - ai[i];
532     countB = bi[i+1] - bi[i];
533     bjj    = bj + bi[i];
534     v1     = av + ai[i];
535     v2     = bv + bi[i];
536 
537     /* A-part */
538     for (j=0; j<countA; j++) {
539       if (reuse == MAT_INITIAL_MATRIX) {
540         row[jj] = irow + shift; col[jj] = rstart + ajj[j] + shift;
541       }
542       val[jj++] = v1[j];
543     }
544 
545     /* B-part */
546     for (j=0; j < countB; j++) {
547       if (reuse == MAT_INITIAL_MATRIX) {
548         row[jj] = irow + shift; col[jj] = garray[bjj[j]] + shift;
549       }
550       val[jj++] = v2[j];
551     }
552     irow++;
553   }
554   PetscFunctionReturn(0);
555 }
556 
557 PetscErrorCode MatConvertToTriples_mpibaij_mpiaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
558 {
559   Mat_MPIBAIJ       *mat    = (Mat_MPIBAIJ*)A->data;
560   Mat_SeqBAIJ       *aa     = (Mat_SeqBAIJ*)(mat->A)->data;
561   Mat_SeqBAIJ       *bb     = (Mat_SeqBAIJ*)(mat->B)->data;
562   const PetscInt    *ai     = aa->i, *bi = bb->i, *aj = aa->j, *bj = bb->j,*ajj, *bjj;
563   const PetscInt    *garray = mat->garray,mbs=mat->mbs,rstart=A->rmap->rstart;
564   const PetscInt    bs2=mat->bs2;
565   PetscErrorCode    ierr;
566   PetscInt          bs,nz,i,j,k,n,jj,irow,countA,countB,idx;
567   PetscInt          *row,*col;
568   const PetscScalar *av=aa->a, *bv=bb->a,*v1,*v2;
569   PetscScalar       *val;
570 
571   PetscFunctionBegin;
572   ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr);
573   if (reuse == MAT_INITIAL_MATRIX) {
574     nz   = bs2*(aa->nz + bb->nz);
575     *nnz = nz;
576     ierr = PetscMalloc((2*nz*sizeof(PetscInt)+nz*sizeof(PetscScalar)), &row);CHKERRQ(ierr);
577     col  = row + nz;
578     val  = (PetscScalar*)(col + nz);
579 
580     *r = row; *c = col; *v = val;
581   } else {
582     row = *r; col = *c; val = *v;
583   }
584 
585   jj = 0; irow = rstart;
586   for (i=0; i<mbs; i++) {
587     countA = ai[i+1] - ai[i];
588     countB = bi[i+1] - bi[i];
589     ajj    = aj + ai[i];
590     bjj    = bj + bi[i];
591     v1     = av + bs2*ai[i];
592     v2     = bv + bs2*bi[i];
593 
594     idx = 0;
595     /* A-part */
596     for (k=0; k<countA; k++) {
597       for (j=0; j<bs; j++) {
598         for (n=0; n<bs; n++) {
599           if (reuse == MAT_INITIAL_MATRIX) {
600             row[jj] = irow + n + shift;
601             col[jj] = rstart + bs*ajj[k] + j + shift;
602           }
603           val[jj++] = v1[idx++];
604         }
605       }
606     }
607 
608     idx = 0;
609     /* B-part */
610     for (k=0; k<countB; k++) {
611       for (j=0; j<bs; j++) {
612         for (n=0; n<bs; n++) {
613           if (reuse == MAT_INITIAL_MATRIX) {
614             row[jj] = irow + n + shift;
615             col[jj] = bs*garray[bjj[k]] + j + shift;
616           }
617           val[jj++] = v2[idx++];
618         }
619       }
620     }
621     irow += bs;
622   }
623   PetscFunctionReturn(0);
624 }
625 
626 PetscErrorCode MatConvertToTriples_mpiaij_mpisbaij(Mat A,int shift,MatReuse reuse,int *nnz,int **r, int **c, PetscScalar **v)
627 {
628   const PetscInt    *ai, *aj,*adiag, *bi, *bj,*garray,m=A->rmap->n,*ajj,*bjj;
629   PetscErrorCode    ierr;
630   PetscInt          rstart,nz,nza,nzb,i,j,jj,irow,countA,countB;
631   PetscInt          *row,*col;
632   const PetscScalar *av, *bv,*v1,*v2;
633   PetscScalar       *val;
634   Mat_MPIAIJ        *mat =  (Mat_MPIAIJ*)A->data;
635   Mat_SeqAIJ        *aa  =(Mat_SeqAIJ*)(mat->A)->data;
636   Mat_SeqAIJ        *bb  =(Mat_SeqAIJ*)(mat->B)->data;
637 
638   PetscFunctionBegin;
639   ai=aa->i; aj=aa->j; adiag=aa->diag;
640   bi=bb->i; bj=bb->j; garray = mat->garray;
641   av=aa->a; bv=bb->a;
642 
643   rstart = A->rmap->rstart;
644 
645   if (reuse == MAT_INITIAL_MATRIX) {
646     nza = 0;    /* num of upper triangular entries in mat->A, including diagonals */
647     nzb = 0;    /* num of upper triangular entries in mat->B */
648     for (i=0; i<m; i++) {
649       nza   += (ai[i+1] - adiag[i]);
650       countB = bi[i+1] - bi[i];
651       bjj    = bj + bi[i];
652       for (j=0; j<countB; j++) {
653         if (garray[bjj[j]] > rstart) nzb++;
654       }
655     }
656 
657     nz   = nza + nzb; /* total nz of upper triangular part of mat */
658     *nnz = nz;
659     ierr = PetscMalloc((2*nz*sizeof(PetscInt)+nz*sizeof(PetscScalar)), &row);CHKERRQ(ierr);
660     col  = row + nz;
661     val  = (PetscScalar*)(col + nz);
662 
663     *r = row; *c = col; *v = val;
664   } else {
665     row = *r; col = *c; val = *v;
666   }
667 
668   jj = 0; irow = rstart;
669   for (i=0; i<m; i++) {
670     ajj    = aj + adiag[i];                 /* ptr to the beginning of the diagonal of this row */
671     v1     = av + adiag[i];
672     countA = ai[i+1] - adiag[i];
673     countB = bi[i+1] - bi[i];
674     bjj    = bj + bi[i];
675     v2     = bv + bi[i];
676 
677     /* A-part */
678     for (j=0; j<countA; j++) {
679       if (reuse == MAT_INITIAL_MATRIX) {
680         row[jj] = irow + shift; col[jj] = rstart + ajj[j] + shift;
681       }
682       val[jj++] = v1[j];
683     }
684 
685     /* B-part */
686     for (j=0; j < countB; j++) {
687       if (garray[bjj[j]] > rstart) {
688         if (reuse == MAT_INITIAL_MATRIX) {
689           row[jj] = irow + shift; col[jj] = garray[bjj[j]] + shift;
690         }
691         val[jj++] = v2[j];
692       }
693     }
694     irow++;
695   }
696   PetscFunctionReturn(0);
697 }
698 
699 PetscErrorCode MatDestroy_MUMPS(Mat A)
700 {
701   Mat_MUMPS      *mumps=(Mat_MUMPS*)A->data;
702   PetscErrorCode ierr;
703 
704   PetscFunctionBegin;
705   ierr = PetscFree2(mumps->id.sol_loc,mumps->id.isol_loc);CHKERRQ(ierr);
706   ierr = VecScatterDestroy(&mumps->scat_rhs);CHKERRQ(ierr);
707   ierr = VecScatterDestroy(&mumps->scat_sol);CHKERRQ(ierr);
708   ierr = VecDestroy(&mumps->b_seq);CHKERRQ(ierr);
709   ierr = VecDestroy(&mumps->x_seq);CHKERRQ(ierr);
710   ierr = PetscFree(mumps->id.perm_in);CHKERRQ(ierr);
711   ierr = PetscFree(mumps->irn);CHKERRQ(ierr);
712   ierr = PetscFree(mumps->info);CHKERRQ(ierr);
713   ierr = MatMumpsResetSchur_Private(mumps);CHKERRQ(ierr);
714   mumps->id.job = JOB_END;
715   PetscMUMPS_c(&mumps->id);
716   ierr = MPI_Comm_free(&mumps->comm_mumps);CHKERRQ(ierr);
717   ierr = PetscFree(A->data);CHKERRQ(ierr);
718 
719   /* clear composed functions */
720   ierr = PetscObjectComposeFunction((PetscObject)A,"MatFactorGetSolverType_C",NULL);CHKERRQ(ierr);
721   ierr = PetscObjectComposeFunction((PetscObject)A,"MatFactorSetSchurIS_C",NULL);CHKERRQ(ierr);
722   ierr = PetscObjectComposeFunction((PetscObject)A,"MatFactorCreateSchurComplement_C",NULL);CHKERRQ(ierr);
723   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsSetIcntl_C",NULL);CHKERRQ(ierr);
724   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetIcntl_C",NULL);CHKERRQ(ierr);
725   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsSetCntl_C",NULL);CHKERRQ(ierr);
726   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetCntl_C",NULL);CHKERRQ(ierr);
727   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInfo_C",NULL);CHKERRQ(ierr);
728   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInfog_C",NULL);CHKERRQ(ierr);
729   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetRinfo_C",NULL);CHKERRQ(ierr);
730   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetRinfog_C",NULL);CHKERRQ(ierr);
731   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInverse_C",NULL);CHKERRQ(ierr);
732   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInverseTranspose_C",NULL);CHKERRQ(ierr);
733   PetscFunctionReturn(0);
734 }
735 
736 PetscErrorCode MatSolve_MUMPS(Mat A,Vec b,Vec x)
737 {
738   Mat_MUMPS        *mumps=(Mat_MUMPS*)A->data;
739   PetscScalar      *array;
740   Vec              b_seq;
741   IS               is_iden,is_petsc;
742   PetscErrorCode   ierr;
743   PetscInt         i;
744   PetscBool        second_solve = PETSC_FALSE;
745   static PetscBool cite1 = PETSC_FALSE,cite2 = PETSC_FALSE;
746 
747   PetscFunctionBegin;
748   ierr = 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 Journal on Matrix Analysis and Applications},\n  volume = {23},\n  number = {1},\n  pages = {15--41},\n  year = {2001}\n}\n",&cite1);CHKERRQ(ierr);
749   ierr = 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 Computing},\n  volume = {32},\n  number = {2},\n  pages = {136--156},\n  year = {2006}\n}\n",&cite2);CHKERRQ(ierr);
750 
751   if (A->factorerrortype) {
752     ierr = PetscInfo2(A,"MatSolve is called with singular matrix factor, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
753     ierr = VecSetInf(x);CHKERRQ(ierr);
754     PetscFunctionReturn(0);
755   }
756 
757   mumps->id.ICNTL(20)= 0; /* dense RHS */
758   mumps->id.nrhs     = 1;
759   b_seq          = mumps->b_seq;
760   if (mumps->size > 1) {
761     /* MUMPS only supports centralized rhs. Scatter b into a seqential rhs vector */
762     ierr = VecScatterBegin(mumps->scat_rhs,b,b_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
763     ierr = VecScatterEnd(mumps->scat_rhs,b,b_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
764     if (!mumps->myid) {ierr = VecGetArray(b_seq,&array);CHKERRQ(ierr);}
765   } else {  /* size == 1 */
766     ierr = VecCopy(b,x);CHKERRQ(ierr);
767     ierr = VecGetArray(x,&array);CHKERRQ(ierr);
768   }
769   if (!mumps->myid) { /* define rhs on the host */
770     mumps->id.nrhs = 1;
771     mumps->id.rhs = (MumpsScalar*)array;
772   }
773 
774   /*
775      handle condensation step of Schur complement (if any)
776      We set by default ICNTL(26) == -1 when Schur indices have been provided by the user.
777      According to MUMPS (5.0.0) manual, any value should be harmful during the factorization phase
778      Unless the user provides a valid value for ICNTL(26), MatSolve and MatMatSolve routines solve the full system.
779      This requires an extra call to PetscMUMPS_c and the computation of the factors for S
780   */
781   if (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2) {
782     if (mumps->size > 1) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Parallel Schur complements not yet supported from PETSc\n");
783     second_solve = PETSC_TRUE;
784     ierr = MatMumpsHandleSchur_Private(A,PETSC_FALSE);CHKERRQ(ierr);
785   }
786   /* solve phase */
787   /*-------------*/
788   mumps->id.job = JOB_SOLVE;
789   PetscMUMPS_c(&mumps->id);
790   if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in solve phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
791 
792   /* handle expansion step of Schur complement (if any) */
793   if (second_solve) {
794     ierr = MatMumpsHandleSchur_Private(A,PETSC_TRUE);CHKERRQ(ierr);
795   }
796 
797   if (mumps->size > 1) { /* convert mumps distributed solution to petsc mpi x */
798     if (mumps->scat_sol && mumps->ICNTL9_pre != mumps->id.ICNTL(9)) {
799       /* when id.ICNTL(9) changes, the contents of lsol_loc may change (not its size, lsol_loc), recreates scat_sol */
800       ierr = VecScatterDestroy(&mumps->scat_sol);CHKERRQ(ierr);
801     }
802     if (!mumps->scat_sol) { /* create scatter scat_sol */
803       ierr = ISCreateStride(PETSC_COMM_SELF,mumps->id.lsol_loc,0,1,&is_iden);CHKERRQ(ierr); /* from */
804       for (i=0; i<mumps->id.lsol_loc; i++) {
805         mumps->id.isol_loc[i] -= 1; /* change Fortran style to C style */
806       }
807       ierr = ISCreateGeneral(PETSC_COMM_SELF,mumps->id.lsol_loc,mumps->id.isol_loc,PETSC_COPY_VALUES,&is_petsc);CHKERRQ(ierr);  /* to */
808       ierr = VecScatterCreate(mumps->x_seq,is_iden,x,is_petsc,&mumps->scat_sol);CHKERRQ(ierr);
809       ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
810       ierr = ISDestroy(&is_petsc);CHKERRQ(ierr);
811 
812       mumps->ICNTL9_pre = mumps->id.ICNTL(9); /* save current value of id.ICNTL(9) */
813     }
814 
815     ierr = VecScatterBegin(mumps->scat_sol,mumps->x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
816     ierr = VecScatterEnd(mumps->scat_sol,mumps->x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
817   }
818   PetscFunctionReturn(0);
819 }
820 
821 PetscErrorCode MatSolveTranspose_MUMPS(Mat A,Vec b,Vec x)
822 {
823   Mat_MUMPS      *mumps=(Mat_MUMPS*)A->data;
824   PetscErrorCode ierr;
825 
826   PetscFunctionBegin;
827   mumps->id.ICNTL(9) = 0;
828   ierr = MatSolve_MUMPS(A,b,x);CHKERRQ(ierr);
829   mumps->id.ICNTL(9) = 1;
830   PetscFunctionReturn(0);
831 }
832 
833 PetscErrorCode MatMatSolve_MUMPS(Mat A,Mat B,Mat X)
834 {
835   PetscErrorCode ierr;
836   Mat            Bt = NULL;
837   PetscBool      flg, flgT;
838   Mat_MUMPS      *mumps=(Mat_MUMPS*)A->data;
839   PetscInt       i,nrhs,M;
840   PetscScalar    *array,*bray;
841   PetscInt       lsol_loc,nlsol_loc,*isol_loc,*idx,*iidx,*idxx,*isol_loc_save;
842   MumpsScalar    *sol_loc,*sol_loc_save;
843   IS             is_to,is_from;
844   PetscInt       k,proc,j,m;
845   const PetscInt *rstart;
846   Vec            v_mpi,b_seq,x_seq;
847   VecScatter     scat_rhs,scat_sol;
848   PetscScalar    *aa;
849   PetscInt       spnr,*ia,*ja;
850   Mat_MPIAIJ     *b = NULL;
851 
852   PetscFunctionBegin;
853   ierr = PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr);
854   if (!flg) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix");
855 
856   ierr = PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr);
857   if (flg) { /* dense B */
858     if (B->rmap->n != X->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Matrix B and X must have same row distribution");
859     mumps->id.ICNTL(20)= 0; /* dense RHS */
860   } else { /* sparse B */
861     ierr = PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&flgT);CHKERRQ(ierr);
862     if (flgT) { /* input B is transpose of actural RHS matrix,
863                  because mumps requires sparse compressed COLUMN storage! See MatMatTransposeSolve_MUMPS() */
864       ierr = MatTransposeGetMat(B,&Bt);CHKERRQ(ierr);
865     } else SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix");
866     mumps->id.ICNTL(20)= 1; /* sparse RHS */
867   }
868 
869   ierr = MatGetSize(B,&M,&nrhs);CHKERRQ(ierr);
870   mumps->id.nrhs = nrhs;
871   mumps->id.lrhs = M;
872   mumps->id.rhs  = NULL;
873 
874   if (mumps->size == 1) {
875     PetscScalar *aa;
876     PetscInt    spnr,*ia,*ja;
877     PetscBool   second_solve = PETSC_FALSE;
878 
879     ierr = MatDenseGetArray(X,&array);CHKERRQ(ierr);
880     mumps->id.rhs = (MumpsScalar*)array;
881 
882     if (!Bt) { /* dense B */
883       /* copy B to X */
884       ierr = MatDenseGetArray(B,&bray);CHKERRQ(ierr);
885       ierr = PetscMemcpy(array,bray,M*nrhs*sizeof(PetscScalar));CHKERRQ(ierr);
886       ierr = MatDenseRestoreArray(B,&bray);CHKERRQ(ierr);
887     } else { /* sparse B */
888       ierr = MatSeqAIJGetArray(Bt,&aa);CHKERRQ(ierr);
889       ierr = MatGetRowIJ(Bt,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
890       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
891       /* mumps requires ia and ja start at 1! */
892       mumps->id.irhs_ptr    = ia;
893       mumps->id.irhs_sparse = ja;
894       mumps->id.nz_rhs      = ia[spnr] - 1;
895       mumps->id.rhs_sparse  = (MumpsScalar*)aa;
896     }
897     /* handle condensation step of Schur complement (if any) */
898     if (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2) {
899       second_solve = PETSC_TRUE;
900       ierr = MatMumpsHandleSchur_Private(A,PETSC_FALSE);CHKERRQ(ierr);
901     }
902     /* solve phase */
903     /*-------------*/
904     mumps->id.job = JOB_SOLVE;
905     PetscMUMPS_c(&mumps->id);
906     if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in solve phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
907 
908     /* handle expansion step of Schur complement (if any) */
909     if (second_solve) {
910       ierr = MatMumpsHandleSchur_Private(A,PETSC_TRUE);CHKERRQ(ierr);
911     }
912     if (Bt) { /* sparse B */
913       ierr = MatSeqAIJRestoreArray(Bt,&aa);CHKERRQ(ierr);
914       ierr = MatRestoreRowIJ(Bt,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
915       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore IJ structure");
916     }
917     ierr = MatDenseRestoreArray(X,&array);CHKERRQ(ierr);
918     PetscFunctionReturn(0);
919   }
920 
921   /*--------- parallel case: MUMPS requires rhs B to be centralized on the host! --------*/
922   if (mumps->size > 1 && mumps->id.ICNTL(19)) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Parallel Schur complements not yet supported from PETSc\n");
923 
924   /* create x_seq to hold mumps local solution */
925   isol_loc_save = mumps->id.isol_loc; /* save it for MatSovle() */
926   sol_loc_save  = mumps->id.sol_loc;
927 
928   lsol_loc  = mumps->id.INFO(23);
929   nlsol_loc = nrhs*lsol_loc;     /* length of sol_loc */
930   ierr = PetscMalloc2(nlsol_loc,&sol_loc,nlsol_loc,&isol_loc);CHKERRQ(ierr);
931   mumps->id.sol_loc  = (MumpsScalar*)sol_loc;
932   mumps->id.isol_loc = isol_loc;
933 
934   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,nlsol_loc,(PetscScalar*)sol_loc,&x_seq);CHKERRQ(ierr);
935 
936   /* scatter v_mpi to b_seq because MUMPS only supports centralized rhs */
937   /* idx: maps from k-th index of v_mpi to (i,j)-th global entry of B;
938      iidx: inverse of idx, will be used by scattering mumps x_seq -> petsc X */
939   ierr = PetscMalloc1(nrhs*M,&idx);CHKERRQ(ierr);
940 
941   if (!Bt) { /* dense B */
942     /* wrap dense rhs matrix B into a vector v_mpi */
943     ierr = MatGetLocalSize(B,&m,NULL);CHKERRQ(ierr);
944     ierr = MatDenseGetArray(B,&bray);CHKERRQ(ierr);
945     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)B),1,nrhs*m,nrhs*M,(const PetscScalar*)bray,&v_mpi);CHKERRQ(ierr);
946     ierr = MatDenseRestoreArray(B,&bray);CHKERRQ(ierr);
947 
948     /* scatter v_mpi to b_seq in proc[0]. MUMPS requires rhs to be centralized on the host! */
949     if (!mumps->myid) {
950       ierr = MatGetOwnershipRanges(B,&rstart);CHKERRQ(ierr);
951       k = 0;
952       for (proc=0; proc<mumps->size; proc++){
953         for (j=0; j<nrhs; j++){
954           for (i=rstart[proc]; i<rstart[proc+1]; i++){
955             idx[k++]      = j*M + i;
956           }
957         }
958       }
959 
960       ierr = VecCreateSeq(PETSC_COMM_SELF,nrhs*M,&b_seq);CHKERRQ(ierr);
961       ierr = ISCreateGeneral(PETSC_COMM_SELF,nrhs*M,idx,PETSC_COPY_VALUES,&is_to);CHKERRQ(ierr);
962       ierr = ISCreateStride(PETSC_COMM_SELF,nrhs*M,0,1,&is_from);CHKERRQ(ierr);
963     } else {
964       ierr = VecCreateSeq(PETSC_COMM_SELF,0,&b_seq);CHKERRQ(ierr);
965       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_to);CHKERRQ(ierr);
966       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_from);CHKERRQ(ierr);
967     }
968     ierr = VecScatterCreate(v_mpi,is_from,b_seq,is_to,&scat_rhs);CHKERRQ(ierr);
969     ierr = VecScatterBegin(scat_rhs,v_mpi,b_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
970     ierr = ISDestroy(&is_to);CHKERRQ(ierr);
971     ierr = ISDestroy(&is_from);CHKERRQ(ierr);
972     ierr = VecScatterEnd(scat_rhs,v_mpi,b_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
973 
974     if (!mumps->myid) { /* define rhs on the host */
975       ierr = VecGetArray(b_seq,&bray);CHKERRQ(ierr);
976       mumps->id.rhs = (MumpsScalar*)bray;
977       ierr = VecRestoreArray(b_seq,&bray);CHKERRQ(ierr);
978     }
979 
980   } else { /* sparse B */
981     b = (Mat_MPIAIJ*)Bt->data;
982 
983     /* wrap dense X into a vector v_mpi */
984     ierr = MatGetLocalSize(X,&m,NULL);CHKERRQ(ierr);
985     ierr = MatDenseGetArray(X,&bray);CHKERRQ(ierr);
986     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)X),1,nrhs*m,nrhs*M,(const PetscScalar*)bray,&v_mpi);CHKERRQ(ierr);
987     ierr = MatDenseRestoreArray(X,&bray);CHKERRQ(ierr);
988 
989     if (!mumps->myid) {
990       ierr = MatSeqAIJGetArray(b->A,&aa);CHKERRQ(ierr);
991       ierr = MatGetRowIJ(b->A,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
992       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
993       /* mumps requires ia and ja start at 1! */
994       mumps->id.irhs_ptr    = ia;
995       mumps->id.irhs_sparse = ja;
996       mumps->id.nz_rhs      = ia[spnr] - 1;
997       mumps->id.rhs_sparse  = (MumpsScalar*)aa;
998     } else {
999       mumps->id.irhs_ptr    = NULL;
1000       mumps->id.irhs_sparse = NULL;
1001       mumps->id.nz_rhs      = 0;
1002       mumps->id.rhs_sparse  = NULL;
1003     }
1004   }
1005 
1006   /* solve phase */
1007   /*-------------*/
1008   mumps->id.job = JOB_SOLVE;
1009   PetscMUMPS_c(&mumps->id);
1010   if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in solve phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
1011 
1012   /* scatter mumps distributed solution to petsc vector v_mpi, which shares local arrays with solution matrix X */
1013   ierr = MatDenseGetArray(X,&array);CHKERRQ(ierr);
1014   ierr = VecPlaceArray(v_mpi,array);CHKERRQ(ierr);
1015 
1016   /* create scatter scat_sol */
1017   ierr = MatGetOwnershipRanges(X,&rstart);CHKERRQ(ierr);
1018   /* iidx: inverse of idx computed above, used for scattering mumps x_seq to petsc X */
1019   iidx = idx;
1020   k    = 0;
1021   for (proc=0; proc<mumps->size; proc++){
1022     for (j=0; j<nrhs; j++){
1023       for (i=rstart[proc]; i<rstart[proc+1]; i++) iidx[j*M + i] = k++;
1024     }
1025   }
1026 
1027   ierr = PetscMalloc1(nlsol_loc,&idxx);CHKERRQ(ierr);
1028   ierr = ISCreateStride(PETSC_COMM_SELF,nlsol_loc,0,1,&is_from);CHKERRQ(ierr);
1029   for (i=0; i<lsol_loc; i++) {
1030     isol_loc[i] -= 1; /* change Fortran style to C style */
1031     idxx[i] = iidx[isol_loc[i]];
1032     for (j=1; j<nrhs; j++){
1033       idxx[j*lsol_loc+i] = iidx[isol_loc[i]+j*M];
1034     }
1035   }
1036   ierr = ISCreateGeneral(PETSC_COMM_SELF,nlsol_loc,idxx,PETSC_COPY_VALUES,&is_to);CHKERRQ(ierr);
1037   ierr = VecScatterCreate(x_seq,is_from,v_mpi,is_to,&scat_sol);CHKERRQ(ierr);
1038   ierr = VecScatterBegin(scat_sol,x_seq,v_mpi,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1039   ierr = ISDestroy(&is_from);CHKERRQ(ierr);
1040   ierr = ISDestroy(&is_to);CHKERRQ(ierr);
1041   ierr = VecScatterEnd(scat_sol,x_seq,v_mpi,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1042   ierr = MatDenseRestoreArray(X,&array);CHKERRQ(ierr);
1043 
1044   /* free spaces */
1045   mumps->id.sol_loc  = sol_loc_save;
1046   mumps->id.isol_loc = isol_loc_save;
1047 
1048   ierr = PetscFree2(sol_loc,isol_loc);CHKERRQ(ierr);
1049   ierr = PetscFree(idx);CHKERRQ(ierr);
1050   ierr = PetscFree(idxx);CHKERRQ(ierr);
1051   ierr = VecDestroy(&x_seq);CHKERRQ(ierr);
1052   ierr = VecDestroy(&v_mpi);CHKERRQ(ierr);
1053   if (Bt) {
1054     if (!mumps->myid) {
1055       b = (Mat_MPIAIJ*)Bt->data;
1056       ierr = MatSeqAIJRestoreArray(b->A,&aa);CHKERRQ(ierr);
1057       ierr = MatRestoreRowIJ(b->A,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
1058       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore IJ structure");
1059     }
1060   } else {
1061     ierr = VecDestroy(&b_seq);CHKERRQ(ierr);
1062     ierr = VecScatterDestroy(&scat_rhs);CHKERRQ(ierr);
1063   }
1064   ierr = VecScatterDestroy(&scat_sol);CHKERRQ(ierr);
1065   PetscFunctionReturn(0);
1066 }
1067 
1068 PetscErrorCode MatMatTransposeSolve_MUMPS(Mat A,Mat Bt,Mat X)
1069 {
1070   PetscErrorCode ierr;
1071   PetscBool      flg;
1072   Mat            B;
1073 
1074   PetscFunctionBegin;
1075   ierr = PetscObjectTypeCompareAny((PetscObject)Bt,&flg,MATSEQAIJ,MATMPIAIJ,NULL);CHKERRQ(ierr);
1076   if (!flg) SETERRQ(PetscObjectComm((PetscObject)Bt),PETSC_ERR_ARG_WRONG,"Matrix Bt must be MATAIJ matrix");
1077 
1078   /* Create B=Bt^T that uses Bt's data structure */
1079   ierr = MatCreateTranspose(Bt,&B);CHKERRQ(ierr);
1080 
1081   ierr = MatMatSolve_MUMPS(A,B,X);CHKERRQ(ierr);
1082   ierr = MatDestroy(&B);CHKERRQ(ierr);
1083   PetscFunctionReturn(0);
1084 }
1085 
1086 #if !defined(PETSC_USE_COMPLEX)
1087 /*
1088   input:
1089    F:        numeric factor
1090   output:
1091    nneg:     total number of negative pivots
1092    nzero:    total number of zero pivots
1093    npos:     (global dimension of F) - nneg - nzero
1094 */
1095 PetscErrorCode MatGetInertia_SBAIJMUMPS(Mat F,int *nneg,int *nzero,int *npos)
1096 {
1097   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
1098   PetscErrorCode ierr;
1099   PetscMPIInt    size;
1100 
1101   PetscFunctionBegin;
1102   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)F),&size);CHKERRQ(ierr);
1103   /* 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 */
1104   if (size > 1 && mumps->id.ICNTL(13) != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"ICNTL(13)=%d. -mat_mumps_icntl_13 must be set as 1 for correct global matrix inertia\n",mumps->id.INFOG(13));
1105 
1106   if (nneg) *nneg = mumps->id.INFOG(12);
1107   if (nzero || npos) {
1108     if (mumps->id.ICNTL(24) != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"-mat_mumps_icntl_24 must be set as 1 for null pivot row detection");
1109     if (nzero) *nzero = mumps->id.INFOG(28);
1110     if (npos) *npos   = F->rmap->N - (mumps->id.INFOG(12) + mumps->id.INFOG(28));
1111   }
1112   PetscFunctionReturn(0);
1113 }
1114 #endif
1115 
1116 PetscErrorCode MatFactorNumeric_MUMPS(Mat F,Mat A,const MatFactorInfo *info)
1117 {
1118   Mat_MUMPS      *mumps =(Mat_MUMPS*)(F)->data;
1119   PetscErrorCode ierr;
1120   PetscBool      isMPIAIJ;
1121 
1122   PetscFunctionBegin;
1123   if (mumps->id.INFOG(1) < 0) {
1124     if (mumps->id.INFOG(1) == -6) {
1125       ierr = PetscInfo2(A,"MatFactorNumeric is called with singular matrix structure, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1126     }
1127     ierr = PetscInfo2(A,"MatFactorNumeric is called after analysis phase fails, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1128     PetscFunctionReturn(0);
1129   }
1130 
1131   ierr = (*mumps->ConvertToTriples)(A, 1, MAT_REUSE_MATRIX, &mumps->nz, &mumps->irn, &mumps->jcn, &mumps->val);CHKERRQ(ierr);
1132 
1133   /* numerical factorization phase */
1134   /*-------------------------------*/
1135   mumps->id.job = JOB_FACTNUMERIC;
1136   if (!mumps->id.ICNTL(18)) { /* A is centralized */
1137     if (!mumps->myid) {
1138       mumps->id.a = (MumpsScalar*)mumps->val;
1139     }
1140   } else {
1141     mumps->id.a_loc = (MumpsScalar*)mumps->val;
1142   }
1143   PetscMUMPS_c(&mumps->id);
1144   if (mumps->id.INFOG(1) < 0) {
1145     if (A->erroriffailure) {
1146       SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in numerical factorization phase: INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
1147     } else {
1148       if (mumps->id.INFOG(1) == -10) { /* numerically singular matrix */
1149         ierr = PetscInfo2(F,"matrix is numerically singular, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1150         F->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1151       } else if (mumps->id.INFOG(1) == -13) {
1152         ierr = PetscInfo2(F,"MUMPS in numerical factorization phase: INFOG(1)=%d, cannot allocate required memory %d megabytes\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1153         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
1154       } else if (mumps->id.INFOG(1) == -8 || mumps->id.INFOG(1) == -9 || (-16 < mumps->id.INFOG(1) && mumps->id.INFOG(1) < -10) ) {
1155         ierr = PetscInfo2(F,"MUMPS in numerical factorization phase: INFOG(1)=%d, INFO(2)=%d, problem with workarray \n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1156         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
1157       } else {
1158         ierr = PetscInfo2(F,"MUMPS in numerical factorization phase: INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1159         F->factorerrortype = MAT_FACTOR_OTHER;
1160       }
1161     }
1162   }
1163   if (!mumps->myid && mumps->id.ICNTL(16) > 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"  mumps->id.ICNTL(16):=%d\n",mumps->id.INFOG(16));
1164 
1165   F->assembled    = PETSC_TRUE;
1166   mumps->matstruc = SAME_NONZERO_PATTERN;
1167   if (F->schur) { /* reset Schur status to unfactored */
1168     if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
1169       mumps->id.ICNTL(19) = 2;
1170       ierr = MatTranspose(F->schur,MAT_INPLACE_MATRIX,&F->schur);CHKERRQ(ierr);
1171     }
1172     ierr = MatFactorRestoreSchurComplement(F,NULL,MAT_FACTOR_SCHUR_UNFACTORED);CHKERRQ(ierr);
1173   }
1174 
1175   /* just to be sure that ICNTL(19) value returned by a call from MatMumpsGetIcntl is always consistent */
1176   if (!mumps->sym && mumps->id.ICNTL(19) && mumps->id.ICNTL(19) != 1) mumps->id.ICNTL(19) = 3;
1177 
1178   if (mumps->size > 1) {
1179     PetscInt    lsol_loc;
1180     PetscScalar *sol_loc;
1181 
1182     ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);CHKERRQ(ierr);
1183 
1184     /* distributed solution; Create x_seq=sol_loc for repeated use */
1185     if (mumps->x_seq) {
1186       ierr = VecScatterDestroy(&mumps->scat_sol);CHKERRQ(ierr);
1187       ierr = PetscFree2(mumps->id.sol_loc,mumps->id.isol_loc);CHKERRQ(ierr);
1188       ierr = VecDestroy(&mumps->x_seq);CHKERRQ(ierr);
1189     }
1190     lsol_loc = mumps->id.INFO(23); /* length of sol_loc */
1191     ierr = PetscMalloc2(lsol_loc,&sol_loc,lsol_loc,&mumps->id.isol_loc);CHKERRQ(ierr);
1192     mumps->id.lsol_loc = lsol_loc;
1193     mumps->id.sol_loc = (MumpsScalar*)sol_loc;
1194     ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,lsol_loc,sol_loc,&mumps->x_seq);CHKERRQ(ierr);
1195   }
1196   PetscFunctionReturn(0);
1197 }
1198 
1199 /* Sets MUMPS options from the options database */
1200 PetscErrorCode PetscSetMUMPSFromOptions(Mat F, Mat A)
1201 {
1202   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
1203   PetscErrorCode ierr;
1204   PetscInt       icntl,info[40],i,ninfo=40;
1205   PetscBool      flg;
1206 
1207   PetscFunctionBegin;
1208   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MUMPS Options","Mat");CHKERRQ(ierr);
1209   ierr = PetscOptionsInt("-mat_mumps_icntl_1","ICNTL(1): output stream for error messages","None",mumps->id.ICNTL(1),&icntl,&flg);CHKERRQ(ierr);
1210   if (flg) mumps->id.ICNTL(1) = icntl;
1211   ierr = PetscOptionsInt("-mat_mumps_icntl_2","ICNTL(2): output stream for diagnostic printing, statistics, and warning","None",mumps->id.ICNTL(2),&icntl,&flg);CHKERRQ(ierr);
1212   if (flg) mumps->id.ICNTL(2) = icntl;
1213   ierr = PetscOptionsInt("-mat_mumps_icntl_3","ICNTL(3): output stream for global information, collected on the host","None",mumps->id.ICNTL(3),&icntl,&flg);CHKERRQ(ierr);
1214   if (flg) mumps->id.ICNTL(3) = icntl;
1215 
1216   ierr = PetscOptionsInt("-mat_mumps_icntl_4","ICNTL(4): level of printing (0 to 4)","None",mumps->id.ICNTL(4),&icntl,&flg);CHKERRQ(ierr);
1217   if (flg) mumps->id.ICNTL(4) = icntl;
1218   if (mumps->id.ICNTL(4) || PetscLogPrintInfo) mumps->id.ICNTL(3) = 6; /* resume MUMPS default id.ICNTL(3) = 6 */
1219 
1220   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1221   if (flg) mumps->id.ICNTL(6) = icntl;
1222 
1223   ierr = PetscOptionsInt("-mat_mumps_icntl_7","ICNTL(7): computes a symmetric permutation in sequential analysis (0 to 7). 3=Scotch, 4=PORD, 5=Metis","None",mumps->id.ICNTL(7),&icntl,&flg);CHKERRQ(ierr);
1224   if (flg) {
1225     if (icntl== 1 && mumps->size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"pivot order be set by the user in PERM_IN -- not supported by the PETSc/MUMPS interface\n");
1226     else mumps->id.ICNTL(7) = icntl;
1227   }
1228 
1229   ierr = PetscOptionsInt("-mat_mumps_icntl_8","ICNTL(8): scaling strategy (-2 to 8 or 77)","None",mumps->id.ICNTL(8),&mumps->id.ICNTL(8),NULL);CHKERRQ(ierr);
1230   /* ierr = 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);CHKERRQ(ierr); handled by MatSolveTranspose_MUMPS() */
1231   ierr = PetscOptionsInt("-mat_mumps_icntl_10","ICNTL(10): max num of refinements","None",mumps->id.ICNTL(10),&mumps->id.ICNTL(10),NULL);CHKERRQ(ierr);
1232   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1233   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1234   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1235   ierr = PetscOptionsInt("-mat_mumps_icntl_14","ICNTL(14): percentage increase in the estimated working space","None",mumps->id.ICNTL(14),&mumps->id.ICNTL(14),NULL);CHKERRQ(ierr);
1236   ierr = PetscOptionsInt("-mat_mumps_icntl_19","ICNTL(19): computes the Schur complement","None",mumps->id.ICNTL(19),&mumps->id.ICNTL(19),NULL);CHKERRQ(ierr);
1237   if (mumps->id.ICNTL(19) <= 0 || mumps->id.ICNTL(19) > 3) { /* reset any schur data (if any) */
1238     ierr = MatDestroy(&F->schur);CHKERRQ(ierr);
1239     ierr = MatMumpsResetSchur_Private(mumps);CHKERRQ(ierr);
1240   }
1241   /* ierr = PetscOptionsInt("-mat_mumps_icntl_20","ICNTL(20): the format (dense or sparse) of the right-hand sides","None",mumps->id.ICNTL(20),&mumps->id.ICNTL(20),NULL);CHKERRQ(ierr); -- sparse rhs is not supported in PETSc API */
1242   /* ierr = PetscOptionsInt("-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);CHKERRQ(ierr); we only use distributed solution vector */
1243 
1244   ierr = PetscOptionsInt("-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),NULL);CHKERRQ(ierr);
1245   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1246   ierr = PetscOptionsInt("-mat_mumps_icntl_24","ICNTL(24): detection of null pivot rows (0 or 1)","None",mumps->id.ICNTL(24),&mumps->id.ICNTL(24),NULL);CHKERRQ(ierr);
1247   if (mumps->id.ICNTL(24)) {
1248     mumps->id.ICNTL(13) = 1; /* turn-off ScaLAPACK to help with the correct detection of null pivots */
1249   }
1250 
1251   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1252   ierr = PetscOptionsInt("-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);CHKERRQ(ierr);
1253   ierr = PetscOptionsInt("-mat_mumps_icntl_27","ICNTL(27): the blocking size for multiple right-hand sides","None",mumps->id.ICNTL(27),&mumps->id.ICNTL(27),NULL);CHKERRQ(ierr);
1254   ierr = PetscOptionsInt("-mat_mumps_icntl_28","ICNTL(28): use 1 for sequential analysis and ictnl(7) ordering, or 2 for parallel analysis and ictnl(29) ordering","None",mumps->id.ICNTL(28),&mumps->id.ICNTL(28),NULL);CHKERRQ(ierr);
1255   ierr = PetscOptionsInt("-mat_mumps_icntl_29","ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis","None",mumps->id.ICNTL(29),&mumps->id.ICNTL(29),NULL);CHKERRQ(ierr);
1256   /* ierr = PetscOptionsInt("-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);CHKERRQ(ierr); */ /* call MatMumpsGetInverse() directly */
1257   ierr = PetscOptionsInt("-mat_mumps_icntl_31","ICNTL(31): indicates which factors may be discarded during factorization","None",mumps->id.ICNTL(31),&mumps->id.ICNTL(31),NULL);CHKERRQ(ierr);
1258   /* ierr = PetscOptionsInt("-mat_mumps_icntl_32","ICNTL(32): performs the forward elemination of the right-hand sides during factorization","None",mumps->id.ICNTL(32),&mumps->id.ICNTL(32),NULL);CHKERRQ(ierr);  -- not supported by PETSc API */
1259   ierr = PetscOptionsInt("-mat_mumps_icntl_33","ICNTL(33): compute determinant","None",mumps->id.ICNTL(33),&mumps->id.ICNTL(33),NULL);CHKERRQ(ierr);
1260   ierr = PetscOptionsInt("-mat_mumps_icntl_35","ICNTL(35): activates Block Lock Rank (BLR) based factorization","None",mumps->id.ICNTL(35),&mumps->id.ICNTL(35),NULL);CHKERRQ(ierr);
1261 
1262   ierr = PetscOptionsReal("-mat_mumps_cntl_1","CNTL(1): relative pivoting threshold","None",mumps->id.CNTL(1),&mumps->id.CNTL(1),NULL);CHKERRQ(ierr);
1263   ierr = PetscOptionsReal("-mat_mumps_cntl_2","CNTL(2): stopping criterion of refinement","None",mumps->id.CNTL(2),&mumps->id.CNTL(2),NULL);CHKERRQ(ierr);
1264   ierr = PetscOptionsReal("-mat_mumps_cntl_3","CNTL(3): absolute pivoting threshold","None",mumps->id.CNTL(3),&mumps->id.CNTL(3),NULL);CHKERRQ(ierr);
1265   ierr = PetscOptionsReal("-mat_mumps_cntl_4","CNTL(4): value for static pivoting","None",mumps->id.CNTL(4),&mumps->id.CNTL(4),NULL);CHKERRQ(ierr);
1266   ierr = PetscOptionsReal("-mat_mumps_cntl_5","CNTL(5): fixation for null pivots","None",mumps->id.CNTL(5),&mumps->id.CNTL(5),NULL);CHKERRQ(ierr);
1267   ierr = PetscOptionsReal("-mat_mumps_cntl_7","CNTL(7): dropping parameter used during BLR","None",mumps->id.CNTL(7),&mumps->id.CNTL(7),NULL);CHKERRQ(ierr);
1268 
1269   ierr = PetscOptionsString("-mat_mumps_ooc_tmpdir", "out of core directory", "None", mumps->id.ooc_tmpdir, mumps->id.ooc_tmpdir, 256, NULL);CHKERRQ(ierr);
1270 
1271   ierr = PetscOptionsIntArray("-mat_mumps_view_info","request INFO local to each processor","",info,&ninfo,NULL);CHKERRQ(ierr);
1272   if (ninfo) {
1273     if (ninfo > 40) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"number of INFO %d must <= 40\n",ninfo);
1274     ierr = PetscMalloc1(ninfo,&mumps->info);CHKERRQ(ierr);
1275     mumps->ninfo = ninfo;
1276     for (i=0; i<ninfo; i++) {
1277       if (info[i] < 0 || info[i]>40) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"index of INFO %d must between 1 and 40\n",ninfo);
1278       else  mumps->info[i] = info[i];
1279     }
1280   }
1281 
1282   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1283   PetscFunctionReturn(0);
1284 }
1285 
1286 PetscErrorCode PetscInitializeMUMPS(Mat A,Mat_MUMPS *mumps)
1287 {
1288   PetscErrorCode ierr;
1289 
1290   PetscFunctionBegin;
1291   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &mumps->myid);CHKERRQ(ierr);
1292   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&mumps->size);CHKERRQ(ierr);
1293   ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)A),&(mumps->comm_mumps));CHKERRQ(ierr);
1294 
1295   mumps->id.comm_fortran = MPI_Comm_c2f(mumps->comm_mumps);
1296 
1297   mumps->id.job = JOB_INIT;
1298   mumps->id.par = 1;  /* host participates factorizaton and solve */
1299   mumps->id.sym = mumps->sym;
1300   PetscMUMPS_c(&mumps->id);
1301 
1302   mumps->scat_rhs     = NULL;
1303   mumps->scat_sol     = NULL;
1304 
1305   /* set PETSc-MUMPS default options - override MUMPS default */
1306   mumps->id.ICNTL(3) = 0;
1307   mumps->id.ICNTL(4) = 0;
1308   if (mumps->size == 1) {
1309     mumps->id.ICNTL(18) = 0;   /* centralized assembled matrix input */
1310   } else {
1311     mumps->id.ICNTL(18) = 3;   /* distributed assembled matrix input */
1312     mumps->id.ICNTL(20) = 0;   /* rhs is in dense format */
1313     mumps->id.ICNTL(21) = 1;   /* distributed solution */
1314   }
1315 
1316   /* schur */
1317   mumps->id.size_schur      = 0;
1318   mumps->id.listvar_schur   = NULL;
1319   mumps->id.schur           = NULL;
1320   mumps->sizeredrhs         = 0;
1321   mumps->schur_sol          = NULL;
1322   mumps->schur_sizesol      = 0;
1323   PetscFunctionReturn(0);
1324 }
1325 
1326 PetscErrorCode MatFactorSymbolic_MUMPS_ReportIfError(Mat F,Mat A,const MatFactorInfo *info,Mat_MUMPS *mumps)
1327 {
1328   PetscErrorCode ierr;
1329 
1330   PetscFunctionBegin;
1331   if (mumps->id.INFOG(1) < 0) {
1332     if (A->erroriffailure) {
1333       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in analysis phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
1334     } else {
1335       if (mumps->id.INFOG(1) == -6) {
1336         ierr = PetscInfo2(F,"matrix is singular in structure, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1337         F->factorerrortype = MAT_FACTOR_STRUCT_ZEROPIVOT;
1338       } else if (mumps->id.INFOG(1) == -5 || mumps->id.INFOG(1) == -7) {
1339         ierr = PetscInfo2(F,"problem of workspace, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1340         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
1341       } else {
1342         ierr = PetscInfo2(F,"Error reported by MUMPS in analysis phase: INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));CHKERRQ(ierr);
1343         F->factorerrortype = MAT_FACTOR_OTHER;
1344       }
1345     }
1346   }
1347   PetscFunctionReturn(0);
1348 }
1349 
1350 /* Note Petsc r(=c) permutation is used when mumps->id.ICNTL(7)==1 with centralized assembled matrix input; otherwise r and c are ignored */
1351 PetscErrorCode MatLUFactorSymbolic_AIJMUMPS(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
1352 {
1353   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
1354   PetscErrorCode ierr;
1355   Vec            b;
1356   IS             is_iden;
1357   const PetscInt M = A->rmap->N;
1358 
1359   PetscFunctionBegin;
1360   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
1361 
1362   /* Set MUMPS options from the options database */
1363   ierr = PetscSetMUMPSFromOptions(F,A);CHKERRQ(ierr);
1364 
1365   ierr = (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, &mumps->nz, &mumps->irn, &mumps->jcn, &mumps->val);CHKERRQ(ierr);
1366 
1367   /* analysis phase */
1368   /*----------------*/
1369   mumps->id.job = JOB_FACTSYMBOLIC;
1370   mumps->id.n   = M;
1371   switch (mumps->id.ICNTL(18)) {
1372   case 0:  /* centralized assembled matrix input */
1373     if (!mumps->myid) {
1374       mumps->id.nz =mumps->nz; mumps->id.irn=mumps->irn; mumps->id.jcn=mumps->jcn;
1375       if (mumps->id.ICNTL(6)>1) {
1376         mumps->id.a = (MumpsScalar*)mumps->val;
1377       }
1378       if (mumps->id.ICNTL(7) == 1) { /* use user-provide matrix ordering - assuming r = c ordering */
1379         /*
1380         PetscBool      flag;
1381         ierr = ISEqual(r,c,&flag);CHKERRQ(ierr);
1382         if (!flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"row_perm != col_perm");
1383         ierr = ISView(r,PETSC_VIEWER_STDOUT_SELF);
1384          */
1385         if (!mumps->myid) {
1386           const PetscInt *idx;
1387           PetscInt       i,*perm_in;
1388 
1389           ierr = PetscMalloc1(M,&perm_in);CHKERRQ(ierr);
1390           ierr = ISGetIndices(r,&idx);CHKERRQ(ierr);
1391 
1392           mumps->id.perm_in = perm_in;
1393           for (i=0; i<M; i++) perm_in[i] = idx[i]+1; /* perm_in[]: start from 1, not 0! */
1394           ierr = ISRestoreIndices(r,&idx);CHKERRQ(ierr);
1395         }
1396       }
1397     }
1398     break;
1399   case 3:  /* distributed assembled matrix input (size>1) */
1400     mumps->id.nz_loc = mumps->nz;
1401     mumps->id.irn_loc=mumps->irn; mumps->id.jcn_loc=mumps->jcn;
1402     if (mumps->id.ICNTL(6)>1) {
1403       mumps->id.a_loc = (MumpsScalar*)mumps->val;
1404     }
1405     /* MUMPS only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
1406     if (!mumps->myid) {
1407       ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->N,&mumps->b_seq);CHKERRQ(ierr);
1408       ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->N,0,1,&is_iden);CHKERRQ(ierr);
1409     } else {
1410       ierr = VecCreateSeq(PETSC_COMM_SELF,0,&mumps->b_seq);CHKERRQ(ierr);
1411       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_iden);CHKERRQ(ierr);
1412     }
1413     ierr = MatCreateVecs(A,NULL,&b);CHKERRQ(ierr);
1414     ierr = VecScatterCreate(b,is_iden,mumps->b_seq,is_iden,&mumps->scat_rhs);CHKERRQ(ierr);
1415     ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
1416     ierr = VecDestroy(&b);CHKERRQ(ierr);
1417     break;
1418   }
1419   PetscMUMPS_c(&mumps->id);
1420   ierr = MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);CHKERRQ(ierr);
1421 
1422   F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
1423   F->ops->solve           = MatSolve_MUMPS;
1424   F->ops->solvetranspose  = MatSolveTranspose_MUMPS;
1425   F->ops->matsolve        = MatMatSolve_MUMPS;
1426   F->ops->mattransposesolve = MatMatTransposeSolve_MUMPS;
1427   PetscFunctionReturn(0);
1428 }
1429 
1430 /* Note the Petsc r and c permutations are ignored */
1431 PetscErrorCode MatLUFactorSymbolic_BAIJMUMPS(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
1432 {
1433   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
1434   PetscErrorCode ierr;
1435   Vec            b;
1436   IS             is_iden;
1437   const PetscInt M = A->rmap->N;
1438 
1439   PetscFunctionBegin;
1440   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
1441 
1442   /* Set MUMPS options from the options database */
1443   ierr = PetscSetMUMPSFromOptions(F,A);CHKERRQ(ierr);
1444 
1445   ierr = (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, &mumps->nz, &mumps->irn, &mumps->jcn, &mumps->val);CHKERRQ(ierr);
1446 
1447   /* analysis phase */
1448   /*----------------*/
1449   mumps->id.job = JOB_FACTSYMBOLIC;
1450   mumps->id.n   = M;
1451   switch (mumps->id.ICNTL(18)) {
1452   case 0:  /* centralized assembled matrix input */
1453     if (!mumps->myid) {
1454       mumps->id.nz =mumps->nz; mumps->id.irn=mumps->irn; mumps->id.jcn=mumps->jcn;
1455       if (mumps->id.ICNTL(6)>1) {
1456         mumps->id.a = (MumpsScalar*)mumps->val;
1457       }
1458     }
1459     break;
1460   case 3:  /* distributed assembled matrix input (size>1) */
1461     mumps->id.nz_loc = mumps->nz;
1462     mumps->id.irn_loc=mumps->irn; mumps->id.jcn_loc=mumps->jcn;
1463     if (mumps->id.ICNTL(6)>1) {
1464       mumps->id.a_loc = (MumpsScalar*)mumps->val;
1465     }
1466     /* MUMPS only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
1467     if (!mumps->myid) {
1468       ierr = VecCreateSeq(PETSC_COMM_SELF,A->cmap->N,&mumps->b_seq);CHKERRQ(ierr);
1469       ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,&is_iden);CHKERRQ(ierr);
1470     } else {
1471       ierr = VecCreateSeq(PETSC_COMM_SELF,0,&mumps->b_seq);CHKERRQ(ierr);
1472       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_iden);CHKERRQ(ierr);
1473     }
1474     ierr = MatCreateVecs(A,NULL,&b);CHKERRQ(ierr);
1475     ierr = VecScatterCreate(b,is_iden,mumps->b_seq,is_iden,&mumps->scat_rhs);CHKERRQ(ierr);
1476     ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
1477     ierr = VecDestroy(&b);CHKERRQ(ierr);
1478     break;
1479   }
1480   PetscMUMPS_c(&mumps->id);
1481   ierr = MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);CHKERRQ(ierr);
1482 
1483   F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
1484   F->ops->solve           = MatSolve_MUMPS;
1485   F->ops->solvetranspose  = MatSolveTranspose_MUMPS;
1486   PetscFunctionReturn(0);
1487 }
1488 
1489 /* Note the Petsc r permutation and factor info are ignored */
1490 PetscErrorCode MatCholeskyFactorSymbolic_MUMPS(Mat F,Mat A,IS r,const MatFactorInfo *info)
1491 {
1492   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
1493   PetscErrorCode ierr;
1494   Vec            b;
1495   IS             is_iden;
1496   const PetscInt M = A->rmap->N;
1497 
1498   PetscFunctionBegin;
1499   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;
1500 
1501   /* Set MUMPS options from the options database */
1502   ierr = PetscSetMUMPSFromOptions(F,A);CHKERRQ(ierr);
1503 
1504   ierr = (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, &mumps->nz, &mumps->irn, &mumps->jcn, &mumps->val);CHKERRQ(ierr);
1505 
1506   /* analysis phase */
1507   /*----------------*/
1508   mumps->id.job = JOB_FACTSYMBOLIC;
1509   mumps->id.n   = M;
1510   switch (mumps->id.ICNTL(18)) {
1511   case 0:  /* centralized assembled matrix input */
1512     if (!mumps->myid) {
1513       mumps->id.nz =mumps->nz; mumps->id.irn=mumps->irn; mumps->id.jcn=mumps->jcn;
1514       if (mumps->id.ICNTL(6)>1) {
1515         mumps->id.a = (MumpsScalar*)mumps->val;
1516       }
1517     }
1518     break;
1519   case 3:  /* distributed assembled matrix input (size>1) */
1520     mumps->id.nz_loc = mumps->nz;
1521     mumps->id.irn_loc=mumps->irn; mumps->id.jcn_loc=mumps->jcn;
1522     if (mumps->id.ICNTL(6)>1) {
1523       mumps->id.a_loc = (MumpsScalar*)mumps->val;
1524     }
1525     /* MUMPS only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
1526     if (!mumps->myid) {
1527       ierr = VecCreateSeq(PETSC_COMM_SELF,A->cmap->N,&mumps->b_seq);CHKERRQ(ierr);
1528       ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,&is_iden);CHKERRQ(ierr);
1529     } else {
1530       ierr = VecCreateSeq(PETSC_COMM_SELF,0,&mumps->b_seq);CHKERRQ(ierr);
1531       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_iden);CHKERRQ(ierr);
1532     }
1533     ierr = MatCreateVecs(A,NULL,&b);CHKERRQ(ierr);
1534     ierr = VecScatterCreate(b,is_iden,mumps->b_seq,is_iden,&mumps->scat_rhs);CHKERRQ(ierr);
1535     ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
1536     ierr = VecDestroy(&b);CHKERRQ(ierr);
1537     break;
1538   }
1539   PetscMUMPS_c(&mumps->id);
1540   ierr = MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);CHKERRQ(ierr);
1541 
1542   F->ops->choleskyfactornumeric = MatFactorNumeric_MUMPS;
1543   F->ops->solve                 = MatSolve_MUMPS;
1544   F->ops->solvetranspose        = MatSolve_MUMPS;
1545   F->ops->matsolve              = MatMatSolve_MUMPS;
1546 #if defined(PETSC_USE_COMPLEX)
1547   F->ops->getinertia = NULL;
1548 #else
1549   F->ops->getinertia = MatGetInertia_SBAIJMUMPS;
1550 #endif
1551   PetscFunctionReturn(0);
1552 }
1553 
1554 PetscErrorCode MatView_MUMPS(Mat A,PetscViewer viewer)
1555 {
1556   PetscErrorCode    ierr;
1557   PetscBool         iascii;
1558   PetscViewerFormat format;
1559   Mat_MUMPS         *mumps=(Mat_MUMPS*)A->data;
1560 
1561   PetscFunctionBegin;
1562   /* check if matrix is mumps type */
1563   if (A->ops->solve != MatSolve_MUMPS) PetscFunctionReturn(0);
1564 
1565   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1566   if (iascii) {
1567     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1568     if (format == PETSC_VIEWER_ASCII_INFO) {
1569       ierr = PetscViewerASCIIPrintf(viewer,"MUMPS run parameters:\n");CHKERRQ(ierr);
1570       ierr = PetscViewerASCIIPrintf(viewer,"  SYM (matrix type):                   %d \n",mumps->id.sym);CHKERRQ(ierr);
1571       ierr = PetscViewerASCIIPrintf(viewer,"  PAR (host participation):            %d \n",mumps->id.par);CHKERRQ(ierr);
1572       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(1) (output for error):         %d \n",mumps->id.ICNTL(1));CHKERRQ(ierr);
1573       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(2) (output of diagnostic msg): %d \n",mumps->id.ICNTL(2));CHKERRQ(ierr);
1574       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(3) (output for global info):   %d \n",mumps->id.ICNTL(3));CHKERRQ(ierr);
1575       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(4) (level of printing):        %d \n",mumps->id.ICNTL(4));CHKERRQ(ierr);
1576       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(5) (input mat struct):         %d \n",mumps->id.ICNTL(5));CHKERRQ(ierr);
1577       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(6) (matrix prescaling):        %d \n",mumps->id.ICNTL(6));CHKERRQ(ierr);
1578       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(7) (sequential matrix ordering):%d \n",mumps->id.ICNTL(7));CHKERRQ(ierr);
1579       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(8) (scaling strategy):        %d \n",mumps->id.ICNTL(8));CHKERRQ(ierr);
1580       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(10) (max num of refinements):  %d \n",mumps->id.ICNTL(10));CHKERRQ(ierr);
1581       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(11) (error analysis):          %d \n",mumps->id.ICNTL(11));CHKERRQ(ierr);
1582       if (mumps->id.ICNTL(11)>0) {
1583         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(4) (inf norm of input mat):        %g\n",mumps->id.RINFOG(4));CHKERRQ(ierr);
1584         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(5) (inf norm of solution):         %g\n",mumps->id.RINFOG(5));CHKERRQ(ierr);
1585         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(6) (inf norm of residual):         %g\n",mumps->id.RINFOG(6));CHKERRQ(ierr);
1586         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(7),RINFOG(8) (backward error est): %g, %g\n",mumps->id.RINFOG(7),mumps->id.RINFOG(8));CHKERRQ(ierr);
1587         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(9) (error estimate):               %g \n",mumps->id.RINFOG(9));CHKERRQ(ierr);
1588         ierr = PetscViewerASCIIPrintf(viewer,"    RINFOG(10),RINFOG(11)(condition numbers): %g, %g\n",mumps->id.RINFOG(10),mumps->id.RINFOG(11));CHKERRQ(ierr);
1589       }
1590       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(12) (efficiency control):                         %d \n",mumps->id.ICNTL(12));CHKERRQ(ierr);
1591       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(13) (efficiency control):                         %d \n",mumps->id.ICNTL(13));CHKERRQ(ierr);
1592       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(14) (percentage of estimated workspace increase): %d \n",mumps->id.ICNTL(14));CHKERRQ(ierr);
1593       /* ICNTL(15-17) not used */
1594       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(18) (input mat struct):                           %d \n",mumps->id.ICNTL(18));CHKERRQ(ierr);
1595       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(19) (Schur complement info):                       %d \n",mumps->id.ICNTL(19));CHKERRQ(ierr);
1596       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(20) (rhs sparse pattern):                         %d \n",mumps->id.ICNTL(20));CHKERRQ(ierr);
1597       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(21) (solution struct):                            %d \n",mumps->id.ICNTL(21));CHKERRQ(ierr);
1598       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(22) (in-core/out-of-core facility):               %d \n",mumps->id.ICNTL(22));CHKERRQ(ierr);
1599       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(23) (max size of memory can be allocated locally):%d \n",mumps->id.ICNTL(23));CHKERRQ(ierr);
1600 
1601       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(24) (detection of null pivot rows):               %d \n",mumps->id.ICNTL(24));CHKERRQ(ierr);
1602       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(25) (computation of a null space basis):          %d \n",mumps->id.ICNTL(25));CHKERRQ(ierr);
1603       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(26) (Schur options for rhs or solution):          %d \n",mumps->id.ICNTL(26));CHKERRQ(ierr);
1604       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(27) (experimental parameter):                     %d \n",mumps->id.ICNTL(27));CHKERRQ(ierr);
1605       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(28) (use parallel or sequential ordering):        %d \n",mumps->id.ICNTL(28));CHKERRQ(ierr);
1606       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(29) (parallel ordering):                          %d \n",mumps->id.ICNTL(29));CHKERRQ(ierr);
1607 
1608       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(30) (user-specified set of entries in inv(A)):    %d \n",mumps->id.ICNTL(30));CHKERRQ(ierr);
1609       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(31) (factors is discarded in the solve phase):    %d \n",mumps->id.ICNTL(31));CHKERRQ(ierr);
1610       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(33) (compute determinant):                        %d \n",mumps->id.ICNTL(33));CHKERRQ(ierr);
1611       ierr = PetscViewerASCIIPrintf(viewer,"  ICNTL(35) (activate BLR based factorization):           %d \n",mumps->id.ICNTL(35));CHKERRQ(ierr);
1612 
1613       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(1) (relative pivoting threshold):      %g \n",mumps->id.CNTL(1));CHKERRQ(ierr);
1614       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(2) (stopping criterion of refinement): %g \n",mumps->id.CNTL(2));CHKERRQ(ierr);
1615       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(3) (absolute pivoting threshold):      %g \n",mumps->id.CNTL(3));CHKERRQ(ierr);
1616       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(4) (value of static pivoting):         %g \n",mumps->id.CNTL(4));CHKERRQ(ierr);
1617       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(5) (fixation for null pivots):         %g \n",mumps->id.CNTL(5));CHKERRQ(ierr);
1618       ierr = PetscViewerASCIIPrintf(viewer,"  CNTL(7) (dropping parameter for BLR):       %g \n",mumps->id.CNTL(7));CHKERRQ(ierr);
1619 
1620       /* infomation local to each processor */
1621       ierr = PetscViewerASCIIPrintf(viewer, "  RINFO(1) (local estimated flops for the elimination after analysis): \n");CHKERRQ(ierr);
1622       ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
1623       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %g \n",mumps->myid,mumps->id.RINFO(1));CHKERRQ(ierr);
1624       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1625       ierr = PetscViewerASCIIPrintf(viewer, "  RINFO(2) (local estimated flops for the assembly after factorization): \n");CHKERRQ(ierr);
1626       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d]  %g \n",mumps->myid,mumps->id.RINFO(2));CHKERRQ(ierr);
1627       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1628       ierr = PetscViewerASCIIPrintf(viewer, "  RINFO(3) (local estimated flops for the elimination after factorization): \n");CHKERRQ(ierr);
1629       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d]  %g \n",mumps->myid,mumps->id.RINFO(3));CHKERRQ(ierr);
1630       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1631 
1632       ierr = PetscViewerASCIIPrintf(viewer, "  INFO(15) (estimated size of (in MB) MUMPS internal data for running numerical factorization): \n");CHKERRQ(ierr);
1633       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] %d \n",mumps->myid,mumps->id.INFO(15));CHKERRQ(ierr);
1634       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1635 
1636       ierr = PetscViewerASCIIPrintf(viewer, "  INFO(16) (size of (in MB) MUMPS internal data used during numerical factorization): \n");CHKERRQ(ierr);
1637       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d \n",mumps->myid,mumps->id.INFO(16));CHKERRQ(ierr);
1638       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1639 
1640       ierr = PetscViewerASCIIPrintf(viewer, "  INFO(23) (num of pivots eliminated on this processor after factorization): \n");CHKERRQ(ierr);
1641       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d \n",mumps->myid,mumps->id.INFO(23));CHKERRQ(ierr);
1642       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1643 
1644       if (mumps->ninfo && mumps->ninfo <= 40){
1645         PetscInt i;
1646         for (i=0; i<mumps->ninfo; i++){
1647           ierr = PetscViewerASCIIPrintf(viewer, "  INFO(%d): \n",mumps->info[i]);CHKERRQ(ierr);
1648           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d \n",mumps->myid,mumps->id.INFO(mumps->info[i]));CHKERRQ(ierr);
1649           ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1650         }
1651       }
1652 
1653 
1654       ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
1655 
1656       if (!mumps->myid) { /* information from the host */
1657         ierr = PetscViewerASCIIPrintf(viewer,"  RINFOG(1) (global estimated flops for the elimination after analysis): %g \n",mumps->id.RINFOG(1));CHKERRQ(ierr);
1658         ierr = PetscViewerASCIIPrintf(viewer,"  RINFOG(2) (global estimated flops for the assembly after factorization): %g \n",mumps->id.RINFOG(2));CHKERRQ(ierr);
1659         ierr = PetscViewerASCIIPrintf(viewer,"  RINFOG(3) (global estimated flops for the elimination after factorization): %g \n",mumps->id.RINFOG(3));CHKERRQ(ierr);
1660         ierr = PetscViewerASCIIPrintf(viewer,"  (RINFOG(12) RINFOG(13))*2^INFOG(34) (determinant): (%g,%g)*(2^%d)\n",mumps->id.RINFOG(12),mumps->id.RINFOG(13),mumps->id.INFOG(34));CHKERRQ(ierr);
1661 
1662         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(3) (estimated real workspace for factors on all processors after analysis): %d \n",mumps->id.INFOG(3));CHKERRQ(ierr);
1663         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(4) (estimated integer workspace for factors on all processors after analysis): %d \n",mumps->id.INFOG(4));CHKERRQ(ierr);
1664         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(5) (estimated maximum front size in the complete tree): %d \n",mumps->id.INFOG(5));CHKERRQ(ierr);
1665         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(6) (number of nodes in the complete tree): %d \n",mumps->id.INFOG(6));CHKERRQ(ierr);
1666         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(7) (ordering option effectively use after analysis): %d \n",mumps->id.INFOG(7));CHKERRQ(ierr);
1667         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(8) (structural symmetry in percent of the permuted matrix after analysis): %d \n",mumps->id.INFOG(8));CHKERRQ(ierr);
1668         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(9) (total real/complex workspace to store the matrix factors after factorization): %d \n",mumps->id.INFOG(9));CHKERRQ(ierr);
1669         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(10) (total integer space store the matrix factors after factorization): %d \n",mumps->id.INFOG(10));CHKERRQ(ierr);
1670         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(11) (order of largest frontal matrix after factorization): %d \n",mumps->id.INFOG(11));CHKERRQ(ierr);
1671         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(12) (number of off-diagonal pivots): %d \n",mumps->id.INFOG(12));CHKERRQ(ierr);
1672         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(13) (number of delayed pivots after factorization): %d \n",mumps->id.INFOG(13));CHKERRQ(ierr);
1673         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(14) (number of memory compress after factorization): %d \n",mumps->id.INFOG(14));CHKERRQ(ierr);
1674         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(15) (number of steps of iterative refinement after solution): %d \n",mumps->id.INFOG(15));CHKERRQ(ierr);
1675         ierr = 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));CHKERRQ(ierr);
1676         ierr = 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));CHKERRQ(ierr);
1677         ierr = 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));CHKERRQ(ierr);
1678         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(19) (size of all MUMPS internal data allocated during factorization: sum over all processors): %d \n",mumps->id.INFOG(19));CHKERRQ(ierr);
1679         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(20) (estimated number of entries in the factors): %d \n",mumps->id.INFOG(20));CHKERRQ(ierr);
1680         ierr = 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));CHKERRQ(ierr);
1681         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(22) (size in MB of memory effectively used during factorization - sum over all processors): %d \n",mumps->id.INFOG(22));CHKERRQ(ierr);
1682         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(23) (after analysis: value of ICNTL(6) effectively used): %d \n",mumps->id.INFOG(23));CHKERRQ(ierr);
1683         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(24) (after analysis: value of ICNTL(12) effectively used): %d \n",mumps->id.INFOG(24));CHKERRQ(ierr);
1684         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(25) (after factorization: number of pivots modified by static pivoting): %d \n",mumps->id.INFOG(25));CHKERRQ(ierr);
1685         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(28) (after factorization: number of null pivots encountered): %d\n",mumps->id.INFOG(28));CHKERRQ(ierr);
1686         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(29) (after factorization: effective number of entries in the factors (sum over all processors)): %d\n",mumps->id.INFOG(29));CHKERRQ(ierr);
1687         ierr = 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));CHKERRQ(ierr);
1688         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(32) (after analysis: type of analysis done): %d\n",mumps->id.INFOG(32));CHKERRQ(ierr);
1689         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(33) (value used for ICNTL(8)): %d\n",mumps->id.INFOG(33));CHKERRQ(ierr);
1690         ierr = PetscViewerASCIIPrintf(viewer,"  INFOG(34) (exponent of the determinant if determinant is requested): %d\n",mumps->id.INFOG(34));CHKERRQ(ierr);
1691       }
1692     }
1693   }
1694   PetscFunctionReturn(0);
1695 }
1696 
1697 PetscErrorCode MatGetInfo_MUMPS(Mat A,MatInfoType flag,MatInfo *info)
1698 {
1699   Mat_MUMPS *mumps =(Mat_MUMPS*)A->data;
1700 
1701   PetscFunctionBegin;
1702   info->block_size        = 1.0;
1703   info->nz_allocated      = mumps->id.INFOG(20);
1704   info->nz_used           = mumps->id.INFOG(20);
1705   info->nz_unneeded       = 0.0;
1706   info->assemblies        = 0.0;
1707   info->mallocs           = 0.0;
1708   info->memory            = 0.0;
1709   info->fill_ratio_given  = 0;
1710   info->fill_ratio_needed = 0;
1711   info->factor_mallocs    = 0;
1712   PetscFunctionReturn(0);
1713 }
1714 
1715 /* -------------------------------------------------------------------------------------------*/
1716 PetscErrorCode MatFactorSetSchurIS_MUMPS(Mat F, IS is)
1717 {
1718   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
1719   const PetscInt *idxs;
1720   PetscInt       size,i;
1721   PetscErrorCode ierr;
1722 
1723   PetscFunctionBegin;
1724   ierr = ISGetLocalSize(is,&size);CHKERRQ(ierr);
1725   if (mumps->size > 1) {
1726     PetscBool ls,gs;
1727 
1728     ls   = mumps->myid ? (size ? PETSC_FALSE : PETSC_TRUE) : PETSC_TRUE;
1729     ierr = MPI_Allreduce(&ls,&gs,1,MPIU_BOOL,MPI_LAND,mumps->comm_mumps);CHKERRQ(ierr);
1730     if (!gs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MUMPS distributed parallel Schur complements not yet supported from PETSc\n");
1731   }
1732   if (mumps->id.size_schur != size) {
1733     ierr = PetscFree2(mumps->id.listvar_schur,mumps->id.schur);CHKERRQ(ierr);
1734     mumps->id.size_schur = size;
1735     mumps->id.schur_lld  = size;
1736     ierr = PetscMalloc2(size,&mumps->id.listvar_schur,size*size,&mumps->id.schur);CHKERRQ(ierr);
1737   }
1738 
1739   /* Schur complement matrix */
1740   ierr = MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.size_schur,(PetscScalar*)mumps->id.schur,&F->schur);CHKERRQ(ierr);
1741   if (mumps->sym == 1) {
1742     ierr = MatSetOption(F->schur,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr);
1743   }
1744 
1745   /* MUMPS expects Fortran style indices */
1746   ierr = ISGetIndices(is,&idxs);CHKERRQ(ierr);
1747   ierr = PetscMemcpy(mumps->id.listvar_schur,idxs,size*sizeof(PetscInt));CHKERRQ(ierr);
1748   for (i=0;i<size;i++) mumps->id.listvar_schur[i]++;
1749   ierr = ISRestoreIndices(is,&idxs);CHKERRQ(ierr);
1750   if (mumps->size > 1) {
1751     mumps->id.ICNTL(19) = 1; /* MUMPS returns Schur centralized on the host */
1752   } else {
1753     if (F->factortype == MAT_FACTOR_LU) {
1754       mumps->id.ICNTL(19) = 3; /* MUMPS returns full matrix */
1755     } else {
1756       mumps->id.ICNTL(19) = 2; /* MUMPS returns lower triangular part */
1757     }
1758   }
1759   /* set a special value of ICNTL (not handled my MUMPS) to be used in the solve phase by PETSc */
1760   mumps->id.ICNTL(26) = -1;
1761   PetscFunctionReturn(0);
1762 }
1763 
1764 /* -------------------------------------------------------------------------------------------*/
1765 PetscErrorCode MatFactorCreateSchurComplement_MUMPS(Mat F,Mat* S)
1766 {
1767   Mat            St;
1768   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
1769   PetscScalar    *array;
1770 #if defined(PETSC_USE_COMPLEX)
1771   PetscScalar    im = PetscSqrtScalar((PetscScalar)-1.0);
1772 #endif
1773   PetscErrorCode ierr;
1774 
1775   PetscFunctionBegin;
1776   if (!mumps->id.ICNTL(19)) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ORDER,"Schur complement mode not selected! You should call MatFactorSetSchurIS to enable it");
1777   ierr = MatCreate(PETSC_COMM_SELF,&St);CHKERRQ(ierr);
1778   ierr = MatSetSizes(St,PETSC_DECIDE,PETSC_DECIDE,mumps->id.size_schur,mumps->id.size_schur);CHKERRQ(ierr);
1779   ierr = MatSetType(St,MATDENSE);CHKERRQ(ierr);
1780   ierr = MatSetUp(St);CHKERRQ(ierr);
1781   ierr = MatDenseGetArray(St,&array);CHKERRQ(ierr);
1782   if (!mumps->sym) { /* MUMPS always return a full matrix */
1783     if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
1784       PetscInt i,j,N=mumps->id.size_schur;
1785       for (i=0;i<N;i++) {
1786         for (j=0;j<N;j++) {
1787 #if !defined(PETSC_USE_COMPLEX)
1788           PetscScalar val = mumps->id.schur[i*N+j];
1789 #else
1790           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
1791 #endif
1792           array[j*N+i] = val;
1793         }
1794       }
1795     } else { /* stored by columns */
1796       ierr = PetscMemcpy(array,mumps->id.schur,mumps->id.size_schur*mumps->id.size_schur*sizeof(PetscScalar));CHKERRQ(ierr);
1797     }
1798   } else { /* either full or lower-triangular (not packed) */
1799     if (mumps->id.ICNTL(19) == 2) { /* lower triangular stored by columns */
1800       PetscInt i,j,N=mumps->id.size_schur;
1801       for (i=0;i<N;i++) {
1802         for (j=i;j<N;j++) {
1803 #if !defined(PETSC_USE_COMPLEX)
1804           PetscScalar val = mumps->id.schur[i*N+j];
1805 #else
1806           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
1807 #endif
1808           array[i*N+j] = val;
1809           array[j*N+i] = val;
1810         }
1811       }
1812     } else if (mumps->id.ICNTL(19) == 3) { /* full matrix */
1813       ierr = PetscMemcpy(array,mumps->id.schur,mumps->id.size_schur*mumps->id.size_schur*sizeof(PetscScalar));CHKERRQ(ierr);
1814     } else { /* ICNTL(19) == 1 lower triangular stored by rows */
1815       PetscInt i,j,N=mumps->id.size_schur;
1816       for (i=0;i<N;i++) {
1817         for (j=0;j<i+1;j++) {
1818 #if !defined(PETSC_USE_COMPLEX)
1819           PetscScalar val = mumps->id.schur[i*N+j];
1820 #else
1821           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
1822 #endif
1823           array[i*N+j] = val;
1824           array[j*N+i] = val;
1825         }
1826       }
1827     }
1828   }
1829   ierr = MatDenseRestoreArray(St,&array);CHKERRQ(ierr);
1830   *S   = St;
1831   PetscFunctionReturn(0);
1832 }
1833 
1834 /* -------------------------------------------------------------------------------------------*/
1835 PetscErrorCode MatMumpsSetIcntl_MUMPS(Mat F,PetscInt icntl,PetscInt ival)
1836 {
1837   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
1838 
1839   PetscFunctionBegin;
1840   mumps->id.ICNTL(icntl) = ival;
1841   PetscFunctionReturn(0);
1842 }
1843 
1844 PetscErrorCode MatMumpsGetIcntl_MUMPS(Mat F,PetscInt icntl,PetscInt *ival)
1845 {
1846   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
1847 
1848   PetscFunctionBegin;
1849   *ival = mumps->id.ICNTL(icntl);
1850   PetscFunctionReturn(0);
1851 }
1852 
1853 /*@
1854   MatMumpsSetIcntl - Set MUMPS parameter ICNTL()
1855 
1856    Logically Collective on Mat
1857 
1858    Input Parameters:
1859 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
1860 .  icntl - index of MUMPS parameter array ICNTL()
1861 -  ival - value of MUMPS ICNTL(icntl)
1862 
1863   Options Database:
1864 .   -mat_mumps_icntl_<icntl> <ival>
1865 
1866    Level: beginner
1867 
1868    References:
1869 .     MUMPS Users' Guide
1870 
1871 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
1872  @*/
1873 PetscErrorCode MatMumpsSetIcntl(Mat F,PetscInt icntl,PetscInt ival)
1874 {
1875   PetscErrorCode ierr;
1876 
1877   PetscFunctionBegin;
1878   PetscValidType(F,1);
1879   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
1880   PetscValidLogicalCollectiveInt(F,icntl,2);
1881   PetscValidLogicalCollectiveInt(F,ival,3);
1882   ierr = PetscTryMethod(F,"MatMumpsSetIcntl_C",(Mat,PetscInt,PetscInt),(F,icntl,ival));CHKERRQ(ierr);
1883   PetscFunctionReturn(0);
1884 }
1885 
1886 /*@
1887   MatMumpsGetIcntl - Get MUMPS parameter ICNTL()
1888 
1889    Logically Collective on Mat
1890 
1891    Input Parameters:
1892 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
1893 -  icntl - index of MUMPS parameter array ICNTL()
1894 
1895   Output Parameter:
1896 .  ival - value of MUMPS ICNTL(icntl)
1897 
1898    Level: beginner
1899 
1900    References:
1901 .     MUMPS Users' Guide
1902 
1903 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
1904 @*/
1905 PetscErrorCode MatMumpsGetIcntl(Mat F,PetscInt icntl,PetscInt *ival)
1906 {
1907   PetscErrorCode ierr;
1908 
1909   PetscFunctionBegin;
1910   PetscValidType(F,1);
1911   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
1912   PetscValidLogicalCollectiveInt(F,icntl,2);
1913   PetscValidIntPointer(ival,3);
1914   ierr = PetscUseMethod(F,"MatMumpsGetIcntl_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));CHKERRQ(ierr);
1915   PetscFunctionReturn(0);
1916 }
1917 
1918 /* -------------------------------------------------------------------------------------------*/
1919 PetscErrorCode MatMumpsSetCntl_MUMPS(Mat F,PetscInt icntl,PetscReal val)
1920 {
1921   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
1922 
1923   PetscFunctionBegin;
1924   mumps->id.CNTL(icntl) = val;
1925   PetscFunctionReturn(0);
1926 }
1927 
1928 PetscErrorCode MatMumpsGetCntl_MUMPS(Mat F,PetscInt icntl,PetscReal *val)
1929 {
1930   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
1931 
1932   PetscFunctionBegin;
1933   *val = mumps->id.CNTL(icntl);
1934   PetscFunctionReturn(0);
1935 }
1936 
1937 /*@
1938   MatMumpsSetCntl - Set MUMPS parameter CNTL()
1939 
1940    Logically Collective on Mat
1941 
1942    Input Parameters:
1943 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
1944 .  icntl - index of MUMPS parameter array CNTL()
1945 -  val - value of MUMPS CNTL(icntl)
1946 
1947   Options Database:
1948 .   -mat_mumps_cntl_<icntl> <val>
1949 
1950    Level: beginner
1951 
1952    References:
1953 .     MUMPS Users' Guide
1954 
1955 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
1956 @*/
1957 PetscErrorCode MatMumpsSetCntl(Mat F,PetscInt icntl,PetscReal val)
1958 {
1959   PetscErrorCode ierr;
1960 
1961   PetscFunctionBegin;
1962   PetscValidType(F,1);
1963   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
1964   PetscValidLogicalCollectiveInt(F,icntl,2);
1965   PetscValidLogicalCollectiveReal(F,val,3);
1966   ierr = PetscTryMethod(F,"MatMumpsSetCntl_C",(Mat,PetscInt,PetscReal),(F,icntl,val));CHKERRQ(ierr);
1967   PetscFunctionReturn(0);
1968 }
1969 
1970 /*@
1971   MatMumpsGetCntl - Get MUMPS parameter CNTL()
1972 
1973    Logically Collective on Mat
1974 
1975    Input Parameters:
1976 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
1977 -  icntl - index of MUMPS parameter array CNTL()
1978 
1979   Output Parameter:
1980 .  val - value of MUMPS CNTL(icntl)
1981 
1982    Level: beginner
1983 
1984    References:
1985 .      MUMPS Users' Guide
1986 
1987 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
1988 @*/
1989 PetscErrorCode MatMumpsGetCntl(Mat F,PetscInt icntl,PetscReal *val)
1990 {
1991   PetscErrorCode ierr;
1992 
1993   PetscFunctionBegin;
1994   PetscValidType(F,1);
1995   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
1996   PetscValidLogicalCollectiveInt(F,icntl,2);
1997   PetscValidRealPointer(val,3);
1998   ierr = PetscUseMethod(F,"MatMumpsGetCntl_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));CHKERRQ(ierr);
1999   PetscFunctionReturn(0);
2000 }
2001 
2002 PetscErrorCode MatMumpsGetInfo_MUMPS(Mat F,PetscInt icntl,PetscInt *info)
2003 {
2004   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
2005 
2006   PetscFunctionBegin;
2007   *info = mumps->id.INFO(icntl);
2008   PetscFunctionReturn(0);
2009 }
2010 
2011 PetscErrorCode MatMumpsGetInfog_MUMPS(Mat F,PetscInt icntl,PetscInt *infog)
2012 {
2013   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
2014 
2015   PetscFunctionBegin;
2016   *infog = mumps->id.INFOG(icntl);
2017   PetscFunctionReturn(0);
2018 }
2019 
2020 PetscErrorCode MatMumpsGetRinfo_MUMPS(Mat F,PetscInt icntl,PetscReal *rinfo)
2021 {
2022   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
2023 
2024   PetscFunctionBegin;
2025   *rinfo = mumps->id.RINFO(icntl);
2026   PetscFunctionReturn(0);
2027 }
2028 
2029 PetscErrorCode MatMumpsGetRinfog_MUMPS(Mat F,PetscInt icntl,PetscReal *rinfog)
2030 {
2031   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;
2032 
2033   PetscFunctionBegin;
2034   *rinfog = mumps->id.RINFOG(icntl);
2035   PetscFunctionReturn(0);
2036 }
2037 
2038 PetscErrorCode MatMumpsGetInverse_MUMPS(Mat F,Mat spRHS)
2039 {
2040   PetscErrorCode ierr;
2041   Mat            Bt = NULL,Btseq = NULL;
2042   PetscBool      flg;
2043   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
2044   PetscScalar    *aa;
2045   PetscInt       spnr,*ia,*ja;
2046 
2047   PetscFunctionBegin;
2048   PetscValidIntPointer(spRHS,2);
2049   ierr = PetscObjectTypeCompare((PetscObject)spRHS,MATTRANSPOSEMAT,&flg);CHKERRQ(ierr);
2050   if (flg) {
2051     ierr = MatTransposeGetMat(spRHS,&Bt);CHKERRQ(ierr);
2052   } else SETERRQ(PetscObjectComm((PetscObject)spRHS),PETSC_ERR_ARG_WRONG,"Matrix spRHS must be type MATTRANSPOSEMAT matrix");
2053 
2054   ierr = MatMumpsSetIcntl(F,30,1);CHKERRQ(ierr);
2055 
2056   if (mumps->size > 1) {
2057     Mat_MPIAIJ *b = (Mat_MPIAIJ*)Bt->data;
2058     Btseq = b->A;
2059   } else {
2060     Btseq = Bt;
2061   }
2062 
2063   if (!mumps->myid) {
2064     ierr = MatSeqAIJGetArray(Btseq,&aa);CHKERRQ(ierr);
2065     ierr = MatGetRowIJ(Btseq,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
2066     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
2067 
2068     mumps->id.irhs_ptr    = ia;
2069     mumps->id.irhs_sparse = ja;
2070     mumps->id.nz_rhs      = ia[spnr] - 1;
2071     mumps->id.rhs_sparse  = (MumpsScalar*)aa;
2072   } else {
2073     mumps->id.irhs_ptr    = NULL;
2074     mumps->id.irhs_sparse = NULL;
2075     mumps->id.nz_rhs      = 0;
2076     mumps->id.rhs_sparse  = NULL;
2077   }
2078   mumps->id.ICNTL(20)   = 1; /* rhs is sparse */
2079   mumps->id.ICNTL(21)   = 0; /* solution is in assembled centralized format */
2080 
2081   /* solve phase */
2082   /*-------------*/
2083   mumps->id.job = JOB_SOLVE;
2084   PetscMUMPS_c(&mumps->id);
2085   if (mumps->id.INFOG(1) < 0)
2086     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in solve phase: INFOG(1)=%d INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
2087 
2088   if (!mumps->myid) {
2089     ierr = MatSeqAIJRestoreArray(Btseq,&aa);CHKERRQ(ierr);
2090     ierr = MatRestoreRowIJ(Btseq,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);CHKERRQ(ierr);
2091     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
2092   }
2093   PetscFunctionReturn(0);
2094 }
2095 
2096 /*@
2097   MatMumpsGetInverse - Get user-specified set of entries in inverse of A
2098 
2099    Logically Collective on Mat
2100 
2101    Input Parameters:
2102 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
2103 -  spRHS - sequential sparse matrix in MATTRANSPOSEMAT format holding specified indices in processor[0]
2104 
2105   Output Parameter:
2106 . spRHS - requested entries of inverse of A
2107 
2108    Level: beginner
2109 
2110    References:
2111 .      MUMPS Users' Guide
2112 
2113 .seealso: MatGetFactor(), MatCreateTranspose()
2114 @*/
2115 PetscErrorCode MatMumpsGetInverse(Mat F,Mat spRHS)
2116 {
2117   PetscErrorCode ierr;
2118 
2119   PetscFunctionBegin;
2120   PetscValidType(F,1);
2121   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2122   ierr = PetscUseMethod(F,"MatMumpsGetInverse_C",(Mat,Mat),(F,spRHS));CHKERRQ(ierr);
2123   PetscFunctionReturn(0);
2124 }
2125 
2126 PetscErrorCode MatMumpsGetInverseTranspose_MUMPS(Mat F,Mat spRHST)
2127 {
2128   PetscErrorCode ierr;
2129   Mat            spRHS;
2130 
2131   PetscFunctionBegin;
2132   ierr = MatCreateTranspose(spRHST,&spRHS);CHKERRQ(ierr);
2133   ierr = MatMumpsGetInverse_MUMPS(F,spRHS);CHKERRQ(ierr);
2134   ierr = MatDestroy(&spRHS);CHKERRQ(ierr);
2135   PetscFunctionReturn(0);
2136 }
2137 
2138 /*@
2139   MatMumpsGetInverseTranspose - Get user-specified set of entries in inverse of matrix A^T
2140 
2141    Logically Collective on Mat
2142 
2143    Input Parameters:
2144 +  F - the factored matrix of A obtained by calling MatGetFactor() from PETSc-MUMPS interface
2145 -  spRHST - sequential sparse matrix in MATAIJ format holding specified indices of A^T in processor[0]
2146 
2147   Output Parameter:
2148 . spRHST - requested entries of inverse of A^T
2149 
2150    Level: beginner
2151 
2152    References:
2153 .      MUMPS Users' Guide
2154 
2155 .seealso: MatGetFactor(), MatCreateTranspose(), MatMumpsGetInverse()
2156 @*/
2157 PetscErrorCode MatMumpsGetInverseTranspose(Mat F,Mat spRHST)
2158 {
2159   PetscErrorCode ierr;
2160   PetscBool      flg;
2161 
2162   PetscFunctionBegin;
2163   PetscValidType(F,1);
2164   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2165   ierr = PetscObjectTypeCompareAny((PetscObject)spRHST,&flg,MATSEQAIJ,MATMPIAIJ,NULL);CHKERRQ(ierr);
2166   if (!flg) SETERRQ(PetscObjectComm((PetscObject)spRHST),PETSC_ERR_ARG_WRONG,"Matrix spRHST must be MATAIJ matrix");
2167 
2168   ierr = PetscUseMethod(F,"MatMumpsGetInverseTranspose_C",(Mat,Mat),(F,spRHST));CHKERRQ(ierr);
2169   PetscFunctionReturn(0);
2170 }
2171 
2172 /*@
2173   MatMumpsGetInfo - Get MUMPS parameter INFO()
2174 
2175    Logically Collective on Mat
2176 
2177    Input Parameters:
2178 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
2179 -  icntl - index of MUMPS parameter array INFO()
2180 
2181   Output Parameter:
2182 .  ival - value of MUMPS INFO(icntl)
2183 
2184    Level: beginner
2185 
2186    References:
2187 .      MUMPS Users' Guide
2188 
2189 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
2190 @*/
2191 PetscErrorCode MatMumpsGetInfo(Mat F,PetscInt icntl,PetscInt *ival)
2192 {
2193   PetscErrorCode ierr;
2194 
2195   PetscFunctionBegin;
2196   PetscValidType(F,1);
2197   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2198   PetscValidIntPointer(ival,3);
2199   ierr = PetscUseMethod(F,"MatMumpsGetInfo_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));CHKERRQ(ierr);
2200   PetscFunctionReturn(0);
2201 }
2202 
2203 /*@
2204   MatMumpsGetInfog - Get MUMPS parameter INFOG()
2205 
2206    Logically Collective on Mat
2207 
2208    Input Parameters:
2209 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
2210 -  icntl - index of MUMPS parameter array INFOG()
2211 
2212   Output Parameter:
2213 .  ival - value of MUMPS INFOG(icntl)
2214 
2215    Level: beginner
2216 
2217    References:
2218 .      MUMPS Users' Guide
2219 
2220 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
2221 @*/
2222 PetscErrorCode MatMumpsGetInfog(Mat F,PetscInt icntl,PetscInt *ival)
2223 {
2224   PetscErrorCode ierr;
2225 
2226   PetscFunctionBegin;
2227   PetscValidType(F,1);
2228   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2229   PetscValidIntPointer(ival,3);
2230   ierr = PetscUseMethod(F,"MatMumpsGetInfog_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));CHKERRQ(ierr);
2231   PetscFunctionReturn(0);
2232 }
2233 
2234 /*@
2235   MatMumpsGetRinfo - Get MUMPS parameter RINFO()
2236 
2237    Logically Collective on Mat
2238 
2239    Input Parameters:
2240 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
2241 -  icntl - index of MUMPS parameter array RINFO()
2242 
2243   Output Parameter:
2244 .  val - value of MUMPS RINFO(icntl)
2245 
2246    Level: beginner
2247 
2248    References:
2249 .       MUMPS Users' Guide
2250 
2251 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
2252 @*/
2253 PetscErrorCode MatMumpsGetRinfo(Mat F,PetscInt icntl,PetscReal *val)
2254 {
2255   PetscErrorCode ierr;
2256 
2257   PetscFunctionBegin;
2258   PetscValidType(F,1);
2259   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2260   PetscValidRealPointer(val,3);
2261   ierr = PetscUseMethod(F,"MatMumpsGetRinfo_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));CHKERRQ(ierr);
2262   PetscFunctionReturn(0);
2263 }
2264 
2265 /*@
2266   MatMumpsGetRinfog - Get MUMPS parameter RINFOG()
2267 
2268    Logically Collective on Mat
2269 
2270    Input Parameters:
2271 +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
2272 -  icntl - index of MUMPS parameter array RINFOG()
2273 
2274   Output Parameter:
2275 .  val - value of MUMPS RINFOG(icntl)
2276 
2277    Level: beginner
2278 
2279    References:
2280 .      MUMPS Users' Guide
2281 
2282 .seealso: MatGetFactor(), MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
2283 @*/
2284 PetscErrorCode MatMumpsGetRinfog(Mat F,PetscInt icntl,PetscReal *val)
2285 {
2286   PetscErrorCode ierr;
2287 
2288   PetscFunctionBegin;
2289   PetscValidType(F,1);
2290   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
2291   PetscValidRealPointer(val,3);
2292   ierr = PetscUseMethod(F,"MatMumpsGetRinfog_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));CHKERRQ(ierr);
2293   PetscFunctionReturn(0);
2294 }
2295 
2296 /*MC
2297   MATSOLVERMUMPS -  A matrix type providing direct solvers (LU and Cholesky) for
2298   distributed and sequential matrices via the external package MUMPS.
2299 
2300   Works with MATAIJ and MATSBAIJ matrices
2301 
2302   Use ./configure --download-mumps --download-scalapack --download-parmetis --download-metis --download-ptscotch  to have PETSc installed with MUMPS
2303 
2304   Use -pc_type cholesky or lu -pc_factor_mat_solver_type mumps to use this direct solver
2305 
2306   Options Database Keys:
2307 +  -mat_mumps_icntl_1 - ICNTL(1): output stream for error messages
2308 .  -mat_mumps_icntl_2 - ICNTL(2): output stream for diagnostic printing, statistics, and warning
2309 .  -mat_mumps_icntl_3 -  ICNTL(3): output stream for global information, collected on the host
2310 .  -mat_mumps_icntl_4 -  ICNTL(4): level of printing (0 to 4)
2311 .  -mat_mumps_icntl_6 - ICNTL(6): permutes to a zero-free diagonal and/or scale the matrix (0 to 7)
2312 .  -mat_mumps_icntl_7 - ICNTL(7): computes a symmetric permutation in sequential analysis (0 to 7). 3=Scotch, 4=PORD, 5=Metis
2313 .  -mat_mumps_icntl_8  - ICNTL(8): scaling strategy (-2 to 8 or 77)
2314 .  -mat_mumps_icntl_10  - ICNTL(10): max num of refinements
2315 .  -mat_mumps_icntl_11  - ICNTL(11): statistics related to an error analysis (via -ksp_view)
2316 .  -mat_mumps_icntl_12  - ICNTL(12): an ordering strategy for symmetric matrices (0 to 3)
2317 .  -mat_mumps_icntl_13  - ICNTL(13): parallelism of the root node (enable ScaLAPACK) and its splitting
2318 .  -mat_mumps_icntl_14  - ICNTL(14): percentage increase in the estimated working space
2319 .  -mat_mumps_icntl_19  - ICNTL(19): computes the Schur complement
2320 .  -mat_mumps_icntl_22  - ICNTL(22): in-core/out-of-core factorization and solve (0 or 1)
2321 .  -mat_mumps_icntl_23  - ICNTL(23): max size of the working memory (MB) that can allocate per processor
2322 .  -mat_mumps_icntl_24  - ICNTL(24): detection of null pivot rows (0 or 1)
2323 .  -mat_mumps_icntl_25  - ICNTL(25): compute a solution of a deficient matrix and a null space basis
2324 .  -mat_mumps_icntl_26  - ICNTL(26): drives the solution phase if a Schur complement matrix
2325 .  -mat_mumps_icntl_28  - ICNTL(28): use 1 for sequential analysis and ictnl(7) ordering, or 2 for parallel analysis and ictnl(29) ordering
2326 .  -mat_mumps_icntl_29 - ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis
2327 .  -mat_mumps_icntl_30 - ICNTL(30): compute user-specified set of entries in inv(A)
2328 .  -mat_mumps_icntl_31 - ICNTL(31): indicates which factors may be discarded during factorization
2329 .  -mat_mumps_icntl_33 - ICNTL(33): compute determinant
2330 .  -mat_mumps_cntl_1  - CNTL(1): relative pivoting threshold
2331 .  -mat_mumps_cntl_2  -  CNTL(2): stopping criterion of refinement
2332 .  -mat_mumps_cntl_3 - CNTL(3): absolute pivoting threshold
2333 .  -mat_mumps_cntl_4 - CNTL(4): value for static pivoting
2334 -  -mat_mumps_cntl_5 - CNTL(5): fixation for null pivots
2335 
2336   Level: beginner
2337 
2338     Notes:
2339     When a MUMPS factorization fails inside a KSP solve, for example with a KSP_DIVERGED_PCSETUP_FAILED, one can find the MUMPS information about the failure by calling
2340 $          KSPGetPC(ksp,&pc);
2341 $          PCFactorGetMatrix(pc,&mat);
2342 $          MatMumpsGetInfo(mat,....);
2343 $          MatMumpsGetInfog(mat,....); etc.
2344            Or you can run with -ksp_error_if_not_converged and the program will be stopped and the information printed in the error message.
2345 
2346 .seealso: PCFactorSetMatSolverType(), MatSolverType, MatMumpsSetICntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog(), KSPGetPC(), PCGetFactor(), PCFactorGetMatrix()
2347 
2348 M*/
2349 
2350 static PetscErrorCode MatFactorGetSolverType_mumps(Mat A,MatSolverType *type)
2351 {
2352   PetscFunctionBegin;
2353   *type = MATSOLVERMUMPS;
2354   PetscFunctionReturn(0);
2355 }
2356 
2357 /* MatGetFactor for Seq and MPI AIJ matrices */
2358 static PetscErrorCode MatGetFactor_aij_mumps(Mat A,MatFactorType ftype,Mat *F)
2359 {
2360   Mat            B;
2361   PetscErrorCode ierr;
2362   Mat_MUMPS      *mumps;
2363   PetscBool      isSeqAIJ;
2364 
2365   PetscFunctionBegin;
2366   /* Create the factorization matrix */
2367   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);CHKERRQ(ierr);
2368   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
2369   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
2370   ierr = PetscStrallocpy("mumps",&((PetscObject)B)->type_name);CHKERRQ(ierr);
2371   ierr = MatSetUp(B);CHKERRQ(ierr);
2372 
2373   ierr = PetscNewLog(B,&mumps);CHKERRQ(ierr);
2374 
2375   B->ops->view        = MatView_MUMPS;
2376   B->ops->getinfo     = MatGetInfo_MUMPS;
2377 
2378   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);CHKERRQ(ierr);
2379   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);CHKERRQ(ierr);
2380   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);CHKERRQ(ierr);
2381   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);CHKERRQ(ierr);
2382   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);CHKERRQ(ierr);
2383   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);CHKERRQ(ierr);
2384   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);CHKERRQ(ierr);
2385   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);CHKERRQ(ierr);
2386   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);CHKERRQ(ierr);
2387   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);CHKERRQ(ierr);
2388   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);CHKERRQ(ierr);
2389   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);CHKERRQ(ierr);
2390   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);CHKERRQ(ierr);
2391 
2392   if (ftype == MAT_FACTOR_LU) {
2393     B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
2394     B->factortype            = MAT_FACTOR_LU;
2395     if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqaij;
2396     else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpiaij;
2397     mumps->sym = 0;
2398   } else {
2399     B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
2400     B->factortype                  = MAT_FACTOR_CHOLESKY;
2401     if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqsbaij;
2402     else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpisbaij;
2403 #if defined(PETSC_USE_COMPLEX)
2404     mumps->sym = 2;
2405 #else
2406     if (A->spd_set && A->spd) mumps->sym = 1;
2407     else                      mumps->sym = 2;
2408 #endif
2409   }
2410 
2411   /* set solvertype */
2412   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
2413   ierr = PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);CHKERRQ(ierr);
2414 
2415   B->ops->destroy = MatDestroy_MUMPS;
2416   B->data         = (void*)mumps;
2417 
2418   ierr = PetscInitializeMUMPS(A,mumps);CHKERRQ(ierr);
2419 
2420   *F = B;
2421   PetscFunctionReturn(0);
2422 }
2423 
2424 /* MatGetFactor for Seq and MPI SBAIJ matrices */
2425 static PetscErrorCode MatGetFactor_sbaij_mumps(Mat A,MatFactorType ftype,Mat *F)
2426 {
2427   Mat            B;
2428   PetscErrorCode ierr;
2429   Mat_MUMPS      *mumps;
2430   PetscBool      isSeqSBAIJ;
2431 
2432   PetscFunctionBegin;
2433   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with MUMPS LU, use AIJ matrix");
2434   if (A->rmap->bs > 1) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with block size > 1 with MUMPS Cholesky, use AIJ matrix instead");
2435   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
2436   /* Create the factorization matrix */
2437   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
2438   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
2439   ierr = PetscStrallocpy("mumps",&((PetscObject)B)->type_name);CHKERRQ(ierr);
2440   ierr = MatSetUp(B);CHKERRQ(ierr);
2441 
2442   ierr = PetscNewLog(B,&mumps);CHKERRQ(ierr);
2443   if (isSeqSBAIJ) {
2444     mumps->ConvertToTriples = MatConvertToTriples_seqsbaij_seqsbaij;
2445   } else {
2446     mumps->ConvertToTriples = MatConvertToTriples_mpisbaij_mpisbaij;
2447   }
2448 
2449   B->ops->getinfo                = MatGetInfo_External;
2450   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
2451   B->ops->view                   = MatView_MUMPS;
2452 
2453   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);CHKERRQ(ierr);
2454   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);CHKERRQ(ierr);
2455   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);CHKERRQ(ierr);
2456   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);CHKERRQ(ierr);
2457   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);CHKERRQ(ierr);
2458   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);CHKERRQ(ierr);
2459   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);CHKERRQ(ierr);
2460   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);CHKERRQ(ierr);
2461   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);CHKERRQ(ierr);
2462   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);CHKERRQ(ierr);
2463   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);CHKERRQ(ierr);
2464   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);CHKERRQ(ierr);
2465   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);CHKERRQ(ierr);
2466 
2467   B->factortype = MAT_FACTOR_CHOLESKY;
2468 #if defined(PETSC_USE_COMPLEX)
2469   mumps->sym = 2;
2470 #else
2471   if (A->spd_set && A->spd) mumps->sym = 1;
2472   else                      mumps->sym = 2;
2473 #endif
2474 
2475   /* set solvertype */
2476   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
2477   ierr = PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);CHKERRQ(ierr);
2478 
2479   B->ops->destroy = MatDestroy_MUMPS;
2480   B->data         = (void*)mumps;
2481 
2482   ierr = PetscInitializeMUMPS(A,mumps);CHKERRQ(ierr);
2483 
2484   *F = B;
2485   PetscFunctionReturn(0);
2486 }
2487 
2488 static PetscErrorCode MatGetFactor_baij_mumps(Mat A,MatFactorType ftype,Mat *F)
2489 {
2490   Mat            B;
2491   PetscErrorCode ierr;
2492   Mat_MUMPS      *mumps;
2493   PetscBool      isSeqBAIJ;
2494 
2495   PetscFunctionBegin;
2496   /* Create the factorization matrix */
2497   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQBAIJ,&isSeqBAIJ);CHKERRQ(ierr);
2498   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
2499   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
2500   ierr = PetscStrallocpy("mumps",&((PetscObject)B)->type_name);CHKERRQ(ierr);
2501   ierr = MatSetUp(B);CHKERRQ(ierr);
2502 
2503   ierr = PetscNewLog(B,&mumps);CHKERRQ(ierr);
2504   if (ftype == MAT_FACTOR_LU) {
2505     B->ops->lufactorsymbolic = MatLUFactorSymbolic_BAIJMUMPS;
2506     B->factortype            = MAT_FACTOR_LU;
2507     if (isSeqBAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqbaij_seqaij;
2508     else mumps->ConvertToTriples = MatConvertToTriples_mpibaij_mpiaij;
2509     mumps->sym = 0;
2510   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc BAIJ matrices with MUMPS Cholesky, use SBAIJ or AIJ matrix instead\n");
2511 
2512   B->ops->getinfo     = MatGetInfo_External;
2513   B->ops->view        = MatView_MUMPS;
2514 
2515   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);CHKERRQ(ierr);
2516   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);CHKERRQ(ierr);
2517   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);CHKERRQ(ierr);
2518   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);CHKERRQ(ierr);
2519   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);CHKERRQ(ierr);
2520   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);CHKERRQ(ierr);
2521   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);CHKERRQ(ierr);
2522   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);CHKERRQ(ierr);
2523   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);CHKERRQ(ierr);
2524   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);CHKERRQ(ierr);
2525   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);CHKERRQ(ierr);
2526   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);CHKERRQ(ierr);
2527   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);CHKERRQ(ierr);
2528 
2529   /* set solvertype */
2530   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
2531   ierr = PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);CHKERRQ(ierr);
2532 
2533   B->ops->destroy = MatDestroy_MUMPS;
2534   B->data         = (void*)mumps;
2535 
2536   ierr = PetscInitializeMUMPS(A,mumps);CHKERRQ(ierr);
2537 
2538   *F = B;
2539   PetscFunctionReturn(0);
2540 }
2541 
2542 /* MatGetFactor for Seq and MPI SELL matrices */
2543 static PetscErrorCode MatGetFactor_sell_mumps(Mat A,MatFactorType ftype,Mat *F)
2544 {
2545   Mat            B;
2546   PetscErrorCode ierr;
2547   Mat_MUMPS      *mumps;
2548   PetscBool      isSeqSELL;
2549 
2550   PetscFunctionBegin;
2551   /* Create the factorization matrix */
2552   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSELL,&isSeqSELL);CHKERRQ(ierr);
2553   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
2554   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
2555   ierr = PetscStrallocpy("mumps",&((PetscObject)B)->type_name);CHKERRQ(ierr);
2556   ierr = MatSetUp(B);CHKERRQ(ierr);
2557 
2558   ierr = PetscNewLog(B,&mumps);CHKERRQ(ierr);
2559 
2560   B->ops->view        = MatView_MUMPS;
2561   B->ops->getinfo     = MatGetInfo_MUMPS;
2562 
2563   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);CHKERRQ(ierr);
2564   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);CHKERRQ(ierr);
2565   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);CHKERRQ(ierr);
2566   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);CHKERRQ(ierr);
2567   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);CHKERRQ(ierr);
2568   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);CHKERRQ(ierr);
2569   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);CHKERRQ(ierr);
2570   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);CHKERRQ(ierr);
2571   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);CHKERRQ(ierr);
2572   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);CHKERRQ(ierr);
2573   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);CHKERRQ(ierr);
2574 
2575   if (ftype == MAT_FACTOR_LU) {
2576     B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
2577     B->factortype            = MAT_FACTOR_LU;
2578     if (isSeqSELL) mumps->ConvertToTriples = MatConvertToTriples_seqsell_seqaij;
2579     else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"To be implemented");
2580     mumps->sym = 0;
2581   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"To be implemented");
2582 
2583   /* set solvertype */
2584   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
2585   ierr = PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);CHKERRQ(ierr);
2586 
2587   B->ops->destroy = MatDestroy_MUMPS;
2588   B->data         = (void*)mumps;
2589 
2590   ierr = PetscInitializeMUMPS(A,mumps);CHKERRQ(ierr);
2591 
2592   *F = B;
2593   PetscFunctionReturn(0);
2594 }
2595 
2596 PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_MUMPS(void)
2597 {
2598   PetscErrorCode ierr;
2599 
2600   PetscFunctionBegin;
2601   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mumps);CHKERRQ(ierr);
2602   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mumps);CHKERRQ(ierr);
2603   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIBAIJ,MAT_FACTOR_LU,MatGetFactor_baij_mumps);CHKERRQ(ierr);
2604   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_baij_mumps);CHKERRQ(ierr);
2605   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATMPISBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
2606   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mumps);CHKERRQ(ierr);
2607   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mumps);CHKERRQ(ierr);
2608   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQBAIJ,MAT_FACTOR_LU,MatGetFactor_baij_mumps);CHKERRQ(ierr);
2609   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_baij_mumps);CHKERRQ(ierr);
2610   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQSBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
2611   ierr = MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQSELL,MAT_FACTOR_LU,MatGetFactor_sell_mumps);CHKERRQ(ierr);
2612   PetscFunctionReturn(0);
2613 }
2614 
2615