xref: /petsc/src/mat/impls/aij/seq/superlu/superlu.c (revision e8e188d2d8d8bcc000607a9a2a524e07d1c8a16a)
1 
2 /*
3      This file implements a subclass of the SeqAIJ matrix class that uses
4      the SuperLU sparse solver.
5 */
6 
7 /*
8      Defines the data structure for the base matrix type (SeqAIJ)
9 */
10 #include <../src/mat/impls/aij/seq/aij.h> /*I "petscmat.h" I*/
11 
12 /*
13      SuperLU include files
14 */
15 EXTERN_C_BEGIN
16 #if defined(PETSC_USE_COMPLEX)
17   #if defined(PETSC_USE_REAL_SINGLE)
18     #include <slu_cdefs.h>
19   #else
20     #include <slu_zdefs.h>
21   #endif
22 #else
23   #if defined(PETSC_USE_REAL_SINGLE)
24     #include <slu_sdefs.h>
25   #else
26     #include <slu_ddefs.h>
27   #endif
28 #endif
29 #include <slu_util.h>
30 EXTERN_C_END
31 
32 /*
33      This is the data that defines the SuperLU factored 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;
48   SuperLUStat_t     stat;
49   Mat               A_dup;
50   PetscScalar      *rhs_dup;
51   GlobalLU_t        Glu;
52   PetscBool         needconversion;
53 
54   /* Flag to clean up (non-global) SuperLU objects during Destroy */
55   PetscBool CleanUpSuperLU;
56 } Mat_SuperLU;
57 
58 /*
59     Utility function
60 */
61 static PetscErrorCode MatView_Info_SuperLU(Mat A, PetscViewer viewer)
62 {
63   Mat_SuperLU      *lu = (Mat_SuperLU *)A->data;
64   superlu_options_t options;
65 
66   PetscFunctionBegin;
67   options = lu->options;
68 
69   PetscCall(PetscViewerASCIIPrintf(viewer, "SuperLU run parameters:\n"));
70   PetscCall(PetscViewerASCIIPrintf(viewer, "  Equil: %s\n", (options.Equil != NO) ? "YES" : "NO"));
71   PetscCall(PetscViewerASCIIPrintf(viewer, "  ColPerm: %" PetscInt_FMT "\n", options.ColPerm));
72   PetscCall(PetscViewerASCIIPrintf(viewer, "  IterRefine: %" PetscInt_FMT "\n", options.IterRefine));
73   PetscCall(PetscViewerASCIIPrintf(viewer, "  SymmetricMode: %s\n", (options.SymmetricMode != NO) ? "YES" : "NO"));
74   PetscCall(PetscViewerASCIIPrintf(viewer, "  DiagPivotThresh: %g\n", options.DiagPivotThresh));
75   PetscCall(PetscViewerASCIIPrintf(viewer, "  PivotGrowth: %s\n", (options.PivotGrowth != NO) ? "YES" : "NO"));
76   PetscCall(PetscViewerASCIIPrintf(viewer, "  ConditionNumber: %s\n", (options.ConditionNumber != NO) ? "YES" : "NO"));
77   PetscCall(PetscViewerASCIIPrintf(viewer, "  RowPerm: %" PetscInt_FMT "\n", options.RowPerm));
78   PetscCall(PetscViewerASCIIPrintf(viewer, "  ReplaceTinyPivot: %s\n", (options.ReplaceTinyPivot != NO) ? "YES" : "NO"));
79   PetscCall(PetscViewerASCIIPrintf(viewer, "  PrintStat: %s\n", (options.PrintStat != NO) ? "YES" : "NO"));
80   PetscCall(PetscViewerASCIIPrintf(viewer, "  lwork: %" PetscInt_FMT "\n", lu->lwork));
81   if (A->factortype == MAT_FACTOR_ILU) {
82     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_DropTol: %g\n", options.ILU_DropTol));
83     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_FillTol: %g\n", options.ILU_FillTol));
84     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_FillFactor: %g\n", options.ILU_FillFactor));
85     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_DropRule: %" PetscInt_FMT "\n", options.ILU_DropRule));
86     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_Norm: %" PetscInt_FMT "\n", options.ILU_Norm));
87     PetscCall(PetscViewerASCIIPrintf(viewer, "  ILU_MILU: %" PetscInt_FMT "\n", options.ILU_MILU));
88   }
89   PetscFunctionReturn(PETSC_SUCCESS);
90 }
91 
92 static PetscErrorCode MatSolve_SuperLU_Private(Mat A, Vec b, Vec x)
93 {
94   Mat_SuperLU       *lu = (Mat_SuperLU *)A->data;
95   const PetscScalar *barray;
96   PetscScalar       *xarray;
97   PetscInt           info, i, n;
98   PetscReal          ferr, berr;
99   static PetscBool   cite = PETSC_FALSE;
100 
101   PetscFunctionBegin;
102   if (lu->lwork == -1) PetscFunctionReturn(PETSC_SUCCESS);
103   PetscCall(PetscCitationsRegister("@article{superlu99,\n  author  = {James W. Demmel and Stanley C. Eisenstat and\n             John R. Gilbert and Xiaoye S. Li and Joseph W. H. Liu},\n  title = {A supernodal approach to sparse partial "
104                                    "pivoting},\n  journal = {SIAM J. Matrix Analysis and Applications},\n  year = {1999},\n  volume  = {20},\n  number = {3},\n  pages = {720-755}\n}\n",
105                                    &cite));
106 
107   PetscCall(VecGetLocalSize(x, &n));
108   lu->B.ncol = 1; /* Set the number of right-hand side */
109   if (lu->options.Equil && !lu->rhs_dup) {
110     /* superlu overwrites b when Equil is used, thus create rhs_dup to keep user's b unchanged */
111     PetscCall(PetscMalloc1(n, &lu->rhs_dup));
112   }
113   if (lu->options.Equil) {
114     /* Copy b into rsh_dup */
115     PetscCall(VecGetArrayRead(b, &barray));
116     PetscCall(PetscArraycpy(lu->rhs_dup, barray, n));
117     PetscCall(VecRestoreArrayRead(b, &barray));
118     barray = lu->rhs_dup;
119   } else {
120     PetscCall(VecGetArrayRead(b, &barray));
121   }
122   PetscCall(VecGetArray(x, &xarray));
123 
124 #if defined(PETSC_USE_COMPLEX)
125   #if defined(PETSC_USE_REAL_SINGLE)
126   ((DNformat *)lu->B.Store)->nzval = (singlecomplex *)barray;
127   ((DNformat *)lu->X.Store)->nzval = (singlecomplex *)xarray;
128   #else
129   ((DNformat *)lu->B.Store)->nzval = (doublecomplex *)barray;
130   ((DNformat *)lu->X.Store)->nzval = (doublecomplex *)xarray;
131   #endif
132 #else
133   ((DNformat *)lu->B.Store)->nzval = (void *)barray;
134   ((DNformat *)lu->X.Store)->nzval = xarray;
135 #endif
136 
137   lu->options.Fact = FACTORED; /* Indicate the factored form of A is supplied. */
138   if (A->factortype == MAT_FACTOR_LU) {
139 #if defined(PETSC_USE_COMPLEX)
140   #if defined(PETSC_USE_REAL_SINGLE)
141     PetscStackCallExternalVoid("SuperLU:cgssvx", cgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
142   #else
143     PetscStackCallExternalVoid("SuperLU:zgssvx", zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
144   #endif
145 #else
146   #if defined(PETSC_USE_REAL_SINGLE)
147     PetscStackCallExternalVoid("SuperLU:sgssvx", sgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
148   #else
149     PetscStackCallExternalVoid("SuperLU:dgssvx", dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
150   #endif
151 #endif
152   } else if (A->factortype == MAT_FACTOR_ILU) {
153 #if defined(PETSC_USE_COMPLEX)
154   #if defined(PETSC_USE_REAL_SINGLE)
155     PetscStackCallExternalVoid("SuperLU:cgsisx", cgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
156   #else
157     PetscStackCallExternalVoid("SuperLU:zgsisx", zgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
158   #endif
159 #else
160   #if defined(PETSC_USE_REAL_SINGLE)
161     PetscStackCallExternalVoid("SuperLU:sgsisx", sgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
162   #else
163     PetscStackCallExternalVoid("SuperLU:dgsisx", dgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &info));
164   #endif
165 #endif
166   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Factor type not supported");
167   if (!lu->options.Equil) PetscCall(VecRestoreArrayRead(b, &barray));
168   PetscCall(VecRestoreArray(x, &xarray));
169 
170   if (!info || info == lu->A.ncol + 1) {
171     if (lu->options.IterRefine) {
172       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Iterative Refinement:\n"));
173       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  %8s%8s%16s%16s\n", "rhs", "Steps", "FERR", "BERR"));
174       for (i = 0; i < 1; ++i) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  %8d%8d%16e%16e\n", i + 1, lu->stat.RefineSteps, ferr, berr));
175     }
176   } else if (info > 0) {
177     if (lu->lwork == -1) {
178       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  ** Estimated memory: %" PetscInt_FMT " bytes\n", info - lu->A.ncol));
179     } else {
180       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Warning: gssvx() returns info %" PetscInt_FMT "\n", info));
181     }
182   } else PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "info = %" PetscInt_FMT ", the %" PetscInt_FMT "-th argument in gssvx() had an illegal value", info, -info);
183 
184   if (lu->options.PrintStat) {
185     PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSolve__SuperLU():\n"));
186     PetscStackCallExternalVoid("SuperLU:StatPrint", StatPrint(&lu->stat));
187   }
188   PetscFunctionReturn(PETSC_SUCCESS);
189 }
190 
191 static PetscErrorCode MatSolve_SuperLU(Mat A, Vec b, Vec x)
192 {
193   Mat_SuperLU *lu = (Mat_SuperLU *)A->data;
194   trans_t      oldOption;
195 
196   PetscFunctionBegin;
197   if (A->factorerrortype) {
198     PetscCall(PetscInfo(A, "MatSolve is called with singular matrix factor, skip\n"));
199     PetscCall(VecSetInf(x));
200     PetscFunctionReturn(PETSC_SUCCESS);
201   }
202 
203   oldOption         = lu->options.Trans;
204   lu->options.Trans = TRANS;
205   PetscCall(MatSolve_SuperLU_Private(A, b, x));
206   lu->options.Trans = oldOption;
207   PetscFunctionReturn(PETSC_SUCCESS);
208 }
209 
210 static PetscErrorCode MatSolveTranspose_SuperLU(Mat A, Vec b, Vec x)
211 {
212   Mat_SuperLU *lu = (Mat_SuperLU *)A->data;
213   trans_t      oldOption;
214 
215   PetscFunctionBegin;
216   if (A->factorerrortype) {
217     PetscCall(PetscInfo(A, "MatSolve is called with singular matrix factor, skip\n"));
218     PetscCall(VecSetInf(x));
219     PetscFunctionReturn(PETSC_SUCCESS);
220   }
221 
222   oldOption         = lu->options.Trans;
223   lu->options.Trans = NOTRANS;
224   PetscCall(MatSolve_SuperLU_Private(A, b, x));
225   lu->options.Trans = oldOption;
226   PetscFunctionReturn(PETSC_SUCCESS);
227 }
228 
229 static PetscErrorCode MatLUFactorNumeric_SuperLU(Mat F, Mat A, const MatFactorInfo *info)
230 {
231   Mat_SuperLU *lu = (Mat_SuperLU *)F->data;
232   Mat_SeqAIJ  *aa;
233   PetscInt     sinfo;
234   PetscReal    ferr, berr;
235   NCformat    *Ustore;
236   SCformat    *Lstore;
237 
238   PetscFunctionBegin;
239   if (lu->flg == SAME_NONZERO_PATTERN) { /* successive numerical factorization */
240     lu->options.Fact = SamePattern;
241     /* Ref: ~SuperLU_3.0/EXAMPLE/dlinsolx2.c */
242     Destroy_SuperMatrix_Store(&lu->A);
243     if (lu->A_dup) PetscCall(MatCopy_SeqAIJ(A, lu->A_dup, SAME_NONZERO_PATTERN));
244 
245     if (lu->lwork >= 0) {
246       PetscStackCallExternalVoid("SuperLU:Destroy_SuperNode_Matrix", Destroy_SuperNode_Matrix(&lu->L));
247       PetscStackCallExternalVoid("SuperLU:Destroy_CompCol_Matrix", Destroy_CompCol_Matrix(&lu->U));
248       lu->options.Fact = SamePattern;
249     }
250   }
251 
252   /* Create the SuperMatrix for lu->A=A^T:
253        Since SuperLU likes column-oriented matrices,we pass it the transpose,
254        and then solve A^T X = B in MatSolve(). */
255   if (lu->A_dup) {
256     aa = (Mat_SeqAIJ *)(lu->A_dup)->data;
257   } else {
258     aa = (Mat_SeqAIJ *)(A)->data;
259   }
260 #if defined(PETSC_USE_COMPLEX)
261   #if defined(PETSC_USE_REAL_SINGLE)
262   PetscStackCallExternalVoid("SuperLU:cCreate_CompCol_Matrix", cCreate_CompCol_Matrix(&lu->A, A->cmap->n, A->rmap->n, aa->nz, (singlecomplex *)aa->a, aa->j, aa->i, SLU_NC, SLU_C, SLU_GE));
263   #else
264   PetscStackCallExternalVoid("SuperLU:zCreate_CompCol_Matrix", zCreate_CompCol_Matrix(&lu->A, A->cmap->n, A->rmap->n, aa->nz, (doublecomplex *)aa->a, aa->j, aa->i, SLU_NC, SLU_Z, SLU_GE));
265   #endif
266 #else
267   #if defined(PETSC_USE_REAL_SINGLE)
268   PetscStackCallExternalVoid("SuperLU:sCreate_CompCol_Matrix", sCreate_CompCol_Matrix(&lu->A, A->cmap->n, A->rmap->n, aa->nz, aa->a, aa->j, aa->i, SLU_NC, SLU_S, SLU_GE));
269   #else
270   PetscStackCallExternalVoid("SuperLU:dCreate_CompCol_Matrix", dCreate_CompCol_Matrix(&lu->A, A->cmap->n, A->rmap->n, aa->nz, aa->a, aa->j, aa->i, SLU_NC, SLU_D, SLU_GE));
271   #endif
272 #endif
273 
274   /* Numerical factorization */
275   lu->B.ncol = 0; /* Indicate not to solve the system */
276   if (F->factortype == MAT_FACTOR_LU) {
277 #if defined(PETSC_USE_COMPLEX)
278   #if defined(PETSC_USE_REAL_SINGLE)
279     PetscStackCallExternalVoid("SuperLU:cgssvx", cgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
280   #else
281     PetscStackCallExternalVoid("SuperLU:zgssvx", zgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
282   #endif
283 #else
284   #if defined(PETSC_USE_REAL_SINGLE)
285     PetscStackCallExternalVoid("SuperLU:sgssvx", sgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
286   #else
287     PetscStackCallExternalVoid("SuperLU:dgssvx", dgssvx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &ferr, &berr, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
288   #endif
289 #endif
290   } else if (F->factortype == MAT_FACTOR_ILU) {
291     /* Compute the incomplete factorization, condition number and pivot growth */
292 #if defined(PETSC_USE_COMPLEX)
293   #if defined(PETSC_USE_REAL_SINGLE)
294     PetscStackCallExternalVoid("SuperLU:cgsisx", cgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
295   #else
296     PetscStackCallExternalVoid("SuperLU:zgsisx", zgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
297   #endif
298 #else
299   #if defined(PETSC_USE_REAL_SINGLE)
300     PetscStackCallExternalVoid("SuperLU:sgsisx", sgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
301   #else
302     PetscStackCallExternalVoid("SuperLU:dgsisx", dgsisx(&lu->options, &lu->A, lu->perm_c, lu->perm_r, lu->etree, lu->equed, lu->R, lu->C, &lu->L, &lu->U, lu->work, lu->lwork, &lu->B, &lu->X, &lu->rpg, &lu->rcond, &lu->Glu, &lu->mem_usage, &lu->stat, &sinfo));
303   #endif
304 #endif
305   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Factor type not supported");
306   if (!sinfo || sinfo == lu->A.ncol + 1) {
307     if (lu->options.PivotGrowth) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Recip. pivot growth = %e\n", lu->rpg));
308     if (lu->options.ConditionNumber) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Recip. condition number = %e\n", lu->rcond));
309   } else if (sinfo > 0) {
310     if (A->erroriffailure) {
311       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT, "Zero pivot in row %" PetscInt_FMT, sinfo);
312     } else {
313       if (sinfo <= lu->A.ncol) {
314         if (lu->options.ILU_FillTol == 0.0) F->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
315         PetscCall(PetscInfo(F, "Number of zero pivots %" PetscInt_FMT ", ILU_FillTol %g\n", sinfo, lu->options.ILU_FillTol));
316       } else if (sinfo == lu->A.ncol + 1) {
317         /*
318          U is nonsingular, but RCOND is less than machine
319                       precision, meaning that the matrix is singular to
320                       working precision. Nevertheless, the solution and
321                       error bounds are computed because there are a number
322                       of situations where the computed solution can be more
323                       accurate than the value of RCOND would suggest.
324          */
325         PetscCall(PetscInfo(F, "Matrix factor U is nonsingular, but is singular to working precision. The solution is computed. info %" PetscInt_FMT "\n", sinfo));
326       } else { /* sinfo > lu->A.ncol + 1 */
327         F->factorerrortype = MAT_FACTOR_OUTMEMORY;
328         PetscCall(PetscInfo(F, "Number of bytes allocated when memory allocation fails %" PetscInt_FMT "\n", sinfo));
329       }
330     }
331   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "info = %" PetscInt_FMT ", the %" PetscInt_FMT "-th argument in gssvx() had an illegal value", sinfo, -sinfo);
332 
333   if (lu->options.PrintStat) {
334     PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatLUFactorNumeric_SuperLU():\n"));
335     PetscStackCallExternalVoid("SuperLU:StatPrint", StatPrint(&lu->stat));
336     Lstore = (SCformat *)lu->L.Store;
337     Ustore = (NCformat *)lu->U.Store;
338     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  No of nonzeros in factor L = %" PetscInt_FMT "\n", Lstore->nnz));
339     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  No of nonzeros in factor U = %" PetscInt_FMT "\n", Ustore->nnz));
340     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  No of nonzeros in L+U = %" PetscInt_FMT "\n", Lstore->nnz + Ustore->nnz - lu->A.ncol));
341     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  L\\U MB %.3f\ttotal MB needed %.3f\n", lu->mem_usage.for_lu / 1e6, lu->mem_usage.total_needed / 1e6));
342   }
343 
344   lu->flg                = SAME_NONZERO_PATTERN;
345   F->ops->solve          = MatSolve_SuperLU;
346   F->ops->solvetranspose = MatSolveTranspose_SuperLU;
347   F->ops->matsolve       = NULL;
348   PetscFunctionReturn(PETSC_SUCCESS);
349 }
350 
351 static PetscErrorCode MatDestroy_SuperLU(Mat A)
352 {
353   Mat_SuperLU *lu = (Mat_SuperLU *)A->data;
354 
355   PetscFunctionBegin;
356   if (lu->CleanUpSuperLU) { /* Free the SuperLU datastructures */
357     PetscStackCallExternalVoid("SuperLU:Destroy_SuperMatrix_Store", Destroy_SuperMatrix_Store(&lu->A));
358     if (lu->lwork >= 0) {
359       PetscStackCallExternalVoid("SuperLU:Destroy_SuperNode_Matrix", Destroy_SuperNode_Matrix(&lu->L));
360       PetscStackCallExternalVoid("SuperLU:Destroy_CompCol_Matrix", Destroy_CompCol_Matrix(&lu->U));
361     }
362   }
363   PetscStackCallExternalVoid("SuperLU:Destroy_SuperMatrix_Store", Destroy_SuperMatrix_Store(&lu->B));
364   PetscStackCallExternalVoid("SuperLU:Destroy_SuperMatrix_Store", Destroy_SuperMatrix_Store(&lu->X));
365   PetscStackCallExternalVoid("SuperLU:StatFree", StatFree(&lu->stat));
366   PetscCall(PetscFree(lu->etree));
367   PetscCall(PetscFree(lu->perm_r));
368   PetscCall(PetscFree(lu->perm_c));
369   PetscCall(PetscFree(lu->R));
370   PetscCall(PetscFree(lu->C));
371   PetscCall(PetscFree(lu->rhs_dup));
372   PetscCall(MatDestroy(&lu->A_dup));
373   PetscCall(PetscFree(A->data));
374 
375   /* clear composed functions */
376   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL));
377   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSuperluSetILUDropTol_C", NULL));
378   PetscFunctionReturn(PETSC_SUCCESS);
379 }
380 
381 static PetscErrorCode MatView_SuperLU(Mat A, PetscViewer viewer)
382 {
383   PetscBool         iascii;
384   PetscViewerFormat format;
385 
386   PetscFunctionBegin;
387   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
388   if (iascii) {
389     PetscCall(PetscViewerGetFormat(viewer, &format));
390     if (format == PETSC_VIEWER_ASCII_INFO) PetscCall(MatView_Info_SuperLU(A, viewer));
391   }
392   PetscFunctionReturn(PETSC_SUCCESS);
393 }
394 
395 static PetscErrorCode MatLUFactorSymbolic_SuperLU(Mat F, Mat A, IS r, IS c, const MatFactorInfo *info)
396 {
397   Mat_SuperLU *lu = (Mat_SuperLU *)(F->data);
398   PetscInt     indx;
399   PetscBool    flg, set;
400   PetscReal    real_input;
401   const char  *colperm[]    = {"NATURAL", "MMD_ATA", "MMD_AT_PLUS_A", "COLAMD"}; /* MY_PERMC - not supported by the petsc interface yet */
402   const char  *iterrefine[] = {"NOREFINE", "SINGLE", "DOUBLE", "EXTRA"};
403   const char  *rowperm[]    = {"NOROWPERM", "LargeDiag"}; /* MY_PERMC - not supported by the petsc interface yet */
404 
405   PetscFunctionBegin;
406   /* Set options to F */
407   PetscOptionsBegin(PetscObjectComm((PetscObject)F), ((PetscObject)F)->prefix, "SuperLU Options", "Mat");
408   PetscCall(PetscOptionsBool("-mat_superlu_equil", "Equil", "None", (PetscBool)lu->options.Equil, (PetscBool *)&lu->options.Equil, NULL));
409   PetscCall(PetscOptionsEList("-mat_superlu_colperm", "ColPerm", "None", colperm, 4, colperm[3], &indx, &flg));
410   if (flg) lu->options.ColPerm = (colperm_t)indx;
411   PetscCall(PetscOptionsEList("-mat_superlu_iterrefine", "IterRefine", "None", iterrefine, 4, iterrefine[0], &indx, &flg));
412   if (flg) lu->options.IterRefine = (IterRefine_t)indx;
413   PetscCall(PetscOptionsBool("-mat_superlu_symmetricmode", "SymmetricMode", "None", (PetscBool)lu->options.SymmetricMode, &flg, &set));
414   if (set && flg) lu->options.SymmetricMode = YES;
415   PetscCall(PetscOptionsReal("-mat_superlu_diagpivotthresh", "DiagPivotThresh", "None", lu->options.DiagPivotThresh, &real_input, &flg));
416   if (flg) lu->options.DiagPivotThresh = (double)real_input;
417   PetscCall(PetscOptionsBool("-mat_superlu_pivotgrowth", "PivotGrowth", "None", (PetscBool)lu->options.PivotGrowth, &flg, &set));
418   if (set && flg) lu->options.PivotGrowth = YES;
419   PetscCall(PetscOptionsBool("-mat_superlu_conditionnumber", "ConditionNumber", "None", (PetscBool)lu->options.ConditionNumber, &flg, &set));
420   if (set && flg) lu->options.ConditionNumber = YES;
421   PetscCall(PetscOptionsEList("-mat_superlu_rowperm", "rowperm", "None", rowperm, 2, rowperm[lu->options.RowPerm], &indx, &flg));
422   if (flg) lu->options.RowPerm = (rowperm_t)indx;
423   PetscCall(PetscOptionsBool("-mat_superlu_replacetinypivot", "ReplaceTinyPivot", "None", (PetscBool)lu->options.ReplaceTinyPivot, &flg, &set));
424   if (set && flg) lu->options.ReplaceTinyPivot = YES;
425   PetscCall(PetscOptionsBool("-mat_superlu_printstat", "PrintStat", "None", (PetscBool)lu->options.PrintStat, &flg, &set));
426   if (set && flg) lu->options.PrintStat = YES;
427   PetscCall(PetscOptionsInt("-mat_superlu_lwork", "size of work array in bytes used by factorization", "None", lu->lwork, &lu->lwork, NULL));
428   if (lu->lwork > 0) {
429     /* lwork is in bytes, hence PetscMalloc() is used here, not PetscMalloc1()*/
430     PetscCall(PetscMalloc(lu->lwork, &lu->work));
431   } else if (lu->lwork != 0 && lu->lwork != -1) {
432     PetscCall(PetscPrintf(PETSC_COMM_SELF, "   Warning: lwork %" PetscInt_FMT " is not supported by SUPERLU. The default lwork=0 is used.\n", lu->lwork));
433     lu->lwork = 0;
434   }
435   /* ilu options */
436   PetscCall(PetscOptionsReal("-mat_superlu_ilu_droptol", "ILU_DropTol", "None", lu->options.ILU_DropTol, &real_input, &flg));
437   if (flg) lu->options.ILU_DropTol = (double)real_input;
438   PetscCall(PetscOptionsReal("-mat_superlu_ilu_filltol", "ILU_FillTol", "None", lu->options.ILU_FillTol, &real_input, &flg));
439   if (flg) lu->options.ILU_FillTol = (double)real_input;
440   PetscCall(PetscOptionsReal("-mat_superlu_ilu_fillfactor", "ILU_FillFactor", "None", lu->options.ILU_FillFactor, &real_input, &flg));
441   if (flg) lu->options.ILU_FillFactor = (double)real_input;
442   PetscCall(PetscOptionsInt("-mat_superlu_ilu_droprull", "ILU_DropRule", "None", lu->options.ILU_DropRule, &lu->options.ILU_DropRule, NULL));
443   PetscCall(PetscOptionsInt("-mat_superlu_ilu_norm", "ILU_Norm", "None", lu->options.ILU_Norm, &indx, &flg));
444   if (flg) lu->options.ILU_Norm = (norm_t)indx;
445   PetscCall(PetscOptionsInt("-mat_superlu_ilu_milu", "ILU_MILU", "None", lu->options.ILU_MILU, &indx, &flg));
446   if (flg) lu->options.ILU_MILU = (milu_t)indx;
447   PetscOptionsEnd();
448 
449   lu->flg                 = DIFFERENT_NONZERO_PATTERN;
450   lu->CleanUpSuperLU      = PETSC_TRUE;
451   F->ops->lufactornumeric = MatLUFactorNumeric_SuperLU;
452 
453   /* if we are here, the nonzero pattern has changed unless the user explicitly called MatLUFactorSymbolic */
454   PetscCall(MatDestroy(&lu->A_dup));
455   if (lu->needconversion) PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &lu->A_dup));
456   if (lu->options.Equil == YES && !lu->A_dup) { /* superlu overwrites input matrix and rhs when Equil is used, thus create A_dup to keep user's A unchanged */
457     PetscCall(MatDuplicate_SeqAIJ(A, MAT_COPY_VALUES, &lu->A_dup));
458   }
459   PetscFunctionReturn(PETSC_SUCCESS);
460 }
461 
462 static PetscErrorCode MatSuperluSetILUDropTol_SuperLU(Mat F, PetscReal dtol)
463 {
464   Mat_SuperLU *lu = (Mat_SuperLU *)F->data;
465 
466   PetscFunctionBegin;
467   lu->options.ILU_DropTol = dtol;
468   PetscFunctionReturn(PETSC_SUCCESS);
469 }
470 
471 /*@
472   MatSuperluSetILUDropTol - Set SuperLU ILU drop tolerance
473 
474   Logically Collective
475 
476   Input Parameters:
477 + F    - the factored matrix obtained by calling `MatGetFactor()`
478 - dtol - drop tolerance
479 
480   Options Database Key:
481 . -mat_superlu_ilu_droptol <dtol> - the drop tolerance
482 
483   Level: beginner
484 
485   References:
486 .  * - SuperLU Users' Guide
487 
488 .seealso: [](ch_matrices), `Mat`, `MatGetFactor()`, `MATSOLVERSUPERLU`
489 @*/
490 PetscErrorCode MatSuperluSetILUDropTol(Mat F, PetscReal dtol)
491 {
492   PetscFunctionBegin;
493   PetscValidHeaderSpecific(F, MAT_CLASSID, 1);
494   PetscValidLogicalCollectiveReal(F, dtol, 2);
495   PetscTryMethod(F, "MatSuperluSetILUDropTol_C", (Mat, PetscReal), (F, dtol));
496   PetscFunctionReturn(PETSC_SUCCESS);
497 }
498 
499 static PetscErrorCode MatFactorGetSolverType_seqaij_superlu(Mat A, MatSolverType *type)
500 {
501   PetscFunctionBegin;
502   *type = MATSOLVERSUPERLU;
503   PetscFunctionReturn(PETSC_SUCCESS);
504 }
505 
506 /*MC
507   MATSOLVERSUPERLU = "superlu" - A solver package providing solvers LU and ILU for sequential matrices
508   via the external package SuperLU.
509 
510   Use `./configure --download-superlu` to have PETSc installed with SuperLU
511 
512   Use `-pc_type lu` `-pc_factor_mat_solver_type superlu` to use this direct solver
513 
514   Options Database Keys:
515 + -mat_superlu_equil <FALSE>            - Equil (None)
516 . -mat_superlu_colperm <COLAMD>         - (choose one of) `NATURAL`, `MMD_ATA MMD_AT_PLUS_A`, `COLAMD`
517 . -mat_superlu_iterrefine <NOREFINE>    - (choose one of) `NOREFINE`, `SINGLE`, `DOUBLE`, `EXTRA`
518 . -mat_superlu_symmetricmode: <FALSE>   - SymmetricMode (None)
519 . -mat_superlu_diagpivotthresh <1>      - DiagPivotThresh (None)
520 . -mat_superlu_pivotgrowth <FALSE>      - PivotGrowth (None)
521 . -mat_superlu_conditionnumber <FALSE>  - ConditionNumber (None)
522 . -mat_superlu_rowperm <NOROWPERM>      - (choose one of) `NOROWPERM`, `LargeDiag`
523 . -mat_superlu_replacetinypivot <FALSE> - ReplaceTinyPivot (None)
524 . -mat_superlu_printstat <FALSE>        - PrintStat (None)
525 . -mat_superlu_lwork <0>                - size of work array in bytes used by factorization (None)
526 . -mat_superlu_ilu_droptol <0>          - ILU_DropTol (None)
527 . -mat_superlu_ilu_filltol <0>          - ILU_FillTol (None)
528 . -mat_superlu_ilu_fillfactor <0>       - ILU_FillFactor (None)
529 . -mat_superlu_ilu_droprull <0>         - ILU_DropRule (None)
530 . -mat_superlu_ilu_norm <0>             - ILU_Norm (None)
531 - -mat_superlu_ilu_milu <0>             - ILU_MILU (None)
532 
533    Level: beginner
534 
535    Notes:
536     Do not confuse this with `MATSOLVERSUPERLU_DIST` which is for parallel sparse solves
537 
538     Cannot use ordering provided by PETSc, provides its own.
539 
540 .seealso: [](ch_matrices), `Mat`, `PCLU`, `PCILU`, `MATSOLVERSUPERLU_DIST`, `MATSOLVERMUMPS`, `PCFactorSetMatSolverType()`, `MatSolverType`
541 M*/
542 
543 static PetscErrorCode MatGetFactor_seqaij_superlu(Mat A, MatFactorType ftype, Mat *F)
544 {
545   Mat          B;
546   Mat_SuperLU *lu;
547   PetscInt     m = A->rmap->n, n = A->cmap->n;
548 
549   PetscFunctionBegin;
550   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
551   PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, PETSC_DETERMINE, PETSC_DETERMINE));
552   PetscCall(PetscStrallocpy("superlu", &((PetscObject)B)->type_name));
553   PetscCall(MatSetUp(B));
554   B->trivialsymbolic = PETSC_TRUE;
555   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU) {
556     B->ops->lufactorsymbolic  = MatLUFactorSymbolic_SuperLU;
557     B->ops->ilufactorsymbolic = MatLUFactorSymbolic_SuperLU;
558   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Factor type not supported");
559 
560   PetscCall(PetscFree(B->solvertype));
561   PetscCall(PetscStrallocpy(MATSOLVERSUPERLU, &B->solvertype));
562 
563   B->ops->getinfo = MatGetInfo_External;
564   B->ops->destroy = MatDestroy_SuperLU;
565   B->ops->view    = MatView_SuperLU;
566   B->factortype   = ftype;
567   B->assembled    = PETSC_TRUE; /* required by -ksp_view */
568   B->preallocated = PETSC_TRUE;
569 
570   PetscCall(PetscNew(&lu));
571 
572   if (ftype == MAT_FACTOR_LU) {
573     set_default_options(&lu->options);
574     /* Comments from SuperLU_4.0/SRC/dgssvx.c:
575       "Whether or not the system will be equilibrated depends on the
576        scaling of the matrix A, but if equilibration is used, A is
577        overwritten by diag(R)*A*diag(C) and B by diag(R)*B
578        (if options->Trans=NOTRANS) or diag(C)*B (if options->Trans = TRANS or CONJ)."
579      We set 'options.Equil = NO' as default because additional space is needed for it.
580     */
581     lu->options.Equil = NO;
582   } else if (ftype == MAT_FACTOR_ILU) {
583     /* Set the default input options of ilu: */
584     PetscStackCallExternalVoid("SuperLU:ilu_set_default_options", ilu_set_default_options(&lu->options));
585   }
586   lu->options.PrintStat = NO;
587 
588   /* Initialize the statistics variables. */
589   PetscStackCallExternalVoid("SuperLU:StatInit", StatInit(&lu->stat));
590   lu->lwork = 0; /* allocate space internally by system malloc */
591 
592   /* Allocate spaces (notice sizes are for the transpose) */
593   PetscCall(PetscMalloc1(m, &lu->etree));
594   PetscCall(PetscMalloc1(n, &lu->perm_r));
595   PetscCall(PetscMalloc1(m, &lu->perm_c));
596   PetscCall(PetscMalloc1(n, &lu->R));
597   PetscCall(PetscMalloc1(m, &lu->C));
598 
599   /* create rhs and solution x without allocate space for .Store */
600 #if defined(PETSC_USE_COMPLEX)
601   #if defined(PETSC_USE_REAL_SINGLE)
602   PetscStackCallExternalVoid("SuperLU:cCreate_Dense_Matrix(", cCreate_Dense_Matrix(&lu->B, m, 1, NULL, m, SLU_DN, SLU_C, SLU_GE));
603   PetscStackCallExternalVoid("SuperLU:cCreate_Dense_Matrix(", cCreate_Dense_Matrix(&lu->X, m, 1, NULL, m, SLU_DN, SLU_C, SLU_GE));
604   #else
605   PetscStackCallExternalVoid("SuperLU:zCreate_Dense_Matrix", zCreate_Dense_Matrix(&lu->B, m, 1, NULL, m, SLU_DN, SLU_Z, SLU_GE));
606   PetscStackCallExternalVoid("SuperLU:zCreate_Dense_Matrix", zCreate_Dense_Matrix(&lu->X, m, 1, NULL, m, SLU_DN, SLU_Z, SLU_GE));
607   #endif
608 #else
609   #if defined(PETSC_USE_REAL_SINGLE)
610   PetscStackCallExternalVoid("SuperLU:sCreate_Dense_Matrix", sCreate_Dense_Matrix(&lu->B, m, 1, NULL, m, SLU_DN, SLU_S, SLU_GE));
611   PetscStackCallExternalVoid("SuperLU:sCreate_Dense_Matrix", sCreate_Dense_Matrix(&lu->X, m, 1, NULL, m, SLU_DN, SLU_S, SLU_GE));
612   #else
613   PetscStackCallExternalVoid("SuperLU:dCreate_Dense_Matrix", dCreate_Dense_Matrix(&lu->B, m, 1, NULL, m, SLU_DN, SLU_D, SLU_GE));
614   PetscStackCallExternalVoid("SuperLU:dCreate_Dense_Matrix", dCreate_Dense_Matrix(&lu->X, m, 1, NULL, m, SLU_DN, SLU_D, SLU_GE));
615   #endif
616 #endif
617 
618   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_seqaij_superlu));
619   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSuperluSetILUDropTol_C", MatSuperluSetILUDropTol_SuperLU));
620   B->data = lu;
621 
622   *F = B;
623   PetscFunctionReturn(PETSC_SUCCESS);
624 }
625 
626 static PetscErrorCode MatGetFactor_seqsell_superlu(Mat A, MatFactorType ftype, Mat *F)
627 {
628   Mat_SuperLU *lu;
629 
630   PetscFunctionBegin;
631   PetscCall(MatGetFactor_seqaij_superlu(A, ftype, F));
632   lu                 = (Mat_SuperLU *)((*F)->data);
633   lu->needconversion = PETSC_TRUE;
634   PetscFunctionReturn(PETSC_SUCCESS);
635 }
636 
637 PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_SuperLU(void)
638 {
639   PetscFunctionBegin;
640   PetscCall(MatSolverTypeRegister(MATSOLVERSUPERLU, MATSEQAIJ, MAT_FACTOR_LU, MatGetFactor_seqaij_superlu));
641   PetscCall(MatSolverTypeRegister(MATSOLVERSUPERLU, MATSEQAIJ, MAT_FACTOR_ILU, MatGetFactor_seqaij_superlu));
642   PetscCall(MatSolverTypeRegister(MATSOLVERSUPERLU, MATSEQSELL, MAT_FACTOR_LU, MatGetFactor_seqsell_superlu));
643   PetscCall(MatSolverTypeRegister(MATSOLVERSUPERLU, MATSEQSELL, MAT_FACTOR_ILU, MatGetFactor_seqsell_superlu));
644   PetscFunctionReturn(PETSC_SUCCESS);
645 }
646