Actual source code: ex8.c
1: static char help[] = "Illustrates use of the preconditioner ASM.\n\
2: The Additive Schwarz Method for solving a linear system in parallel with KSP. The\n\
3: code indicates the procedure for setting user-defined subdomains. Input\n\
4: parameters include:\n\
5: -user_set_subdomain_solvers: User explicitly sets subdomain solvers\n\
6: -user_set_subdomains: Activate user-defined subdomains\n\n";
8: /*
9: Note: This example focuses on setting the subdomains for the ASM
10: preconditioner for a problem on a 2D rectangular grid. See ex1.c
11: and ex2.c for more detailed comments on the basic usage of KSP
12: (including working with matrices and vectors).
14: The ASM preconditioner is fully parallel, but currently the routine
15: PCASMCreateSubdomains2D(), which is used in this example to demonstrate
16: user-defined subdomains (activated via -user_set_subdomains), is
17: uniprocessor only.
19: This matrix in this linear system arises from the discretized Laplacian,
20: and thus is not very interesting in terms of experimenting with variants
21: of the ASM preconditioner.
22: */
24: /*
25: Include "petscksp.h" so that we can use KSP solvers. Note that this file
26: automatically includes:
27: petscsys.h - base PETSc routines petscvec.h - vectors
28: petscmat.h - matrices
29: petscis.h - index sets petscksp.h - Krylov subspace methods
30: petscviewer.h - viewers petscpc.h - preconditioners
31: */
32: #include <petscksp.h>
34: int main(int argc, char **args)
35: {
36: Vec x, b, u; /* approx solution, RHS, exact solution */
37: Mat A; /* linear system matrix */
38: KSP ksp; /* linear solver context */
39: PC pc; /* PC context */
40: IS *is, *is_local; /* array of index sets that define the subdomains */
41: PetscInt overlap = 1; /* width of subdomain overlap */
42: PetscInt Nsub; /* number of subdomains */
43: PetscInt m = 15, n = 17; /* mesh dimensions in x- and y- directions */
44: PetscInt M = 2, N = 1; /* number of subdomains in x- and y- directions */
45: PetscInt i, j, Ii, J, Istart, Iend;
46: PetscMPIInt size;
47: PetscBool flg;
48: PetscBool user_subdomains = PETSC_FALSE;
49: PCASMType asmtype;
50: PetscScalar v, one = 1.0;
51: PetscReal e;
53: PetscFunctionBeginUser;
54: PetscCall(PetscInitialize(&argc, &args, NULL, help));
55: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
56: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
57: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
58: PetscCall(PetscOptionsGetInt(NULL, NULL, "-Mdomains", &M, NULL));
59: PetscCall(PetscOptionsGetInt(NULL, NULL, "-Ndomains", &N, NULL));
60: PetscCall(PetscOptionsGetInt(NULL, NULL, "-overlap", &overlap, NULL));
61: PetscCall(PetscOptionsGetBool(NULL, NULL, "-user_set_subdomains", &user_subdomains, NULL));
63: /* -------------------------------------------------------------------
64: Compute the matrix and right-hand-side vector that define
65: the linear system, Ax = b.
66: ------------------------------------------------------------------- */
68: /*
69: Assemble the matrix for the five point stencil, YET AGAIN
70: */
71: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
72: PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n));
73: PetscCall(MatSetFromOptions(A));
74: PetscCall(MatSetUp(A));
75: PetscCall(MatGetOwnershipRange(A, &Istart, &Iend));
76: for (Ii = Istart; Ii < Iend; Ii++) {
77: v = -1.0;
78: i = Ii / n;
79: j = Ii - i * n;
80: if (i > 0) {
81: J = Ii - n;
82: PetscCall(MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES));
83: }
84: if (i < m - 1) {
85: J = Ii + n;
86: PetscCall(MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES));
87: }
88: if (j > 0) {
89: J = Ii - 1;
90: PetscCall(MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES));
91: }
92: if (j < n - 1) {
93: J = Ii + 1;
94: PetscCall(MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES));
95: }
96: v = 4.0;
97: PetscCall(MatSetValues(A, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
98: }
99: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
100: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
102: /*
103: Create and set vectors
104: */
105: PetscCall(MatCreateVecs(A, &u, &b));
106: PetscCall(VecDuplicate(u, &x));
107: PetscCall(VecSet(u, one));
108: PetscCall(MatMult(A, u, b));
110: /*
111: Create linear solver context
112: */
113: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
115: /*
116: Set operators. Here the matrix that defines the linear system
117: also serves as the matrix from which the preconditioner is constructed.
118: */
119: PetscCall(KSPSetOperators(ksp, A, A));
121: /*
122: Set the default preconditioner for this program to be ASM
123: */
124: PetscCall(KSPGetPC(ksp, &pc));
125: PetscCall(PCSetType(pc, PCASM));
127: /* -------------------------------------------------------------------
128: Define the problem decomposition
129: ------------------------------------------------------------------- */
131: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132: Basic method, should be sufficient for the needs of many users.
133: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135: Set the overlap, using the default PETSc decomposition via
136: PCASMSetOverlap(pc,overlap);
137: Could instead use the option -pc_asm_overlap <ovl>
139: Set the total number of blocks via -pc_asm_blocks <blks>
140: Note: The ASM default is to use 1 block per processor. To
141: experiment on a single processor with various overlaps, you
142: must specify use of multiple blocks!
143: */
145: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146: More advanced method, setting user-defined subdomains
147: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149: Firstly, create index sets that define the subdomains. The utility
150: routine PCASMCreateSubdomains2D() is a simple example (that currently
151: supports 1 processor only!). More generally, the user should write
152: a custom routine for a particular problem geometry.
154: Then call either PCASMSetLocalSubdomains() or PCASMSetTotalSubdomains()
155: to set the subdomains for the ASM preconditioner.
156: */
158: if (!user_subdomains) { /* basic version */
159: PetscCall(PCASMSetOverlap(pc, overlap));
160: } else { /* advanced version */
161: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_SUP, "PCASMCreateSubdomains2D() is currently a uniprocessor routine only!");
162: PetscCall(PCASMCreateSubdomains2D(m, n, M, N, 1, overlap, &Nsub, &is, &is_local));
163: PetscCall(PCASMSetLocalSubdomains(pc, Nsub, is, is_local));
164: flg = PETSC_FALSE;
165: PetscCall(PetscOptionsGetBool(NULL, NULL, "-subdomain_view", &flg, NULL));
166: if (flg) {
167: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Nmesh points: %" PetscInt_FMT " x %" PetscInt_FMT "; subdomain partition: %" PetscInt_FMT " x %" PetscInt_FMT "; overlap: %" PetscInt_FMT "; Nsub: %" PetscInt_FMT "\n", m, n, M, N, overlap, Nsub));
168: PetscCall(PetscPrintf(PETSC_COMM_SELF, "IS:\n"));
169: for (i = 0; i < Nsub; i++) {
170: PetscCall(PetscPrintf(PETSC_COMM_SELF, " IS[%" PetscInt_FMT "]\n", i));
171: PetscCall(ISView(is[i], PETSC_VIEWER_STDOUT_SELF));
172: }
173: PetscCall(PetscPrintf(PETSC_COMM_SELF, "IS_local:\n"));
174: for (i = 0; i < Nsub; i++) {
175: PetscCall(PetscPrintf(PETSC_COMM_SELF, " IS_local[%" PetscInt_FMT "]\n", i));
176: PetscCall(ISView(is_local[i], PETSC_VIEWER_STDOUT_SELF));
177: }
178: }
179: }
181: /* -------------------------------------------------------------------
182: Set the linear solvers for the subblocks
183: ------------------------------------------------------------------- */
185: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186: Basic method, should be sufficient for the needs of most users.
187: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
189: By default, the ASM preconditioner uses the same solver on each
190: block of the problem. To set the same solver options on all blocks,
191: use the prefix -sub before the usual PC and KSP options, e.g.,
192: -sub_pc_type <pc> -sub_ksp_type <ksp> -sub_ksp_rtol 1.e-4
194: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195: Advanced method, setting different solvers for various blocks.
196: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
198: Note that each block's KSP context is completely independent of
199: the others, and the full range of uniprocessor KSP options is
200: available for each block.
202: - Use PCASMGetSubKSP() to extract the array of KSP contexts for
203: the local blocks.
204: - See ex7.c for a simple example of setting different linear solvers
205: for the individual blocks for the block Jacobi method (which is
206: equivalent to the ASM method with zero overlap).
207: */
209: flg = PETSC_FALSE;
210: PetscCall(PetscOptionsGetBool(NULL, NULL, "-user_set_subdomain_solvers", &flg, NULL));
211: if (flg) {
212: KSP *subksp; /* array of KSP contexts for local subblocks */
213: PetscInt nlocal, first; /* number of local subblocks, first local subblock */
214: PC subpc; /* PC context for subblock */
215: PetscBool isasm;
217: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "User explicitly sets subdomain solvers.\n"));
219: /*
220: Set runtime options
221: */
222: PetscCall(KSPSetFromOptions(ksp));
224: /*
225: Flag an error if PCTYPE is changed from the runtime options
226: */
227: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &isasm));
228: PetscCheck(isasm, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Cannot Change the PCTYPE when manually changing the subdomain solver settings");
230: /*
231: Call KSPSetUp() to set the block Jacobi data structures (including
232: creation of an internal KSP context for each block).
234: Note: KSPSetUp() MUST be called before PCASMGetSubKSP().
235: */
236: PetscCall(KSPSetUp(ksp));
238: /*
239: Extract the array of KSP contexts for the local blocks
240: */
241: PetscCall(PCASMGetSubKSP(pc, &nlocal, &first, &subksp));
243: /*
244: Loop over the local blocks, setting various KSP options
245: for each block.
246: */
247: for (i = 0; i < nlocal; i++) {
248: PetscCall(KSPGetPC(subksp[i], &subpc));
249: PetscCall(PCSetType(subpc, PCILU));
250: PetscCall(KSPSetType(subksp[i], KSPGMRES));
251: PetscCall(KSPSetTolerances(subksp[i], 1.e-7, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT));
252: }
253: } else {
254: /*
255: Set runtime options
256: */
257: PetscCall(KSPSetFromOptions(ksp));
258: }
260: /*
261: PC_ASM_WEIGHTED scales each subdomain correction by user-supplied diagonal weights, so
262: unlike the other PCASMType values it cannot be selected from the options database alone.
263: The weights follow the overlapping subdomain ordering, which is only final after setup,
264: hence the KSPSetUp() below. Unit weights reproduce PC_ASM_BASIC exactly.
265: */
266: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &flg));
267: if (flg) PetscCall(PCASMGetType(pc, &asmtype));
268: if (flg && asmtype == PC_ASM_WEIGHTED) {
269: Mat *submat;
270: Vec *scaling, *stored;
271: PetscInt nsub, nstored;
273: PetscCall(KSPSetUp(ksp));
275: /* the PC is set up but no weights have been supplied, so the getter must report an empty array */
276: PetscCall(PCASMWeightedGetScaling(pc, &nstored, &stored));
277: PetscCheck(!nstored && !stored, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCASMWeightedGetScaling() reported weights before any were supplied");
279: PetscCall(PCASMGetLocalSubmatrices(pc, &nsub, &submat));
280: PetscCall(PetscMalloc1(nsub, &scaling));
281: for (i = 0; i < nsub; i++) PetscCall(MatCreateVecs(submat[i], &scaling[i], NULL));
283: /*
284: A vanishing partition of unity must annihilate the correction. This is applied with
285: PCApply() rather than KSPSolve() because a zero preconditioner makes no progress.
286: */
287: flg = PETSC_FALSE;
288: PetscCall(PetscOptionsGetBool(NULL, NULL, "-check_zero_weights", &flg, NULL));
289: if (flg) {
290: for (i = 0; i < nsub; i++) PetscCall(VecSet(scaling[i], 0.0));
291: PetscCall(PCASMWeightedSetScaling(pc, nsub, scaling));
292: PetscCall(PCApply(pc, b, x));
293: PetscCall(VecNorm(x, NORM_INFINITY, &e));
294: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Zero weights annihilate the correction: %s\n", PetscBools[e == 0.0]));
295: }
297: /* installing a second time also exercises replacing the weights on an already set up PC */
298: for (i = 0; i < nsub; i++) PetscCall(VecSet(scaling[i], 1.0));
299: PetscCall(PCASMWeightedSetScaling(pc, nsub, scaling));
300: PetscCall(PCASMWeightedGetScaling(pc, &nstored, &stored));
301: PetscCheck(nstored == nsub && stored, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCASMWeightedGetScaling() did not return the supplied weights");
302: for (i = 0; i < nsub; i++) PetscCall(VecDestroy(&scaling[i]));
303: PetscCall(PetscFree(scaling));
304: }
306: /* -------------------------------------------------------------------
307: Solve the linear system
308: ------------------------------------------------------------------- */
310: PetscCall(KSPSolve(ksp, b, x));
312: /* -------------------------------------------------------------------
313: Compare result to the exact solution
314: ------------------------------------------------------------------- */
315: PetscCall(VecAXPY(x, -1.0, u));
316: PetscCall(VecNorm(x, NORM_INFINITY, &e));
318: flg = PETSC_FALSE;
319: PetscCall(PetscOptionsGetBool(NULL, NULL, "-print_error", &flg, NULL));
320: if (flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Infinity norm of the error: %g\n", (double)e));
322: /*
323: Free work space. All PETSc objects should be destroyed when they
324: are no longer needed.
325: */
327: if (user_subdomains) {
328: for (i = 0; i < Nsub; i++) {
329: PetscCall(ISDestroy(&is[i]));
330: PetscCall(ISDestroy(&is_local[i]));
331: }
332: PetscCall(PetscFree(is));
333: PetscCall(PetscFree(is_local));
334: }
335: PetscCall(KSPDestroy(&ksp));
336: PetscCall(VecDestroy(&u));
337: PetscCall(VecDestroy(&x));
338: PetscCall(VecDestroy(&b));
339: PetscCall(MatDestroy(&A));
340: PetscCall(PetscFinalize());
341: return 0;
342: }
344: /*TEST
346: test:
347: suffix: 1
348: args: -print_error
350: test:
351: suffix: weighted
352: nsize: 2
353: args: -ksp_converged_reason -mat_partitioning_type current -pc_asm_blocks 4 -pc_asm_type {{basic weighted}shared output}
355: test:
356: suffix: weighted_zero
357: nsize: 2
358: args: -pc_asm_blocks 4 -pc_asm_type weighted -check_zero_weights
360: TEST*/