Actual source code: superlu_dist.c

  1: #define PETSCMAT_DLL

  3: /* 
  4:         Provides an interface to the SuperLU_DIST_2.0 sparse solver
  5: */

  7: #include "src/mat/impls/aij/seq/aij.h"
  8: #include "src/mat/impls/aij/mpi/mpiaij.h"
  9: #if defined(PETSC_HAVE_STDLIB_H) /* This is to get arround weird problem with SuperLU on cray */
 10: #include "stdlib.h"
 11: #endif

 14: #if defined(PETSC_USE_COMPLEX)
 15: #include "superlu_zdefs.h"
 16: #else
 17: #include "superlu_ddefs.h"
 18: #endif

 21: typedef enum { GLOBAL,DISTRIBUTED
 22: } SuperLU_MatInputMode;

 24: typedef struct {
 25:   int_t                   nprow,npcol,*row,*col;
 26:   gridinfo_t              grid;
 27:   superlu_options_t       options;
 28:   SuperMatrix             A_sup;
 29:   ScalePermstruct_t       ScalePermstruct;
 30:   LUstruct_t              LUstruct;
 31:   int                     StatPrint;
 32:   int                     MatInputMode;
 33:   SOLVEstruct_t           SOLVEstruct;
 34:   MatStructure            flg;
 35:   MPI_Comm                comm_superlu;
 36: #if defined(PETSC_USE_COMPLEX)
 37:   doublecomplex           *val;
 38: #else
 39:   double                  *val;
 40: #endif

 42:   /* A few function pointers for inheritance */
 43:   PetscErrorCode (*MatDuplicate)(Mat,MatDuplicateOption,Mat*);
 44:   PetscErrorCode (*MatView)(Mat,PetscViewer);
 45:   PetscErrorCode (*MatAssemblyEnd)(Mat,MatAssemblyType);
 46:   PetscErrorCode (*MatLUFactorSymbolic)(Mat,IS,IS,MatFactorInfo*,Mat*);
 47:   PetscErrorCode (*MatDestroy)(Mat);

 49:   /* Flag to clean up (non-global) SuperLU objects during Destroy */
 50:   PetscTruth CleanUpSuperLU_Dist;
 51: } Mat_SuperLU_DIST;

 53: EXTERN PetscErrorCode MatDuplicate_SuperLU_DIST(Mat,MatDuplicateOption,Mat*);

 58: PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SuperLU_DIST_Base(Mat A,MatType type,MatReuse reuse,Mat *newmat)
 59: {
 60:   PetscErrorCode   ierr;
 61:   Mat              B=*newmat;
 62:   Mat_SuperLU_DIST *lu=(Mat_SuperLU_DIST *)A->spptr;

 65:   if (reuse == MAT_INITIAL_MATRIX) {
 66:     MatDuplicate(A,MAT_COPY_VALUES,&B);
 67:   }
 68:   /* Reset the original function pointers */
 69:   B->ops->duplicate        = lu->MatDuplicate;
 70:   B->ops->view             = lu->MatView;
 71:   B->ops->assemblyend      = lu->MatAssemblyEnd;
 72:   B->ops->lufactorsymbolic = lu->MatLUFactorSymbolic;
 73:   B->ops->destroy          = lu->MatDestroy;

 75:   PetscFree(lu);

 77:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_superlu_dist_C","",PETSC_NULL);
 78:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_superlu_dist_seqaij_C","",PETSC_NULL);
 79:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_superlu_dist_C","",PETSC_NULL);
 80:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_superlu_dist_mpiaij_C","",PETSC_NULL);

 82:   PetscObjectChangeTypeName((PetscObject)B,type);
 83:   *newmat = B;
 84:   return(0);
 85: }

 90: PetscErrorCode MatDestroy_SuperLU_DIST(Mat A)
 91: {
 92:   PetscErrorCode   ierr;
 93:   PetscMPIInt      size;
 94:   Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)A->spptr;
 95: 
 97:   if (lu->CleanUpSuperLU_Dist) {
 98:     /* Deallocate SuperLU_DIST storage */
 99:     if (lu->MatInputMode == GLOBAL) {
100:       Destroy_CompCol_Matrix_dist(&lu->A_sup);
101:     } else {
102:       Destroy_CompRowLoc_Matrix_dist(&lu->A_sup);
103:       if ( lu->options.SolveInitialized ) {
104: #if defined(PETSC_USE_COMPLEX)
105:         zSolveFinalize(&lu->options, &lu->SOLVEstruct);
106: #else
107:         dSolveFinalize(&lu->options, &lu->SOLVEstruct);
108: #endif
109:       }
110:     }
111:     Destroy_LU(A->cmap.N, &lu->grid, &lu->LUstruct);
112:     ScalePermstructFree(&lu->ScalePermstruct);
113:     LUstructFree(&lu->LUstruct);

115:     /* Release the SuperLU_DIST process grid. */
116:     superlu_gridexit(&lu->grid);
117: 
118:     MPI_Comm_free(&(lu->comm_superlu));
119:   }

121:   MPI_Comm_size(A->comm,&size);
122:   if (size == 1) {
123:     MatConvert_SuperLU_DIST_Base(A,MATSEQAIJ,MAT_REUSE_MATRIX,&A);
124:   } else {
125:     MatConvert_SuperLU_DIST_Base(A,MATMPIAIJ,MAT_REUSE_MATRIX,&A);
126:   }
127:   (*A->ops->destroy)(A);
128:   return(0);
129: }

133: PetscErrorCode MatSolve_SuperLU_DIST(Mat A,Vec b_mpi,Vec x)
134: {
135:   Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)A->spptr;
136:   PetscErrorCode   ierr;
137:   PetscMPIInt      size;
138:   PetscInt         m=A->rmap.N, N=A->cmap.N;
139:   SuperLUStat_t    stat;
140:   double           berr[1];
141:   PetscScalar      *bptr;
142:   PetscInt         info, nrhs=1;
143:   Vec              x_seq;
144:   IS               iden;
145:   VecScatter       scat;
146: 
148:   MPI_Comm_size(A->comm,&size);
149:   if (size > 1) {
150:     if (lu->MatInputMode == GLOBAL) { /* global mat input, convert b to x_seq */
151:       VecCreateSeq(PETSC_COMM_SELF,N,&x_seq);
152:       ISCreateStride(PETSC_COMM_SELF,N,0,1,&iden);
153:       VecScatterCreate(b_mpi,iden,x_seq,iden,&scat);
154:       ISDestroy(iden);

156:       VecScatterBegin(b_mpi,x_seq,INSERT_VALUES,SCATTER_FORWARD,scat);
157:       VecScatterEnd(b_mpi,x_seq,INSERT_VALUES,SCATTER_FORWARD,scat);
158:       VecGetArray(x_seq,&bptr);
159:     } else { /* distributed mat input */
160:       VecCopy(b_mpi,x);
161:       VecGetArray(x,&bptr);
162:     }
163:   } else { /* size == 1 */
164:     VecCopy(b_mpi,x);
165:     VecGetArray(x,&bptr);
166:   }
167: 
168:   lu->options.Fact = FACTORED; /* The factored form of A is supplied. Local option used by this func. only.*/

170:   PStatInit(&stat);        /* Initialize the statistics variables. */
171:   if (lu->MatInputMode == GLOBAL) {
172: #if defined(PETSC_USE_COMPLEX)
173:     pzgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct,(doublecomplex*)bptr, m, nrhs,
174:                    &lu->grid, &lu->LUstruct, berr, &stat, &info);
175: #else
176:     pdgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct,bptr, m, nrhs,
177:                    &lu->grid, &lu->LUstruct, berr, &stat, &info);
178: #endif 
179:   } else { /* distributed mat input */
180: #if defined(PETSC_USE_COMPLEX)
181:     pzgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, (doublecomplex*)bptr, A->rmap.N, nrhs, &lu->grid,
182:             &lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &info);
183:     if (info) SETERRQ1(PETSC_ERR_LIB,"pzgssvx fails, info: %d\n",info);
184: #else
185:     pdgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, bptr, A->rmap.N, nrhs, &lu->grid,
186:             &lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &info);
187:     if (info) SETERRQ1(PETSC_ERR_LIB,"pdgssvx fails, info: %d\n",info);
188: #endif
189:   }
190:   if (lu->options.PrintStat) {
191:      PStatPrint(&lu->options, &stat, &lu->grid);     /* Print the statistics. */
192:   }
193:   PStatFree(&stat);
194: 
195:   if (size > 1) {
196:     if (lu->MatInputMode == GLOBAL){ /* convert seq x to mpi x */
197:       VecRestoreArray(x_seq,&bptr);
198:       VecScatterBegin(x_seq,x,INSERT_VALUES,SCATTER_REVERSE,scat);
199:       VecScatterEnd(x_seq,x,INSERT_VALUES,SCATTER_REVERSE,scat);
200:       VecScatterDestroy(scat);
201:       VecDestroy(x_seq);
202:     } else {
203:       VecRestoreArray(x,&bptr);
204:     }
205:   } else {
206:     VecRestoreArray(x,&bptr);
207:   }
208:   return(0);
209: }

213: PetscErrorCode MatLUFactorNumeric_SuperLU_DIST(Mat A,MatFactorInfo *info,Mat *F)
214: {
215:   Mat              *tseq,A_seq = PETSC_NULL;
216:   Mat_SeqAIJ       *aa,*bb;
217:   Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)(*F)->spptr;
218:   PetscErrorCode   ierr;
219:   PetscInt         M=A->rmap.N,N=A->cmap.N,sinfo,i,*ai,*aj,*bi,*bj,nz,rstart,*garray,
220:                    m=A->rmap.n, irow,colA_start,j,jcol,jB,countA,countB,*bjj,*ajj;
221:   PetscMPIInt      size,rank;
222:   SuperLUStat_t    stat;
223:   double           *berr=0;
224:   IS               isrow;
225:   PetscLogDouble   time0,time,time_min,time_max;
226:   Mat              F_diag=PETSC_NULL;
227: #if defined(PETSC_USE_COMPLEX)
228:   doublecomplex    *av, *bv;
229: #else
230:   double           *av, *bv;
231: #endif

234:   MPI_Comm_size(A->comm,&size);
235:   MPI_Comm_rank(A->comm,&rank);
236: 
237:   if (lu->options.PrintStat) { /* collect time for mat conversion */
238:     MPI_Barrier(A->comm);
239:     PetscGetTime(&time0);
240:   }

242:   if (lu->MatInputMode == GLOBAL) { /* global mat input */
243:     if (size > 1) { /* convert mpi A to seq mat A */
244:       ISCreateStride(PETSC_COMM_SELF,M,0,1,&isrow);
245:       MatGetSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&tseq);
246:       ISDestroy(isrow);
247: 
248:       A_seq = *tseq;
249:       PetscFree(tseq);
250:       aa =  (Mat_SeqAIJ*)A_seq->data;
251:     } else {
252:       aa =  (Mat_SeqAIJ*)A->data;
253:     }

255:     /* Convert Petsc NR matrix to SuperLU_DIST NC. 
256:        Note: memories of lu->val, col and row are allocated by CompRow_to_CompCol_dist()! */
257:     if (lu->flg == SAME_NONZERO_PATTERN) {/* successive numeric factorization, sparsity pattern is reused. */
258:       Destroy_CompCol_Matrix_dist(&lu->A_sup);
259:       Destroy_LU(N, &lu->grid, &lu->LUstruct);
260:       lu->options.Fact = SamePattern;
261:     }
262: #if defined(PETSC_USE_COMPLEX)
263:     zCompRow_to_CompCol_dist(M,N,aa->nz,(doublecomplex*)aa->a,aa->j,aa->i,&lu->val,&lu->col, &lu->row);
264: #else
265:     dCompRow_to_CompCol_dist(M,N,aa->nz,aa->a,aa->j,aa->i,&lu->val, &lu->col, &lu->row);
266: #endif

268:     /* Create compressed column matrix A_sup. */
269: #if defined(PETSC_USE_COMPLEX)
270:     zCreate_CompCol_Matrix_dist(&lu->A_sup, M, N, aa->nz, lu->val, lu->col, lu->row, SLU_NC, SLU_Z, SLU_GE);
271: #else
272:     dCreate_CompCol_Matrix_dist(&lu->A_sup, M, N, aa->nz, lu->val, lu->col, lu->row, SLU_NC, SLU_D, SLU_GE);
273: #endif
274:   } else { /* distributed mat input */
275:     Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data;
276:     aa=(Mat_SeqAIJ*)(mat->A)->data;
277:     bb=(Mat_SeqAIJ*)(mat->B)->data;
278:     ai=aa->i; aj=aa->j;
279:     bi=bb->i; bj=bb->j;
280: #if defined(PETSC_USE_COMPLEX)
281:     av=(doublecomplex*)aa->a;
282:     bv=(doublecomplex*)bb->a;
283: #else
284:     av=aa->a;
285:     bv=bb->a;
286: #endif
287:     rstart = A->rmap.rstart;
288:     nz     = aa->nz + bb->nz;
289:     garray = mat->garray;

291:     if (lu->flg == DIFFERENT_NONZERO_PATTERN) {/* first numeric factorization */
292: #if defined(PETSC_USE_COMPLEX)
293:       zallocateA_dist(m, nz, &lu->val, &lu->col, &lu->row);
294: #else
295:       dallocateA_dist(m, nz, &lu->val, &lu->col, &lu->row);
296: #endif
297:     } else { /* successive numeric factorization, sparsity pattern and perm_c are reused. */
298:       /* Destroy_CompRowLoc_Matrix_dist(&lu->A_sup);  */ /* crash! */
299:       Destroy_LU(N, &lu->grid, &lu->LUstruct);
300:       lu->options.Fact = SamePattern;
301:     }
302:     nz = 0; irow = rstart;
303:     for ( i=0; i<m; i++ ) {
304:       lu->row[i] = nz;
305:       countA = ai[i+1] - ai[i];
306:       countB = bi[i+1] - bi[i];
307:       ajj = aj + ai[i];  /* ptr to the beginning of this row */
308:       bjj = bj + bi[i];

310:       /* B part, smaller col index */
311:       colA_start = rstart + ajj[0]; /* the smallest global col index of A */
312:       jB = 0;
313:       for (j=0; j<countB; j++){
314:         jcol = garray[bjj[j]];
315:         if (jcol > colA_start) {
316:           jB = j;
317:           break;
318:         }
319:         lu->col[nz] = jcol;
320:         lu->val[nz++] = *bv++;
321:         if (j==countB-1) jB = countB;
322:       }

324:       /* A part */
325:       for (j=0; j<countA; j++){
326:         lu->col[nz] = rstart + ajj[j];
327:         lu->val[nz++] = *av++;
328:       }

330:       /* B part, larger col index */
331:       for (j=jB; j<countB; j++){
332:         lu->col[nz] = garray[bjj[j]];
333:         lu->val[nz++] = *bv++;
334:       }
335:     }
336:     lu->row[m] = nz;
337: #if defined(PETSC_USE_COMPLEX)
338:     zCreate_CompRowLoc_Matrix_dist(&lu->A_sup, M, N, nz, m, rstart,
339:                                    lu->val, lu->col, lu->row, SLU_NR_loc, SLU_Z, SLU_GE);
340: #else
341:     dCreate_CompRowLoc_Matrix_dist(&lu->A_sup, M, N, nz, m, rstart,
342:                                    lu->val, lu->col, lu->row, SLU_NR_loc, SLU_D, SLU_GE);
343: #endif
344:   }
345:   if (lu->options.PrintStat) {
346:     PetscGetTime(&time);
347:     time0 = time - time0;
348:   }

350:   /* Factor the matrix. */
351:   PStatInit(&stat);   /* Initialize the statistics variables. */

353:   if (lu->MatInputMode == GLOBAL) { /* global mat input */
354: #if defined(PETSC_USE_COMPLEX)
355:     pzgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0,
356:                    &lu->grid, &lu->LUstruct, berr, &stat, &sinfo);
357: #else
358:     pdgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0,
359:                    &lu->grid, &lu->LUstruct, berr, &stat, &sinfo);
360: #endif 
361:   } else { /* distributed mat input */
362: #if defined(PETSC_USE_COMPLEX)
363:     pzgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0, &lu->grid,
364:             &lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &sinfo);
365:     if (sinfo) SETERRQ1(PETSC_ERR_LIB,"pzgssvx fails, info: %d\n",sinfo);
366: #else
367:     pdgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0, &lu->grid,
368:             &lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &sinfo);
369:     if (sinfo) SETERRQ1(PETSC_ERR_LIB,"pdgssvx fails, info: %d\n",sinfo);
370: #endif
371:   }

373:   if (lu->MatInputMode == GLOBAL && size > 1){
374:     MatDestroy(A_seq);
375:   }

377:   if (lu->options.PrintStat) {
378:     if (size > 1){
379:       MPI_Reduce(&time0,&time_max,1,MPI_DOUBLE,MPI_MAX,0,A->comm);
380:       MPI_Reduce(&time0,&time_min,1,MPI_DOUBLE,MPI_MIN,0,A->comm);
381:       MPI_Reduce(&time0,&time,1,MPI_DOUBLE,MPI_SUM,0,A->comm);
382:       time = time/size; /* average time */
383:       if (!rank)
384:         PetscPrintf(PETSC_COMM_SELF, "        Mat conversion(PETSc->SuperLU_DIST) time (max/min/avg): \n \
385:                               %g / %g / %g\n",time_max,time_min,time);
386:     } else {
387:       PetscPrintf(PETSC_COMM_SELF, "        Mat conversion(PETSc->SuperLU_DIST) time: \n \
388:                               %g\n",time0);
389:     }
390: 
391:     PStatPrint(&lu->options, &stat, &lu->grid);  /* Print the statistics. */
392:   }
393:   PStatFree(&stat);
394:   if (size > 1){
395:     F_diag = ((Mat_MPIAIJ *)(*F)->data)->A;
396:     F_diag->assembled = PETSC_TRUE;
397:   }
398:   (*F)->assembled   = PETSC_TRUE;
399:   lu->flg           = SAME_NONZERO_PATTERN;
400:   return(0);
401: }

403: /* Note the Petsc r and c permutations are ignored */
406: PetscErrorCode MatLUFactorSymbolic_SuperLU_DIST(Mat A,IS r,IS c,MatFactorInfo *info,Mat *F)
407: {
408:   Mat               B;
409:   Mat_SuperLU_DIST  *lu;
410:   PetscErrorCode    ierr;
411:   PetscInt          M=A->rmap.N,N=A->cmap.N,indx;
412:   PetscMPIInt       size;
413:   superlu_options_t options;
414:   PetscTruth        flg;
415:   const char        *ptype[] = {"MMD_AT_PLUS_A","NATURAL","MMD_ATA"};
416:   const char        *prtype[] = {"LargeDiag","NATURAL"};

419:   /* Create the factorization matrix */
420:   MatCreate(A->comm,&B);
421:   MatSetSizes(B,A->rmap.n,A->cmap.n,M,N);
422:   MatSetType(B,A->type_name);
423:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
424:   MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);

426:   B->ops->lufactornumeric  = MatLUFactorNumeric_SuperLU_DIST;
427:   B->ops->solve            = MatSolve_SuperLU_DIST;
428:   B->factor                = FACTOR_LU;

430:   lu = (Mat_SuperLU_DIST*)(B->spptr);

432:   /* Set the input options */
433:   set_default_options_dist(&options);

435:   lu->MatInputMode = GLOBAL;
436:   MPI_Comm_dup(A->comm,&(lu->comm_superlu));

438:   MPI_Comm_size(A->comm,&size);
439:   lu->nprow = size/2;               /* Default process rows.      */
440:   if (!lu->nprow) lu->nprow = 1;
441:   lu->npcol = size/lu->nprow;           /* Default process columns.   */

443:   PetscOptionsBegin(A->comm,A->prefix,"SuperLU_Dist Options","Mat");
444: 
445:     PetscOptionsInt("-mat_superlu_dist_r","Number rows in processor partition","None",lu->nprow,&lu->nprow,PETSC_NULL);
446:     PetscOptionsInt("-mat_superlu_dist_c","Number columns in processor partition","None",lu->npcol,&lu->npcol,PETSC_NULL);
447:     if (size != lu->nprow * lu->npcol) SETERRQ(PETSC_ERR_ARG_SIZ,"Number of processes should be equal to nprow*npcol");
448: 
449:     PetscOptionsInt("-mat_superlu_dist_matinput","Matrix input mode (0: GLOBAL; 1: DISTRIBUTED)","None",lu->MatInputMode,&lu->MatInputMode,PETSC_NULL);
450:     if(lu->MatInputMode == DISTRIBUTED && size == 1) lu->MatInputMode = GLOBAL;

452:     PetscOptionsTruth("-mat_superlu_dist_equil","Equilibrate matrix","None",PETSC_TRUE,&flg,0);
453:     if (!flg) {
454:       options.Equil = NO;
455:     }

457:     PetscOptionsEList("-mat_superlu_dist_rowperm","Row permutation","None",prtype,2,prtype[0],&indx,&flg);
458:     if (flg) {
459:       switch (indx) {
460:       case 0:
461:         options.RowPerm = LargeDiag;
462:         break;
463:       case 1:
464:         options.RowPerm = NOROWPERM;
465:         break;
466:       }
467:     }

469:     PetscOptionsEList("-mat_superlu_dist_colperm","Column permutation","None",ptype,4,ptype[0],&indx,&flg);
470:     if (flg) {
471:       switch (indx) {
472:       case 0:
473:         options.ColPerm = MMD_AT_PLUS_A;
474:         break;
475:       case 1:
476:         options.ColPerm = NATURAL;
477:         break;
478:       case 2:
479:         options.ColPerm = MMD_ATA;
480:         break;
481:       }
482:     }

484:     PetscOptionsTruth("-mat_superlu_dist_replacetinypivot","Replace tiny pivots","None",PETSC_TRUE,&flg,0);
485:     if (!flg) {
486:       options.ReplaceTinyPivot = NO;
487:     }

489:     options.IterRefine = NOREFINE;
490:     PetscOptionsTruth("-mat_superlu_dist_iterrefine","Use iterative refinement","None",PETSC_FALSE,&flg,0);
491:     if (flg) {
492:       options.IterRefine = DOUBLE;
493:     }

495:     if (PetscLogPrintInfo) {
496:       options.PrintStat = YES;
497:     } else {
498:       options.PrintStat = NO;
499:     }
500:     PetscOptionsTruth("-mat_superlu_dist_statprint","Print factorization information","None",
501:                               (PetscTruth)options.PrintStat,(PetscTruth*)&options.PrintStat,0);
502:   PetscOptionsEnd();

504:   /* Initialize the SuperLU process grid. */
505:   superlu_gridinit(lu->comm_superlu, lu->nprow, lu->npcol, &lu->grid);

507:   /* Initialize ScalePermstruct and LUstruct. */
508:   ScalePermstructInit(M, N, &lu->ScalePermstruct);
509:   LUstructInit(M, N, &lu->LUstruct);

511:   lu->options            = options;
512:   lu->flg                = DIFFERENT_NONZERO_PATTERN;
513:   lu->CleanUpSuperLU_Dist = PETSC_TRUE;
514:   *F = B;

516:   return(0);
517: }

521: PetscErrorCode MatAssemblyEnd_SuperLU_DIST(Mat A,MatAssemblyType mode) {
522:   PetscErrorCode   ierr;
523:   Mat_SuperLU_DIST *lu=(Mat_SuperLU_DIST*)(A->spptr);

526:   (*lu->MatAssemblyEnd)(A,mode);
527:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
528:   A->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU_DIST;
529:   return(0);
530: }

534: PetscErrorCode MatFactorInfo_SuperLU_DIST(Mat A,PetscViewer viewer)
535: {
536:   Mat_SuperLU_DIST  *lu=(Mat_SuperLU_DIST*)A->spptr;
537:   superlu_options_t options;
538:   PetscErrorCode    ierr;

541:   /* check if matrix is superlu_dist type */
542:   if (A->ops->solve != MatSolve_SuperLU_DIST) return(0);

544:   options = lu->options;
545:   PetscViewerASCIIPrintf(viewer,"SuperLU_DIST run parameters:\n");
546:   PetscViewerASCIIPrintf(viewer,"  Equilibrate matrix %s \n",PetscTruths[options.Equil != NO]);
547:   PetscViewerASCIIPrintf(viewer,"  Matrix input mode %d \n",lu->MatInputMode);
548:   PetscViewerASCIIPrintf(viewer,"  Replace tiny pivots %s \n",PetscTruths[options.ReplaceTinyPivot != NO]);
549:   PetscViewerASCIIPrintf(viewer,"  Use iterative refinement %s \n",PetscTruths[options.IterRefine == DOUBLE]);
550:   PetscViewerASCIIPrintf(viewer,"  Processors in row %d col partition %d \n",lu->nprow,lu->npcol);
551:   PetscViewerASCIIPrintf(viewer,"  Row permutation %s \n",(options.RowPerm == NOROWPERM) ? "NATURAL": "LargeDiag");
552:   if (options.ColPerm == NATURAL) {
553:     PetscViewerASCIIPrintf(viewer,"  Column permutation NATURAL\n");
554:   } else if (options.ColPerm == MMD_AT_PLUS_A) {
555:     PetscViewerASCIIPrintf(viewer,"  Column permutation MMD_AT_PLUS_A\n");
556:   } else if (options.ColPerm == MMD_ATA) {
557:     PetscViewerASCIIPrintf(viewer,"  Column permutation MMD_ATA\n");
558:   } else {
559:     SETERRQ(PETSC_ERR_ARG_WRONG,"Unknown column permutation");
560:   }
561:   return(0);
562: }

566: PetscErrorCode MatView_SuperLU_DIST(Mat A,PetscViewer viewer)
567: {
568:   PetscErrorCode    ierr;
569:   PetscTruth        iascii;
570:   PetscViewerFormat format;
571:   Mat_SuperLU_DIST  *lu=(Mat_SuperLU_DIST*)(A->spptr);

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

576:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
577:   if (iascii) {
578:     PetscViewerGetFormat(viewer,&format);
579:     if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
580:       MatFactorInfo_SuperLU_DIST(A,viewer);
581:     }
582:   }
583:   return(0);
584: }


590: PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_Base_SuperLU_DIST(Mat A,MatType type,MatReuse reuse,Mat *newmat)
591: {
592:   /* This routine is only called to convert to MATSUPERLU_DIST */
593:   /* from MATSEQAIJ if A has a single process communicator */
594:   /* or MATMPIAIJ otherwise, so we will ignore 'MatType type'. */
595:   PetscErrorCode   ierr;
596:   PetscMPIInt      size;
597:   MPI_Comm         comm;
598:   Mat              B=*newmat;
599:   Mat_SuperLU_DIST *lu;

602:   if (reuse == MAT_INITIAL_MATRIX) {
603:     MatDuplicate(A,MAT_COPY_VALUES,&B);
604:   }

606:   PetscObjectGetComm((PetscObject)A,&comm);
607:   PetscNew(Mat_SuperLU_DIST,&lu);

609:   lu->MatDuplicate         = A->ops->duplicate;
610:   lu->MatView              = A->ops->view;
611:   lu->MatAssemblyEnd       = A->ops->assemblyend;
612:   lu->MatLUFactorSymbolic  = A->ops->lufactorsymbolic;
613:   lu->MatDestroy           = A->ops->destroy;
614:   lu->CleanUpSuperLU_Dist  = PETSC_FALSE;

616:   B->spptr                 = (void*)lu;
617:   B->ops->duplicate        = MatDuplicate_SuperLU_DIST;
618:   B->ops->view             = MatView_SuperLU_DIST;
619:   B->ops->assemblyend      = MatAssemblyEnd_SuperLU_DIST;
620:   B->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU_DIST;
621:   B->ops->destroy          = MatDestroy_SuperLU_DIST;
622:   MPI_Comm_size(comm,&size);
623:   if (size == 1) {
624:     PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_superlu_dist_C",
625:                                              "MatConvert_Base_SuperLU_DIST",MatConvert_Base_SuperLU_DIST);
626:     PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_superlu_dist_seqaij_C",
627:                                              "MatConvert_SuperLU_DIST_Base",MatConvert_SuperLU_DIST_Base);
628:   } else {
629:     PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_superlu_dist_C",
630:                                              "MatConvert_Base_SuperLU_DIST",MatConvert_Base_SuperLU_DIST);
631:     PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_superlu_dist_mpiaij_C",
632:                                              "MatConvert_SuperLU_DIST_Base",MatConvert_SuperLU_DIST_Base);
633:   }
634:   PetscInfo(0,"Using SuperLU_DIST for SeqAIJ LU factorization and solves.\n");
635:   PetscObjectChangeTypeName((PetscObject)B,MATSUPERLU_DIST);
636:   *newmat = B;
637:   return(0);
638: }

643: PetscErrorCode MatDuplicate_SuperLU_DIST(Mat A, MatDuplicateOption op, Mat *M) {
644:   PetscErrorCode   ierr;
645:   Mat_SuperLU_DIST *lu=(Mat_SuperLU_DIST *)A->spptr;

648:   (*lu->MatDuplicate)(A,op,M);
649:   PetscMemcpy((*M)->spptr,lu,sizeof(Mat_SuperLU_DIST));
650:   return(0);
651: }

653: /*MC
654:   MATSUPERLU_DIST - MATSUPERLU_DIST = "superlu_dist" - A matrix type providing direct solvers (LU) for parallel matrices 
655:   via the external package SuperLU_DIST.

657:   If SuperLU_DIST is installed (see the manual for
658:   instructions on how to declare the existence of external packages),
659:   a matrix type can be constructed which invokes SuperLU_DIST solvers.
660:   After calling MatCreate(...,A), simply call MatSetType(A,MATSUPERLU_DIST).

662:   This matrix inherits from MATSEQAIJ when constructed with a single process communicator,
663:   and from MATMPIAIJ otherwise.  As a result, for single process communicators, 
664:   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 
665:   for communicators controlling multiple processes.  It is recommended that you call both of
666:   the above preallocation routines for simplicity.  One can also call MatConvert for an inplace
667:   conversion to or from the MATSEQAIJ or MATMPIAIJ type (depending on the communicator size)
668:   without data copy.

670:   Options Database Keys:
671: + -mat_type superlu_dist - sets the matrix type to "superlu_dist" during a call to MatSetFromOptions()
672: . -mat_superlu_dist_r <n> - number of rows in processor partition
673: . -mat_superlu_dist_c <n> - number of columns in processor partition
674: . -mat_superlu_dist_matinput <0,1> - matrix input mode; 0=global, 1=distributed
675: . -mat_superlu_dist_equil - equilibrate the matrix
676: . -mat_superlu_dist_rowperm <LargeDiag,NATURAL> - row permutation
677: . -mat_superlu_dist_colperm <MMD_AT_PLUS_A,MMD_ATA,NATURAL> - column permutation
678: . -mat_superlu_dist_replacetinypivot - replace tiny pivots
679: . -mat_superlu_dist_iterrefine - use iterative refinement
680: - -mat_superlu_dist_statprint - print factorization information

682:    Level: beginner

684: .seealso: PCLU
685: M*/

690: PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SuperLU_DIST(Mat A)
691: {
693:   PetscMPIInt    size;

696:   /* Change type name before calling MatSetType to force proper construction of SeqAIJ or MPIAIJ */
697:   /*   and SuperLU_DIST types */
698:   PetscObjectChangeTypeName((PetscObject)A,MATSUPERLU_DIST);
699:   MPI_Comm_size(A->comm,&size);
700:   if (size == 1) {
701:     MatSetType(A,MATSEQAIJ);
702:   } else {
703:     MatSetType(A,MATMPIAIJ);
704:     /*  A_diag = 0x0 ???  -- do we need it?
705:     Mat A_diag = ((Mat_MPIAIJ *)A->data)->A;
706:     MatConvert_Base_SuperLU_DIST(A_diag,MATSUPERLU_DIST,MAT_REUSE_MATRIX,&A_diag);
707:     */
708:   }
709:   MatConvert_Base_SuperLU_DIST(A,MATSUPERLU_DIST,MAT_REUSE_MATRIX,&A);
710:   return(0);
711: }