Actual source code: gasm.c
1: /*
2: This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation.
3: In this version, each MPI process may intersect multiple subdomains and any subdomain may
4: intersect multiple MPI processes. Intersections of subdomains with MPI processes are called *local
5: subdomains*.
7: N - total number of distinct global subdomains (set explicitly in PCGASMSetTotalSubdomains() or implicitly PCGASMSetSubdomains() and then calculated in PCSetUp_GASM())
8: n - actual number of local subdomains on this process (set in `PCGASMSetSubdomains()` or calculated in `PCGASMSetTotalSubdomains()`)
9: nmax - maximum number of local subdomains per process (calculated in PCSetUp_GASM())
10: */
11: #include <petsc/private/pcimpl.h>
12: #include <petscdm.h>
14: typedef struct {
15: PetscInt N, n, nmax;
16: PetscInt overlap; /* overlap requested by user */
17: PCGASMType type; /* use reduced interpolation, restriction or both */
18: PetscBool type_set; /* if user set this value (so won't change it for symmetric problems) */
19: PetscBool same_subdomain_solvers; /* flag indicating whether all local solvers are same */
20: PetscBool sort_indices; /* flag to sort subdomain indices */
21: PetscBool user_subdomains; /* whether the user set explicit subdomain index sets -- keep them on PCReset() */
22: PetscBool dm_subdomains; /* whether DM is allowed to define subdomains */
23: PetscBool hierarchicalpartitioning;
24: IS *ois; /* index sets that define the outer (conceptually, overlapping) subdomains */
25: IS *iis; /* index sets that define the inner (conceptually, nonoverlapping) subdomains */
26: KSP *ksp; /* linear solvers for each subdomain */
27: Mat *pmat; /* subdomain block matrices */
28: Vec gx, gy; /* Merged work vectors */
29: Vec *x, *y; /* Split work vectors; storage aliases pieces of storage of the above merged vectors. */
30: VecScatter gorestriction; /* merged restriction to disjoint union of outer subdomains */
31: VecScatter girestriction; /* merged restriction to disjoint union of inner subdomains */
32: VecScatter pctoouter;
33: IS permutationIS;
34: Mat permutationP;
35: Mat pcmat;
36: Vec pcx, pcy;
37: } PC_GASM;
39: static PetscErrorCode PCGASMComputeGlobalSubdomainNumbering_Private(PC pc, PetscInt **numbering, PetscInt **permutation)
40: {
41: PC_GASM *osm = (PC_GASM *)pc->data;
42: PetscInt i;
44: PetscFunctionBegin;
45: /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
46: PetscCall(PetscMalloc2(osm->n, numbering, osm->n, permutation));
47: PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->iis, NULL, *numbering));
48: for (i = 0; i < osm->n; ++i) (*permutation)[i] = i;
49: PetscCall(PetscSortIntWithPermutation(osm->n, *numbering, *permutation));
50: PetscFunctionReturn(PETSC_SUCCESS);
51: }
53: static PetscErrorCode PCGASMSubdomainView_Private(PC pc, PetscInt i, PetscViewer viewer)
54: {
55: PC_GASM *osm = (PC_GASM *)pc->data;
56: PetscInt nidx;
57: const PetscInt *idx;
58: PetscViewer sviewer;
59: char *cidx;
61: PetscFunctionBegin;
62: PetscCheck(i >= -1 && i < osm->n, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONG, "Invalid subdomain %" PetscInt_FMT ": must nonnegative and less than %" PetscInt_FMT, i, osm->n);
64: /* Inner subdomains. */
65: /*
66: No more than 15 characters per index plus a space.
67: PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
68: in case nidx == 0. That will take care of the space for the trailing '\0' as well.
69: For nidx == 0, the whole string 16 '\0'.
70: */
71: PetscCall(PetscViewerASCIIPrintf(viewer, "Inner subdomain:\n"));
72: PetscCall(PetscViewerFlush(viewer));
73: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
74: if (i > -1) {
75: PetscCall(ISGetLocalSize(osm->iis[i], &nidx));
76: PetscCall(PetscMalloc1(16 * (nidx + 1) + 1, &cidx));
77: PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, 16 * (nidx + 1) + 1, &sviewer));
78: PetscCall(ISGetIndices(osm->iis[i], &idx));
79: for (PetscInt j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
80: PetscCall(ISRestoreIndices(osm->iis[i], &idx));
81: PetscCall(PetscViewerDestroy(&sviewer));
82: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
83: PetscCall(PetscFree(cidx));
84: }
85: PetscCall(PetscViewerFlush(viewer));
86: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
87: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
88: PetscCall(PetscViewerFlush(viewer));
90: /* Outer subdomains. */
91: /*
92: No more than 15 characters per index plus a space.
93: PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
94: in case nidx == 0. That will take care of the space for the trailing '\0' as well.
95: For nidx == 0, the whole string 16 '\0'.
96: */
97: PetscCall(PetscViewerASCIIPrintf(viewer, "Outer subdomain:\n"));
98: PetscCall(PetscViewerFlush(viewer));
99: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
100: if (i > -1) {
101: PetscCall(ISGetLocalSize(osm->ois[i], &nidx));
102: PetscCall(PetscMalloc1(16 * (nidx + 1) + 1, &cidx));
103: PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, 16 * (nidx + 1) + 1, &sviewer));
104: PetscCall(ISGetIndices(osm->ois[i], &idx));
105: for (PetscInt j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
106: PetscCall(PetscViewerDestroy(&sviewer));
107: PetscCall(ISRestoreIndices(osm->ois[i], &idx));
108: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
109: PetscCall(PetscFree(cidx));
110: }
111: PetscCall(PetscViewerFlush(viewer));
112: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
113: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
114: PetscCall(PetscViewerFlush(viewer));
115: PetscFunctionReturn(PETSC_SUCCESS);
116: }
118: static PetscErrorCode PCGASMPrintSubdomains(PC pc)
119: {
120: PC_GASM *osm = (PC_GASM *)pc->data;
121: const char *prefix;
122: char fname[PETSC_MAX_PATH_LEN + 1];
123: PetscInt l, d, count;
124: PetscBool found;
125: PetscViewer viewer;
126: PetscInt *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
128: PetscFunctionBegin;
129: PetscCall(PCGetOptionsPrefix(pc, &prefix));
130: PetscCall(PetscOptionsHasName(NULL, prefix, "-pc_gasm_print_subdomains", &found));
131: if (!found) PetscFunctionReturn(PETSC_SUCCESS);
132: PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_gasm_print_subdomains", fname, sizeof(fname), &found));
133: if (!found) PetscCall(PetscStrncpy(fname, "stdout", sizeof(fname)));
134: PetscCall(PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc), fname, &viewer));
135: /*
136: Make sure the viewer has a name. Otherwise this may cause a deadlock or other weird errors when creating a subcomm viewer:
137: the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed collectively on the comm.
138: */
139: PetscCall(PetscObjectName((PetscObject)viewer));
140: l = 0;
141: PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
142: for (count = 0; count < osm->N; ++count) {
143: /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
144: if (l < osm->n) {
145: d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
146: if (numbering[d] == count) l++;
147: else d = -1;
148: } else d = -1;
149: PetscCall(PCGASMSubdomainView_Private(pc, d, viewer));
150: }
151: PetscCall(PetscFree2(numbering, permutation));
152: PetscCall(PetscViewerDestroy(&viewer));
153: PetscFunctionReturn(PETSC_SUCCESS);
154: }
156: static PetscErrorCode PCView_GASM(PC pc, PetscViewer viewer)
157: {
158: PC_GASM *osm = (PC_GASM *)pc->data;
159: const char *prefix;
160: PetscMPIInt rank, size;
161: PetscInt bsz;
162: PetscBool isascii, view_subdomains = PETSC_FALSE;
163: PetscViewer sviewer;
164: PetscInt l;
165: char overlap[256] = "user-defined overlap";
166: char gsubdomains[256] = "unknown total number of subdomains";
167: char msubdomains[256] = "unknown max number of local subdomains";
168: PetscInt *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
170: PetscFunctionBegin;
171: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
172: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
174: if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlap, sizeof(overlap), "requested amount of overlap = %" PetscInt_FMT, osm->overlap));
175: if (osm->N != PETSC_DETERMINE) PetscCall(PetscSNPrintf(gsubdomains, sizeof(gsubdomains), "total number of subdomains = %" PetscInt_FMT, osm->N));
176: if (osm->nmax != PETSC_DETERMINE) PetscCall(PetscSNPrintf(msubdomains, sizeof(msubdomains), "max number of local subdomains = %" PetscInt_FMT, osm->nmax));
178: PetscCall(PCGetOptionsPrefix(pc, &prefix));
179: PetscCall(PetscOptionsGetBool(NULL, prefix, "-pc_gasm_view_subdomains", &view_subdomains, NULL));
181: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
182: if (isascii) {
183: /*
184: Make sure the viewer has a name. Otherwise this may cause a deadlock when creating a subcomm viewer:
185: the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed
186: collectively on the comm.
187: */
188: PetscCall(PetscObjectName((PetscObject)viewer));
189: PetscCall(PetscViewerASCIIPrintf(viewer, " Restriction/interpolation type: %s\n", PCGASMTypes[osm->type]));
190: PetscCall(PetscViewerASCIIPrintf(viewer, " %s\n", overlap));
191: PetscCall(PetscViewerASCIIPrintf(viewer, " %s\n", gsubdomains));
192: PetscCall(PetscViewerASCIIPrintf(viewer, " %s\n", msubdomains));
193: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
194: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d|%d] number of locally-supported subdomains = %" PetscInt_FMT "\n", rank, size, osm->n));
195: PetscCall(PetscViewerFlush(viewer));
196: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
197: /* Cannot take advantage of osm->same_subdomain_solvers without a global numbering of subdomains. */
198: PetscCall(PetscViewerASCIIPrintf(viewer, " Subdomain solver info:\n"));
199: PetscCall(PetscViewerASCIIPushTab(viewer));
200: PetscCall(PetscViewerASCIIPrintf(viewer, " - - - - - - - - - - - - - - - - - -\n"));
201: /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
202: PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
203: l = 0;
204: for (PetscInt count = 0; count < osm->N; ++count) {
205: PetscMPIInt srank, ssize;
206: if (l < osm->n) {
207: PetscInt d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
208: if (numbering[d] == count) {
209: PetscCallMPI(MPI_Comm_size(((PetscObject)osm->ois[d])->comm, &ssize));
210: PetscCallMPI(MPI_Comm_rank(((PetscObject)osm->ois[d])->comm, &srank));
211: PetscCall(PetscViewerGetSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
212: PetscCall(ISGetLocalSize(osm->ois[d], &bsz));
213: PetscCall(PetscViewerASCIISynchronizedPrintf(sviewer, " [%d|%d] (subcomm [%d|%d]) local subdomain number %" PetscInt_FMT ", local size = %" PetscInt_FMT "\n", rank, size, srank, ssize, d, bsz));
214: PetscCall(PetscViewerFlush(sviewer));
215: PetscCall(PetscViewerASCIIPushTab(sviewer));
216: if (view_subdomains) PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
217: if (!pc->setupcalled) {
218: PetscCall(PetscViewerASCIISynchronizedPrintf(sviewer, " Solver not set up yet: PCSetUp() not yet called\n"));
219: } else {
220: PetscCall(KSPView(osm->ksp[d], sviewer));
221: }
222: PetscCall(PetscViewerASCIIPopTab(sviewer));
223: PetscCall(PetscViewerASCIIPrintf(sviewer, " - - - - - - - - - - - - - - - - - -\n"));
224: PetscCall(PetscViewerFlush(sviewer));
225: PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
226: ++l;
227: } else {
228: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
229: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
230: }
231: } else {
232: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
233: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
234: }
235: }
236: PetscCall(PetscFree2(numbering, permutation));
237: PetscCall(PetscViewerASCIIPopTab(viewer));
238: PetscCall(PetscViewerFlush(viewer));
239: /* this line is needed to match the extra PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
240: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
241: }
242: PetscFunctionReturn(PETSC_SUCCESS);
243: }
245: PETSC_INTERN PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]);
247: static PetscErrorCode PCGASMSetHierarchicalPartitioning(PC pc)
248: {
249: PC_GASM *osm = (PC_GASM *)pc->data;
250: MatPartitioning part;
251: MPI_Comm comm;
252: PetscMPIInt size;
253: PetscInt nlocalsubdomains, fromrows_localsize;
254: IS partitioning, fromrows, isn;
255: Vec outervec;
257: PetscFunctionBegin;
258: PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
259: PetscCallMPI(MPI_Comm_size(comm, &size));
260: /* we do not need a hierarchical partitioning when
261: * the total number of subdomains is consistent with
262: * the number of MPI tasks.
263: * For the following cases, we do not need to use HP
264: * */
265: if (osm->N == PETSC_DETERMINE || osm->N >= size || osm->N == 1) PetscFunctionReturn(PETSC_SUCCESS);
266: PetscCheck(size % osm->N == 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP, "have to specify the total number of subdomains %" PetscInt_FMT " to be a factor of the number of ranks %d ", osm->N, size);
267: nlocalsubdomains = size / osm->N;
268: osm->n = 1;
269: PetscCall(MatPartitioningCreate(comm, &part));
270: PetscCall(MatPartitioningSetAdjacency(part, pc->pmat));
271: PetscCall(MatPartitioningSetType(part, MATPARTITIONINGHIERARCH));
272: PetscCall(MatPartitioningHierarchicalSetNcoarseparts(part, osm->N));
273: PetscCall(MatPartitioningHierarchicalSetNfineparts(part, nlocalsubdomains));
274: PetscCall(MatPartitioningSetFromOptions(part));
275: /* get new rank owner number of each vertex */
276: PetscCall(MatPartitioningApply(part, &partitioning));
277: PetscCall(ISBuildTwoSided(partitioning, NULL, &fromrows));
278: PetscCall(ISPartitioningToNumbering(partitioning, &isn));
279: PetscCall(ISDestroy(&isn));
280: PetscCall(ISGetLocalSize(fromrows, &fromrows_localsize));
281: PetscCall(MatPartitioningDestroy(&part));
282: PetscCall(MatCreateVecs(pc->pmat, &outervec, NULL));
283: PetscCall(VecCreateMPI(comm, fromrows_localsize, PETSC_DETERMINE, &osm->pcx));
284: PetscCall(VecDuplicate(osm->pcx, &osm->pcy));
285: PetscCall(VecScatterCreate(osm->pcx, NULL, outervec, fromrows, &osm->pctoouter));
286: PetscCall(MatCreateSubMatrix(pc->pmat, fromrows, fromrows, MAT_INITIAL_MATRIX, &osm->permutationP));
287: PetscCall(PetscObjectReference((PetscObject)fromrows));
288: osm->permutationIS = fromrows;
289: osm->pcmat = pc->pmat;
290: PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
291: pc->pmat = osm->permutationP;
292: PetscCall(VecDestroy(&outervec));
293: PetscCall(ISDestroy(&fromrows));
294: PetscCall(ISDestroy(&partitioning));
295: osm->n = PETSC_DETERMINE;
296: PetscFunctionReturn(PETSC_SUCCESS);
297: }
299: static PetscErrorCode PCSetUp_GASM(PC pc)
300: {
301: PC_GASM *osm = (PC_GASM *)pc->data;
302: PetscInt nInnerIndices, nTotalInnerIndices;
303: PetscMPIInt rank, size;
304: MatReuse scall = MAT_REUSE_MATRIX;
305: KSP ksp;
306: PC subpc;
307: const char *prefix, *pprefix;
308: Vec x, y;
309: PetscInt oni; /* Number of indices in the i-th local outer subdomain. */
310: const PetscInt *oidxi; /* Indices from the i-th subdomain local outer subdomain. */
311: PetscInt on; /* Number of indices in the disjoint union of local outer subdomains. */
312: PetscInt *oidx; /* Indices in the disjoint union of local outer subdomains. */
313: IS gois; /* Disjoint union the global indices of outer subdomains. */
314: IS goid; /* Identity IS of the size of the disjoint union of outer subdomains. */
315: PetscInt gostart; /* Start of locally-owned indices in the vectors -- osm->gx,osm->gy -- over the disjoint union of outer subdomains. */
316: PetscInt num_subdomains = 0;
317: DM *subdomain_dm = NULL;
318: char **subdomain_names = NULL;
319: PetscInt *numbering;
321: PetscFunctionBegin;
322: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
323: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
324: if (!pc->setupcalled) {
325: /* use a hierarchical partitioning */
326: if (osm->hierarchicalpartitioning) PetscCall(PCGASMSetHierarchicalPartitioning(pc));
327: if (osm->n == PETSC_DETERMINE) {
328: if (osm->N != PETSC_DETERMINE) {
329: /* No local subdomains given, but the desired number of total subdomains is known, so construct them accordingly. */
330: PetscCall(PCGASMCreateSubdomains(pc->pmat, osm->N, &osm->n, &osm->iis));
331: } else if (osm->dm_subdomains && pc->dm) {
332: /* try pc->dm next, if allowed */
333: IS *inner_subdomain_is, *outer_subdomain_is;
334: PetscCall(DMCreateDomainDecomposition(pc->dm, &num_subdomains, &subdomain_names, &inner_subdomain_is, &outer_subdomain_is, &subdomain_dm));
335: if (num_subdomains) PetscCall(PCGASMSetSubdomains(pc, num_subdomains, inner_subdomain_is, outer_subdomain_is));
336: for (PetscInt d = 0; d < num_subdomains; ++d) {
337: if (inner_subdomain_is) PetscCall(ISDestroy(&inner_subdomain_is[d]));
338: if (outer_subdomain_is) PetscCall(ISDestroy(&outer_subdomain_is[d]));
339: }
340: PetscCall(PetscFree(inner_subdomain_is));
341: PetscCall(PetscFree(outer_subdomain_is));
342: } else {
343: /* still no subdomains; use one per rank */
344: osm->nmax = osm->n = 1;
345: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
346: osm->N = size;
347: PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
348: }
349: }
350: if (!osm->iis) {
351: /*
352: osm->n was set in PCGASMSetSubdomains(), but the actual subdomains have not been supplied.
353: We create the requisite number of local inner subdomains and then expand them into
354: out subdomains, if necessary.
355: */
356: PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
357: }
358: if (!osm->ois) {
359: /*
360: Initially make outer subdomains the same as inner subdomains. If nonzero additional overlap
361: has been requested, copy the inner subdomains over so they can be modified.
362: */
363: PetscCall(PetscMalloc1(osm->n, &osm->ois));
364: for (PetscInt i = 0; i < osm->n; ++i) {
365: if (osm->overlap > 0 && osm->N > 1) { /* With positive overlap, osm->iis[i] will be modified */
366: PetscCall(ISDuplicate(osm->iis[i], (osm->ois) + i));
367: PetscCall(ISCopy(osm->iis[i], osm->ois[i]));
368: } else {
369: PetscCall(PetscObjectReference((PetscObject)osm->iis[i]));
370: osm->ois[i] = osm->iis[i];
371: }
372: }
373: if (osm->overlap > 0 && osm->N > 1) {
374: /* Extend the "overlapping" regions by a number of steps */
375: PetscCall(MatIncreaseOverlapSplit(pc->pmat, osm->n, osm->ois, osm->overlap));
376: }
377: }
379: /* Now the subdomains are defined. Determine their global and max local numbers, if necessary. */
380: if (osm->nmax == PETSC_DETERMINE) {
381: PetscInt outwork;
382: /* determine global number of subdomains and the max number of local subdomains */
383: outwork = osm->n;
384: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &outwork, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
385: osm->nmax = outwork;
386: }
387: if (osm->N == PETSC_DETERMINE) {
388: /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
389: PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, &osm->N, NULL));
390: }
392: if (osm->sort_indices) {
393: for (PetscInt i = 0; i < osm->n; i++) {
394: PetscCall(ISSort(osm->ois[i]));
395: PetscCall(ISSort(osm->iis[i]));
396: }
397: }
398: PetscCall(PCGetOptionsPrefix(pc, &prefix));
399: PetscCall(PCGASMPrintSubdomains(pc));
401: /*
402: Merge the ISs, create merged vectors and restrictions.
403: */
404: /* Merge outer subdomain ISs and construct a restriction onto the disjoint union of local outer subdomains. */
405: on = 0;
406: for (PetscInt i = 0; i < osm->n; i++) {
407: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
408: on += oni;
409: }
410: PetscCall(PetscMalloc1(on, &oidx));
411: on = 0;
412: /* Merge local indices together */
413: for (PetscInt i = 0; i < osm->n; i++) {
414: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
415: PetscCall(ISGetIndices(osm->ois[i], &oidxi));
416: PetscCall(PetscArraycpy(oidx + on, oidxi, oni));
417: PetscCall(ISRestoreIndices(osm->ois[i], &oidxi));
418: on += oni;
419: }
420: PetscCall(ISCreateGeneral(((PetscObject)pc)->comm, on, oidx, PETSC_OWN_POINTER, &gois));
421: nTotalInnerIndices = 0;
422: for (PetscInt i = 0; i < osm->n; i++) {
423: PetscCall(ISGetLocalSize(osm->iis[i], &nInnerIndices));
424: nTotalInnerIndices += nInnerIndices;
425: }
426: PetscCall(VecCreateMPI(((PetscObject)pc)->comm, nTotalInnerIndices, PETSC_DETERMINE, &x));
427: PetscCall(VecDuplicate(x, &y));
429: /* Keep the merged vectors on the host so that their arrays also work with host-only subdomain solvers. */
430: PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), on, PETSC_DECIDE, &osm->gx));
431: PetscCall(VecDuplicate(osm->gx, &osm->gy));
432: PetscCall(VecGetOwnershipRange(osm->gx, &gostart, NULL));
433: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), on, gostart, 1, &goid));
434: /* gois might indices not on local */
435: PetscCall(VecScatterCreate(x, gois, osm->gx, goid, &osm->gorestriction));
436: PetscCall(PetscMalloc1(osm->n, &numbering));
437: PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, NULL, numbering));
438: PetscCall(VecDestroy(&x));
439: PetscCall(ISDestroy(&gois));
441: /* Merge inner subdomain ISs and construct a restriction onto the disjoint union of local inner subdomains. */
442: {
443: PetscInt ini; /* Number of indices the i-th a local inner subdomain. */
444: PetscInt in; /* Number of indices in the disjoint union of local inner subdomains. */
445: PetscInt *iidx; /* Global indices in the merged local inner subdomain. */
446: PetscInt *ioidx; /* Global indices of the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
447: IS giis; /* IS for the disjoint union of inner subdomains. */
448: IS giois; /* IS for the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
449: PetscScalar *array;
450: const PetscInt *indices;
451: on = 0;
452: for (PetscInt i = 0; i < osm->n; i++) {
453: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
454: on += oni;
455: }
456: PetscCall(PetscMalloc1(on, &iidx));
457: PetscCall(PetscMalloc1(on, &ioidx));
458: PetscCall(VecGetArray(y, &array));
459: /* set communicator id to determine where overlap is */
460: in = 0;
461: for (PetscInt i = 0; i < osm->n; i++) {
462: PetscCall(ISGetLocalSize(osm->iis[i], &ini));
463: for (PetscInt k = 0; k < ini; ++k) array[in + k] = numbering[i];
464: in += ini;
465: }
466: PetscCall(VecRestoreArray(y, &array));
467: PetscCall(VecScatterBegin(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
468: PetscCall(VecScatterEnd(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
469: PetscCall(VecGetOwnershipRange(osm->gy, &gostart, NULL));
470: PetscCall(VecGetArray(osm->gy, &array));
471: on = 0;
472: in = 0;
473: for (PetscInt i = 0; i < osm->n; i++) {
474: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
475: PetscCall(ISGetIndices(osm->ois[i], &indices));
476: for (PetscInt k = 0; k < oni; k++) {
477: /* skip overlapping indices to get inner domain */
478: if (PetscRealPart(array[on + k]) != numbering[i]) continue;
479: iidx[in] = indices[k];
480: ioidx[in++] = gostart + on + k;
481: }
482: PetscCall(ISRestoreIndices(osm->ois[i], &indices));
483: on += oni;
484: }
485: PetscCall(VecRestoreArray(osm->gy, &array));
486: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, iidx, PETSC_OWN_POINTER, &giis));
487: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, ioidx, PETSC_OWN_POINTER, &giois));
488: PetscCall(VecScatterCreate(y, giis, osm->gy, giois, &osm->girestriction));
489: PetscCall(VecDestroy(&y));
490: PetscCall(ISDestroy(&giis));
491: PetscCall(ISDestroy(&giois));
492: }
493: PetscCall(ISDestroy(&goid));
494: PetscCall(PetscFree(numbering));
496: /* Create the subdomain solvers */
497: PetscCall(PetscMalloc1(osm->n, &osm->ksp));
498: for (PetscInt i = 0; i < osm->n; i++) {
499: char subprefix[PETSC_MAX_PATH_LEN + 1];
500: PetscCall(KSPCreate(((PetscObject)osm->ois[i])->comm, &ksp));
501: PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
502: PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
503: PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
504: PetscCall(KSPSetType(ksp, KSPPREONLY));
505: PetscCall(KSPGetPC(ksp, &subpc)); /* Why do we need this here? */
506: if (subdomain_dm) {
507: PetscCall(KSPSetDM(ksp, subdomain_dm[i]));
508: PetscCall(DMDestroy(subdomain_dm + i));
509: }
510: PetscCall(PCGetOptionsPrefix(pc, &prefix));
511: PetscCall(KSPSetOptionsPrefix(ksp, prefix));
512: if (subdomain_names && subdomain_names[i]) {
513: PetscCall(PetscSNPrintf(subprefix, PETSC_MAX_PATH_LEN, "sub_%s_", subdomain_names[i]));
514: PetscCall(KSPAppendOptionsPrefix(ksp, subprefix));
515: PetscCall(PetscFree(subdomain_names[i]));
516: }
517: PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
518: osm->ksp[i] = ksp;
519: }
520: PetscCall(PetscFree(subdomain_dm));
521: PetscCall(PetscFree(subdomain_names));
522: scall = MAT_INITIAL_MATRIX;
523: } else { /* if (pc->setupcalled) */
524: /*
525: Destroy the submatrices from the previous iteration
526: */
527: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
528: PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
529: scall = MAT_INITIAL_MATRIX;
530: }
531: if (osm->permutationIS) {
532: PetscCall(MatCreateSubMatrix(pc->pmat, osm->permutationIS, osm->permutationIS, scall, &osm->permutationP));
533: PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
534: osm->pcmat = pc->pmat;
535: pc->pmat = osm->permutationP;
536: }
537: }
539: /*
540: Extract the submatrices.
541: */
542: if (size > 1) PetscCall(MatCreateSubMatricesMPI(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
543: else PetscCall(MatCreateSubMatrices(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
544: if (scall == MAT_INITIAL_MATRIX) {
545: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix));
546: for (PetscInt i = 0; i < osm->n; i++) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix));
547: }
549: /* Return control to the user so that the submatrices can be modified (e.g., to apply
550: different boundary conditions for the submatrices than for the global problem) */
551: PetscCall(PCModifySubMatrices(pc, osm->n, osm->ois, osm->ois, osm->pmat, pc->modifysubmatricesP));
553: /*
554: Loop over submatrices putting them into local ksps
555: */
556: for (PetscInt i = 0; i < osm->n; i++) {
557: PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i]));
558: PetscCall(KSPGetOptionsPrefix(osm->ksp[i], &prefix));
559: PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix));
560: if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i]));
561: }
562: if (!pc->setupcalled) {
563: /* MatCreateVecs() would allocate arrays before they are needed. Create arrayless work vectors where supported because
564: PCApply_GASM() and PCApplyTranspose_GASM() place the merged vectors' arrays into them. */
565: PetscCall(PetscMalloc1(osm->n, &osm->x));
566: PetscCall(PetscMalloc1(osm->n, &osm->y));
567: for (PetscInt i = 0; i < osm->n; ++i) {
568: VecType vtype;
569: PetscInt oNi, m, n, M, N;
570: PetscMPIInt subsize;
571: PetscBool boundtocpu, bindingpropagates, iskok;
573: PetscCall(MatGetVecType(osm->pmat[i], &vtype));
574: PetscCall(PetscStrcmpAny(vtype, &iskok, VECKOKKOS, VECSEQKOKKOS, VECMPIKOKKOS, ""));
575: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
576: PetscCall(ISGetSize(osm->ois[i], &oNi));
577: PetscCall(MatGetLocalSize(osm->pmat[i], &m, &n));
578: PetscCall(MatGetSize(osm->pmat[i], &M, &N));
579: PetscCheck(m == oni && n == oni && M == oNi && N == oNi, PetscObjectComm((PetscObject)osm->pmat[i]), PETSC_ERR_ARG_SIZ, "Modified submatrix %" PetscInt_FMT " has size %" PetscInt_FMT " x %" PetscInt_FMT " (local %" PetscInt_FMT " x %" PetscInt_FMT "), but its outer index set has size %" PetscInt_FMT " (local %" PetscInt_FMT ")", i, M, N, m, n, oNi, oni);
580: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)osm->pmat[i]), &subsize));
581: PetscCall(MatBoundToCPU(osm->pmat[i], &boundtocpu));
582: PetscCall(MatGetBindingPropagates(osm->pmat[i], &bindingpropagates));
583: for (PetscInt j = 0; j < 2; ++j) {
584: Vec *v = j ? &osm->y[i] : &osm->x[i];
586: if (iskok) {
587: /* Kokkos vectors require valid host storage even when VecPlaceArray() will replace it. */
588: PetscCall(VecCreate(PetscObjectComm((PetscObject)osm->pmat[i]), v));
589: PetscCall(VecSetSizes(*v, oni, oNi));
590: PetscCall(VecSetBlockSize(*v, 1));
591: } else if (subsize == 1) PetscCall(VecCreateSeqWithArray(PetscObjectComm((PetscObject)osm->pmat[i]), 1, oni, NULL, v));
592: else PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)osm->pmat[i]), 1, oni, oNi, NULL, v));
593: PetscCall(VecSetType(*v, vtype));
594: if (boundtocpu && bindingpropagates) {
595: PetscCall(VecSetBindingPropagates(*v, PETSC_TRUE));
596: PetscCall(VecBindToCPU(*v, PETSC_TRUE));
597: }
598: }
599: }
600: }
601: if (osm->pcmat) {
602: PetscCall(MatDestroy(&pc->pmat));
603: pc->pmat = osm->pcmat;
604: osm->pcmat = NULL;
605: }
606: PetscFunctionReturn(PETSC_SUCCESS);
607: }
609: static PetscErrorCode PCSetUpOnBlocks_GASM(PC pc)
610: {
611: PC_GASM *osm = (PC_GASM *)pc->data;
612: PetscInt i;
614: PetscFunctionBegin;
615: for (i = 0; i < osm->n; i++) PetscCall(KSPSetUp(osm->ksp[i]));
616: PetscFunctionReturn(PETSC_SUCCESS);
617: }
619: static PetscErrorCode PCApply_GASM(PC pc, Vec xin, Vec yout)
620: {
621: PC_GASM *osm = (PC_GASM *)pc->data;
622: const PetscScalar *gxarray;
623: PetscScalar *gyarray;
624: PetscInt i, oni, on;
625: Vec x, y;
626: ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
628: PetscFunctionBegin;
629: if (osm->pctoouter) {
630: PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
631: PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
632: x = osm->pcx;
633: y = osm->pcy;
634: } else {
635: x = xin;
636: y = yout;
637: }
638: /*
639: support for limiting the restriction or interpolation only to the inner
640: subdomain values (leaving the other values 0).
641: */
642: if (!(osm->type & PC_GASM_RESTRICT)) {
643: /* have to zero the work RHS since scatter may leave some slots empty */
644: PetscCall(VecZeroEntries(osm->gx));
645: PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
646: } else {
647: PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
648: }
649: PetscCall(VecZeroEntries(osm->gy));
650: if (!(osm->type & PC_GASM_RESTRICT)) {
651: PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
652: } else {
653: PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
654: }
655: /* do the subdomain solves */
656: PetscCall(VecGetArrayRead(osm->gx, &gxarray));
657: PetscCall(VecGetArray(osm->gy, &gyarray));
658: for (i = 0, on = 0; i < osm->n; ++i, on += oni) {
659: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
660: PetscCall(VecPlaceArray(osm->x[i], PetscSafePointerPlusOffset(gxarray, on)));
661: PetscCall(VecPlaceArray(osm->y[i], PetscSafePointerPlusOffset(gyarray, on)));
662: PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]));
663: PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
664: PetscCall(VecResetArray(osm->x[i]));
665: PetscCall(VecResetArray(osm->y[i]));
666: }
667: PetscCall(VecRestoreArrayRead(osm->gx, &gxarray));
668: PetscCall(VecRestoreArray(osm->gy, &gyarray));
669: /* do we need to zero y? */
670: PetscCall(VecZeroEntries(y));
671: if (!(osm->type & PC_GASM_INTERPOLATE)) {
672: PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
673: PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
674: } else {
675: PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
676: PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
677: }
678: if (osm->pctoouter) {
679: PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
680: PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
681: }
682: PetscFunctionReturn(PETSC_SUCCESS);
683: }
685: static PetscErrorCode PCMatApply_GASM(PC pc, Mat Xin, Mat Yout)
686: {
687: PC_GASM *osm = (PC_GASM *)pc->data;
688: Mat X, Y, O = NULL, Z, W;
689: Vec x, y;
690: PetscInt i, m, M, N;
691: ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
693: PetscFunctionBegin;
694: PetscCheck(osm->n == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
695: PetscCall(MatGetSize(Xin, NULL, &N));
696: if (osm->pctoouter) {
697: PetscCall(VecGetLocalSize(osm->pcx, &m));
698: PetscCall(VecGetSize(osm->pcx, &M));
699: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &O));
700: for (i = 0; i < N; ++i) {
701: PetscCall(MatDenseGetColumnVecRead(Xin, i, &x));
702: PetscCall(MatDenseGetColumnVecWrite(O, i, &y));
703: PetscCall(VecScatterBegin(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
704: PetscCall(VecScatterEnd(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
705: PetscCall(MatDenseRestoreColumnVecWrite(O, i, &y));
706: PetscCall(MatDenseRestoreColumnVecRead(Xin, i, &x));
707: }
708: X = Y = O;
709: } else {
710: X = Xin;
711: Y = Yout;
712: }
713: /*
714: support for limiting the restriction or interpolation only to the inner
715: subdomain values (leaving the other values 0).
716: */
717: PetscCall(VecGetLocalSize(osm->x[0], &m));
718: PetscCall(VecGetSize(osm->x[0], &M));
719: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &Z));
720: for (i = 0; i < N; ++i) {
721: PetscCall(MatDenseGetColumnVecRead(X, i, &x));
722: PetscCall(MatDenseGetColumnVecWrite(Z, i, &y));
723: if (!(osm->type & PC_GASM_RESTRICT)) {
724: /* have to zero the work RHS since scatter may leave some slots empty */
725: PetscCall(VecZeroEntries(y));
726: PetscCall(VecScatterBegin(osm->girestriction, x, y, INSERT_VALUES, forward));
727: PetscCall(VecScatterEnd(osm->girestriction, x, y, INSERT_VALUES, forward));
728: } else {
729: PetscCall(VecScatterBegin(osm->gorestriction, x, y, INSERT_VALUES, forward));
730: PetscCall(VecScatterEnd(osm->gorestriction, x, y, INSERT_VALUES, forward));
731: }
732: PetscCall(MatDenseRestoreColumnVecWrite(Z, i, &y));
733: PetscCall(MatDenseRestoreColumnVecRead(X, i, &x));
734: }
735: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &W));
736: PetscCall(MatSetOption(Z, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
737: PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY));
738: PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY));
739: /* do the subdomain solve */
740: PetscCall(KSPMatSolve(osm->ksp[0], Z, W));
741: PetscCall(KSPCheckSolve(osm->ksp[0], pc, NULL));
742: PetscCall(MatDestroy(&Z));
743: /* do we need to zero y? */
744: PetscCall(MatZeroEntries(Y));
745: for (i = 0; i < N; ++i) {
746: PetscCall(MatDenseGetColumnVecWrite(Y, i, &y));
747: PetscCall(MatDenseGetColumnVecRead(W, i, &x));
748: if (!(osm->type & PC_GASM_INTERPOLATE)) {
749: PetscCall(VecScatterBegin(osm->girestriction, x, y, ADD_VALUES, reverse));
750: PetscCall(VecScatterEnd(osm->girestriction, x, y, ADD_VALUES, reverse));
751: } else {
752: PetscCall(VecScatterBegin(osm->gorestriction, x, y, ADD_VALUES, reverse));
753: PetscCall(VecScatterEnd(osm->gorestriction, x, y, ADD_VALUES, reverse));
754: }
755: PetscCall(MatDenseRestoreColumnVecRead(W, i, &x));
756: if (osm->pctoouter) {
757: PetscCall(MatDenseGetColumnVecWrite(Yout, i, &x));
758: PetscCall(VecScatterBegin(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
759: PetscCall(VecScatterEnd(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
760: PetscCall(MatDenseRestoreColumnVecRead(Yout, i, &x));
761: }
762: PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y));
763: }
764: PetscCall(MatDestroy(&W));
765: PetscCall(MatDestroy(&O));
766: PetscFunctionReturn(PETSC_SUCCESS);
767: }
769: static PetscErrorCode PCApplyTranspose_GASM(PC pc, Vec xin, Vec yout)
770: {
771: PC_GASM *osm = (PC_GASM *)pc->data;
772: const PetscScalar *gxarray;
773: PetscScalar *gyarray;
774: PetscInt i, oni, on;
775: Vec x, y;
776: ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
778: PetscFunctionBegin;
779: if (osm->pctoouter) {
780: PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
781: PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
782: x = osm->pcx;
783: y = osm->pcy;
784: } else {
785: x = xin;
786: y = yout;
787: }
788: /*
789: Support for limiting the restriction or interpolation to only local
790: subdomain values (leaving the other values 0).
792: Note: these are reversed from the PCApply_GASM() because we are applying the
793: transpose of the three terms
794: */
795: if (!(osm->type & PC_GASM_INTERPOLATE)) {
796: /* have to zero the work RHS since scatter may leave some slots empty */
797: PetscCall(VecZeroEntries(osm->gx));
798: PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
799: } else {
800: PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
801: }
802: PetscCall(VecZeroEntries(osm->gy));
803: if (!(osm->type & PC_GASM_INTERPOLATE)) {
804: PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
805: } else {
806: PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
807: }
808: /* do the local solves */
809: PetscCall(VecGetArrayRead(osm->gx, &gxarray));
810: PetscCall(VecGetArray(osm->gy, &gyarray));
811: for (i = 0, on = 0; i < osm->n; ++i, on += oni) { /* Note that the solves are local, so we can go to osm->n, rather than osm->nmax. */
812: PetscCall(ISGetLocalSize(osm->ois[i], &oni));
813: PetscCall(VecPlaceArray(osm->x[i], PetscSafePointerPlusOffset(gxarray, on)));
814: PetscCall(VecPlaceArray(osm->y[i], PetscSafePointerPlusOffset(gyarray, on)));
815: PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i]));
816: PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
817: PetscCall(VecResetArray(osm->x[i]));
818: PetscCall(VecResetArray(osm->y[i]));
819: }
820: PetscCall(VecRestoreArrayRead(osm->gx, &gxarray));
821: PetscCall(VecRestoreArray(osm->gy, &gyarray));
822: PetscCall(VecZeroEntries(y));
823: if (!(osm->type & PC_GASM_RESTRICT)) {
824: PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
825: PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
826: } else {
827: PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
828: PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
829: }
830: if (osm->pctoouter) {
831: PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
832: PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
833: }
834: PetscFunctionReturn(PETSC_SUCCESS);
835: }
837: static PetscErrorCode PCReset_GASM(PC pc)
838: {
839: PC_GASM *osm = (PC_GASM *)pc->data;
841: PetscFunctionBegin;
842: if (osm->ksp) {
843: for (PetscInt i = 0; i < osm->n; i++) PetscCall(KSPReset(osm->ksp[i]));
844: }
845: if (osm->pmat) {
846: if (osm->n > 0) {
847: PetscMPIInt size;
849: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
850: if (size > 1) {
851: /* osm->pmat is created by MatCreateSubMatricesMPI(), cannot use MatDestroySubMatrices() */
852: PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
853: } else {
854: PetscCall(MatDestroySubMatrices(osm->n, &osm->pmat));
855: }
856: }
857: }
858: if (osm->x) {
859: for (PetscInt i = 0; i < osm->n; i++) {
860: PetscCall(VecDestroy(&osm->x[i]));
861: PetscCall(VecDestroy(&osm->y[i]));
862: }
863: }
864: PetscCall(PetscFree(osm->x));
865: PetscCall(PetscFree(osm->y));
866: PetscCall(VecDestroy(&osm->gx));
867: PetscCall(VecDestroy(&osm->gy));
869: PetscCall(VecScatterDestroy(&osm->gorestriction));
870: PetscCall(VecScatterDestroy(&osm->girestriction));
871: if (!osm->user_subdomains) {
872: PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
873: osm->N = PETSC_DETERMINE;
874: osm->nmax = PETSC_DETERMINE;
875: }
876: PetscCall(VecScatterDestroy(&osm->pctoouter));
877: PetscCall(ISDestroy(&osm->permutationIS));
878: PetscCall(VecDestroy(&osm->pcx));
879: PetscCall(VecDestroy(&osm->pcy));
880: PetscCall(MatDestroy(&osm->permutationP));
881: PetscCall(MatDestroy(&osm->pcmat));
882: PetscFunctionReturn(PETSC_SUCCESS);
883: }
885: static PetscErrorCode PCDestroy_GASM(PC pc)
886: {
887: PC_GASM *osm = (PC_GASM *)pc->data;
889: PetscFunctionBegin;
890: PetscCall(PCReset_GASM(pc));
891: /* PCReset will not destroy subdomains, if user_subdomains is true. */
892: PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
893: if (osm->ksp) {
894: for (PetscInt i = 0; i < osm->n; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
895: PetscCall(PetscFree(osm->ksp));
896: }
897: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", NULL));
898: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", NULL));
899: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", NULL));
900: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", NULL));
901: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", NULL));
902: PetscCall(PetscFree(pc->data));
903: PetscFunctionReturn(PETSC_SUCCESS);
904: }
906: static PetscErrorCode PCSetFromOptions_GASM(PC pc, PetscOptionItems PetscOptionsObject)
907: {
908: PC_GASM *osm = (PC_GASM *)pc->data;
909: PetscInt blocks, ovl;
910: PetscBool flg;
911: PCGASMType gasmtype;
913: PetscFunctionBegin;
914: PetscOptionsHeadBegin(PetscOptionsObject, "Generalized additive Schwarz options");
915: PetscCall(PetscOptionsBool("-pc_gasm_use_dm_subdomains", "If subdomains aren't set, use DMCreateDomainDecomposition() to define subdomains.", "PCGASMSetUseDMSubdomains", osm->dm_subdomains, &osm->dm_subdomains, &flg));
916: PetscCall(PetscOptionsInt("-pc_gasm_total_subdomains", "Total number of subdomains across communicator", "PCGASMSetTotalSubdomains", osm->N, &blocks, &flg));
917: if (flg) PetscCall(PCGASMSetTotalSubdomains(pc, blocks));
918: PetscCall(PetscOptionsInt("-pc_gasm_overlap", "Number of overlapping degrees of freedom", "PCGASMSetOverlap", osm->overlap, &ovl, &flg));
919: if (flg) {
920: PetscCall(PCGASMSetOverlap(pc, ovl));
921: osm->dm_subdomains = PETSC_FALSE;
922: }
923: flg = PETSC_FALSE;
924: PetscCall(PetscOptionsEnum("-pc_gasm_type", "Type of restriction/extension", "PCGASMSetType", PCGASMTypes, (PetscEnum)osm->type, (PetscEnum *)&gasmtype, &flg));
925: if (flg) PetscCall(PCGASMSetType(pc, gasmtype));
926: PetscCall(PetscOptionsBool("-pc_gasm_use_hierachical_partitioning", "use hierarchical partitioning", NULL, osm->hierarchicalpartitioning, &osm->hierarchicalpartitioning, &flg));
927: PetscOptionsHeadEnd();
928: PetscFunctionReturn(PETSC_SUCCESS);
929: }
931: /*@
932: PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the communicator for `PCGASM`
934: Logically Collective
936: Input Parameters:
937: + pc - the preconditioner
938: - N - total number of subdomains
940: Level: beginner
942: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
943: `PCGASMCreateSubdomains2D()`
944: @*/
945: PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N)
946: {
947: PC_GASM *osm = (PC_GASM *)pc->data;
948: PetscMPIInt size, rank;
950: PetscFunctionBegin;
951: PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Total number of subdomains must be 1 or more, got N = %" PetscInt_FMT, N);
952: PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp().");
954: PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
955: osm->ois = osm->iis = NULL;
957: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
958: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
959: osm->N = N;
960: osm->n = PETSC_DETERMINE;
961: osm->nmax = PETSC_DETERMINE;
962: osm->dm_subdomains = PETSC_FALSE;
963: PetscFunctionReturn(PETSC_SUCCESS);
964: }
966: static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[])
967: {
968: PC_GASM *osm = (PC_GASM *)pc->data;
970: PetscFunctionBegin;
971: PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Each MPI rank must have 1 or more subdomains, got n = %" PetscInt_FMT, n);
972: PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp().");
974: PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
975: osm->iis = osm->ois = NULL;
976: osm->n = n;
977: osm->N = PETSC_DETERMINE;
978: osm->nmax = PETSC_DETERMINE;
979: if (ois) {
980: PetscCall(PetscMalloc1(n, &osm->ois));
981: for (PetscInt i = 0; i < n; i++) {
982: PetscCall(PetscObjectReference((PetscObject)ois[i]));
983: osm->ois[i] = ois[i];
984: }
985: /*
986: Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(),
987: it will be ignored. To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1.
988: */
989: osm->overlap = -1;
990: /* inner subdomains must be provided */
991: PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided ");
992: } /* end if */
993: if (iis) {
994: PetscCall(PetscMalloc1(n, &osm->iis));
995: for (PetscInt i = 0; i < n; i++) {
996: PetscCall(PetscObjectReference((PetscObject)iis[i]));
997: osm->iis[i] = iis[i];
998: }
999: if (!ois) {
1000: osm->ois = NULL;
1001: /* if user does not provide outer indices, we will create the corresponding outer indices using osm->overlap =1 in PCSetUp_GASM */
1002: }
1003: }
1004: if (PetscDefined(USE_DEBUG)) {
1005: PetscInt j, rstart, rend, *covered, lsize;
1006: const PetscInt *indices;
1008: if (osm->iis) {
1009: /* check if the inner indices cover and only cover the local portion of the matrix */
1010: PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
1011: PetscCall(PetscCalloc1(rend - rstart, &covered));
1012: /* check if the current MPI process owns indices from others */
1013: for (PetscInt i = 0; i < n; i++) {
1014: PetscCall(ISGetIndices(osm->iis[i], &indices));
1015: PetscCall(ISGetLocalSize(osm->iis[i], &lsize));
1016: for (j = 0; j < lsize; j++) {
1017: PetscCheck(indices[j] >= rstart && indices[j] < rend, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "inner subdomains can not own an index %" PetscInt_FMT " from other ranks", indices[j]);
1018: PetscCheck(covered[indices[j] - rstart] != 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "inner subdomains can not have an overlapping index %" PetscInt_FMT " ", indices[j]);
1019: covered[indices[j] - rstart] = 1;
1020: }
1021: PetscCall(ISRestoreIndices(osm->iis[i], &indices));
1022: }
1023: /* check if we miss any indices */
1024: for (PetscInt i = rstart; i < rend; i++) PetscCheck(covered[i - rstart], PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "local entity %" PetscInt_FMT " was not covered by inner subdomains", i);
1025: PetscCall(PetscFree(covered));
1026: }
1027: }
1028: if (iis) osm->user_subdomains = PETSC_TRUE;
1029: PetscFunctionReturn(PETSC_SUCCESS);
1030: }
1032: static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl)
1033: {
1034: PC_GASM *osm = (PC_GASM *)pc->data;
1036: PetscFunctionBegin;
1037: PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested");
1038: PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp().");
1039: if (!pc->setupcalled) osm->overlap = ovl;
1040: PetscFunctionReturn(PETSC_SUCCESS);
1041: }
1043: static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type)
1044: {
1045: PC_GASM *osm = (PC_GASM *)pc->data;
1047: PetscFunctionBegin;
1048: osm->type = type;
1049: osm->type_set = PETSC_TRUE;
1050: PetscFunctionReturn(PETSC_SUCCESS);
1051: }
1053: static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort)
1054: {
1055: PC_GASM *osm = (PC_GASM *)pc->data;
1057: PetscFunctionBegin;
1058: osm->sort_indices = doSort;
1059: PetscFunctionReturn(PETSC_SUCCESS);
1060: }
1062: /*
1063: FIXME: This routine might need to be modified now that multiple processes per subdomain are allowed.
1064: In particular, it would upset the global subdomain number calculation.
1065: */
1066: static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp)
1067: {
1068: PC_GASM *osm = (PC_GASM *)pc->data;
1070: PetscFunctionBegin;
1071: PetscCheck(osm->n >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Need to call PCSetUp() on PC (or KSPSetUp() on the outer KSP object) before calling here");
1073: if (n) *n = osm->n;
1074: if (first) {
1075: PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
1076: *first -= osm->n;
1077: }
1078: if (ksp) {
1079: /* Assume that local solves are now different; not necessarily
1080: true, though! This flag is used only for PCView_GASM() */
1081: *ksp = osm->ksp;
1082: osm->same_subdomain_solvers = PETSC_FALSE;
1083: }
1084: PetscFunctionReturn(PETSC_SUCCESS);
1085: } /* PCGASMGetSubKSP_GASM() */
1087: /*@
1088: PCGASMSetSubdomains - Sets the subdomains for this MPI process
1089: for the additive Schwarz preconditioner with multiple MPI processes per subdomain, `PCGASM`
1091: Collective
1093: Input Parameters:
1094: + pc - the preconditioner object
1095: . n - the number of subdomains for this MPI process
1096: . iis - the index sets that define the inner subdomains (or `NULL` for PETSc to determine subdomains), the `iis` array is
1097: copied so may be freed after this call.
1098: - ois - the index sets that define the outer subdomains (or `NULL` to use the same as `iis`, or to construct by expanding `iis` by
1099: the requested overlap), the `ois` array is copied so may be freed after this call.
1101: Level: advanced
1103: Notes:
1104: The `IS` indices use the parallel, global numbering of the vector entries.
1106: Inner subdomains are those where the correction is applied.
1108: Outer subdomains are those where the residual necessary to obtain the
1109: corrections is obtained (see `PCGASMType` for the use of inner/outer subdomains).
1111: Both inner and outer subdomains can extend over several MPI processes.
1112: This process' portion of a subdomain is known as a local subdomain.
1114: Inner subdomains can not overlap with each other, do not have any entities from remote processes,
1115: and have to cover the entire local subdomain owned by the current process. The index sets on each
1116: process should be ordered such that the ith local subdomain is connected to the ith remote subdomain
1117: on another MPI process.
1119: By default the `PGASM` preconditioner uses 1 (local) subdomain per MPI process.
1121: The `iis` and `ois` arrays may be freed after this call using `PCGASMDestroySubdomains()`
1123: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMDestroySubdomains()`,
1124: `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1125: @*/
1126: PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[])
1127: {
1128: PC_GASM *osm = (PC_GASM *)pc->data;
1130: PetscFunctionBegin;
1132: PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois));
1133: osm->dm_subdomains = PETSC_FALSE;
1134: PetscFunctionReturn(PETSC_SUCCESS);
1135: }
1137: /*@
1138: PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the
1139: additive Schwarz preconditioner `PCGASM`. Either all or no MPI processes in the
1140: pc communicator must call this routine.
1142: Logically Collective
1144: Input Parameters:
1145: + pc - the preconditioner context
1146: - ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0)
1148: Options Database Key:
1149: . -pc_gasm_overlap overlap - Sets overlap
1151: Level: intermediate
1153: Notes:
1154: By default the `PCGASM` preconditioner uses 1 subdomain per process. To use
1155: multiple subdomain per perocessor or "straddling" subdomains that intersect
1156: multiple processes use `PCGASMSetSubdomains()` (or option `-pc_gasm_total_subdomains` <n>).
1158: The overlap defaults to 0, so if one desires that no additional
1159: overlap be computed beyond what may have been set with a call to
1160: `PCGASMSetSubdomains()`, then `ovl` must be set to be 0. In particular, if one does
1161: not explicitly set the subdomains in application code, then all overlap would be computed
1162: internally by PETSc, and using an overlap of 0 would result in an `PCGASM`
1163: variant that is equivalent to the block Jacobi preconditioner.
1165: One can define initial index sets with any overlap via
1166: `PCGASMSetSubdomains()`; the routine `PCGASMSetOverlap()` merely allows
1167: PETSc to extend that overlap further, if desired.
1169: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1170: `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1171: @*/
1172: PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl)
1173: {
1174: PC_GASM *osm = (PC_GASM *)pc->data;
1176: PetscFunctionBegin;
1179: PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl));
1180: osm->dm_subdomains = PETSC_FALSE;
1181: PetscFunctionReturn(PETSC_SUCCESS);
1182: }
1184: /*@
1185: PCGASMSetType - Sets the type of restriction and interpolation used
1186: for local problems in the `PCGASM` additive Schwarz method.
1188: Logically Collective
1190: Input Parameters:
1191: + pc - the preconditioner context
1192: - type - variant of `PCGASM`, one of
1193: .vb
1194: `PC_GASM_BASIC` - full interpolation and restriction
1195: `PC_GASM_RESTRICT` - full restriction, local MPI process interpolation
1196: `PC_GASM_INTERPOLATE` - full interpolation, local MPI process restriction
1197: `PC_GASM_NONE` - local MPI process restriction and interpolation
1198: .ve
1200: Options Database Key:
1201: . -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASM` type
1203: Level: intermediate
1205: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1206: `PCGASMCreateSubdomains2D()`, `PCASM`, `PCASMSetType()`
1207: @*/
1208: PetscErrorCode PCGASMSetType(PC pc, PCGASMType type)
1209: {
1210: PetscFunctionBegin;
1213: PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type));
1214: PetscFunctionReturn(PETSC_SUCCESS);
1215: }
1217: /*@
1218: PCGASMSetSortIndices - Determines whether subdomain indices are sorted.
1220: Logically Collective
1222: Input Parameters:
1223: + pc - the preconditioner context
1224: - doSort - sort the subdomain indices
1226: Level: intermediate
1228: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1229: `PCGASMCreateSubdomains2D()`
1230: @*/
1231: PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort)
1232: {
1233: PetscFunctionBegin;
1236: PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort));
1237: PetscFunctionReturn(PETSC_SUCCESS);
1238: }
1240: /*@
1241: PCGASMGetSubKSP - Gets the local `KSP` contexts for all subdomains on this MPI process.
1243: Collective iff first_local is requested
1245: Input Parameter:
1246: . pc - the preconditioner context
1248: Output Parameters:
1249: + n_local - the number of blocks on this MPI process or `NULL`
1250: . first_local - the global number of the first block on this process or `NULL`, all processes must request or all must pass `NULL`
1251: - ksp - the array of `KSP` contexts
1253: Level: advanced
1255: Note:
1256: After `PCGASMGetSubKSP()` the array of `KSP`es is not to be freed
1258: Currently for some matrix implementations only 1 block per MPI process
1259: is supported.
1261: You must call `KSPSetUp()` before calling `PCGASMGetSubKSP()`.
1263: Fortran Note:
1264: Call `PCGASMRestoreSubKSP()` when the array of `KSP` is no longer needed
1266: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
1267: `PCGASMCreateSubdomains2D()`
1268: @*/
1269: PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[])
1270: {
1271: PetscFunctionBegin;
1273: PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
1274: PetscFunctionReturn(PETSC_SUCCESS);
1275: }
1277: /*MC
1278: PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
1279: its own `KSP` object on a subset of MPI processes
1281: Options Database Keys:
1282: + -pc_gasm_total_subdomains n - Sets total number of local subdomains to be distributed among the MPI processes
1283: . -pc_gasm_view_subdomains - activates the printing of subdomain indices in `PCView()`, -ksp_view or -snes_view
1284: . -pc_gasm_print_subdomains - activates the printing of subdomain indices in `PCSetUp()`
1285: . -pc_gasm_overlap ovl - Sets overlap by which to (automatically) extend local subdomains
1286: - -pc_gasm_type (basic|restrict|interpolate|none) - Sets `PCGASMType`
1288: Level: beginner
1290: Notes:
1291: To set options on the solvers for each block append `-sub_` to all the `KSP`, and `PC`
1292: options database keys. For example, `-sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly`
1294: To set the options on the solvers separate for each block call `PCGASMGetSubKSP()`
1295: and set the options directly on the resulting `KSP` object (you can access its `PC`
1296: with `KSPGetPC()`)
1298: `PCGASM` uses host memory for the merged subdomain vectors so that host-only subdomain solvers work with device global vectors.
1299: Device subdomain solvers therefore copy each right-hand side to the device and each solution back to the host during every application.
1301: See {cite}`dryja1987additive` and {cite}`1sbg` for details on additive Schwarz algorithms
1303: .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASM`, `PCGASMType`, `PCGASMSetType()`,
1304: `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`,
1305: `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCASMSetType()`
1306: M*/
1308: PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc)
1309: {
1310: PC_GASM *osm;
1312: PetscFunctionBegin;
1313: PetscCall(PetscNew(&osm));
1315: osm->N = PETSC_DETERMINE;
1316: osm->n = PETSC_DECIDE;
1317: osm->nmax = PETSC_DETERMINE;
1318: osm->overlap = 0;
1319: osm->ksp = NULL;
1320: osm->gorestriction = NULL;
1321: osm->girestriction = NULL;
1322: osm->pctoouter = NULL;
1323: osm->gx = NULL;
1324: osm->gy = NULL;
1325: osm->x = NULL;
1326: osm->y = NULL;
1327: osm->pcx = NULL;
1328: osm->pcy = NULL;
1329: osm->permutationIS = NULL;
1330: osm->permutationP = NULL;
1331: osm->pcmat = NULL;
1332: osm->ois = NULL;
1333: osm->iis = NULL;
1334: osm->pmat = NULL;
1335: osm->type = PC_GASM_RESTRICT;
1336: osm->same_subdomain_solvers = PETSC_TRUE;
1337: osm->sort_indices = PETSC_TRUE;
1338: osm->dm_subdomains = PETSC_FALSE;
1339: osm->hierarchicalpartitioning = PETSC_FALSE;
1341: pc->data = (void *)osm;
1342: pc->ops->apply = PCApply_GASM;
1343: pc->ops->matapply = PCMatApply_GASM;
1344: pc->ops->applytranspose = PCApplyTranspose_GASM;
1345: pc->ops->setup = PCSetUp_GASM;
1346: pc->ops->reset = PCReset_GASM;
1347: pc->ops->destroy = PCDestroy_GASM;
1348: pc->ops->setfromoptions = PCSetFromOptions_GASM;
1349: pc->ops->setuponblocks = PCSetUpOnBlocks_GASM;
1350: pc->ops->view = PCView_GASM;
1351: pc->ops->applyrichardson = NULL;
1353: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM));
1354: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM));
1355: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM));
1356: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM));
1357: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM));
1358: PetscFunctionReturn(PETSC_SUCCESS);
1359: }
1361: PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[])
1362: {
1363: MatPartitioning mpart;
1364: const char *prefix;
1365: PetscInt i, j, rstart, rend, bs;
1366: PetscBool hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE;
1367: Mat Ad = NULL, adj;
1368: IS ispart, isnumb, *is;
1370: PetscFunctionBegin;
1371: PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc);
1373: /* Get prefix, row distribution, and block size */
1374: PetscCall(MatGetOptionsPrefix(A, &prefix));
1375: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
1376: PetscCall(MatGetBlockSize(A, &bs));
1377: PetscCheck(rstart / bs * bs == rstart && rend / bs * bs == rend, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "bad row distribution [%" PetscInt_FMT ",%" PetscInt_FMT ") for matrix block size %" PetscInt_FMT, rstart, rend, bs);
1379: /* Get diagonal block from matrix if possible */
1380: PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop));
1381: if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad));
1382: if (Ad) {
1383: PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij));
1384: if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij));
1385: }
1386: if (Ad && nloc > 1) {
1387: PetscBool match, done;
1388: /* Try to setup a good matrix partitioning if available */
1389: PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart));
1390: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
1391: PetscCall(MatPartitioningSetFromOptions(mpart));
1392: PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match));
1393: if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match));
1394: if (!match) { /* assume a "good" partitioner is available */
1395: PetscInt na;
1396: const PetscInt *ia, *ja;
1397: PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1398: if (done) {
1399: /* Build adjacency matrix by hand. Unfortunately a call to
1400: MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1401: remove the block-aij structure and we cannot expect
1402: MatPartitioning to split vertices as we need */
1403: PetscInt i, j, len, nnz, cnt, *iia = NULL, *jja = NULL;
1404: const PetscInt *row;
1405: nnz = 0;
1406: for (i = 0; i < na; i++) { /* count number of nonzeros */
1407: len = ia[i + 1] - ia[i];
1408: row = ja + ia[i];
1409: for (j = 0; j < len; j++) {
1410: if (row[j] == i) { /* don't count diagonal */
1411: len--;
1412: break;
1413: }
1414: }
1415: nnz += len;
1416: }
1417: PetscCall(PetscMalloc1(na + 1, &iia));
1418: PetscCall(PetscMalloc1(nnz, &jja));
1419: nnz = 0;
1420: iia[0] = 0;
1421: for (i = 0; i < na; i++) { /* fill adjacency */
1422: cnt = 0;
1423: len = ia[i + 1] - ia[i];
1424: row = ja + ia[i];
1425: for (j = 0; j < len; j++) {
1426: if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */
1427: }
1428: nnz += cnt;
1429: iia[i + 1] = nnz;
1430: }
1431: /* Partitioning of the adjacency matrix */
1432: PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj));
1433: PetscCall(MatPartitioningSetAdjacency(mpart, adj));
1434: PetscCall(MatPartitioningSetNParts(mpart, nloc));
1435: PetscCall(MatPartitioningApply(mpart, &ispart));
1436: PetscCall(ISPartitioningToNumbering(ispart, &isnumb));
1437: PetscCall(MatDestroy(&adj));
1438: foundpart = PETSC_TRUE;
1439: }
1440: PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1441: }
1442: PetscCall(MatPartitioningDestroy(&mpart));
1443: }
1444: PetscCall(PetscMalloc1(nloc, &is));
1445: if (!foundpart) {
1446: /* Partitioning by contiguous chunks of rows */
1448: PetscInt mbs = (rend - rstart) / bs;
1449: PetscInt start = rstart;
1450: for (i = 0; i < nloc; i++) {
1451: PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs;
1452: PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i]));
1453: start += count;
1454: }
1456: } else {
1457: /* Partitioning by adjacency of diagonal block */
1459: const PetscInt *numbering;
1460: PetscInt *count, nidx, *indices, *newidx, start = 0;
1461: /* Get node count in each partition */
1462: PetscCall(PetscMalloc1(nloc, &count));
1463: PetscCall(ISPartitioningCount(ispart, nloc, count));
1464: if (isbaij && bs > 1) { /* adjust for the block-aij case */
1465: for (i = 0; i < nloc; i++) count[i] *= bs;
1466: }
1467: /* Build indices from node numbering */
1468: PetscCall(ISGetLocalSize(isnumb, &nidx));
1469: PetscCall(PetscMalloc1(nidx, &indices));
1470: for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */
1471: PetscCall(ISGetIndices(isnumb, &numbering));
1472: PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices));
1473: PetscCall(ISRestoreIndices(isnumb, &numbering));
1474: if (isbaij && bs > 1) { /* adjust for the block-aij case */
1475: PetscCall(PetscMalloc1(nidx * bs, &newidx));
1476: for (i = 0; i < nidx; i++) {
1477: for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j;
1478: }
1479: PetscCall(PetscFree(indices));
1480: nidx *= bs;
1481: indices = newidx;
1482: }
1483: /* Shift to get global indices */
1484: for (i = 0; i < nidx; i++) indices[i] += rstart;
1486: /* Build the index sets for each block */
1487: for (i = 0; i < nloc; i++) {
1488: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i]));
1489: PetscCall(ISSort(is[i]));
1490: start += count[i];
1491: }
1493: PetscCall(PetscFree(count));
1494: PetscCall(PetscFree(indices));
1495: PetscCall(ISDestroy(&isnumb));
1496: PetscCall(ISDestroy(&ispart));
1497: }
1498: *iis = is;
1499: PetscFunctionReturn(PETSC_SUCCESS);
1500: }
1502: PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1503: {
1504: PetscFunctionBegin;
1505: PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis));
1506: PetscFunctionReturn(PETSC_SUCCESS);
1507: }
1509: /*@
1510: PCGASMCreateSubdomains - Creates `n` index sets defining `n` nonoverlapping subdomains on this MPI process for the `PCGASM` additive
1511: Schwarz preconditioner for a any problem based on its matrix.
1513: Collective
1515: Input Parameters:
1516: + A - The global matrix operator
1517: - N - the number of global subdomains requested
1519: Output Parameters:
1520: + n - the number of subdomains created on this MPI process
1521: - iis - the array of index sets defining the local inner subdomains (on which the correction is applied)
1523: Level: advanced
1525: Notes:
1526: When `N` >= A's communicator size, each subdomain is local -- contained within a single MPI process.
1527: When `N` < size, the subdomains are 'straddling' (process boundaries) and are no longer local.
1528: The resulting subdomains can be use in `PCGASMSetSubdomains`(pc,n,iss,`NULL`). The overlapping
1529: outer subdomains will be automatically generated from these according to the requested amount of
1530: overlap; this is currently supported only with local subdomains.
1532: Use `PCGASMDestroySubdomains()` to free the array and the list of index sets.
1534: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()`
1535: @*/
1536: PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1537: {
1538: PetscMPIInt size;
1540: PetscFunctionBegin;
1542: PetscAssertPointer(iis, 4);
1544: PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N);
1545: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
1546: if (N >= size) {
1547: *n = N / size + (N % size);
1548: PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis));
1549: } else {
1550: PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis));
1551: }
1552: PetscFunctionReturn(PETSC_SUCCESS);
1553: }
1555: /*@
1556: PCGASMDestroySubdomains - Destroys the index sets created with
1557: `PCGASMCreateSubdomains()` or `PCGASMCreateSubdomains2D()`. Should be
1558: called after setting subdomains with `PCGASMSetSubdomains()`.
1560: Collective
1562: Input Parameters:
1563: + n - the number of index sets
1564: . iis - the array of inner subdomains
1565: - ois - the array of outer subdomains, can be `NULL`
1567: Level: intermediate
1569: Note:
1570: This is a convenience subroutine that walks each list,
1571: destroys each `IS` on the list, and then frees the list. At the end the
1572: list pointers are set to `NULL`.
1574: .seealso: [](ch_ksp), `PCGASM`, `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()`
1575: @*/
1576: PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS *iis[], IS *ois[])
1577: {
1578: PetscFunctionBegin;
1579: if (n <= 0) PetscFunctionReturn(PETSC_SUCCESS);
1580: if (ois) {
1581: PetscAssertPointer(ois, 3);
1582: if (*ois) {
1583: PetscAssertPointer(*ois, 3);
1584: for (PetscInt i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i]));
1585: PetscCall(PetscFree(*ois));
1586: }
1587: }
1588: if (iis) {
1589: PetscAssertPointer(iis, 2);
1590: if (*iis) {
1591: PetscAssertPointer(*iis, 2);
1592: for (PetscInt i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i]));
1593: PetscCall(PetscFree(*iis));
1594: }
1595: }
1596: PetscFunctionReturn(PETSC_SUCCESS);
1597: }
1599: #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \
1600: do { \
1601: PetscInt first_row = first / M, last_row = last / M + 1; \
1602: /* \
1603: Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners \
1604: of the bounding box of the intersection of the subdomain with the local ownership range (local \
1605: subdomain). \
1606: Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows \
1607: of the intersection. \
1608: */ \
1609: /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \
1610: *ylow_loc = PetscMax(first_row, ylow); \
1611: /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \
1612: *xleft_loc = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \
1613: /* yhigh_loc is the grid row above the last local subdomain element */ \
1614: *yhigh_loc = PetscMin(last_row, yhigh); \
1615: /* xright is the offset of the end of the local subdomain within its grid row (might actually be outside the local subdomain) */ \
1616: *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \
1617: /* Now compute the size of the local subdomain n. */ \
1618: *n = 0; \
1619: if (*ylow_loc < *yhigh_loc) { \
1620: PetscInt width = xright - xleft; \
1621: *n += width * (*yhigh_loc - *ylow_loc - 1); \
1622: *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \
1623: *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \
1624: } \
1625: } while (0)
1627: /*@
1628: PCGASMCreateSubdomains2D - Creates the index sets for the `PCGASM` overlapping Schwarz
1629: preconditioner for a two-dimensional problem on a regular grid.
1631: Collective
1633: Input Parameters:
1634: + pc - the preconditioner context
1635: . M - the global number of grid points in the x direction
1636: . N - the global number of grid points in the y direction
1637: . Mdomains - the global number of subdomains in the x direction
1638: . Ndomains - the global number of subdomains in the y direction
1639: . dof - degrees of freedom per node
1640: - overlap - overlap in mesh lines
1642: Output Parameters:
1643: + nsub - the number of local subdomains created
1644: . iis - array of index sets defining inner (nonoverlapping) subdomains
1645: - ois - array of index sets defining outer (overlapping, if overlap > 0) subdomains
1647: Level: advanced
1649: Note:
1650: Use `PCGASMDestroySubdomains()` to free the index sets and the arrays
1652: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()`, `PCASMCreateSubdomains2D()`,
1653: `PCGASMDestroySubdomains()`
1654: @*/
1655: PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS *iis[], IS *ois[])
1656: {
1657: PetscMPIInt size, rank;
1658: PetscInt maxheight, maxwidth;
1659: PetscInt xstart, xleft, xright, xleft_loc, xright_loc;
1660: PetscInt ystart, ylow, yhigh, ylow_loc, yhigh_loc;
1661: PetscInt x[2][2], y[2][2], n[2];
1662: PetscInt first, last;
1663: PetscInt nidx, *idx;
1664: PetscInt ii, jj, s, q, d;
1665: PetscInt k, kk;
1666: PetscMPIInt color;
1667: MPI_Comm comm, subcomm;
1668: IS **xis = NULL, **is = ois, **is_local = iis;
1670: PetscFunctionBegin;
1671: PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
1672: PetscCallMPI(MPI_Comm_size(comm, &size));
1673: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1674: PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last));
1675: PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
1676: "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") "
1677: "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT,
1678: first, last, dof);
1680: /* Determine the number of domains with nonzero intersections with the local ownership range. */
1681: s = 0;
1682: ystart = 0;
1683: for (PetscInt j = 0; j < Ndomains; ++j) {
1684: maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
1685: PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical direction for mesh height %" PetscInt_FMT, Ndomains, N);
1686: /* Vertical domain limits with an overlap. */
1687: ylow = PetscMax(ystart - overlap, 0);
1688: yhigh = PetscMin(ystart + maxheight + overlap, N);
1689: xstart = 0;
1690: for (PetscInt i = 0; i < Mdomains; ++i) {
1691: maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
1692: PetscCheck(maxwidth >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the horizontal direction for mesh width %" PetscInt_FMT, Mdomains, M);
1693: /* Horizontal domain limits with an overlap. */
1694: xleft = PetscMax(xstart - overlap, 0);
1695: xright = PetscMin(xstart + maxwidth + overlap, M);
1696: /*
1697: Determine whether this subdomain intersects this rank's ownership range of pc->pmat.
1698: */
1699: PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
1700: if (nidx) ++s;
1701: xstart += maxwidth;
1702: } /* for (PetscInt i = 0; i < Mdomains; ++i) */
1703: ystart += maxheight;
1704: } /* for (PetscInt j = 0; j < Ndomains; ++j) */
1706: /* Now we can allocate the necessary number of ISs. */
1707: *nsub = s;
1708: PetscCall(PetscMalloc1(*nsub, is));
1709: PetscCall(PetscMalloc1(*nsub, is_local));
1710: s = 0;
1711: ystart = 0;
1712: for (PetscInt j = 0; j < Ndomains; ++j) {
1713: maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
1714: PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical direction for mesh height %" PetscInt_FMT, Ndomains, N);
1715: /* Vertical domain limits with an overlap. */
1716: y[0][0] = PetscMax(ystart - overlap, 0);
1717: y[0][1] = PetscMin(ystart + maxheight + overlap, N);
1718: /* Vertical domain limits without an overlap. */
1719: y[1][0] = ystart;
1720: y[1][1] = PetscMin(ystart + maxheight, N);
1721: xstart = 0;
1722: for (PetscInt i = 0; i < Mdomains; ++i) {
1723: maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
1724: PetscCheck(maxwidth >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the horizontal direction for mesh width %" PetscInt_FMT, Mdomains, M);
1725: /* Horizontal domain limits with an overlap. */
1726: x[0][0] = PetscMax(xstart - overlap, 0);
1727: x[0][1] = PetscMin(xstart + maxwidth + overlap, M);
1728: /* Horizontal domain limits without an overlap. */
1729: x[1][0] = xstart;
1730: x[1][1] = PetscMin(xstart + maxwidth, M);
1731: /*
1732: Determine whether this domain intersects this rank's ownership range of pc->pmat.
1733: Do this twice: first for the domains with overlaps, and once without.
1734: During the first pass create the subcommunicators, and use them on the second pass as well.
1735: */
1736: for (q = 0; q < 2; ++q) {
1737: PetscBool split = PETSC_FALSE;
1738: /*
1739: domain limits, (xleft, xright) and (ylow, yheigh) are adjusted
1740: according to whether the domain with an overlap or without is considered.
1741: */
1742: xleft = x[q][0];
1743: xright = x[q][1];
1744: ylow = y[q][0];
1745: yhigh = y[q][1];
1746: PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
1747: nidx *= dof;
1748: n[q] = nidx;
1749: /*
1750: Based on the counted number of indices in the local domain *with an overlap*,
1751: construct a subcommunicator of all the MPI ranks supporting this domain.
1752: Observe that a domain with an overlap might have nontrivial local support,
1753: while the domain without an overlap might not. Hence, the decision to participate
1754: in the subcommunicator must be based on the domain with an overlap.
1755: */
1756: if (q == 0) {
1757: if (nidx) color = 1;
1758: else color = MPI_UNDEFINED;
1759: PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm));
1760: split = PETSC_TRUE;
1761: }
1762: /*
1763: Proceed only if the number of local indices *with an overlap* is nonzero.
1764: */
1765: if (n[0]) {
1766: if (q == 0) xis = is;
1767: if (q == 1) {
1768: /*
1769: The IS for the no-overlap subdomain shares a communicator with the overlapping domain.
1770: Moreover, if the overlap is zero, the two ISs are identical.
1771: */
1772: if (overlap == 0) {
1773: (*is_local)[s] = (*is)[s];
1774: PetscCall(PetscObjectReference((PetscObject)(*is)[s]));
1775: continue;
1776: } else {
1777: xis = is_local;
1778: subcomm = ((PetscObject)(*is)[s])->comm;
1779: }
1780: } /* if (q == 1) */
1781: idx = NULL;
1782: PetscCall(PetscMalloc1(nidx, &idx));
1783: if (nidx) {
1784: k = 0;
1785: for (jj = ylow_loc; jj < yhigh_loc; ++jj) {
1786: PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft;
1787: PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright;
1788: kk = dof * (M * jj + x0);
1789: for (ii = x0; ii < x1; ++ii) {
1790: for (d = 0; d < dof; ++d) idx[k++] = kk++;
1791: }
1792: }
1793: }
1794: PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s));
1795: if (split) PetscCallMPI(MPI_Comm_free(&subcomm));
1796: } /* if (n[0]) */
1797: } /* for (q = 0; q < 2; ++q) */
1798: if (n[0]) ++s;
1799: xstart += maxwidth;
1800: } /* for (PetscInt i = 0; i < Mdomains; ++i) */
1801: ystart += maxheight;
1802: } /* for (PetscInt j = 0; j < Ndomains; ++j) */
1803: PetscFunctionReturn(PETSC_SUCCESS);
1804: }
1806: /*@
1807: PCGASMGetSubdomains - Gets the subdomains supported on this MPI process
1808: for the `PCGASM` additive Schwarz preconditioner.
1810: Not Collective
1812: Input Parameter:
1813: . pc - the preconditioner context
1815: Output Parameters:
1816: + n - the number of subdomains for this MPI process (default value = 1)
1817: . iis - the index sets that define the inner subdomains (without overlap) supported on this process (can be `NULL`)
1818: - ois - the index sets that define the outer subdomains (with overlap) supported on this process (can be `NULL`)
1820: Level: advanced
1822: Notes:
1823: The user is responsible for destroying the `IS`s and freeing the returned arrays, this can be done with
1824: `PCGASMDestroySubdomains()`
1826: The `IS` numbering is in the parallel, global numbering of the vector.
1828: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`,
1829: `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()`, `PCGASMDestroySubdomains()`
1830: @*/
1831: PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[])
1832: {
1833: PC_GASM *osm;
1834: PetscBool match;
1836: PetscFunctionBegin;
1838: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1839: PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1840: osm = (PC_GASM *)pc->data;
1841: if (n) *n = osm->n;
1842: if (iis) PetscCall(PetscMalloc1(osm->n, iis));
1843: if (ois) PetscCall(PetscMalloc1(osm->n, ois));
1844: if (iis || ois) {
1845: for (PetscInt i = 0; i < osm->n; ++i) {
1846: if (iis) (*iis)[i] = osm->iis[i];
1847: if (ois) (*ois)[i] = osm->ois[i];
1848: }
1849: }
1850: PetscFunctionReturn(PETSC_SUCCESS);
1851: }
1853: /*@
1854: PCGASMGetSubmatrices - Gets the local submatrices (for this MPI process
1855: only) for the `PCGASM` additive Schwarz preconditioner.
1857: Not Collective
1859: Input Parameter:
1860: . pc - the preconditioner context
1862: Output Parameters:
1863: + n - the number of matrices for this MPI process (default value = 1)
1864: - mat - the matrices
1866: Level: advanced
1868: Note:
1869: Matrices returned by this routine have the same communicators as the index sets (`IS`)
1870: used to define subdomains in `PCGASMSetSubdomains()`
1872: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1873: `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()`
1874: @*/
1875: PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[])
1876: {
1877: PC_GASM *osm;
1878: PetscBool match;
1880: PetscFunctionBegin;
1882: PetscAssertPointer(n, 2);
1883: if (mat) PetscAssertPointer(mat, 3);
1884: PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp().");
1885: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1886: PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1887: osm = (PC_GASM *)pc->data;
1888: if (n) *n = osm->n;
1889: if (mat) *mat = osm->pmat;
1890: PetscFunctionReturn(PETSC_SUCCESS);
1891: }
1893: /*@
1894: PCGASMSetUseDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible for `PCGASM`
1896: Logically Collective
1898: Input Parameters:
1899: + pc - the preconditioner
1900: - flg - boolean indicating whether to use subdomains defined by the `DM`
1902: Options Database Key:
1903: + -pc_gasm_dm_subdomains - configure subdomains
1904: . -pc_gasm_overlap - set overlap
1905: - -pc_gasm_total_subdomains - set number of subdomains
1907: Level: intermediate
1909: Note:
1910: `PCGASMSetSubdomains()`, `PCGASMSetTotalSubdomains()` or `PCGASMSetOverlap()` take precedence over `PCGASMSetUseDMSubdomains()`,
1911: so setting `PCGASMSetSubdomains()` with nontrivial subdomain ISs or any of `PCGASMSetTotalSubdomains()` and `PCGASMSetOverlap()`
1912: automatically turns the latter off.
1914: .seealso: [](ch_ksp), `PCGASM`, `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
1915: `PCGASMCreateSubdomains2D()`
1916: @*/
1917: PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg)
1918: {
1919: PC_GASM *osm = (PC_GASM *)pc->data;
1920: PetscBool match;
1922: PetscFunctionBegin;
1925: PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC.");
1926: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1927: if (match && !osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) osm->dm_subdomains = flg;
1928: PetscFunctionReturn(PETSC_SUCCESS);
1929: }
1931: /*@
1932: PCGASMGetUseDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible with `PCGASM`
1934: Not Collective
1936: Input Parameter:
1937: . pc - the preconditioner
1939: Output Parameter:
1940: . flg - boolean indicating whether to use subdomains defined by the `DM`
1942: Level: intermediate
1944: .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()`,
1945: `PCGASMCreateSubdomains2D()`
1946: @*/
1947: PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg)
1948: {
1949: PC_GASM *osm = (PC_GASM *)pc->data;
1950: PetscBool match;
1952: PetscFunctionBegin;
1954: PetscAssertPointer(flg, 2);
1955: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1956: if (match) {
1957: if (flg) *flg = osm->dm_subdomains;
1958: }
1959: PetscFunctionReturn(PETSC_SUCCESS);
1960: }