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