Actual source code: ex16.c

  1: static const char help[] = "Tests PCHPDDM with a user-supplied deflation space and scaling against an explicitly assembled reference operator.\n\n";

  3: /*
  4:   Setup, on two processes:
  5:   - A is an 8 x 8 tridiagonal matrix, complex in complex builds, with 4 rows per process.
  6:   - The overlapping subdomain of process p holds the global rows 2p, ..., 2p + 5, see SubdomainRow().
  7:   - The scaling D_p is given by Scaling() and the single deflation vector U_p by Mode().
  8:     With -empty_rank0, process 0 supplies no deflation vector.
  9:   - ModifySubmatrices() shifts the local matrices of the fine-level Schwarz preconditioner, as optimized transmission
 10:     conditions would, so that the local solvers A_p differ from the restrictions of A.

 12:   After each setup, PCApply() and PCMatApply() must match the reference operator
 13:     T = Q + B                     with -pc_hpddm_coarse_correction additive,
 14:     T = Q + B (I - A Q)           with -pc_hpddm_coarse_correction deflated,
 15:     T = B + Q (I - A B)           with -pc_hpddm_coarse_correction deflated_reversed,
 16:     T = Q + (I - Q A) B (I - A Q) with -pc_hpddm_coarse_correction balanced,
 17:   where Q = Z (Z^H A Z)^{-1} Z^H is the coarse correction with Z = [R_p^T D_p U_p], and B is the
 18:   fine-level correction, see FineLevelType.
 19:   Also checks without deflation (-scaling_only) and then T = B.
 20: */

 22: #include <petscksp.h>

 24: #define N_GLOBAL    8 // number of unknowns
 25: #define N_LOCAL     4 // rows owned by each process
 26: #define N_SUBDOMAIN 6 // rows of each overlapping subdomain

 28: // fine-level corrections that the reference operator can reproduce
 29: typedef enum {
 30:   FINE_ASM_WEIGHTED,  // default with a user-supplied scaling: B = sum_p R_p^T D_p A_p^{-1} R_p
 31:   FINE_SCHWARZ_BASIC, // PCASM or PCGASM of type basic: B = sum_p R_p^T A_p^{-1} R_p
 32:   FINE_JACOBI         // -pc_hpddm_levels_1_pc_type jacobi:    B = diag(A)^{-1}
 33: } FineLevelType;

 35: static PetscScalar TestScalar(PetscReal real, PetscReal imag)
 36: {
 37: #if PetscDefined(USE_COMPLEX)
 38:   return PetscCMPLX(real, imag);
 39: #else
 40:   (void)imag;
 41:   return real;
 42: #endif
 43: }

 45: // global row of the i-th row of the overlapping subdomain of process p: rows 0-5 for process 0, rows 2-7 for process 1
 46: static PetscInt SubdomainRow(PetscMPIInt p, PetscInt i)
 47: {
 48:   return 2 * p + i;
 49: }

 51: /*
 52:   Scaling of process p at a global row. Process 0 scales its rows 0-5 by 1, 1, 1, 0.65, 0.35, 0, and process 1 its
 53:   rows 2-7 by the complements 0, 0.35, 0.65, 1, 1, 1. The two scalings sum to one on every row and vanish outside each
 54:   subdomain. They also vanish on the subdomain row that A couples to the outside (row 5 for process 0, row 2 for
 55:   process 1), as PCHPDDMSetScaling() requires.
 56: */
 57: static PetscScalar Scaling(PetscMPIInt p, PetscInt row)
 58: {
 59:   const PetscReal process0[N_GLOBAL] = {1.0, 1.0, 1.0, 0.65, 0.35, 0.0, 0.0, 0.0};

 61:   return p == 0 ? process0[row] : 1.0 - process0[row];
 62: }

 64: // entry of the deflation vector of process p at a global row; complex in complex builds, which exercises conjugation
 65: static PetscScalar Mode(PetscMPIInt p, PetscInt row)
 66: {
 67:   return TestScalar(1.0 + 0.15 * row, 0.3 * (p + 1) - 0.05 * row);
 68: }

 70: // shift applied to every fine-level local matrix by ModifySubmatrices(), and by the reference operator
 71: static PetscScalar LocalShift(void)
 72: {
 73:   return TestScalar(0.35, 0.2);
 74: }

 76: static PetscErrorCode ModifySubmatrices(PC pc, PetscInt n, const IS rows[], const IS cols[], Mat sub[], PetscCtx ctx)
 77: {
 78:   PetscFunctionBeginUser;
 79:   for (PetscInt i = 0; i < n; ++i) PetscCall(MatShift(sub[i], LocalShift()));
 80:   PetscFunctionReturn(PETSC_SUCCESS);
 81: }

 83: static PetscErrorCode CreateOperator(Mat *A)
 84: {
 85:   PetscInt start, end;

 87:   PetscFunctionBeginUser;
 88:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, N_LOCAL, N_LOCAL, N_GLOBAL, N_GLOBAL, 3, NULL, 2, NULL, A));
 89:   PetscCall(MatGetOwnershipRange(*A, &start, &end));
 90:   for (PetscInt row = start; row < end; ++row) {
 91:     PetscCall(MatSetValue(*A, row, row, TestScalar(3.0 + 0.1 * row, 0.25), INSERT_VALUES));
 92:     if (row > 0) PetscCall(MatSetValue(*A, row, row - 1, TestScalar(-0.9, 0.07), INSERT_VALUES));
 93:     if (row < N_GLOBAL - 1) PetscCall(MatSetValue(*A, row, row + 1, TestScalar(-0.3, -0.12), INSERT_VALUES));
 94:   }
 95:   PetscCall(MatAssemblyBegin(*A, MAT_FINAL_ASSEMBLY));
 96:   PetscCall(MatAssemblyEnd(*A, MAT_FINAL_ASSEMBLY));
 97:   PetscFunctionReturn(PETSC_SUCCESS);
 98: }

100: /*
101:   Inputs of PCHPDDMSetDeflationMat() and PCHPDDMSetScaling(), as a user would build them: the subdomain IS,
102:   the local deflation matrix U (one column, or none on process 0 with -empty_rank0), and the local scaling D. All three
103:   list the subdomain rows in reverse order, unlike the sorted order used internally by PCASM, so that the test also
104:   checks how PCHPDDM reorders the scaling.
105: */
106: static PetscErrorCode CreateDeflation(PetscBool empty_rank0, IS *is, Mat *U, Vec *D)
107: {
108:   PetscMPIInt  rank;
109:   PetscInt     rows[N_SUBDOMAIN], nmodes;
110:   PetscScalar *u, *d;

112:   PetscFunctionBeginUser;
113:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
114:   nmodes = (empty_rank0 == PETSC_TRUE && rank == 0) ? 0 : 1; // number of local deflation vectors
115:   for (PetscInt i = 0; i < N_SUBDOMAIN; ++i) rows[i] = SubdomainRow(rank, N_SUBDOMAIN - 1 - i);
116:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, N_SUBDOMAIN, rows, PETSC_COPY_VALUES, is));
117:   PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, N_SUBDOMAIN, nmodes, NULL, U));
118:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, N_SUBDOMAIN, D));
119:   PetscCall(MatDenseGetArray(*U, &u));
120:   PetscCall(VecGetArray(*D, &d));
121:   for (PetscInt i = 0; i < N_SUBDOMAIN; ++i) {
122:     d[i] = Scaling(rank, rows[i]);
123:     if (nmodes > 0) u[i] = Mode(rank, rows[i]);
124:   }
125:   PetscCall(VecRestoreArray(*D, &d));
126:   PetscCall(MatDenseRestoreArray(*U, &u));
127:   PetscFunctionReturn(PETSC_SUCCESS);
128: }

130: /*
131:   Fine-level correction B, see FineLevelType. For ASM and GASM, each process adds its term R_p^T [D_p] A_p^{-1} R_p column by
132:   column, with A_p the shifted restriction of A to its subdomain, factored with LU like the subdomain solver.
133: */
134: static PetscErrorCode CreateFineReference(Mat A, FineLevelType fine, Mat *B)
135: {
136:   Mat               *sub;
137:   KSP                ksp;
138:   PC                 lu;
139:   Vec                x, y;
140:   IS                 is;
141:   PetscMPIInt        rank;
142:   PetscInt           rows[N_SUBDOMAIN], start, end;
143:   const PetscScalar *values;
144:   PetscScalar        column[N_SUBDOMAIN];

146:   PetscFunctionBeginUser;
147:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
148:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, N_LOCAL, N_LOCAL, N_GLOBAL, N_GLOBAL, N_LOCAL, NULL, N_LOCAL, NULL, B)); // rows may be dense
149:   if (fine == FINE_JACOBI) {
150:     PetscCall(MatCreateVecs(A, NULL, &x));
151:     PetscCall(MatGetDiagonal(A, x));
152:     PetscCall(MatGetOwnershipRange(A, &start, &end));
153:     PetscCall(VecGetArrayRead(x, &values));
154:     for (PetscInt row = start; row < end; ++row) PetscCall(MatSetValue(*B, row, row, (PetscScalar)1.0 / values[row - start], INSERT_VALUES));
155:     PetscCall(VecRestoreArrayRead(x, &values));
156:     PetscCall(VecDestroy(&x));
157:   } else {
158:     for (PetscInt i = 0; i < N_SUBDOMAIN; ++i) rows[i] = SubdomainRow(rank, i);
159:     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, N_SUBDOMAIN, rows, PETSC_COPY_VALUES, &is));
160:     PetscCall(MatCreateSubMatrices(A, 1, &is, &is, MAT_INITIAL_MATRIX, &sub));
161:     PetscCall(MatShift(sub[0], LocalShift()));
162:     PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
163:     PetscCall(KSPSetType(ksp, KSPPREONLY));
164:     PetscCall(KSPSetOperators(ksp, sub[0], sub[0]));
165:     PetscCall(KSPGetPC(ksp, &lu));
166:     PetscCall(PCSetType(lu, PCLU));
167:     PetscCall(MatCreateVecs(sub[0], &x, &y));
168:     for (PetscInt j = 0; j < N_SUBDOMAIN; ++j) { // column j of A_p^{-1}, scaled by D_p for PC_ASM_WEIGHTED
169:       PetscCall(VecSet(x, 0.0));
170:       PetscCall(VecSetValue(x, j, 1.0, INSERT_VALUES));
171:       PetscCall(VecAssemblyBegin(x));
172:       PetscCall(VecAssemblyEnd(x));
173:       PetscCall(KSPSolve(ksp, x, y));
174:       PetscCall(VecGetArrayRead(y, &values));
175:       for (PetscInt i = 0; i < N_SUBDOMAIN; ++i) column[i] = (fine == FINE_ASM_WEIGHTED ? Scaling(rank, rows[i]) : (PetscScalar)1.0) * values[i];
176:       PetscCall(MatSetValues(*B, N_SUBDOMAIN, rows, 1, rows + j, column, ADD_VALUES));
177:       PetscCall(VecRestoreArrayRead(y, &values));
178:     }
179:     PetscCall(VecDestroy(&x));
180:     PetscCall(VecDestroy(&y));
181:     PetscCall(KSPDestroy(&ksp));
182:     PetscCall(MatDestroySubMatrices(1, &sub));
183:     PetscCall(ISDestroy(&is));
184:   }
185:   PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
186:   PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
187:   PetscFunctionReturn(PETSC_SUCCESS);
188: }

190: /*
191:   Coarse correction Q = Z (Z^H A Z)^{-1} Z^H. Column p of Z is the scaled deflation vector R_p^T D_p U_p of process p,
192:   which is zero outside subdomain p because Scaling() is. With -empty_rank0, Z only has the column of process 1.
193: */
194: static PetscErrorCode CreateCoarseReference(Mat A, PetscBool empty_rank0, Mat *Q)
195: {
196:   Mat         Z, Zh, E, EinvZh;
197:   KSP         ksp;
198:   PC          redundant;
199:   PetscMPIInt rank, first_process = empty_rank0 == PETSC_TRUE ? 1 : 0; // first process with a deflation vector
200:   PetscInt    start, end;

202:   PetscFunctionBeginUser;
203:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
204:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, N_LOCAL, rank < first_process ? 0 : 1, N_GLOBAL, 2 - first_process, 1, NULL, 1, NULL, &Z));
205:   PetscCall(MatGetOwnershipRange(Z, &start, &end));
206:   for (PetscInt row = start; row < end; ++row)
207:     for (PetscMPIInt p = first_process; p < 2; ++p) PetscCall(MatSetValue(Z, row, p - first_process, Scaling(p, row) * Mode(p, row), INSERT_VALUES));
208:   PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY));
209:   PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY));
210:   PetscCall(MatHermitianTranspose(Z, MAT_INITIAL_MATRIX, &Zh));
211:   PetscCall(MatMatMatMult(Zh, A, Z, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &E));
212:   PetscCall(MatConvert(Zh, MATDENSE, MAT_INPLACE_MATRIX, &Zh)); // KSPMatSolve() needs dense right-hand sides
213:   PetscCall(MatDuplicate(Zh, MAT_DO_NOT_COPY_VALUES, &EinvZh));
214:   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
215:   PetscCall(KSPSetType(ksp, KSPPREONLY));
216:   PetscCall(KSPSetOperators(ksp, E, E));
217:   PetscCall(KSPGetPC(ksp, &redundant));
218:   PetscCall(PCSetType(redundant, PCREDUNDANT)); // gathers E on every process and factors it with the sequential PETSc LU
219:   PetscCall(KSPMatSolve(ksp, Zh, EinvZh));
220:   PetscCall(MatMatMult(Z, EinvZh, MAT_INITIAL_MATRIX, PETSC_DETERMINE, Q));
221:   PetscCall(KSPDestroy(&ksp));
222:   PetscCall(MatDestroy(&Z));
223:   PetscCall(MatDestroy(&Zh));
224:   PetscCall(MatDestroy(&E));
225:   PetscCall(MatDestroy(&EinvZh));
226:   PetscFunctionReturn(PETSC_SUCCESS);
227: }

229: /*
230:   Reference two-level operator T = first + second (I - A first), see the description at the top of this file: the
231:   deflated correction applies the coarse level first, the reversed one applies the fine level first. The balanced
232:   correction also multiplies second (I - A first) on the left by (I - first A).
233: */
234: static PetscErrorCode CreateReference(Mat A, PetscBool empty_rank0, FineLevelType fine, PCHPDDMCoarseCorrectionType correction, Mat *T)
235: {
236:   Mat B, Q, first, second, IA;

238:   PetscFunctionBeginUser;
239:   PetscCall(CreateFineReference(A, fine, &B));
240:   PetscCall(CreateCoarseReference(A, empty_rank0, &Q));
241:   if (correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE) {
242:     PetscCall(MatConvert(B, MATDENSE, MAT_INPLACE_MATRIX, &B));
243:     PetscCall(MatAXPY(Q, 1.0, B, SAME_NONZERO_PATTERN));
244:     PetscCall(MatDestroy(&B));
245:     *T = Q;
246:     PetscFunctionReturn(PETSC_SUCCESS);
247:   }
248:   PetscCheck(correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED_REVERSED || correction == PC_HPDDM_COARSE_CORRECTION_BALANCED, PETSC_COMM_WORLD, PETSC_ERR_SUP, "No reference for PCHPDDMCoarseCorrectionType %s", PCHPDDMCoarseCorrectionTypes[correction]);
249:   if (correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED_REVERSED) {
250:     first  = B;
251:     second = Q;
252:   } else {
253:     first  = Q;
254:     second = B;
255:   }
256:   PetscCall(MatMatMult(A, first, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &IA));
257:   PetscCall(MatScale(IA, -1.0));
258:   PetscCall(MatShift(IA, 1.0)); // I - A first
259:   PetscCall(MatMatMult(second, IA, MAT_INITIAL_MATRIX, PETSC_DETERMINE, T));
260:   if (correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
261:     Mat product;

263:     PetscCall(MatDestroy(&IA));
264:     PetscCall(MatMatMult(first, A, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &IA));
265:     PetscCall(MatScale(IA, -1.0));
266:     PetscCall(MatShift(IA, 1.0)); // I - first A
267:     PetscCall(MatMatMult(IA, *T, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &product));
268:     PetscCall(MatDestroy(T));
269:     *T = product;
270:   }
271:   PetscCall(MatConvert(B, MATDENSE, MAT_INPLACE_MATRIX, &B)); // T is dense, and MatAXPY() needs B in the same format; in place, so first still refers to B
272:   PetscCall(MatAXPY(*T, 1.0, first, SAME_NONZERO_PATTERN));
273:   PetscCall(MatDestroy(&B));
274:   PetscCall(MatDestroy(&Q));
275:   PetscCall(MatDestroy(&IA));
276:   PetscFunctionReturn(PETSC_SUCCESS);
277: }

279: /*
280:   Fine-level correction that PCHPDDM builds from the same options, read with the prefix of its fine level:
281:   pc_hpddm_levels_1_, or pc_hpddm_coarse_ with a single level. Only the corrections of FineLevelType have a reference.
282: */
283: static PetscErrorCode GetFineLevelType(const char prefix[], FineLevelType *fine)
284: {
285:   char       type[256] = PCASM;
286:   PCASMType  asm_type  = PC_ASM_WEIGHTED; // default selected by PCHPDDM with a user-supplied scaling
287:   PCGASMType gasm_type = PC_GASM_RESTRICT;
288:   PetscBool  asm_pc, gasm_pc, jacobi;

290:   PetscFunctionBeginUser;
291:   PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_type", type, sizeof(type), NULL));
292:   PetscCall(PetscOptionsGetEnum(NULL, prefix, "-pc_asm_type", PCASMTypes, (PetscEnum *)&asm_type, NULL));
293:   PetscCall(PetscOptionsGetEnum(NULL, prefix, "-pc_gasm_type", PCGASMTypes, (PetscEnum *)&gasm_type, NULL));
294:   PetscCall(PetscStrcmp(type, PCASM, &asm_pc));
295:   PetscCall(PetscStrcmp(type, PCGASM, &gasm_pc));
296:   PetscCall(PetscStrcmp(type, PCJACOBI, &jacobi));
297:   PetscCheck(jacobi == PETSC_TRUE || (gasm_pc == PETSC_TRUE && gasm_type == PC_GASM_BASIC) || (asm_pc == PETSC_TRUE && (asm_type == PC_ASM_WEIGHTED || asm_type == PC_ASM_BASIC)), PETSC_COMM_WORLD, PETSC_ERR_SUP, "No reference for this fine-level PC, use PCJACOBI, basic PCGASM, or weighted or basic PCASM");
298:   if (jacobi == PETSC_TRUE) *fine = FINE_JACOBI;
299:   else *fine = asm_pc == PETSC_TRUE && asm_type == PC_ASM_WEIGHTED ? FINE_ASM_WEIGHTED : FINE_SCHWARZ_BASIC;
300:   PetscFunctionReturn(PETSC_SUCCESS);
301: }

303: static PetscErrorCode CheckVector(Vec actual, Vec expected)
304: {
305:   PetscReal error, norm;

307:   PetscFunctionBeginUser;
308:   PetscCall(VecNorm(expected, NORM_INFINITY, &norm));
309:   PetscCall(VecAXPY(expected, -1.0, actual));
310:   PetscCall(VecNorm(expected, NORM_INFINITY, &error));
311:   PetscCheck(error <= 1000.0 * PETSC_MACHINE_EPSILON * PetscMax(1.0, norm), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "PCHPDDM differs from the reference operator by %g", (double)error);
312:   PetscFunctionReturn(PETSC_SUCCESS);
313: }

315: /*
316:   Compares the PC with the reference operator T: PCApply() on every unit vector, and PCMatApply() on blocks of two and three
317:   vectors. Also compares PCApplyTranspose() and PCMatApplyTranspose() with T^T, including with complex scalars.
318: */
319: static PetscErrorCode CheckApplications(PC pc, Mat T)
320: {
321:   Vec      x, y, expected;
322:   PetscInt N, m, start, end;

324:   PetscFunctionBeginUser;
325:   PetscCall(MatGetSize(T, &N, NULL));
326:   PetscCall(MatGetLocalSize(T, &m, NULL));
327:   PetscCall(MatCreateVecs(T, &x, &y));
328:   PetscCall(VecDuplicate(y, &expected));
329:   PetscCall(VecGetOwnershipRange(x, &start, &end));
330:   for (PetscInt j = 0; j < N; ++j) {
331:     PetscCall(VecSet(x, 0.0));
332:     if (start <= j && j < end) PetscCall(VecSetValue(x, j, 1.0, INSERT_VALUES));
333:     PetscCall(VecAssemblyBegin(x));
334:     PetscCall(VecAssemblyEnd(x));
335:     PetscCall(PCApply(pc, x, y));
336:     PetscCall(MatMult(T, x, expected));
337:     PetscCall(CheckVector(y, expected));
338:     PetscCall(PCApplyTranspose(pc, x, y));
339:     PetscCall(MatMultTranspose(T, x, expected));
340:     PetscCall(CheckVector(y, expected));
341:   }
342:   for (PetscInt nrhs = 2; nrhs <= 3; ++nrhs) {
343:     Mat X, Y;

345:     PetscCall(MatCreateDense(PETSC_COMM_WORLD, m, PETSC_DECIDE, N, nrhs, NULL, &X));
346:     PetscCall(MatDuplicate(X, MAT_DO_NOT_COPY_VALUES, &Y));
347:     PetscCall(MatSetRandom(X, NULL));
348:     for (PetscInt transpose = 0; transpose < 2; ++transpose) {
349:       if (transpose) PetscCall(PCMatApplyTranspose(pc, X, Y));
350:       else PetscCall(PCMatApply(pc, X, Y));
351:       for (PetscInt j = 0; j < nrhs; ++j) {
352:         Vec xc, yc;

354:         PetscCall(MatDenseGetColumnVecRead(X, j, &xc));
355:         PetscCall(MatDenseGetColumnVecRead(Y, j, &yc));
356:         if (transpose) PetscCall(MatMultTranspose(T, xc, expected));
357:         else PetscCall(MatMult(T, xc, expected));
358:         PetscCall(CheckVector(yc, expected));
359:         PetscCall(MatDenseRestoreColumnVecRead(Y, j, &yc));
360:         PetscCall(MatDenseRestoreColumnVecRead(X, j, &xc));
361:       }
362:     }
363:     PetscCall(MatDestroy(&X));
364:     PetscCall(MatDestroy(&Y));
365:   }
366:   PetscCall(VecDestroy(&x));
367:   PetscCall(VecDestroy(&y));
368:   PetscCall(VecDestroy(&expected));
369:   PetscFunctionReturn(PETSC_SUCCESS);
370: }

372: // checks that the PC holds the shared IS of PCHPDDMSetDeflationMat() and PCHPDDMSetScaling() exactly when one of them uses it
373: static PetscErrorCode CheckReferences(IS is, PetscBool held)
374: {
375:   PetscInt refct;

377:   PetscFunctionBeginUser;
378:   PetscCall(PetscObjectGetReference((PetscObject)is, &refct));
379:   PetscCheck(refct == (held ? 2 : 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "IS has %" PetscInt_FMT " references, expected %d", refct, held ? 2 : 1);
380:   PetscFunctionReturn(PETSC_SUCCESS);
381: }

383: int main(int argc, char **argv)
384: {
385:   PC                          pc;
386:   Mat                         A, U, T;
387:   IS                          is;
388:   Vec                         D;
389:   PetscMPIInt                 size;
390:   PetscBool                   empty_rank0 = PETSC_FALSE, scaling_only = PETSC_FALSE;
391:   FineLevelType               fine = FINE_ASM_WEIGHTED;
392:   PCHPDDMCoarseCorrectionType correction;

394:   PetscFunctionBeginUser;
395:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
396:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
397:   PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This example requires 2 processes");
398:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-empty_rank0", &empty_rank0, NULL));
399:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-scaling_only", &scaling_only, NULL)); // no deflation space, the scaling only applies to the fine level
400:   PetscCall(GetFineLevelType(scaling_only ? "pc_hpddm_coarse_" : "pc_hpddm_levels_1_", &fine));
401:   PetscCall(CreateOperator(&A));
402:   PetscCall(PCCreate(PETSC_COMM_WORLD, &pc));
403:   PetscCall(PCSetType(pc, PCHPDDM));
404:   /*
405:     Three setups of the same PC, each checked against the reference operator:
406:     0. initial setup, after installing and removing the deflation space and scaling in both orders, which must leave
407:        no trace, and with the PC holding their shared IS exactly while one of them uses it;
408:     1. new operator values with the same deflation space and scaling: both levels are rebuilt, and the fine-level
409:        PCASMType selected at the initial setup must persist;
410:     2. PCReset() discards the deflation space and scaling, which are then installed again, the scaling first.
411:   */
412:   for (PetscInt pass = 0; pass < 3; ++pass) {
413:     if (pass == 1) PetscCall(MatScale(A, 1.1));
414:     else {
415:       if (pass == 2) PetscCall(PCReset(pc));
416:       PetscCall(CreateDeflation(empty_rank0, &is, &U, &D));
417:       if (scaling_only) PetscCall(PCHPDDMSetScaling(pc, is, D));
418:       else if (pass == 0) {
419:         PetscCall(PCHPDDMSetScaling(pc, is, D)); // the scaling alone stores the IS
420:         PetscCall(CheckReferences(is, PETSC_TRUE));
421:         PetscCall(PCHPDDMSetDeflationMat(pc, NULL, U)); // reuses the stored IS and keeps the scaling
422:         PetscCall(PCHPDDMSetScaling(pc, NULL, NULL));
423:         PetscCall(CheckReferences(is, PETSC_TRUE)); // still used by the deflation space
424:         PetscCall(PCHPDDMSetScaling(pc, NULL, D));
425:         PetscCall(PCHPDDMSetDeflationMat(pc, NULL, NULL));
426:         PetscCall(CheckReferences(is, PETSC_TRUE)); // still used by the scaling
427:         PetscCall(PCHPDDMSetDeflationMat(pc, is, U));
428:         PetscCall(PCHPDDMSetScaling(pc, NULL, NULL));
429:         PetscCall(PCHPDDMSetDeflationMat(pc, NULL, NULL));
430:         PetscCall(CheckReferences(is, PETSC_FALSE));
431:       }
432:       if (!scaling_only) {
433:         if (pass == 2) { // the deflation space reuses the IS stored by the scaling
434:           PetscCall(PCHPDDMSetScaling(pc, is, D));
435:           PetscCall(PCHPDDMSetDeflationMat(pc, NULL, U));
436:         } else {
437:           IS copy;

439:           PetscCall(PCHPDDMSetDeflationMat(pc, is, U));
440:           PetscCall(ISDuplicate(is, &copy)); // replace the stored IS while keeping the deflation matrix
441:           PetscCall(PCHPDDMSetScaling(pc, copy, D));
442:           PetscCall(CheckReferences(is, PETSC_FALSE));
443:           PetscCall(CheckReferences(copy, PETSC_TRUE));
444:           PetscCall(ISDestroy(&copy));
445:         }
446:       }
447:       PetscCall(VecDestroy(&D)); // the PC keeps its own reference
448:       PetscCall(ISDestroy(&is));
449:       PetscCall(MatDestroy(&U));
450:     }
451:     PetscCall(PCSetOperators(pc, A, A));
452:     PetscCall(PCSetModifySubMatrices(pc, ModifySubmatrices, NULL));
453:     PetscCall(PCSetFromOptions(pc));
454:     PetscCall(PCSetUp(pc));
455:     if (scaling_only) PetscCall(CreateFineReference(A, fine, &T));
456:     else {
457:       PetscCall(PCHPDDMGetCoarseCorrectionType(pc, &correction));
458:       PetscCall(CreateReference(A, empty_rank0, fine, correction, &T));
459:     }
460:     PetscCall(CheckApplications(pc, T));
461:     PetscCall(MatDestroy(&T));
462:   }
463:   PetscCall(PCDestroy(&pc));
464:   PetscCall(MatDestroy(&A));
465:   PetscCall(PetscFinalize());
466:   return 0;
467: }

469: /*TEST

471:   testset:
472:     # every case runs with and without a deflation vector on process 0
473:     nsize: 2
474:     requires: hpddm slepc defined(PETSC_HAVE_DYNAMIC_LIBRARIES) defined(PETSC_USE_SHARED_LIBRARIES)
475:     args: -pc_hpddm_define_subdomains true -pc_hpddm_coarse_mat_type aij -empty_rank0 {{false true}}
476:     output_file: output/empty.out
477:     test:
478:       # default fine level: PCASM of type PC_ASM_WEIGHTED, with the same scaling as the coarse space
479:       suffix: weighted
480:       args: -pc_hpddm_levels_1_sub_pc_type lu
481:     test:
482:       # same fine level with the fine-level correction applied first
483:       suffix: weighted_reversed
484:       args: -pc_hpddm_levels_1_sub_pc_type lu -pc_hpddm_coarse_correction deflated_reversed
485:     test:
486:       suffix: additive
487:       args: -pc_hpddm_levels_1_sub_pc_type lu -pc_hpddm_coarse_correction additive
488:     test:
489:       suffix: balanced
490:       args: -pc_hpddm_levels_1_sub_pc_type lu -pc_hpddm_coarse_correction balanced
491:     test:
492:       # PCASM of type PC_ASM_BASIC: the scaling only applies to the coarse space
493:       # with one subdomain per process, multiplicative composition gives the same operator as additive, but it checks
494:       # that the second setup keeps the selected PCASMType instead of reverting to PC_ASM_WEIGHTED
495:       suffix: basic
496:       args: -pc_hpddm_levels_1_sub_pc_type lu -pc_hpddm_levels_1_pc_asm_type basic -pc_hpddm_levels_1_pc_asm_local_type {{additive multiplicative}}
497:     test:
498:       suffix: gasm
499:       args: -pc_hpddm_levels_1_pc_type gasm -pc_hpddm_levels_1_pc_gasm_type basic -pc_hpddm_levels_1_pc_gasm_overlap 2 -pc_hpddm_levels_1_sub_pc_type lu
500:     test:
501:       # non-PCASM fine level: the scaling only applies to the coarse space
502:       suffix: jacobi
503:       args: -pc_hpddm_levels_1_pc_type jacobi

505:   test:
506:     # no deflation space: the scaling only applies to the fine-level PCASM of a single-level PC, whose prefix is -pc_hpddm_coarse_
507:     suffix: scaling_only
508:     nsize: 2
509:     requires: hpddm slepc defined(PETSC_HAVE_DYNAMIC_LIBRARIES) defined(PETSC_USE_SHARED_LIBRARIES)
510:     args: -pc_hpddm_define_subdomains true -scaling_only -pc_hpddm_coarse_sub_pc_type lu
511:     output_file: output/empty.out

513: TEST*/