Date: 2022-03-04T17:37:46+00:00

Actual source code: mumps.c

      2: /*
      3:     Provides an interface to the MUMPS sparse solver
      4: */
      5: #include <petscpkg_version.h>
      6: #include <../src/mat/impls/aij/mpi/mpiaij.h>
      7: #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
      8: #include <../src/mat/impls/sell/mpi/mpisell.h>

     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

     31: /* calls to MUMPS */
     32: #if defined(PETSC_USE_COMPLEX)
     33: #if defined(PETSC_USE_REAL_SINGLE)
     34: #define MUMPS_c cmumps_c
     35: #else
     36: #define MUMPS_c zmumps_c
     37: #endif
     38: #else
     39: #if defined(PETSC_USE_REAL_SINGLE)
     40: #define MUMPS_c smumps_c
     41: #else
     42: #define MUMPS_c dmumps_c
     43: #endif
     44: #endif

     46: /* MUMPS uses MUMPS_INT for nonzero indices such as irn/jcn, irn_loc/jcn_loc and uses int64_t for
     47:    number of nonzeros such as nnz, nnz_loc. We typedef MUMPS_INT to PetscMUMPSInt to follow the
     48:    naming convention in PetscMPIInt, PetscBLASInt etc.
     49: */
     50: typedef MUMPS_INT PetscMUMPSInt;

     52: #if PETSC_PKG_MUMPS_VERSION_GE(5,3,0)
     53:   #if defined(MUMPS_INTSIZE64) /* MUMPS_INTSIZE64 is in MUMPS headers if it is built in full 64-bit mode, therefore the macro is more reliable */
     54:     #error "Petsc has not been tested with full 64-bit MUMPS and we choose to error out"
     55:   #endif
     56: #else
     57:   #if defined(INTSIZE64) /* INTSIZE64 is a command line macro one used to build MUMPS in full 64-bit mode */
     58:     #error "Petsc has not been tested with full 64-bit MUMPS and we choose to error out"
     59:   #endif
     60: #endif

     62: #define MPIU_MUMPSINT             MPI_INT
     63: #define PETSC_MUMPS_INT_MAX       2147483647
     64: #define PETSC_MUMPS_INT_MIN       -2147483648

     66: /* Cast PetscInt to PetscMUMPSInt. Usually there is no overflow since <a> is row/col indices or some small integers*/
     67: PETSC_STATIC_INLINE PetscErrorCode PetscMUMPSIntCast(PetscInt a,PetscMUMPSInt *b)
     68: {
     70:   if (PetscDefined(USE_64BIT_INDICES) && PetscUnlikelyDebug(a > PETSC_MUMPS_INT_MAX || a < PETSC_MUMPS_INT_MIN)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"PetscInt too long for PetscMUMPSInt");
     71:   *b = (PetscMUMPSInt)(a);
     72:   return(0);
     73: }

     75: /* Put these utility routines here since they are only used in this file */
     76: PETSC_STATIC_INLINE PetscErrorCode PetscOptionsMUMPSInt_Private(PetscOptionItems *PetscOptionsObject,const char opt[],const char text[],const char man[],PetscMUMPSInt currentvalue,PetscMUMPSInt *value,PetscBool *set,PetscMUMPSInt lb,PetscMUMPSInt ub)
     77: {
     79:   PetscInt       myval;
     80:   PetscBool      myset;
     82:   /* PetscInt's size should be always >= PetscMUMPSInt's. It is safe to call PetscOptionsInt_Private to read a PetscMUMPSInt */
     83:   PetscOptionsInt_Private(PetscOptionsObject,opt,text,man,(PetscInt)currentvalue,&myval,&myset,lb,ub);
     84:   if (myset) {PetscMUMPSIntCast(myval,value);}
     85:   if (set) *set = myset;
     86:   return(0);
     87: }
     88: #define PetscOptionsMUMPSInt(a,b,c,d,e,f) PetscOptionsMUMPSInt_Private(PetscOptionsObject,a,b,c,d,e,f,PETSC_MUMPS_INT_MIN,PETSC_MUMPS_INT_MAX)

     90: /* if using PETSc OpenMP support, we only call MUMPS on master ranks. Before/after the call, we change/restore CPUs the master ranks can run on */
     91: #if defined(PETSC_HAVE_OPENMP_SUPPORT)
     92: #define PetscMUMPS_c(mumps) \
     93:   do { \
     94:     if (mumps->use_petsc_omp_support) { \
     95:       if (mumps->is_omp_master) { \
     96:         PetscOmpCtrlOmpRegionOnMasterBegin(mumps->omp_ctrl); \
     97:         MUMPS_c(&mumps->id); \
     98:         PetscOmpCtrlOmpRegionOnMasterEnd(mumps->omp_ctrl); \
     99:       } \
    100:       PetscOmpCtrlBarrier(mumps->omp_ctrl); \
    101:       /* Global info is same on all processes so we Bcast it within omp_comm. Local info is specific      \
    102:          to processes, so we only Bcast info[1], an error code and leave others (since they do not have   \
    103:          an easy translation between omp_comm and petsc_comm). See MUMPS-5.1.2 manual p82.                   \
    104:          omp_comm is a small shared memory communicator, hence doing multiple Bcast as shown below is OK. \
    105:       */ \
    106:       MPI_Bcast(mumps->id.infog, 40,MPIU_MUMPSINT, 0,mumps->omp_comm);\
    107:       MPI_Bcast(mumps->id.rinfog,20,MPIU_REAL,     0,mumps->omp_comm);\
    108:       MPI_Bcast(mumps->id.info,  1, MPIU_MUMPSINT, 0,mumps->omp_comm);\
    109:     } else { \
    110:       MUMPS_c(&mumps->id); \
    111:     } \
    112:   } while (0)
    113: #else
    114: #define PetscMUMPS_c(mumps) \
    115:   do { MUMPS_c(&mumps->id); } while (0)
    116: #endif

    118: /* declare MumpsScalar */
    119: #if defined(PETSC_USE_COMPLEX)
    120: #if defined(PETSC_USE_REAL_SINGLE)
    121: #define MumpsScalar mumps_complex
    122: #else
    123: #define MumpsScalar mumps_double_complex
    124: #endif
    125: #else
    126: #define MumpsScalar PetscScalar
    127: #endif

    129: /* macros s.t. indices match MUMPS documentation */
    130: #define ICNTL(I) icntl[(I)-1]
    131: #define CNTL(I) cntl[(I)-1]
    132: #define INFOG(I) infog[(I)-1]
    133: #define INFO(I) info[(I)-1]
    134: #define RINFOG(I) rinfog[(I)-1]
    135: #define RINFO(I) rinfo[(I)-1]

    137: typedef struct Mat_MUMPS Mat_MUMPS;
    138: struct Mat_MUMPS {
    139: #if defined(PETSC_USE_COMPLEX)
    140: #if defined(PETSC_USE_REAL_SINGLE)
    141:   CMUMPS_STRUC_C id;
    142: #else
    143:   ZMUMPS_STRUC_C id;
    144: #endif
    145: #else
    146: #if defined(PETSC_USE_REAL_SINGLE)
    147:   SMUMPS_STRUC_C id;
    148: #else
    149:   DMUMPS_STRUC_C id;
    150: #endif
    151: #endif

    153:   MatStructure   matstruc;
    154:   PetscMPIInt    myid,petsc_size;
    155:   PetscMUMPSInt  *irn,*jcn;             /* the (i,j,v) triplets passed to mumps. */
    156:   PetscScalar    *val,*val_alloc;       /* For some matrices, we can directly access their data array without a buffer. For others, we need a buffer. So comes val_alloc. */
    157:   PetscInt64     nnz;                   /* number of nonzeros. The type is called selective 64-bit in mumps */
    158:   PetscMUMPSInt  sym;
    159:   MPI_Comm       mumps_comm;
    160:   PetscMUMPSInt  ICNTL9_pre;            /* check if ICNTL(9) is changed from previous MatSolve */
    161:   VecScatter     scat_rhs, scat_sol;    /* used by MatSolve() */
    162:   PetscMUMPSInt  ICNTL20;               /* use centralized (0) or distributed (10) dense RHS */
    163:   PetscMUMPSInt  lrhs_loc,nloc_rhs,*irhs_loc;
    164: #if defined(PETSC_HAVE_OPENMP_SUPPORT)
    165:   PetscInt       *rhs_nrow,max_nrhs;
    166:   PetscMPIInt    *rhs_recvcounts,*rhs_disps;
    167:   PetscScalar    *rhs_loc,*rhs_recvbuf;
    168: #endif
    169:   Vec            b_seq,x_seq;
    170:   PetscInt       ninfo,*info;           /* which INFO to display */
    171:   PetscInt       sizeredrhs;
    172:   PetscScalar    *schur_sol;
    173:   PetscInt       schur_sizesol;
    174:   PetscMUMPSInt  *ia_alloc,*ja_alloc;   /* work arrays used for the CSR struct for sparse rhs */
    175:   PetscInt64     cur_ilen,cur_jlen;     /* current len of ia_alloc[], ja_alloc[] */
    176:   PetscErrorCode (*ConvertToTriples)(Mat,PetscInt,MatReuse,Mat_MUMPS*);

    178:   /* stuff used by petsc/mumps OpenMP support*/
    179:   PetscBool      use_petsc_omp_support;
    180:   PetscOmpCtrl   omp_ctrl;              /* an OpenMP controler that blocked processes will release their CPU (MPI_Barrier does not have this guarantee) */
    181:   MPI_Comm       petsc_comm,omp_comm;   /* petsc_comm is petsc matrix's comm */
    182:   PetscInt64     *recvcount;            /* a collection of nnz on omp_master */
    183:   PetscMPIInt    tag,omp_comm_size;
    184:   PetscBool      is_omp_master;         /* is this rank the master of omp_comm */
    185:   MPI_Request    *reqs;
    186: };

    188: /* Cast a 1-based CSR represented by (nrow, ia, ja) of type PetscInt to a CSR of type PetscMUMPSInt.
    189:    Here, nrow is number of rows, ia[] is row pointer and ja[] is column indices.
    190:  */
    191: static PetscErrorCode PetscMUMPSIntCSRCast(Mat_MUMPS *mumps,PetscInt nrow,PetscInt *ia,PetscInt *ja,PetscMUMPSInt **ia_mumps,PetscMUMPSInt **ja_mumps,PetscMUMPSInt *nnz_mumps)
    192: {
    194:   PetscInt       nnz=ia[nrow]-1; /* mumps uses 1-based indices. Uses PetscInt instead of PetscInt64 since mumps only uses PetscMUMPSInt for rhs */

    197: #if defined(PETSC_USE_64BIT_INDICES)
    198:   {
    199:     PetscInt i;
    200:     if (nrow+1 > mumps->cur_ilen) { /* realloc ia_alloc/ja_alloc to fit ia/ja */
    201:       PetscFree(mumps->ia_alloc);
    202:       PetscMalloc1(nrow+1,&mumps->ia_alloc);
    203:       mumps->cur_ilen = nrow+1;
    204:     }
    205:     if (nnz > mumps->cur_jlen) {
    206:       PetscFree(mumps->ja_alloc);
    207:       PetscMalloc1(nnz,&mumps->ja_alloc);
    208:       mumps->cur_jlen = nnz;
    209:     }
    210:     for (i=0; i<nrow+1; i++) {PetscMUMPSIntCast(ia[i],&(mumps->ia_alloc[i]));}
    211:     for (i=0; i<nnz; i++)    {PetscMUMPSIntCast(ja[i],&(mumps->ja_alloc[i]));}
    212:     *ia_mumps = mumps->ia_alloc;
    213:     *ja_mumps = mumps->ja_alloc;
    214:   }
    215: #else
    216:   *ia_mumps = ia;
    217:   *ja_mumps = ja;
    218: #endif
    219:   PetscMUMPSIntCast(nnz,nnz_mumps);
    220:   return(0);
    221: }

    223: static PetscErrorCode MatMumpsResetSchur_Private(Mat_MUMPS* mumps)
    224: {

    228:   PetscFree(mumps->id.listvar_schur);
    229:   PetscFree(mumps->id.redrhs);
    230:   PetscFree(mumps->schur_sol);
    231:   mumps->id.size_schur = 0;
    232:   mumps->id.schur_lld  = 0;
    233:   mumps->id.ICNTL(19)  = 0;
    234:   return(0);
    235: }

    237: /* solve with rhs in mumps->id.redrhs and return in the same location */
    238: static PetscErrorCode MatMumpsSolveSchur_Private(Mat F)
    239: {
    240:   Mat_MUMPS            *mumps=(Mat_MUMPS*)F->data;
    241:   Mat                  S,B,X;
    242:   MatFactorSchurStatus schurstatus;
    243:   PetscInt             sizesol;
    244:   PetscErrorCode       ierr;

    247:   MatFactorFactorizeSchurComplement(F);
    248:   MatFactorGetSchurComplement(F,&S,&schurstatus);
    249:   MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,(PetscScalar*)mumps->id.redrhs,&B);
    250:   MatSetType(B,((PetscObject)S)->type_name);
    251: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    252:   MatBindToCPU(B,S->boundtocpu);
    253: #endif
    254:   switch (schurstatus) {
    255:   case MAT_FACTOR_SCHUR_FACTORED:
    256:     MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,(PetscScalar*)mumps->id.redrhs,&X);
    257:     MatSetType(X,((PetscObject)S)->type_name);
    258: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    259:     MatBindToCPU(X,S->boundtocpu);
    260: #endif
    261:     if (!mumps->id.ICNTL(9)) { /* transpose solve */
    262:       MatMatSolveTranspose(S,B,X);
    263:     } else {
    264:       MatMatSolve(S,B,X);
    265:     }
    266:     break;
    267:   case MAT_FACTOR_SCHUR_INVERTED:
    268:     sizesol = mumps->id.nrhs*mumps->id.size_schur;
    269:     if (!mumps->schur_sol || sizesol > mumps->schur_sizesol) {
    270:       PetscFree(mumps->schur_sol);
    271:       PetscMalloc1(sizesol,&mumps->schur_sol);
    272:       mumps->schur_sizesol = sizesol;
    273:     }
    274:     MatCreateSeqDense(PETSC_COMM_SELF,mumps->id.size_schur,mumps->id.nrhs,mumps->schur_sol,&X);
    275:     MatSetType(X,((PetscObject)S)->type_name);
    276: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    277:     MatBindToCPU(X,S->boundtocpu);
    278: #endif
    279:     MatProductCreateWithMat(S,B,NULL,X);
    280:     if (!mumps->id.ICNTL(9)) { /* transpose solve */
    281:       MatProductSetType(X,MATPRODUCT_AtB);
    282:     } else {
    283:       MatProductSetType(X,MATPRODUCT_AB);
    284:     }
    285:     MatProductSetFromOptions(X);
    286:     MatProductSymbolic(X);
    287:     MatProductNumeric(X);

    289:     MatCopy(X,B,SAME_NONZERO_PATTERN);
    290:     break;
    291:   default:
    292:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
    293:   }
    294:   MatFactorRestoreSchurComplement(F,&S,schurstatus);
    295:   MatDestroy(&B);
    296:   MatDestroy(&X);
    297:   return(0);
    298: }

    300: static PetscErrorCode MatMumpsHandleSchur_Private(Mat F, PetscBool expansion)
    301: {
    302:   Mat_MUMPS     *mumps=(Mat_MUMPS*)F->data;

    306:   if (!mumps->id.ICNTL(19)) { /* do nothing when Schur complement has not been computed */
    307:     return(0);
    308:   }
    309:   if (!expansion) { /* prepare for the condensation step */
    310:     PetscInt sizeredrhs = mumps->id.nrhs*mumps->id.size_schur;
    311:     /* allocate MUMPS internal array to store reduced right-hand sides */
    312:     if (!mumps->id.redrhs || sizeredrhs > mumps->sizeredrhs) {
    313:       PetscFree(mumps->id.redrhs);
    314:       mumps->id.lredrhs = mumps->id.size_schur;
    315:       PetscMalloc1(mumps->id.nrhs*mumps->id.lredrhs,&mumps->id.redrhs);
    316:       mumps->sizeredrhs = mumps->id.nrhs*mumps->id.lredrhs;
    317:     }
    318:     mumps->id.ICNTL(26) = 1; /* condensation phase */
    319:   } else { /* prepare for the expansion step */
    320:     /* solve Schur complement (this has to be done by the MUMPS user, so basically us) */
    321:     MatMumpsSolveSchur_Private(F);
    322:     mumps->id.ICNTL(26) = 2; /* expansion phase */
    323:     PetscMUMPS_c(mumps);
    324:     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));
    325:     /* restore defaults */
    326:     mumps->id.ICNTL(26) = -1;
    327:     /* free MUMPS internal array for redrhs if we have solved for multiple rhs in order to save memory space */
    328:     if (mumps->id.nrhs > 1) {
    329:       PetscFree(mumps->id.redrhs);
    330:       mumps->id.lredrhs = 0;
    331:       mumps->sizeredrhs = 0;
    332:     }
    333:   }
    334:   return(0);
    335: }

    337: /*
    338:   MatConvertToTriples_A_B - convert Petsc matrix to triples: row[nz], col[nz], val[nz]

    340:   input:
    341:     A       - matrix in aij,baij or sbaij format
    342:     shift   - 0: C style output triple; 1: Fortran style output triple.
    343:     reuse   - MAT_INITIAL_MATRIX: spaces are allocated and values are set for the triple
    344:               MAT_REUSE_MATRIX:   only the values in v array are updated
    345:   output:
    346:     nnz     - dim of r, c, and v (number of local nonzero entries of A)
    347:     r, c, v - row and col index, matrix values (matrix triples)

    349:   The returned values r, c, and sometimes v are obtained in a single PetscMalloc(). Then in MatDestroy_MUMPS() it is
    350:   freed with PetscFree(mumps->irn);  This is not ideal code, the fact that v is ONLY sometimes part of mumps->irn means
    351:   that the PetscMalloc() cannot easily be replaced with a PetscMalloc3().

    353:  */

    355: PetscErrorCode MatConvertToTriples_seqaij_seqaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    356: {
    357:   const PetscScalar *av;
    358:   const PetscInt    *ai,*aj,*ajj,M=A->rmap->n;
    359:   PetscInt64        nz,rnz,i,j,k;
    360:   PetscErrorCode    ierr;
    361:   PetscMUMPSInt     *row,*col;
    362:   Mat_SeqAIJ        *aa=(Mat_SeqAIJ*)A->data;

    365:   MatSeqAIJGetArrayRead(A,&av);
    366:   mumps->val = (PetscScalar*)av;
    367:   if (reuse == MAT_INITIAL_MATRIX) {
    368:     nz   = aa->nz;
    369:     ai   = aa->i;
    370:     aj   = aa->j;
    371:     PetscMalloc2(nz,&row,nz,&col);
    372:     for (i=k=0; i<M; i++) {
    373:       rnz = ai[i+1] - ai[i];
    374:       ajj = aj + ai[i];
    375:       for (j=0; j<rnz; j++) {
    376:         PetscMUMPSIntCast(i+shift,&row[k]);
    377:         PetscMUMPSIntCast(ajj[j] + shift,&col[k]);
    378:         k++;
    379:       }
    380:     }
    381:     mumps->irn = row;
    382:     mumps->jcn = col;
    383:     mumps->nnz = nz;
    384:   }
    385:   MatSeqAIJRestoreArrayRead(A,&av);
    386:   return(0);
    387: }

    389: PetscErrorCode MatConvertToTriples_seqsell_seqaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    390: {
    392:   PetscInt64     nz,i,j,k,r;
    393:   Mat_SeqSELL    *a=(Mat_SeqSELL*)A->data;
    394:   PetscMUMPSInt  *row,*col;

    397:   mumps->val = a->val;
    398:   if (reuse == MAT_INITIAL_MATRIX) {
    399:     nz   = a->sliidx[a->totalslices];
    400:     PetscMalloc2(nz,&row,nz,&col);
    401:     for (i=k=0; i<a->totalslices; i++) {
    402:       for (j=a->sliidx[i],r=0; j<a->sliidx[i+1]; j++,r=((r+1)&0x07)) {
    403:         PetscMUMPSIntCast(8*i+r+shift,&row[k++]);
    404:       }
    405:     }
    406:     for (i=0;i<nz;i++) {PetscMUMPSIntCast(a->colidx[i]+shift,&col[i]);}
    407:     mumps->irn = row;
    408:     mumps->jcn = col;
    409:     mumps->nnz = nz;
    410:   }
    411:   return(0);
    412: }

    414: PetscErrorCode MatConvertToTriples_seqbaij_seqaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    415: {
    416:   Mat_SeqBAIJ    *aa=(Mat_SeqBAIJ*)A->data;
    417:   const PetscInt *ai,*aj,*ajj,bs2 = aa->bs2;
    418:   PetscInt64     M,nz,idx=0,rnz,i,j,k,m;
    419:   PetscInt       bs;
    421:   PetscMUMPSInt  *row,*col;

    424:   MatGetBlockSize(A,&bs);
    425:   M          = A->rmap->N/bs;
    426:   mumps->val = aa->a;
    427:   if (reuse == MAT_INITIAL_MATRIX) {
    428:     ai   = aa->i; aj = aa->j;
    429:     nz   = bs2*aa->nz;
    430:     PetscMalloc2(nz,&row,nz,&col);
    431:     for (i=0; i<M; i++) {
    432:       ajj = aj + ai[i];
    433:       rnz = ai[i+1] - ai[i];
    434:       for (k=0; k<rnz; k++) {
    435:         for (j=0; j<bs; j++) {
    436:           for (m=0; m<bs; m++) {
    437:             PetscMUMPSIntCast(i*bs + m + shift,&row[idx]);
    438:             PetscMUMPSIntCast(bs*ajj[k] + j + shift,&col[idx]);
    439:             idx++;
    440:           }
    441:         }
    442:       }
    443:     }
    444:     mumps->irn = row;
    445:     mumps->jcn = col;
    446:     mumps->nnz = nz;
    447:   }
    448:   return(0);
    449: }

    451: PetscErrorCode MatConvertToTriples_seqsbaij_seqsbaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    452: {
    453:   const PetscInt *ai, *aj,*ajj;
    454:   PetscInt        bs;
    455:   PetscInt64      nz,rnz,i,j,k,m;
    456:   PetscErrorCode  ierr;
    457:   PetscMUMPSInt   *row,*col;
    458:   PetscScalar     *val;
    459:   Mat_SeqSBAIJ    *aa=(Mat_SeqSBAIJ*)A->data;
    460:   const PetscInt  bs2=aa->bs2,mbs=aa->mbs;
    461: #if defined(PETSC_USE_COMPLEX)
    462:   PetscBool       hermitian;
    463: #endif

    466: #if defined(PETSC_USE_COMPLEX)
    467:   MatGetOption(A,MAT_HERMITIAN,&hermitian);
    468:   if (hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MUMPS does not support Hermitian symmetric matrices for Choleksy");
    469: #endif
    470:   ai   = aa->i;
    471:   aj   = aa->j;
    472:   MatGetBlockSize(A,&bs);
    473:   if (reuse == MAT_INITIAL_MATRIX) {
    474:     nz   = aa->nz;
    475:     PetscMalloc2(bs2*nz,&row,bs2*nz,&col);
    476:     if (bs>1) {
    477:       PetscMalloc1(bs2*nz,&mumps->val_alloc);
    478:       mumps->val = mumps->val_alloc;
    479:     } else {
    480:       mumps->val = aa->a;
    481:     }
    482:     mumps->irn = row;
    483:     mumps->jcn = col;
    484:   } else {
    485:     if (bs == 1) mumps->val = aa->a;
    486:     row = mumps->irn;
    487:     col = mumps->jcn;
    488:   }
    489:   val = mumps->val;

    491:   nz = 0;
    492:   if (bs>1) {
    493:     for (i=0; i<mbs; i++) {
    494:       rnz = ai[i+1] - ai[i];
    495:       ajj = aj + ai[i];
    496:       for (j=0; j<rnz; j++) {
    497:         for (k=0; k<bs; k++) {
    498:           for (m=0; m<bs; m++) {
    499:             if (ajj[j]>i || k>=m) {
    500:               if (reuse == MAT_INITIAL_MATRIX) {
    501:                 PetscMUMPSIntCast(i*bs + m + shift,&row[nz]);
    502:                 PetscMUMPSIntCast(ajj[j]*bs + k + shift,&col[nz]);
    503:               }
    504:               val[nz++] = aa->a[(ai[i]+j)*bs2 + m + k*bs];
    505:             }
    506:           }
    507:         }
    508:       }
    509:     }
    510:   } else if (reuse == MAT_INITIAL_MATRIX) {
    511:     for (i=0; i<mbs; i++) {
    512:       rnz = ai[i+1] - ai[i];
    513:       ajj = aj + ai[i];
    514:       for (j=0; j<rnz; j++) {
    515:         PetscMUMPSIntCast(i+shift,&row[nz]);
    516:         PetscMUMPSIntCast(ajj[j] + shift,&col[nz]);
    517:         nz++;
    518:       }
    519:     }
    520:     if (nz != aa->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Different numbers of nonzeros %D != %D",nz,aa->nz);
    521:   }
    522:   if (reuse == MAT_INITIAL_MATRIX) mumps->nnz = nz;
    523:   return(0);
    524: }

    526: PetscErrorCode MatConvertToTriples_seqaij_seqsbaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    527: {
    528:   const PetscInt    *ai,*aj,*ajj,*adiag,M=A->rmap->n;
    529:   PetscInt64        nz,rnz,i,j;
    530:   const PetscScalar *av,*v1;
    531:   PetscScalar       *val;
    532:   PetscErrorCode    ierr;
    533:   PetscMUMPSInt     *row,*col;
    534:   Mat_SeqAIJ        *aa=(Mat_SeqAIJ*)A->data;
    535:   PetscBool         missing;
    536: #if defined(PETSC_USE_COMPLEX)
    537:   PetscBool         hermitian;
    538: #endif

    541: #if defined(PETSC_USE_COMPLEX)
    542:   MatGetOption(A,MAT_HERMITIAN,&hermitian);
    543:   if (hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MUMPS does not support Hermitian symmetric matrices for Choleksy");
    544: #endif
    545:   MatSeqAIJGetArrayRead(A,&av);
    546:   ai    = aa->i; aj = aa->j;
    547:   adiag = aa->diag;
    548:   MatMissingDiagonal_SeqAIJ(A,&missing,NULL);
    549:   if (reuse == MAT_INITIAL_MATRIX) {
    550:     /* count nz in the upper triangular part of A */
    551:     nz = 0;
    552:     if (missing) {
    553:       for (i=0; i<M; i++) {
    554:         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
    555:           for (j=ai[i];j<ai[i+1];j++) {
    556:             if (aj[j] < i) continue;
    557:             nz++;
    558:           }
    559:         } else {
    560:           nz += ai[i+1] - adiag[i];
    561:         }
    562:       }
    563:     } else {
    564:       for (i=0; i<M; i++) nz += ai[i+1] - adiag[i];
    565:     }
    566:     PetscMalloc2(nz,&row,nz,&col);
    567:     PetscMalloc1(nz,&val);
    568:     mumps->nnz = nz;
    569:     mumps->irn = row;
    570:     mumps->jcn = col;
    571:     mumps->val = mumps->val_alloc = val;

    573:     nz = 0;
    574:     if (missing) {
    575:       for (i=0; i<M; i++) {
    576:         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
    577:           for (j=ai[i];j<ai[i+1];j++) {
    578:             if (aj[j] < i) continue;
    579:             PetscMUMPSIntCast(i+shift,&row[nz]);
    580:             PetscMUMPSIntCast(aj[j]+shift,&col[nz]);
    581:             val[nz] = av[j];
    582:             nz++;
    583:           }
    584:         } else {
    585:           rnz = ai[i+1] - adiag[i];
    586:           ajj = aj + adiag[i];
    587:           v1  = av + adiag[i];
    588:           for (j=0; j<rnz; j++) {
    589:             PetscMUMPSIntCast(i+shift,&row[nz]);
    590:             PetscMUMPSIntCast(ajj[j] + shift,&col[nz]);
    591:             val[nz++] = v1[j];
    592:           }
    593:         }
    594:       }
    595:     } else {
    596:       for (i=0; i<M; i++) {
    597:         rnz = ai[i+1] - adiag[i];
    598:         ajj = aj + adiag[i];
    599:         v1  = av + adiag[i];
    600:         for (j=0; j<rnz; j++) {
    601:           PetscMUMPSIntCast(i+shift,&row[nz]);
    602:           PetscMUMPSIntCast(ajj[j] + shift,&col[nz]);
    603:           val[nz++] = v1[j];
    604:         }
    605:       }
    606:     }
    607:   } else {
    608:     nz = 0;
    609:     val = mumps->val;
    610:     if (missing) {
    611:       for (i=0; i <M; i++) {
    612:         if (PetscUnlikely(adiag[i] >= ai[i+1])) {
    613:           for (j=ai[i];j<ai[i+1];j++) {
    614:             if (aj[j] < i) continue;
    615:             val[nz++] = av[j];
    616:           }
    617:         } else {
    618:           rnz = ai[i+1] - adiag[i];
    619:           v1  = av + adiag[i];
    620:           for (j=0; j<rnz; j++) {
    621:             val[nz++] = v1[j];
    622:           }
    623:         }
    624:       }
    625:     } else {
    626:       for (i=0; i <M; i++) {
    627:         rnz = ai[i+1] - adiag[i];
    628:         v1  = av + adiag[i];
    629:         for (j=0; j<rnz; j++) {
    630:           val[nz++] = v1[j];
    631:         }
    632:       }
    633:     }
    634:   }
    635:   MatSeqAIJRestoreArrayRead(A,&av);
    636:   return(0);
    637: }

    639: PetscErrorCode MatConvertToTriples_mpisbaij_mpisbaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    640: {
    641:   PetscErrorCode    ierr;
    642:   const PetscInt    *ai,*aj,*bi,*bj,*garray,*ajj,*bjj;
    643:   PetscInt          bs;
    644:   PetscInt64        rstart,nz,i,j,k,m,jj,irow,countA,countB;
    645:   PetscMUMPSInt     *row,*col;
    646:   const PetscScalar *av,*bv,*v1,*v2;
    647:   PetscScalar       *val;
    648:   Mat_MPISBAIJ      *mat = (Mat_MPISBAIJ*)A->data;
    649:   Mat_SeqSBAIJ      *aa  = (Mat_SeqSBAIJ*)(mat->A)->data;
    650:   Mat_SeqBAIJ       *bb  = (Mat_SeqBAIJ*)(mat->B)->data;
    651:   const PetscInt    bs2=aa->bs2,mbs=aa->mbs;
    652: #if defined(PETSC_USE_COMPLEX)
    653:   PetscBool         hermitian;
    654: #endif

    657: #if defined(PETSC_USE_COMPLEX)
    658:   MatGetOption(A,MAT_HERMITIAN,&hermitian);
    659:   if (hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MUMPS does not support Hermitian symmetric matrices for Choleksy");
    660: #endif
    661:   MatGetBlockSize(A,&bs);
    662:   rstart = A->rmap->rstart;
    663:   ai = aa->i;
    664:   aj = aa->j;
    665:   bi = bb->i;
    666:   bj = bb->j;
    667:   av = aa->a;
    668:   bv = bb->a;

    670:   garray = mat->garray;

    672:   if (reuse == MAT_INITIAL_MATRIX) {
    673:     nz   = (aa->nz+bb->nz)*bs2; /* just a conservative estimate */
    674:     PetscMalloc2(nz,&row,nz,&col);
    675:     PetscMalloc1(nz,&val);
    676:     /* can not decide the exact mumps->nnz now because of the SBAIJ */
    677:     mumps->irn = row;
    678:     mumps->jcn = col;
    679:     mumps->val = mumps->val_alloc = val;
    680:   } else {
    681:     val = mumps->val;
    682:   }

    684:   jj = 0; irow = rstart;
    685:   for (i=0; i<mbs; i++) {
    686:     ajj    = aj + ai[i];                 /* ptr to the beginning of this row */
    687:     countA = ai[i+1] - ai[i];
    688:     countB = bi[i+1] - bi[i];
    689:     bjj    = bj + bi[i];
    690:     v1     = av + ai[i]*bs2;
    691:     v2     = bv + bi[i]*bs2;

    693:     if (bs>1) {
    694:       /* A-part */
    695:       for (j=0; j<countA; j++) {
    696:         for (k=0; k<bs; k++) {
    697:           for (m=0; m<bs; m++) {
    698:             if (rstart + ajj[j]*bs>irow || k>=m) {
    699:               if (reuse == MAT_INITIAL_MATRIX) {
    700:                 PetscMUMPSIntCast(irow + m + shift,&row[jj]);
    701:                 PetscMUMPSIntCast(rstart + ajj[j]*bs + k + shift,&col[jj]);
    702:               }
    703:               val[jj++] = v1[j*bs2 + m + k*bs];
    704:             }
    705:           }
    706:         }
    707:       }

    709:       /* B-part */
    710:       for (j=0; j < countB; j++) {
    711:         for (k=0; k<bs; k++) {
    712:           for (m=0; m<bs; m++) {
    713:             if (reuse == MAT_INITIAL_MATRIX) {
    714:               PetscMUMPSIntCast(irow + m + shift,&row[jj]);
    715:               PetscMUMPSIntCast(garray[bjj[j]]*bs + k + shift,&col[jj]);
    716:             }
    717:             val[jj++] = v2[j*bs2 + m + k*bs];
    718:           }
    719:         }
    720:       }
    721:     } else {
    722:       /* A-part */
    723:       for (j=0; j<countA; j++) {
    724:         if (reuse == MAT_INITIAL_MATRIX) {
    725:           PetscMUMPSIntCast(irow + shift,&row[jj]);
    726:           PetscMUMPSIntCast(rstart + ajj[j] + shift,&col[jj]);
    727:         }
    728:         val[jj++] = v1[j];
    729:       }

    731:       /* B-part */
    732:       for (j=0; j < countB; j++) {
    733:         if (reuse == MAT_INITIAL_MATRIX) {
    734:           PetscMUMPSIntCast(irow + shift,&row[jj]);
    735:           PetscMUMPSIntCast(garray[bjj[j]] + shift,&col[jj]);
    736:         }
    737:         val[jj++] = v2[j];
    738:       }
    739:     }
    740:     irow+=bs;
    741:   }
    742:   mumps->nnz = jj;
    743:   return(0);
    744: }

    746: PetscErrorCode MatConvertToTriples_mpiaij_mpiaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    747: {
    748:   const PetscInt    *ai, *aj, *bi, *bj,*garray,m=A->rmap->n,*ajj,*bjj;
    749:   PetscErrorCode    ierr;
    750:   PetscInt64        rstart,nz,i,j,jj,irow,countA,countB;
    751:   PetscMUMPSInt     *row,*col;
    752:   const PetscScalar *av, *bv,*v1,*v2;
    753:   PetscScalar       *val;
    754:   Mat               Ad,Ao;
    755:   Mat_SeqAIJ        *aa;
    756:   Mat_SeqAIJ        *bb;

    759:   MatMPIAIJGetSeqAIJ(A,&Ad,&Ao,&garray);
    760:   MatSeqAIJGetArrayRead(Ad,&av);
    761:   MatSeqAIJGetArrayRead(Ao,&bv);

    763:   aa = (Mat_SeqAIJ*)(Ad)->data;
    764:   bb = (Mat_SeqAIJ*)(Ao)->data;
    765:   ai = aa->i;
    766:   aj = aa->j;
    767:   bi = bb->i;
    768:   bj = bb->j;

    770:   rstart = A->rmap->rstart;

    772:   if (reuse == MAT_INITIAL_MATRIX) {
    773:     nz   = (PetscInt64)aa->nz + bb->nz; /* make sure the sum won't overflow PetscInt */
    774:     PetscMalloc2(nz,&row,nz,&col);
    775:     PetscMalloc1(nz,&val);
    776:     mumps->nnz = nz;
    777:     mumps->irn = row;
    778:     mumps->jcn = col;
    779:     mumps->val = mumps->val_alloc = val;
    780:   } else {
    781:     val = mumps->val;
    782:   }

    784:   jj = 0; irow = rstart;
    785:   for (i=0; i<m; i++) {
    786:     ajj    = aj + ai[i];                 /* ptr to the beginning of this row */
    787:     countA = ai[i+1] - ai[i];
    788:     countB = bi[i+1] - bi[i];
    789:     bjj    = bj + bi[i];
    790:     v1     = av + ai[i];
    791:     v2     = bv + bi[i];

    793:     /* A-part */
    794:     for (j=0; j<countA; j++) {
    795:       if (reuse == MAT_INITIAL_MATRIX) {
    796:         PetscMUMPSIntCast(irow + shift,&row[jj]);
    797:         PetscMUMPSIntCast(rstart + ajj[j] + shift,&col[jj]);
    798:       }
    799:       val[jj++] = v1[j];
    800:     }

    802:     /* B-part */
    803:     for (j=0; j < countB; j++) {
    804:       if (reuse == MAT_INITIAL_MATRIX) {
    805:         PetscMUMPSIntCast(irow + shift,&row[jj]);
    806:         PetscMUMPSIntCast(garray[bjj[j]] + shift,&col[jj]);
    807:       }
    808:       val[jj++] = v2[j];
    809:     }
    810:     irow++;
    811:   }
    812:   MatSeqAIJRestoreArrayRead(Ad,&av);
    813:   MatSeqAIJRestoreArrayRead(Ao,&bv);
    814:   return(0);
    815: }

    817: PetscErrorCode MatConvertToTriples_mpibaij_mpiaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    818: {
    819:   Mat_MPIBAIJ       *mat    = (Mat_MPIBAIJ*)A->data;
    820:   Mat_SeqBAIJ       *aa     = (Mat_SeqBAIJ*)(mat->A)->data;
    821:   Mat_SeqBAIJ       *bb     = (Mat_SeqBAIJ*)(mat->B)->data;
    822:   const PetscInt    *ai     = aa->i, *bi = bb->i, *aj = aa->j, *bj = bb->j,*ajj, *bjj;
    823:   const PetscInt    *garray = mat->garray,mbs=mat->mbs,rstart=A->rmap->rstart;
    824:   const PetscInt    bs2=mat->bs2;
    825:   PetscErrorCode    ierr;
    826:   PetscInt          bs;
    827:   PetscInt64        nz,i,j,k,n,jj,irow,countA,countB,idx;
    828:   PetscMUMPSInt     *row,*col;
    829:   const PetscScalar *av=aa->a, *bv=bb->a,*v1,*v2;
    830:   PetscScalar       *val;

    833:   MatGetBlockSize(A,&bs);
    834:   if (reuse == MAT_INITIAL_MATRIX) {
    835:     nz   = bs2*(aa->nz + bb->nz);
    836:     PetscMalloc2(nz,&row,nz,&col);
    837:     PetscMalloc1(nz,&val);
    838:     mumps->nnz = nz;
    839:     mumps->irn = row;
    840:     mumps->jcn = col;
    841:     mumps->val = mumps->val_alloc = val;
    842:   } else {
    843:     val = mumps->val;
    844:   }

    846:   jj = 0; irow = rstart;
    847:   for (i=0; i<mbs; i++) {
    848:     countA = ai[i+1] - ai[i];
    849:     countB = bi[i+1] - bi[i];
    850:     ajj    = aj + ai[i];
    851:     bjj    = bj + bi[i];
    852:     v1     = av + bs2*ai[i];
    853:     v2     = bv + bs2*bi[i];

    855:     idx = 0;
    856:     /* A-part */
    857:     for (k=0; k<countA; k++) {
    858:       for (j=0; j<bs; j++) {
    859:         for (n=0; n<bs; n++) {
    860:           if (reuse == MAT_INITIAL_MATRIX) {
    861:             PetscMUMPSIntCast(irow + n + shift,&row[jj]);
    862:             PetscMUMPSIntCast(rstart + bs*ajj[k] + j + shift,&col[jj]);
    863:           }
    864:           val[jj++] = v1[idx++];
    865:         }
    866:       }
    867:     }

    869:     idx = 0;
    870:     /* B-part */
    871:     for (k=0; k<countB; k++) {
    872:       for (j=0; j<bs; j++) {
    873:         for (n=0; n<bs; n++) {
    874:           if (reuse == MAT_INITIAL_MATRIX) {
    875:             PetscMUMPSIntCast(irow + n + shift,&row[jj]);
    876:             PetscMUMPSIntCast(bs*garray[bjj[k]] + j + shift,&col[jj]);
    877:           }
    878:           val[jj++] = v2[idx++];
    879:         }
    880:       }
    881:     }
    882:     irow += bs;
    883:   }
    884:   return(0);
    885: }

    887: PetscErrorCode MatConvertToTriples_mpiaij_mpisbaij(Mat A,PetscInt shift,MatReuse reuse,Mat_MUMPS *mumps)
    888: {
    889:   const PetscInt    *ai, *aj,*adiag, *bi, *bj,*garray,m=A->rmap->n,*ajj,*bjj;
    890:   PetscErrorCode    ierr;
    891:   PetscInt64        rstart,nz,nza,nzb,i,j,jj,irow,countA,countB;
    892:   PetscMUMPSInt     *row,*col;
    893:   const PetscScalar *av, *bv,*v1,*v2;
    894:   PetscScalar       *val;
    895:   Mat               Ad,Ao;
    896:   Mat_SeqAIJ        *aa;
    897:   Mat_SeqAIJ        *bb;
    898: #if defined(PETSC_USE_COMPLEX)
    899:   PetscBool         hermitian;
    900: #endif

    903: #if defined(PETSC_USE_COMPLEX)
    904:   MatGetOption(A,MAT_HERMITIAN,&hermitian);
    905:   if (hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MUMPS does not support Hermitian symmetric matrices for Choleksy");
    906: #endif
    907:   MatMPIAIJGetSeqAIJ(A,&Ad,&Ao,&garray);
    908:   MatSeqAIJGetArrayRead(Ad,&av);
    909:   MatSeqAIJGetArrayRead(Ao,&bv);

    911:   aa    = (Mat_SeqAIJ*)(Ad)->data;
    912:   bb    = (Mat_SeqAIJ*)(Ao)->data;
    913:   ai    = aa->i;
    914:   aj    = aa->j;
    915:   adiag = aa->diag;
    916:   bi    = bb->i;
    917:   bj    = bb->j;

    919:   rstart = A->rmap->rstart;

    921:   if (reuse == MAT_INITIAL_MATRIX) {
    922:     nza = 0;    /* num of upper triangular entries in mat->A, including diagonals */
    923:     nzb = 0;    /* num of upper triangular entries in mat->B */
    924:     for (i=0; i<m; i++) {
    925:       nza   += (ai[i+1] - adiag[i]);
    926:       countB = bi[i+1] - bi[i];
    927:       bjj    = bj + bi[i];
    928:       for (j=0; j<countB; j++) {
    929:         if (garray[bjj[j]] > rstart) nzb++;
    930:       }
    931:     }

    933:     nz   = nza + nzb; /* total nz of upper triangular part of mat */
    934:     PetscMalloc2(nz,&row,nz,&col);
    935:     PetscMalloc1(nz,&val);
    936:     mumps->nnz = nz;
    937:     mumps->irn = row;
    938:     mumps->jcn = col;
    939:     mumps->val = mumps->val_alloc = val;
    940:   } else {
    941:     val = mumps->val;
    942:   }

    944:   jj = 0; irow = rstart;
    945:   for (i=0; i<m; i++) {
    946:     ajj    = aj + adiag[i];                 /* ptr to the beginning of the diagonal of this row */
    947:     v1     = av + adiag[i];
    948:     countA = ai[i+1] - adiag[i];
    949:     countB = bi[i+1] - bi[i];
    950:     bjj    = bj + bi[i];
    951:     v2     = bv + bi[i];

    953:     /* A-part */
    954:     for (j=0; j<countA; j++) {
    955:       if (reuse == MAT_INITIAL_MATRIX) {
    956:         PetscMUMPSIntCast(irow + shift,&row[jj]);
    957:         PetscMUMPSIntCast(rstart + ajj[j] + shift,&col[jj]);
    958:       }
    959:       val[jj++] = v1[j];
    960:     }

    962:     /* B-part */
    963:     for (j=0; j < countB; j++) {
    964:       if (garray[bjj[j]] > rstart) {
    965:         if (reuse == MAT_INITIAL_MATRIX) {
    966:           PetscMUMPSIntCast(irow + shift,&row[jj]);
    967:           PetscMUMPSIntCast(garray[bjj[j]] + shift,&col[jj]);
    968:         }
    969:         val[jj++] = v2[j];
    970:       }
    971:     }
    972:     irow++;
    973:   }
    974:   MatSeqAIJRestoreArrayRead(Ad,&av);
    975:   MatSeqAIJRestoreArrayRead(Ao,&bv);
    976:   return(0);
    977: }

    979: PetscErrorCode MatDestroy_MUMPS(Mat A)
    980: {
    982:   Mat_MUMPS      *mumps=(Mat_MUMPS*)A->data;

    985:   PetscFree2(mumps->id.sol_loc,mumps->id.isol_loc);
    986:   VecScatterDestroy(&mumps->scat_rhs);
    987:   VecScatterDestroy(&mumps->scat_sol);
    988:   VecDestroy(&mumps->b_seq);
    989:   VecDestroy(&mumps->x_seq);
    990:   PetscFree(mumps->id.perm_in);
    991:   PetscFree2(mumps->irn,mumps->jcn);
    992:   PetscFree(mumps->val_alloc);
    993:   PetscFree(mumps->info);
    994:   MatMumpsResetSchur_Private(mumps);
    995:   mumps->id.job = JOB_END;
    996:   PetscMUMPS_c(mumps);
    997:   if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in MatDestroy_MUMPS: INFOG(1)=%d\n",mumps->id.INFOG(1));
    998: #if defined(PETSC_HAVE_OPENMP_SUPPORT)
    999:   if (mumps->use_petsc_omp_support) {
    1000:     PetscOmpCtrlDestroy(&mumps->omp_ctrl);
    1001:     PetscFree2(mumps->rhs_loc,mumps->rhs_recvbuf);
    1002:     PetscFree3(mumps->rhs_nrow,mumps->rhs_recvcounts,mumps->rhs_disps);
    1003:   }
    1004: #endif
    1005:   PetscFree(mumps->ia_alloc);
    1006:   PetscFree(mumps->ja_alloc);
    1007:   PetscFree(mumps->recvcount);
    1008:   PetscFree(mumps->reqs);
    1009:   PetscFree(mumps->irhs_loc);
    1010:   if (mumps->mumps_comm != MPI_COMM_NULL) {MPI_Comm_free(&mumps->mumps_comm);}
    1011:   PetscFree(A->data);

    1013:   /* clear composed functions */
    1014:   PetscObjectComposeFunction((PetscObject)A,"MatFactorGetSolverType_C",NULL);
    1015:   PetscObjectComposeFunction((PetscObject)A,"MatFactorSetSchurIS_C",NULL);
    1016:   PetscObjectComposeFunction((PetscObject)A,"MatFactorCreateSchurComplement_C",NULL);
    1017:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsSetIcntl_C",NULL);
    1018:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetIcntl_C",NULL);
    1019:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsSetCntl_C",NULL);
    1020:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetCntl_C",NULL);
    1021:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInfo_C",NULL);
    1022:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInfog_C",NULL);
    1023:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetRinfo_C",NULL);
    1024:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetRinfog_C",NULL);
    1025:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInverse_C",NULL);
    1026:   PetscObjectComposeFunction((PetscObject)A,"MatMumpsGetInverseTranspose_C",NULL);
    1027:   return(0);
    1028: }

    1030: /* Set up the distributed RHS info for MUMPS. <nrhs> is the number of RHS. <array> points to start of RHS on the local processor. */
    1031: static PetscErrorCode MatMumpsSetUpDistRHSInfo(Mat A,PetscInt nrhs,const PetscScalar *array)
    1032: {
    1033:   PetscErrorCode     ierr;
    1034:   Mat_MUMPS          *mumps=(Mat_MUMPS*)A->data;
    1035:   const PetscMPIInt  ompsize=mumps->omp_comm_size;
    1036:   PetscInt           i,m,M,rstart;

    1039:   MatGetSize(A,&M,NULL);
    1040:   MatGetLocalSize(A,&m,NULL);
    1041:   if (M > PETSC_MUMPS_INT_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"PetscInt too long for PetscMUMPSInt");
    1042:   if (ompsize == 1) {
    1043:     if (!mumps->irhs_loc) {
    1044:       mumps->nloc_rhs = m;
    1045:       PetscMalloc1(m,&mumps->irhs_loc);
    1046:       MatGetOwnershipRange(A,&rstart,NULL);
    1047:       for (i=0; i<m; i++) mumps->irhs_loc[i] = rstart+i+1; /* use 1-based indices */
    1048:     }
    1049:     mumps->id.rhs_loc = (MumpsScalar*)array;
    1050:   } else {
    1051:   #if defined(PETSC_HAVE_OPENMP_SUPPORT)
    1052:     const PetscInt  *ranges;
    1053:     PetscMPIInt     j,k,sendcount,*petsc_ranks,*omp_ranks;
    1054:     MPI_Group       petsc_group,omp_group;
    1055:     PetscScalar     *recvbuf=NULL;

    1057:     if (mumps->is_omp_master) {
    1058:       /* Lazily initialize the omp stuff for distributed rhs */
    1059:       if (!mumps->irhs_loc) {
    1060:         PetscMalloc2(ompsize,&omp_ranks,ompsize,&petsc_ranks);
    1061:         PetscMalloc3(ompsize,&mumps->rhs_nrow,ompsize,&mumps->rhs_recvcounts,ompsize,&mumps->rhs_disps);
    1062:         MPI_Comm_group(mumps->petsc_comm,&petsc_group);
    1063:         MPI_Comm_group(mumps->omp_comm,&omp_group);
    1064:         for (j=0; j<ompsize; j++) omp_ranks[j] = j;
    1065:         MPI_Group_translate_ranks(omp_group,ompsize,omp_ranks,petsc_group,petsc_ranks);

    1067:         /* Populate mumps->irhs_loc[], rhs_nrow[] */
    1068:         mumps->nloc_rhs = 0;
    1069:         MatGetOwnershipRanges(A,&ranges);
    1070:         for (j=0; j<ompsize; j++) {
    1071:           mumps->rhs_nrow[j] = ranges[petsc_ranks[j]+1] - ranges[petsc_ranks[j]];
    1072:           mumps->nloc_rhs   += mumps->rhs_nrow[j];
    1073:         }
    1074:         PetscMalloc1(mumps->nloc_rhs,&mumps->irhs_loc);
    1075:         for (j=k=0; j<ompsize; j++) {
    1076:           for (i=ranges[petsc_ranks[j]]; i<ranges[petsc_ranks[j]+1]; i++,k++) mumps->irhs_loc[k] = i+1; /* uses 1-based indices */
    1077:         }

    1079:         PetscFree2(omp_ranks,petsc_ranks);
    1080:         MPI_Group_free(&petsc_group);
    1081:         MPI_Group_free(&omp_group);
    1082:       }

    1084:       /* Realloc buffers when current nrhs is bigger than what we have met */
    1085:       if (nrhs > mumps->max_nrhs) {
    1086:         PetscFree2(mumps->rhs_loc,mumps->rhs_recvbuf);
    1087:         PetscMalloc2(mumps->nloc_rhs*nrhs,&mumps->rhs_loc,mumps->nloc_rhs*nrhs,&mumps->rhs_recvbuf);
    1088:         mumps->max_nrhs = nrhs;
    1089:       }

    1091:       /* Setup recvcounts[], disps[], recvbuf on omp rank 0 for the upcoming MPI_Gatherv */
    1092:       for (j=0; j<ompsize; j++) {PetscMPIIntCast(mumps->rhs_nrow[j]*nrhs,&mumps->rhs_recvcounts[j]);}
    1093:       mumps->rhs_disps[0] = 0;
    1094:       for (j=1; j<ompsize; j++) {
    1095:         mumps->rhs_disps[j] = mumps->rhs_disps[j-1] + mumps->rhs_recvcounts[j-1];
    1096:         if (mumps->rhs_disps[j] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"PetscMPIInt overflow!");
    1097:       }
    1098:       recvbuf = (nrhs == 1) ? mumps->rhs_loc : mumps->rhs_recvbuf; /* Directly use rhs_loc[] as recvbuf. Single rhs is common in Ax=b */
    1099:     }

    1101:     PetscMPIIntCast(m*nrhs,&sendcount);
    1102:     MPI_Gatherv(array,sendcount,MPIU_SCALAR,recvbuf,mumps->rhs_recvcounts,mumps->rhs_disps,MPIU_SCALAR,0,mumps->omp_comm);

    1104:     if (mumps->is_omp_master) {
    1105:       if (nrhs > 1) { /* Copy & re-arrange data from rhs_recvbuf[] to mumps->rhs_loc[] only when there are multiple rhs */
    1106:         PetscScalar *dst,*dstbase = mumps->rhs_loc;
    1107:         for (j=0; j<ompsize; j++) {
    1108:           const PetscScalar *src = mumps->rhs_recvbuf + mumps->rhs_disps[j];
    1109:           dst = dstbase;
    1110:           for (i=0; i<nrhs; i++) {
    1111:             PetscArraycpy(dst,src,mumps->rhs_nrow[j]);
    1112:             src += mumps->rhs_nrow[j];
    1113:             dst += mumps->nloc_rhs;
    1114:           }
    1115:           dstbase += mumps->rhs_nrow[j];
    1116:         }
    1117:       }
    1118:       mumps->id.rhs_loc = (MumpsScalar*)mumps->rhs_loc;
    1119:     }
    1120:   #endif /* PETSC_HAVE_OPENMP_SUPPORT */
    1121:   }
    1122:   mumps->id.nrhs     = nrhs;
    1123:   mumps->id.nloc_rhs = mumps->nloc_rhs;
    1124:   mumps->id.lrhs_loc = mumps->nloc_rhs;
    1125:   mumps->id.irhs_loc = mumps->irhs_loc;
    1126:   return(0);
    1127: }

    1129: PetscErrorCode MatSolve_MUMPS(Mat A,Vec b,Vec x)
    1130: {
    1131:   Mat_MUMPS          *mumps=(Mat_MUMPS*)A->data;
    1132:   const PetscScalar  *rarray = NULL;
    1133:   PetscScalar        *array;
    1134:   IS                 is_iden,is_petsc;
    1135:   PetscErrorCode     ierr;
    1136:   PetscInt           i;
    1137:   PetscBool          second_solve = PETSC_FALSE;
    1138:   static PetscBool   cite1 = PETSC_FALSE,cite2 = PETSC_FALSE;

    1141:   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);
    1142:   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);

    1144:   if (A->factorerrortype) {
    1145:     PetscInfo2(A,"MatSolve is called with singular matrix factor, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1146:     VecSetInf(x);
    1147:     return(0);
    1148:   }

    1150:   mumps->id.nrhs = 1;
    1151:   if (mumps->petsc_size > 1) {
    1152:     if (mumps->ICNTL20 == 10) {
    1153:       mumps->id.ICNTL(20) = 10; /* dense distributed RHS */
    1154:       VecGetArrayRead(b,&rarray);
    1155:       MatMumpsSetUpDistRHSInfo(A,1,rarray);
    1156:     } else {
    1157:       mumps->id.ICNTL(20) = 0; /* dense centralized RHS; Scatter b into a sequential rhs vector*/
    1158:       VecScatterBegin(mumps->scat_rhs,b,mumps->b_seq,INSERT_VALUES,SCATTER_FORWARD);
    1159:       VecScatterEnd(mumps->scat_rhs,b,mumps->b_seq,INSERT_VALUES,SCATTER_FORWARD);
    1160:       if (!mumps->myid) {
    1161:         VecGetArray(mumps->b_seq,&array);
    1162:         mumps->id.rhs = (MumpsScalar*)array;
    1163:       }
    1164:     }
    1165:   } else {  /* petsc_size == 1 */
    1166:     mumps->id.ICNTL(20) = 0; /* dense centralized RHS */
    1167:     VecCopy(b,x);
    1168:     VecGetArray(x,&array);
    1169:     mumps->id.rhs = (MumpsScalar*)array;
    1170:   }

    1172:   /*
    1173:      handle condensation step of Schur complement (if any)
    1174:      We set by default ICNTL(26) == -1 when Schur indices have been provided by the user.
    1175:      According to MUMPS (5.0.0) manual, any value should be harmful during the factorization phase
    1176:      Unless the user provides a valid value for ICNTL(26), MatSolve and MatMatSolve routines solve the full system.
    1177:      This requires an extra call to PetscMUMPS_c and the computation of the factors for S
    1178:   */
    1179:   if (mumps->id.size_schur > 0 && (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2)) {
    1180:     if (mumps->petsc_size > 1) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Parallel Schur complements not yet supported from PETSc\n");
    1181:     second_solve = PETSC_TRUE;
    1182:     MatMumpsHandleSchur_Private(A,PETSC_FALSE);
    1183:   }
    1184:   /* solve phase */
    1185:   /*-------------*/
    1186:   mumps->id.job = JOB_SOLVE;
    1187:   PetscMUMPS_c(mumps);
    1188:   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));

    1190:   /* handle expansion step of Schur complement (if any) */
    1191:   if (second_solve) {
    1192:     MatMumpsHandleSchur_Private(A,PETSC_TRUE);
    1193:   }

    1195:   if (mumps->petsc_size > 1) { /* convert mumps distributed solution to petsc mpi x */
    1196:     if (mumps->scat_sol && mumps->ICNTL9_pre != mumps->id.ICNTL(9)) {
    1197:       /* when id.ICNTL(9) changes, the contents of lsol_loc may change (not its size, lsol_loc), recreates scat_sol */
    1198:       VecScatterDestroy(&mumps->scat_sol);
    1199:     }
    1200:     if (!mumps->scat_sol) { /* create scatter scat_sol */
    1201:       PetscInt *isol2_loc=NULL;
    1202:       ISCreateStride(PETSC_COMM_SELF,mumps->id.lsol_loc,0,1,&is_iden); /* from */
    1203:       PetscMalloc1(mumps->id.lsol_loc,&isol2_loc);
    1204:       for (i=0; i<mumps->id.lsol_loc; i++) isol2_loc[i] = mumps->id.isol_loc[i]-1; /* change Fortran style to C style */
    1205:       ISCreateGeneral(PETSC_COMM_SELF,mumps->id.lsol_loc,isol2_loc,PETSC_OWN_POINTER,&is_petsc);  /* to */
    1206:       VecScatterCreate(mumps->x_seq,is_iden,x,is_petsc,&mumps->scat_sol);
    1207:       ISDestroy(&is_iden);
    1208:       ISDestroy(&is_petsc);
    1209:       mumps->ICNTL9_pre = mumps->id.ICNTL(9); /* save current value of id.ICNTL(9) */
    1210:     }

    1212:     VecScatterBegin(mumps->scat_sol,mumps->x_seq,x,INSERT_VALUES,SCATTER_FORWARD);
    1213:     VecScatterEnd(mumps->scat_sol,mumps->x_seq,x,INSERT_VALUES,SCATTER_FORWARD);
    1214:   }

    1216:   if (mumps->petsc_size > 1) {
    1217:     if (mumps->ICNTL20 == 10) {
    1218:       VecRestoreArrayRead(b,&rarray);
    1219:     } else if (!mumps->myid) {
    1220:       VecRestoreArray(mumps->b_seq,&array);
    1221:     }
    1222:   } else {VecRestoreArray(x,&array);}

    1224:   PetscLogFlops(2.0*mumps->id.RINFO(3));
    1225:   return(0);
    1226: }

    1228: PetscErrorCode MatSolveTranspose_MUMPS(Mat A,Vec b,Vec x)
    1229: {
    1230:   Mat_MUMPS      *mumps=(Mat_MUMPS*)A->data;

    1234:   mumps->id.ICNTL(9) = 0;
    1235:   MatSolve_MUMPS(A,b,x);
    1236:   mumps->id.ICNTL(9) = 1;
    1237:   return(0);
    1238: }

    1240: PetscErrorCode MatMatSolve_MUMPS(Mat A,Mat B,Mat X)
    1241: {
    1242:   PetscErrorCode    ierr;
    1243:   Mat               Bt = NULL;
    1244:   PetscBool         denseX,denseB,flg,flgT;
    1245:   Mat_MUMPS         *mumps=(Mat_MUMPS*)A->data;
    1246:   PetscInt          i,nrhs,M;
    1247:   PetscScalar       *array;
    1248:   const PetscScalar *rbray;
    1249:   PetscInt          lsol_loc,nlsol_loc,*idxx,iidx = 0;
    1250:   PetscMUMPSInt     *isol_loc,*isol_loc_save;
    1251:   PetscScalar       *bray,*sol_loc,*sol_loc_save;
    1252:   IS                is_to,is_from;
    1253:   PetscInt          k,proc,j,m,myrstart;
    1254:   const PetscInt    *rstart;
    1255:   Vec               v_mpi,msol_loc;
    1256:   VecScatter        scat_sol;
    1257:   Vec               b_seq;
    1258:   VecScatter        scat_rhs;
    1259:   PetscScalar       *aa;
    1260:   PetscInt          spnr,*ia,*ja;
    1261:   Mat_MPIAIJ        *b = NULL;

    1264:   PetscObjectTypeCompareAny((PetscObject)X,&denseX,MATSEQDENSE,MATMPIDENSE,NULL);
    1265:   if (!denseX) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix");

    1267:   PetscObjectTypeCompareAny((PetscObject)B,&denseB,MATSEQDENSE,MATMPIDENSE,NULL);
    1268:   if (denseB) {
    1269:     if (B->rmap->n != X->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Matrix B and X must have same row distribution");
    1270:     mumps->id.ICNTL(20)= 0; /* dense RHS */
    1271:   } else { /* sparse B */
    1272:     if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
    1273:     PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&flgT);
    1274:     if (flgT) { /* input B is transpose of actural RHS matrix,
    1275:                  because mumps requires sparse compressed COLUMN storage! See MatMatTransposeSolve_MUMPS() */
    1276:       MatTransposeGetMat(B,&Bt);
    1277:     } else SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONG,"Matrix B must be MATTRANSPOSEMAT matrix");
    1278:     mumps->id.ICNTL(20)= 1; /* sparse RHS */
    1279:   }

    1281:   MatGetSize(B,&M,&nrhs);
    1282:   mumps->id.nrhs = nrhs;
    1283:   mumps->id.lrhs = M;
    1284:   mumps->id.rhs  = NULL;

    1286:   if (mumps->petsc_size == 1) {
    1287:     PetscScalar *aa;
    1288:     PetscInt    spnr,*ia,*ja;
    1289:     PetscBool   second_solve = PETSC_FALSE;

    1291:     MatDenseGetArray(X,&array);
    1292:     mumps->id.rhs = (MumpsScalar*)array;

    1294:     if (denseB) {
    1295:       /* copy B to X */
    1296:       MatDenseGetArrayRead(B,&rbray);
    1297:       PetscArraycpy(array,rbray,M*nrhs);
    1298:       MatDenseRestoreArrayRead(B,&rbray);
    1299:     } else { /* sparse B */
    1300:       MatSeqAIJGetArray(Bt,&aa);
    1301:       MatGetRowIJ(Bt,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    1302:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
    1303:       PetscMUMPSIntCSRCast(mumps,spnr,ia,ja,&mumps->id.irhs_ptr,&mumps->id.irhs_sparse,&mumps->id.nz_rhs);
    1304:       mumps->id.rhs_sparse  = (MumpsScalar*)aa;
    1305:     }
    1306:     /* handle condensation step of Schur complement (if any) */
    1307:     if (mumps->id.size_schur > 0 && (mumps->id.ICNTL(26) < 0 || mumps->id.ICNTL(26) > 2)) {
    1308:       second_solve = PETSC_TRUE;
    1309:       MatMumpsHandleSchur_Private(A,PETSC_FALSE);
    1310:     }
    1311:     /* solve phase */
    1312:     /*-------------*/
    1313:     mumps->id.job = JOB_SOLVE;
    1314:     PetscMUMPS_c(mumps);
    1315:     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));

    1317:     /* handle expansion step of Schur complement (if any) */
    1318:     if (second_solve) {
    1319:       MatMumpsHandleSchur_Private(A,PETSC_TRUE);
    1320:     }
    1321:     if (!denseB) { /* sparse B */
    1322:       MatSeqAIJRestoreArray(Bt,&aa);
    1323:       MatRestoreRowIJ(Bt,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    1324:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore IJ structure");
    1325:     }
    1326:     MatDenseRestoreArray(X,&array);
    1327:     return(0);
    1328:   }

    1330:   /*--------- parallel case: MUMPS requires rhs B to be centralized on the host! --------*/
    1331:   if (mumps->petsc_size > 1 && mumps->id.ICNTL(19)) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Parallel Schur complements not yet supported from PETSc\n");

    1333:   /* create msol_loc to hold mumps local solution */
    1334:   isol_loc_save = mumps->id.isol_loc; /* save it for MatSolve() */
    1335:   sol_loc_save  = (PetscScalar*)mumps->id.sol_loc;

    1337:   lsol_loc  = mumps->id.lsol_loc;
    1338:   nlsol_loc = nrhs*lsol_loc;     /* length of sol_loc */
    1339:   PetscMalloc2(nlsol_loc,&sol_loc,lsol_loc,&isol_loc);
    1340:   mumps->id.sol_loc  = (MumpsScalar*)sol_loc;
    1341:   mumps->id.isol_loc = isol_loc;

    1343:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nlsol_loc,(PetscScalar*)sol_loc,&msol_loc);

    1345:   if (denseB) {
    1346:     if (mumps->ICNTL20 == 10) {
    1347:       mumps->id.ICNTL(20) = 10; /* dense distributed RHS */
    1348:       MatDenseGetArrayRead(B,&rbray);
    1349:       MatMumpsSetUpDistRHSInfo(A,nrhs,rbray);
    1350:       MatDenseRestoreArrayRead(B,&rbray);
    1351:       MatGetLocalSize(B,&m,NULL);
    1352:       VecCreateMPIWithArray(PetscObjectComm((PetscObject)B),1,nrhs*m,nrhs*M,NULL,&v_mpi);
    1353:     } else {
    1354:       mumps->id.ICNTL(20) = 0; /* dense centralized RHS */
    1355:       /* TODO: Because of non-contiguous indices, the created vecscatter scat_rhs is not done in MPI_Gather, resulting in
    1356:         very inefficient communication. An optimization is to use VecScatterCreateToZero to gather B to rank 0. Then on rank
    1357:         0, re-arrange B into desired order, which is a local operation.
    1358:       */

    1360:       /* scatter v_mpi to b_seq because MUMPS before 5.3.0 only supports centralized rhs */
    1361:       /* wrap dense rhs matrix B into a vector v_mpi */
    1362:       MatGetLocalSize(B,&m,NULL);
    1363:       MatDenseGetArray(B,&bray);
    1364:       VecCreateMPIWithArray(PetscObjectComm((PetscObject)B),1,nrhs*m,nrhs*M,(const PetscScalar*)bray,&v_mpi);
    1365:       MatDenseRestoreArray(B,&bray);

    1367:       /* scatter v_mpi to b_seq in proc[0]. MUMPS requires rhs to be centralized on the host! */
    1368:       if (!mumps->myid) {
    1369:         PetscInt *idx;
    1370:         /* idx: maps from k-th index of v_mpi to (i,j)-th global entry of B */
    1371:         PetscMalloc1(nrhs*M,&idx);
    1372:         MatGetOwnershipRanges(B,&rstart);
    1373:         k = 0;
    1374:         for (proc=0; proc<mumps->petsc_size; proc++) {
    1375:           for (j=0; j<nrhs; j++) {
    1376:             for (i=rstart[proc]; i<rstart[proc+1]; i++) idx[k++] = j*M + i;
    1377:           }
    1378:         }

    1380:         VecCreateSeq(PETSC_COMM_SELF,nrhs*M,&b_seq);
    1381:         ISCreateGeneral(PETSC_COMM_SELF,nrhs*M,idx,PETSC_OWN_POINTER,&is_to);
    1382:         ISCreateStride(PETSC_COMM_SELF,nrhs*M,0,1,&is_from);
    1383:       } else {
    1384:         VecCreateSeq(PETSC_COMM_SELF,0,&b_seq);
    1385:         ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_to);
    1386:         ISCreateStride(PETSC_COMM_SELF,0,0,1,&is_from);
    1387:       }
    1388:       VecScatterCreate(v_mpi,is_from,b_seq,is_to,&scat_rhs);
    1389:       VecScatterBegin(scat_rhs,v_mpi,b_seq,INSERT_VALUES,SCATTER_FORWARD);
    1390:       ISDestroy(&is_to);
    1391:       ISDestroy(&is_from);
    1392:       VecScatterEnd(scat_rhs,v_mpi,b_seq,INSERT_VALUES,SCATTER_FORWARD);

    1394:       if (!mumps->myid) { /* define rhs on the host */
    1395:         VecGetArray(b_seq,&bray);
    1396:         mumps->id.rhs = (MumpsScalar*)bray;
    1397:         VecRestoreArray(b_seq,&bray);
    1398:       }
    1399:     }
    1400:   } else { /* sparse B */
    1401:     b = (Mat_MPIAIJ*)Bt->data;

    1403:     /* wrap dense X into a vector v_mpi */
    1404:     MatGetLocalSize(X,&m,NULL);
    1405:     MatDenseGetArray(X,&bray);
    1406:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)X),1,nrhs*m,nrhs*M,(const PetscScalar*)bray,&v_mpi);
    1407:     MatDenseRestoreArray(X,&bray);

    1409:     if (!mumps->myid) {
    1410:       MatSeqAIJGetArray(b->A,&aa);
    1411:       MatGetRowIJ(b->A,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    1412:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
    1413:       PetscMUMPSIntCSRCast(mumps,spnr,ia,ja,&mumps->id.irhs_ptr,&mumps->id.irhs_sparse,&mumps->id.nz_rhs);
    1414:       mumps->id.rhs_sparse  = (MumpsScalar*)aa;
    1415:     } else {
    1416:       mumps->id.irhs_ptr    = NULL;
    1417:       mumps->id.irhs_sparse = NULL;
    1418:       mumps->id.nz_rhs      = 0;
    1419:       mumps->id.rhs_sparse  = NULL;
    1420:     }
    1421:   }

    1423:   /* solve phase */
    1424:   /*-------------*/
    1425:   mumps->id.job = JOB_SOLVE;
    1426:   PetscMUMPS_c(mumps);
    1427:   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));

    1429:   /* scatter mumps distributed solution to petsc vector v_mpi, which shares local arrays with solution matrix X */
    1430:   MatDenseGetArray(X,&array);
    1431:   VecPlaceArray(v_mpi,array);

    1433:   /* create scatter scat_sol */
    1434:   MatGetOwnershipRanges(X,&rstart);
    1435:   /* iidx: index for scatter mumps solution to petsc X */

    1437:   ISCreateStride(PETSC_COMM_SELF,nlsol_loc,0,1,&is_from);
    1438:   PetscMalloc1(nlsol_loc,&idxx);
    1439:   for (i=0; i<lsol_loc; i++) {
    1440:     isol_loc[i] -= 1; /* change Fortran style to C style. isol_loc[i+j*lsol_loc] contains x[isol_loc[i]] in j-th vector */

    1442:     for (proc=0; proc<mumps->petsc_size; proc++) {
    1443:       if (isol_loc[i] >= rstart[proc] && isol_loc[i] < rstart[proc+1]) {
    1444:         myrstart = rstart[proc];
    1445:         k        = isol_loc[i] - myrstart;        /* local index on 1st column of petsc vector X */
    1446:         iidx     = k + myrstart*nrhs;             /* maps mumps isol_loc[i] to petsc index in X */
    1447:         m        = rstart[proc+1] - rstart[proc]; /* rows of X for this proc */
    1448:         break;
    1449:       }
    1450:     }

    1452:     for (j=0; j<nrhs; j++) idxx[i+j*lsol_loc] = iidx + j*m;
    1453:   }
    1454:   ISCreateGeneral(PETSC_COMM_SELF,nlsol_loc,idxx,PETSC_COPY_VALUES,&is_to);
    1455:   VecScatterCreate(msol_loc,is_from,v_mpi,is_to,&scat_sol);
    1456:   VecScatterBegin(scat_sol,msol_loc,v_mpi,INSERT_VALUES,SCATTER_FORWARD);
    1457:   ISDestroy(&is_from);
    1458:   ISDestroy(&is_to);
    1459:   VecScatterEnd(scat_sol,msol_loc,v_mpi,INSERT_VALUES,SCATTER_FORWARD);
    1460:   MatDenseRestoreArray(X,&array);

    1462:   /* free spaces */
    1463:   mumps->id.sol_loc  = (MumpsScalar*)sol_loc_save;
    1464:   mumps->id.isol_loc = isol_loc_save;

    1466:   PetscFree2(sol_loc,isol_loc);
    1467:   PetscFree(idxx);
    1468:   VecDestroy(&msol_loc);
    1469:   VecDestroy(&v_mpi);
    1470:   if (!denseB) {
    1471:     if (!mumps->myid) {
    1472:       b = (Mat_MPIAIJ*)Bt->data;
    1473:       MatSeqAIJRestoreArray(b->A,&aa);
    1474:       MatRestoreRowIJ(b->A,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    1475:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore IJ structure");
    1476:     }
    1477:   } else {
    1478:     if (mumps->ICNTL20 == 0) {
    1479:       VecDestroy(&b_seq);
    1480:       VecScatterDestroy(&scat_rhs);
    1481:     }
    1482:   }
    1483:   VecScatterDestroy(&scat_sol);
    1484:   PetscLogFlops(2.0*nrhs*mumps->id.RINFO(3));
    1485:   return(0);
    1486: }

    1488: PetscErrorCode MatMatTransposeSolve_MUMPS(Mat A,Mat Bt,Mat X)
    1489: {
    1491:   PetscBool      flg;
    1492:   Mat            B;

    1495:   PetscObjectTypeCompareAny((PetscObject)Bt,&flg,MATSEQAIJ,MATMPIAIJ,NULL);
    1496:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)Bt),PETSC_ERR_ARG_WRONG,"Matrix Bt must be MATAIJ matrix");

    1498:   /* Create B=Bt^T that uses Bt's data structure */
    1499:   MatCreateTranspose(Bt,&B);

    1501:   MatMatSolve_MUMPS(A,B,X);
    1502:   MatDestroy(&B);
    1503:   return(0);
    1504: }

    1506: #if !defined(PETSC_USE_COMPLEX)
    1507: /*
    1508:   input:
    1509:    F:        numeric factor
    1510:   output:
    1511:    nneg:     total number of negative pivots
    1512:    nzero:    total number of zero pivots
    1513:    npos:     (global dimension of F) - nneg - nzero
    1514: */
    1515: PetscErrorCode MatGetInertia_SBAIJMUMPS(Mat F,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
    1516: {
    1517:   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
    1519:   PetscMPIInt    size;

    1522:   MPI_Comm_size(PetscObjectComm((PetscObject)F),&size);
    1523:   /* 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 */
    1524:   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));

    1526:   if (nneg) *nneg = mumps->id.INFOG(12);
    1527:   if (nzero || npos) {
    1528:     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");
    1529:     if (nzero) *nzero = mumps->id.INFOG(28);
    1530:     if (npos) *npos   = F->rmap->N - (mumps->id.INFOG(12) + mumps->id.INFOG(28));
    1531:   }
    1532:   return(0);
    1533: }
    1534: #endif

    1536: PetscErrorCode MatMumpsGatherNonzerosOnMaster(MatReuse reuse,Mat_MUMPS *mumps)
    1537: {
    1539:   PetscInt       i,nreqs;
    1540:   PetscMUMPSInt  *irn,*jcn;
    1541:   PetscMPIInt    count;
    1542:   PetscInt64     totnnz,remain;
    1543:   const PetscInt osize=mumps->omp_comm_size;
    1544:   PetscScalar    *val;

    1547:   if (osize > 1) {
    1548:     if (reuse == MAT_INITIAL_MATRIX) {
    1549:       /* master first gathers counts of nonzeros to receive */
    1550:       if (mumps->is_omp_master) {PetscMalloc1(osize,&mumps->recvcount);}
    1551:       MPI_Gather(&mumps->nnz,1,MPIU_INT64,mumps->recvcount,1,MPIU_INT64,0/*master*/,mumps->omp_comm);

    1553:       /* Then each computes number of send/recvs */
    1554:       if (mumps->is_omp_master) {
    1555:         /* Start from 1 since self communication is not done in MPI */
    1556:         nreqs = 0;
    1557:         for (i=1; i<osize; i++) nreqs += (mumps->recvcount[i]+PETSC_MPI_INT_MAX-1)/PETSC_MPI_INT_MAX;
    1558:       } else {
    1559:         nreqs = (mumps->nnz+PETSC_MPI_INT_MAX-1)/PETSC_MPI_INT_MAX;
    1560:       }
    1561:       PetscMalloc1(nreqs*3,&mumps->reqs); /* Triple the requests since we send irn, jcn and val seperately */

    1563:       /* The following code is doing a very simple thing: omp_master rank gathers irn/jcn/val from others.
    1564:          MPI_Gatherv would be enough if it supports big counts > 2^31-1. Since it does not, and mumps->nnz
    1565:          might be a prime number > 2^31-1, we have to slice the message. Note omp_comm_size
    1566:          is very small, the current approach should have no extra overhead compared to MPI_Gatherv.
    1567:        */
    1568:       nreqs = 0; /* counter for actual send/recvs */
    1569:       if (mumps->is_omp_master) {
    1570:         for (i=0,totnnz=0; i<osize; i++) totnnz += mumps->recvcount[i]; /* totnnz = sum of nnz over omp_comm */
    1571:         PetscMalloc2(totnnz,&irn,totnnz,&jcn);
    1572:         PetscMalloc1(totnnz,&val);

    1574:         /* Self communication */
    1575:         PetscArraycpy(irn,mumps->irn,mumps->nnz);
    1576:         PetscArraycpy(jcn,mumps->jcn,mumps->nnz);
    1577:         PetscArraycpy(val,mumps->val,mumps->nnz);

    1579:         /* Replace mumps->irn/jcn etc on master with the newly allocated bigger arrays */
    1580:         PetscFree2(mumps->irn,mumps->jcn);
    1581:         PetscFree(mumps->val_alloc);
    1582:         mumps->nnz = totnnz;
    1583:         mumps->irn = irn;
    1584:         mumps->jcn = jcn;
    1585:         mumps->val = mumps->val_alloc = val;

    1587:         irn += mumps->recvcount[0]; /* recvcount[0] is old mumps->nnz on omp rank 0 */
    1588:         jcn += mumps->recvcount[0];
    1589:         val += mumps->recvcount[0];

    1591:         /* Remote communication */
    1592:         for (i=1; i<osize; i++) {
    1593:           count  = PetscMin(mumps->recvcount[i],PETSC_MPI_INT_MAX);
    1594:           remain = mumps->recvcount[i] - count;
    1595:           while (count>0) {
    1596:             MPI_Irecv(irn,count,MPIU_MUMPSINT,i,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1597:             MPI_Irecv(jcn,count,MPIU_MUMPSINT,i,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1598:             MPI_Irecv(val,count,MPIU_SCALAR,  i,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1599:             irn    += count;
    1600:             jcn    += count;
    1601:             val    += count;
    1602:             count   = PetscMin(remain,PETSC_MPI_INT_MAX);
    1603:             remain -= count;
    1604:           }
    1605:         }
    1606:       } else {
    1607:         irn    = mumps->irn;
    1608:         jcn    = mumps->jcn;
    1609:         val    = mumps->val;
    1610:         count  = PetscMin(mumps->nnz,PETSC_MPI_INT_MAX);
    1611:         remain = mumps->nnz - count;
    1612:         while (count>0) {
    1613:           MPI_Isend(irn,count,MPIU_MUMPSINT,0,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1614:           MPI_Isend(jcn,count,MPIU_MUMPSINT,0,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1615:           MPI_Isend(val,count,MPIU_SCALAR,  0,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1616:           irn    += count;
    1617:           jcn    += count;
    1618:           val    += count;
    1619:           count   = PetscMin(remain,PETSC_MPI_INT_MAX);
    1620:           remain -= count;
    1621:         }
    1622:       }
    1623:     } else {
    1624:       nreqs = 0;
    1625:       if (mumps->is_omp_master) {
    1626:         val = mumps->val + mumps->recvcount[0];
    1627:         for (i=1; i<osize; i++) { /* Remote communication only since self data is already in place */
    1628:           count  = PetscMin(mumps->recvcount[i],PETSC_MPI_INT_MAX);
    1629:           remain = mumps->recvcount[i] - count;
    1630:           while (count>0) {
    1631:             MPI_Irecv(val,count,MPIU_SCALAR,i,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1632:             val    += count;
    1633:             count   = PetscMin(remain,PETSC_MPI_INT_MAX);
    1634:             remain -= count;
    1635:           }
    1636:         }
    1637:       } else {
    1638:         val    = mumps->val;
    1639:         count  = PetscMin(mumps->nnz,PETSC_MPI_INT_MAX);
    1640:         remain = mumps->nnz - count;
    1641:         while (count>0) {
    1642:           MPI_Isend(val,count,MPIU_SCALAR,0,mumps->tag,mumps->omp_comm,&mumps->reqs[nreqs++]);
    1643:           val    += count;
    1644:           count   = PetscMin(remain,PETSC_MPI_INT_MAX);
    1645:           remain -= count;
    1646:         }
    1647:       }
    1648:     }
    1649:     MPI_Waitall(nreqs,mumps->reqs,MPI_STATUSES_IGNORE);
    1650:     mumps->tag++; /* It is totally fine for above send/recvs to share one mpi tag */
    1651:   }
    1652:   return(0);
    1653: }

    1655: PetscErrorCode MatFactorNumeric_MUMPS(Mat F,Mat A,const MatFactorInfo *info)
    1656: {
    1657:   Mat_MUMPS      *mumps =(Mat_MUMPS*)(F)->data;
    1659:   PetscBool      isMPIAIJ;

    1662:   if (mumps->id.INFOG(1) < 0 && !(mumps->id.INFOG(1) == -16 && mumps->id.INFOG(1) == 0)) {
    1663:     if (mumps->id.INFOG(1) == -6) {
    1664:       PetscInfo2(A,"MatFactorNumeric is called with singular matrix structure, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1665:     }
    1666:     PetscInfo2(A,"MatFactorNumeric is called after analysis phase fails, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1667:     return(0);
    1668:   }

    1670:   (*mumps->ConvertToTriples)(A, 1, MAT_REUSE_MATRIX, mumps);
    1671:   MatMumpsGatherNonzerosOnMaster(MAT_REUSE_MATRIX,mumps);

    1673:   /* numerical factorization phase */
    1674:   /*-------------------------------*/
    1675:   mumps->id.job = JOB_FACTNUMERIC;
    1676:   if (!mumps->id.ICNTL(18)) { /* A is centralized */
    1677:     if (!mumps->myid) {
    1678:       mumps->id.a = (MumpsScalar*)mumps->val;
    1679:     }
    1680:   } else {
    1681:     mumps->id.a_loc = (MumpsScalar*)mumps->val;
    1682:   }
    1683:   PetscMUMPS_c(mumps);
    1684:   if (mumps->id.INFOG(1) < 0) {
    1685:     if (A->erroriffailure) {
    1686:       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));
    1687:     } else {
    1688:       if (mumps->id.INFOG(1) == -10) { /* numerically singular matrix */
    1689:         PetscInfo2(F,"matrix is numerically singular, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1690:         F->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    1691:       } else if (mumps->id.INFOG(1) == -13) {
    1692:         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));
    1693:         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
    1694:       } else if (mumps->id.INFOG(1) == -8 || mumps->id.INFOG(1) == -9 || (-16 < mumps->id.INFOG(1) && mumps->id.INFOG(1) < -10)) {
    1695:         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));
    1696:         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
    1697:       } else {
    1698:         PetscInfo2(F,"MUMPS in numerical factorization phase: INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1699:         F->factorerrortype = MAT_FACTOR_OTHER;
    1700:       }
    1701:     }
    1702:   }
    1703:   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));

    1705:   F->assembled    = PETSC_TRUE;
    1706:   mumps->matstruc = SAME_NONZERO_PATTERN;
    1707:   if (F->schur) { /* reset Schur status to unfactored */
    1708: #if defined(PETSC_HAVE_CUDA)
    1709:     F->schur->offloadmask = PETSC_OFFLOAD_CPU;
    1710: #endif
    1711:     if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
    1712:       mumps->id.ICNTL(19) = 2;
    1713:       MatTranspose(F->schur,MAT_INPLACE_MATRIX,&F->schur);
    1714:     }
    1715:     MatFactorRestoreSchurComplement(F,NULL,MAT_FACTOR_SCHUR_UNFACTORED);
    1716:   }

    1718:   /* just to be sure that ICNTL(19) value returned by a call from MatMumpsGetIcntl is always consistent */
    1719:   if (!mumps->sym && mumps->id.ICNTL(19) && mumps->id.ICNTL(19) != 1) mumps->id.ICNTL(19) = 3;

    1721:   if (!mumps->is_omp_master) mumps->id.INFO(23) = 0;
    1722:   if (mumps->petsc_size > 1) {
    1723:     PetscInt    lsol_loc;
    1724:     PetscScalar *sol_loc;

    1726:     PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);

    1728:     /* distributed solution; Create x_seq=sol_loc for repeated use */
    1729:     if (mumps->x_seq) {
    1730:       VecScatterDestroy(&mumps->scat_sol);
    1731:       PetscFree2(mumps->id.sol_loc,mumps->id.isol_loc);
    1732:       VecDestroy(&mumps->x_seq);
    1733:     }
    1734:     lsol_loc = mumps->id.INFO(23); /* length of sol_loc */
    1735:     PetscMalloc2(lsol_loc,&sol_loc,lsol_loc,&mumps->id.isol_loc);
    1736:     mumps->id.lsol_loc = lsol_loc;
    1737:     mumps->id.sol_loc = (MumpsScalar*)sol_loc;
    1738:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,lsol_loc,sol_loc,&mumps->x_seq);
    1739:   }
    1740:   PetscLogFlops(mumps->id.RINFO(2));
    1741:   return(0);
    1742: }

    1744: /* Sets MUMPS options from the options database */
    1745: PetscErrorCode PetscSetMUMPSFromOptions(Mat F, Mat A)
    1746: {
    1747:   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
    1749:   PetscMUMPSInt  icntl=0;
    1750:   PetscInt       info[80],i,ninfo=80;
    1751:   PetscBool      flg=PETSC_FALSE;

    1754:   PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MUMPS Options","Mat");
    1755:   PetscOptionsMUMPSInt("-mat_mumps_icntl_1","ICNTL(1): output stream for error messages","None",mumps->id.ICNTL(1),&icntl,&flg);
    1756:   if (flg) mumps->id.ICNTL(1) = icntl;
    1757:   PetscOptionsMUMPSInt("-mat_mumps_icntl_2","ICNTL(2): output stream for diagnostic printing, statistics, and warning","None",mumps->id.ICNTL(2),&icntl,&flg);
    1758:   if (flg) mumps->id.ICNTL(2) = icntl;
    1759:   PetscOptionsMUMPSInt("-mat_mumps_icntl_3","ICNTL(3): output stream for global information, collected on the host","None",mumps->id.ICNTL(3),&icntl,&flg);
    1760:   if (flg) mumps->id.ICNTL(3) = icntl;

    1762:   PetscOptionsMUMPSInt("-mat_mumps_icntl_4","ICNTL(4): level of printing (0 to 4)","None",mumps->id.ICNTL(4),&icntl,&flg);
    1763:   if (flg) mumps->id.ICNTL(4) = icntl;
    1764:   if (mumps->id.ICNTL(4) || PetscLogPrintInfo) mumps->id.ICNTL(3) = 6; /* resume MUMPS default id.ICNTL(3) = 6 */

    1766:   PetscOptionsMUMPSInt("-mat_mumps_icntl_6","ICNTL(6): permutes to a zero-free diagonal and/or scale the matrix (0 to 7)","None",mumps->id.ICNTL(6),&icntl,&flg);
    1767:   if (flg) mumps->id.ICNTL(6) = icntl;

    1769:   PetscOptionsMUMPSInt("-mat_mumps_icntl_7","ICNTL(7): computes a symmetric permutation in sequential analysis. 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto(default)","None",mumps->id.ICNTL(7),&icntl,&flg);
    1770:   if (flg) {
    1771:     if (icntl == 1 || icntl < 0 || icntl > 7) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Valid values are 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto\n");
    1772:     mumps->id.ICNTL(7) = icntl;
    1773:   }

    1775:   PetscOptionsMUMPSInt("-mat_mumps_icntl_8","ICNTL(8): scaling strategy (-2 to 8 or 77)","None",mumps->id.ICNTL(8),&mumps->id.ICNTL(8),NULL);
    1776:   /* PetscOptionsInt("-mat_mumps_icntl_9","ICNTL(9): computes the solution using A or A^T","None",mumps->id.ICNTL(9),&mumps->id.ICNTL(9),NULL); handled by MatSolveTranspose_MUMPS() */
    1777:   PetscOptionsMUMPSInt("-mat_mumps_icntl_10","ICNTL(10): max num of refinements","None",mumps->id.ICNTL(10),&mumps->id.ICNTL(10),NULL);
    1778:   PetscOptionsMUMPSInt("-mat_mumps_icntl_11","ICNTL(11): statistics related to an error analysis (via -ksp_view)","None",mumps->id.ICNTL(11),&mumps->id.ICNTL(11),NULL);
    1779:   PetscOptionsMUMPSInt("-mat_mumps_icntl_12","ICNTL(12): an ordering strategy for symmetric matrices (0 to 3)","None",mumps->id.ICNTL(12),&mumps->id.ICNTL(12),NULL);
    1780:   PetscOptionsMUMPSInt("-mat_mumps_icntl_13","ICNTL(13): parallelism of the root node (enable ScaLAPACK) and its splitting","None",mumps->id.ICNTL(13),&mumps->id.ICNTL(13),NULL);
    1781:   PetscOptionsMUMPSInt("-mat_mumps_icntl_14","ICNTL(14): percentage increase in the estimated working space","None",mumps->id.ICNTL(14),&mumps->id.ICNTL(14),NULL);
    1782:   PetscOptionsMUMPSInt("-mat_mumps_icntl_19","ICNTL(19): computes the Schur complement","None",mumps->id.ICNTL(19),&mumps->id.ICNTL(19),NULL);
    1783:   if (mumps->id.ICNTL(19) <= 0 || mumps->id.ICNTL(19) > 3) { /* reset any schur data (if any) */
    1784:     MatDestroy(&F->schur);
    1785:     MatMumpsResetSchur_Private(mumps);
    1786:   }

    1788:   /* Two MPICH Fortran MPI_IN_PLACE binding bugs prevented the use of 'mpich + mumps'. One happened with "mpi4py + mpich + mumps",
    1789:      and was reported by Firedrake. See https://bitbucket.org/mpi4py/mpi4py/issues/162/mpi4py-initialization-breaks-fortran
    1790:      and a petsc-maint mailing list thread with subject 'MUMPS segfaults in parallel because of ...'
    1791:      This bug was fixed by https://github.com/pmodels/mpich/pull/4149. But the fix brought a new bug,
    1792:      see https://github.com/pmodels/mpich/issues/5589. This bug was fixed by https://github.com/pmodels/mpich/pull/5590.
    1793:      In short, we could not use distributed RHS with MPICH until v4.0b1.
    1794:    */
    1795: #if PETSC_PKG_MUMPS_VERSION_LT(5,3,0) || (defined(PETSC_HAVE_MPICH_NUMVERSION) && (PETSC_HAVE_MPICH_NUMVERSION < 40000101))
    1796:   mumps->ICNTL20 = 0;  /* Centralized dense RHS*/
    1797: #else
    1798:   mumps->ICNTL20 = 10; /* Distributed dense RHS*/
    1799: #endif
    1800:   PetscOptionsMUMPSInt("-mat_mumps_icntl_20","ICNTL(20): give mumps centralized (0) or distributed (10) dense right-hand sides","None",mumps->ICNTL20,&mumps->ICNTL20,&flg);
    1801:   if (flg && mumps->ICNTL20 != 10 && mumps->ICNTL20 != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"ICNTL(20)=%d is not supported by the PETSc/MUMPS interface. Allowed values are 0, 10\n",(int)mumps->ICNTL20);
    1802: #if PETSC_PKG_MUMPS_VERSION_LT(5,3,0)
    1803:   if (flg && mumps->ICNTL20 == 10) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"ICNTL(20)=10 is not supported before MUMPS-5.3.0\n");
    1804: #endif
    1805:   /* PetscOptionsMUMPSInt("-mat_mumps_icntl_21","ICNTL(21): the distribution (centralized or distributed) of the solution vectors","None",mumps->id.ICNTL(21),&mumps->id.ICNTL(21),NULL); we only use distributed solution vector */

    1807:   PetscOptionsMUMPSInt("-mat_mumps_icntl_22","ICNTL(22): in-core/out-of-core factorization and solve (0 or 1)","None",mumps->id.ICNTL(22),&mumps->id.ICNTL(22),NULL);
    1808:   PetscOptionsMUMPSInt("-mat_mumps_icntl_23","ICNTL(23): max size of the working memory (MB) that can allocate per processor","None",mumps->id.ICNTL(23),&mumps->id.ICNTL(23),NULL);
    1809:   PetscOptionsMUMPSInt("-mat_mumps_icntl_24","ICNTL(24): detection of null pivot rows (0 or 1)","None",mumps->id.ICNTL(24),&mumps->id.ICNTL(24),NULL);
    1810:   if (mumps->id.ICNTL(24)) {
    1811:     mumps->id.ICNTL(13) = 1; /* turn-off ScaLAPACK to help with the correct detection of null pivots */
    1812:   }

    1814:   PetscOptionsMUMPSInt("-mat_mumps_icntl_25","ICNTL(25): computes a solution of a deficient matrix and a null space basis","None",mumps->id.ICNTL(25),&mumps->id.ICNTL(25),NULL);
    1815:   PetscOptionsMUMPSInt("-mat_mumps_icntl_26","ICNTL(26): drives the solution phase if a Schur complement matrix","None",mumps->id.ICNTL(26),&mumps->id.ICNTL(26),NULL);
    1816:   PetscOptionsMUMPSInt("-mat_mumps_icntl_27","ICNTL(27): controls the blocking size for multiple right-hand sides","None",mumps->id.ICNTL(27),&mumps->id.ICNTL(27),NULL);
    1817:   PetscOptionsMUMPSInt("-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);
    1818:   PetscOptionsMUMPSInt("-mat_mumps_icntl_29","ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis","None",mumps->id.ICNTL(29),&mumps->id.ICNTL(29),NULL);
    1819:   /* PetscOptionsMUMPSInt("-mat_mumps_icntl_30","ICNTL(30): compute user-specified set of entries in inv(A)","None",mumps->id.ICNTL(30),&mumps->id.ICNTL(30),NULL); */ /* call MatMumpsGetInverse() directly */
    1820:   PetscOptionsMUMPSInt("-mat_mumps_icntl_31","ICNTL(31): indicates which factors may be discarded during factorization","None",mumps->id.ICNTL(31),&mumps->id.ICNTL(31),NULL);
    1821:   /* PetscOptionsMUMPSInt("-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);  -- not supported by PETSc API */
    1822:   PetscOptionsMUMPSInt("-mat_mumps_icntl_33","ICNTL(33): compute determinant","None",mumps->id.ICNTL(33),&mumps->id.ICNTL(33),NULL);
    1823:   PetscOptionsMUMPSInt("-mat_mumps_icntl_35","ICNTL(35): activates Block Low Rank (BLR) based factorization","None",mumps->id.ICNTL(35),&mumps->id.ICNTL(35),NULL);
    1824:   PetscOptionsMUMPSInt("-mat_mumps_icntl_36","ICNTL(36): choice of BLR factorization variant","None",mumps->id.ICNTL(36),&mumps->id.ICNTL(36),NULL);
    1825:   PetscOptionsMUMPSInt("-mat_mumps_icntl_38","ICNTL(38): estimated compression rate of LU factors with BLR","None",mumps->id.ICNTL(38),&mumps->id.ICNTL(38),NULL);

    1827:   PetscOptionsReal("-mat_mumps_cntl_1","CNTL(1): relative pivoting threshold","None",mumps->id.CNTL(1),&mumps->id.CNTL(1),NULL);
    1828:   PetscOptionsReal("-mat_mumps_cntl_2","CNTL(2): stopping criterion of refinement","None",mumps->id.CNTL(2),&mumps->id.CNTL(2),NULL);
    1829:   PetscOptionsReal("-mat_mumps_cntl_3","CNTL(3): absolute pivoting threshold","None",mumps->id.CNTL(3),&mumps->id.CNTL(3),NULL);
    1830:   PetscOptionsReal("-mat_mumps_cntl_4","CNTL(4): value for static pivoting","None",mumps->id.CNTL(4),&mumps->id.CNTL(4),NULL);
    1831:   PetscOptionsReal("-mat_mumps_cntl_5","CNTL(5): fixation for null pivots","None",mumps->id.CNTL(5),&mumps->id.CNTL(5),NULL);
    1832:   PetscOptionsReal("-mat_mumps_cntl_7","CNTL(7): dropping parameter used during BLR","None",mumps->id.CNTL(7),&mumps->id.CNTL(7),NULL);

    1834:   PetscOptionsString("-mat_mumps_ooc_tmpdir", "out of core directory", "None", mumps->id.ooc_tmpdir, mumps->id.ooc_tmpdir, sizeof(mumps->id.ooc_tmpdir), NULL);

    1836:   PetscOptionsIntArray("-mat_mumps_view_info","request INFO local to each processor","",info,&ninfo,NULL);
    1837:   if (ninfo) {
    1838:     if (ninfo > 80) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"number of INFO %d must <= 80\n",ninfo);
    1839:     PetscMalloc1(ninfo,&mumps->info);
    1840:     mumps->ninfo = ninfo;
    1841:     for (i=0; i<ninfo; i++) {
    1842:       if (info[i] < 0 || info[i]>80) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"index of INFO %d must between 1 and 80\n",ninfo);
    1843:       else  mumps->info[i] = info[i];
    1844:     }
    1845:   }

    1847:   PetscOptionsEnd();
    1848:   return(0);
    1849: }

    1851: PetscErrorCode PetscInitializeMUMPS(Mat A,Mat_MUMPS *mumps)
    1852: {
    1854:   PetscInt       nthreads=0;
    1855:   MPI_Comm       newcomm=MPI_COMM_NULL;

    1858:   mumps->petsc_comm = PetscObjectComm((PetscObject)A);
    1859:   MPI_Comm_size(mumps->petsc_comm,&mumps->petsc_size);
    1860:   MPI_Comm_rank(mumps->petsc_comm,&mumps->myid);/* "if (!myid)" still works even if mumps_comm is different */

    1862:   PetscOptionsHasName(NULL,((PetscObject)A)->prefix,"-mat_mumps_use_omp_threads",&mumps->use_petsc_omp_support);
    1863:   if (mumps->use_petsc_omp_support) nthreads = -1; /* -1 will let PetscOmpCtrlCreate() guess a proper value when user did not supply one */
    1864:   PetscOptionsGetInt(NULL,((PetscObject)A)->prefix,"-mat_mumps_use_omp_threads",&nthreads,NULL);
    1865:   if (mumps->use_petsc_omp_support) {
    1866: #if defined(PETSC_HAVE_OPENMP_SUPPORT)
    1867:     PetscOmpCtrlCreate(mumps->petsc_comm,nthreads,&mumps->omp_ctrl);
    1868:     PetscOmpCtrlGetOmpComms(mumps->omp_ctrl,&mumps->omp_comm,&mumps->mumps_comm,&mumps->is_omp_master);
    1869: #else
    1870:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"the system does not have PETSc OpenMP support but you added the -%smat_mumps_use_omp_threads option. Configure PETSc with --with-openmp --download-hwloc (or --with-hwloc) to enable it, see more in MATSOLVERMUMPS manual\n",((PetscObject)A)->prefix?((PetscObject)A)->prefix:"");
    1871: #endif
    1872:   } else {
    1873:     mumps->omp_comm      = PETSC_COMM_SELF;
    1874:     mumps->mumps_comm    = mumps->petsc_comm;
    1875:     mumps->is_omp_master = PETSC_TRUE;
    1876:   }
    1877:   MPI_Comm_size(mumps->omp_comm,&mumps->omp_comm_size);
    1878:   mumps->reqs = NULL;
    1879:   mumps->tag  = 0;

    1881:   /* It looks like MUMPS does not dup the input comm. Dup a new comm for MUMPS to avoid any tag mismatches. */
    1882:   if (mumps->mumps_comm != MPI_COMM_NULL) {
    1883:     MPI_Comm_dup(mumps->mumps_comm,&newcomm);
    1884:     mumps->mumps_comm = newcomm;
    1885:   }

    1887:   mumps->id.comm_fortran = MPI_Comm_c2f(mumps->mumps_comm);
    1888:   mumps->id.job = JOB_INIT;
    1889:   mumps->id.par = 1;  /* host participates factorizaton and solve */
    1890:   mumps->id.sym = mumps->sym;

    1892:   PetscMUMPS_c(mumps);
    1893:   if (mumps->id.INFOG(1) < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in PetscInitializeMUMPS: INFOG(1)=%d\n",mumps->id.INFOG(1));

    1895:   /* copy MUMPS default control values from master to slaves. Although slaves do not call MUMPS, they may access these values in code.
    1896:      For example, ICNTL(9) is initialized to 1 by MUMPS and slaves check ICNTL(9) in MatSolve_MUMPS.
    1897:    */
    1898:   MPI_Bcast(mumps->id.icntl,40,MPI_INT,  0,mumps->omp_comm);
    1899:   MPI_Bcast(mumps->id.cntl, 15,MPIU_REAL,0,mumps->omp_comm);

    1901:   mumps->scat_rhs = NULL;
    1902:   mumps->scat_sol = NULL;

    1904:   /* set PETSc-MUMPS default options - override MUMPS default */
    1905:   mumps->id.ICNTL(3) = 0;
    1906:   mumps->id.ICNTL(4) = 0;
    1907:   if (mumps->petsc_size == 1) {
    1908:     mumps->id.ICNTL(18) = 0;   /* centralized assembled matrix input */
    1909:     mumps->id.ICNTL(7)  = 7;   /* automatic choice of ordering done by the package */
    1910:   } else {
    1911:     mumps->id.ICNTL(18) = 3;   /* distributed assembled matrix input */
    1912:     mumps->id.ICNTL(21) = 1;   /* distributed solution */
    1913:   }

    1915:   /* schur */
    1916:   mumps->id.size_schur    = 0;
    1917:   mumps->id.listvar_schur = NULL;
    1918:   mumps->id.schur         = NULL;
    1919:   mumps->sizeredrhs       = 0;
    1920:   mumps->schur_sol        = NULL;
    1921:   mumps->schur_sizesol    = 0;
    1922:   return(0);
    1923: }

    1925: PetscErrorCode MatFactorSymbolic_MUMPS_ReportIfError(Mat F,Mat A,const MatFactorInfo *info,Mat_MUMPS *mumps)
    1926: {

    1930:   if (mumps->id.INFOG(1) < 0) {
    1931:     if (A->erroriffailure) {
    1932:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MUMPS in analysis phase: INFOG(1)=%d\n",mumps->id.INFOG(1));
    1933:     } else {
    1934:       if (mumps->id.INFOG(1) == -6) {
    1935:         PetscInfo2(F,"matrix is singular in structure, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1936:         F->factorerrortype = MAT_FACTOR_STRUCT_ZEROPIVOT;
    1937:       } else if (mumps->id.INFOG(1) == -5 || mumps->id.INFOG(1) == -7) {
    1938:         PetscInfo2(F,"problem of workspace, INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1939:         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
    1940:       } else if (mumps->id.INFOG(1) == -16 && mumps->id.INFOG(1) == 0) {
    1941:         PetscInfo(F,"Empty matrix\n");
    1942:       } else {
    1943:         PetscInfo2(F,"Error reported by MUMPS in analysis phase: INFOG(1)=%d, INFO(2)=%d\n",mumps->id.INFOG(1),mumps->id.INFO(2));
    1944:         F->factorerrortype = MAT_FACTOR_OTHER;
    1945:       }
    1946:     }
    1947:   }
    1948:   return(0);
    1949: }

    1951: PetscErrorCode MatLUFactorSymbolic_AIJMUMPS(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
    1952: {
    1953:   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
    1955:   Vec            b;
    1956:   const PetscInt M = A->rmap->N;

    1959:   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;

    1961:   /* Set MUMPS options from the options database */
    1962:   PetscSetMUMPSFromOptions(F,A);

    1964:   (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps);
    1965:   MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX,mumps);

    1967:   /* analysis phase */
    1968:   /*----------------*/
    1969:   mumps->id.job = JOB_FACTSYMBOLIC;
    1970:   mumps->id.n   = M;
    1971:   switch (mumps->id.ICNTL(18)) {
    1972:   case 0:  /* centralized assembled matrix input */
    1973:     if (!mumps->myid) {
    1974:       mumps->id.nnz = mumps->nnz;
    1975:       mumps->id.irn = mumps->irn;
    1976:       mumps->id.jcn = mumps->jcn;
    1977:       if (mumps->id.ICNTL(6)>1) mumps->id.a = (MumpsScalar*)mumps->val;
    1978:       if (r) {
    1979:         mumps->id.ICNTL(7) = 1;
    1980:         if (!mumps->myid) {
    1981:           const PetscInt *idx;
    1982:           PetscInt       i;

    1984:           PetscMalloc1(M,&mumps->id.perm_in);
    1985:           ISGetIndices(r,&idx);
    1986:           for (i=0; i<M; i++) {PetscMUMPSIntCast(idx[i]+1,&(mumps->id.perm_in[i]));} /* perm_in[]: start from 1, not 0! */
    1987:           ISRestoreIndices(r,&idx);
    1988:         }
    1989:       }
    1990:     }
    1991:     break;
    1992:   case 3:  /* distributed assembled matrix input (size>1) */
    1993:     mumps->id.nnz_loc = mumps->nnz;
    1994:     mumps->id.irn_loc = mumps->irn;
    1995:     mumps->id.jcn_loc = mumps->jcn;
    1996:     if (mumps->id.ICNTL(6)>1) mumps->id.a_loc = (MumpsScalar*)mumps->val;
    1997:     if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
    1998:       MatCreateVecs(A,NULL,&b);
    1999:       VecScatterCreateToZero(b,&mumps->scat_rhs,&mumps->b_seq);
    2000:       VecDestroy(&b);
    2001:     }
    2002:     break;
    2003:   }
    2004:   PetscMUMPS_c(mumps);
    2005:   MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);

    2007:   F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
    2008:   F->ops->solve           = MatSolve_MUMPS;
    2009:   F->ops->solvetranspose  = MatSolveTranspose_MUMPS;
    2010:   F->ops->matsolve        = MatMatSolve_MUMPS;
    2011:   F->ops->mattransposesolve = MatMatTransposeSolve_MUMPS;
    2012:   return(0);
    2013: }

    2015: /* Note the Petsc r and c permutations are ignored */
    2016: PetscErrorCode MatLUFactorSymbolic_BAIJMUMPS(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
    2017: {
    2018:   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
    2020:   Vec            b;
    2021:   const PetscInt M = A->rmap->N;

    2024:   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;

    2026:   /* Set MUMPS options from the options database */
    2027:   PetscSetMUMPSFromOptions(F,A);

    2029:   (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps);
    2030:   MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX,mumps);

    2032:   /* analysis phase */
    2033:   /*----------------*/
    2034:   mumps->id.job = JOB_FACTSYMBOLIC;
    2035:   mumps->id.n   = M;
    2036:   switch (mumps->id.ICNTL(18)) {
    2037:   case 0:  /* centralized assembled matrix input */
    2038:     if (!mumps->myid) {
    2039:       mumps->id.nnz = mumps->nnz;
    2040:       mumps->id.irn = mumps->irn;
    2041:       mumps->id.jcn = mumps->jcn;
    2042:       if (mumps->id.ICNTL(6)>1) {
    2043:         mumps->id.a = (MumpsScalar*)mumps->val;
    2044:       }
    2045:     }
    2046:     break;
    2047:   case 3:  /* distributed assembled matrix input (size>1) */
    2048:     mumps->id.nnz_loc = mumps->nnz;
    2049:     mumps->id.irn_loc = mumps->irn;
    2050:     mumps->id.jcn_loc = mumps->jcn;
    2051:     if (mumps->id.ICNTL(6)>1) {
    2052:       mumps->id.a_loc = (MumpsScalar*)mumps->val;
    2053:     }
    2054:     if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
    2055:       MatCreateVecs(A,NULL,&b);
    2056:       VecScatterCreateToZero(b,&mumps->scat_rhs,&mumps->b_seq);
    2057:       VecDestroy(&b);
    2058:     }
    2059:     break;
    2060:   }
    2061:   PetscMUMPS_c(mumps);
    2062:   MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);

    2064:   F->ops->lufactornumeric = MatFactorNumeric_MUMPS;
    2065:   F->ops->solve           = MatSolve_MUMPS;
    2066:   F->ops->solvetranspose  = MatSolveTranspose_MUMPS;
    2067:   return(0);
    2068: }

    2070: /* Note the Petsc r permutation and factor info are ignored */
    2071: PetscErrorCode MatCholeskyFactorSymbolic_MUMPS(Mat F,Mat A,IS r,const MatFactorInfo *info)
    2072: {
    2073:   Mat_MUMPS      *mumps = (Mat_MUMPS*)F->data;
    2075:   Vec            b;
    2076:   const PetscInt M = A->rmap->N;

    2079:   mumps->matstruc = DIFFERENT_NONZERO_PATTERN;

    2081:   /* Set MUMPS options from the options database */
    2082:   PetscSetMUMPSFromOptions(F,A);

    2084:   (*mumps->ConvertToTriples)(A, 1, MAT_INITIAL_MATRIX, mumps);
    2085:   MatMumpsGatherNonzerosOnMaster(MAT_INITIAL_MATRIX,mumps);

    2087:   /* analysis phase */
    2088:   /*----------------*/
    2089:   mumps->id.job = JOB_FACTSYMBOLIC;
    2090:   mumps->id.n   = M;
    2091:   switch (mumps->id.ICNTL(18)) {
    2092:   case 0:  /* centralized assembled matrix input */
    2093:     if (!mumps->myid) {
    2094:       mumps->id.nnz = mumps->nnz;
    2095:       mumps->id.irn = mumps->irn;
    2096:       mumps->id.jcn = mumps->jcn;
    2097:       if (mumps->id.ICNTL(6)>1) {
    2098:         mumps->id.a = (MumpsScalar*)mumps->val;
    2099:       }
    2100:     }
    2101:     break;
    2102:   case 3:  /* distributed assembled matrix input (size>1) */
    2103:     mumps->id.nnz_loc = mumps->nnz;
    2104:     mumps->id.irn_loc = mumps->irn;
    2105:     mumps->id.jcn_loc = mumps->jcn;
    2106:     if (mumps->id.ICNTL(6)>1) {
    2107:       mumps->id.a_loc = (MumpsScalar*)mumps->val;
    2108:     }
    2109:     if (mumps->ICNTL20 == 0) { /* Centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
    2110:       MatCreateVecs(A,NULL,&b);
    2111:       VecScatterCreateToZero(b,&mumps->scat_rhs,&mumps->b_seq);
    2112:       VecDestroy(&b);
    2113:     }
    2114:     break;
    2115:   }
    2116:   PetscMUMPS_c(mumps);
    2117:   MatFactorSymbolic_MUMPS_ReportIfError(F,A,info,mumps);

    2119:   F->ops->choleskyfactornumeric = MatFactorNumeric_MUMPS;
    2120:   F->ops->solve                 = MatSolve_MUMPS;
    2121:   F->ops->solvetranspose        = MatSolve_MUMPS;
    2122:   F->ops->matsolve              = MatMatSolve_MUMPS;
    2123:   F->ops->mattransposesolve     = MatMatTransposeSolve_MUMPS;
    2124: #if defined(PETSC_USE_COMPLEX)
    2125:   F->ops->getinertia = NULL;
    2126: #else
    2127:   F->ops->getinertia = MatGetInertia_SBAIJMUMPS;
    2128: #endif
    2129:   return(0);
    2130: }

    2132: PetscErrorCode MatView_MUMPS(Mat A,PetscViewer viewer)
    2133: {
    2134:   PetscErrorCode    ierr;
    2135:   PetscBool         iascii;
    2136:   PetscViewerFormat format;
    2137:   Mat_MUMPS         *mumps=(Mat_MUMPS*)A->data;

    2140:   /* check if matrix is mumps type */
    2141:   if (A->ops->solve != MatSolve_MUMPS) return(0);

    2143:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    2144:   if (iascii) {
    2145:     PetscViewerGetFormat(viewer,&format);
    2146:     if (format == PETSC_VIEWER_ASCII_INFO) {
    2147:       PetscViewerASCIIPrintf(viewer,"MUMPS run parameters:\n");
    2148:       PetscViewerASCIIPrintf(viewer,"  SYM (matrix type):                   %d\n",mumps->id.sym);
    2149:       PetscViewerASCIIPrintf(viewer,"  PAR (host participation):            %d\n",mumps->id.par);
    2150:       PetscViewerASCIIPrintf(viewer,"  ICNTL(1) (output for error):         %d\n",mumps->id.ICNTL(1));
    2151:       PetscViewerASCIIPrintf(viewer,"  ICNTL(2) (output of diagnostic msg): %d\n",mumps->id.ICNTL(2));
    2152:       PetscViewerASCIIPrintf(viewer,"  ICNTL(3) (output for global info):   %d\n",mumps->id.ICNTL(3));
    2153:       PetscViewerASCIIPrintf(viewer,"  ICNTL(4) (level of printing):        %d\n",mumps->id.ICNTL(4));
    2154:       PetscViewerASCIIPrintf(viewer,"  ICNTL(5) (input mat struct):         %d\n",mumps->id.ICNTL(5));
    2155:       PetscViewerASCIIPrintf(viewer,"  ICNTL(6) (matrix prescaling):        %d\n",mumps->id.ICNTL(6));
    2156:       PetscViewerASCIIPrintf(viewer,"  ICNTL(7) (sequential matrix ordering):%d\n",mumps->id.ICNTL(7));
    2157:       PetscViewerASCIIPrintf(viewer,"  ICNTL(8) (scaling strategy):        %d\n",mumps->id.ICNTL(8));
    2158:       PetscViewerASCIIPrintf(viewer,"  ICNTL(10) (max num of refinements):  %d\n",mumps->id.ICNTL(10));
    2159:       PetscViewerASCIIPrintf(viewer,"  ICNTL(11) (error analysis):          %d\n",mumps->id.ICNTL(11));
    2160:       if (mumps->id.ICNTL(11)>0) {
    2161:         PetscViewerASCIIPrintf(viewer,"    RINFOG(4) (inf norm of input mat):        %g\n",mumps->id.RINFOG(4));
    2162:         PetscViewerASCIIPrintf(viewer,"    RINFOG(5) (inf norm of solution):         %g\n",mumps->id.RINFOG(5));
    2163:         PetscViewerASCIIPrintf(viewer,"    RINFOG(6) (inf norm of residual):         %g\n",mumps->id.RINFOG(6));
    2164:         PetscViewerASCIIPrintf(viewer,"    RINFOG(7),RINFOG(8) (backward error est): %g, %g\n",mumps->id.RINFOG(7),mumps->id.RINFOG(8));
    2165:         PetscViewerASCIIPrintf(viewer,"    RINFOG(9) (error estimate):               %g \n",mumps->id.RINFOG(9));
    2166:         PetscViewerASCIIPrintf(viewer,"    RINFOG(10),RINFOG(11)(condition numbers): %g, %g\n",mumps->id.RINFOG(10),mumps->id.RINFOG(11));
    2167:       }
    2168:       PetscViewerASCIIPrintf(viewer,"  ICNTL(12) (efficiency control):                         %d\n",mumps->id.ICNTL(12));
    2169:       PetscViewerASCIIPrintf(viewer,"  ICNTL(13) (sequential factorization of the root node):  %d\n",mumps->id.ICNTL(13));
    2170:       PetscViewerASCIIPrintf(viewer,"  ICNTL(14) (percentage of estimated workspace increase): %d\n",mumps->id.ICNTL(14));
    2171:       /* ICNTL(15-17) not used */
    2172:       PetscViewerASCIIPrintf(viewer,"  ICNTL(18) (input mat struct):                           %d\n",mumps->id.ICNTL(18));
    2173:       PetscViewerASCIIPrintf(viewer,"  ICNTL(19) (Schur complement info):                      %d\n",mumps->id.ICNTL(19));
    2174:       PetscViewerASCIIPrintf(viewer,"  ICNTL(20) (RHS sparse pattern):                         %d\n",mumps->id.ICNTL(20));
    2175:       PetscViewerASCIIPrintf(viewer,"  ICNTL(21) (solution struct):                            %d\n",mumps->id.ICNTL(21));
    2176:       PetscViewerASCIIPrintf(viewer,"  ICNTL(22) (in-core/out-of-core facility):               %d\n",mumps->id.ICNTL(22));
    2177:       PetscViewerASCIIPrintf(viewer,"  ICNTL(23) (max size of memory can be allocated locally):%d\n",mumps->id.ICNTL(23));

    2179:       PetscViewerASCIIPrintf(viewer,"  ICNTL(24) (detection of null pivot rows):               %d\n",mumps->id.ICNTL(24));
    2180:       PetscViewerASCIIPrintf(viewer,"  ICNTL(25) (computation of a null space basis):          %d\n",mumps->id.ICNTL(25));
    2181:       PetscViewerASCIIPrintf(viewer,"  ICNTL(26) (Schur options for RHS or solution):          %d\n",mumps->id.ICNTL(26));
    2182:       PetscViewerASCIIPrintf(viewer,"  ICNTL(27) (blocking size for multiple RHS):             %d\n",mumps->id.ICNTL(27));
    2183:       PetscViewerASCIIPrintf(viewer,"  ICNTL(28) (use parallel or sequential ordering):        %d\n",mumps->id.ICNTL(28));
    2184:       PetscViewerASCIIPrintf(viewer,"  ICNTL(29) (parallel ordering):                          %d\n",mumps->id.ICNTL(29));

    2186:       PetscViewerASCIIPrintf(viewer,"  ICNTL(30) (user-specified set of entries in inv(A)):    %d\n",mumps->id.ICNTL(30));
    2187:       PetscViewerASCIIPrintf(viewer,"  ICNTL(31) (factors is discarded in the solve phase):    %d\n",mumps->id.ICNTL(31));
    2188:       PetscViewerASCIIPrintf(viewer,"  ICNTL(33) (compute determinant):                        %d\n",mumps->id.ICNTL(33));
    2189:       PetscViewerASCIIPrintf(viewer,"  ICNTL(35) (activate BLR based factorization):           %d\n",mumps->id.ICNTL(35));
    2190:       PetscViewerASCIIPrintf(viewer,"  ICNTL(36) (choice of BLR factorization variant):        %d\n",mumps->id.ICNTL(36));
    2191:       PetscViewerASCIIPrintf(viewer,"  ICNTL(38) (estimated compression rate of LU factors):   %d\n",mumps->id.ICNTL(38));

    2193:       PetscViewerASCIIPrintf(viewer,"  CNTL(1) (relative pivoting threshold):      %g \n",mumps->id.CNTL(1));
    2194:       PetscViewerASCIIPrintf(viewer,"  CNTL(2) (stopping criterion of refinement): %g \n",mumps->id.CNTL(2));
    2195:       PetscViewerASCIIPrintf(viewer,"  CNTL(3) (absolute pivoting threshold):      %g \n",mumps->id.CNTL(3));
    2196:       PetscViewerASCIIPrintf(viewer,"  CNTL(4) (value of static pivoting):         %g \n",mumps->id.CNTL(4));
    2197:       PetscViewerASCIIPrintf(viewer,"  CNTL(5) (fixation for null pivots):         %g \n",mumps->id.CNTL(5));
    2198:       PetscViewerASCIIPrintf(viewer,"  CNTL(7) (dropping parameter for BLR):       %g \n",mumps->id.CNTL(7));

    2200:       /* information local to each processor */
    2201:       PetscViewerASCIIPrintf(viewer, "  RINFO(1) (local estimated flops for the elimination after analysis): \n");
    2202:       PetscViewerASCIIPushSynchronized(viewer);
    2203:       PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %g \n",mumps->myid,mumps->id.RINFO(1));
    2204:       PetscViewerFlush(viewer);
    2205:       PetscViewerASCIIPrintf(viewer, "  RINFO(2) (local estimated flops for the assembly after factorization): \n");
    2206:       PetscViewerASCIISynchronizedPrintf(viewer,"    [%d]  %g \n",mumps->myid,mumps->id.RINFO(2));
    2207:       PetscViewerFlush(viewer);
    2208:       PetscViewerASCIIPrintf(viewer, "  RINFO(3) (local estimated flops for the elimination after factorization): \n");
    2209:       PetscViewerASCIISynchronizedPrintf(viewer,"    [%d]  %g \n",mumps->myid,mumps->id.RINFO(3));
    2210:       PetscViewerFlush(viewer);

    2212:       PetscViewerASCIIPrintf(viewer, "  INFO(15) (estimated size of (in MB) MUMPS internal data for running numerical factorization): \n");
    2213:       PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] %d\n",mumps->myid,mumps->id.INFO(15));
    2214:       PetscViewerFlush(viewer);

    2216:       PetscViewerASCIIPrintf(viewer, "  INFO(16) (size of (in MB) MUMPS internal data used during numerical factorization): \n");
    2217:       PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d\n",mumps->myid,mumps->id.INFO(16));
    2218:       PetscViewerFlush(viewer);

    2220:       PetscViewerASCIIPrintf(viewer, "  INFO(23) (num of pivots eliminated on this processor after factorization): \n");
    2221:       PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d\n",mumps->myid,mumps->id.INFO(23));
    2222:       PetscViewerFlush(viewer);

    2224:       if (mumps->ninfo && mumps->ninfo <= 80) {
    2225:         PetscInt i;
    2226:         for (i=0; i<mumps->ninfo; i++) {
    2227:           PetscViewerASCIIPrintf(viewer, "  INFO(%d): \n",mumps->info[i]);
    2228:           PetscViewerASCIISynchronizedPrintf(viewer,"    [%d] %d\n",mumps->myid,mumps->id.INFO(mumps->info[i]));
    2229:           PetscViewerFlush(viewer);
    2230:         }
    2231:       }
    2232:       PetscViewerASCIIPopSynchronized(viewer);

    2234:       if (!mumps->myid) { /* information from the host */
    2235:         PetscViewerASCIIPrintf(viewer,"  RINFOG(1) (global estimated flops for the elimination after analysis): %g \n",mumps->id.RINFOG(1));
    2236:         PetscViewerASCIIPrintf(viewer,"  RINFOG(2) (global estimated flops for the assembly after factorization): %g \n",mumps->id.RINFOG(2));
    2237:         PetscViewerASCIIPrintf(viewer,"  RINFOG(3) (global estimated flops for the elimination after factorization): %g \n",mumps->id.RINFOG(3));
    2238:         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));

    2240:         PetscViewerASCIIPrintf(viewer,"  INFOG(3) (estimated real workspace for factors on all processors after analysis): %d\n",mumps->id.INFOG(3));
    2241:         PetscViewerASCIIPrintf(viewer,"  INFOG(4) (estimated integer workspace for factors on all processors after analysis): %d\n",mumps->id.INFOG(4));
    2242:         PetscViewerASCIIPrintf(viewer,"  INFOG(5) (estimated maximum front size in the complete tree): %d\n",mumps->id.INFOG(5));
    2243:         PetscViewerASCIIPrintf(viewer,"  INFOG(6) (number of nodes in the complete tree): %d\n",mumps->id.INFOG(6));
    2244:         PetscViewerASCIIPrintf(viewer,"  INFOG(7) (ordering option effectively used after analysis): %d\n",mumps->id.INFOG(7));
    2245:         PetscViewerASCIIPrintf(viewer,"  INFOG(8) (structural symmetry in percent of the permuted matrix after analysis): %d\n",mumps->id.INFOG(8));
    2246:         PetscViewerASCIIPrintf(viewer,"  INFOG(9) (total real/complex workspace to store the matrix factors after factorization): %d\n",mumps->id.INFOG(9));
    2247:         PetscViewerASCIIPrintf(viewer,"  INFOG(10) (total integer space store the matrix factors after factorization): %d\n",mumps->id.INFOG(10));
    2248:         PetscViewerASCIIPrintf(viewer,"  INFOG(11) (order of largest frontal matrix after factorization): %d\n",mumps->id.INFOG(11));
    2249:         PetscViewerASCIIPrintf(viewer,"  INFOG(12) (number of off-diagonal pivots): %d\n",mumps->id.INFOG(12));
    2250:         PetscViewerASCIIPrintf(viewer,"  INFOG(13) (number of delayed pivots after factorization): %d\n",mumps->id.INFOG(13));
    2251:         PetscViewerASCIIPrintf(viewer,"  INFOG(14) (number of memory compress after factorization): %d\n",mumps->id.INFOG(14));
    2252:         PetscViewerASCIIPrintf(viewer,"  INFOG(15) (number of steps of iterative refinement after solution): %d\n",mumps->id.INFOG(15));
    2253:         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));
    2254:         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));
    2255:         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));
    2256:         PetscViewerASCIIPrintf(viewer,"  INFOG(19) (size of all MUMPS internal data allocated during factorization: sum over all processors): %d\n",mumps->id.INFOG(19));
    2257:         PetscViewerASCIIPrintf(viewer,"  INFOG(20) (estimated number of entries in the factors): %d\n",mumps->id.INFOG(20));
    2258:         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));
    2259:         PetscViewerASCIIPrintf(viewer,"  INFOG(22) (size in MB of memory effectively used during factorization - sum over all processors): %d\n",mumps->id.INFOG(22));
    2260:         PetscViewerASCIIPrintf(viewer,"  INFOG(23) (after analysis: value of ICNTL(6) effectively used): %d\n",mumps->id.INFOG(23));
    2261:         PetscViewerASCIIPrintf(viewer,"  INFOG(24) (after analysis: value of ICNTL(12) effectively used): %d\n",mumps->id.INFOG(24));
    2262:         PetscViewerASCIIPrintf(viewer,"  INFOG(25) (after factorization: number of pivots modified by static pivoting): %d\n",mumps->id.INFOG(25));
    2263:         PetscViewerASCIIPrintf(viewer,"  INFOG(28) (after factorization: number of null pivots encountered): %d\n",mumps->id.INFOG(28));
    2264:         PetscViewerASCIIPrintf(viewer,"  INFOG(29) (after factorization: effective number of entries in the factors (sum over all processors)): %d\n",mumps->id.INFOG(29));
    2265:         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));
    2266:         PetscViewerASCIIPrintf(viewer,"  INFOG(32) (after analysis: type of analysis done): %d\n",mumps->id.INFOG(32));
    2267:         PetscViewerASCIIPrintf(viewer,"  INFOG(33) (value used for ICNTL(8)): %d\n",mumps->id.INFOG(33));
    2268:         PetscViewerASCIIPrintf(viewer,"  INFOG(34) (exponent of the determinant if determinant is requested): %d\n",mumps->id.INFOG(34));
    2269:         PetscViewerASCIIPrintf(viewer,"  INFOG(35) (after factorization: number of entries taking into account BLR factor compression - sum over all processors): %d\n",mumps->id.INFOG(35));
    2270:         PetscViewerASCIIPrintf(viewer,"  INFOG(36) (after analysis: estimated size of all MUMPS internal data for running BLR in-core - value on the most memory consuming processor): %d\n",mumps->id.INFOG(36));
    2271:         PetscViewerASCIIPrintf(viewer,"  INFOG(37) (after analysis: estimated size of all MUMPS internal data for running BLR in-core - sum over all processors): %d\n",mumps->id.INFOG(37));
    2272:         PetscViewerASCIIPrintf(viewer,"  INFOG(38) (after analysis: estimated size of all MUMPS internal data for running BLR out-of-core - value on the most memory consuming processor): %d\n",mumps->id.INFOG(38));
    2273:         PetscViewerASCIIPrintf(viewer,"  INFOG(39) (after analysis: estimated size of all MUMPS internal data for running BLR out-of-core - sum over all processors): %d\n",mumps->id.INFOG(39));
    2274:       }
    2275:     }
    2276:   }
    2277:   return(0);
    2278: }

    2280: PetscErrorCode MatGetInfo_MUMPS(Mat A,MatInfoType flag,MatInfo *info)
    2281: {
    2282:   Mat_MUMPS *mumps =(Mat_MUMPS*)A->data;

    2285:   info->block_size        = 1.0;
    2286:   info->nz_allocated      = mumps->id.INFOG(20);
    2287:   info->nz_used           = mumps->id.INFOG(20);
    2288:   info->nz_unneeded       = 0.0;
    2289:   info->assemblies        = 0.0;
    2290:   info->mallocs           = 0.0;
    2291:   info->memory            = 0.0;
    2292:   info->fill_ratio_given  = 0;
    2293:   info->fill_ratio_needed = 0;
    2294:   info->factor_mallocs    = 0;
    2295:   return(0);
    2296: }

    2298: /* -------------------------------------------------------------------------------------------*/
    2299: PetscErrorCode MatFactorSetSchurIS_MUMPS(Mat F, IS is)
    2300: {
    2301:   Mat_MUMPS         *mumps =(Mat_MUMPS*)F->data;
    2302:   const PetscScalar *arr;
    2303:   const PetscInt    *idxs;
    2304:   PetscInt          size,i;
    2305:   PetscErrorCode    ierr;

    2308:   ISGetLocalSize(is,&size);
    2309:   if (mumps->petsc_size > 1) {
    2310:     PetscBool ls,gs; /* gs is false if any rank other than root has non-empty IS */

    2312:     ls   = mumps->myid ? (size ? PETSC_FALSE : PETSC_TRUE) : PETSC_TRUE; /* always true on root; false on others if their size != 0 */
    2313:     MPI_Allreduce(&ls,&gs,1,MPIU_BOOL,MPI_LAND,mumps->petsc_comm);
    2314:     if (!gs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MUMPS distributed parallel Schur complements not yet supported from PETSc\n");
    2315:   }

    2317:   /* Schur complement matrix */
    2318:   MatDestroy(&F->schur);
    2319:   MatCreateSeqDense(PETSC_COMM_SELF,size,size,NULL,&F->schur);
    2320:   MatDenseGetArrayRead(F->schur,&arr);
    2321:   mumps->id.schur      = (MumpsScalar*)arr;
    2322:   mumps->id.size_schur = size;
    2323:   mumps->id.schur_lld  = size;
    2324:   MatDenseRestoreArrayRead(F->schur,&arr);
    2325:   if (mumps->sym == 1) {
    2326:     MatSetOption(F->schur,MAT_SPD,PETSC_TRUE);
    2327:   }

    2329:   /* MUMPS expects Fortran style indices */
    2330:   PetscFree(mumps->id.listvar_schur);
    2331:   PetscMalloc1(size,&mumps->id.listvar_schur);
    2332:   ISGetIndices(is,&idxs);
    2333:   for (i=0; i<size; i++) {PetscMUMPSIntCast(idxs[i]+1,&(mumps->id.listvar_schur[i]));}
    2334:   ISRestoreIndices(is,&idxs);
    2335:   if (mumps->petsc_size > 1) {
    2336:     mumps->id.ICNTL(19) = 1; /* MUMPS returns Schur centralized on the host */
    2337:   } else {
    2338:     if (F->factortype == MAT_FACTOR_LU) {
    2339:       mumps->id.ICNTL(19) = 3; /* MUMPS returns full matrix */
    2340:     } else {
    2341:       mumps->id.ICNTL(19) = 2; /* MUMPS returns lower triangular part */
    2342:     }
    2343:   }
    2344:   /* set a special value of ICNTL (not handled my MUMPS) to be used in the solve phase by PETSc */
    2345:   mumps->id.ICNTL(26) = -1;
    2346:   return(0);
    2347: }

    2349: /* -------------------------------------------------------------------------------------------*/
    2350: PetscErrorCode MatFactorCreateSchurComplement_MUMPS(Mat F,Mat* S)
    2351: {
    2352:   Mat            St;
    2353:   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
    2354:   PetscScalar    *array;
    2355: #if defined(PETSC_USE_COMPLEX)
    2356:   PetscScalar    im = PetscSqrtScalar((PetscScalar)-1.0);
    2357: #endif

    2361:   if (!mumps->id.ICNTL(19)) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ORDER,"Schur complement mode not selected! You should call MatFactorSetSchurIS to enable it");
    2362:   MatCreate(PETSC_COMM_SELF,&St);
    2363:   MatSetSizes(St,PETSC_DECIDE,PETSC_DECIDE,mumps->id.size_schur,mumps->id.size_schur);
    2364:   MatSetType(St,MATDENSE);
    2365:   MatSetUp(St);
    2366:   MatDenseGetArray(St,&array);
    2367:   if (!mumps->sym) { /* MUMPS always return a full matrix */
    2368:     if (mumps->id.ICNTL(19) == 1) { /* stored by rows */
    2369:       PetscInt i,j,N=mumps->id.size_schur;
    2370:       for (i=0;i<N;i++) {
    2371:         for (j=0;j<N;j++) {
    2372: #if !defined(PETSC_USE_COMPLEX)
    2373:           PetscScalar val = mumps->id.schur[i*N+j];
    2374: #else
    2375:           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
    2376: #endif
    2377:           array[j*N+i] = val;
    2378:         }
    2379:       }
    2380:     } else { /* stored by columns */
    2381:       PetscArraycpy(array,mumps->id.schur,mumps->id.size_schur*mumps->id.size_schur);
    2382:     }
    2383:   } else { /* either full or lower-triangular (not packed) */
    2384:     if (mumps->id.ICNTL(19) == 2) { /* lower triangular stored by columns */
    2385:       PetscInt i,j,N=mumps->id.size_schur;
    2386:       for (i=0;i<N;i++) {
    2387:         for (j=i;j<N;j++) {
    2388: #if !defined(PETSC_USE_COMPLEX)
    2389:           PetscScalar val = mumps->id.schur[i*N+j];
    2390: #else
    2391:           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
    2392: #endif
    2393:           array[i*N+j] = val;
    2394:           array[j*N+i] = val;
    2395:         }
    2396:       }
    2397:     } else if (mumps->id.ICNTL(19) == 3) { /* full matrix */
    2398:       PetscArraycpy(array,mumps->id.schur,mumps->id.size_schur*mumps->id.size_schur);
    2399:     } else { /* ICNTL(19) == 1 lower triangular stored by rows */
    2400:       PetscInt i,j,N=mumps->id.size_schur;
    2401:       for (i=0;i<N;i++) {
    2402:         for (j=0;j<i+1;j++) {
    2403: #if !defined(PETSC_USE_COMPLEX)
    2404:           PetscScalar val = mumps->id.schur[i*N+j];
    2405: #else
    2406:           PetscScalar val = mumps->id.schur[i*N+j].r + im*mumps->id.schur[i*N+j].i;
    2407: #endif
    2408:           array[i*N+j] = val;
    2409:           array[j*N+i] = val;
    2410:         }
    2411:       }
    2412:     }
    2413:   }
    2414:   MatDenseRestoreArray(St,&array);
    2415:   *S   = St;
    2416:   return(0);
    2417: }

    2419: /* -------------------------------------------------------------------------------------------*/
    2420: PetscErrorCode MatMumpsSetIcntl_MUMPS(Mat F,PetscInt icntl,PetscInt ival)
    2421: {
    2423:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2426:   PetscMUMPSIntCast(ival,&mumps->id.ICNTL(icntl));
    2427:   return(0);
    2428: }

    2430: PetscErrorCode MatMumpsGetIcntl_MUMPS(Mat F,PetscInt icntl,PetscInt *ival)
    2431: {
    2432:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2435:   *ival = mumps->id.ICNTL(icntl);
    2436:   return(0);
    2437: }

    2439: /*@
    2440:   MatMumpsSetIcntl - Set MUMPS parameter ICNTL()

    2442:    Logically Collective on Mat

    2444:    Input Parameters:
    2445: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2446: .  icntl - index of MUMPS parameter array ICNTL()
    2447: -  ival - value of MUMPS ICNTL(icntl)

    2449:   Options Database:
    2450: .   -mat_mumps_icntl_<icntl> <ival>

    2452:    Level: beginner

    2454:    References:
    2455: .     MUMPS Users' Guide

    2457: .seealso: MatGetFactor(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2458:  @*/
    2459: PetscErrorCode MatMumpsSetIcntl(Mat F,PetscInt icntl,PetscInt ival)
    2460: {

    2465:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2468:   PetscTryMethod(F,"MatMumpsSetIcntl_C",(Mat,PetscInt,PetscInt),(F,icntl,ival));
    2469:   return(0);
    2470: }

    2472: /*@
    2473:   MatMumpsGetIcntl - Get MUMPS parameter ICNTL()

    2475:    Logically Collective on Mat

    2477:    Input Parameters:
    2478: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2479: -  icntl - index of MUMPS parameter array ICNTL()

    2481:   Output Parameter:
    2482: .  ival - value of MUMPS ICNTL(icntl)

    2484:    Level: beginner

    2486:    References:
    2487: .     MUMPS Users' Guide

    2489: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2490: @*/
    2491: PetscErrorCode MatMumpsGetIcntl(Mat F,PetscInt icntl,PetscInt *ival)
    2492: {

    2497:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2500:   PetscUseMethod(F,"MatMumpsGetIcntl_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));
    2501:   return(0);
    2502: }

    2504: /* -------------------------------------------------------------------------------------------*/
    2505: PetscErrorCode MatMumpsSetCntl_MUMPS(Mat F,PetscInt icntl,PetscReal val)
    2506: {
    2507:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2510:   mumps->id.CNTL(icntl) = val;
    2511:   return(0);
    2512: }

    2514: PetscErrorCode MatMumpsGetCntl_MUMPS(Mat F,PetscInt icntl,PetscReal *val)
    2515: {
    2516:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2519:   *val = mumps->id.CNTL(icntl);
    2520:   return(0);
    2521: }

    2523: /*@
    2524:   MatMumpsSetCntl - Set MUMPS parameter CNTL()

    2526:    Logically Collective on Mat

    2528:    Input Parameters:
    2529: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2530: .  icntl - index of MUMPS parameter array CNTL()
    2531: -  val - value of MUMPS CNTL(icntl)

    2533:   Options Database:
    2534: .   -mat_mumps_cntl_<icntl> <val>

    2536:    Level: beginner

    2538:    References:
    2539: .     MUMPS Users' Guide

    2541: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2542: @*/
    2543: PetscErrorCode MatMumpsSetCntl(Mat F,PetscInt icntl,PetscReal val)
    2544: {

    2549:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2552:   PetscTryMethod(F,"MatMumpsSetCntl_C",(Mat,PetscInt,PetscReal),(F,icntl,val));
    2553:   return(0);
    2554: }

    2556: /*@
    2557:   MatMumpsGetCntl - Get MUMPS parameter CNTL()

    2559:    Logically Collective on Mat

    2561:    Input Parameters:
    2562: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2563: -  icntl - index of MUMPS parameter array CNTL()

    2565:   Output Parameter:
    2566: .  val - value of MUMPS CNTL(icntl)

    2568:    Level: beginner

    2570:    References:
    2571: .      MUMPS Users' Guide

    2573: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2574: @*/
    2575: PetscErrorCode MatMumpsGetCntl(Mat F,PetscInt icntl,PetscReal *val)
    2576: {

    2581:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2584:   PetscUseMethod(F,"MatMumpsGetCntl_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));
    2585:   return(0);
    2586: }

    2588: PetscErrorCode MatMumpsGetInfo_MUMPS(Mat F,PetscInt icntl,PetscInt *info)
    2589: {
    2590:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2593:   *info = mumps->id.INFO(icntl);
    2594:   return(0);
    2595: }

    2597: PetscErrorCode MatMumpsGetInfog_MUMPS(Mat F,PetscInt icntl,PetscInt *infog)
    2598: {
    2599:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2602:   *infog = mumps->id.INFOG(icntl);
    2603:   return(0);
    2604: }

    2606: PetscErrorCode MatMumpsGetRinfo_MUMPS(Mat F,PetscInt icntl,PetscReal *rinfo)
    2607: {
    2608:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2611:   *rinfo = mumps->id.RINFO(icntl);
    2612:   return(0);
    2613: }

    2615: PetscErrorCode MatMumpsGetRinfog_MUMPS(Mat F,PetscInt icntl,PetscReal *rinfog)
    2616: {
    2617:   Mat_MUMPS *mumps =(Mat_MUMPS*)F->data;

    2620:   *rinfog = mumps->id.RINFOG(icntl);
    2621:   return(0);
    2622: }

    2624: PetscErrorCode MatMumpsGetInverse_MUMPS(Mat F,Mat spRHS)
    2625: {
    2627:   Mat            Bt = NULL,Btseq = NULL;
    2628:   PetscBool      flg;
    2629:   Mat_MUMPS      *mumps =(Mat_MUMPS*)F->data;
    2630:   PetscScalar    *aa;
    2631:   PetscInt       spnr,*ia,*ja,M,nrhs;

    2635:   PetscObjectTypeCompare((PetscObject)spRHS,MATTRANSPOSEMAT,&flg);
    2636:   if (flg) {
    2637:     MatTransposeGetMat(spRHS,&Bt);
    2638:   } else SETERRQ(PetscObjectComm((PetscObject)spRHS),PETSC_ERR_ARG_WRONG,"Matrix spRHS must be type MATTRANSPOSEMAT matrix");

    2640:   MatMumpsSetIcntl(F,30,1);

    2642:   if (mumps->petsc_size > 1) {
    2643:     Mat_MPIAIJ *b = (Mat_MPIAIJ*)Bt->data;
    2644:     Btseq = b->A;
    2645:   } else {
    2646:     Btseq = Bt;
    2647:   }

    2649:   MatGetSize(spRHS,&M,&nrhs);
    2650:   mumps->id.nrhs = nrhs;
    2651:   mumps->id.lrhs = M;
    2652:   mumps->id.rhs  = NULL;

    2654:   if (!mumps->myid) {
    2655:     MatSeqAIJGetArray(Btseq,&aa);
    2656:     MatGetRowIJ(Btseq,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    2657:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
    2658:     PetscMUMPSIntCSRCast(mumps,spnr,ia,ja,&mumps->id.irhs_ptr,&mumps->id.irhs_sparse,&mumps->id.nz_rhs);
    2659:     mumps->id.rhs_sparse  = (MumpsScalar*)aa;
    2660:   } else {
    2661:     mumps->id.irhs_ptr    = NULL;
    2662:     mumps->id.irhs_sparse = NULL;
    2663:     mumps->id.nz_rhs      = 0;
    2664:     mumps->id.rhs_sparse  = NULL;
    2665:   }
    2666:   mumps->id.ICNTL(20)   = 1; /* rhs is sparse */
    2667:   mumps->id.ICNTL(21)   = 0; /* solution is in assembled centralized format */

    2669:   /* solve phase */
    2670:   /*-------------*/
    2671:   mumps->id.job = JOB_SOLVE;
    2672:   PetscMUMPS_c(mumps);
    2673:   if (mumps->id.INFOG(1) < 0)
    2674:     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));

    2676:   if (!mumps->myid) {
    2677:     MatSeqAIJRestoreArray(Btseq,&aa);
    2678:     MatRestoreRowIJ(Btseq,1,PETSC_FALSE,PETSC_FALSE,&spnr,(const PetscInt**)&ia,(const PetscInt**)&ja,&flg);
    2679:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot get IJ structure");
    2680:   }
    2681:   return(0);
    2682: }

    2684: /*@
    2685:   MatMumpsGetInverse - Get user-specified set of entries in inverse of A

    2687:    Logically Collective on Mat

    2689:    Input Parameters:
    2690: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2691: -  spRHS - sequential sparse matrix in MATTRANSPOSEMAT format holding specified indices in processor[0]

    2693:   Output Parameter:
    2694: . spRHS - requested entries of inverse of A

    2696:    Level: beginner

    2698:    References:
    2699: .      MUMPS Users' Guide

    2701: .seealso: MatGetFactor(), MatCreateTranspose()
    2702: @*/
    2703: PetscErrorCode MatMumpsGetInverse(Mat F,Mat spRHS)
    2704: {

    2709:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2710:   PetscUseMethod(F,"MatMumpsGetInverse_C",(Mat,Mat),(F,spRHS));
    2711:   return(0);
    2712: }

    2714: PetscErrorCode MatMumpsGetInverseTranspose_MUMPS(Mat F,Mat spRHST)
    2715: {
    2717:   Mat            spRHS;

    2720:   MatCreateTranspose(spRHST,&spRHS);
    2721:   MatMumpsGetInverse_MUMPS(F,spRHS);
    2722:   MatDestroy(&spRHS);
    2723:   return(0);
    2724: }

    2726: /*@
    2727:   MatMumpsGetInverseTranspose - Get user-specified set of entries in inverse of matrix A^T

    2729:    Logically Collective on Mat

    2731:    Input Parameters:
    2732: +  F - the factored matrix of A obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2733: -  spRHST - sequential sparse matrix in MATAIJ format holding specified indices of A^T in processor[0]

    2735:   Output Parameter:
    2736: . spRHST - requested entries of inverse of A^T

    2738:    Level: beginner

    2740:    References:
    2741: .      MUMPS Users' Guide

    2743: .seealso: MatGetFactor(), MatCreateTranspose(), MatMumpsGetInverse()
    2744: @*/
    2745: PetscErrorCode MatMumpsGetInverseTranspose(Mat F,Mat spRHST)
    2746: {
    2748:   PetscBool      flg;

    2752:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2753:   PetscObjectTypeCompareAny((PetscObject)spRHST,&flg,MATSEQAIJ,MATMPIAIJ,NULL);
    2754:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)spRHST),PETSC_ERR_ARG_WRONG,"Matrix spRHST must be MATAIJ matrix");

    2756:   PetscUseMethod(F,"MatMumpsGetInverseTranspose_C",(Mat,Mat),(F,spRHST));
    2757:   return(0);
    2758: }

    2760: /*@
    2761:   MatMumpsGetInfo - Get MUMPS parameter INFO()

    2763:    Logically Collective on Mat

    2765:    Input Parameters:
    2766: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2767: -  icntl - index of MUMPS parameter array INFO()

    2769:   Output Parameter:
    2770: .  ival - value of MUMPS INFO(icntl)

    2772:    Level: beginner

    2774:    References:
    2775: .      MUMPS Users' Guide

    2777: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2778: @*/
    2779: PetscErrorCode MatMumpsGetInfo(Mat F,PetscInt icntl,PetscInt *ival)
    2780: {

    2785:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2787:   PetscUseMethod(F,"MatMumpsGetInfo_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));
    2788:   return(0);
    2789: }

    2791: /*@
    2792:   MatMumpsGetInfog - Get MUMPS parameter INFOG()

    2794:    Logically Collective on Mat

    2796:    Input Parameters:
    2797: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2798: -  icntl - index of MUMPS parameter array INFOG()

    2800:   Output Parameter:
    2801: .  ival - value of MUMPS INFOG(icntl)

    2803:    Level: beginner

    2805:    References:
    2806: .      MUMPS Users' Guide

    2808: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetRinfo(), MatMumpsGetRinfog()
    2809: @*/
    2810: PetscErrorCode MatMumpsGetInfog(Mat F,PetscInt icntl,PetscInt *ival)
    2811: {

    2816:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2818:   PetscUseMethod(F,"MatMumpsGetInfog_C",(Mat,PetscInt,PetscInt*),(F,icntl,ival));
    2819:   return(0);
    2820: }

    2822: /*@
    2823:   MatMumpsGetRinfo - Get MUMPS parameter RINFO()

    2825:    Logically Collective on Mat

    2827:    Input Parameters:
    2828: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2829: -  icntl - index of MUMPS parameter array RINFO()

    2831:   Output Parameter:
    2832: .  val - value of MUMPS RINFO(icntl)

    2834:    Level: beginner

    2836:    References:
    2837: .       MUMPS Users' Guide

    2839: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfog()
    2840: @*/
    2841: PetscErrorCode MatMumpsGetRinfo(Mat F,PetscInt icntl,PetscReal *val)
    2842: {

    2847:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2849:   PetscUseMethod(F,"MatMumpsGetRinfo_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));
    2850:   return(0);
    2851: }

    2853: /*@
    2854:   MatMumpsGetRinfog - Get MUMPS parameter RINFOG()

    2856:    Logically Collective on Mat

    2858:    Input Parameters:
    2859: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
    2860: -  icntl - index of MUMPS parameter array RINFOG()

    2862:   Output Parameter:
    2863: .  val - value of MUMPS RINFOG(icntl)

    2865:    Level: beginner

    2867:    References:
    2868: .      MUMPS Users' Guide

    2870: .seealso: MatGetFactor(), MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo()
    2871: @*/
    2872: PetscErrorCode MatMumpsGetRinfog(Mat F,PetscInt icntl,PetscReal *val)
    2873: {

    2878:   if (!F->factortype) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
    2880:   PetscUseMethod(F,"MatMumpsGetRinfog_C",(Mat,PetscInt,PetscReal*),(F,icntl,val));
    2881:   return(0);
    2882: }

    2884: /*MC
    2885:   MATSOLVERMUMPS -  A matrix type providing direct solvers (LU and Cholesky) for
    2886:   distributed and sequential matrices via the external package MUMPS.

    2888:   Works with MATAIJ and MATSBAIJ matrices

    2890:   Use ./configure --download-mumps --download-scalapack --download-parmetis --download-metis --download-ptscotch to have PETSc installed with MUMPS

    2892:   Use ./configure --with-openmp --download-hwloc (or --with-hwloc) to enable running MUMPS in MPI+OpenMP hybrid mode and non-MUMPS in flat-MPI mode. See details below.

    2894:   Use -pc_type cholesky or lu -pc_factor_mat_solver_type mumps to use this direct solver

    2896:   Options Database Keys:
    2897: +  -mat_mumps_icntl_1 - ICNTL(1): output stream for error messages
    2898: .  -mat_mumps_icntl_2 - ICNTL(2): output stream for diagnostic printing, statistics, and warning
    2899: .  -mat_mumps_icntl_3 -  ICNTL(3): output stream for global information, collected on the host
    2900: .  -mat_mumps_icntl_4 -  ICNTL(4): level of printing (0 to 4)
    2901: .  -mat_mumps_icntl_6 - ICNTL(6): permutes to a zero-free diagonal and/or scale the matrix (0 to 7)
    2902: .  -mat_mumps_icntl_7 - ICNTL(7): computes a symmetric permutation in sequential analysis, 0=AMD, 2=AMF, 3=Scotch, 4=PORD, 5=Metis, 6=QAMD, and 7=auto
    2903:                         Use -pc_factor_mat_ordering_type <type> to have PETSc perform the ordering (sequential only)
    2904: .  -mat_mumps_icntl_8  - ICNTL(8): scaling strategy (-2 to 8 or 77)
    2905: .  -mat_mumps_icntl_10  - ICNTL(10): max num of refinements
    2906: .  -mat_mumps_icntl_11  - ICNTL(11): statistics related to an error analysis (via -ksp_view)
    2907: .  -mat_mumps_icntl_12  - ICNTL(12): an ordering strategy for symmetric matrices (0 to 3)
    2908: .  -mat_mumps_icntl_13  - ICNTL(13): parallelism of the root node (enable ScaLAPACK) and its splitting
    2909: .  -mat_mumps_icntl_14  - ICNTL(14): percentage increase in the estimated working space
    2910: .  -mat_mumps_icntl_19  - ICNTL(19): computes the Schur complement
    2911: .  -mat_mumps_icntl_20  - ICNTL(20): give MUMPS centralized (0) or distributed (10) dense RHS
    2912: .  -mat_mumps_icntl_22  - ICNTL(22): in-core/out-of-core factorization and solve (0 or 1)
    2913: .  -mat_mumps_icntl_23  - ICNTL(23): max size of the working memory (MB) that can allocate per processor
    2914: .  -mat_mumps_icntl_24  - ICNTL(24): detection of null pivot rows (0 or 1)
    2915: .  -mat_mumps_icntl_25  - ICNTL(25): compute a solution of a deficient matrix and a null space basis
    2916: .  -mat_mumps_icntl_26  - ICNTL(26): drives the solution phase if a Schur complement matrix
    2917: .  -mat_mumps_icntl_28  - ICNTL(28): use 1 for sequential analysis and ictnl(7) ordering, or 2 for parallel analysis and ictnl(29) ordering
    2918: .  -mat_mumps_icntl_29 - ICNTL(29): parallel ordering 1 = ptscotch, 2 = parmetis
    2919: .  -mat_mumps_icntl_30 - ICNTL(30): compute user-specified set of entries in inv(A)
    2920: .  -mat_mumps_icntl_31 - ICNTL(31): indicates which factors may be discarded during factorization
    2921: .  -mat_mumps_icntl_33 - ICNTL(33): compute determinant
    2922: .  -mat_mumps_icntl_35 - ICNTL(35): level of activation of BLR (Block Low-Rank) feature
    2923: .  -mat_mumps_icntl_36 - ICNTL(36): controls the choice of BLR factorization variant
    2924: .  -mat_mumps_icntl_38 - ICNTL(38): sets the estimated compression rate of LU factors with BLR
    2925: .  -mat_mumps_cntl_1  - CNTL(1): relative pivoting threshold
    2926: .  -mat_mumps_cntl_2  -  CNTL(2): stopping criterion of refinement
    2927: .  -mat_mumps_cntl_3 - CNTL(3): absolute pivoting threshold
    2928: .  -mat_mumps_cntl_4 - CNTL(4): value for static pivoting
    2929: .  -mat_mumps_cntl_5 - CNTL(5): fixation for null pivots
    2930: .  -mat_mumps_cntl_7 - CNTL(7): precision of the dropping parameter used during BLR factorization
    2931: -  -mat_mumps_use_omp_threads [m] - run MUMPS in MPI+OpenMP hybrid mode as if omp_set_num_threads(m) is called before calling MUMPS.
    2932:                                    Default might be the number of cores per CPU package (socket) as reported by hwloc and suggested by the MUMPS manual.

    2934:   Level: beginner

    2936:     Notes:
    2937:     MUMPS Cholesky does not handle (complex) Hermitian matrices http://mumps.enseeiht.fr/doc/userguide_5.2.1.pdf so using it will error if the matrix is Hermitian.

    2939:     When a MUMPS factorization fails inside a KSP solve, for example with a KSP_DIVERGED_PC_FAILED, one can find the MUMPS information about the failure by calling
    2940: $          KSPGetPC(ksp,&pc);
    2941: $          PCFactorGetMatrix(pc,&mat);
    2942: $          MatMumpsGetInfo(mat,....);
    2943: $          MatMumpsGetInfog(mat,....); etc.
    2944:            Or you can run with -ksp_error_if_not_converged and the program will be stopped and the information printed in the error message.

    2946:   Using MUMPS with 64-bit integers
    2947:     MUMPS provides 64-bit integer support in two build modes:
    2948:       full 64-bit: here MUMPS is built with C preprocessing flag -DINTSIZE64 and Fortran compiler option -i8, -fdefault-integer-8 or equivalent, and
    2949:       requires all dependent libraries MPI, ScaLAPACK, LAPACK and BLAS built the same way with 64-bit integers (for example ILP64 Intel MKL and MPI).

    2951:       selective 64-bit: with the default MUMPS build, 64-bit integers have been introduced where needed. In compressed sparse row (CSR) storage of matrices,
    2952:       MUMPS stores column indices in 32-bit, but row offsets in 64-bit, so you can have a huge number of non-zeros, but must have less than 2^31 rows and
    2953:       columns. This can lead to significant memory and performance gains with respect to a full 64-bit integer MUMPS version. This requires a regular (32-bit
    2954:       integer) build of all dependent libraries MPI, ScaLAPACK, LAPACK and BLAS.

    2956:     With --download-mumps=1, PETSc always build MUMPS in selective 64-bit mode, which can be used by both --with-64-bit-indices=0/1 variants of PETSc.

    2958:   Two modes to run MUMPS/PETSc with OpenMP
    2959: $     Set OMP_NUM_THREADS and run with fewer MPI ranks than cores. For example, if you want to have 16 OpenMP
    2960: $     threads per rank, then you may use "export OMP_NUM_THREADS=16 && mpirun -n 4 ./test".

    2962: $     -mat_mumps_use_omp_threads [m] and run your code with as many MPI ranks as the number of cores. For example,
    2963: $     if a compute node has 32 cores and you run on two nodes, you may use "mpirun -n 64 ./test -mat_mumps_use_omp_threads 16"

    2965:    To run MUMPS in MPI+OpenMP hybrid mode (i.e., enable multithreading in MUMPS), but still run the non-MUMPS part
    2966:    (i.e., PETSc part) of your code in the so-called flat-MPI (aka pure-MPI) mode, you need to configure PETSc with --with-openmp --download-hwloc
    2967:    (or --with-hwloc), and have an MPI that supports MPI-3.0's process shared memory (which is usually available). Since MUMPS calls BLAS
    2968:    libraries, to really get performance, you should have multithreaded BLAS libraries such as Intel MKL, AMD ACML, Cray libSci or OpenBLAS
    2969:    (PETSc will automatically try to utilized a threaded BLAS if --with-openmp is provided).

    2971:    If you run your code through a job submission system, there are caveats in MPI rank mapping. We use MPI_Comm_split_type() to obtain MPI
    2972:    processes on each compute node. Listing the processes in rank ascending order, we split processes on a node into consecutive groups of
    2973:    size m and create a communicator called omp_comm for each group. Rank 0 in an omp_comm is called the master rank, and others in the omp_comm
    2974:    are called slave ranks (or slaves). Only master ranks are seen to MUMPS and slaves are not. We will free CPUs assigned to slaves (might be set
    2975:    by CPU binding policies in job scripts) and make the CPUs available to the master so that OMP threads spawned by MUMPS can run on the CPUs.
    2976:    In a multi-socket compute node, MPI rank mapping is an issue. Still use the above example and suppose your compute node has two sockets,
    2977:    if you interleave MPI ranks on the two sockets, in other words, even ranks are placed on socket 0, and odd ranks are on socket 1, and bind
    2978:    MPI ranks to cores, then with -mat_mumps_use_omp_threads 16, a master rank (and threads it spawns) will use half cores in socket 0, and half
    2979:    cores in socket 1, that definitely hurts locality. On the other hand, if you map MPI ranks consecutively on the two sockets, then the
    2980:    problem will not happen. Therefore, when you use -mat_mumps_use_omp_threads, you need to keep an eye on your MPI rank mapping and CPU binding.
    2981:    For example, with the Slurm job scheduler, one can use srun --cpu-bind=verbose -m block:block to map consecutive MPI ranks to sockets and
    2982:    examine the mapping result.

    2984:    PETSc does not control thread binding in MUMPS. So to get best performance, one still has to set OMP_PROC_BIND and OMP_PLACES in job scripts,
    2985:    for example, export OMP_PLACES=threads and export OMP_PROC_BIND=spread. One does not need to export OMP_NUM_THREADS=m in job scripts as PETSc
    2986:    calls omp_set_num_threads(m) internally before calling MUMPS.

    2988:    References:
    2989: +   1. - Heroux, Michael A., R. Brightwell, and Michael M. Wolf. "Bi-modal MPI and MPI+ threads computing on scalable multicore systems." IJHPCA (Submitted) (2011).
    2990: -   2. - Gutierrez, Samuel K., et al. "Accommodating Thread-Level Heterogeneity in Coupled Parallel Applications." Parallel and Distributed Processing Symposium (IPDPS), 2017 IEEE International. IEEE, 2017.

    2992: .seealso: PCFactorSetMatSolverType(), MatSolverType, MatMumpsSetIcntl(), MatMumpsGetIcntl(), MatMumpsSetCntl(), MatMumpsGetCntl(), MatMumpsGetInfo(), MatMumpsGetInfog(), MatMumpsGetRinfo(), MatMumpsGetRinfog(), KSPGetPC(), PCFactorGetMatrix()

    2994: M*/

    2996: static PetscErrorCode MatFactorGetSolverType_mumps(Mat A,MatSolverType *type)
    2997: {
    2999:   *type = MATSOLVERMUMPS;
    3000:   return(0);
    3001: }

    3003: /* MatGetFactor for Seq and MPI AIJ matrices */
    3004: static PetscErrorCode MatGetFactor_aij_mumps(Mat A,MatFactorType ftype,Mat *F)
    3005: {
    3006:   Mat            B;
    3008:   Mat_MUMPS      *mumps;
    3009:   PetscBool      isSeqAIJ;
    3010:   PetscMPIInt    size;

    3013:  #if defined(PETSC_USE_COMPLEX)
    3014:   if (A->hermitian && !A->symmetric && ftype == MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian CHOLESKY Factor is not supported");
    3015:  #endif
    3016:   /* Create the factorization matrix */
    3017:   PetscObjectBaseTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);
    3018:   MatCreate(PetscObjectComm((PetscObject)A),&B);
    3019:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
    3020:   PetscStrallocpy("mumps",&((PetscObject)B)->type_name);
    3021:   MatSetUp(B);

    3023:   PetscNewLog(B,&mumps);

    3025:   B->ops->view    = MatView_MUMPS;
    3026:   B->ops->getinfo = MatGetInfo_MUMPS;

    3028:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);
    3029:   PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);
    3030:   PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);
    3031:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);
    3032:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);
    3033:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);
    3034:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);
    3035:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);
    3036:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);
    3037:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);
    3038:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);
    3039:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);
    3040:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);

    3042:   if (ftype == MAT_FACTOR_LU) {
    3043:     B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
    3044:     B->factortype            = MAT_FACTOR_LU;
    3045:     if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqaij;
    3046:     else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpiaij;
    3047:     PetscStrallocpy(MATORDERINGEXTERNAL,(char**)&B->preferredordering[MAT_FACTOR_LU]);
    3048:     mumps->sym = 0;
    3049:   } else {
    3050:     B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
    3051:     B->factortype                  = MAT_FACTOR_CHOLESKY;
    3052:     if (isSeqAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqaij_seqsbaij;
    3053:     else mumps->ConvertToTriples = MatConvertToTriples_mpiaij_mpisbaij;
    3054:     PetscStrallocpy(MATORDERINGEXTERNAL,(char**)&B->preferredordering[MAT_FACTOR_CHOLESKY]);
    3055: #if defined(PETSC_USE_COMPLEX)
    3056:     mumps->sym = 2;
    3057: #else
    3058:     if (A->spd_set && A->spd) mumps->sym = 1;
    3059:     else                      mumps->sym = 2;
    3060: #endif
    3061:   }

    3063:   /* set solvertype */
    3064:   PetscFree(B->solvertype);
    3065:   PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);
    3066:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
    3067:   if (size == 1) {
    3068:     /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
    3069:     B->canuseordering = PETSC_TRUE;
    3070:   }
    3071:   B->ops->destroy = MatDestroy_MUMPS;
    3072:   B->data         = (void*)mumps;

    3074:   PetscInitializeMUMPS(A,mumps);

    3076:   *F = B;
    3077:   return(0);
    3078: }

    3080: /* MatGetFactor for Seq and MPI SBAIJ matrices */
    3081: static PetscErrorCode MatGetFactor_sbaij_mumps(Mat A,MatFactorType ftype,Mat *F)
    3082: {
    3083:   Mat            B;
    3085:   Mat_MUMPS      *mumps;
    3086:   PetscBool      isSeqSBAIJ;
    3087:   PetscMPIInt    size;

    3090:  #if defined(PETSC_USE_COMPLEX)
    3091:   if (A->hermitian && !A->symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian CHOLESKY Factor is not supported");
    3092:  #endif
    3093:   MatCreate(PetscObjectComm((PetscObject)A),&B);
    3094:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
    3095:   PetscStrallocpy("mumps",&((PetscObject)B)->type_name);
    3096:   MatSetUp(B);

    3098:   PetscNewLog(B,&mumps);
    3099:   PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);
    3100:   if (isSeqSBAIJ) {
    3101:     mumps->ConvertToTriples = MatConvertToTriples_seqsbaij_seqsbaij;
    3102:   } else {
    3103:     mumps->ConvertToTriples = MatConvertToTriples_mpisbaij_mpisbaij;
    3104:   }

    3106:   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_MUMPS;
    3107:   B->ops->view                   = MatView_MUMPS;
    3108:   B->ops->getinfo                = MatGetInfo_MUMPS;

    3110:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);
    3111:   PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);
    3112:   PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);
    3113:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);
    3114:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);
    3115:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);
    3116:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);
    3117:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);
    3118:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);
    3119:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);
    3120:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);
    3121:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);
    3122:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);

    3124:   B->factortype = MAT_FACTOR_CHOLESKY;
    3125: #if defined(PETSC_USE_COMPLEX)
    3126:   mumps->sym = 2;
    3127: #else
    3128:   if (A->spd_set && A->spd) mumps->sym = 1;
    3129:   else                      mumps->sym = 2;
    3130: #endif

    3132:   /* set solvertype */
    3133:   PetscFree(B->solvertype);
    3134:   PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);
    3135:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
    3136:   if (size == 1) {
    3137:     /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
    3138:     B->canuseordering = PETSC_TRUE;
    3139:   }
    3140:   PetscStrallocpy(MATORDERINGEXTERNAL,(char**)&B->preferredordering[MAT_FACTOR_CHOLESKY]);
    3141:   B->ops->destroy = MatDestroy_MUMPS;
    3142:   B->data         = (void*)mumps;

    3144:   PetscInitializeMUMPS(A,mumps);

    3146:   *F = B;
    3147:   return(0);
    3148: }

    3150: static PetscErrorCode MatGetFactor_baij_mumps(Mat A,MatFactorType ftype,Mat *F)
    3151: {
    3152:   Mat            B;
    3154:   Mat_MUMPS      *mumps;
    3155:   PetscBool      isSeqBAIJ;
    3156:   PetscMPIInt    size;

    3159:   /* Create the factorization matrix */
    3160:   PetscObjectTypeCompare((PetscObject)A,MATSEQBAIJ,&isSeqBAIJ);
    3161:   MatCreate(PetscObjectComm((PetscObject)A),&B);
    3162:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
    3163:   PetscStrallocpy("mumps",&((PetscObject)B)->type_name);
    3164:   MatSetUp(B);

    3166:   PetscNewLog(B,&mumps);
    3167:   if (ftype == MAT_FACTOR_LU) {
    3168:     B->ops->lufactorsymbolic = MatLUFactorSymbolic_BAIJMUMPS;
    3169:     B->factortype            = MAT_FACTOR_LU;
    3170:     if (isSeqBAIJ) mumps->ConvertToTriples = MatConvertToTriples_seqbaij_seqaij;
    3171:     else mumps->ConvertToTriples = MatConvertToTriples_mpibaij_mpiaij;
    3172:     mumps->sym = 0;
    3173:     PetscStrallocpy(MATORDERINGEXTERNAL,(char**)&B->preferredordering[MAT_FACTOR_LU]);
    3174:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc BAIJ matrices with MUMPS Cholesky, use SBAIJ or AIJ matrix instead\n");

    3176:   B->ops->view        = MatView_MUMPS;
    3177:   B->ops->getinfo     = MatGetInfo_MUMPS;

    3179:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);
    3180:   PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);
    3181:   PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);
    3182:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);
    3183:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);
    3184:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);
    3185:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);
    3186:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);
    3187:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);
    3188:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);
    3189:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);
    3190:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverse_C",MatMumpsGetInverse_MUMPS);
    3191:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInverseTranspose_C",MatMumpsGetInverseTranspose_MUMPS);

    3193:   /* set solvertype */
    3194:   PetscFree(B->solvertype);
    3195:   PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);
    3196:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
    3197:   if (size == 1) {
    3198:     /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization */
    3199:     B->canuseordering = PETSC_TRUE;
    3200:   }
    3201:   B->ops->destroy = MatDestroy_MUMPS;
    3202:   B->data         = (void*)mumps;

    3204:   PetscInitializeMUMPS(A,mumps);

    3206:   *F = B;
    3207:   return(0);
    3208: }

    3210: /* MatGetFactor for Seq and MPI SELL matrices */
    3211: static PetscErrorCode MatGetFactor_sell_mumps(Mat A,MatFactorType ftype,Mat *F)
    3212: {
    3213:   Mat            B;
    3215:   Mat_MUMPS      *mumps;
    3216:   PetscBool      isSeqSELL;
    3217:   PetscMPIInt    size;

    3220:   /* Create the factorization matrix */
    3221:   PetscObjectTypeCompare((PetscObject)A,MATSEQSELL,&isSeqSELL);
    3222:   MatCreate(PetscObjectComm((PetscObject)A),&B);
    3223:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
    3224:   PetscStrallocpy("mumps",&((PetscObject)B)->type_name);
    3225:   MatSetUp(B);

    3227:   PetscNewLog(B,&mumps);

    3229:   B->ops->view        = MatView_MUMPS;
    3230:   B->ops->getinfo     = MatGetInfo_MUMPS;

    3232:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mumps);
    3233:   PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MUMPS);
    3234:   PetscObjectComposeFunction((PetscObject)B,"MatFactorCreateSchurComplement_C",MatFactorCreateSchurComplement_MUMPS);
    3235:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetIcntl_C",MatMumpsSetIcntl_MUMPS);
    3236:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetIcntl_C",MatMumpsGetIcntl_MUMPS);
    3237:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsSetCntl_C",MatMumpsSetCntl_MUMPS);
    3238:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetCntl_C",MatMumpsGetCntl_MUMPS);
    3239:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfo_C",MatMumpsGetInfo_MUMPS);
    3240:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetInfog_C",MatMumpsGetInfog_MUMPS);
    3241:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfo_C",MatMumpsGetRinfo_MUMPS);
    3242:   PetscObjectComposeFunction((PetscObject)B,"MatMumpsGetRinfog_C",MatMumpsGetRinfog_MUMPS);

    3244:   if (ftype == MAT_FACTOR_LU) {
    3245:     B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMUMPS;
    3246:     B->factortype            = MAT_FACTOR_LU;
    3247:     if (isSeqSELL) mumps->ConvertToTriples = MatConvertToTriples_seqsell_seqaij;
    3248:     else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"To be implemented");
    3249:     mumps->sym = 0;
    3250:     PetscStrallocpy(MATORDERINGEXTERNAL,(char**)&B->preferredordering[MAT_FACTOR_LU]);
    3251:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"To be implemented");

    3253:   /* set solvertype */
    3254:   PetscFree(B->solvertype);
    3255:   PetscStrallocpy(MATSOLVERMUMPS,&B->solvertype);
    3256:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
    3257:   if (size == 1) {
    3258:     /* MUMPS option -mat_mumps_icntl_7 1 is automatically set if PETSc ordering is passed into symbolic factorization  */
    3259:     B->canuseordering = PETSC_TRUE;
    3260:   }
    3261:   B->ops->destroy = MatDestroy_MUMPS;
    3262:   B->data         = (void*)mumps;

    3264:   PetscInitializeMUMPS(A,mumps);

    3266:   *F = B;
    3267:   return(0);
    3268: }

    3270: PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_MUMPS(void)
    3271: {

    3275:   MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mumps);
    3276:   MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mumps);
    3277:   MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIBAIJ,MAT_FACTOR_LU,MatGetFactor_baij_mumps);
    3278:   MatSolverTypeRegister(MATSOLVERMUMPS,MATMPIBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_baij_mumps);
    3279:   MatSolverTypeRegister(MATSOLVERMUMPS,MATMPISBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_sbaij_mumps);
    3280:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mumps);
    3281:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mumps);
    3282:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQBAIJ,MAT_FACTOR_LU,MatGetFactor_baij_mumps);
    3283:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_baij_mumps);
    3284:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQSBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_sbaij_mumps);
    3285:   MatSolverTypeRegister(MATSOLVERMUMPS,MATSEQSELL,MAT_FACTOR_LU,MatGetFactor_sell_mumps);
    3286:   return(0);
    3287: }