xref: /petsc/src/ksp/pc/impls/factor/lu/lu.c (revision 3dd83b3843bef2e44404f8ac40307e9aa6509644)
1 
2 /*
3    Defines a direct factorization preconditioner for any Mat implementation
4    Note: this need not be consided a preconditioner since it supplies
5          a direct solver.
6 */
7 
8 #include <../src/ksp/pc/impls/factor/lu/lu.h>  /*I "petscpc.h" I*/
9 
10 
11 #undef __FUNCT__
12 #define __FUNCT__ "PCFactorReorderForNonzeroDiagonal_LU"
13 PetscErrorCode PCFactorReorderForNonzeroDiagonal_LU(PC pc,PetscReal z)
14 {
15   PC_LU *lu = (PC_LU*)pc->data;
16 
17   PetscFunctionBegin;
18   lu->nonzerosalongdiagonal = PETSC_TRUE;
19   if (z == PETSC_DECIDE) lu->nonzerosalongdiagonaltol = 1.e-10;
20   else lu->nonzerosalongdiagonaltol = z;
21   PetscFunctionReturn(0);
22 }
23 
24 #undef __FUNCT__
25 #define __FUNCT__ "PCFactorSetReuseOrdering_LU"
26 PetscErrorCode PCFactorSetReuseOrdering_LU(PC pc,PetscBool flag)
27 {
28   PC_LU *lu = (PC_LU*)pc->data;
29 
30   PetscFunctionBegin;
31   lu->reuseordering = flag;
32   PetscFunctionReturn(0);
33 }
34 
35 #undef __FUNCT__
36 #define __FUNCT__ "PCFactorSetReuseFill_LU"
37 PetscErrorCode PCFactorSetReuseFill_LU(PC pc,PetscBool flag)
38 {
39   PC_LU *lu = (PC_LU*)pc->data;
40 
41   PetscFunctionBegin;
42   lu->reusefill = flag;
43   PetscFunctionReturn(0);
44 }
45 
46 #undef __FUNCT__
47 #define __FUNCT__ "PCSetFromOptions_LU"
48 static PetscErrorCode PCSetFromOptions_LU(PetscOptionItems *PetscOptionsObject,PC pc)
49 {
50   PC_LU          *lu = (PC_LU*)pc->data;
51   PetscErrorCode ierr;
52   PetscBool      flg = PETSC_FALSE;
53   PetscReal      tol;
54 
55   PetscFunctionBegin;
56   ierr = PetscOptionsHead(PetscOptionsObject,"LU options");CHKERRQ(ierr);
57   ierr = PCSetFromOptions_Factor(PetscOptionsObject,pc);CHKERRQ(ierr);
58 
59   ierr = PetscOptionsName("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",&flg);CHKERRQ(ierr);
60   if (flg) {
61     tol  = PETSC_DECIDE;
62     ierr = PetscOptionsReal("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",lu->nonzerosalongdiagonaltol,&tol,0);CHKERRQ(ierr);
63     ierr = PCFactorReorderForNonzeroDiagonal(pc,tol);CHKERRQ(ierr);
64   }
65   ierr = PetscOptionsTail();CHKERRQ(ierr);
66   PetscFunctionReturn(0);
67 }
68 
69 #undef __FUNCT__
70 #define __FUNCT__ "PCView_LU"
71 static PetscErrorCode PCView_LU(PC pc,PetscViewer viewer)
72 {
73   PC_LU          *lu = (PC_LU*)pc->data;
74   PetscErrorCode ierr;
75   PetscBool      iascii;
76 
77   PetscFunctionBegin;
78   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
79   if (iascii) {
80     if (lu->inplace) {
81       ierr = PetscViewerASCIIPrintf(viewer,"  LU: in-place factorization\n");CHKERRQ(ierr);
82     } else {
83       ierr = PetscViewerASCIIPrintf(viewer,"  LU: out-of-place factorization\n");CHKERRQ(ierr);
84     }
85 
86     if (lu->reusefill)    {ierr = PetscViewerASCIIPrintf(viewer,"       Reusing fill from past factorization\n");CHKERRQ(ierr);}
87     if (lu->reuseordering) {ierr = PetscViewerASCIIPrintf(viewer,"       Reusing reordering from past factorization\n");CHKERRQ(ierr);}
88   }
89   ierr = PCView_Factor(pc,viewer);CHKERRQ(ierr);
90   PetscFunctionReturn(0);
91 }
92 
93 #undef __FUNCT__
94 #define __FUNCT__ "PCSetUp_LU"
95 static PetscErrorCode PCSetUp_LU(PC pc)
96 {
97   PetscErrorCode         ierr;
98   PC_LU                  *dir = (PC_LU*)pc->data;
99   const MatSolverPackage stype;
100 
101   PetscFunctionBegin;
102   pc->failedreason = PC_NOERROR;
103   if (dir->reusefill && pc->setupcalled) ((PC_Factor*)dir)->info.fill = dir->actualfill;
104 
105   ierr = MatSetErrorIfFailure(pc->pmat,pc->erroriffailure);CHKERRQ(ierr);
106   if (dir->inplace) {
107     if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
108     ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
109     ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
110     if (dir->row) {
111       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
112       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
113     }
114     ierr = MatLUFactor(pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
115     if (pc->pmat->errortype) { /* Factor() fails */
116       pc->failedreason = (PCFailedReason)pc->pmat->errortype;
117       PetscFunctionReturn(0);
118     }
119 
120     ((PC_Factor*)dir)->fact = pc->pmat;
121   } else {
122     MatInfo info;
123     Mat     F;
124     if (!pc->setupcalled) {
125       ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
126       if (dir->nonzerosalongdiagonal) {
127         ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr);
128       }
129       if (dir->row) {
130         ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
131         ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
132       }
133       if (!((PC_Factor*)dir)->fact) {
134         ierr = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
135       }
136       ierr            = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
137       ierr            = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr);
138       dir->actualfill = info.fill_ratio_needed;
139       ierr            = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr);
140     } else if (pc->flag != SAME_NONZERO_PATTERN) {
141       if (!dir->reuseordering) {
142         if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
143         ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
144         ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
145         if (dir->nonzerosalongdiagonal) {
146           ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr);
147         }
148         if (dir->row) {
149           ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
150           ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
151         }
152       }
153       ierr            = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
154       ierr            = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
155       ierr            = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
156       ierr            = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr);
157       dir->actualfill = info.fill_ratio_needed;
158       ierr            = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr);
159     } else {
160       F = ((PC_Factor*)dir)->fact;
161       if ((PCFailedReason)F->errortype == PC_FACTOR_NUMERIC_ZEROPIVOT) {
162         F->errortype     = MAT_FACTOR_NOERROR;
163         pc->failedreason = (PCFailedReason)F->errortype;
164       }
165     }
166     F = ((PC_Factor*)dir)->fact;
167     if (F->errortype) { /* FactorSymbolic() fails */
168       pc->failedreason = (PCFailedReason)F->errortype;
169       PetscFunctionReturn(0);
170     }
171 
172     ierr = MatLUFactorNumeric(((PC_Factor*)dir)->fact,pc->pmat,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
173     if (F->errortype) { /* FactorNumeric() fails */
174       pc->failedreason = (PCFailedReason)F->errortype;
175     }
176 
177   }
178 
179   ierr = PCFactorGetMatSolverPackage(pc,&stype);CHKERRQ(ierr);
180   if (!stype) {
181     ierr = PCFactorSetMatSolverPackage(pc,((PC_Factor*)dir)->fact->solvertype);CHKERRQ(ierr);
182   }
183   PetscFunctionReturn(0);
184 }
185 
186 #undef __FUNCT__
187 #define __FUNCT__ "PCReset_LU"
188 static PetscErrorCode PCReset_LU(PC pc)
189 {
190   PC_LU          *dir = (PC_LU*)pc->data;
191   PetscErrorCode ierr;
192 
193   PetscFunctionBegin;
194   if (!dir->inplace && ((PC_Factor*)dir)->fact) {ierr = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr);}
195   if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
196   ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
197   PetscFunctionReturn(0);
198 }
199 
200 #undef __FUNCT__
201 #define __FUNCT__ "PCDestroy_LU"
202 static PetscErrorCode PCDestroy_LU(PC pc)
203 {
204   PC_LU          *dir = (PC_LU*)pc->data;
205   PetscErrorCode ierr;
206 
207   PetscFunctionBegin;
208   ierr = PCReset_LU(pc);CHKERRQ(ierr);
209   ierr = PetscFree(((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
210   ierr = PetscFree(((PC_Factor*)dir)->solvertype);CHKERRQ(ierr);
211   ierr = PetscFree(pc->data);CHKERRQ(ierr);
212   PetscFunctionReturn(0);
213 }
214 
215 #undef __FUNCT__
216 #define __FUNCT__ "PCApply_LU"
217 static PetscErrorCode PCApply_LU(PC pc,Vec x,Vec y)
218 {
219   PC_LU          *dir = (PC_LU*)pc->data;
220   PetscErrorCode ierr;
221 
222   PetscFunctionBegin;
223   if (dir->inplace) {
224     ierr = MatSolve(pc->pmat,x,y);CHKERRQ(ierr);
225   } else {
226     ierr = MatSolve(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr);
227   }
228   PetscFunctionReturn(0);
229 }
230 
231 #undef __FUNCT__
232 #define __FUNCT__ "PCApplyTranspose_LU"
233 static PetscErrorCode PCApplyTranspose_LU(PC pc,Vec x,Vec y)
234 {
235   PC_LU          *dir = (PC_LU*)pc->data;
236   PetscErrorCode ierr;
237 
238   PetscFunctionBegin;
239   if (dir->inplace) {
240     ierr = MatSolveTranspose(pc->pmat,x,y);CHKERRQ(ierr);
241   } else {
242     ierr = MatSolveTranspose(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr);
243   }
244   PetscFunctionReturn(0);
245 }
246 
247 /* -----------------------------------------------------------------------------------*/
248 
249 #undef __FUNCT__
250 #define __FUNCT__ "PCFactorSetUseInPlace_LU"
251 PetscErrorCode  PCFactorSetUseInPlace_LU(PC pc,PetscBool flg)
252 {
253   PC_LU *dir = (PC_LU*)pc->data;
254 
255   PetscFunctionBegin;
256   dir->inplace = flg;
257   PetscFunctionReturn(0);
258 }
259 
260 #undef __FUNCT__
261 #define __FUNCT__ "PCFactorGetUseInPlace_LU"
262 PetscErrorCode  PCFactorGetUseInPlace_LU(PC pc,PetscBool *flg)
263 {
264   PC_LU *dir = (PC_LU*)pc->data;
265 
266   PetscFunctionBegin;
267   *flg = dir->inplace;
268   PetscFunctionReturn(0);
269 }
270 
271 /* ------------------------------------------------------------------------ */
272 
273 /*MC
274    PCLU - Uses a direct solver, based on LU factorization, as a preconditioner
275 
276    Options Database Keys:
277 +  -pc_factor_reuse_ordering - Activate PCFactorSetReuseOrdering()
278 .  -pc_factor_mat_solver_package - Actives PCFactorSetMatSolverPackage() to choose the direct solver, like superlu
279 .  -pc_factor_reuse_fill - Activates PCFactorSetReuseFill()
280 .  -pc_factor_fill <fill> - Sets fill amount
281 .  -pc_factor_in_place - Activates in-place factorization
282 .  -pc_factor_mat_ordering_type <nd,rcm,...> - Sets ordering routine
283 .  -pc_factor_pivot_in_blocks <true,false> - allow pivoting within the small blocks during factorization (may increase
284                                          stability of factorization.
285 .  -pc_factor_shift_type <shifttype> - Sets shift type or PETSC_DECIDE for the default; use '-help' for a list of available types
286 .  -pc_factor_shift_amount <shiftamount> - Sets shift amount or PETSC_DECIDE for the default
287 -   -pc_factor_nonzeros_along_diagonal - permutes the rows and columns to try to put nonzero value along the
288         diagonal.
289 
290    Notes: Not all options work for all matrix formats
291           Run with -help to see additional options for particular matrix formats or factorization
292           algorithms
293 
294    Level: beginner
295 
296    Concepts: LU factorization, direct solver
297 
298    Notes: Usually this will compute an "exact" solution in one iteration and does
299           not need a Krylov method (i.e. you can use -ksp_type preonly, or
300           KSPSetType(ksp,KSPPREONLY) for the Krylov method
301 
302 .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
303            PCILU, PCCHOLESKY, PCICC, PCFactorSetReuseOrdering(), PCFactorSetReuseFill(), PCFactorGetMatrix(),
304            PCFactorSetFill(), PCFactorSetUseInPlace(), PCFactorSetMatOrderingType(), PCFactorSetColumnPivot(),
305            PCFactorSetPivotingInBlocks(),PCFactorSetShiftType(),PCFactorSetShiftAmount()
306            PCFactorReorderForNonzeroDiagonal()
307 M*/
308 
309 #undef __FUNCT__
310 #define __FUNCT__ "PCCreate_LU"
311 PETSC_EXTERN PetscErrorCode PCCreate_LU(PC pc)
312 {
313   PetscErrorCode ierr;
314   PetscMPIInt    size;
315   PC_LU          *dir;
316 
317   PetscFunctionBegin;
318   ierr = PetscNewLog(pc,&dir);CHKERRQ(ierr);
319 
320   ierr = MatFactorInfoInitialize(&((PC_Factor*)dir)->info);CHKERRQ(ierr);
321 
322   ((PC_Factor*)dir)->fact       = NULL;
323   ((PC_Factor*)dir)->factortype = MAT_FACTOR_LU;
324   dir->inplace                  = PETSC_FALSE;
325   dir->nonzerosalongdiagonal    = PETSC_FALSE;
326 
327   ((PC_Factor*)dir)->info.fill          = 5.0;
328   ((PC_Factor*)dir)->info.dtcol         = 1.e-6;  /* default to pivoting; this is only thing PETSc LU supports */
329   ((PC_Factor*)dir)->info.shifttype     = (PetscReal)MAT_SHIFT_NONE;
330   ((PC_Factor*)dir)->info.shiftamount   = 0.0;
331   ((PC_Factor*)dir)->info.zeropivot     = 100.0*PETSC_MACHINE_EPSILON;
332   ((PC_Factor*)dir)->info.pivotinblocks = 1.0;
333   dir->col                              = 0;
334   dir->row                              = 0;
335 
336   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);CHKERRQ(ierr);
337   if (size == 1) {
338     ierr = PetscStrallocpy(MATORDERINGND,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
339   } else {
340     ierr = PetscStrallocpy(MATORDERINGNATURAL,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
341   }
342   dir->reusefill     = PETSC_FALSE;
343   dir->reuseordering = PETSC_FALSE;
344   pc->data           = (void*)dir;
345 
346   pc->ops->reset             = PCReset_LU;
347   pc->ops->destroy           = PCDestroy_LU;
348   pc->ops->apply             = PCApply_LU;
349   pc->ops->applytranspose    = PCApplyTranspose_LU;
350   pc->ops->setup             = PCSetUp_LU;
351   pc->ops->setfromoptions    = PCSetFromOptions_LU;
352   pc->ops->view              = PCView_LU;
353   pc->ops->applyrichardson   = 0;
354   pc->ops->getfactoredmatrix = PCFactorGetMatrix_Factor;
355 
356   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetZeroPivot_C",PCFactorSetZeroPivot_Factor);CHKERRQ(ierr);
357   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetZeroPivot_C",PCFactorGetZeroPivot_Factor);CHKERRQ(ierr);
358   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftType_C",PCFactorSetShiftType_Factor);CHKERRQ(ierr);
359   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetShiftType_C",PCFactorGetShiftType_Factor);CHKERRQ(ierr);
360   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftAmount_C",PCFactorSetShiftAmount_Factor);CHKERRQ(ierr);
361   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetShiftAmount_C",PCFactorGetShiftAmount_Factor);CHKERRQ(ierr);
362   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUpMatSolverPackage_C",PCFactorSetUpMatSolverPackage_Factor);CHKERRQ(ierr);
363   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetMatSolverPackage_C",PCFactorGetMatSolverPackage_Factor);CHKERRQ(ierr);
364   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatSolverPackage_C",PCFactorSetMatSolverPackage_Factor);CHKERRQ(ierr);
365   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetFill_C",PCFactorSetFill_Factor);CHKERRQ(ierr);
366   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUseInPlace_C",PCFactorSetUseInPlace_LU);CHKERRQ(ierr);
367   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetUseInPlace_C",PCFactorGetUseInPlace_LU);CHKERRQ(ierr);
368   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatOrderingType_C",PCFactorSetMatOrderingType_Factor);CHKERRQ(ierr);
369   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseOrdering_C",PCFactorSetReuseOrdering_LU);CHKERRQ(ierr);
370   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseFill_C",PCFactorSetReuseFill_LU);CHKERRQ(ierr);
371   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetColumnPivot_C",PCFactorSetColumnPivot_Factor);CHKERRQ(ierr);
372   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetPivotInBlocks_C",PCFactorSetPivotInBlocks_Factor);CHKERRQ(ierr);
373   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorReorderForNonzeroDiagonal_C",PCFactorReorderForNonzeroDiagonal_LU);CHKERRQ(ierr);
374   PetscFunctionReturn(0);
375 }
376