xref: /petsc/src/ksp/pc/impls/factor/icc/icc.c (revision 5f309d014d30c8e30ef8d484fee079cd79b2cbfc)
1 
2 #include <../src/ksp/pc/impls/factor/icc/icc.h>   /*I "petscpc.h" I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "PCSetUp_ICC"
6 static PetscErrorCode PCSetUp_ICC(PC pc)
7 {
8   PC_ICC                 *icc = (PC_ICC*)pc->data;
9   IS                     perm,cperm;
10   PetscErrorCode         ierr;
11   MatInfo                info;
12   const MatSolverPackage stype;
13   MatFactorError         err;
14 
15   PetscFunctionBegin;
16   ierr = MatGetOrdering(pc->pmat, ((PC_Factor*)icc)->ordering,&perm,&cperm);CHKERRQ(ierr);
17 
18   ierr = MatSetErrorIfFailure(pc->pmat,pc->erroriffailure);CHKERRQ(ierr);
19   if (!pc->setupcalled) {
20     if (!((PC_Factor*)icc)->fact) {
21       ierr = MatGetFactor(pc->pmat,((PC_Factor*)icc)->solvertype,MAT_FACTOR_ICC,&((PC_Factor*)icc)->fact);CHKERRQ(ierr);
22     }
23     ierr = MatICCFactorSymbolic(((PC_Factor*)icc)->fact,pc->pmat,perm,&((PC_Factor*)icc)->info);CHKERRQ(ierr);
24   } else if (pc->flag != SAME_NONZERO_PATTERN) {
25     ierr = MatDestroy(&((PC_Factor*)icc)->fact);CHKERRQ(ierr);
26     ierr = MatGetFactor(pc->pmat,((PC_Factor*)icc)->solvertype,MAT_FACTOR_ICC,&((PC_Factor*)icc)->fact);CHKERRQ(ierr);
27     ierr = MatICCFactorSymbolic(((PC_Factor*)icc)->fact,pc->pmat,perm,&((PC_Factor*)icc)->info);CHKERRQ(ierr);
28   }
29   ierr            = MatGetInfo(((PC_Factor*)icc)->fact,MAT_LOCAL,&info);CHKERRQ(ierr);
30   icc->actualfill = info.fill_ratio_needed;
31 
32   ierr = ISDestroy(&cperm);CHKERRQ(ierr);
33   ierr = ISDestroy(&perm);CHKERRQ(ierr);
34 
35   ierr = MatFactorGetError(((PC_Factor*)icc)->fact,&err);CHKERRQ(ierr);
36   if (err) { /* FactorSymbolic() fails */
37     pc->failedreason = (PCFailedReason)err;
38     PetscFunctionReturn(0);
39   }
40 
41   ierr = MatCholeskyFactorNumeric(((PC_Factor*)icc)->fact,pc->pmat,&((PC_Factor*)icc)->info);CHKERRQ(ierr);
42   ierr = MatFactorGetError(((PC_Factor*)icc)->fact,&err);CHKERRQ(ierr);
43   if (err) { /* FactorNumeric() fails */
44     pc->failedreason = (PCFailedReason)err;
45   }
46 
47   ierr = PCFactorGetMatSolverPackage(pc,&stype);CHKERRQ(ierr);
48   if (!stype) {
49     const MatSolverPackage solverpackage;
50     ierr = MatFactorGetSolverPackage(((PC_Factor*)icc)->fact,&solverpackage);CHKERRQ(ierr);
51     ierr = PCFactorSetMatSolverPackage(pc,solverpackage);CHKERRQ(ierr);
52   }
53   PetscFunctionReturn(0);
54 }
55 
56 #undef __FUNCT__
57 #define __FUNCT__ "PCReset_ICC"
58 static PetscErrorCode PCReset_ICC(PC pc)
59 {
60   PC_ICC         *icc = (PC_ICC*)pc->data;
61   PetscErrorCode ierr;
62 
63   PetscFunctionBegin;
64   ierr = MatDestroy(&((PC_Factor*)icc)->fact);CHKERRQ(ierr);
65   PetscFunctionReturn(0);
66 }
67 
68 #undef __FUNCT__
69 #define __FUNCT__ "PCDestroy_ICC"
70 static PetscErrorCode PCDestroy_ICC(PC pc)
71 {
72   PC_ICC         *icc = (PC_ICC*)pc->data;
73   PetscErrorCode ierr;
74 
75   PetscFunctionBegin;
76   ierr = PCReset_ICC(pc);CHKERRQ(ierr);
77   ierr = PetscFree(((PC_Factor*)icc)->ordering);CHKERRQ(ierr);
78   ierr = PetscFree(((PC_Factor*)icc)->solvertype);CHKERRQ(ierr);
79   ierr = PetscFree(pc->data);CHKERRQ(ierr);
80   PetscFunctionReturn(0);
81 }
82 
83 #undef __FUNCT__
84 #define __FUNCT__ "PCApply_ICC"
85 static PetscErrorCode PCApply_ICC(PC pc,Vec x,Vec y)
86 {
87   PC_ICC         *icc = (PC_ICC*)pc->data;
88   PetscErrorCode ierr;
89 
90   PetscFunctionBegin;
91   ierr = MatSolve(((PC_Factor*)icc)->fact,x,y);CHKERRQ(ierr);
92   PetscFunctionReturn(0);
93 }
94 
95 #undef __FUNCT__
96 #define __FUNCT__ "PCApplySymmetricLeft_ICC"
97 static PetscErrorCode PCApplySymmetricLeft_ICC(PC pc,Vec x,Vec y)
98 {
99   PetscErrorCode ierr;
100   PC_ICC         *icc = (PC_ICC*)pc->data;
101 
102   PetscFunctionBegin;
103   ierr = MatForwardSolve(((PC_Factor*)icc)->fact,x,y);CHKERRQ(ierr);
104   PetscFunctionReturn(0);
105 }
106 
107 #undef __FUNCT__
108 #define __FUNCT__ "PCApplySymmetricRight_ICC"
109 static PetscErrorCode PCApplySymmetricRight_ICC(PC pc,Vec x,Vec y)
110 {
111   PetscErrorCode ierr;
112   PC_ICC         *icc = (PC_ICC*)pc->data;
113 
114   PetscFunctionBegin;
115   ierr = MatBackwardSolve(((PC_Factor*)icc)->fact,x,y);CHKERRQ(ierr);
116   PetscFunctionReturn(0);
117 }
118 
119 #undef __FUNCT__
120 #define __FUNCT__ "PCSetFromOptions_ICC"
121 static PetscErrorCode PCSetFromOptions_ICC(PetscOptionItems *PetscOptionsObject,PC pc)
122 {
123   PC_ICC         *icc = (PC_ICC*)pc->data;
124   PetscBool      flg;
125   PetscErrorCode ierr;
126   /* PetscReal      dt[3];*/
127 
128   PetscFunctionBegin;
129   ierr = PetscOptionsHead(PetscOptionsObject,"ICC Options");CHKERRQ(ierr);
130   ierr = PCSetFromOptions_Factor(PetscOptionsObject,pc);CHKERRQ(ierr);
131 
132   ierr = PetscOptionsReal("-pc_factor_levels","levels of fill","PCFactorSetLevels",((PC_Factor*)icc)->info.levels,&((PC_Factor*)icc)->info.levels,&flg);CHKERRQ(ierr);
133   /*dt[0] = ((PC_Factor*)icc)->info.dt;
134   dt[1] = ((PC_Factor*)icc)->info.dtcol;
135   dt[2] = ((PC_Factor*)icc)->info.dtcount;
136   PetscInt       dtmax = 3;
137   ierr = PetscOptionsRealArray("-pc_factor_drop_tolerance","<dt,dtcol,maxrowcount>","PCFactorSetDropTolerance",dt,&dtmax,&flg);CHKERRQ(ierr);
138   if (flg) {
139     ierr = PCFactorSetDropTolerance(pc,dt[0],dt[1],(PetscInt)dt[2]);CHKERRQ(ierr);
140   }
141   */
142   ierr = PetscOptionsTail();CHKERRQ(ierr);
143   PetscFunctionReturn(0);
144 }
145 
146 #undef __FUNCT__
147 #define __FUNCT__ "PCView_ICC"
148 static PetscErrorCode PCView_ICC(PC pc,PetscViewer viewer)
149 {
150   PetscErrorCode ierr;
151 
152   PetscFunctionBegin;
153   ierr = PCView_Factor(pc,viewer);CHKERRQ(ierr);
154   PetscFunctionReturn(0);
155 }
156 
157 extern PetscErrorCode  PCFactorSetDropTolerance_ILU(PC,PetscReal,PetscReal,PetscInt);
158 
159 #undef __FUNCT__
160 #define __FUNCT__ "PCFactorGetUseInPlace_ICC"
161 PetscErrorCode  PCFactorGetUseInPlace_ICC(PC pc,PetscBool *flg)
162 {
163   PetscFunctionBegin;
164   *flg = PETSC_FALSE;
165   PetscFunctionReturn(0);
166 }
167 
168 /*MC
169      PCICC - Incomplete Cholesky factorization preconditioners.
170 
171    Options Database Keys:
172 +  -pc_factor_levels <k> - number of levels of fill for ICC(k)
173 .  -pc_factor_in_place - only for ICC(0) with natural ordering, reuses the space of the matrix for
174                       its factorization (overwrites original matrix)
175 .  -pc_factor_fill <nfill> - expected amount of fill in factored matrix compared to original matrix, nfill > 1
176 -  -pc_factor_mat_ordering_type <natural,nd,1wd,rcm,qmd> - set the row/column ordering of the factored matrix
177 
178    Level: beginner
179 
180   Concepts: incomplete Cholesky factorization
181 
182    Notes: Only implemented for some matrix formats. Not implemented in parallel.
183 
184           For BAIJ matrices this implements a point block ICC.
185 
186           The Manteuffel shift is only implemented for matrices with block size 1
187 
188           By default, the Manteuffel is applied (for matrices with block size 1). Call PCFactorSetShiftType(pc,MAT_SHIFT_POSITIVE_DEFINITE);
189           to turn off the shift.
190 
191    References:
192 .  1. - TONY F. CHAN AND HENK A. VAN DER VORST, Review article: APPROXIMATE AND INCOMPLETE FACTORIZATIONS,
193       Chapter in Parallel Numerical Algorithms, edited by D. Keyes, A. Semah, V. Venkatakrishnan, ICASE/LaRC Interdisciplinary Series in
194       Science and Engineering, Kluwer.
195 
196 
197 .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCSOR, MatOrderingType,
198            PCFactorSetZeroPivot(), PCFactorSetShiftType(), PCFactorSetShiftAmount(),
199            PCFactorSetFill(), PCFactorSetMatOrderingType(), PCFactorSetReuseOrdering(),
200            PCFactorSetLevels()
201 
202 M*/
203 
204 #undef __FUNCT__
205 #define __FUNCT__ "PCCreate_ICC"
206 PETSC_EXTERN PetscErrorCode PCCreate_ICC(PC pc)
207 {
208   PetscErrorCode ierr;
209   PC_ICC         *icc;
210 
211   PetscFunctionBegin;
212   ierr = PetscNewLog(pc,&icc);CHKERRQ(ierr);
213 
214   ((PC_Factor*)icc)->fact = 0;
215 
216   ierr = PetscStrallocpy(MATORDERINGNATURAL,(char**)&((PC_Factor*)icc)->ordering);CHKERRQ(ierr);
217   ierr = MatFactorInfoInitialize(&((PC_Factor*)icc)->info);CHKERRQ(ierr);
218 
219   ((PC_Factor*)icc)->factortype  = MAT_FACTOR_ICC;
220   ((PC_Factor*)icc)->info.levels = 0.;
221   ((PC_Factor*)icc)->info.fill   = 1.0;
222   icc->implctx                   = 0;
223 
224   ((PC_Factor*)icc)->info.dtcol       = PETSC_DEFAULT;
225   ((PC_Factor*)icc)->info.shifttype   = (PetscReal) MAT_SHIFT_POSITIVE_DEFINITE;
226   ((PC_Factor*)icc)->info.shiftamount = 100.0*PETSC_MACHINE_EPSILON;
227   ((PC_Factor*)icc)->info.zeropivot   = 100.0*PETSC_MACHINE_EPSILON;
228 
229   pc->data                     = (void*)icc;
230   pc->ops->apply               = PCApply_ICC;
231   pc->ops->applytranspose      = PCApply_ICC;
232   pc->ops->setup               = PCSetUp_ICC;
233   pc->ops->reset               = PCReset_ICC;
234   pc->ops->destroy             = PCDestroy_ICC;
235   pc->ops->setfromoptions      = PCSetFromOptions_ICC;
236   pc->ops->view                = PCView_ICC;
237   pc->ops->getfactoredmatrix   = PCFactorGetMatrix_Factor;
238   pc->ops->applysymmetricleft  = PCApplySymmetricLeft_ICC;
239   pc->ops->applysymmetricright = PCApplySymmetricRight_ICC;
240 
241   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetZeroPivot_C",PCFactorSetZeroPivot_Factor);CHKERRQ(ierr);
242   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetZeroPivot_C",PCFactorGetZeroPivot_Factor);CHKERRQ(ierr);
243   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftType_C",PCFactorSetShiftType_Factor);CHKERRQ(ierr);
244   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetShiftType_C",PCFactorGetShiftType_Factor);CHKERRQ(ierr);
245   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftAmount_C",PCFactorSetShiftAmount_Factor);CHKERRQ(ierr);
246   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetShiftAmount_C",PCFactorGetShiftAmount_Factor);CHKERRQ(ierr);
247   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUpMatSolverPackage_C",PCFactorSetUpMatSolverPackage_Factor);CHKERRQ(ierr);
248   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetMatSolverPackage_C",PCFactorGetMatSolverPackage_Factor);CHKERRQ(ierr);
249   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetLevels_C",PCFactorSetLevels_Factor);CHKERRQ(ierr);
250   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetLevels_C",PCFactorGetLevels_Factor);CHKERRQ(ierr);
251   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetFill_C",PCFactorSetFill_Factor);CHKERRQ(ierr);
252   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatOrderingType_C",PCFactorSetMatOrderingType_Factor);CHKERRQ(ierr);
253   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatSolverPackage_C",PCFactorSetMatSolverPackage_Factor);CHKERRQ(ierr);
254   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetDropTolerance_C",PCFactorSetDropTolerance_ILU);CHKERRQ(ierr);
255   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetUseInPlace_C",PCFactorGetUseInPlace_ICC);CHKERRQ(ierr);
256   PetscFunctionReturn(0);
257 }
258 
259 
260