Actual source code: superlu.c

  1: #define PETSCMAT_DLL

  3: /*  -------------------------------------------------------------------- 

  5:      This file implements a subclass of the SeqAIJ matrix class that uses
  6:      the SuperLU 3.0 sparse solver. You can use this as a starting point for 
  7:      implementing your own subclass of a PETSc matrix class.

  9:      This demonstrates a way to make an implementation inheritence of a PETSc
 10:      matrix type. This means constructing a new matrix type (SuperLU) by changing some
 11:      of the methods of the previous type (SeqAIJ), adding additional data, and possibly
 12:      additional method. (See any book on object oriented programming).
 13: */

 15: /*
 16:      Defines the data structure for the base matrix type (SeqAIJ)
 17: */
 18:  #include src/mat/impls/aij/seq/aij.h

 20: /*
 21:      SuperLU include files
 22: */
 24: #if defined(PETSC_USE_COMPLEX)
 25: #include "slu_zdefs.h"
 26: #else
 27: #include "slu_ddefs.h"
 28: #endif  
 29: #include "slu_util.h"

 32: /*
 33:      This is the data we are "ADDING" to the SeqAIJ matrix type to get the SuperLU matrix type
 34: */
 35: typedef struct {
 36:   SuperMatrix       A,L,U,B,X;
 37:   superlu_options_t options;
 38:   PetscInt          *perm_c; /* column permutation vector */
 39:   PetscInt          *perm_r; /* row permutations from partial pivoting */
 40:   PetscInt          *etree;
 41:   PetscReal         *R, *C;
 42:   char              equed[1];
 43:   PetscInt          lwork;
 44:   void              *work;
 45:   PetscReal         rpg, rcond;
 46:   mem_usage_t       mem_usage;
 47:   MatStructure      flg;

 49:   /* 
 50:      This is where the methods for the superclass (SeqAIJ) are kept while we 
 51:      reset the pointers in the function table to the new (SuperLU) versions
 52:   */
 53:   PetscErrorCode (*MatDuplicate)(Mat,MatDuplicateOption,Mat*);
 54:   PetscErrorCode (*MatView)(Mat,PetscViewer);
 55:   PetscErrorCode (*MatAssemblyEnd)(Mat,MatAssemblyType);
 56:   PetscErrorCode (*MatLUFactorSymbolic)(Mat,IS,IS,MatFactorInfo*,Mat*);
 57:   PetscErrorCode (*MatDestroy)(Mat);

 59:   /* Flag to clean up (non-global) SuperLU objects during Destroy */
 60:   PetscTruth CleanUpSuperLU;
 61: } Mat_SuperLU;


 73: /*
 74:     Takes a SuperLU matrix (that is a SeqAIJ matrix with the additional SuperLU data-structures
 75:    and methods) and converts it back to a regular SeqAIJ matrix.
 76: */
 80: PetscErrorCode  MatConvert_SuperLU_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat)
 81: {
 83:   Mat            B=*newmat;
 84:   Mat_SuperLU    *lu=(Mat_SuperLU *)A->spptr;

 87:   if (reuse == MAT_INITIAL_MATRIX) {
 88:     MatDuplicate(A,MAT_COPY_VALUES,&B);
 89:   }
 90:   /* Reset the original SeqAIJ function pointers */
 91:   B->ops->duplicate        = lu->MatDuplicate;
 92:   B->ops->view             = lu->MatView;
 93:   B->ops->assemblyend      = lu->MatAssemblyEnd;
 94:   B->ops->lufactorsymbolic = lu->MatLUFactorSymbolic;
 95:   B->ops->destroy          = lu->MatDestroy;
 96:   PetscFree(lu);
 97:   A->spptr = PETSC_NULL;

 99:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_superlu_C","",PETSC_NULL);
100:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_superlu_seqaij_C","",PETSC_NULL);

102:   /* change the type name back to its original value */
103:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
104:   *newmat = B;
105:   return(0);
106: }

112: PetscErrorCode  MatConvert_SeqAIJ_SuperLU(Mat A,MatType type,MatReuse reuse,Mat *newmat)
113: {
115:   Mat            B=*newmat;
116:   Mat_SuperLU    *lu;

119:   if (reuse == MAT_INITIAL_MATRIX){
120:     MatDuplicate(A,MAT_COPY_VALUES,&B);
121:   }

123:   PetscNew(Mat_SuperLU,&lu);
124:   /* save the original SeqAIJ methods that we are changing */
125:   lu->MatDuplicate         = A->ops->duplicate;
126:   lu->MatView              = A->ops->view;
127:   lu->MatAssemblyEnd       = A->ops->assemblyend;
128:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
129:   lu->MatDestroy           = A->ops->destroy;
130:   lu->CleanUpSuperLU       = PETSC_FALSE;

132:   /* add to the matrix the location for all the SuperLU data is to be stored */
133:   B->spptr                 = (void*)lu; /* attach Mat_SuperLU to B->spptr is a bad design! */

135:   /* set the methods in the function table to the SuperLU versions */
136:   B->ops->duplicate        = MatDuplicate_SuperLU;
137:   B->ops->view             = MatView_SuperLU;
138:   B->ops->assemblyend      = MatAssemblyEnd_SuperLU;
139:   B->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU;
140:   B->ops->choleskyfactorsymbolic = 0;
141:   B->ops->destroy          = MatDestroy_SuperLU;

143:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_superlu_C",
144:                                            "MatConvert_SeqAIJ_SuperLU",MatConvert_SeqAIJ_SuperLU);
145:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_superlu_seqaij_C",
146:                                            "MatConvert_SuperLU_SeqAIJ",MatConvert_SuperLU_SeqAIJ);
147:   PetscInfo(0,"Using SuperLU for SeqAIJ LU factorization and solves.\n");
148:   PetscObjectChangeTypeName((PetscObject)B,MATSUPERLU);
149:   *newmat = B;
150:   return(0);
151: }

154: /*
155:     Utility function
156: */
159: PetscErrorCode MatFactorInfo_SuperLU(Mat A,PetscViewer viewer)
160: {
161:   Mat_SuperLU       *lu= (Mat_SuperLU*)A->spptr;
162:   PetscErrorCode    ierr;
163:   superlu_options_t options;

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

169:   options = lu->options;
170:   PetscViewerASCIIPrintf(viewer,"SuperLU run parameters:\n");
171:   PetscViewerASCIIPrintf(viewer,"  Equil: %s\n",(options.Equil != NO) ? "YES": "NO");
172:   PetscViewerASCIIPrintf(viewer,"  ColPerm: %D\n",options.ColPerm);
173:   PetscViewerASCIIPrintf(viewer,"  IterRefine: %D\n",options.IterRefine);
174:   PetscViewerASCIIPrintf(viewer,"  SymmetricMode: %s\n",(options.SymmetricMode != NO) ? "YES": "NO");
175:   PetscViewerASCIIPrintf(viewer,"  DiagPivotThresh: %g\n",options.DiagPivotThresh);
176:   PetscViewerASCIIPrintf(viewer,"  PivotGrowth: %s\n",(options.PivotGrowth != NO) ? "YES": "NO");
177:   PetscViewerASCIIPrintf(viewer,"  ConditionNumber: %s\n",(options.ConditionNumber != NO) ? "YES": "NO");
178:   PetscViewerASCIIPrintf(viewer,"  RowPerm: %D\n",options.RowPerm);
179:   PetscViewerASCIIPrintf(viewer,"  ReplaceTinyPivot: %s\n",(options.ReplaceTinyPivot != NO) ? "YES": "NO");
180:   PetscViewerASCIIPrintf(viewer,"  PrintStat: %s\n",(options.PrintStat != NO) ? "YES": "NO");
181:   PetscViewerASCIIPrintf(viewer,"  lwork: %D\n",lu->lwork);

183:   return(0);
184: }

186: /*
187:     These are the methods provided to REPLACE the corresponding methods of the 
188:    SeqAIJ matrix class. Other methods of SeqAIJ are not replaced
189: */
192: PetscErrorCode MatLUFactorNumeric_SuperLU(Mat A,MatFactorInfo *info,Mat *F)
193: {
194:   Mat_SeqAIJ     *aa = (Mat_SeqAIJ*)(A)->data;
195:   Mat_SuperLU    *lu = (Mat_SuperLU*)(*F)->spptr;
197:   PetscInt       sinfo;
198:   SuperLUStat_t  stat;
199:   PetscReal      ferr, berr;
200:   NCformat       *Ustore;
201:   SCformat       *Lstore;
202: 
204:   if (lu->flg == SAME_NONZERO_PATTERN){ /* successing numerical factorization */
205:     lu->options.Fact = SamePattern;
206:     /* Ref: ~SuperLU_3.0/EXAMPLE/dlinsolx2.c */
207:     Destroy_SuperMatrix_Store(&lu->A);
208:     if ( lu->lwork >= 0 ) {
209:       Destroy_SuperNode_Matrix(&lu->L);
210:       Destroy_CompCol_Matrix(&lu->U);
211:       lu->options.Fact = SamePattern;
212:     }
213:   }

215:   /* Create the SuperMatrix for lu->A=A^T:
216:        Since SuperLU likes column-oriented matrices,we pass it the transpose,
217:        and then solve A^T X = B in MatSolve(). */
218: #if defined(PETSC_USE_COMPLEX)
219:   zCreate_CompCol_Matrix(&lu->A,A->cmap.n,A->rmap.n,aa->nz,(doublecomplex*)aa->a,aa->j,aa->i,
220:                            SLU_NC,SLU_Z,SLU_GE);
221: #else
222:   dCreate_CompCol_Matrix(&lu->A,A->cmap.n,A->rmap.n,aa->nz,aa->a,aa->j,aa->i,
223:                            SLU_NC,SLU_D,SLU_GE);
224: #endif
225: 
226:   /* Initialize the statistics variables. */
227:   StatInit(&stat);

229:   /* Numerical factorization */
230:   lu->B.ncol = 0;  /* Indicate not to solve the system */
231: #if defined(PETSC_USE_COMPLEX)
232:    zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
233:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
234:            &lu->mem_usage, &stat, &sinfo);
235: #else
236:   dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
237:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
238:            &lu->mem_usage, &stat, &sinfo);
239: #endif
240:   if ( !sinfo || sinfo == lu->A.ncol+1 ) {
241:     if ( lu->options.PivotGrowth )
242:       PetscPrintf(PETSC_COMM_SELF,"  Recip. pivot growth = %e\n", lu->rpg);
243:     if ( lu->options.ConditionNumber )
244:       PetscPrintf(PETSC_COMM_SELF,"  Recip. condition number = %e\n", lu->rcond);
245:   } else if ( sinfo > 0 ){
246:     if ( lu->lwork == -1 ) {
247:       PetscPrintf(PETSC_COMM_SELF,"  ** Estimated memory: %D bytes\n", sinfo - lu->A.ncol);
248:     } else {
249:       PetscPrintf(PETSC_COMM_SELF,"  Warning: gssvx() returns info %D\n",sinfo);
250:     }
251:   } else { /* sinfo < 0 */
252:     SETERRQ2(PETSC_ERR_LIB, "info = %D, the %D-th argument in gssvx() had an illegal value", sinfo,-sinfo);
253:   }

255:   if ( lu->options.PrintStat ) {
256:     PetscPrintf(PETSC_COMM_SELF,"MatLUFactorNumeric_SuperLU():\n");
257:     StatPrint(&stat);
258:     Lstore = (SCformat *) lu->L.Store;
259:     Ustore = (NCformat *) lu->U.Store;
260:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in factor L = %D\n", Lstore->nnz);
261:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in factor U = %D\n", Ustore->nnz);
262:     PetscPrintf(PETSC_COMM_SELF,"  No of nonzeros in L+U = %D\n", Lstore->nnz + Ustore->nnz - lu->A.ncol);
263:     PetscPrintf(PETSC_COMM_SELF,"  L\\U MB %.3f\ttotal MB needed %.3f\texpansions %D\n",
264:                lu->mem_usage.for_lu/1e6, lu->mem_usage.total_needed/1e6,
265:                lu->mem_usage.expansions);
266:   }
267:   StatFree(&stat);

269:   lu->flg = SAME_NONZERO_PATTERN;
270:   return(0);
271: }

275: PetscErrorCode MatDestroy_SuperLU(Mat A)
276: {
278:   Mat_SuperLU    *lu=(Mat_SuperLU*)A->spptr;

281:   if (lu->CleanUpSuperLU) { /* Free the SuperLU datastructures */
282:     Destroy_SuperMatrix_Store(&lu->A);
283:     Destroy_SuperMatrix_Store(&lu->B);
284:     Destroy_SuperMatrix_Store(&lu->X);

286:     PetscFree(lu->etree);
287:     PetscFree(lu->perm_r);
288:     PetscFree(lu->perm_c);
289:     PetscFree(lu->R);
290:     PetscFree(lu->C);
291:     if ( lu->lwork >= 0 ) {
292:       Destroy_SuperNode_Matrix(&lu->L);
293:       Destroy_CompCol_Matrix(&lu->U);
294:     }
295:   }
296:   MatConvert_SuperLU_SeqAIJ(A,MATSEQAIJ,MAT_REUSE_MATRIX,&A);
297:   (*A->ops->destroy)(A);
298:   return(0);
299: }

303: PetscErrorCode MatView_SuperLU(Mat A,PetscViewer viewer)
304: {
305:   PetscErrorCode    ierr;
306:   PetscTruth        iascii;
307:   PetscViewerFormat format;
308:   Mat_SuperLU       *lu=(Mat_SuperLU*)(A->spptr);

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

313:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
314:   if (iascii) {
315:     PetscViewerGetFormat(viewer,&format);
316:     if (format == PETSC_VIEWER_ASCII_INFO) {
317:       MatFactorInfo_SuperLU(A,viewer);
318:     }
319:   }
320:   return(0);
321: }

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

330:   (*lu->MatAssemblyEnd)(A,mode);
331:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
332:   A->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU;
333:   return(0);
334: }


339: PetscErrorCode MatSolve_SuperLU_Private(Mat A,Vec b,Vec x)
340: {
341:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;
342:   PetscScalar    *barray,*xarray;
344:   PetscInt       info,i;
345:   SuperLUStat_t  stat;
346:   PetscReal      ferr,berr;

349:   if ( lu->lwork == -1 ) {
350:     return(0);
351:   }
352:   lu->B.ncol = 1;   /* Set the number of right-hand side */
353:   VecGetArray(b,&barray);
354:   VecGetArray(x,&xarray);

356: #if defined(PETSC_USE_COMPLEX)
357:   ((DNformat*)lu->B.Store)->nzval = (doublecomplex*)barray;
358:   ((DNformat*)lu->X.Store)->nzval = (doublecomplex*)xarray;
359: #else
360:   ((DNformat*)lu->B.Store)->nzval = barray;
361:   ((DNformat*)lu->X.Store)->nzval = xarray;
362: #endif

364:   /* Initialize the statistics variables. */
365:   StatInit(&stat);

367:   lu->options.Fact  = FACTORED; /* Indicate the factored form of A is supplied. */
368: #if defined(PETSC_USE_COMPLEX)
369:   zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
370:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
371:            &lu->mem_usage, &stat, &info);
372: #else
373:   dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
374:            &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
375:            &lu->mem_usage, &stat, &info);
376: #endif   
377:   VecRestoreArray(b,&barray);
378:   VecRestoreArray(x,&xarray);

380:   if ( !info || info == lu->A.ncol+1 ) {
381:     if ( lu->options.IterRefine ) {
382:       PetscPrintf(PETSC_COMM_SELF,"Iterative Refinement:\n");
383:       PetscPrintf(PETSC_COMM_SELF,"  %8s%8s%16s%16s\n", "rhs", "Steps", "FERR", "BERR");
384:       for (i = 0; i < 1; ++i)
385:         PetscPrintf(PETSC_COMM_SELF,"  %8d%8d%16e%16e\n", i+1, stat.RefineSteps, ferr, berr);
386:     }
387:   } else if ( info > 0 ){
388:     if ( lu->lwork == -1 ) {
389:       PetscPrintf(PETSC_COMM_SELF,"  ** Estimated memory: %D bytes\n", info - lu->A.ncol);
390:     } else {
391:       PetscPrintf(PETSC_COMM_SELF,"  Warning: gssvx() returns info %D\n",info);
392:     }
393:   } else if (info < 0){
394:     SETERRQ2(PETSC_ERR_LIB, "info = %D, the %D-th argument in gssvx() had an illegal value", info,-info);
395:   }

397:   if ( lu->options.PrintStat ) {
398:     PetscPrintf(PETSC_COMM_SELF,"MatSolve__SuperLU():\n");
399:     StatPrint(&stat);
400:   }
401:   StatFree(&stat);
402:   return(0);
403: }

407: PetscErrorCode MatSolve_SuperLU(Mat A,Vec b,Vec x)
408: {
409:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;

413:   lu->options.Trans = TRANS;
414:   MatSolve_SuperLU_Private(A,b,x);
415:   return(0);
416: }

420: PetscErrorCode MatSolveTranspose_SuperLU(Mat A,Vec b,Vec x)
421: {
422:   Mat_SuperLU    *lu = (Mat_SuperLU*)A->spptr;

426:   lu->options.Trans = NOTRANS;
427:   MatSolve_SuperLU_Private(A,b,x);
428:   return(0);
429: }


432: /*
433:    Note the r permutation is ignored
434: */
437: PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat A,IS r,IS c,MatFactorInfo *info,Mat *F)
438: {
439:   Mat            B;
440:   Mat_SuperLU    *lu;
442:   PetscInt       m=A->rmap.n,n=A->cmap.n,indx;
443:   PetscTruth     flg;
444:   const char   *colperm[]={"NATURAL","MMD_ATA","MMD_AT_PLUS_A","COLAMD"}; /* MY_PERMC - not supported by the petsc interface yet */
445:   const char   *iterrefine[]={"NOREFINE", "SINGLE", "DOUBLE", "EXTRA"};
446:   const char   *rowperm[]={"NOROWPERM", "LargeDiag"}; /* MY_PERMC - not supported by the petsc interface yet */

449:   MatCreate(A->comm,&B);
450:   MatSetSizes(B,A->rmap.n,A->cmap.n,PETSC_DETERMINE,PETSC_DETERMINE);
451:   MatSetType(B,A->type_name);
452:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);

454:   B->ops->lufactornumeric = MatLUFactorNumeric_SuperLU;
455:   B->ops->solve           = MatSolve_SuperLU;
456:   B->ops->solvetranspose  = MatSolveTranspose_SuperLU;
457:   B->factor               = FACTOR_LU;
458:   B->assembled            = PETSC_TRUE;  /* required by -ksp_view */
459: 
460:   lu = (Mat_SuperLU*)(B->spptr);

462:   /* Set SuperLU options */
463:     /* the default values for options argument:
464:         options.Fact = DOFACT;
465:         options.Equil = YES;
466:             options.ColPerm = COLAMD;
467:         options.DiagPivotThresh = 1.0;
468:             options.Trans = NOTRANS;
469:             options.IterRefine = NOREFINE;
470:             options.SymmetricMode = NO;
471:             options.PivotGrowth = NO;
472:             options.ConditionNumber = NO;
473:             options.PrintStat = YES;
474:     */
475:   set_default_options(&lu->options);
476:   /* equilibration causes error in solve(), thus not supported here. See dgssvx.c for possible reason. */
477:   lu->options.Equil = NO;
478:   lu->options.PrintStat = NO;
479:   lu->lwork = 0;   /* allocate space internally by system malloc */

481:   PetscOptionsBegin(A->comm,A->prefix,"SuperLU Options","Mat");
482:   PetscOptionsEList("-mat_superlu_colperm","ColPerm","None",colperm,4,colperm[3],&indx,&flg);
483:   if (flg) {lu->options.ColPerm = (colperm_t)indx;}
484:   PetscOptionsEList("-mat_superlu_iterrefine","IterRefine","None",iterrefine,4,iterrefine[0],&indx,&flg);
485:   if (flg) { lu->options.IterRefine = (IterRefine_t)indx;}
486:   PetscOptionsTruth("-mat_superlu_symmetricmode","SymmetricMode","None",PETSC_FALSE,&flg,0);
487:   if (flg) lu->options.SymmetricMode = YES;
488:   PetscOptionsReal("-mat_superlu_diagpivotthresh","DiagPivotThresh","None",lu->options.DiagPivotThresh,&lu->options.DiagPivotThresh,PETSC_NULL);
489:   PetscOptionsTruth("-mat_superlu_pivotgrowth","PivotGrowth","None",PETSC_FALSE,&flg,0);
490:   if (flg) lu->options.PivotGrowth = YES;
491:   PetscOptionsTruth("-mat_superlu_conditionnumber","ConditionNumber","None",PETSC_FALSE,&flg,0);
492:   if (flg) lu->options.ConditionNumber = YES;
493:   PetscOptionsEList("-mat_superlu_rowperm","rowperm","None",rowperm,2,rowperm[0],&indx,&flg);
494:   if (flg) {lu->options.RowPerm = (rowperm_t)indx;}
495:   PetscOptionsTruth("-mat_superlu_replacetinypivot","ReplaceTinyPivot","None",PETSC_FALSE,&flg,0);
496:   if (flg) lu->options.ReplaceTinyPivot = YES;
497:   PetscOptionsTruth("-mat_superlu_printstat","PrintStat","None",PETSC_FALSE,&flg,0);
498:   if (flg) lu->options.PrintStat = YES;
499:   PetscOptionsInt("-mat_superlu_lwork","size of work array in bytes used by factorization","None",lu->lwork,&lu->lwork,PETSC_NULL);
500:   if (lu->lwork > 0 ){
501:     PetscMalloc(lu->lwork,&lu->work);
502:   } else if (lu->lwork != 0 && lu->lwork != -1){
503:     PetscPrintf(PETSC_COMM_SELF,"   Warning: lwork %D is not supported by SUPERLU. The default lwork=0 is used.\n",lu->lwork);
504:     lu->lwork = 0;
505:   }
506:   PetscOptionsEnd();

508: #ifdef SUPERLU2
509:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatCreateNull","MatCreateNull_SuperLU",
510:                                     (void(*)(void))MatCreateNull_SuperLU);
511: #endif

513:   /* Allocate spaces (notice sizes are for the transpose) */
514:   PetscMalloc(m*sizeof(PetscInt),&lu->etree);
515:   PetscMalloc(n*sizeof(PetscInt),&lu->perm_r);
516:   PetscMalloc(m*sizeof(PetscInt),&lu->perm_c);
517:   PetscMalloc(n*sizeof(PetscInt),&lu->R);
518:   PetscMalloc(m*sizeof(PetscInt),&lu->C);
519: 
520:   /* create rhs and solution x without allocate space for .Store */
521: #if defined(PETSC_USE_COMPLEX)
522:   zCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
523:   zCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
524: #else
525:   dCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
526:   dCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
527: #endif

529:   lu->flg            = DIFFERENT_NONZERO_PATTERN;
530:   lu->CleanUpSuperLU = PETSC_TRUE;

532:   *F = B;
533:   PetscLogObjectMemory(B,(A->rmap.n+A->cmap.n)*sizeof(PetscInt)+sizeof(Mat_SuperLU));
534:   return(0);
535: }


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

545:   (*lu->MatDuplicate)(A,op,M);
546:   PetscMemcpy((*M)->spptr,lu,sizeof(Mat_SuperLU));
547:   return(0);
548: }


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

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

560:   This matrix inherits from MATSEQAIJ.  As a result, MatSeqAIJSetPreallocation() is 
561:   supported for this matrix type.  One can also call MatConvert for an inplace conversion to or from 
562:   the MATSEQAIJ type without data copy.

564:   Options Database Keys:
565: + -mat_type superlu - sets the matrix type to "superlu" during a call to MatSetFromOptions()
566: . -mat_superlu_ordering <0,1,2,3> - 0: natural ordering, 
567:                                     1: MMD applied to A'*A, 
568:                                     2: MMD applied to A'+A, 
569:                                     3: COLAMD, approximate minimum degree column ordering
570: . -mat_superlu_iterrefine - have SuperLU do iterative refinement after the triangular solve
571:                           choices: NOREFINE, SINGLE, DOUBLE, EXTRA; default is NOREFINE
572: - -mat_superlu_printstat - print SuperLU statistics about the factorization

574:    Level: beginner

576: .seealso: PCLU
577: M*/

579: /*
580:     Constructor for the new derived matrix class. It simply creates the base 
581:    matrix class and then adds the additional information/methods needed by SuperLU.
582: */
586: PetscErrorCode  MatCreate_SuperLU(Mat A)
587: {

591:   MatSetType(A,MATSEQAIJ);
592:   MatConvert_SeqAIJ_SuperLU(A,MATSUPERLU,MAT_REUSE_MATRIX,&A);
593:   return(0);
594: }