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