Actual source code: mkl_pardiso.c

petsc-master 2020-02-15
Report Typos and Errors
  1: #include <../src/mat/impls/aij/seq/aij.h>        /*I "petscmat.h" I*/
  2: #include <../src/mat/impls/sbaij/seq/sbaij.h>
  3: #include <../src/mat/impls/dense/seq/dense.h>

  5: #if defined(PETSC_HAVE_MKL_INTEL_ILP64)
  6: #define MKL_ILP64
  7: #endif
  8: #include <mkl_pardiso.h>

 10: PETSC_EXTERN void PetscSetMKL_PARDISOThreads(int);

 12: /*
 13:  *  Possible mkl_pardiso phases that controls the execution of the solver.
 14:  *  For more information check mkl_pardiso manual.
 15:  */
 16: #define JOB_ANALYSIS 11
 17: #define JOB_ANALYSIS_NUMERICAL_FACTORIZATION 12
 18: #define JOB_ANALYSIS_NUMERICAL_FACTORIZATION_SOLVE_ITERATIVE_REFINEMENT 13
 19: #define JOB_NUMERICAL_FACTORIZATION 22
 20: #define JOB_NUMERICAL_FACTORIZATION_SOLVE_ITERATIVE_REFINEMENT 23
 21: #define JOB_SOLVE_ITERATIVE_REFINEMENT 33
 22: #define JOB_SOLVE_FORWARD_SUBSTITUTION 331
 23: #define JOB_SOLVE_DIAGONAL_SUBSTITUTION 332
 24: #define JOB_SOLVE_BACKWARD_SUBSTITUTION 333
 25: #define JOB_RELEASE_OF_LU_MEMORY 0
 26: #define JOB_RELEASE_OF_ALL_MEMORY -1

 28: #define IPARM_SIZE 64

 30: #if defined(PETSC_USE_64BIT_INDICES)
 31:  #if defined(PETSC_HAVE_MKL_INTEL_ILP64)
 32:   #define INT_TYPE long long int
 33:   #define MKL_PARDISO pardiso
 34:   #define MKL_PARDISO_INIT pardisoinit
 35:  #else
 36:   /* this is the case where the MKL BLAS/LAPACK are 32 bit integers but the 64 bit integer version of
 37:      of Pardiso code is used; hence the need for the 64 below*/
 38:   #define INT_TYPE long long int
 39:   #define MKL_PARDISO pardiso_64
 40:   #define MKL_PARDISO_INIT pardiso_64init
 41: void pardiso_64init(void *pt, INT_TYPE *mtype, INT_TYPE iparm [])
 42: {
 43:   int iparm_copy[IPARM_SIZE], mtype_copy, i;

 45:   mtype_copy = *mtype;
 46:   pardisoinit(pt, &mtype_copy, iparm_copy);
 47:   for (i=0; i<IPARM_SIZE; i++) iparm[i] = iparm_copy[i];
 48: }
 49:  #endif
 50: #else
 51:  #define INT_TYPE int
 52:  #define MKL_PARDISO pardiso
 53:  #define MKL_PARDISO_INIT pardisoinit
 54: #endif


 57: /*
 58:  *  Internal data structure.
 59:  *  For more information check mkl_pardiso manual.
 60:  */
 61: typedef struct {

 63:   /* Configuration vector*/
 64:   INT_TYPE     iparm[IPARM_SIZE];

 66:   /*
 67:    * Internal mkl_pardiso memory location.
 68:    * After the first call to mkl_pardiso do not modify pt, as that could cause a serious memory leak.
 69:    */
 70:   void         *pt[IPARM_SIZE];

 72:   /* Basic mkl_pardiso info*/
 73:   INT_TYPE     phase, maxfct, mnum, mtype, n, nrhs, msglvl, err;

 75:   /* Matrix structure*/
 76:   void         *a;
 77:   INT_TYPE     *ia, *ja;

 79:   /* Number of non-zero elements*/
 80:   INT_TYPE     nz;

 82:   /* Row permutaton vector*/
 83:   INT_TYPE     *perm;

 85:   /* Define if matrix preserves sparse structure.*/
 86:   MatStructure matstruc;

 88:   PetscBool    needsym;
 89:   PetscBool    freeaij;

 91:   /* Schur complement */
 92:   PetscScalar  *schur;
 93:   PetscInt     schur_size;
 94:   PetscInt     *schur_idxs;
 95:   PetscScalar  *schur_work;
 96:   PetscBLASInt schur_work_size;
 97:   PetscBool    solve_interior;

 99:   /* True if mkl_pardiso function have been used.*/
100:   PetscBool CleanUp;

102:   /* Conversion to a format suitable for MKL */
103:   PetscErrorCode (*Convert)(Mat, PetscBool, MatReuse, PetscBool*, INT_TYPE*, INT_TYPE**, INT_TYPE**, PetscScalar**);
104: } Mat_MKL_PARDISO;

106: PetscErrorCode MatMKLPardiso_Convert_seqsbaij(Mat A,PetscBool sym,MatReuse reuse,PetscBool *free,INT_TYPE *nnz,INT_TYPE **r,INT_TYPE **c,PetscScalar **v)
107: {
108:   Mat_SeqSBAIJ   *aa = (Mat_SeqSBAIJ*)A->data;
109:   PetscInt       bs  = A->rmap->bs,i;

113:   if (!sym) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"This should not happen");
114:   *v      = aa->a;
115:   if (bs == 1) { /* already in the correct format */
116:     /* though PetscInt and INT_TYPE are of the same size since they are defined differently the Intel compiler requires a cast */
117:     *r    = (INT_TYPE*)aa->i;
118:     *c    = (INT_TYPE*)aa->j;
119:     *nnz  = (INT_TYPE)aa->nz;
120:     *free = PETSC_FALSE;
121:   } else if (reuse == MAT_INITIAL_MATRIX) {
122:     PetscInt m = A->rmap->n,nz = aa->nz;
123:     PetscInt *row,*col;
124:     PetscMalloc2(m+1,&row,nz,&col);
125:     for (i=0; i<m+1; i++) {
126:       row[i] = aa->i[i]+1;
127:     }
128:     for (i=0; i<nz; i++) {
129:       col[i] = aa->j[i]+1;
130:     }
131:     *r    = (INT_TYPE*)row;
132:     *c    = (INT_TYPE*)col;
133:     *nnz  = (INT_TYPE)nz;
134:     *free = PETSC_TRUE;
135:   }
136:   return(0);
137: }

139: PetscErrorCode MatMKLPardiso_Convert_seqbaij(Mat A,PetscBool sym,MatReuse reuse,PetscBool *free,INT_TYPE *nnz,INT_TYPE **r,INT_TYPE **c,PetscScalar **v)
140: {
141:   Mat_SeqBAIJ    *aa = (Mat_SeqBAIJ*)A->data;
142:   PetscInt       bs  = A->rmap->bs,i;

146:   if (!sym) {
147:     *v      = aa->a;
148:     if (bs == 1) { /* already in the correct format */
149:       /* though PetscInt and INT_TYPE are of the same size since they are defined differently the Intel compiler requires a cast */
150:       *r    = (INT_TYPE*)aa->i;
151:       *c    = (INT_TYPE*)aa->j;
152:       *nnz  = (INT_TYPE)aa->nz;
153:       *free = PETSC_FALSE;
154:       return(0);
155:     } else if (reuse == MAT_INITIAL_MATRIX) {
156:       PetscInt m = A->rmap->n,nz = aa->nz;
157:       PetscInt *row,*col;
158:       PetscMalloc2(m+1,&row,nz,&col);
159:       for (i=0; i<m+1; i++) {
160:         row[i] = aa->i[i]+1;
161:       }
162:       for (i=0; i<nz; i++) {
163:         col[i] = aa->j[i]+1;
164:       }
165:       *r    = (INT_TYPE*)row;
166:       *c    = (INT_TYPE*)col;
167:       *nnz  = (INT_TYPE)nz;
168:     }
169:     *free = PETSC_TRUE;
170:   } else {
171:     SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"This should not happen");
172:   }
173:   return(0);
174: }

176: PetscErrorCode MatMKLPardiso_Convert_seqaij(Mat A,PetscBool sym,MatReuse reuse,PetscBool *free,INT_TYPE *nnz,INT_TYPE **r,INT_TYPE **c,PetscScalar **v)
177: {
178:   Mat_SeqAIJ     *aa = (Mat_SeqAIJ*)A->data;
179:   PetscScalar    *aav;

183:   MatSeqAIJGetArrayRead(A,(const PetscScalar**)&aav);
184:   if (!sym) { /* already in the correct format */
185:     *v    = aav;
186:     *r    = (INT_TYPE*)aa->i;
187:     *c    = (INT_TYPE*)aa->j;
188:     *nnz  = (INT_TYPE)aa->nz;
189:     *free = PETSC_FALSE;
190:   } else if (reuse == MAT_INITIAL_MATRIX) { /* need to get the triangular part */
191:     PetscScalar *vals,*vv;
192:     PetscInt    *row,*col,*jj;
193:     PetscInt    m = A->rmap->n,nz,i;

195:     nz = 0;
196:     for (i=0; i<m; i++) nz += aa->i[i+1] - aa->diag[i];
197:     PetscMalloc2(m+1,&row,nz,&col);
198:     PetscMalloc1(nz,&vals);
199:     jj = col;
200:     vv = vals;

202:     row[0] = 0;
203:     for (i=0; i<m; i++) {
204:       PetscInt    *aj = aa->j + aa->diag[i];
205:       PetscScalar *av = aav + aa->diag[i];
206:       PetscInt    rl  = aa->i[i+1] - aa->diag[i],j;

208:       for (j=0; j<rl; j++) {
209:         *jj = *aj; jj++; aj++;
210:         *vv = *av; vv++; av++;
211:       }
212:       row[i+1] = row[i] + rl;
213:     }
214:     *v    = vals;
215:     *r    = (INT_TYPE*)row;
216:     *c    = (INT_TYPE*)col;
217:     *nnz  = (INT_TYPE)nz;
218:     *free = PETSC_TRUE;
219:   } else {
220:     PetscScalar *vv;
221:     PetscInt    m = A->rmap->n,i;

223:     vv = *v;
224:     for (i=0; i<m; i++) {
225:       PetscScalar *av = aav + aa->diag[i];
226:       PetscInt    rl  = aa->i[i+1] - aa->diag[i],j;
227:       for (j=0; j<rl; j++) {
228:         *vv = *av; vv++; av++;
229:       }
230:     }
231:     *free = PETSC_TRUE;
232:   }
233:   MatSeqAIJRestoreArrayRead(A,(const PetscScalar**)&aav);
234:   return(0);
235: }


238: static PetscErrorCode MatMKLPardisoSolveSchur_Private(Mat F, PetscScalar *B, PetscScalar *X)
239: {
240:   Mat_MKL_PARDISO      *mpardiso = (Mat_MKL_PARDISO*)F->data;
241:   Mat                  S,Xmat,Bmat;
242:   MatFactorSchurStatus schurstatus;
243:   PetscErrorCode       ierr;

246:   MatFactorFactorizeSchurComplement(F);
247:   MatFactorGetSchurComplement(F,&S,&schurstatus);
248:   if (X == B && schurstatus == MAT_FACTOR_SCHUR_INVERTED) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"X and B cannot point to the same address");
249:   MatCreateSeqDense(PETSC_COMM_SELF,mpardiso->schur_size,mpardiso->nrhs,B,&Bmat);
250:   MatCreateSeqDense(PETSC_COMM_SELF,mpardiso->schur_size,mpardiso->nrhs,X,&Xmat);
251:   MatSetType(Bmat,((PetscObject)S)->type_name);
252:   MatSetType(Xmat,((PetscObject)S)->type_name);
253: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
254:   MatBindToCPU(Xmat,S->boundtocpu);
255:   MatBindToCPU(Bmat,S->boundtocpu);
256: #endif

258: #if defined(PETSC_USE_COMPLEX)
259:   if (mpardiso->iparm[12-1] == 1) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Hermitian solve not implemented yet");
260: #endif

262:   switch (schurstatus) {
263:   case MAT_FACTOR_SCHUR_FACTORED:
264:     if (!mpardiso->iparm[12-1]) {
265:       MatMatSolve(S,Bmat,Xmat);
266:     } else { /* transpose solve */
267:       MatMatSolveTranspose(S,Bmat,Xmat);
268:     }
269:     break;
270:   case MAT_FACTOR_SCHUR_INVERTED:
271:     if (!mpardiso->iparm[12-1]) {
272:       MatMatMult(S,Bmat,MAT_REUSE_MATRIX,PETSC_DEFAULT,&Xmat);
273:     } else { /* transpose solve */
274:       MatTransposeMatMult(S,Bmat,MAT_REUSE_MATRIX,PETSC_DEFAULT,&Xmat);
275:     }
276:     break;
277:   default:
278:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
279:     break;
280:   }
281:   MatFactorRestoreSchurComplement(F,&S,schurstatus);
282:   MatDestroy(&Bmat);
283:   MatDestroy(&Xmat);
284:   return(0);
285: }

287: PetscErrorCode MatFactorSetSchurIS_MKL_PARDISO(Mat F, IS is)
288: {
289:   Mat_MKL_PARDISO   *mpardiso = (Mat_MKL_PARDISO*)F->data;
290:   const PetscScalar *arr;
291:   const PetscInt    *idxs;
292:   PetscInt          size,i;
293:   PetscMPIInt       csize;
294:   PetscBool         sorted;
295:   PetscErrorCode    ierr;

298:   MPI_Comm_size(PetscObjectComm((PetscObject)F),&csize);
299:   if (csize > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MKL_PARDISO parallel Schur complements not yet supported from PETSc");
300:   ISSorted(is,&sorted);
301:   if (!sorted) {
302:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS for MKL_PARDISO Schur complements needs to be sorted");
303:   }
304:   ISGetLocalSize(is,&size);
305:   PetscFree(mpardiso->schur_work);
306:   PetscBLASIntCast(PetscMax(mpardiso->n,2*size),&mpardiso->schur_work_size);
307:   PetscMalloc1(mpardiso->schur_work_size,&mpardiso->schur_work);
308:   MatDestroy(&F->schur);
309:   MatCreateSeqDense(PETSC_COMM_SELF,size,size,NULL,&F->schur);
310:   MatDenseGetArrayRead(F->schur,&arr);
311:   mpardiso->schur      = (PetscScalar*)arr;
312:   mpardiso->schur_size = size;
313:   MatDenseRestoreArrayRead(F->schur,&arr);
314:   if (mpardiso->mtype == 2) {
315:     MatSetOption(F->schur,MAT_SPD,PETSC_TRUE);
316:   }

318:   PetscFree(mpardiso->schur_idxs);
319:   PetscMalloc1(size,&mpardiso->schur_idxs);
320:   PetscArrayzero(mpardiso->perm,mpardiso->n);
321:   ISGetIndices(is,&idxs);
322:   PetscArraycpy(mpardiso->schur_idxs,idxs,size);
323:   for (i=0;i<size;i++) mpardiso->perm[idxs[i]] = 1;
324:   ISRestoreIndices(is,&idxs);
325:   if (size) { /* turn on Schur switch if the set of indices is not empty */
326:     mpardiso->iparm[36-1] = 2;
327:   }
328:   return(0);
329: }

331: PetscErrorCode MatDestroy_MKL_PARDISO(Mat A)
332: {
333:   Mat_MKL_PARDISO *mat_mkl_pardiso=(Mat_MKL_PARDISO*)A->data;
334:   PetscErrorCode  ierr;

337:   if (mat_mkl_pardiso->CleanUp) {
338:     mat_mkl_pardiso->phase = JOB_RELEASE_OF_ALL_MEMORY;

340:     MKL_PARDISO (mat_mkl_pardiso->pt,
341:       &mat_mkl_pardiso->maxfct,
342:       &mat_mkl_pardiso->mnum,
343:       &mat_mkl_pardiso->mtype,
344:       &mat_mkl_pardiso->phase,
345:       &mat_mkl_pardiso->n,
346:       NULL,
347:       NULL,
348:       NULL,
349:       NULL,
350:       &mat_mkl_pardiso->nrhs,
351:       mat_mkl_pardiso->iparm,
352:       &mat_mkl_pardiso->msglvl,
353:       NULL,
354:       NULL,
355:       &mat_mkl_pardiso->err);
356:   }
357:   PetscFree(mat_mkl_pardiso->perm);
358:   PetscFree(mat_mkl_pardiso->schur_work);
359:   PetscFree(mat_mkl_pardiso->schur_idxs);
360:   if (mat_mkl_pardiso->freeaij) {
361:     PetscFree2(mat_mkl_pardiso->ia,mat_mkl_pardiso->ja);
362:     if (mat_mkl_pardiso->iparm[34] == 1) {
363:       PetscFree(mat_mkl_pardiso->a);
364:     }
365:   }
366:   PetscFree(A->data);

368:   /* clear composed functions */
369:   PetscObjectComposeFunction((PetscObject)A,"MatFactorGetSolverType_C",NULL);
370:   PetscObjectComposeFunction((PetscObject)A,"MatFactorSetSchurIS_C",NULL);
371:   PetscObjectComposeFunction((PetscObject)A,"MatMkl_PardisoSetCntl_C",NULL);
372:   return(0);
373: }

375: static PetscErrorCode MatMKLPardisoScatterSchur_Private(Mat_MKL_PARDISO *mpardiso, PetscScalar *whole, PetscScalar *schur, PetscBool reduce)
376: {
378:   if (reduce) { /* data given for the whole matrix */
379:     PetscInt i,m=0,p=0;
380:     for (i=0;i<mpardiso->nrhs;i++) {
381:       PetscInt j;
382:       for (j=0;j<mpardiso->schur_size;j++) {
383:         schur[p+j] = whole[m+mpardiso->schur_idxs[j]];
384:       }
385:       m += mpardiso->n;
386:       p += mpardiso->schur_size;
387:     }
388:   } else { /* from Schur to whole */
389:     PetscInt i,m=0,p=0;
390:     for (i=0;i<mpardiso->nrhs;i++) {
391:       PetscInt j;
392:       for (j=0;j<mpardiso->schur_size;j++) {
393:         whole[m+mpardiso->schur_idxs[j]] = schur[p+j];
394:       }
395:       m += mpardiso->n;
396:       p += mpardiso->schur_size;
397:     }
398:   }
399:   return(0);
400: }

402: PetscErrorCode MatSolve_MKL_PARDISO(Mat A,Vec b,Vec x)
403: {
404:   Mat_MKL_PARDISO   *mat_mkl_pardiso=(Mat_MKL_PARDISO*)A->data;
405:   PetscErrorCode    ierr;
406:   PetscScalar       *xarray;
407:   const PetscScalar *barray;

410:   mat_mkl_pardiso->nrhs = 1;
411:   VecGetArray(x,&xarray);
412:   VecGetArrayRead(b,&barray);

414:   if (!mat_mkl_pardiso->schur) mat_mkl_pardiso->phase = JOB_SOLVE_ITERATIVE_REFINEMENT;
415:   else mat_mkl_pardiso->phase = JOB_SOLVE_FORWARD_SUBSTITUTION;

417:   if (barray == xarray) { /* if the two vectors share the same memory */
418:     PetscScalar *work;
419:     if (!mat_mkl_pardiso->schur_work) {
420:       PetscMalloc1(mat_mkl_pardiso->n,&work);
421:     } else {
422:       work = mat_mkl_pardiso->schur_work;
423:     }
424:     mat_mkl_pardiso->iparm[6-1] = 1;
425:     MKL_PARDISO (mat_mkl_pardiso->pt,
426:       &mat_mkl_pardiso->maxfct,
427:       &mat_mkl_pardiso->mnum,
428:       &mat_mkl_pardiso->mtype,
429:       &mat_mkl_pardiso->phase,
430:       &mat_mkl_pardiso->n,
431:       mat_mkl_pardiso->a,
432:       mat_mkl_pardiso->ia,
433:       mat_mkl_pardiso->ja,
434:       NULL,
435:       &mat_mkl_pardiso->nrhs,
436:       mat_mkl_pardiso->iparm,
437:       &mat_mkl_pardiso->msglvl,
438:       (void*)xarray,
439:       (void*)work,
440:       &mat_mkl_pardiso->err);
441:     if (!mat_mkl_pardiso->schur_work) {
442:       PetscFree(work);
443:     }
444:   } else {
445:     mat_mkl_pardiso->iparm[6-1] = 0;
446:     MKL_PARDISO (mat_mkl_pardiso->pt,
447:       &mat_mkl_pardiso->maxfct,
448:       &mat_mkl_pardiso->mnum,
449:       &mat_mkl_pardiso->mtype,
450:       &mat_mkl_pardiso->phase,
451:       &mat_mkl_pardiso->n,
452:       mat_mkl_pardiso->a,
453:       mat_mkl_pardiso->ia,
454:       mat_mkl_pardiso->ja,
455:       mat_mkl_pardiso->perm,
456:       &mat_mkl_pardiso->nrhs,
457:       mat_mkl_pardiso->iparm,
458:       &mat_mkl_pardiso->msglvl,
459:       (void*)barray,
460:       (void*)xarray,
461:       &mat_mkl_pardiso->err);
462:   }
463:   VecRestoreArrayRead(b,&barray);

465:   if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);

467:   if (mat_mkl_pardiso->schur) { /* solve Schur complement and expand solution */
468:     PetscInt shift = mat_mkl_pardiso->schur_size;

470:     /* if inverted, uses BLAS *MM subroutines, otherwise LAPACK *TRS */
471:     if (A->schur_status != MAT_FACTOR_SCHUR_INVERTED) shift = 0;

473:     if (!mat_mkl_pardiso->solve_interior) {
474:       /* solve Schur complement */
475:       MatMKLPardisoScatterSchur_Private(mat_mkl_pardiso,xarray,mat_mkl_pardiso->schur_work,PETSC_TRUE);
476:       MatMKLPardisoSolveSchur_Private(A,mat_mkl_pardiso->schur_work,mat_mkl_pardiso->schur_work+shift);
477:       MatMKLPardisoScatterSchur_Private(mat_mkl_pardiso,xarray,mat_mkl_pardiso->schur_work+shift,PETSC_FALSE);
478:     } else { /* if we are solving for the interior problem, any value in barray[schur] forward-substituted to xarray[schur] will be neglected */
479:       PetscInt i;
480:       for (i=0;i<mat_mkl_pardiso->schur_size;i++) {
481:         xarray[mat_mkl_pardiso->schur_idxs[i]] = 0.;
482:       }
483:     }

485:     /* expansion phase */
486:     mat_mkl_pardiso->iparm[6-1] = 1;
487:     mat_mkl_pardiso->phase = JOB_SOLVE_BACKWARD_SUBSTITUTION;
488:     MKL_PARDISO (mat_mkl_pardiso->pt,
489:       &mat_mkl_pardiso->maxfct,
490:       &mat_mkl_pardiso->mnum,
491:       &mat_mkl_pardiso->mtype,
492:       &mat_mkl_pardiso->phase,
493:       &mat_mkl_pardiso->n,
494:       mat_mkl_pardiso->a,
495:       mat_mkl_pardiso->ia,
496:       mat_mkl_pardiso->ja,
497:       mat_mkl_pardiso->perm,
498:       &mat_mkl_pardiso->nrhs,
499:       mat_mkl_pardiso->iparm,
500:       &mat_mkl_pardiso->msglvl,
501:       (void*)xarray,
502:       (void*)mat_mkl_pardiso->schur_work, /* according to the specs, the solution vector is always used */
503:       &mat_mkl_pardiso->err);

505:     if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);
506:     mat_mkl_pardiso->iparm[6-1] = 0;
507:   }
508:   VecRestoreArray(x,&xarray);
509:   mat_mkl_pardiso->CleanUp = PETSC_TRUE;
510:   return(0);
511: }

513: PetscErrorCode MatSolveTranspose_MKL_PARDISO(Mat A,Vec b,Vec x)
514: {
515:   Mat_MKL_PARDISO *mat_mkl_pardiso=(Mat_MKL_PARDISO*)A->data;
516:   PetscInt        oiparm12;
517:   PetscErrorCode  ierr;

520:   oiparm12 = mat_mkl_pardiso->iparm[12 - 1];
521:   mat_mkl_pardiso->iparm[12 - 1] = 2;
522:   MatSolve_MKL_PARDISO(A,b,x);
523:   mat_mkl_pardiso->iparm[12 - 1] = oiparm12;
524:   return(0);
525: }

527: PetscErrorCode MatMatSolve_MKL_PARDISO(Mat A,Mat B,Mat X)
528: {
529:   Mat_MKL_PARDISO   *mat_mkl_pardiso=(Mat_MKL_PARDISO*)(A)->data;
530:   PetscErrorCode    ierr;
531:   const PetscScalar *barray;
532:   PetscScalar       *xarray;
533:   PetscBool         flg;

536:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQDENSE,&flg);
537:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATSEQDENSE matrix");
538:   if (X != B) {
539:     PetscObjectBaseTypeCompare((PetscObject)X,MATSEQDENSE,&flg);
540:     if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATSEQDENSE matrix");
541:   }

543:   MatGetSize(B,NULL,(PetscInt*)&mat_mkl_pardiso->nrhs);

545:   if (mat_mkl_pardiso->nrhs > 0) {
546:     MatDenseGetArrayRead(B,&barray);
547:     MatDenseGetArray(X,&xarray);

549:     if (barray == xarray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"B and X cannot share the same memory location");
550:     if (!mat_mkl_pardiso->schur) mat_mkl_pardiso->phase = JOB_SOLVE_ITERATIVE_REFINEMENT;
551:     else mat_mkl_pardiso->phase = JOB_SOLVE_FORWARD_SUBSTITUTION;

553:     MKL_PARDISO (mat_mkl_pardiso->pt,
554:       &mat_mkl_pardiso->maxfct,
555:       &mat_mkl_pardiso->mnum,
556:       &mat_mkl_pardiso->mtype,
557:       &mat_mkl_pardiso->phase,
558:       &mat_mkl_pardiso->n,
559:       mat_mkl_pardiso->a,
560:       mat_mkl_pardiso->ia,
561:       mat_mkl_pardiso->ja,
562:       mat_mkl_pardiso->perm,
563:       &mat_mkl_pardiso->nrhs,
564:       mat_mkl_pardiso->iparm,
565:       &mat_mkl_pardiso->msglvl,
566:       (void*)barray,
567:       (void*)xarray,
568:       &mat_mkl_pardiso->err);
569:     if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);

571:     MatDenseRestoreArrayRead(B,&barray);
572:     if (mat_mkl_pardiso->schur) { /* solve Schur complement and expand solution */
573:       PetscScalar *o_schur_work = NULL;
574:       PetscInt    shift = mat_mkl_pardiso->schur_size*mat_mkl_pardiso->nrhs,scale;
575:       PetscInt    mem = mat_mkl_pardiso->n*mat_mkl_pardiso->nrhs;

577:       /* allocate extra memory if it is needed */
578:       scale = 1;
579:       if (A->schur_status == MAT_FACTOR_SCHUR_INVERTED) scale = 2;

581:       mem *= scale;
582:       if (mem > mat_mkl_pardiso->schur_work_size) {
583:         o_schur_work = mat_mkl_pardiso->schur_work;
584:         PetscMalloc1(mem,&mat_mkl_pardiso->schur_work);
585:       }

587:       /* if inverted, uses BLAS *MM subroutines, otherwise LAPACK *TRS */
588:       if (A->schur_status != MAT_FACTOR_SCHUR_INVERTED) shift = 0;

590:       /* solve Schur complement */
591:       if (!mat_mkl_pardiso->solve_interior) {
592:         MatMKLPardisoScatterSchur_Private(mat_mkl_pardiso,xarray,mat_mkl_pardiso->schur_work,PETSC_TRUE);
593:         MatMKLPardisoSolveSchur_Private(A,mat_mkl_pardiso->schur_work,mat_mkl_pardiso->schur_work+shift);
594:         MatMKLPardisoScatterSchur_Private(mat_mkl_pardiso,xarray,mat_mkl_pardiso->schur_work+shift,PETSC_FALSE);
595:       } else { /* if we are solving for the interior problem, any value in barray[schur,n] forward-substituted to xarray[schur,n] will be neglected */
596:         PetscInt i,n,m=0;
597:         for (n=0;n<mat_mkl_pardiso->nrhs;n++) {
598:           for (i=0;i<mat_mkl_pardiso->schur_size;i++) {
599:             xarray[mat_mkl_pardiso->schur_idxs[i]+m] = 0.;
600:           }
601:           m += mat_mkl_pardiso->n;
602:         }
603:       }

605:       /* expansion phase */
606:       mat_mkl_pardiso->iparm[6-1] = 1;
607:       mat_mkl_pardiso->phase = JOB_SOLVE_BACKWARD_SUBSTITUTION;
608:       MKL_PARDISO (mat_mkl_pardiso->pt,
609:         &mat_mkl_pardiso->maxfct,
610:         &mat_mkl_pardiso->mnum,
611:         &mat_mkl_pardiso->mtype,
612:         &mat_mkl_pardiso->phase,
613:         &mat_mkl_pardiso->n,
614:         mat_mkl_pardiso->a,
615:         mat_mkl_pardiso->ia,
616:         mat_mkl_pardiso->ja,
617:         mat_mkl_pardiso->perm,
618:         &mat_mkl_pardiso->nrhs,
619:         mat_mkl_pardiso->iparm,
620:         &mat_mkl_pardiso->msglvl,
621:         (void*)xarray,
622:         (void*)mat_mkl_pardiso->schur_work, /* according to the specs, the solution vector is always used */
623:         &mat_mkl_pardiso->err);
624:       if (o_schur_work) { /* restore original schur_work (minimal size) */
625:         PetscFree(mat_mkl_pardiso->schur_work);
626:         mat_mkl_pardiso->schur_work = o_schur_work;
627:       }
628:       if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);
629:       mat_mkl_pardiso->iparm[6-1] = 0;
630:     }
631:   }
632:   mat_mkl_pardiso->CleanUp = PETSC_TRUE;
633:   return(0);
634: }

636: PetscErrorCode MatFactorNumeric_MKL_PARDISO(Mat F,Mat A,const MatFactorInfo *info)
637: {
638:   Mat_MKL_PARDISO *mat_mkl_pardiso=(Mat_MKL_PARDISO*)(F)->data;
639:   PetscErrorCode  ierr;

642:   mat_mkl_pardiso->matstruc = SAME_NONZERO_PATTERN;
643:   (*mat_mkl_pardiso->Convert)(A,mat_mkl_pardiso->needsym,MAT_REUSE_MATRIX,&mat_mkl_pardiso->freeaij,&mat_mkl_pardiso->nz,&mat_mkl_pardiso->ia,&mat_mkl_pardiso->ja,(PetscScalar**)&mat_mkl_pardiso->a);

645:   mat_mkl_pardiso->phase = JOB_NUMERICAL_FACTORIZATION;
646:   MKL_PARDISO (mat_mkl_pardiso->pt,
647:     &mat_mkl_pardiso->maxfct,
648:     &mat_mkl_pardiso->mnum,
649:     &mat_mkl_pardiso->mtype,
650:     &mat_mkl_pardiso->phase,
651:     &mat_mkl_pardiso->n,
652:     mat_mkl_pardiso->a,
653:     mat_mkl_pardiso->ia,
654:     mat_mkl_pardiso->ja,
655:     mat_mkl_pardiso->perm,
656:     &mat_mkl_pardiso->nrhs,
657:     mat_mkl_pardiso->iparm,
658:     &mat_mkl_pardiso->msglvl,
659:     NULL,
660:     (void*)mat_mkl_pardiso->schur,
661:     &mat_mkl_pardiso->err);
662:   if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);

664:   /* report flops */
665:   if (mat_mkl_pardiso->iparm[18] > 0) {
666:     PetscLogFlops(PetscPowRealInt(10.,6)*mat_mkl_pardiso->iparm[18]);
667:   }

669:   if (F->schur) { /* schur output from pardiso is in row major format */
670: #if defined(PETSC_HAVE_CUDA)
671:     F->schur->offloadmask = PETSC_OFFLOAD_CPU;
672: #endif
673:     MatFactorRestoreSchurComplement(F,NULL,MAT_FACTOR_SCHUR_UNFACTORED);
674:     MatTranspose(F->schur,MAT_INPLACE_MATRIX,&F->schur);
675:   }
676:   mat_mkl_pardiso->matstruc = SAME_NONZERO_PATTERN;
677:   mat_mkl_pardiso->CleanUp  = PETSC_TRUE;
678:   return(0);
679: }

681: PetscErrorCode PetscSetMKL_PARDISOFromOptions(Mat F, Mat A)
682: {
683:   Mat_MKL_PARDISO     *mat_mkl_pardiso = (Mat_MKL_PARDISO*)F->data;
684:   PetscErrorCode      ierr;
685:   PetscInt            icntl,threads=1;
686:   PetscBool           flg;

689:   PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MKL_PARDISO Options","Mat");

691:   PetscOptionsInt("-mat_mkl_pardiso_65","Number of threads to use within PARDISO","None",threads,&threads,&flg);
692:   if (flg) PetscSetMKL_PARDISOThreads((int)threads);

694:   PetscOptionsInt("-mat_mkl_pardiso_66","Maximum number of factors with identical sparsity structure that must be kept in memory at the same time","None",mat_mkl_pardiso->maxfct,&icntl,&flg);
695:   if (flg) mat_mkl_pardiso->maxfct = icntl;

697:   PetscOptionsInt("-mat_mkl_pardiso_67","Indicates the actual matrix for the solution phase","None",mat_mkl_pardiso->mnum,&icntl,&flg);
698:   if (flg) mat_mkl_pardiso->mnum = icntl;

700:   PetscOptionsInt("-mat_mkl_pardiso_68","Message level information","None",mat_mkl_pardiso->msglvl,&icntl,&flg);
701:   if (flg) mat_mkl_pardiso->msglvl = icntl;

703:   PetscOptionsInt("-mat_mkl_pardiso_69","Defines the matrix type","None",mat_mkl_pardiso->mtype,&icntl,&flg);
704:   if (flg) {
705:     void *pt[IPARM_SIZE];
706:     mat_mkl_pardiso->mtype = icntl;
707:     MKL_PARDISO_INIT(pt, &mat_mkl_pardiso->mtype, mat_mkl_pardiso->iparm);
708: #if defined(PETSC_USE_REAL_SINGLE)
709:     mat_mkl_pardiso->iparm[27] = 1;
710: #else
711:     mat_mkl_pardiso->iparm[27] = 0;
712: #endif
713:     mat_mkl_pardiso->iparm[34] = 1; /* use 0-based indexing */
714:   }
715:   PetscOptionsInt("-mat_mkl_pardiso_1","Use default values (if 0)","None",mat_mkl_pardiso->iparm[0],&icntl,&flg);

717:   if (flg && icntl != 0) {
718:     PetscOptionsInt("-mat_mkl_pardiso_2","Fill-in reducing ordering for the input matrix","None",mat_mkl_pardiso->iparm[1],&icntl,&flg);
719:     if (flg) mat_mkl_pardiso->iparm[1] = icntl;

721:     PetscOptionsInt("-mat_mkl_pardiso_4","Preconditioned CGS/CG","None",mat_mkl_pardiso->iparm[3],&icntl,&flg);
722:     if (flg) mat_mkl_pardiso->iparm[3] = icntl;

724:     PetscOptionsInt("-mat_mkl_pardiso_5","User permutation","None",mat_mkl_pardiso->iparm[4],&icntl,&flg);
725:     if (flg) mat_mkl_pardiso->iparm[4] = icntl;

727:     PetscOptionsInt("-mat_mkl_pardiso_6","Write solution on x","None",mat_mkl_pardiso->iparm[5],&icntl,&flg);
728:     if (flg) mat_mkl_pardiso->iparm[5] = icntl;

730:     PetscOptionsInt("-mat_mkl_pardiso_8","Iterative refinement step","None",mat_mkl_pardiso->iparm[7],&icntl,&flg);
731:     if (flg) mat_mkl_pardiso->iparm[7] = icntl;

733:     PetscOptionsInt("-mat_mkl_pardiso_10","Pivoting perturbation","None",mat_mkl_pardiso->iparm[9],&icntl,&flg);
734:     if (flg) mat_mkl_pardiso->iparm[9] = icntl;

736:     PetscOptionsInt("-mat_mkl_pardiso_11","Scaling vectors","None",mat_mkl_pardiso->iparm[10],&icntl,&flg);
737:     if (flg) mat_mkl_pardiso->iparm[10] = icntl;

739:     PetscOptionsInt("-mat_mkl_pardiso_12","Solve with transposed or conjugate transposed matrix A","None",mat_mkl_pardiso->iparm[11],&icntl,&flg);
740:     if (flg) mat_mkl_pardiso->iparm[11] = icntl;

742:     PetscOptionsInt("-mat_mkl_pardiso_13","Improved accuracy using (non-) symmetric weighted matching","None",mat_mkl_pardiso->iparm[12],&icntl,&flg);
743:     if (flg) mat_mkl_pardiso->iparm[12] = icntl;

745:     PetscOptionsInt("-mat_mkl_pardiso_18","Numbers of non-zero elements","None",mat_mkl_pardiso->iparm[17],&icntl,&flg);
746:     if (flg) mat_mkl_pardiso->iparm[17] = icntl;

748:     PetscOptionsInt("-mat_mkl_pardiso_19","Report number of floating point operations (0 to disable)","None",mat_mkl_pardiso->iparm[18],&icntl,&flg);
749:     if (flg) mat_mkl_pardiso->iparm[18] = icntl;

751:     PetscOptionsInt("-mat_mkl_pardiso_21","Pivoting for symmetric indefinite matrices","None",mat_mkl_pardiso->iparm[20],&icntl,&flg);
752:     if (flg) mat_mkl_pardiso->iparm[20] = icntl;

754:     PetscOptionsInt("-mat_mkl_pardiso_24","Parallel factorization control","None",mat_mkl_pardiso->iparm[23],&icntl,&flg);
755:     if (flg) mat_mkl_pardiso->iparm[23] = icntl;

757:     PetscOptionsInt("-mat_mkl_pardiso_25","Parallel forward/backward solve control","None",mat_mkl_pardiso->iparm[24],&icntl,&flg);
758:     if (flg) mat_mkl_pardiso->iparm[24] = icntl;

760:     PetscOptionsInt("-mat_mkl_pardiso_27","Matrix checker","None",mat_mkl_pardiso->iparm[26],&icntl,&flg);
761:     if (flg) mat_mkl_pardiso->iparm[26] = icntl;

763:     PetscOptionsInt("-mat_mkl_pardiso_31","Partial solve and computing selected components of the solution vectors","None",mat_mkl_pardiso->iparm[30],&icntl,&flg);
764:     if (flg) mat_mkl_pardiso->iparm[30] = icntl;

766:     PetscOptionsInt("-mat_mkl_pardiso_34","Optimal number of threads for conditional numerical reproducibility (CNR) mode","None",mat_mkl_pardiso->iparm[33],&icntl,&flg);
767:     if (flg) mat_mkl_pardiso->iparm[33] = icntl;

769:     PetscOptionsInt("-mat_mkl_pardiso_60","Intel MKL_PARDISO mode","None",mat_mkl_pardiso->iparm[59],&icntl,&flg);
770:     if (flg) mat_mkl_pardiso->iparm[59] = icntl;
771:   }
772:   PetscOptionsEnd();
773:   return(0);
774: }

776: PetscErrorCode MatFactorMKL_PARDISOInitialize_Private(Mat A, MatFactorType ftype, Mat_MKL_PARDISO *mat_mkl_pardiso)
777: {
779:   PetscInt       i,bs;
780:   PetscBool      match;

783:   for (i=0; i<IPARM_SIZE; i++) mat_mkl_pardiso->iparm[i] = 0;
784:   for (i=0; i<IPARM_SIZE; i++) mat_mkl_pardiso->pt[i] = 0;
785:   /* Default options for both sym and unsym */
786:   mat_mkl_pardiso->iparm[ 0] =  1; /* Solver default parameters overriden with provided by iparm */
787:   mat_mkl_pardiso->iparm[ 1] =  2; /* Metis reordering */
788:   mat_mkl_pardiso->iparm[ 5] =  0; /* Write solution into x */
789:   mat_mkl_pardiso->iparm[ 7] =  0; /* Max number of iterative refinement steps */
790:   mat_mkl_pardiso->iparm[17] = -1; /* Output: Number of nonzeros in the factor LU */
791:   mat_mkl_pardiso->iparm[18] = -1; /* Output: Mflops for LU factorization */
792: #if 0
793:   mat_mkl_pardiso->iparm[23] =  1; /* Parallel factorization control*/
794: #endif
795:   PetscObjectTypeCompareAny((PetscObject)A,&match,MATSEQBAIJ,MATSEQSBAIJ,"");
796:   MatGetBlockSize(A,&bs);
797:   if (!match || bs == 1) {
798:     mat_mkl_pardiso->iparm[34] = 1; /* Cluster Sparse Solver use C-style indexing for ia and ja arrays */
799:     mat_mkl_pardiso->n         = A->rmap->N;
800:   } else {
801:     mat_mkl_pardiso->iparm[34] = 0; /* Cluster Sparse Solver use Fortran-style indexing for ia and ja arrays */
802:     mat_mkl_pardiso->iparm[36] = bs;
803:     mat_mkl_pardiso->n         = A->rmap->N/bs;
804:   }
805:   mat_mkl_pardiso->iparm[39] =  0; /* Input: matrix/rhs/solution stored on master */

807:   mat_mkl_pardiso->CleanUp   = PETSC_FALSE;
808:   mat_mkl_pardiso->maxfct    = 1; /* Maximum number of numerical factorizations. */
809:   mat_mkl_pardiso->mnum      = 1; /* Which factorization to use. */
810:   mat_mkl_pardiso->msglvl    = 0; /* 0: do not print 1: Print statistical information in file */
811:   mat_mkl_pardiso->phase     = -1;
812:   mat_mkl_pardiso->err       = 0;

814:   mat_mkl_pardiso->nrhs      = 1;
815:   mat_mkl_pardiso->err       = 0;
816:   mat_mkl_pardiso->phase     = -1;

818:   if (ftype == MAT_FACTOR_LU) {
819:     mat_mkl_pardiso->iparm[ 9] = 13; /* Perturb the pivot elements with 1E-13 */
820:     mat_mkl_pardiso->iparm[10] =  1; /* Use nonsymmetric permutation and scaling MPS */
821:     mat_mkl_pardiso->iparm[12] =  1; /* Switch on Maximum Weighted Matching algorithm (default for non-symmetric) */
822:   } else {
823:     mat_mkl_pardiso->iparm[ 9] = 8; /* Perturb the pivot elements with 1E-8 */
824:     mat_mkl_pardiso->iparm[10] = 0; /* Use nonsymmetric permutation and scaling MPS */
825:     mat_mkl_pardiso->iparm[12] = 1; /* Switch on Maximum Weighted Matching algorithm (default for non-symmetric) */
826: #if defined(PETSC_USE_DEBUG)
827:     mat_mkl_pardiso->iparm[26] = 1; /* Matrix checker */
828: #endif
829:   }
830:   PetscMalloc1(A->rmap->N*sizeof(INT_TYPE), &mat_mkl_pardiso->perm);
831:   for (i=0; i<A->rmap->N; i++) {
832:     mat_mkl_pardiso->perm[i] = 0;
833:   }
834:   mat_mkl_pardiso->schur_size = 0;
835:   return(0);
836: }

838: PetscErrorCode MatFactorSymbolic_AIJMKL_PARDISO_Private(Mat F,Mat A,const MatFactorInfo *info)
839: {
840:   Mat_MKL_PARDISO *mat_mkl_pardiso = (Mat_MKL_PARDISO*)F->data;
841:   PetscErrorCode  ierr;

844:   mat_mkl_pardiso->matstruc = DIFFERENT_NONZERO_PATTERN;
845:   PetscSetMKL_PARDISOFromOptions(F,A);

847:   /* throw away any previously computed structure */
848:   if (mat_mkl_pardiso->freeaij) {
849:     PetscFree2(mat_mkl_pardiso->ia,mat_mkl_pardiso->ja);
850:     if (mat_mkl_pardiso->iparm[34] == 1) {
851:       PetscFree(mat_mkl_pardiso->a);
852:     }
853:   }
854:   (*mat_mkl_pardiso->Convert)(A,mat_mkl_pardiso->needsym,MAT_INITIAL_MATRIX,&mat_mkl_pardiso->freeaij,&mat_mkl_pardiso->nz,&mat_mkl_pardiso->ia,&mat_mkl_pardiso->ja,(PetscScalar**)&mat_mkl_pardiso->a);
855:   if (mat_mkl_pardiso->iparm[34] == 1) mat_mkl_pardiso->n = A->rmap->N;
856:   else mat_mkl_pardiso->n = A->rmap->N/A->rmap->bs;

858:   mat_mkl_pardiso->phase = JOB_ANALYSIS;

860:   /* reset flops counting if requested */
861:   if (mat_mkl_pardiso->iparm[18]) mat_mkl_pardiso->iparm[18] = -1;

863:   MKL_PARDISO (mat_mkl_pardiso->pt,
864:     &mat_mkl_pardiso->maxfct,
865:     &mat_mkl_pardiso->mnum,
866:     &mat_mkl_pardiso->mtype,
867:     &mat_mkl_pardiso->phase,
868:     &mat_mkl_pardiso->n,
869:     mat_mkl_pardiso->a,
870:     mat_mkl_pardiso->ia,
871:     mat_mkl_pardiso->ja,
872:     mat_mkl_pardiso->perm,
873:     &mat_mkl_pardiso->nrhs,
874:     mat_mkl_pardiso->iparm,
875:     &mat_mkl_pardiso->msglvl,
876:     NULL,
877:     NULL,
878:     &mat_mkl_pardiso->err);
879:   if (mat_mkl_pardiso->err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by MKL_PARDISO: err=%d. Please check manual",mat_mkl_pardiso->err);

881:   mat_mkl_pardiso->CleanUp = PETSC_TRUE;

883:   if (F->factortype == MAT_FACTOR_LU) F->ops->lufactornumeric = MatFactorNumeric_MKL_PARDISO;
884:   else F->ops->choleskyfactornumeric = MatFactorNumeric_MKL_PARDISO;

886:   F->ops->solve           = MatSolve_MKL_PARDISO;
887:   F->ops->solvetranspose  = MatSolveTranspose_MKL_PARDISO;
888:   F->ops->matsolve        = MatMatSolve_MKL_PARDISO;
889:   return(0);
890: }

892: PetscErrorCode MatLUFactorSymbolic_AIJMKL_PARDISO(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
893: {

897:   MatFactorSymbolic_AIJMKL_PARDISO_Private(F, A, info);
898:   return(0);
899: }

901: #if !defined(PETSC_USE_COMPLEX)
902: PetscErrorCode MatGetInertia_MKL_PARDISO(Mat F,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
903: {
904:   Mat_MKL_PARDISO   *mat_mkl_pardiso=(Mat_MKL_PARDISO*)F->data;

907:   if (nneg) *nneg = mat_mkl_pardiso->iparm[22];
908:   if (npos) *npos = mat_mkl_pardiso->iparm[21];
909:   if (nzero) *nzero = F->rmap->N - (mat_mkl_pardiso->iparm[22] + mat_mkl_pardiso->iparm[21]);
910:   return(0);
911: }
912: #endif

914: PetscErrorCode MatCholeskyFactorSymbolic_AIJMKL_PARDISO(Mat F,Mat A,IS r,const MatFactorInfo *info)
915: {

919:   MatFactorSymbolic_AIJMKL_PARDISO_Private(F, A, info);
920: #if defined(PETSC_USE_COMPLEX)
921:   F->ops->getinertia = NULL;
922: #else
923:   F->ops->getinertia = MatGetInertia_MKL_PARDISO;
924: #endif
925:   return(0);
926: }

928: PetscErrorCode MatView_MKL_PARDISO(Mat A, PetscViewer viewer)
929: {
930:   PetscErrorCode    ierr;
931:   PetscBool         iascii;
932:   PetscViewerFormat format;
933:   Mat_MKL_PARDISO   *mat_mkl_pardiso=(Mat_MKL_PARDISO*)A->data;
934:   PetscInt          i;

937:   if (A->ops->solve != MatSolve_MKL_PARDISO) return(0);

939:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
940:   if (iascii) {
941:     PetscViewerGetFormat(viewer,&format);
942:     if (format == PETSC_VIEWER_ASCII_INFO) {
943:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO run parameters:\n");
944:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO phase:             %d \n",mat_mkl_pardiso->phase);
945:       for (i=1; i<=64; i++) {
946:         PetscViewerASCIIPrintf(viewer,"MKL_PARDISO iparm[%d]:     %d \n",i, mat_mkl_pardiso->iparm[i - 1]);
947:       }
948:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO maxfct:     %d \n", mat_mkl_pardiso->maxfct);
949:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO mnum:     %d \n", mat_mkl_pardiso->mnum);
950:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO mtype:     %d \n", mat_mkl_pardiso->mtype);
951:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO n:     %d \n", mat_mkl_pardiso->n);
952:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO nrhs:     %d \n", mat_mkl_pardiso->nrhs);
953:       PetscViewerASCIIPrintf(viewer,"MKL_PARDISO msglvl:     %d \n", mat_mkl_pardiso->msglvl);
954:     }
955:   }
956:   return(0);
957: }


960: PetscErrorCode MatGetInfo_MKL_PARDISO(Mat A, MatInfoType flag, MatInfo *info)
961: {
962:   Mat_MKL_PARDISO *mat_mkl_pardiso = (Mat_MKL_PARDISO*)A->data;

965:   info->block_size        = 1.0;
966:   info->nz_used           = mat_mkl_pardiso->iparm[17];
967:   info->nz_allocated      = mat_mkl_pardiso->iparm[17];
968:   info->nz_unneeded       = 0.0;
969:   info->assemblies        = 0.0;
970:   info->mallocs           = 0.0;
971:   info->memory            = 0.0;
972:   info->fill_ratio_given  = 0;
973:   info->fill_ratio_needed = 0;
974:   info->factor_mallocs    = 0;
975:   return(0);
976: }

978: PetscErrorCode MatMkl_PardisoSetCntl_MKL_PARDISO(Mat F,PetscInt icntl,PetscInt ival)
979: {
980:   Mat_MKL_PARDISO *mat_mkl_pardiso = (Mat_MKL_PARDISO*)F->data;

983:   if (icntl <= 64) {
984:     mat_mkl_pardiso->iparm[icntl - 1] = ival;
985:   } else {
986:     if (icntl == 65) PetscSetMKL_PARDISOThreads(ival);
987:     else if (icntl == 66) mat_mkl_pardiso->maxfct = ival;
988:     else if (icntl == 67) mat_mkl_pardiso->mnum = ival;
989:     else if (icntl == 68) mat_mkl_pardiso->msglvl = ival;
990:     else if (icntl == 69) {
991:       void *pt[IPARM_SIZE];
992:       mat_mkl_pardiso->mtype = ival;
993:       MKL_PARDISO_INIT(pt, &mat_mkl_pardiso->mtype, mat_mkl_pardiso->iparm);
994: #if defined(PETSC_USE_REAL_SINGLE)
995:       mat_mkl_pardiso->iparm[27] = 1;
996: #else
997:       mat_mkl_pardiso->iparm[27] = 0;
998: #endif
999:       mat_mkl_pardiso->iparm[34] = 1;
1000:     } else if (icntl==70) mat_mkl_pardiso->solve_interior = (PetscBool)!!ival;
1001:   }
1002:   return(0);
1003: }

1005: /*@
1006:   MatMkl_PardisoSetCntl - Set Mkl_Pardiso parameters

1008:    Logically Collective on Mat

1010:    Input Parameters:
1011: +  F - the factored matrix obtained by calling MatGetFactor()
1012: .  icntl - index of Mkl_Pardiso parameter
1013: -  ival - value of Mkl_Pardiso parameter

1015:   Options Database:
1016: .   -mat_mkl_pardiso_<icntl> <ival>

1018:    Level: beginner

1020:    References:
1021: .      Mkl_Pardiso Users' Guide

1023: .seealso: MatGetFactor()
1024: @*/
1025: PetscErrorCode MatMkl_PardisoSetCntl(Mat F,PetscInt icntl,PetscInt ival)
1026: {

1030:   PetscTryMethod(F,"MatMkl_PardisoSetCntl_C",(Mat,PetscInt,PetscInt),(F,icntl,ival));
1031:   return(0);
1032: }

1034: /*MC
1035:   MATSOLVERMKL_PARDISO -  A matrix type providing direct solvers (LU) for
1036:   sequential matrices via the external package MKL_PARDISO.

1038:   Works with MATSEQAIJ matrices

1040:   Use -pc_type lu -pc_factor_mat_solver_type mkl_pardiso to use this direct solver

1042:   Options Database Keys:
1043: + -mat_mkl_pardiso_65 - Number of threads to use within MKL_PARDISO
1044: . -mat_mkl_pardiso_66 - Maximum number of factors with identical sparsity structure that must be kept in memory at the same time
1045: . -mat_mkl_pardiso_67 - Indicates the actual matrix for the solution phase
1046: . -mat_mkl_pardiso_68 - Message level information
1047: . -mat_mkl_pardiso_69 - Defines the matrix type. IMPORTANT: When you set this flag, iparm parameters are going to be set to the default ones for the matrix type
1048: . -mat_mkl_pardiso_1  - Use default values
1049: . -mat_mkl_pardiso_2  - Fill-in reducing ordering for the input matrix
1050: . -mat_mkl_pardiso_4  - Preconditioned CGS/CG
1051: . -mat_mkl_pardiso_5  - User permutation
1052: . -mat_mkl_pardiso_6  - Write solution on x
1053: . -mat_mkl_pardiso_8  - Iterative refinement step
1054: . -mat_mkl_pardiso_10 - Pivoting perturbation
1055: . -mat_mkl_pardiso_11 - Scaling vectors
1056: . -mat_mkl_pardiso_12 - Solve with transposed or conjugate transposed matrix A
1057: . -mat_mkl_pardiso_13 - Improved accuracy using (non-) symmetric weighted matching
1058: . -mat_mkl_pardiso_18 - Numbers of non-zero elements
1059: . -mat_mkl_pardiso_19 - Report number of floating point operations
1060: . -mat_mkl_pardiso_21 - Pivoting for symmetric indefinite matrices
1061: . -mat_mkl_pardiso_24 - Parallel factorization control
1062: . -mat_mkl_pardiso_25 - Parallel forward/backward solve control
1063: . -mat_mkl_pardiso_27 - Matrix checker
1064: . -mat_mkl_pardiso_31 - Partial solve and computing selected components of the solution vectors
1065: . -mat_mkl_pardiso_34 - Optimal number of threads for conditional numerical reproducibility (CNR) mode
1066: - -mat_mkl_pardiso_60 - Intel MKL_PARDISO mode

1068:   Level: beginner

1070:   For more information please check  mkl_pardiso manual

1072: .seealso: PCFactorSetMatSolverType(), MatSolverType

1074: M*/
1075: static PetscErrorCode MatFactorGetSolverType_mkl_pardiso(Mat A, MatSolverType *type)
1076: {
1078:   *type = MATSOLVERMKL_PARDISO;
1079:   return(0);
1080: }

1082: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mkl_pardiso(Mat A,MatFactorType ftype,Mat *F)
1083: {
1084:   Mat             B;
1085:   PetscErrorCode  ierr;
1086:   Mat_MKL_PARDISO *mat_mkl_pardiso;
1087:   PetscBool       isSeqAIJ,isSeqBAIJ,isSeqSBAIJ;

1090:   PetscObjectBaseTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);
1091:   PetscObjectTypeCompare((PetscObject)A,MATSEQBAIJ,&isSeqBAIJ);
1092:   PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);
1093:   MatCreate(PetscObjectComm((PetscObject)A),&B);
1094:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
1095:   PetscStrallocpy("mkl_pardiso",&((PetscObject)B)->type_name);
1096:   MatSetUp(B);

1098:   PetscNewLog(B,&mat_mkl_pardiso);
1099:   B->data = mat_mkl_pardiso;

1101:   MatFactorMKL_PARDISOInitialize_Private(A, ftype, mat_mkl_pardiso);
1102:   if (ftype == MAT_FACTOR_LU) {
1103:     B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJMKL_PARDISO;
1104:     B->factortype            = MAT_FACTOR_LU;
1105:     mat_mkl_pardiso->needsym = PETSC_FALSE;
1106:     if (isSeqAIJ) mat_mkl_pardiso->Convert = MatMKLPardiso_Convert_seqaij;
1107:     else if (isSeqBAIJ) mat_mkl_pardiso->Convert = MatMKLPardiso_Convert_seqbaij;
1108:     else if (isSeqSBAIJ) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for PARDISO LU factor with SEQSBAIJ format! Use MAT_FACTOR_CHOLESKY instead");
1109:     else SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for PARDISO LU with %s format",((PetscObject)A)->type_name);
1110: #if defined(PETSC_USE_COMPLEX)
1111:     mat_mkl_pardiso->mtype = 13;
1112: #else
1113:     mat_mkl_pardiso->mtype = 11;
1114: #endif
1115:   } else {
1116:     B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_AIJMKL_PARDISO;
1117:     B->factortype                  = MAT_FACTOR_CHOLESKY;
1118:     if (isSeqAIJ) mat_mkl_pardiso->Convert = MatMKLPardiso_Convert_seqaij;
1119:     else if (isSeqBAIJ) mat_mkl_pardiso->Convert = MatMKLPardiso_Convert_seqbaij;
1120:     else if (isSeqSBAIJ) mat_mkl_pardiso->Convert = MatMKLPardiso_Convert_seqsbaij;
1121:     else SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for PARDISO CHOLESKY with %s format",((PetscObject)A)->type_name);

1123:     mat_mkl_pardiso->needsym = PETSC_TRUE;
1124: #if !defined(PETSC_USE_COMPLEX)
1125:     if (A->spd_set && A->spd) mat_mkl_pardiso->mtype = 2;
1126:     else                      mat_mkl_pardiso->mtype = -2;
1127: #else
1128:     mat_mkl_pardiso->mtype = 6;
1129:     if (A->hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for PARDISO CHOLESKY with Hermitian matrices! Use MAT_FACTOR_LU instead");
1130: #endif
1131:   }
1132:   B->ops->destroy = MatDestroy_MKL_PARDISO;
1133:   B->ops->view    = MatView_MKL_PARDISO;
1134:   B->ops->getinfo = MatGetInfo_MKL_PARDISO;
1135:   B->factortype   = ftype;
1136:   B->assembled    = PETSC_TRUE;

1138:   PetscFree(B->solvertype);
1139:   PetscStrallocpy(MATSOLVERMKL_PARDISO,&B->solvertype);

1141:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_mkl_pardiso);
1142:   PetscObjectComposeFunction((PetscObject)B,"MatFactorSetSchurIS_C",MatFactorSetSchurIS_MKL_PARDISO);
1143:   PetscObjectComposeFunction((PetscObject)B,"MatMkl_PardisoSetCntl_C",MatMkl_PardisoSetCntl_MKL_PARDISO);

1145:   *F = B;
1146:   return(0);
1147: }

1149: PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_MKL_Pardiso(void)
1150: {

1154:   MatSolverTypeRegister(MATSOLVERMKL_PARDISO,MATSEQAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mkl_pardiso);
1155:   MatSolverTypeRegister(MATSOLVERMKL_PARDISO,MATSEQAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mkl_pardiso);
1156:   MatSolverTypeRegister(MATSOLVERMKL_PARDISO,MATSEQBAIJ,MAT_FACTOR_LU,MatGetFactor_aij_mkl_pardiso);
1157:   MatSolverTypeRegister(MATSOLVERMKL_PARDISO,MATSEQSBAIJ,MAT_FACTOR_CHOLESKY,MatGetFactor_aij_mkl_pardiso);
1158:   return(0);
1159: }