xref: /petsc/src/ksp/pc/impls/factor/lu/lu.c (revision d4869aaafd4db1b5077f043a252e5f7e7976eb2a)
1 /*
2    Defines a direct factorization preconditioner for any Mat implementation
3    Note: this need not be considered a preconditioner since it supplies
4          a direct solver.
5 */
6 
7 #include <../src/ksp/pc/impls/factor/lu/lu.h> /*I "petscpc.h" I*/
8 
9 static PetscErrorCode PCFactorReorderForNonzeroDiagonal_LU(PC pc, PetscReal z)
10 {
11   PC_LU *lu = (PC_LU *)pc->data;
12 
13   PetscFunctionBegin;
14   lu->nonzerosalongdiagonal = PETSC_TRUE;
15   if (z == (PetscReal)PETSC_DECIDE) lu->nonzerosalongdiagonaltol = 1.e-10;
16   else lu->nonzerosalongdiagonaltol = z;
17   PetscFunctionReturn(PETSC_SUCCESS);
18 }
19 
20 static PetscErrorCode PCSetFromOptions_LU(PC pc, PetscOptionItems PetscOptionsObject)
21 {
22   PC_LU    *lu  = (PC_LU *)pc->data;
23   PetscBool flg = PETSC_FALSE;
24   PetscReal tol;
25 
26   PetscFunctionBegin;
27   PetscOptionsHeadBegin(PetscOptionsObject, "LU options");
28   PetscCall(PCSetFromOptions_Factor(pc, PetscOptionsObject));
29 
30   PetscCall(PetscOptionsName("-pc_factor_nonzeros_along_diagonal", "Reorder to remove zeros from diagonal", "PCFactorReorderForNonzeroDiagonal", &flg));
31   if (flg) {
32     tol = PETSC_DECIDE;
33     PetscCall(PetscOptionsReal("-pc_factor_nonzeros_along_diagonal", "Reorder to remove zeros from diagonal", "PCFactorReorderForNonzeroDiagonal", lu->nonzerosalongdiagonaltol, &tol, NULL));
34     PetscCall(PCFactorReorderForNonzeroDiagonal(pc, tol));
35   }
36   PetscOptionsHeadEnd();
37   PetscFunctionReturn(PETSC_SUCCESS);
38 }
39 
40 static PetscErrorCode PCSetUp_LU(PC pc)
41 {
42   PC_LU         *dir = (PC_LU *)pc->data;
43   MatSolverType  stype;
44   MatFactorError err;
45   const char    *prefix;
46 
47   PetscFunctionBegin;
48   pc->failedreason = PC_NOERROR;
49   if (dir->hdr.reusefill && pc->setupcalled) ((PC_Factor *)dir)->info.fill = dir->hdr.actualfill;
50 
51   PetscCall(PCGetOptionsPrefix(pc, &prefix));
52   PetscCall(MatSetOptionsPrefixFactor(pc->pmat, prefix));
53 
54   PetscCall(MatSetErrorIfFailure(pc->pmat, pc->erroriffailure));
55   if (dir->hdr.inplace) {
56     MatFactorType ftype;
57 
58     PetscCall(MatGetFactorType(pc->pmat, &ftype));
59     if (ftype == MAT_FACTOR_NONE) {
60       if (dir->row && dir->col && dir->row != dir->col) PetscCall(ISDestroy(&dir->row));
61       PetscCall(ISDestroy(&dir->col));
62       /* This should only get the ordering if needed, but since MatGetFactor() is not called we can't know if it is needed */
63       PetscCall(PCFactorSetDefaultOrdering_Factor(pc));
64       PetscCall(MatGetOrdering(pc->pmat, ((PC_Factor *)dir)->ordering, &dir->row, &dir->col));
65       PetscCall(MatLUFactor(pc->pmat, dir->row, dir->col, &((PC_Factor *)dir)->info));
66       PetscCall(MatFactorGetError(pc->pmat, &err));
67       if (err) { /* Factor() fails */
68         pc->failedreason = (PCFailedReason)err;
69         PetscFunctionReturn(PETSC_SUCCESS);
70       }
71     }
72     ((PC_Factor *)dir)->fact = pc->pmat;
73   } else {
74     MatInfo info;
75 
76     if (!pc->setupcalled) {
77       PetscBool canuseordering;
78 
79       PetscCall(PCFactorSetUpMatSolverType(pc));
80       PetscCall(MatFactorGetCanUseOrdering(((PC_Factor *)dir)->fact, &canuseordering));
81       if (canuseordering) {
82         PetscCall(PCFactorSetDefaultOrdering_Factor(pc));
83         PetscCall(MatGetOrdering(pc->pmat, ((PC_Factor *)dir)->ordering, &dir->row, &dir->col));
84         if (dir->nonzerosalongdiagonal) PetscCall(MatReorderForNonzeroDiagonal(pc->pmat, dir->nonzerosalongdiagonaltol, dir->row, dir->col));
85       }
86       PetscCall(MatLUFactorSymbolic(((PC_Factor *)dir)->fact, pc->pmat, dir->row, dir->col, &((PC_Factor *)dir)->info));
87       PetscCall(MatGetInfo(((PC_Factor *)dir)->fact, MAT_LOCAL, &info));
88       dir->hdr.actualfill = info.fill_ratio_needed;
89     } else if (pc->flag != SAME_NONZERO_PATTERN) {
90       PetscBool canuseordering;
91 
92       if (!dir->hdr.reuseordering) {
93         PetscCall(MatDestroy(&((PC_Factor *)dir)->fact));
94         PetscCall(PCFactorSetUpMatSolverType(pc));
95         PetscCall(MatFactorGetCanUseOrdering(((PC_Factor *)dir)->fact, &canuseordering));
96         if (canuseordering) {
97           if (dir->row && dir->col && dir->row != dir->col) PetscCall(ISDestroy(&dir->row));
98           PetscCall(ISDestroy(&dir->col));
99           PetscCall(PCFactorSetDefaultOrdering_Factor(pc));
100           PetscCall(MatGetOrdering(pc->pmat, ((PC_Factor *)dir)->ordering, &dir->row, &dir->col));
101           if (dir->nonzerosalongdiagonal) PetscCall(MatReorderForNonzeroDiagonal(pc->pmat, dir->nonzerosalongdiagonaltol, dir->row, dir->col));
102         }
103       }
104       PetscCall(MatLUFactorSymbolic(((PC_Factor *)dir)->fact, pc->pmat, dir->row, dir->col, &((PC_Factor *)dir)->info));
105       PetscCall(MatGetInfo(((PC_Factor *)dir)->fact, MAT_LOCAL, &info));
106       dir->hdr.actualfill = info.fill_ratio_needed;
107     } else {
108       PetscCall(MatFactorGetError(((PC_Factor *)dir)->fact, &err));
109       if (err == MAT_FACTOR_NUMERIC_ZEROPIVOT) {
110         PetscCall(MatFactorClearError(((PC_Factor *)dir)->fact));
111         pc->failedreason = PC_NOERROR;
112       }
113     }
114     PetscCall(MatFactorGetError(((PC_Factor *)dir)->fact, &err));
115     if (err) { /* FactorSymbolic() fails */
116       pc->failedreason = (PCFailedReason)err;
117       PetscFunctionReturn(PETSC_SUCCESS);
118     }
119 
120     PetscCall(MatLUFactorNumeric(((PC_Factor *)dir)->fact, pc->pmat, &((PC_Factor *)dir)->info));
121     PetscCall(MatFactorGetError(((PC_Factor *)dir)->fact, &err));
122     if (err) { /* FactorNumeric() fails */
123       pc->failedreason = (PCFailedReason)err;
124     }
125   }
126 
127   PetscCall(PCFactorGetMatSolverType(pc, &stype));
128   if (!stype) {
129     MatSolverType solverpackage;
130     PetscCall(MatFactorGetSolverType(((PC_Factor *)dir)->fact, &solverpackage));
131     PetscCall(PCFactorSetMatSolverType(pc, solverpackage));
132   }
133   PetscFunctionReturn(PETSC_SUCCESS);
134 }
135 
136 static PetscErrorCode PCReset_LU(PC pc)
137 {
138   PC_LU *dir = (PC_LU *)pc->data;
139 
140   PetscFunctionBegin;
141   if (!dir->hdr.inplace && ((PC_Factor *)dir)->fact) PetscCall(MatDestroy(&((PC_Factor *)dir)->fact));
142   if (dir->row && dir->col && dir->row != dir->col) PetscCall(ISDestroy(&dir->row));
143   PetscCall(ISDestroy(&dir->col));
144   PetscFunctionReturn(PETSC_SUCCESS);
145 }
146 
147 static PetscErrorCode PCDestroy_LU(PC pc)
148 {
149   PC_LU *dir = (PC_LU *)pc->data;
150 
151   PetscFunctionBegin;
152   PetscCall(PCReset_LU(pc));
153   PetscCall(PetscFree(((PC_Factor *)dir)->ordering));
154   PetscCall(PetscFree(((PC_Factor *)dir)->solvertype));
155   PetscCall(PCFactorClearComposedFunctions(pc));
156   PetscCall(PetscFree(pc->data));
157   PetscFunctionReturn(PETSC_SUCCESS);
158 }
159 
160 static PetscErrorCode PCApply_LU(PC pc, Vec x, Vec y)
161 {
162   PC_LU *dir = (PC_LU *)pc->data;
163 
164   PetscFunctionBegin;
165   if (dir->hdr.inplace) {
166     PetscCall(MatSolve(pc->pmat, x, y));
167   } else {
168     PetscCall(MatSolve(((PC_Factor *)dir)->fact, x, y));
169   }
170   PetscFunctionReturn(PETSC_SUCCESS);
171 }
172 
173 static PetscErrorCode PCMatApply_LU(PC pc, Mat X, Mat Y)
174 {
175   PC_LU *dir = (PC_LU *)pc->data;
176 
177   PetscFunctionBegin;
178   if (dir->hdr.inplace) {
179     PetscCall(MatMatSolve(pc->pmat, X, Y));
180   } else {
181     PetscCall(MatMatSolve(((PC_Factor *)dir)->fact, X, Y));
182   }
183   PetscFunctionReturn(PETSC_SUCCESS);
184 }
185 
186 static PetscErrorCode PCApplyTranspose_LU(PC pc, Vec x, Vec y)
187 {
188   PC_LU *dir = (PC_LU *)pc->data;
189 
190   PetscFunctionBegin;
191   if (dir->hdr.inplace) {
192     PetscCall(MatSolveTranspose(pc->pmat, x, y));
193   } else {
194     PetscCall(MatSolveTranspose(((PC_Factor *)dir)->fact, x, y));
195   }
196   PetscFunctionReturn(PETSC_SUCCESS);
197 }
198 
199 static PetscErrorCode PCMatApplyTranspose_LU(PC pc, Mat X, Mat Y)
200 {
201   PC_LU *dir = (PC_LU *)pc->data;
202 
203   PetscFunctionBegin;
204   if (dir->hdr.inplace) {
205     PetscCall(MatMatSolveTranspose(pc->pmat, X, Y));
206   } else {
207     PetscCall(MatMatSolveTranspose(((PC_Factor *)dir)->fact, X, Y));
208   }
209   PetscFunctionReturn(PETSC_SUCCESS);
210 }
211 
212 /*MC
213    PCLU - Uses a direct solver, based on LU factorization, as a preconditioner
214 
215    Options Database Keys:
216 +  -pc_factor_reuse_ordering - Activate `PCFactorSetReuseOrdering()`
217 .  -pc_factor_mat_solver_type - Actives `PCFactorSetMatSolverType()` to choose the direct solver, like superlu
218 .  -pc_factor_reuse_fill - Activates `PCFactorSetReuseFill()`
219 .  -pc_factor_fill <fill> - Sets fill amount
220 .  -pc_factor_in_place - Activates in-place factorization
221 .  -pc_factor_mat_ordering_type <nd,rcm,...> - Sets ordering routine
222 .  -pc_factor_pivot_in_blocks <true,false> - allow pivoting within the small blocks during factorization (may increase
223                                          stability of factorization.
224 .  -pc_factor_shift_type <shifttype> - Sets shift type or -1 for the default; use '-help' for a list of available types
225 .  -pc_factor_shift_amount <shiftamount> - Sets shift amount or -1 for the default
226 .  -pc_factor_nonzeros_along_diagonal - permutes the rows and columns to try to put nonzero value along the diagonal.
227 .  -pc_factor_mat_solver_type <packagename> - use an external package for the solve, see `MatSolverType` for possibilities
228 -  -mat_solvertype_optionname - options for a specific solver package, for example -mat_mumps_cntl_1
229 
230    Level: beginner
231 
232    Notes:
233    Not all options work for all matrix formats
234 
235    Run with `-help` to see additional options for particular matrix formats or factorization algorithms
236 
237    The Cholesky factorization direct solver, `PCCHOLESKY` will be more efficient than `PCLU` for symmetric positive-definite (SPD) matrices
238 
239    Usually this will compute an "exact" solution in one iteration and does
240    not need a Krylov method (i.e. you can use -ksp_type preonly, or
241    `KSPSetType`(ksp,`KSPPREONLY`) for the Krylov method.
242 
243 .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `MatSolverType`, `MatGetFactor()`, `PCQR`, `PCSVD`,
244           `PCILU`, `PCCHOLESKY`, `PCICC`, `PCFactorSetReuseOrdering()`, `PCFactorSetReuseFill()`, `PCFactorGetMatrix()`,
245           `PCFactorSetFill()`, `PCFactorSetUseInPlace()`, `PCFactorSetMatOrderingType()`, `PCFactorSetColumnPivot()`,
246           `PCFactorSetPivotInBlocks()`, `PCFactorSetShiftType()`, `PCFactorSetShiftAmount()`
247           `PCFactorReorderForNonzeroDiagonal()`
248 M*/
249 
250 PETSC_EXTERN PetscErrorCode PCCreate_LU(PC pc)
251 {
252   PC_LU *dir;
253 
254   PetscFunctionBegin;
255   PetscCall(PetscNew(&dir));
256   pc->data = (void *)dir;
257   PetscCall(PCFactorInitialize(pc, MAT_FACTOR_LU));
258   dir->nonzerosalongdiagonal = PETSC_FALSE;
259 
260   ((PC_Factor *)dir)->info.fill      = 5.0;
261   ((PC_Factor *)dir)->info.dtcol     = 1.e-6; /* default to pivoting; this is only thing PETSc LU supports */
262   ((PC_Factor *)dir)->info.shifttype = (PetscReal)MAT_SHIFT_NONE;
263   dir->col                           = NULL;
264   dir->row                           = NULL;
265 
266   pc->ops->reset             = PCReset_LU;
267   pc->ops->destroy           = PCDestroy_LU;
268   pc->ops->apply             = PCApply_LU;
269   pc->ops->matapply          = PCMatApply_LU;
270   pc->ops->applytranspose    = PCApplyTranspose_LU;
271   pc->ops->matapplytranspose = PCMatApplyTranspose_LU;
272   pc->ops->setup             = PCSetUp_LU;
273   pc->ops->setfromoptions    = PCSetFromOptions_LU;
274   pc->ops->view              = PCView_Factor;
275   pc->ops->applyrichardson   = NULL;
276   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFactorReorderForNonzeroDiagonal_C", PCFactorReorderForNonzeroDiagonal_LU));
277   PetscFunctionReturn(PETSC_SUCCESS);
278 }
279