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