xref: /petsc/src/ksp/pc/impls/pbjacobi/pbjacobi.c (revision 1f7136835039a38fe450516aa819843c72c3a1ba)
1 
2 /*
3    Include files needed for the PBJacobi preconditioner:
4      pcimpl.h - private include file intended for use by all preconditioners
5 */
6 
7 #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
8 
9 /*
10    Private context (data structure) for the PBJacobi preconditioner.
11 */
12 typedef struct {
13   const MatScalar *diag;
14   PetscInt         bs, mbs;
15 } PC_PBJacobi;
16 
17 static PetscErrorCode PCApply_PBJacobi(PC pc, Vec x, Vec y)
18 {
19   PC_PBJacobi       *jac = (PC_PBJacobi *)pc->data;
20   PetscInt           i, ib, jb;
21   const PetscInt     m    = jac->mbs;
22   const PetscInt     bs   = jac->bs;
23   const MatScalar   *diag = jac->diag;
24   PetscScalar       *yy, x0, x1, x2, x3, x4, x5, x6;
25   const PetscScalar *xx;
26 
27   PetscFunctionBegin;
28   PetscCall(VecGetArrayRead(x, &xx));
29   PetscCall(VecGetArray(y, &yy));
30   switch (bs) {
31   case 1:
32     for (i = 0; i < m; i++) yy[i] = diag[i] * xx[i];
33     break;
34   case 2:
35     for (i = 0; i < m; i++) {
36       x0            = xx[2 * i];
37       x1            = xx[2 * i + 1];
38       yy[2 * i]     = diag[0] * x0 + diag[2] * x1;
39       yy[2 * i + 1] = diag[1] * x0 + diag[3] * x1;
40       diag += 4;
41     }
42     break;
43   case 3:
44     for (i = 0; i < m; i++) {
45       x0 = xx[3 * i];
46       x1 = xx[3 * i + 1];
47       x2 = xx[3 * i + 2];
48 
49       yy[3 * i]     = diag[0] * x0 + diag[3] * x1 + diag[6] * x2;
50       yy[3 * i + 1] = diag[1] * x0 + diag[4] * x1 + diag[7] * x2;
51       yy[3 * i + 2] = diag[2] * x0 + diag[5] * x1 + diag[8] * x2;
52       diag += 9;
53     }
54     break;
55   case 4:
56     for (i = 0; i < m; i++) {
57       x0 = xx[4 * i];
58       x1 = xx[4 * i + 1];
59       x2 = xx[4 * i + 2];
60       x3 = xx[4 * i + 3];
61 
62       yy[4 * i]     = diag[0] * x0 + diag[4] * x1 + diag[8] * x2 + diag[12] * x3;
63       yy[4 * i + 1] = diag[1] * x0 + diag[5] * x1 + diag[9] * x2 + diag[13] * x3;
64       yy[4 * i + 2] = diag[2] * x0 + diag[6] * x1 + diag[10] * x2 + diag[14] * x3;
65       yy[4 * i + 3] = diag[3] * x0 + diag[7] * x1 + diag[11] * x2 + diag[15] * x3;
66       diag += 16;
67     }
68     break;
69   case 5:
70     for (i = 0; i < m; i++) {
71       x0 = xx[5 * i];
72       x1 = xx[5 * i + 1];
73       x2 = xx[5 * i + 2];
74       x3 = xx[5 * i + 3];
75       x4 = xx[5 * i + 4];
76 
77       yy[5 * i]     = diag[0] * x0 + diag[5] * x1 + diag[10] * x2 + diag[15] * x3 + diag[20] * x4;
78       yy[5 * i + 1] = diag[1] * x0 + diag[6] * x1 + diag[11] * x2 + diag[16] * x3 + diag[21] * x4;
79       yy[5 * i + 2] = diag[2] * x0 + diag[7] * x1 + diag[12] * x2 + diag[17] * x3 + diag[22] * x4;
80       yy[5 * i + 3] = diag[3] * x0 + diag[8] * x1 + diag[13] * x2 + diag[18] * x3 + diag[23] * x4;
81       yy[5 * i + 4] = diag[4] * x0 + diag[9] * x1 + diag[14] * x2 + diag[19] * x3 + diag[24] * x4;
82       diag += 25;
83     }
84     break;
85   case 6:
86     for (i = 0; i < m; i++) {
87       x0 = xx[6 * i];
88       x1 = xx[6 * i + 1];
89       x2 = xx[6 * i + 2];
90       x3 = xx[6 * i + 3];
91       x4 = xx[6 * i + 4];
92       x5 = xx[6 * i + 5];
93 
94       yy[6 * i]     = diag[0] * x0 + diag[6] * x1 + diag[12] * x2 + diag[18] * x3 + diag[24] * x4 + diag[30] * x5;
95       yy[6 * i + 1] = diag[1] * x0 + diag[7] * x1 + diag[13] * x2 + diag[19] * x3 + diag[25] * x4 + diag[31] * x5;
96       yy[6 * i + 2] = diag[2] * x0 + diag[8] * x1 + diag[14] * x2 + diag[20] * x3 + diag[26] * x4 + diag[32] * x5;
97       yy[6 * i + 3] = diag[3] * x0 + diag[9] * x1 + diag[15] * x2 + diag[21] * x3 + diag[27] * x4 + diag[33] * x5;
98       yy[6 * i + 4] = diag[4] * x0 + diag[10] * x1 + diag[16] * x2 + diag[22] * x3 + diag[28] * x4 + diag[34] * x5;
99       yy[6 * i + 5] = diag[5] * x0 + diag[11] * x1 + diag[17] * x2 + diag[23] * x3 + diag[29] * x4 + diag[35] * x5;
100       diag += 36;
101     }
102     break;
103   case 7:
104     for (i = 0; i < m; i++) {
105       x0 = xx[7 * i];
106       x1 = xx[7 * i + 1];
107       x2 = xx[7 * i + 2];
108       x3 = xx[7 * i + 3];
109       x4 = xx[7 * i + 4];
110       x5 = xx[7 * i + 5];
111       x6 = xx[7 * i + 6];
112 
113       yy[7 * i]     = diag[0] * x0 + diag[7] * x1 + diag[14] * x2 + diag[21] * x3 + diag[28] * x4 + diag[35] * x5 + diag[42] * x6;
114       yy[7 * i + 1] = diag[1] * x0 + diag[8] * x1 + diag[15] * x2 + diag[22] * x3 + diag[29] * x4 + diag[36] * x5 + diag[43] * x6;
115       yy[7 * i + 2] = diag[2] * x0 + diag[9] * x1 + diag[16] * x2 + diag[23] * x3 + diag[30] * x4 + diag[37] * x5 + diag[44] * x6;
116       yy[7 * i + 3] = diag[3] * x0 + diag[10] * x1 + diag[17] * x2 + diag[24] * x3 + diag[31] * x4 + diag[38] * x5 + diag[45] * x6;
117       yy[7 * i + 4] = diag[4] * x0 + diag[11] * x1 + diag[18] * x2 + diag[25] * x3 + diag[32] * x4 + diag[39] * x5 + diag[46] * x6;
118       yy[7 * i + 5] = diag[5] * x0 + diag[12] * x1 + diag[19] * x2 + diag[26] * x3 + diag[33] * x4 + diag[40] * x5 + diag[47] * x6;
119       yy[7 * i + 6] = diag[6] * x0 + diag[13] * x1 + diag[20] * x2 + diag[27] * x3 + diag[34] * x4 + diag[41] * x5 + diag[48] * x6;
120       diag += 49;
121     }
122     break;
123   default:
124     for (i = 0; i < m; i++) {
125       for (ib = 0; ib < bs; ib++) {
126         PetscScalar rowsum = 0;
127         for (jb = 0; jb < bs; jb++) rowsum += diag[ib + jb * bs] * xx[bs * i + jb];
128         yy[bs * i + ib] = rowsum;
129       }
130       diag += bs * bs;
131     }
132   }
133   PetscCall(VecRestoreArrayRead(x, &xx));
134   PetscCall(VecRestoreArray(y, &yy));
135   PetscCall(PetscLogFlops((2.0 * bs * bs - bs) * m)); /* 2*bs2 - bs */
136   PetscFunctionReturn(0);
137 }
138 
139 static PetscErrorCode PCApplyTranspose_PBJacobi_N(PC pc, Vec x, Vec y)
140 {
141   PC_PBJacobi       *jac = (PC_PBJacobi *)pc->data;
142   PetscInt           i, j, k, m = jac->mbs, bs = jac->bs;
143   const MatScalar   *diag = jac->diag;
144   const PetscScalar *xx;
145   PetscScalar       *yy;
146 
147   PetscFunctionBegin;
148   PetscCall(VecGetArrayRead(x, &xx));
149   PetscCall(VecGetArray(y, &yy));
150   for (i = 0; i < m; i++) {
151     for (j = 0; j < bs; j++) yy[i * bs + j] = 0.;
152     for (j = 0; j < bs; j++) {
153       for (k = 0; k < bs; k++) yy[i * bs + k] += diag[k * bs + j] * xx[i * bs + j];
154     }
155     diag += bs * bs;
156   }
157   PetscCall(VecRestoreArrayRead(x, &xx));
158   PetscCall(VecRestoreArray(y, &yy));
159   PetscCall(PetscLogFlops(m * bs * (2 * bs - 1)));
160   PetscFunctionReturn(0);
161 }
162 
163 static PetscErrorCode PCSetUp_PBJacobi(PC pc)
164 {
165   PC_PBJacobi   *jac = (PC_PBJacobi *)pc->data;
166   Mat            A   = pc->pmat;
167   MatFactorError err;
168   PetscInt       nlocal;
169 
170   PetscFunctionBegin;
171   PetscCall(MatInvertBlockDiagonal(A, &jac->diag));
172   PetscCall(MatFactorGetError(A, &err));
173   if (err) pc->failedreason = (PCFailedReason)err;
174 
175   PetscCall(MatGetBlockSize(A, &jac->bs));
176   PetscCall(MatGetLocalSize(A, &nlocal, NULL));
177   jac->mbs                = nlocal / jac->bs;
178   pc->ops->apply          = PCApply_PBJacobi;
179   pc->ops->applytranspose = PCApplyTranspose_PBJacobi_N;
180   PetscFunctionReturn(0);
181 }
182 
183 static PetscErrorCode PCDestroy_PBJacobi(PC pc)
184 {
185   PetscFunctionBegin;
186   /*
187       Free the private data structure that was hanging off the PC
188   */
189   PetscCall(PetscFree(pc->data));
190   PetscFunctionReturn(0);
191 }
192 
193 static PetscErrorCode PCView_PBJacobi(PC pc, PetscViewer viewer)
194 {
195   PC_PBJacobi *jac = (PC_PBJacobi *)pc->data;
196   PetscBool    iascii;
197 
198   PetscFunctionBegin;
199   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
200   if (iascii) PetscCall(PetscViewerASCIIPrintf(viewer, "  point-block size %" PetscInt_FMT "\n", jac->bs));
201   PetscFunctionReturn(0);
202 }
203 
204 /*MC
205      PCPBJACOBI - Point block Jacobi preconditioner
206 
207    Notes:
208     See `PCJACOBI` for diagonal Jacobi, `PCVPBJACOBI` for variable-size point block, and `PCBJACOBI` for large size blocks
209 
210    This works for `MATAIJ` and `MATBAIJ` matrices and uses the blocksize provided to the matrix
211 
212    Uses dense LU factorization with partial pivoting to invert the blocks; if a zero pivot
213    is detected a PETSc error is generated.
214 
215    Developer Notes:
216      This should support the `PCSetErrorIfFailure()` flag set to `PETSC_TRUE` to allow
217      the factorization to continue even after a zero pivot is found resulting in a Nan and hence
218      terminating `KSP` with a `KSP_DIVERGED_NANORIF` allowing
219      a nonlinear solver/ODE integrator to recover without stopping the program as currently happens.
220 
221      Perhaps should provide an option that allows generation of a valid preconditioner
222      even if a block is singular as the `PCJACOBI` does.
223 
224    Level: beginner
225 
226 .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCJACOBI`, `PCVPBJACOBI`, `PCBJACOBI`
227 M*/
228 
229 PETSC_EXTERN PetscErrorCode PCCreate_PBJacobi(PC pc)
230 {
231   PC_PBJacobi *jac;
232 
233   PetscFunctionBegin;
234   /*
235      Creates the private data structure for this preconditioner and
236      attach it to the PC object.
237   */
238   PetscCall(PetscNew(&jac));
239   pc->data = (void *)jac;
240 
241   /*
242      Initialize the pointers to vectors to ZERO; these will be used to store
243      diagonal entries of the matrix for fast preconditioner application.
244   */
245   jac->diag = NULL;
246 
247   /*
248       Set the pointers for the functions that are provided above.
249       Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
250       are called, they will automatically call these functions.  Note we
251       choose not to provide a couple of these functions since they are
252       not needed.
253   */
254   pc->ops->apply               = NULL; /*set depending on the block size */
255   pc->ops->applytranspose      = NULL;
256   pc->ops->setup               = PCSetUp_PBJacobi;
257   pc->ops->destroy             = PCDestroy_PBJacobi;
258   pc->ops->setfromoptions      = NULL;
259   pc->ops->view                = PCView_PBJacobi;
260   pc->ops->applyrichardson     = NULL;
261   pc->ops->applysymmetricleft  = NULL;
262   pc->ops->applysymmetricright = NULL;
263   PetscFunctionReturn(0);
264 }
265