Actual source code: superlu.c

  1: #define PETSCMAT_DLL

  3: /* 
  4:         Provides an interface to the SuperLU 3.0 sparse solver
  5: */

 7:  #include src/mat/impls/aij/seq/aij.h

 10: #if defined(PETSC_USE_COMPLEX)
 11: #include "slu_zdefs.h"
 12: #else
 13: #include "slu_ddefs.h"
 14: #endif  
 15: #include "slu_util.h"

 18: typedef struct {
 19:   SuperMatrix       A,L,U,B,X;
 20:   superlu_options_t options;
 21:   PetscInt          *perm_c; /* column permutation vector */
 22:   PetscInt          *perm_r; /* row permutations from partial pivoting */
 23:   PetscInt          *etree;
 24:   PetscReal         *R, *C;
 25:   char              equed[1];
 26:   PetscInt          lwork;
 27:   void              *work;
 28:   PetscReal         rpg, rcond;
 29:   mem_usage_t       mem_usage;
 30:   MatStructure      flg;

 32:   /* A few function pointers for inheritance */
 33:   PetscErrorCode (*MatDuplicate)(Mat,MatDuplicateOption,Mat*);
 34:   PetscErrorCode (*MatView)(Mat,PetscViewer);
 35:   PetscErrorCode (*MatAssemblyEnd)(Mat,MatAssemblyType);
 36:   PetscErrorCode (*MatLUFactorSymbolic)(Mat,IS,IS,MatFactorInfo*,Mat*);
 37:   PetscErrorCode (*MatDestroy)(Mat);

 39:   /* Flag to clean up (non-global) SuperLU objects during Destroy */
 40:   PetscTruth CleanUpSuperLU;
 41: } Mat_SuperLU;


 44: EXTERN PetscErrorCode MatFactorInfo_SuperLU(Mat,PetscViewer);
 45: EXTERN PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat,IS,IS,MatFactorInfo*,Mat*);

 48: EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SuperLU_SeqAIJ(Mat,MatType,MatReuse,Mat*);
 49: EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SuperLU(Mat,MatType,MatReuse,Mat*);

 54: PetscErrorCode MatDestroy_SuperLU(Mat A)
 55: {
 57:   Mat_SuperLU    *lu=(Mat_SuperLU*)A->spptr;

 60:   if (lu->CleanUpSuperLU) { /* Free the SuperLU datastructures */
 61:     Destroy_SuperMatrix_Store(&lu->A);
 62:     Destroy_SuperMatrix_Store(&lu->B);
 63:     Destroy_SuperMatrix_Store(&lu->X);

 65:     PetscFree(lu->etree);
 66:     PetscFree(lu->perm_r);
 67:     PetscFree(lu->perm_c);
 68:     PetscFree(lu->R);
 69:     PetscFree(lu->C);
 70:     if ( lu->lwork >= 0 ) {
 71:       Destroy_SuperNode_Matrix(&lu->L);
 72:       Destroy_CompCol_Matrix(&lu->U);
 73:     }
 74:   }
 75:   MatConvert_SuperLU_SeqAIJ(A,MATSEQAIJ,MAT_REUSE_MATRIX,&A);
 76:   (*A->ops->destroy)(A);
 77:   return(0);
 78: }

 82: PetscErrorCode MatView_SuperLU(Mat A,PetscViewer viewer)
 83: {
 84:   PetscErrorCode    ierr;
 85:   PetscTruth        iascii;
 86:   PetscViewerFormat format;
 87:   Mat_SuperLU       *lu=(Mat_SuperLU*)(A->spptr);

 90:   (*lu->MatView)(A,viewer);

 92:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
 93:   if (iascii) {
 94:     PetscViewerGetFormat(viewer,&format);
 95:     if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
 96:       MatFactorInfo_SuperLU(A,viewer);
 97:     }
 98:   }
 99:   return(0);
100: }

104: PetscErrorCode MatAssemblyEnd_SuperLU(Mat A,MatAssemblyType mode) {
106:   Mat_SuperLU    *lu=(Mat_SuperLU*)(A->spptr);

109:   (*lu->MatAssemblyEnd)(A,mode);
110:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
111:   A->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU;
112:   return(0);
113: }

115: /* This function was written for SuperLU 2.0 by Matthew Knepley. Not tested for SuperLU 3.0! */
116: #ifdef SuperLU2
117:  #include src/mat/impls/dense/seq/dense.h
120: PetscErrorCode MatCreateNull_SuperLU(Mat A,Mat *nullMat)
121: {
122:   Mat_SuperLU   *lu = (Mat_SuperLU*)A->spptr;
123:   PetscInt      numRows = A->rmap.n,numCols = A->cmap.n;
124:   SCformat      *Lstore;
125:   PetscInt      numNullCols,size;
126:   SuperLUStat_t stat;
127: #if defined(PETSC_USE_COMPLEX)
128:   doublecomplex *nullVals,*workVals;
129: #else
130:   PetscScalar   *nullVals,*workVals;
131: #endif
132:   PetscInt           row,newRow,col,newCol,block,b;

136:   if (!A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
137:   numNullCols = numCols - numRows;
138:   if (numNullCols < 0) SETERRQ(PETSC_ERR_ARG_WRONG,"Function only applies to underdetermined problems");
139:   /* Create the null matrix using MATSEQDENSE explicitly */
140:   MatCreate(A->comm,nullMat);
141:   MatSetSizes(*nullMat,numRows,numNullCols,numRows,numNullCols);
142:   MatSetType(*nullMat,MATSEQDENSE);
143:   MatSeqDenseSetPreallocation(*nullMat,PETSC_NULL);
144:   if (!numNullCols) {
145:     MatAssemblyBegin(*nullMat,MAT_FINAL_ASSEMBLY);
146:     MatAssemblyEnd(*nullMat,MAT_FINAL_ASSEMBLY);
147:     return(0);
148:   }
149: #if defined(PETSC_USE_COMPLEX)
150:   nullVals = (doublecomplex*)((Mat_SeqDense*)(*nullMat)->data)->v;
151: #else
152:   nullVals = ((Mat_SeqDense*)(*nullMat)->data)->v;
153: #endif
154:   /* Copy in the columns */
155:   Lstore = (SCformat*)lu->L.Store;
156:   for(block = 0; block <= Lstore->nsuper; block++) {
157:     newRow = Lstore->sup_to_col[block];
158:     size   = Lstore->sup_to_col[block+1] - Lstore->sup_to_col[block];
159:     for(col = Lstore->rowind_colptr[newRow]; col < Lstore->rowind_colptr[newRow+1]; col++) {
160:       newCol = Lstore->rowind[col];
161:       if (newCol >= numRows) {
162:         for(b = 0; b < size; b++)
163: #if defined(PETSC_USE_COMPLEX)
164:           nullVals[(newCol-numRows)*numRows+newRow+b] = ((doublecomplex*)Lstore->nzval)[Lstore->nzval_colptr[newRow+b]+col];
165: #else
166:           nullVals[(newCol-numRows)*numRows+newRow+b] = ((double*)Lstore->nzval)[Lstore->nzval_colptr[newRow+b]+col];
167: #endif
168:       }
169:     }
170:   }
171:   /* Permute rhs to form P^T_c B */
172:   PetscMalloc(numRows*sizeof(PetscReal),&workVals);
173:   for(b = 0; b < numNullCols; b++) {
174:     for(row = 0; row < numRows; row++) workVals[lu->perm_c[row]] = nullVals[b*numRows+row];
175:     for(row = 0; row < numRows; row++) nullVals[b*numRows+row]   = workVals[row];
176:   }
177:   /* Backward solve the upper triangle A x = b */
178:   for(b = 0; b < numNullCols; b++) {
179: #if defined(PETSC_USE_COMPLEX)
180:     sp_ztrsv("L","T","U",&lu->L,&lu->U,&nullVals[b*numRows],&stat,&ierr);
181: #else
182:     sp_dtrsv("L","T","U",&lu->L,&lu->U,&nullVals[b*numRows],&stat,&ierr);
183: #endif
184:     if (ierr < 0)
185:       SETERRQ1(PETSC_ERR_ARG_WRONG,"The argument %D was invalid",-ierr);
186:   }
187:   PetscFree(workVals);

189:   MatAssemblyBegin(*nullMat,MAT_FINAL_ASSEMBLY);
190:   MatAssemblyEnd(*nullMat,MAT_FINAL_ASSEMBLY);
191:   return(0);
192: }
193: #endif

197: PetscErrorCode MatSolve_SuperLU_Private(Mat A,Vec b,Vec x)
198: {
199:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;
200:   PetscScalar    *barray,*xarray;
202:   PetscInt       info,i;
203:   SuperLUStat_t  stat;
204:   PetscReal      ferr,berr;

207:   if ( lu->lwork == -1 ) {
208:     return(0);
209:   }
210:   lu->B.ncol = 1;   /* Set the number of right-hand side */
211:   VecGetArray(b,&barray);
212:   VecGetArray(x,&xarray);

214: #if defined(PETSC_USE_COMPLEX)
215:   ((DNformat*)lu->B.Store)->nzval = (doublecomplex*)barray;
216:   ((DNformat*)lu->X.Store)->nzval = (doublecomplex*)xarray;
217: #else
218:   ((DNformat*)lu->B.Store)->nzval = barray;
219:   ((DNformat*)lu->X.Store)->nzval = xarray;
220: #endif

222:   /* Initialize the statistics variables. */
223:   StatInit(&stat);

225:   lu->options.Fact  = FACTORED; /* Indicate the factored form of A is supplied. */
226: #if defined(PETSC_USE_COMPLEX)
227:   zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
228:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
229:            &lu->mem_usage, &stat, &info);
230: #else
231:   dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
232:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
233:            &lu->mem_usage, &stat, &info);
234: #endif   
235:   VecRestoreArray(b,&barray);
236:   VecRestoreArray(x,&xarray);

238:   if ( !info || info == lu->A.ncol+1 ) {
239:     if ( lu->options.IterRefine ) {
240:       PetscPrintf(PETSC_COMM_SELF,"Iterative Refinement:\n");
241:       PetscPrintf(PETSC_COMM_SELF,"  %8s%8s%16s%16s\n", "rhs", "Steps", "FERR", "BERR");
242:       for (i = 0; i < 1; ++i)
243:         PetscPrintf(PETSC_COMM_SELF,"  %8d%8d%16e%16e\n", i+1, stat.RefineSteps, ferr, berr);
244:     }
245:   } else if ( info > 0 ){
246:     if ( lu->lwork == -1 ) {
247:       PetscPrintf(PETSC_COMM_SELF,"  ** Estimated memory: %D bytes\n", info - lu->A.ncol);
248:     } else {
249:       PetscPrintf(PETSC_COMM_SELF,"  Warning: gssvx() returns info %D\n",info);
250:     }
251:   } else if (info < 0){
252:     SETERRQ2(PETSC_ERR_LIB, "info = %D, the %D-th argument in gssvx() had an illegal value", info,-info);
253:   }

255:   if ( lu->options.PrintStat ) {
256:     PetscPrintf(PETSC_COMM_SELF,"MatSolve__SuperLU():\n");
257:     StatPrint(&stat);
258:   }
259:   StatFree(&stat);
260:   return(0);
261: }

265: PetscErrorCode MatSolve_SuperLU(Mat A,Vec b,Vec x)
266: {
267:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;

271:   lu->options.Trans = TRANS;
272:   MatSolve_SuperLU_Private(A,b,x);
273:   return(0);
274: }

278: PetscErrorCode MatSolveTranspose_SuperLU(Mat A,Vec b,Vec x)
279: {
280:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;

284:   lu->options.Trans = NOTRANS;
285:   MatSolve_SuperLU_Private(A,b,x);
286:   return(0);
287: }

291: PetscErrorCode MatLUFactorNumeric_SuperLU(Mat A,MatFactorInfo *info,Mat *F)
292: {
293:   Mat_SeqAIJ     *aa = (Mat_SeqAIJ*)(A)->data;
294:   Mat_SuperLU    *lu = (Mat_SuperLU*)(*F)->spptr;
296:   PetscInt       sinfo;
297:   SuperLUStat_t  stat;
298:   PetscReal      ferr, berr;
299:   NCformat       *Ustore;
300:   SCformat       *Lstore;
301: 
303:   if (lu->flg == SAME_NONZERO_PATTERN){ /* successing numerical factorization */
304:     lu->options.Fact = SamePattern;
305:     /* Ref: ~SuperLU_3.0/EXAMPLE/dlinsolx2.c */
306:     Destroy_SuperMatrix_Store(&lu->A);
307:     if ( lu->lwork >= 0 ) {
308:       Destroy_SuperNode_Matrix(&lu->L);
309:       Destroy_CompCol_Matrix(&lu->U);
310:       lu->options.Fact = SamePattern;
311:     }
312:   }

314:   /* Create the SuperMatrix for lu->A=A^T:
315:        Since SuperLU likes column-oriented matrices,we pass it the transpose,
316:        and then solve A^T X = B in MatSolve(). */
317: #if defined(PETSC_USE_COMPLEX)
318:   zCreate_CompCol_Matrix(&lu->A,A->cmap.n,A->rmap.n,aa->nz,(doublecomplex*)aa->a,aa->j,aa->i,
319:                            SLU_NC,SLU_Z,SLU_GE);
320: #else
321:   dCreate_CompCol_Matrix(&lu->A,A->cmap.n,A->rmap.n,aa->nz,aa->a,aa->j,aa->i,
322:                            SLU_NC,SLU_D,SLU_GE);
323: #endif
324: 
325:   /* Initialize the statistics variables. */
326:   StatInit(&stat);

328:   /* Numerical factorization */
329:   lu->B.ncol = 0;  /* Indicate not to solve the system */
330: #if defined(PETSC_USE_COMPLEX)
331:    zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
332:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
333:            &lu->mem_usage, &stat, &sinfo);
334: #else
335:   dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
336:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
337:            &lu->mem_usage, &stat, &sinfo);
338: #endif
339:   if ( !sinfo || sinfo == lu->A.ncol+1 ) {
340:     if ( lu->options.PivotGrowth )
341:       PetscPrintf(PETSC_COMM_SELF,"  Recip. pivot growth = %e\n", lu->rpg);
342:     if ( lu->options.ConditionNumber )
343:       PetscPrintf(PETSC_COMM_SELF,"  Recip. condition number = %e\n", lu->rcond);
344:   } else if ( sinfo > 0 ){
345:     if ( lu->lwork == -1 ) {
346:       PetscPrintf(PETSC_COMM_SELF,"  ** Estimated memory: %D bytes\n", sinfo - lu->A.ncol);
347:     } else {
348:       PetscPrintf(PETSC_COMM_SELF,"  Warning: gssvx() returns info %D\n",sinfo);
349:     }
350:   } else { /* sinfo < 0 */
351:     SETERRQ2(PETSC_ERR_LIB, "info = %D, the %D-th argument in gssvx() had an illegal value", sinfo,-sinfo);
352:   }

354:   if ( lu->options.PrintStat ) {
355:     PetscPrintf(PETSC_COMM_SELF,"MatLUFactorNumeric_SuperLU():\n");
356:     StatPrint(&stat);
357:     Lstore = (SCformat *) lu->L.Store;
358:     Ustore = (NCformat *) lu->U.Store;
359:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in factor L = %D\n", Lstore->nnz);
360:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in factor U = %D\n", Ustore->nnz);
361:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in L+U = %D\n", Lstore->nnz + Ustore->nnz - lu->A.ncol);
362:     PetscPrintf(PETSC_COMM_SELF,"  L\\U MB %.3f\ttotal MB needed %.3f\texpansions %D\n",
363:                lu->mem_usage.for_lu/1e6, lu->mem_usage.total_needed/1e6,
364:                lu->mem_usage.expansions);
365:   }
366:   StatFree(&stat);

368:   lu->flg = SAME_NONZERO_PATTERN;
369:   return(0);
370: }

372: /*
373:    Note the r permutation is ignored
374: */
377: PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat A,IS r,IS c,MatFactorInfo *info,Mat *F)
378: {
379:   Mat            B;
380:   Mat_SuperLU    *lu;
382:   PetscInt       m=A->rmap.n,n=A->cmap.n,indx;
383:   PetscTruth     flg;
384:   const char   *colperm[]={"NATURAL","MMD_ATA","MMD_AT_PLUS_A","COLAMD"}; /* MY_PERMC - not supported by the petsc interface yet */
385:   const char   *iterrefine[]={"NOREFINE", "SINGLE", "DOUBLE", "EXTRA"};
386:   const char   *rowperm[]={"NOROWPERM", "LargeDiag"}; /* MY_PERMC - not supported by the petsc interface yet */

389:   MatCreate(A->comm,&B);
390:   MatSetSizes(B,A->rmap.n,A->cmap.n,PETSC_DETERMINE,PETSC_DETERMINE);
391:   MatSetType(B,A->type_name);
392:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);

394:   B->ops->lufactornumeric = MatLUFactorNumeric_SuperLU;
395:   B->ops->solve           = MatSolve_SuperLU;
396:   B->ops->solvetranspose  = MatSolveTranspose_SuperLU;
397:   B->factor               = FACTOR_LU;
398:   B->assembled            = PETSC_TRUE;  /* required by -ksp_view */
399: 
400:   lu = (Mat_SuperLU*)(B->spptr);

402:   /* Set SuperLU options */
403:     /* the default values for options argument:
404:         options.Fact = DOFACT;
405:         options.Equil = YES;
406:             options.ColPerm = COLAMD;
407:         options.DiagPivotThresh = 1.0;
408:             options.Trans = NOTRANS;
409:             options.IterRefine = NOREFINE;
410:             options.SymmetricMode = NO;
411:             options.PivotGrowth = NO;
412:             options.ConditionNumber = NO;
413:             options.PrintStat = YES;
414:     */
415:   set_default_options(&lu->options);
416:   /* equilibration causes error in solve(), thus not supported here. See dgssvx.c for possible reason. */
417:   lu->options.Equil = NO;
418:   lu->options.PrintStat = NO;
419:   lu->lwork = 0;   /* allocate space internally by system malloc */

421:   PetscOptionsBegin(A->comm,A->prefix,"SuperLU Options","Mat");
422:   /* 
423:   PetscOptionsTruth("-mat_superlu_equil","Equil","None",PETSC_FALSE,&flg,0);
424:   if (flg) lu->options.Equil = YES; -- not supported by the interface !!!
425:   */
426:   PetscOptionsEList("-mat_superlu_colperm","ColPerm","None",colperm,4,colperm[3],&indx,&flg);
427:   if (flg) {lu->options.ColPerm = (colperm_t)indx;}
428:   PetscOptionsEList("-mat_superlu_iterrefine","IterRefine","None",iterrefine,4,iterrefine[0],&indx,&flg);
429:   if (flg) { lu->options.IterRefine = (IterRefine_t)indx;}
430:   PetscOptionsTruth("-mat_superlu_symmetricmode","SymmetricMode","None",PETSC_FALSE,&flg,0);
431:   if (flg) lu->options.SymmetricMode = YES;
432:   PetscOptionsReal("-mat_superlu_diagpivotthresh","DiagPivotThresh","None",lu->options.DiagPivotThresh,&lu->options.DiagPivotThresh,PETSC_NULL);
433:   PetscOptionsTruth("-mat_superlu_pivotgrowth","PivotGrowth","None",PETSC_FALSE,&flg,0);
434:   if (flg) lu->options.PivotGrowth = YES;
435:   PetscOptionsTruth("-mat_superlu_conditionnumber","ConditionNumber","None",PETSC_FALSE,&flg,0);
436:   if (flg) lu->options.ConditionNumber = YES;
437:   PetscOptionsEList("-mat_superlu_rowperm","rowperm","None",rowperm,2,rowperm[0],&indx,&flg);
438:   if (flg) {lu->options.RowPerm = (rowperm_t)indx;}
439:   PetscOptionsTruth("-mat_superlu_replacetinypivot","ReplaceTinyPivot","None",PETSC_FALSE,&flg,0);
440:   if (flg) lu->options.ReplaceTinyPivot = YES;
441:   PetscOptionsTruth("-mat_superlu_printstat","PrintStat","None",PETSC_FALSE,&flg,0);
442:   if (flg) lu->options.PrintStat = YES;
443:   PetscOptionsInt("-mat_superlu_lwork","size of work array in bytes used by factorization","None",lu->lwork,&lu->lwork,PETSC_NULL);
444:   if (lu->lwork > 0 ){
445:     PetscMalloc(lu->lwork,&lu->work);
446:   } else if (lu->lwork != 0 && lu->lwork != -1){
447:     PetscPrintf(PETSC_COMM_SELF,"   Warning: lwork %D is not supported by SUPERLU. The default lwork=0 is used.\n",lu->lwork);
448:     lu->lwork = 0;
449:   }
450:   PetscOptionsEnd();

452: #ifdef SUPERLU2
453:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatCreateNull","MatCreateNull_SuperLU",
454:                                     (void(*)(void))MatCreateNull_SuperLU);
455: #endif

457:   /* Allocate spaces (notice sizes are for the transpose) */
458:   PetscMalloc(m*sizeof(PetscInt),&lu->etree);
459:   PetscMalloc(n*sizeof(PetscInt),&lu->perm_r);
460:   PetscMalloc(m*sizeof(PetscInt),&lu->perm_c);
461:   PetscMalloc(n*sizeof(PetscInt),&lu->R);
462:   PetscMalloc(m*sizeof(PetscInt),&lu->C);
463: 
464:   /* create rhs and solution x without allocate space for .Store */
465: #if defined(PETSC_USE_COMPLEX)
466:   zCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
467:   zCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
468: #else
469:   dCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
470:   dCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
471: #endif

473:   lu->flg            = DIFFERENT_NONZERO_PATTERN;
474:   lu->CleanUpSuperLU = PETSC_TRUE;

476:   *F = B;
477:   PetscLogObjectMemory(B,(A->rmap.n+A->cmap.n)*sizeof(PetscInt)+sizeof(Mat_SuperLU));
478:   return(0);
479: }

481: /* used by -ksp_view */
484: PetscErrorCode MatFactorInfo_SuperLU(Mat A,PetscViewer viewer)
485: {
486:   Mat_SuperLU       *lu= (Mat_SuperLU*)A->spptr;
487:   PetscErrorCode    ierr;
488:   superlu_options_t options;

491:   /* check if matrix is superlu_dist type */
492:   if (A->ops->solve != MatSolve_SuperLU) return(0);

494:   options = lu->options;
495:   PetscViewerASCIIPrintf(viewer,"SuperLU run parameters:\n");
496:   PetscViewerASCIIPrintf(viewer,"  Equil: %s\n",(options.Equil != NO) ? "YES": "NO");
497:   PetscViewerASCIIPrintf(viewer,"  ColPerm: %D\n",options.ColPerm);
498:   PetscViewerASCIIPrintf(viewer,"  IterRefine: %D\n",options.IterRefine);
499:   PetscViewerASCIIPrintf(viewer,"  SymmetricMode: %s\n",(options.SymmetricMode != NO) ? "YES": "NO");
500:   PetscViewerASCIIPrintf(viewer,"  DiagPivotThresh: %g\n",options.DiagPivotThresh);
501:   PetscViewerASCIIPrintf(viewer,"  PivotGrowth: %s\n",(options.PivotGrowth != NO) ? "YES": "NO");
502:   PetscViewerASCIIPrintf(viewer,"  ConditionNumber: %s\n",(options.ConditionNumber != NO) ? "YES": "NO");
503:   PetscViewerASCIIPrintf(viewer,"  RowPerm: %D\n",options.RowPerm);
504:   PetscViewerASCIIPrintf(viewer,"  ReplaceTinyPivot: %s\n",(options.ReplaceTinyPivot != NO) ? "YES": "NO");
505:   PetscViewerASCIIPrintf(viewer,"  PrintStat: %s\n",(options.PrintStat != NO) ? "YES": "NO");
506:   PetscViewerASCIIPrintf(viewer,"  lwork: %D\n",lu->lwork);

508:   return(0);
509: }

513: PetscErrorCode MatDuplicate_SuperLU(Mat A, MatDuplicateOption op, Mat *M) {
515:   Mat_SuperLU    *lu=(Mat_SuperLU *)A->spptr;

518:   (*lu->MatDuplicate)(A,op,M);
519:   PetscMemcpy((*M)->spptr,lu,sizeof(Mat_SuperLU));
520:   return(0);
521: }

526: PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SuperLU_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat)
527: {
528:   /* This routine is only called to convert an unfactored PETSc-SuperLU matrix */
529:   /* to its base PETSc type, so we will ignore 'MatType type'. */
531:   Mat            B=*newmat;
532:   Mat_SuperLU    *lu=(Mat_SuperLU *)A->spptr;

535:   if (reuse == MAT_INITIAL_MATRIX) {
536:     MatDuplicate(A,MAT_COPY_VALUES,&B);
537:   }
538:   /* Reset the original function pointers */
539:   B->ops->duplicate        = lu->MatDuplicate;
540:   B->ops->view             = lu->MatView;
541:   B->ops->assemblyend      = lu->MatAssemblyEnd;
542:   B->ops->lufactorsymbolic = lu->MatLUFactorSymbolic;
543:   B->ops->destroy          = lu->MatDestroy;

545:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_superlu_C","",PETSC_NULL);
546:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_superlu_seqaij_C","",PETSC_NULL);

548:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
549:   *newmat = B;
550:   return(0);
551: }

557: PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SuperLU(Mat A,MatType type,MatReuse reuse,Mat *newmat)
558: {
559:   /* This routine is only called to convert to MATSUPERLU */
560:   /* from MATSEQAIJ, so we will ignore 'MatType type'. */
562:   Mat            B=*newmat;
563:   Mat_SuperLU    *lu;

566:   if (reuse == MAT_INITIAL_MATRIX) {
567:     MatDuplicate(A,MAT_COPY_VALUES,&B);
568:   }

570:   PetscNew(Mat_SuperLU,&lu);
571:   lu->MatDuplicate         = A->ops->duplicate;
572:   lu->MatView              = A->ops->view;
573:   lu->MatAssemblyEnd       = A->ops->assemblyend;
574:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
575:   lu->MatDestroy           = A->ops->destroy;
576:   lu->CleanUpSuperLU       = PETSC_FALSE;

578:   B->spptr                 = (void*)lu;
579:   B->ops->duplicate        = MatDuplicate_SuperLU;
580:   B->ops->view             = MatView_SuperLU;
581:   B->ops->assemblyend      = MatAssemblyEnd_SuperLU;
582:   B->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU;
583:   B->ops->choleskyfactorsymbolic = 0;
584:   B->ops->destroy          = MatDestroy_SuperLU;

586:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_superlu_C",
587:                                            "MatConvert_SeqAIJ_SuperLU",MatConvert_SeqAIJ_SuperLU);
588:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_superlu_seqaij_C",
589:                                            "MatConvert_SuperLU_SeqAIJ",MatConvert_SuperLU_SeqAIJ);
590:   PetscInfo(0,"Using SuperLU for SeqAIJ LU factorization and solves.\n");
591:   PetscObjectChangeTypeName((PetscObject)B,MATSUPERLU);
592:   *newmat = B;
593:   return(0);
594: }

597: /*MC
598:   MATSUPERLU - MATSUPERLU = "superlu" - A matrix type providing direct solvers (LU) for sequential matrices 
599:   via the external package SuperLU.

601:   If SuperLU is installed (see the manual for
602:   instructions on how to declare the existence of external packages),
603:   a matrix type can be constructed which invokes SuperLU solvers.
604:   After calling MatCreate(...,A), simply call MatSetType(A,MATSUPERLU).

606:   This matrix inherits from MATSEQAIJ.  As a result, MatSeqAIJSetPreallocation is 
607:   supported for this matrix type.  One can also call MatConvert for an inplace conversion to or from 
608:   the MATSEQAIJ type without data copy.

610:   Options Database Keys:
611: + -mat_type superlu - sets the matrix type to "superlu" during a call to MatSetFromOptions()
612: - -mat_superlu_ordering <0,1,2,3> - 0: natural ordering, 
613:                                     1: MMD applied to A'*A, 
614:                                     2: MMD applied to A'+A, 
615:                                     3: COLAMD, approximate minimum degree column ordering

617:    Level: beginner

619: .seealso: PCLU
620: M*/

625: PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SuperLU(Mat A)
626: {

630:   /* Change type name before calling MatSetType to force proper construction of SeqAIJ and SUPERLU types */
631:   PetscObjectChangeTypeName((PetscObject)A,MATSUPERLU);
632:   MatSetType(A,MATSEQAIJ);
633:   MatConvert_SeqAIJ_SuperLU(A,MATSUPERLU,MAT_REUSE_MATRIX,&A);
634:   return(0);
635: }