xref: /petsc/src/ksp/pc/impls/factor/lu/lu.c (revision 7dd42bbaa135494ed32e16fd8b2ca0023653500d)
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   if (dir->reusefill && pc->setupcalled) ((PC_Factor*)dir)->info.fill = dir->actualfill;
103 
104   ierr = MatSetErrorIfFailure(pc->pmat,pc->erroriffailure);CHKERRQ(ierr);
105   if (dir->inplace) {
106     if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
107     ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
108     ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
109     if (dir->row) {
110       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
111       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
112     }
113     ierr = MatLUFactor(pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
114     if (pc->pmat->errortype) { /* Factor() fails */
115       pc->failedreason = (PCFailedReason)pc->pmat->errortype;
116       PetscFunctionReturn(0);
117     }
118 
119     ((PC_Factor*)dir)->fact = pc->pmat;
120   } else {
121     MatInfo info;
122     Mat     F;
123     if (!pc->setupcalled) {
124       ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
125       if (dir->nonzerosalongdiagonal) {
126         ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr);
127       }
128       if (dir->row) {
129         ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
130         ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
131       }
132       if (!((PC_Factor*)dir)->fact) {
133         ierr = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
134       }
135       ierr            = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
136       ierr            = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr);
137       dir->actualfill = info.fill_ratio_needed;
138       ierr            = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr);
139     } else if (pc->flag != SAME_NONZERO_PATTERN) {
140       if (!dir->reuseordering) {
141         if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
142         ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
143         ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr);
144         if (dir->nonzerosalongdiagonal) {
145           ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr);
146         }
147         if (dir->row) {
148           ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr);
149           ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr);
150         }
151       }
152       ierr            = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
153       ierr            = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr);
154       ierr            = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
155       ierr            = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr);
156       dir->actualfill = info.fill_ratio_needed;
157       ierr            = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr);
158     } else {
159       F = ((PC_Factor*)dir)->fact;
160       if ((PCFailedReason)F->errortype == PC_FACTOR_NUMERIC_ZEROPIVOT) {
161         F->errortype     = MAT_FACTOR_NOERROR;
162         pc->failedreason = (PCFailedReason)F->errortype;
163       }
164     }
165     F = ((PC_Factor*)dir)->fact;
166     if (F->errortype) { /* FactorSymbolic() fails */
167       pc->failedreason = (PCFailedReason)F->errortype;
168       PetscFunctionReturn(0);
169     }
170 
171     ierr = MatLUFactorNumeric(((PC_Factor*)dir)->fact,pc->pmat,&((PC_Factor*)dir)->info);CHKERRQ(ierr);
172     if (F->errortype) { /* FactorNumeric() fails */
173       pc->failedreason = (PCFailedReason)F->errortype;
174     }
175 
176   }
177 
178   ierr = PCFactorGetMatSolverPackage(pc,&stype);CHKERRQ(ierr);
179   if (!stype) {
180     ierr = PCFactorSetMatSolverPackage(pc,((PC_Factor*)dir)->fact->solvertype);CHKERRQ(ierr);
181   }
182   PetscFunctionReturn(0);
183 }
184 
185 #undef __FUNCT__
186 #define __FUNCT__ "PCReset_LU"
187 static PetscErrorCode PCReset_LU(PC pc)
188 {
189   PC_LU          *dir = (PC_LU*)pc->data;
190   PetscErrorCode ierr;
191 
192   PetscFunctionBegin;
193   if (!dir->inplace && ((PC_Factor*)dir)->fact) {ierr = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr);}
194   if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);}
195   ierr = ISDestroy(&dir->col);CHKERRQ(ierr);
196   PetscFunctionReturn(0);
197 }
198 
199 #undef __FUNCT__
200 #define __FUNCT__ "PCDestroy_LU"
201 static PetscErrorCode PCDestroy_LU(PC pc)
202 {
203   PC_LU          *dir = (PC_LU*)pc->data;
204   PetscErrorCode ierr;
205 
206   PetscFunctionBegin;
207   ierr = PCReset_LU(pc);CHKERRQ(ierr);
208   ierr = PetscFree(((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
209   ierr = PetscFree(((PC_Factor*)dir)->solvertype);CHKERRQ(ierr);
210   ierr = PetscFree(pc->data);CHKERRQ(ierr);
211   PetscFunctionReturn(0);
212 }
213 
214 #undef __FUNCT__
215 #define __FUNCT__ "PCApply_LU"
216 static PetscErrorCode PCApply_LU(PC pc,Vec x,Vec y)
217 {
218   PC_LU          *dir = (PC_LU*)pc->data;
219   PetscErrorCode ierr;
220 
221   PetscFunctionBegin;
222   if (dir->inplace) {
223     ierr = MatSolve(pc->pmat,x,y);CHKERRQ(ierr);
224   } else {
225     ierr = MatSolve(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr);
226   }
227   PetscFunctionReturn(0);
228 }
229 
230 #undef __FUNCT__
231 #define __FUNCT__ "PCApplyTranspose_LU"
232 static PetscErrorCode PCApplyTranspose_LU(PC pc,Vec x,Vec y)
233 {
234   PC_LU          *dir = (PC_LU*)pc->data;
235   PetscErrorCode ierr;
236 
237   PetscFunctionBegin;
238   if (dir->inplace) {
239     ierr = MatSolveTranspose(pc->pmat,x,y);CHKERRQ(ierr);
240   } else {
241     ierr = MatSolveTranspose(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr);
242   }
243   PetscFunctionReturn(0);
244 }
245 
246 /* -----------------------------------------------------------------------------------*/
247 
248 #undef __FUNCT__
249 #define __FUNCT__ "PCFactorSetUseInPlace_LU"
250 PetscErrorCode  PCFactorSetUseInPlace_LU(PC pc,PetscBool flg)
251 {
252   PC_LU *dir = (PC_LU*)pc->data;
253 
254   PetscFunctionBegin;
255   dir->inplace = flg;
256   PetscFunctionReturn(0);
257 }
258 
259 #undef __FUNCT__
260 #define __FUNCT__ "PCFactorGetUseInPlace_LU"
261 PetscErrorCode  PCFactorGetUseInPlace_LU(PC pc,PetscBool *flg)
262 {
263   PC_LU *dir = (PC_LU*)pc->data;
264 
265   PetscFunctionBegin;
266   *flg = dir->inplace;
267   PetscFunctionReturn(0);
268 }
269 
270 /* ------------------------------------------------------------------------ */
271 
272 /*MC
273    PCLU - Uses a direct solver, based on LU factorization, as a preconditioner
274 
275    Options Database Keys:
276 +  -pc_factor_reuse_ordering - Activate PCFactorSetReuseOrdering()
277 .  -pc_factor_mat_solver_package - Actives PCFactorSetMatSolverPackage() to choose the direct solver, like superlu
278 .  -pc_factor_reuse_fill - Activates PCFactorSetReuseFill()
279 .  -pc_factor_fill <fill> - Sets fill amount
280 .  -pc_factor_in_place - Activates in-place factorization
281 .  -pc_factor_mat_ordering_type <nd,rcm,...> - Sets ordering routine
282 .  -pc_factor_pivot_in_blocks <true,false> - allow pivoting within the small blocks during factorization (may increase
283                                          stability of factorization.
284 .  -pc_factor_shift_type <shifttype> - Sets shift type or PETSC_DECIDE for the default; use '-help' for a list of available types
285 .  -pc_factor_shift_amount <shiftamount> - Sets shift amount or PETSC_DECIDE for the default
286 -   -pc_factor_nonzeros_along_diagonal - permutes the rows and columns to try to put nonzero value along the
287         diagonal.
288 
289    Notes: Not all options work for all matrix formats
290           Run with -help to see additional options for particular matrix formats or factorization
291           algorithms
292 
293    Level: beginner
294 
295    Concepts: LU factorization, direct solver
296 
297    Notes: Usually this will compute an "exact" solution in one iteration and does
298           not need a Krylov method (i.e. you can use -ksp_type preonly, or
299           KSPSetType(ksp,KSPPREONLY) for the Krylov method
300 
301 .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
302            PCILU, PCCHOLESKY, PCICC, PCFactorSetReuseOrdering(), PCFactorSetReuseFill(), PCFactorGetMatrix(),
303            PCFactorSetFill(), PCFactorSetUseInPlace(), PCFactorSetMatOrderingType(), PCFactorSetColumnPivot(),
304            PCFactorSetPivotingInBlocks(),PCFactorSetShiftType(),PCFactorSetShiftAmount()
305            PCFactorReorderForNonzeroDiagonal()
306 M*/
307 
308 #undef __FUNCT__
309 #define __FUNCT__ "PCCreate_LU"
310 PETSC_EXTERN PetscErrorCode PCCreate_LU(PC pc)
311 {
312   PetscErrorCode ierr;
313   PetscMPIInt    size;
314   PC_LU          *dir;
315 
316   PetscFunctionBegin;
317   ierr = PetscNewLog(pc,&dir);CHKERRQ(ierr);
318 
319   ierr = MatFactorInfoInitialize(&((PC_Factor*)dir)->info);CHKERRQ(ierr);
320 
321   ((PC_Factor*)dir)->fact       = NULL;
322   ((PC_Factor*)dir)->factortype = MAT_FACTOR_LU;
323   dir->inplace                  = PETSC_FALSE;
324   dir->nonzerosalongdiagonal    = PETSC_FALSE;
325 
326   ((PC_Factor*)dir)->info.fill          = 5.0;
327   ((PC_Factor*)dir)->info.dtcol         = 1.e-6;  /* default to pivoting; this is only thing PETSc LU supports */
328   ((PC_Factor*)dir)->info.shifttype     = (PetscReal)MAT_SHIFT_NONE;
329   ((PC_Factor*)dir)->info.shiftamount   = 0.0;
330   ((PC_Factor*)dir)->info.zeropivot     = 100.0*PETSC_MACHINE_EPSILON;
331   ((PC_Factor*)dir)->info.pivotinblocks = 1.0;
332   dir->col                              = 0;
333   dir->row                              = 0;
334 
335   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);CHKERRQ(ierr);
336   if (size == 1) {
337     ierr = PetscStrallocpy(MATORDERINGND,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
338   } else {
339     ierr = PetscStrallocpy(MATORDERINGNATURAL,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr);
340   }
341   dir->reusefill     = PETSC_FALSE;
342   dir->reuseordering = PETSC_FALSE;
343   pc->data           = (void*)dir;
344 
345   pc->ops->reset             = PCReset_LU;
346   pc->ops->destroy           = PCDestroy_LU;
347   pc->ops->apply             = PCApply_LU;
348   pc->ops->applytranspose    = PCApplyTranspose_LU;
349   pc->ops->setup             = PCSetUp_LU;
350   pc->ops->setfromoptions    = PCSetFromOptions_LU;
351   pc->ops->view              = PCView_LU;
352   pc->ops->applyrichardson   = 0;
353   pc->ops->getfactoredmatrix = PCFactorGetMatrix_Factor;
354 
355   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUpMatSolverPackage_C",PCFactorSetUpMatSolverPackage_Factor);CHKERRQ(ierr);
356   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetMatSolverPackage_C",PCFactorGetMatSolverPackage_Factor);CHKERRQ(ierr);
357   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatSolverPackage_C",PCFactorSetMatSolverPackage_Factor);CHKERRQ(ierr);
358   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetZeroPivot_C",PCFactorSetZeroPivot_Factor);CHKERRQ(ierr);
359   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftType_C",PCFactorSetShiftType_Factor);CHKERRQ(ierr);
360   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftAmount_C",PCFactorSetShiftAmount_Factor);CHKERRQ(ierr);
361   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetFill_C",PCFactorSetFill_Factor);CHKERRQ(ierr);
362   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUseInPlace_C",PCFactorSetUseInPlace_LU);CHKERRQ(ierr);
363   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetUseInPlace_C",PCFactorGetUseInPlace_LU);CHKERRQ(ierr);
364   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatOrderingType_C",PCFactorSetMatOrderingType_Factor);CHKERRQ(ierr);
365   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseOrdering_C",PCFactorSetReuseOrdering_LU);CHKERRQ(ierr);
366   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseFill_C",PCFactorSetReuseFill_LU);CHKERRQ(ierr);
367   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetColumnPivot_C",PCFactorSetColumnPivot_Factor);CHKERRQ(ierr);
368   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetPivotInBlocks_C",PCFactorSetPivotInBlocks_Factor);CHKERRQ(ierr);
369   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorReorderForNonzeroDiagonal_C",PCFactorReorderForNonzeroDiagonal_LU);CHKERRQ(ierr);
370   PetscFunctionReturn(0);
371 }
372