Actual source code: lu.c
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: */
7: #include <../src/ksp/pc/impls/factor/lu/lu.h>
9: static PetscErrorCode PCFactorReorderForNonzeroDiagonal_LU(PC pc, PetscReal z)
10: {
11: PC_LU *lu = (PC_LU *)pc->data;
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: }
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;
26: PetscFunctionBegin;
27: PetscOptionsHeadBegin(PetscOptionsObject, "LU options");
28: PetscCall(PCSetFromOptions_Factor(pc, PetscOptionsObject));
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: }
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;
47: PetscFunctionBegin;
48: pc->failedreason = PC_NOERROR;
49: if (dir->hdr.reusefill && pc->setupcalled) ((PC_Factor *)dir)->info.fill = dir->hdr.actualfill;
51: PetscCall(PCGetOptionsPrefix(pc, &prefix));
52: PetscCall(MatSetOptionsPrefixFactor(pc->pmat, prefix));
54: PetscCall(MatSetErrorIfFailure(pc->pmat, pc->erroriffailure));
55: if (dir->hdr.inplace) {
56: MatFactorType ftype;
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;
76: if (!pc->setupcalled) {
77: PetscBool canuseordering;
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;
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: }
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: }
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: }
136: static PetscErrorCode PCReset_LU(PC pc)
137: {
138: PC_LU *dir = (PC_LU *)pc->data;
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: }
147: static PetscErrorCode PCDestroy_LU(PC pc)
148: {
149: PC_LU *dir = (PC_LU *)pc->data;
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: }
160: static PetscErrorCode PCApply_LU(PC pc, Vec x, Vec y)
161: {
162: PC_LU *dir = (PC_LU *)pc->data;
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: }
173: static PetscErrorCode PCMatApply_LU(PC pc, Mat X, Mat Y)
174: {
175: PC_LU *dir = (PC_LU *)pc->data;
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: }
186: static PetscErrorCode PCApplyTranspose_LU(PC pc, Vec x, Vec y)
187: {
188: PC_LU *dir = (PC_LU *)pc->data;
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: }
199: static PetscErrorCode PCMatApplyTranspose_LU(PC pc, Mat X, Mat Y)
200: {
201: PC_LU *dir = (PC_LU *)pc->data;
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: }
212: /*MC
213: PCLU - Uses a direct solver, based on LU factorization, as a preconditioner
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
230: Level: beginner
232: Notes:
233: Not all options work for all matrix formats
235: Run with `-help` to see additional options for particular matrix formats or factorization algorithms
237: The Cholesky factorization direct solver, `PCCHOLESKY` will be more efficient than `PCLU` for symmetric positive-definite (SPD) matrices
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.
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*/
250: PETSC_EXTERN PetscErrorCode PCCreate_LU(PC pc)
251: {
252: PC_LU *dir;
254: PetscFunctionBegin;
255: PetscCall(PetscNew(&dir));
256: pc->data = (void *)dir;
257: PetscCall(PCFactorInitialize(pc, MAT_FACTOR_LU));
258: dir->nonzerosalongdiagonal = PETSC_FALSE;
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;
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: }