Actual source code: superlu.c
2: /*
3: Provides an interface to the SuperLU 3.0 sparse solver
4: */
6: #include src/mat/impls/aij/seq/aij.h
9: #if defined(PETSC_USE_COMPLEX)
10: #include "zsp_defs.h"
11: #else
12: #include "dsp_defs.h"
13: #endif
14: #include "util.h"
17: typedef struct {
18: SuperMatrix A,L,U,B,X;
19: superlu_options_t options;
20: int *perm_c; /* column permutation vector */
21: int *perm_r; /* row permutations from partial pivoting */
22: int *etree;
23: double *R, *C;
24: char equed[1];
25: int lwork;
26: void *work;
27: double rpg, rcond;
28: mem_usage_t mem_usage;
29: MatStructure flg;
31: /* A few function pointers for inheritance */
32: PetscErrorCode (*MatDuplicate)(Mat,MatDuplicateOption,Mat*);
33: PetscErrorCode (*MatView)(Mat,PetscViewer);
34: PetscErrorCode (*MatAssemblyEnd)(Mat,MatAssemblyType);
35: PetscErrorCode (*MatLUFactorSymbolic)(Mat,IS,IS,MatFactorInfo*,Mat*);
36: PetscErrorCode (*MatDestroy)(Mat);
38: /* Flag to clean up (non-global) SuperLU objects during Destroy */
39: PetscTruth CleanUpSuperLU;
40: } Mat_SuperLU;
43: EXTERN PetscErrorCode MatFactorInfo_SuperLU(Mat,PetscViewer);
44: EXTERN PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat,IS,IS,MatFactorInfo*,Mat*);
47: EXTERN PetscErrorCode MatConvert_SuperLU_SeqAIJ(Mat,const MatType,Mat*);
48: EXTERN PetscErrorCode MatConvert_SeqAIJ_SuperLU(Mat,const MatType,Mat*);
53: PetscErrorCode MatDestroy_SuperLU(Mat A)
54: {
56: Mat_SuperLU *lu = (Mat_SuperLU*)A->spptr;
59: if (lu->CleanUpSuperLU) { /* Free the SuperLU datastructures */
60: Destroy_SuperMatrix_Store(&lu->A);
61: Destroy_SuperMatrix_Store(&lu->B);
62: Destroy_SuperMatrix_Store(&lu->X);
64: PetscFree(lu->etree);
65: PetscFree(lu->perm_r);
66: PetscFree(lu->perm_c);
67: PetscFree(lu->R);
68: PetscFree(lu->C);
69: if ( lu->lwork >= 0 ) {
70: Destroy_SuperNode_Matrix(&lu->L);
71: Destroy_CompCol_Matrix(&lu->U);
72: }
73: }
74: MatConvert_SuperLU_SeqAIJ(A,MATSEQAIJ,&A);
75: (*A->ops->destroy)(A);
76: return(0);
77: }
81: PetscErrorCode MatView_SuperLU(Mat A,PetscViewer viewer)
82: {
84: PetscTruth iascii;
85: PetscViewerFormat format;
86: Mat_SuperLU *lu=(Mat_SuperLU*)(A->spptr);
89: (*lu->MatView)(A,viewer);
91: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
92: if (iascii) {
93: PetscViewerGetFormat(viewer,&format);
94: if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
95: MatFactorInfo_SuperLU(A,viewer);
96: }
97: }
98: return(0);
99: }
103: PetscErrorCode MatAssemblyEnd_SuperLU(Mat A,MatAssemblyType mode) {
105: Mat_SuperLU *lu=(Mat_SuperLU*)(A->spptr);
108: (*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: int numRows = A->m,numCols = A->n;
124: SCformat *Lstore;
125: int 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: int 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,numRows,numNullCols,numRows,numNullCols,nullMat);
141: MatSetType(*nullMat,MATSEQDENSE);
142: MatSeqDenseSetPreallocation(*nullMat,PETSC_NULL);
143: if (!numNullCols) {
144: MatAssemblyBegin(*nullMat,MAT_FINAL_ASSEMBLY);
145: MatAssemblyEnd(*nullMat,MAT_FINAL_ASSEMBLY);
146: return(0);
147: }
148: #if defined(PETSC_USE_COMPLEX)
149: nullVals = (doublecomplex*)((Mat_SeqDense*)(*nullMat)->data)->v;
150: #else
151: nullVals = ((Mat_SeqDense*)(*nullMat)->data)->v;
152: #endif
153: /* Copy in the columns */
154: Lstore = (SCformat*)lu->L.Store;
155: for(block = 0; block <= Lstore->nsuper; block++) {
156: newRow = Lstore->sup_to_col[block];
157: size = Lstore->sup_to_col[block+1] - Lstore->sup_to_col[block];
158: for(col = Lstore->rowind_colptr[newRow]; col < Lstore->rowind_colptr[newRow+1]; col++) {
159: newCol = Lstore->rowind[col];
160: if (newCol >= numRows) {
161: for(b = 0; b < size; b++)
162: #if defined(PETSC_USE_COMPLEX)
163: nullVals[(newCol-numRows)*numRows+newRow+b] = ((doublecomplex*)Lstore->nzval)[Lstore->nzval_colptr[newRow+b]+col];
164: #else
165: nullVals[(newCol-numRows)*numRows+newRow+b] = ((double*)Lstore->nzval)[Lstore->nzval_colptr[newRow+b]+col];
166: #endif
167: }
168: }
169: }
170: /* Permute rhs to form P^T_c B */
171: PetscMalloc(numRows*sizeof(double),&workVals);
172: for(b = 0; b < numNullCols; b++) {
173: for(row = 0; row < numRows; row++) workVals[lu->perm_c[row]] = nullVals[b*numRows+row];
174: for(row = 0; row < numRows; row++) nullVals[b*numRows+row] = workVals[row];
175: }
176: /* Backward solve the upper triangle A x = b */
177: for(b = 0; b < numNullCols; b++) {
178: #if defined(PETSC_USE_COMPLEX)
179: sp_ztrsv("L","T","U",&lu->L,&lu->U,&nullVals[b*numRows],&stat,&ierr);
180: #else
181: sp_dtrsv("L","T","U",&lu->L,&lu->U,&nullVals[b*numRows],&stat,&ierr);
182: #endif
183: if (ierr < 0)
184: SETERRQ1(PETSC_ERR_ARG_WRONG,"The argument %D was invalid",-ierr);
185: }
186: PetscFree(workVals);
188: MatAssemblyBegin(*nullMat,MAT_FINAL_ASSEMBLY);
189: MatAssemblyEnd(*nullMat,MAT_FINAL_ASSEMBLY);
190: return(0);
191: }
192: #endif
196: PetscErrorCode MatSolve_SuperLU(Mat A,Vec b,Vec x)
197: {
198: Mat_SuperLU *lu = (Mat_SuperLU*)A->spptr;
199: PetscScalar *barray,*xarray;
201: int info,i;
202: SuperLUStat_t stat;
203: double ferr,berr;
206: if ( lu->lwork == -1 ) {
207: return(0);
208: }
209: lu->B.ncol = 1; /* Set the number of right-hand side */
210: VecGetArray(b,&barray);
211: VecGetArray(x,&xarray);
213: #if defined(PETSC_USE_COMPLEX)
214: ((DNformat*)lu->B.Store)->nzval = (doublecomplex*)barray;
215: ((DNformat*)lu->X.Store)->nzval = (doublecomplex*)xarray;
216: #else
217: ((DNformat*)lu->B.Store)->nzval = barray;
218: ((DNformat*)lu->X.Store)->nzval = xarray;
219: #endif
221: /* Initialize the statistics variables. */
222: StatInit(&stat);
224: lu->options.Fact = FACTORED; /* Indicate the factored form of A is supplied. */
225: lu->options.Trans = TRANS;
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 MatLUFactorNumeric_SuperLU(Mat A,Mat *F)
266: {
267: Mat_SeqAIJ *aa = (Mat_SeqAIJ*)(A)->data;
268: Mat_SuperLU *lu = (Mat_SuperLU*)(*F)->spptr;
270: int info;
271: SuperLUStat_t stat;
272: double ferr, berr;
273: NCformat *Ustore;
274: SCformat *Lstore;
275:
277: if (lu->flg == SAME_NONZERO_PATTERN){ /* successing numerical factorization */
278: lu->options.Fact = SamePattern;
279: /* Ref: ~SuperLU_3.0/EXAMPLE/dlinsolx2.c */
280: Destroy_SuperMatrix_Store(&lu->A);
281: if ( lu->lwork >= 0 ) {
282: Destroy_SuperNode_Matrix(&lu->L);
283: Destroy_CompCol_Matrix(&lu->U);
284: lu->options.Fact = SamePattern;
285: }
286: }
288: /* Create the SuperMatrix for lu->A=A^T:
289: Since SuperLU likes column-oriented matrices,we pass it the transpose,
290: and then solve A^T X = B in MatSolve(). */
291: #if defined(PETSC_USE_COMPLEX)
292: zCreate_CompCol_Matrix(&lu->A,A->n,A->m,aa->nz,(doublecomplex*)aa->a,aa->j,aa->i,
293: SLU_NC,SLU_Z,SLU_GE);
294: #else
295: dCreate_CompCol_Matrix(&lu->A,A->n,A->m,aa->nz,aa->a,aa->j,aa->i,
296: SLU_NC,SLU_D,SLU_GE);
297: #endif
298:
299: /* Initialize the statistics variables. */
300: StatInit(&stat);
302: /* Numerical factorization */
303: lu->B.ncol = 0; /* Indicate not to solve the system */
304: #if defined(PETSC_USE_COMPLEX)
305: zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
306: &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
307: &lu->mem_usage, &stat, &info);
308: #else
309: dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C,
310: &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr,
311: &lu->mem_usage, &stat, &info);
312: #endif
313: if ( !info || info == lu->A.ncol+1 ) {
314: if ( lu->options.PivotGrowth )
315: PetscPrintf(PETSC_COMM_SELF," Recip. pivot growth = %e\n", lu->rpg);
316: if ( lu->options.ConditionNumber )
317: PetscPrintf(PETSC_COMM_SELF," Recip. condition number = %e\n", lu->rcond);
318: } else if ( info > 0 ){
319: if ( lu->lwork == -1 ) {
320: PetscPrintf(PETSC_COMM_SELF," ** Estimated memory: %D bytes\n", info - lu->A.ncol);
321: } else {
322: PetscPrintf(PETSC_COMM_SELF," Warning: gssvx() returns info %D\n",info);
323: }
324: } else { /* info < 0 */
325: SETERRQ2(PETSC_ERR_LIB, "info = %D, the %D-th argument in gssvx() had an illegal value", info,-info);
326: }
328: if ( lu->options.PrintStat ) {
329: PetscPrintf(PETSC_COMM_SELF,"MatLUFactorNumeric_SuperLU():\n");
330: StatPrint(&stat);
331: Lstore = (SCformat *) lu->L.Store;
332: Ustore = (NCformat *) lu->U.Store;
333: PetscPrintf(PETSC_COMM_SELF," No of nonzeros in factor L = %D\n", Lstore->nnz);
334: PetscPrintf(PETSC_COMM_SELF," No of nonzeros in factor U = %D\n", Ustore->nnz);
335: PetscPrintf(PETSC_COMM_SELF," No of nonzeros in L+U = %D\n", Lstore->nnz + Ustore->nnz - lu->A.ncol);
336: PetscPrintf(PETSC_COMM_SELF," L\\U MB %.3f\ttotal MB needed %.3f\texpansions %D\n",
337: lu->mem_usage.for_lu/1e6, lu->mem_usage.total_needed/1e6,
338: lu->mem_usage.expansions);
339: }
340: StatFree(&stat);
342: lu->flg = SAME_NONZERO_PATTERN;
343: return(0);
344: }
346: /*
347: Note the r permutation is ignored
348: */
351: PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat A,IS r,IS c,MatFactorInfo *info,Mat *F)
352: {
353: Mat B;
354: Mat_SuperLU *lu;
356: int m=A->m,n=A->n,indx;
357: PetscTruth flg;
358: const char *colperm[]={"NATURAL","MMD_ATA","MMD_AT_PLUS_A","COLAMD"}; /* MY_PERMC - not supported by the petsc interface yet */
359: const char *iterrefine[]={"NOREFINE", "SINGLE", "DOUBLE", "EXTRA"};
360: const char *rowperm[]={"NOROWPERM", "LargeDiag"}; /* MY_PERMC - not supported by the petsc interface yet */
363:
364: MatCreate(A->comm,A->m,A->n,PETSC_DETERMINE,PETSC_DETERMINE,&B);
365: MatSetType(B,A->type_name);
366: MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
368: B->ops->lufactornumeric = MatLUFactorNumeric_SuperLU;
369: B->ops->solve = MatSolve_SuperLU;
370: B->factor = FACTOR_LU;
371: B->assembled = PETSC_TRUE; /* required by -ksp_view */
372:
373: lu = (Mat_SuperLU*)(B->spptr);
375: /* Set SuperLU options */
376: /* the default values for options argument:
377: options.Fact = DOFACT;
378: options.Equil = YES;
379: options.ColPerm = COLAMD;
380: options.DiagPivotThresh = 1.0;
381: options.Trans = NOTRANS;
382: options.IterRefine = NOREFINE;
383: options.SymmetricMode = NO;
384: options.PivotGrowth = NO;
385: options.ConditionNumber = NO;
386: options.PrintStat = YES;
387: */
388: set_default_options(&lu->options);
389: /* equilibration causes error in solve(), thus not supported here. See dgssvx.c for possible reason. */
390: lu->options.Equil = NO;
391: lu->options.PrintStat = NO;
392: lu->lwork = 0; /* allocate space internally by system malloc */
394: PetscOptionsBegin(A->comm,A->prefix,"SuperLU Options","Mat");
395: /*
396: PetscOptionsLogical("-mat_superlu_equil","Equil","None",PETSC_FALSE,&flg,0);
397: if (flg) lu->options.Equil = YES; -- not supported by the interface !!!
398: */
399: PetscOptionsEList("-mat_superlu_colperm","ColPerm","None",colperm,4,colperm[3],&indx,&flg);
400: if (flg) {lu->options.ColPerm = (colperm_t)indx;}
401: PetscOptionsEList("-mat_superlu_iterrefine","IterRefine","None",iterrefine,4,iterrefine[0],&indx,&flg);
402: if (flg) { lu->options.IterRefine = (IterRefine_t)indx;}
403: PetscOptionsLogical("-mat_superlu_symmetricmode","SymmetricMode","None",PETSC_FALSE,&flg,0);
404: if (flg) lu->options.SymmetricMode = YES;
405: PetscOptionsReal("-mat_superlu_diagpivotthresh","DiagPivotThresh","None",lu->options.DiagPivotThresh,&lu->options.DiagPivotThresh,PETSC_NULL);
406: PetscOptionsLogical("-mat_superlu_pivotgrowth","PivotGrowth","None",PETSC_FALSE,&flg,0);
407: if (flg) lu->options.PivotGrowth = YES;
408: PetscOptionsLogical("-mat_superlu_conditionnumber","ConditionNumber","None",PETSC_FALSE,&flg,0);
409: if (flg) lu->options.ConditionNumber = YES;
410: PetscOptionsEList("-mat_superlu_rowperm","rowperm","None",rowperm,2,rowperm[0],&indx,&flg);
411: if (flg) {lu->options.RowPerm = (rowperm_t)indx;}
412: PetscOptionsLogical("-mat_superlu_replacetinypivot","ReplaceTinyPivot","None",PETSC_FALSE,&flg,0);
413: if (flg) lu->options.ReplaceTinyPivot = YES;
414: PetscOptionsLogical("-mat_superlu_printstat","PrintStat","None",PETSC_FALSE,&flg,0);
415: if (flg) lu->options.PrintStat = YES;
416: PetscOptionsInt("-mat_superlu_lwork","size of work array in bytes used by factorization","None",lu->lwork,&lu->lwork,PETSC_NULL);
417: if (lu->lwork > 0 ){
418: PetscMalloc(lu->lwork,&lu->work);
419: } else if (lu->lwork != 0 && lu->lwork != -1){
420: PetscPrintf(PETSC_COMM_SELF," Warning: lwork %D is not supported by SUPERLU. The default lwork=0 is used.\n",lu->lwork);
421: lu->lwork = 0;
422: }
423: PetscOptionsEnd();
425: #ifdef SUPERLU2
426: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatCreateNull","MatCreateNull_SuperLU",
427: (void(*)(void))MatCreateNull_SuperLU);
428: #endif
430: /* Allocate spaces (notice sizes are for the transpose) */
431: PetscMalloc(m*sizeof(int),&lu->etree);
432: PetscMalloc(n*sizeof(int),&lu->perm_r);
433: PetscMalloc(m*sizeof(int),&lu->perm_c);
434: PetscMalloc(n*sizeof(int),&lu->R);
435: PetscMalloc(m*sizeof(int),&lu->C);
436:
437: /* create rhs and solution x without allocate space for .Store */
438: #if defined(PETSC_USE_COMPLEX)
439: zCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
440: zCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_Z, SLU_GE);
441: #else
442: dCreate_Dense_Matrix(&lu->B, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
443: dCreate_Dense_Matrix(&lu->X, m, 1, PETSC_NULL, m, SLU_DN, SLU_D, SLU_GE);
444: #endif
446: lu->flg = DIFFERENT_NONZERO_PATTERN;
447: lu->CleanUpSuperLU = PETSC_TRUE;
449: *F = B;
450: PetscLogObjectMemory(B,(A->m+A->n)*sizeof(int)+sizeof(Mat_SuperLU));
451: return(0);
452: }
454: /* used by -ksp_view */
457: PetscErrorCode MatFactorInfo_SuperLU(Mat A,PetscViewer viewer)
458: {
459: Mat_SuperLU *lu= (Mat_SuperLU*)A->spptr;
460: int ierr;
461: superlu_options_t options;
464: /* check if matrix is superlu_dist type */
465: if (A->ops->solve != MatSolve_SuperLU) return(0);
467: options = lu->options;
468: PetscViewerASCIIPrintf(viewer,"SuperLU run parameters:\n");
469: PetscViewerASCIIPrintf(viewer," Equil: %s\n",(options.Equil != NO) ? "YES": "NO");
470: PetscViewerASCIIPrintf(viewer," ColPerm: %D\n",options.ColPerm);
471: PetscViewerASCIIPrintf(viewer," IterRefine: %D\n",options.IterRefine);
472: PetscViewerASCIIPrintf(viewer," SymmetricMode: %s\n",(options.SymmetricMode != NO) ? "YES": "NO");
473: PetscViewerASCIIPrintf(viewer," DiagPivotThresh: %g\n",options.DiagPivotThresh);
474: PetscViewerASCIIPrintf(viewer," PivotGrowth: %s\n",(options.PivotGrowth != NO) ? "YES": "NO");
475: PetscViewerASCIIPrintf(viewer," ConditionNumber: %s\n",(options.ConditionNumber != NO) ? "YES": "NO");
476: PetscViewerASCIIPrintf(viewer," RowPerm: %D\n",options.RowPerm);
477: PetscViewerASCIIPrintf(viewer," ReplaceTinyPivot: %s\n",(options.ReplaceTinyPivot != NO) ? "YES": "NO");
478: PetscViewerASCIIPrintf(viewer," PrintStat: %s\n",(options.PrintStat != NO) ? "YES": "NO");
479: PetscViewerASCIIPrintf(viewer," lwork: %D\n",lu->lwork);
481: return(0);
482: }
486: PetscErrorCode MatDuplicate_SuperLU(Mat A, MatDuplicateOption op, Mat *M) {
487: int ierr;
488: Mat_SuperLU *lu=(Mat_SuperLU *)A->spptr;
491: (*lu->MatDuplicate)(A,op,M);
492: PetscMemcpy((*M)->spptr,lu,sizeof(Mat_SuperLU));
493: return(0);
494: }
499: PetscErrorCode MatConvert_SuperLU_SeqAIJ(Mat A,const MatType type,Mat *newmat)
500: {
501: /* This routine is only called to convert an unfactored PETSc-SuperLU matrix */
502: /* to its base PETSc type, so we will ignore 'MatType type'. */
503: int ierr;
504: Mat B=*newmat;
505: Mat_SuperLU *lu=(Mat_SuperLU *)A->spptr;
508: if (B != A) {
509: MatDuplicate(A,MAT_COPY_VALUES,&B);
510: }
511: /* Reset the original function pointers */
512: B->ops->duplicate = lu->MatDuplicate;
513: B->ops->view = lu->MatView;
514: B->ops->assemblyend = lu->MatAssemblyEnd;
515: B->ops->lufactorsymbolic = lu->MatLUFactorSymbolic;
516: B->ops->destroy = lu->MatDestroy;
517: /* lu is only a function pointer stash unless we've factored the matrix, which we haven't! */
518: PetscFree(lu);
520: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_superlu_C","",PETSC_NULL);
521: PetscObjectComposeFunction((PetscObject)B,"MatConvert_superlu_seqaij_C","",PETSC_NULL);
523: PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
524: *newmat = B;
525: return(0);
526: }
532: PetscErrorCode MatConvert_SeqAIJ_SuperLU(Mat A,const MatType type,Mat *newmat)
533: {
534: /* This routine is only called to convert to MATSUPERLU */
535: /* from MATSEQAIJ, so we will ignore 'MatType type'. */
536: int ierr;
537: Mat B=*newmat;
538: Mat_SuperLU *lu;
541: if (B != A) {
542: MatDuplicate(A,MAT_COPY_VALUES,&B);
543: }
545: PetscNew(Mat_SuperLU,&lu);
546: lu->MatDuplicate = A->ops->duplicate;
547: lu->MatView = A->ops->view;
548: lu->MatAssemblyEnd = A->ops->assemblyend;
549: lu->MatLUFactorSymbolic = A->ops->lufactorsymbolic;
550: lu->MatDestroy = A->ops->destroy;
551: lu->CleanUpSuperLU = PETSC_FALSE;
553: B->spptr = (void*)lu;
554: B->ops->duplicate = MatDuplicate_SuperLU;
555: B->ops->view = MatView_SuperLU;
556: B->ops->assemblyend = MatAssemblyEnd_SuperLU;
557: B->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU;
558: B->ops->choleskyfactorsymbolic = 0;
559: B->ops->destroy = MatDestroy_SuperLU;
561: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_superlu_C",
562: "MatConvert_SeqAIJ_SuperLU",MatConvert_SeqAIJ_SuperLU);
563: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_superlu_seqaij_C",
564: "MatConvert_SuperLU_SeqAIJ",MatConvert_SuperLU_SeqAIJ);
565: PetscLogInfo(0,"Using SuperLU for SeqAIJ LU factorization and solves.");
566: PetscObjectChangeTypeName((PetscObject)B,MATSUPERLU);
567: *newmat = B;
568: return(0);
569: }
572: /*MC
573: MATSUPERLU - MATSUPERLU = "superlu" - A matrix type providing direct solvers (LU) for sequential matrices
574: via the external package SuperLU.
576: If SuperLU is installed (see the manual for
577: instructions on how to declare the existence of external packages),
578: a matrix type can be constructed which invokes SuperLU solvers.
579: After calling MatCreate(...,A), simply call MatSetType(A,MATSUPERLU).
580: This matrix type is only supported for double precision real.
582: This matrix inherits from MATSEQAIJ. As a result, MatSeqAIJSetPreallocation is
583: supported for this matrix type. One can also call MatConvert for an inplace conversion to or from
584: the MATSEQAIJ type without data copy.
586: Options Database Keys:
587: + -mat_type superlu - sets the matrix type to "superlu" during a call to MatSetFromOptions()
588: - -mat_superlu_ordering <0,1,2,3> - 0: natural ordering,
589: 1: MMD applied to A'*A,
590: 2: MMD applied to A'+A,
591: 3: COLAMD, approximate minimum degree column ordering
593: Level: beginner
595: .seealso: PCLU
596: M*/
601: PetscErrorCode MatCreate_SuperLU(Mat A)
602: {
606: /* Change type name before calling MatSetType to force proper construction of SeqAIJ and SUPERLU types */
607: PetscObjectChangeTypeName((PetscObject)A,MATSUPERLU);
608: MatSetType(A,MATSEQAIJ);
609: MatConvert_SeqAIJ_SuperLU(A,MATSUPERLU,&A);
610: return(0);
611: }