Actual source code: asm.c
1: /*
2: This file defines an additive Schwarz preconditioner for any Mat implementation.
4: Note that each processor may have any number of subdomains. But in order to
5: deal easily with the VecScatter(), we treat each processor as if it has the
6: same number of subdomains.
8: n - total number of true subdomains on all processors
9: n_local_true - actual number of subdomains on this processor
10: n_local = maximum over all processors of n_local_true
11: */
13: #include <petsc/private/pcasmimpl.h>
14: #include <petsc/private/matimpl.h>
16: static PetscErrorCode PCView_ASM(PC pc, PetscViewer viewer)
17: {
18: PC_ASM *osm = (PC_ASM *)pc->data;
19: PetscMPIInt rank;
20: PetscInt i, bsz;
21: PetscBool isascii, isstring;
22: PetscViewer sviewer;
23: PetscViewerFormat format;
24: const char *prefix;
26: PetscFunctionBegin;
27: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
28: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
29: if (isascii) {
30: char overlaps[256] = "user-defined overlap", blocks[256] = "total subdomain blocks not yet set";
31: if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlaps, sizeof(overlaps), "amount of overlap = %" PetscInt_FMT, osm->overlap));
32: if (osm->n > 0) PetscCall(PetscSNPrintf(blocks, sizeof(blocks), "total subdomain blocks = %" PetscInt_FMT, osm->n));
33: PetscCall(PetscViewerASCIIPrintf(viewer, " %s, %s\n", blocks, overlaps));
34: PetscCall(PetscViewerASCIIPrintf(viewer, " restriction/interpolation type - %s\n", PCASMTypes[osm->type]));
35: if (osm->dm_subdomains) PetscCall(PetscViewerASCIIPrintf(viewer, " Additive Schwarz: using DM to define subdomains\n"));
36: if (osm->loctype != PC_COMPOSITE_ADDITIVE) PetscCall(PetscViewerASCIIPrintf(viewer, " Additive Schwarz: local solve composition type - %s\n", PCCompositeTypes[osm->loctype]));
37: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
38: PetscCall(PetscViewerGetFormat(viewer, &format));
39: if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) {
40: if (osm->ksp) {
41: PetscCall(PetscViewerASCIIPrintf(viewer, " Local solver information for first block is in the following KSP and PC objects on rank 0:\n"));
42: PetscCall(PCGetOptionsPrefix(pc, &prefix));
43: PetscCall(PetscViewerASCIIPrintf(viewer, " Use -%sksp_view ::ascii_info_detail to display information for all blocks\n", prefix ? prefix : ""));
44: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
45: if (rank == 0) {
46: PetscCall(PetscViewerASCIIPushTab(sviewer));
47: PetscCall(KSPView(osm->ksp[0], sviewer));
48: PetscCall(PetscViewerASCIIPopTab(sviewer));
49: }
50: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
51: }
52: } else {
53: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
54: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] number of local blocks = %" PetscInt_FMT "\n", rank, osm->n_local_true));
55: PetscCall(PetscViewerFlush(viewer));
56: PetscCall(PetscViewerASCIIPrintf(viewer, " Local solver information for each block is in the following KSP and PC objects:\n"));
57: PetscCall(PetscViewerASCIIPushTab(viewer));
58: PetscCall(PetscViewerASCIIPrintf(viewer, "- - - - - - - - - - - - - - - - - -\n"));
59: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
60: for (i = 0; i < osm->n_local_true; i++) {
61: PetscCall(ISGetLocalSize(osm->is[i], &bsz));
62: PetscCall(PetscViewerASCIIPrintf(sviewer, "[%d] local block number %" PetscInt_FMT ", size = %" PetscInt_FMT "\n", rank, i, bsz));
63: PetscCall(KSPView(osm->ksp[i], sviewer));
64: PetscCall(PetscViewerASCIIPrintf(sviewer, "- - - - - - - - - - - - - - - - - -\n"));
65: }
66: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
67: PetscCall(PetscViewerASCIIPopTab(viewer));
68: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
69: }
70: } else if (isstring) {
71: PetscCall(PetscViewerStringSPrintf(viewer, " blocks=%" PetscInt_FMT ", overlap=%" PetscInt_FMT ", type=%s", osm->n, osm->overlap, PCASMTypes[osm->type]));
72: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
73: if (osm->ksp) PetscCall(KSPView(osm->ksp[0], sviewer));
74: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
75: }
76: PetscFunctionReturn(PETSC_SUCCESS);
77: }
79: static PetscErrorCode PCASMPrintSubdomains(PC pc)
80: {
81: PC_ASM *osm = (PC_ASM *)pc->data;
82: const char *prefix;
83: char fname[PETSC_MAX_PATH_LEN + 1];
84: PetscViewer viewer, sviewer;
85: char *s;
86: PetscInt i, j, nidx;
87: const PetscInt *idx;
88: PetscMPIInt rank, size;
90: PetscFunctionBegin;
91: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
92: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
93: PetscCall(PCGetOptionsPrefix(pc, &prefix));
94: PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_asm_print_subdomains", fname, sizeof(fname), NULL));
95: if (fname[0] == 0) PetscCall(PetscStrncpy(fname, "stdout", sizeof(fname)));
96: PetscCall(PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc), fname, &viewer));
97: for (i = 0; i < osm->n_local; i++) {
98: if (i < osm->n_local_true) {
99: PetscCall(ISGetLocalSize(osm->is[i], &nidx));
100: PetscCall(ISGetIndices(osm->is[i], &idx));
101: /* Print to a string viewer; no more than 15 characters per index plus 512 char for the header.*/
102: #define len 16 * (nidx + 1) + 512
103: PetscCall(PetscMalloc1(len, &s));
104: PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, s, len, &sviewer));
105: #undef len
106: PetscCall(PetscViewerStringSPrintf(sviewer, "[%d:%d] Subdomain %" PetscInt_FMT " with overlap:\n", rank, size, i));
107: for (j = 0; j < nidx; j++) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
108: PetscCall(ISRestoreIndices(osm->is[i], &idx));
109: PetscCall(PetscViewerStringSPrintf(sviewer, "\n"));
110: PetscCall(PetscViewerDestroy(&sviewer));
111: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
112: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", s));
113: PetscCall(PetscViewerFlush(viewer));
114: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
115: PetscCall(PetscFree(s));
116: if (osm->is_local) {
117: /* Print to a string viewer; no more than 15 characters per index plus 512 char for the header.*/
118: #define len 16 * (nidx + 1) + 512
119: PetscCall(PetscMalloc1(len, &s));
120: PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, s, len, &sviewer));
121: #undef len
122: PetscCall(PetscViewerStringSPrintf(sviewer, "[%d:%d] Subdomain %" PetscInt_FMT " without overlap:\n", rank, size, i));
123: PetscCall(ISGetLocalSize(osm->is_local[i], &nidx));
124: PetscCall(ISGetIndices(osm->is_local[i], &idx));
125: for (j = 0; j < nidx; j++) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
126: PetscCall(ISRestoreIndices(osm->is_local[i], &idx));
127: PetscCall(PetscViewerStringSPrintf(sviewer, "\n"));
128: PetscCall(PetscViewerDestroy(&sviewer));
129: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
130: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", s));
131: PetscCall(PetscViewerFlush(viewer));
132: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
133: PetscCall(PetscFree(s));
134: }
135: } else {
136: /* Participate in collective viewer calls. */
137: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
138: PetscCall(PetscViewerFlush(viewer));
139: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
140: /* Assume either all ranks have is_local or none do. */
141: if (osm->is_local) {
142: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
143: PetscCall(PetscViewerFlush(viewer));
144: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
145: }
146: }
147: }
148: PetscCall(PetscViewerFlush(viewer));
149: PetscCall(PetscViewerDestroy(&viewer));
150: PetscFunctionReturn(PETSC_SUCCESS);
151: }
153: static PetscErrorCode PCSetUp_ASM(PC pc)
154: {
155: PC_ASM *osm = (PC_ASM *)pc->data;
156: PetscBool flg;
157: PetscInt i, m, m_local;
158: MatReuse scall = MAT_REUSE_MATRIX;
159: IS isl;
160: KSP ksp;
161: PC subpc;
162: const char *prefix, *pprefix;
163: Vec vec;
164: DM *domain_dm = NULL;
165: MatNullSpace *nullsp = NULL;
167: PetscFunctionBegin;
168: if (!pc->setupcalled) {
169: PetscInt m;
171: /* Note: if subdomains have been set either via PCASMSetTotalSubdomains() or via PCASMSetLocalSubdomains(), osm->n_local_true will not be PETSC_DECIDE */
172: if (osm->n_local_true == PETSC_DECIDE) {
173: /* no subdomains given */
174: /* try pc->dm first, if allowed */
175: if (osm->dm_subdomains && pc->dm) {
176: PetscInt num_domains;
177: char **domain_names;
178: IS *inner_domain_is, *outer_domain_is;
179: PetscCall(DMCreateDomainDecomposition(pc->dm, &num_domains, &domain_names, &inner_domain_is, &outer_domain_is, &domain_dm));
180: osm->overlap = -1; /* We do not want to increase the overlap of the IS.
181: A future improvement of this code might allow one to use
182: DM-defined subdomains and also increase the overlap,
183: but that is not currently supported */
184: if (num_domains) PetscCall(PCASMSetLocalSubdomains(pc, num_domains, outer_domain_is, inner_domain_is));
185: for (PetscInt d = 0; d < num_domains; ++d) {
186: if (domain_names) PetscCall(PetscFree(domain_names[d]));
187: if (inner_domain_is) PetscCall(ISDestroy(&inner_domain_is[d]));
188: if (outer_domain_is) PetscCall(ISDestroy(&outer_domain_is[d]));
189: }
190: PetscCall(PetscFree(domain_names));
191: PetscCall(PetscFree(inner_domain_is));
192: PetscCall(PetscFree(outer_domain_is));
193: }
194: if (osm->n_local_true == PETSC_DECIDE) {
195: /* still no subdomains; use one subdomain per processor */
196: osm->n_local_true = 1;
197: }
198: }
199: { /* determine the global and max number of subdomains */
200: struct {
201: PetscInt max, sum;
202: } outwork;
203: PetscMPIInt size;
205: outwork.max = osm->n_local_true;
206: outwork.sum = osm->n_local_true;
207: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &outwork, 1, MPIU_2INT, MPIU_MAXSUM_OP, PetscObjectComm((PetscObject)pc)));
208: osm->n_local = outwork.max;
209: osm->n = outwork.sum;
211: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
212: if (outwork.max == 1 && outwork.sum == size) {
213: /* osm->n_local_true = 1 on all processes, set this option may enable use of optimized MatCreateSubMatrices() implementation */
214: PetscCall(MatSetOption(pc->pmat, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
215: }
216: }
217: if (!osm->is) { /* create the index sets */
218: PetscCall(PCASMCreateSubdomains(pc->pmat, osm->n_local_true, &osm->is));
219: }
220: if (osm->n_local_true > 1 && !osm->is_local) {
221: PetscCall(PetscMalloc1(osm->n_local_true, &osm->is_local));
222: for (i = 0; i < osm->n_local_true; i++) {
223: if (osm->overlap > 0) { /* With positive overlap, osm->is[i] will be modified */
224: PetscCall(ISDuplicate(osm->is[i], &osm->is_local[i]));
225: PetscCall(ISCopy(osm->is[i], osm->is_local[i]));
226: } else {
227: PetscCall(PetscObjectReference((PetscObject)osm->is[i]));
228: osm->is_local[i] = osm->is[i];
229: }
230: }
231: }
232: PetscCall(PCGetOptionsPrefix(pc, &prefix));
233: if (osm->overlap > 0) {
234: /* Extend the "overlapping" regions by a number of steps */
235: PetscCall(MatIncreaseOverlap(pc->pmat, osm->n_local_true, osm->is, osm->overlap));
236: }
237: if (osm->sort_indices) {
238: for (i = 0; i < osm->n_local_true; i++) {
239: PetscCall(ISSort(osm->is[i]));
240: if (osm->is_local) PetscCall(ISSort(osm->is_local[i]));
241: }
242: }
243: flg = PETSC_FALSE;
244: PetscCall(PetscOptionsHasName(NULL, prefix, "-pc_asm_print_subdomains", &flg));
245: if (flg) PetscCall(PCASMPrintSubdomains(pc));
246: if (!osm->ksp) {
247: /* Create the local solvers */
248: PetscCall(PetscMalloc1(osm->n_local_true, &osm->ksp));
249: if (domain_dm) PetscCall(PetscInfo(pc, "Setting up ASM subproblems using the embedded DM\n"));
250: for (i = 0; i < osm->n_local_true; i++) {
251: PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
252: PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
253: PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
254: PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
255: PetscCall(KSPSetType(ksp, KSPPREONLY));
256: PetscCall(KSPGetPC(ksp, &subpc));
257: PetscCall(PCGetOptionsPrefix(pc, &prefix));
258: PetscCall(KSPSetOptionsPrefix(ksp, prefix));
259: PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
260: if (domain_dm) {
261: PetscCall(KSPSetDM(ksp, domain_dm[i]));
262: PetscCall(KSPSetDMActive(ksp, KSP_DMACTIVE_ALL, PETSC_FALSE));
263: PetscCall(DMDestroy(&domain_dm[i]));
264: }
265: osm->ksp[i] = ksp;
266: }
267: PetscCall(PetscFree(domain_dm));
268: }
270: PetscCall(ISConcatenate(PETSC_COMM_SELF, osm->n_local_true, osm->is, &osm->lis));
271: if (osm->n_local > 1) PetscCall(ISSortRemoveDups(osm->lis));
272: PetscCall(ISGetLocalSize(osm->lis, &m));
274: scall = MAT_INITIAL_MATRIX;
275: } else {
276: /*
277: Destroy the blocks from the previous iteration
278: */
279: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
280: PetscCall(MatGetNullSpaces(osm->n_local_true, osm->pmat, &nullsp));
281: PetscCall(MatDestroyMatrices(osm->n_local_true, &osm->pmat));
282: scall = MAT_INITIAL_MATRIX;
283: }
284: }
286: /* Destroy previous submatrices of a different type than pc->pmat since MAT_REUSE_MATRIX won't work in that case */
287: if (scall == MAT_REUSE_MATRIX && osm->sub_mat_type) {
288: PetscCall(MatGetNullSpaces(osm->n_local_true, osm->pmat, &nullsp));
289: if (osm->n_local_true > 0) PetscCall(MatDestroySubMatrices(osm->n_local_true, &osm->pmat));
290: scall = MAT_INITIAL_MATRIX;
291: }
293: /* A subsolver may have factored its submatrix in place, which leaves it
294: flagged as factored and so unfillable by MatCreateSubMatrices() below. */
295: if (scall == MAT_REUSE_MATRIX) {
296: for (i = 0; i < osm->n_local_true; i++) PetscCall(MatSetUnfactored(osm->pmat[i]));
297: }
299: /*
300: Extract out the submatrices
301: */
302: PetscCall(MatCreateSubMatrices(pc->pmat, osm->n_local_true, osm->is, osm->is, scall, &osm->pmat));
303: if (scall == MAT_INITIAL_MATRIX) {
304: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix));
305: for (i = 0; i < osm->n_local_true; i++) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix));
306: if (nullsp) PetscCall(MatRestoreNullSpaces(osm->n_local_true, osm->pmat, &nullsp));
307: }
309: /* Convert the types of the submatrices (if needbe) */
310: if (osm->sub_mat_type) {
311: for (i = 0; i < osm->n_local_true; i++) PetscCall(MatConvert(osm->pmat[i], osm->sub_mat_type, MAT_INPLACE_MATRIX, &osm->pmat[i]));
312: }
314: if (!pc->setupcalled) {
315: VecType vtype;
317: /* Create the local work vectors (from the local matrices) and scatter contexts */
318: PetscCall(MatCreateVecs(pc->pmat, &vec, NULL));
320: PetscCheck(!osm->is_local || osm->n_local_true == 1 || (osm->type != PC_ASM_INTERPOLATE && osm->type != PC_ASM_NONE), PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot use interpolate or none PCASMType if is_local was provided to PCASMSetLocalSubdomains() with more than a single subdomain");
321: if (osm->is_local && osm->type != PC_ASM_BASIC && osm->type != PC_ASM_WEIGHTED && osm->loctype == PC_COMPOSITE_ADDITIVE) PetscCall(PetscMalloc1(osm->n_local_true, &osm->lprolongation));
322: PetscCall(PetscMalloc1(osm->n_local_true, &osm->lrestriction));
323: PetscCall(PetscMalloc1(osm->n_local_true, &osm->x));
324: PetscCall(PetscMalloc1(osm->n_local_true, &osm->y));
326: PetscCall(ISGetLocalSize(osm->lis, &m));
327: PetscCall(ISCreateStride(PETSC_COMM_SELF, m, 0, 1, &isl));
328: if (osm->n_local == 1) PetscCall(MatCreateVecs(osm->pmat[0], &osm->lx, NULL));
329: else {
330: PetscCall(MatGetVecType(osm->pmat[0], &vtype));
331: PetscCall(VecCreate(PETSC_COMM_SELF, &osm->lx));
332: PetscCall(VecSetSizes(osm->lx, m, m));
333: PetscCall(VecSetType(osm->lx, vtype));
334: }
335: PetscCall(VecDuplicate(osm->lx, &osm->ly));
336: PetscCall(VecScatterCreate(vec, osm->lis, osm->lx, isl, &osm->restriction));
337: PetscCall(ISDestroy(&isl));
339: for (i = 0; i < osm->n_local_true; ++i) {
340: ISLocalToGlobalMapping ltog;
341: IS isll;
342: const PetscInt *idx_is;
343: PetscInt *idx_lis, nout;
345: if (osm->n_local == 1) {
346: Vec local;
347: PetscInt start, end;
349: /* The process and subdomain work vectors coincide. Build the local parts of the
350: restriction and prolongation directly in subdomain order */
351: osm->x[0] = osm->lx;
352: osm->y[0] = osm->ly;
353: PetscCall(PetscObjectReference((PetscObject)osm->x[0]));
354: PetscCall(PetscObjectReference((PetscObject)osm->y[0]));
355: PetscCall(VecGetOwnershipRange(vec, &start, &end));
356: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, end - start, NULL, &local));
357: PetscCall(ISLocalToGlobalMappingCreateIS(osm->is[0], <og));
358: for (PetscInt j = 0; j < (osm->lprolongation != NULL ? 2 : 1); ++j) {
359: IS source, target, is = j ? osm->is_local[0] : osm->is[0];
360: const PetscInt *idx;
361: PetscInt n, count = 0;
362: PetscInt *from, *to;
364: PetscCall(ISGetLocalSize(is, &n));
365: PetscCall(ISGetIndices(is, &idx));
366: PetscCall(PetscMalloc1(n, &from));
367: PetscCall(PetscMalloc1(n, &to));
368: PetscCall(ISGlobalToLocalMappingApply(ltog, IS_GTOLM_DROP, n, idx, &nout, to));
369: PetscCheck(nout == n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "is_local not a subset of is");
370: for (PetscInt k = 0; k < n; ++k) {
371: if (idx[k] >= start && idx[k] < end) {
372: from[count] = idx[k] - start;
373: to[count++] = to[k];
374: }
375: }
376: PetscCall(ISRestoreIndices(is, &idx));
377: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count, from, PETSC_OWN_POINTER, &source));
378: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count, to, PETSC_OWN_POINTER, &target));
379: PetscCall(VecScatterCreate(local, source, osm->lx, target, j ? &osm->lprolongation[0] : &osm->lrestriction[0]));
380: PetscCall(ISDestroy(&source));
381: PetscCall(ISDestroy(&target));
382: }
383: PetscCall(ISLocalToGlobalMappingDestroy(<og));
384: PetscCall(VecDestroy(&local));
385: continue;
386: }
387: PetscCall(ISGetLocalSize(osm->is[i], &m));
388: PetscCall(MatCreateVecs(osm->pmat[i], &osm->x[i], NULL));
389: PetscCall(VecDuplicate(osm->x[i], &osm->y[i]));
391: /* generate a scatter from ly to y[i] picking all the overlapping is[i] entries */
392: PetscCall(ISLocalToGlobalMappingCreateIS(osm->lis, <og));
393: PetscCall(ISGetLocalSize(osm->is[i], &m));
394: PetscCall(ISGetIndices(osm->is[i], &idx_is));
395: PetscCall(PetscMalloc1(m, &idx_lis));
396: PetscCall(ISGlobalToLocalMappingApply(ltog, IS_GTOLM_DROP, m, idx_is, &nout, idx_lis));
397: PetscCheck(nout == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "is not a subset of lis");
398: PetscCall(ISRestoreIndices(osm->is[i], &idx_is));
399: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, m, idx_lis, PETSC_OWN_POINTER, &isll));
400: PetscCall(ISLocalToGlobalMappingDestroy(<og));
401: PetscCall(ISCreateStride(PETSC_COMM_SELF, m, 0, 1, &isl));
402: PetscCall(VecScatterCreate(osm->ly, isll, osm->y[i], isl, &osm->lrestriction[i]));
403: PetscCall(ISDestroy(&isll));
404: PetscCall(ISDestroy(&isl));
405: if (osm->lprolongation) { /* generate a scatter from y[i] to ly picking only the non-overlapping is_local[i] entries */
406: ISLocalToGlobalMapping ltog;
407: IS isll, isll_local;
408: const PetscInt *idx_local;
409: PetscInt *idx1, *idx2, nout;
411: PetscCall(ISGetLocalSize(osm->is_local[i], &m_local));
412: PetscCall(ISGetIndices(osm->is_local[i], &idx_local));
414: PetscCall(ISLocalToGlobalMappingCreateIS(osm->is[i], <og));
415: PetscCall(PetscMalloc1(m_local, &idx1));
416: PetscCall(ISGlobalToLocalMappingApply(ltog, IS_GTOLM_DROP, m_local, idx_local, &nout, idx1));
417: PetscCall(ISLocalToGlobalMappingDestroy(<og));
418: PetscCheck(nout == m_local, PETSC_COMM_SELF, PETSC_ERR_PLIB, "is_local not a subset of is");
419: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, m_local, idx1, PETSC_OWN_POINTER, &isll));
421: PetscCall(ISLocalToGlobalMappingCreateIS(osm->lis, <og));
422: PetscCall(PetscMalloc1(m_local, &idx2));
423: PetscCall(ISGlobalToLocalMappingApply(ltog, IS_GTOLM_DROP, m_local, idx_local, &nout, idx2));
424: PetscCall(ISLocalToGlobalMappingDestroy(<og));
425: PetscCheck(nout == m_local, PETSC_COMM_SELF, PETSC_ERR_PLIB, "is_local not a subset of lis");
426: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, m_local, idx2, PETSC_OWN_POINTER, &isll_local));
428: PetscCall(ISRestoreIndices(osm->is_local[i], &idx_local));
429: PetscCall(VecScatterCreate(osm->y[i], isll, osm->ly, isll_local, &osm->lprolongation[i]));
431: PetscCall(ISDestroy(&isll));
432: PetscCall(ISDestroy(&isll_local));
433: }
434: }
435: PetscCall(VecDestroy(&vec));
436: }
438: if (osm->loctype == PC_COMPOSITE_MULTIPLICATIVE && osm->n_local > 1) {
439: IS *cis;
441: PetscCall(PetscMalloc1(osm->n_local_true, &cis));
442: for (PetscInt c = 0; c < osm->n_local_true; ++c) cis[c] = osm->lis;
443: PetscCall(MatCreateSubMatrices(pc->pmat, osm->n_local_true, osm->is, cis, scall, &osm->lmats));
444: PetscCall(PetscFree(cis));
445: }
447: /* Return control to the user so that the submatrices can be modified (e.g., to apply
448: different boundary conditions for the submatrices than for the global problem) */
449: PetscCall(PCModifySubMatrices(pc, osm->n_local_true, osm->is, osm->is, osm->pmat, pc->modifysubmatricesP));
451: /* Loop over subdomains putting them into local KSP */
452: PetscCall(KSPGetOptionsPrefix(osm->ksp[0], &prefix));
453: for (i = 0; i < osm->n_local_true; i++) {
454: PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i]));
455: PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix));
456: if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i]));
457: }
458: if (osm->type == PC_ASM_WEIGHTED && osm->computescaling) {
459: if (!osm->scaling) PetscCall(PetscCalloc1(osm->n_local_true, &osm->scaling));
460: for (i = 0; i < osm->n_local_true; i++) {
461: if (!osm->scaling[i]) PetscCall(VecDuplicate(osm->x[i], &osm->scaling[i]));
462: PetscCallBack("PCASMWeightedComputeScalingFn", (*osm->computescaling)(pc, i, osm->scaling[i], osm->computescalingctx));
463: }
464: }
465: PetscFunctionReturn(PETSC_SUCCESS);
466: }
468: static PetscErrorCode PCSetUpOnBlocks_ASM(PC pc)
469: {
470: PC_ASM *osm = (PC_ASM *)pc->data;
471: PetscInt i;
472: KSPConvergedReason reason;
474: PetscFunctionBegin;
475: for (i = 0; i < osm->n_local_true; i++) {
476: PetscCall(KSPSetUp(osm->ksp[i]));
477: PetscCall(KSPGetConvergedReason(osm->ksp[i], &reason));
478: if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
479: }
480: PetscFunctionReturn(PETSC_SUCCESS);
481: }
483: static PetscErrorCode PCASMGetVecScatters_Private(PC pc, PetscBool transpose, VecScatter *restriction, VecScatter *prolongation)
484: {
485: PC_ASM *osm = (PC_ASM *)pc->data;
486: PCASMType type = osm->type == PC_ASM_WEIGHTED ? PC_ASM_BASIC : osm->type;
487: VecScatter forward, reverse;
489: PetscFunctionBegin;
490: PetscCheck(osm->loctype == PC_COMPOSITE_MULTIPLICATIVE || osm->loctype == PC_COMPOSITE_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Invalid local composition type: %s", PCCompositeTypes[osm->loctype]);
491: forward = type & PC_ASM_RESTRICT ? osm->restriction : osm->lrestriction[0];
492: reverse = type & PC_ASM_INTERPOLATE ? osm->restriction : (osm->lprolongation != NULL ? osm->lprolongation[0] : osm->lrestriction[0]);
493: *restriction = transpose == PETSC_TRUE ? reverse : forward;
494: *prolongation = transpose == PETSC_TRUE ? forward : reverse;
495: PetscFunctionReturn(PETSC_SUCCESS);
496: }
498: static PetscErrorCode PCApply_ASM(PC pc, Vec x, Vec y)
499: {
500: PC_ASM *osm = (PC_ASM *)pc->data;
501: PCASMType type = osm->type == PC_ASM_WEIGHTED ? PC_ASM_BASIC : osm->type; /* PC_ASM_WEIGHTED scatters like PC_ASM_BASIC, then applies the PCASMWeightedSetScaling() weights */
502: PetscInt i, n_local_true = osm->n_local_true;
503: ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
504: VecScatter restriction = osm->restriction, prolongation = osm->restriction;
506: PetscFunctionBegin;
507: PetscCheck(osm->type != PC_ASM_WEIGHTED || osm->scaling, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Call PCASMWeightedSetScaling() after PCSetUp() before applying PC_ASM_WEIGHTED");
508: if (osm->n_local == 1) {
509: PetscCall(PCASMGetVecScatters_Private(pc, PETSC_FALSE, &restriction, &prolongation));
510: if (restriction != osm->restriction) PetscCall(VecZeroEntries(osm->lx));
511: } else {
512: /* limit restriction or interpolation to local subdomain values when requested */
513: if (!(type & PC_ASM_RESTRICT)) {
514: forward = SCATTER_FORWARD_LOCAL;
515: /* have to zero the work RHS since scatter may leave some slots empty */
516: PetscCall(VecSet(osm->lx, 0.0));
517: }
518: if (!(type & PC_ASM_INTERPOLATE)) reverse = SCATTER_REVERSE_LOCAL;
519: }
521: PetscCheck(osm->loctype == PC_COMPOSITE_MULTIPLICATIVE || osm->loctype == PC_COMPOSITE_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Invalid local composition type: %s", PCCompositeTypes[osm->loctype]);
522: /* zero the global and the local solutions */
523: PetscCall(VecSet(y, 0.0));
524: if (osm->n_local > 1) PetscCall(VecSet(osm->ly, 0.0));
526: /* copy the global RHS to local RHS including the ghost nodes */
527: PetscCall(VecScatterBegin(restriction, x, osm->lx, INSERT_VALUES, forward));
528: PetscCall(VecScatterEnd(restriction, x, osm->lx, INSERT_VALUES, forward));
530: /* restrict local RHS to the overlapping 0-block RHS */
531: if (osm->n_local > 1) {
532: PetscCall(VecScatterBegin(osm->lrestriction[0], osm->lx, osm->x[0], INSERT_VALUES, forward));
533: PetscCall(VecScatterEnd(osm->lrestriction[0], osm->lx, osm->x[0], INSERT_VALUES, forward));
534: }
536: /* do the local solves */
537: for (i = 0; i < n_local_true; ++i) {
538: /* solve the overlapping i-block */
539: PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, osm->ksp[i], osm->x[i], osm->y[i], 0));
540: PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]));
541: PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
542: PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, osm->ksp[i], osm->x[i], osm->y[i], 0));
543: if (osm->type == PC_ASM_WEIGHTED) PetscCall(VecPointwiseMult(osm->y[i], osm->scaling[i], osm->y[i]));
544: if (osm->n_local == 1) break;
546: if (osm->lprolongation && !(type & PC_ASM_INTERPOLATE)) { /* interpolate the non-overlapping i-block solution to the local solution (only for restrictive additive) */
547: PetscCall(VecScatterBegin(osm->lprolongation[i], osm->y[i], osm->ly, ADD_VALUES, forward));
548: PetscCall(VecScatterEnd(osm->lprolongation[i], osm->y[i], osm->ly, ADD_VALUES, forward));
549: } else { /* interpolate the overlapping i-block solution to the local solution */
550: PetscCall(VecScatterBegin(osm->lrestriction[i], osm->y[i], osm->ly, ADD_VALUES, reverse));
551: PetscCall(VecScatterEnd(osm->lrestriction[i], osm->y[i], osm->ly, ADD_VALUES, reverse));
552: }
554: if (i < n_local_true - 1) {
555: /* restrict local RHS to the overlapping (i+1)-block RHS */
556: PetscCall(VecScatterBegin(osm->lrestriction[i + 1], osm->lx, osm->x[i + 1], INSERT_VALUES, forward));
557: PetscCall(VecScatterEnd(osm->lrestriction[i + 1], osm->lx, osm->x[i + 1], INSERT_VALUES, forward));
559: if (osm->loctype == PC_COMPOSITE_MULTIPLICATIVE) {
560: /* update the overlapping (i+1)-block RHS using the current local solution */
561: PetscCall(MatMult(osm->lmats[i + 1], osm->ly, osm->y[i + 1]));
562: PetscCall(VecAXPBY(osm->x[i + 1], -1., 1., osm->y[i + 1]));
563: }
564: }
565: }
566: /* add the local solution to the global solution including the ghost nodes */
567: PetscCall(VecScatterBegin(prolongation, osm->ly, y, ADD_VALUES, reverse));
568: PetscCall(VecScatterEnd(prolongation, osm->ly, y, ADD_VALUES, reverse));
569: PetscFunctionReturn(PETSC_SUCCESS);
570: }
572: /*
573: PCMatWorkGet_ASM - Gets the dense right-hand side and solution matrices for the local subdomain solve.
575: Collective on `pc`
577: Input Parameters:
578: + pc - `PCASM` after `PCSetUp()`, with one local subdomain per process
579: - N - global number of right-hand sides
581: Output Parameters:
582: + Z - subdomain right-hand side matrix, or `NULL` if not needed
583: - W - subdomain solution matrix
585: Level: developer
587: Note:
588: The `PC` owns both matrices. Take a reference to retain either matrix across a change in `N` or a workspace reset.
590: .seealso: `PCMatWorkGet_GASM()`, `PCMatWorkSetUp()`
591: */
592: PetscErrorCode PCMatWorkGet_ASM(PC pc, PetscInt N, Mat *Z, Mat *W)
593: {
594: PC_ASM *osm = (PC_ASM *)pc->data;
596: PetscFunctionBegin;
597: PetscCheck(osm->n_local == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
598: PetscCall(PCMatWorkSetUp(pc, 2, N));
599: PetscCall(PCMatWorkCreate(osm->x[0], N, N, &pc->matwork[0]));
600: if (pc->matwork[1] == NULL) PetscCall(MatDuplicate(pc->matwork[0], MAT_DO_NOT_COPY_VALUES, &pc->matwork[1]));
601: if (Z != NULL) *Z = pc->matwork[0];
602: *W = pc->matwork[1];
603: PetscFunctionReturn(PETSC_SUCCESS);
604: }
606: static PetscErrorCode PCMatApply_ASM_Private(PC pc, Mat X, Mat Y, PetscBool transpose)
607: {
608: PC_ASM *osm = (PC_ASM *)pc->data;
609: Mat Z, W;
610: PetscBool nonzero;
611: VecScatter restriction, prolongation;
612: PetscInt N;
614: PetscFunctionBegin;
615: PetscCheck(osm->type != PC_ASM_WEIGHTED || osm->scaling, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Call PCASMWeightedSetScaling() after PCSetUp() before applying PC_ASM_WEIGHTED");
616: PetscCall(PCASMGetVecScatters_Private(pc, transpose, &restriction, &prolongation));
617: PetscCall(MatGetSize(X, NULL, &N));
618: PetscCall(PCMatWorkGet_ASM(pc, N, &Z, &W));
619: PetscCall(KSPGetInitialGuessNonzero(osm->ksp[0], &nonzero));
620: if (restriction != osm->restriction) PetscCall(MatZeroEntries(Z));
621: PetscCall(MatDenseScatter_Private(restriction, X, Z, INSERT_VALUES, SCATTER_FORWARD));
622: if (nonzero == PETSC_TRUE) PetscCall(MatZeroEntries(W));
623: if (transpose == PETSC_TRUE && osm->type == PC_ASM_WEIGHTED) PetscCall(MatDiagonalScale(Z, osm->scaling[0], NULL));
624: PetscCall(PetscLogEventBegin(transpose == PETSC_TRUE ? PC_ApplyTransposeOnBlocks : PC_ApplyOnBlocks, osm->ksp[0], Z, W, 0));
625: if (transpose == PETSC_TRUE) PetscCall(KSPMatSolveTranspose(osm->ksp[0], Z, W));
626: else PetscCall(KSPMatSolve(osm->ksp[0], Z, W));
627: PetscCall(PetscLogEventEnd(transpose == PETSC_TRUE ? PC_ApplyTransposeOnBlocks : PC_ApplyOnBlocks, osm->ksp[0], Z, W, 0));
628: PetscCall(KSPCheckMatSolve(osm->ksp[0], pc, W));
629: if (transpose == PETSC_FALSE && osm->type == PC_ASM_WEIGHTED) PetscCall(MatDiagonalScale(W, osm->scaling[0], NULL));
630: PetscCall(MatZeroEntries(Y));
631: PetscCall(MatDenseScatter_Private(prolongation, W, Y, ADD_VALUES, SCATTER_REVERSE));
632: PetscFunctionReturn(PETSC_SUCCESS);
633: }
635: static PetscErrorCode PCMatApply_ASM(PC pc, Mat X, Mat Y)
636: {
637: PetscFunctionBegin;
638: PetscCall(PCMatApply_ASM_Private(pc, X, Y, PETSC_FALSE));
639: PetscFunctionReturn(PETSC_SUCCESS);
640: }
642: static PetscErrorCode PCMatApplyTranspose_ASM(PC pc, Mat X, Mat Y)
643: {
644: PetscFunctionBegin;
645: PetscCall(PCMatApply_ASM_Private(pc, X, Y, PETSC_TRUE));
646: PetscFunctionReturn(PETSC_SUCCESS);
647: }
649: static PetscErrorCode PCApplyTranspose_ASM(PC pc, Vec x, Vec y)
650: {
651: PC_ASM *osm = (PC_ASM *)pc->data;
652: PCASMType type = osm->type == PC_ASM_WEIGHTED ? PC_ASM_BASIC : osm->type; /* PC_ASM_WEIGHTED scatters like PC_ASM_BASIC, then applies the PCASMWeightedSetScaling() weights */
653: PetscInt i, n_local_true = osm->n_local_true;
654: ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
655: VecScatter restriction = osm->restriction, prolongation = osm->restriction;
657: PetscFunctionBegin;
658: PetscCheck(osm->n_local_true <= 1 || osm->loctype == PC_COMPOSITE_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
659: PetscCheck(osm->type != PC_ASM_WEIGHTED || osm->scaling, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Call PCASMWeightedSetScaling() after PCSetUp() before applying PC_ASM_WEIGHTED");
660: if (osm->n_local == 1) {
661: PetscCall(PCASMGetVecScatters_Private(pc, PETSC_TRUE, &restriction, &prolongation));
662: if (restriction != osm->restriction) PetscCall(VecZeroEntries(osm->lx));
663: } else {
664: /* reverse the restriction and interpolation modes used by PCApply_ASM() */
665: if (!(type & PC_ASM_INTERPOLATE)) {
666: forward = SCATTER_FORWARD_LOCAL;
667: /* have to zero the work RHS since scatter may leave some slots empty */
668: PetscCall(VecSet(osm->lx, 0.0));
669: }
670: if (!(type & PC_ASM_RESTRICT)) reverse = SCATTER_REVERSE_LOCAL;
671: }
673: /* zero the global and the local solutions */
674: PetscCall(VecSet(y, 0.0));
675: if (osm->n_local > 1) PetscCall(VecSet(osm->ly, 0.0));
677: /* copy the global RHS to local RHS including the ghost nodes */
678: PetscCall(VecScatterBegin(restriction, x, osm->lx, INSERT_VALUES, forward));
679: PetscCall(VecScatterEnd(restriction, x, osm->lx, INSERT_VALUES, forward));
681: /* restrict local RHS to the overlapping 0-block RHS */
682: if (osm->n_local > 1) {
683: PetscCall(VecScatterBegin(osm->lrestriction[0], osm->lx, osm->x[0], INSERT_VALUES, forward));
684: PetscCall(VecScatterEnd(osm->lrestriction[0], osm->lx, osm->x[0], INSERT_VALUES, forward));
685: }
687: /* do the local solves */
688: for (i = 0; i < n_local_true; ++i) {
689: /* solve the overlapping i-block */
690: if (osm->type == PC_ASM_WEIGHTED) PetscCall(VecPointwiseMult(osm->x[i], osm->scaling[i], osm->x[i]));
691: PetscCall(PetscLogEventBegin(PC_ApplyTransposeOnBlocks, osm->ksp[i], osm->x[i], osm->y[i], 0));
692: PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i]));
693: PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
694: PetscCall(PetscLogEventEnd(PC_ApplyTransposeOnBlocks, osm->ksp[i], osm->x[i], osm->y[i], 0));
695: if (osm->n_local == 1) break;
697: if (osm->lprolongation && !(type & PC_ASM_RESTRICT)) { /* interpolate the non-overlapping i-block solution to the local solution */
698: PetscCall(VecScatterBegin(osm->lprolongation[i], osm->y[i], osm->ly, ADD_VALUES, forward));
699: PetscCall(VecScatterEnd(osm->lprolongation[i], osm->y[i], osm->ly, ADD_VALUES, forward));
700: } else { /* interpolate the overlapping i-block solution to the local solution */
701: PetscCall(VecScatterBegin(osm->lrestriction[i], osm->y[i], osm->ly, ADD_VALUES, reverse));
702: PetscCall(VecScatterEnd(osm->lrestriction[i], osm->y[i], osm->ly, ADD_VALUES, reverse));
703: }
705: if (i < n_local_true - 1) {
706: /* Restrict local RHS to the overlapping (i+1)-block RHS */
707: PetscCall(VecScatterBegin(osm->lrestriction[i + 1], osm->lx, osm->x[i + 1], INSERT_VALUES, forward));
708: PetscCall(VecScatterEnd(osm->lrestriction[i + 1], osm->lx, osm->x[i + 1], INSERT_VALUES, forward));
709: }
710: }
711: /* Add the local solution to the global solution including the ghost nodes */
712: PetscCall(VecScatterBegin(prolongation, osm->ly, y, ADD_VALUES, reverse));
713: PetscCall(VecScatterEnd(prolongation, osm->ly, y, ADD_VALUES, reverse));
714: PetscFunctionReturn(PETSC_SUCCESS);
715: }
717: static PetscErrorCode PCReset_ASM(PC pc)
718: {
719: PC_ASM *osm = (PC_ASM *)pc->data;
721: PetscFunctionBegin;
722: if (osm->scaling) {
723: for (PetscInt i = 0; i < osm->n_local_true; i++) PetscCall(VecDestroy(&osm->scaling[i]));
724: PetscCall(PetscFree(osm->scaling));
725: }
726: if (osm->ksp) {
727: for (PetscInt i = 0; i < osm->n_local_true; i++) PetscCall(KSPReset(osm->ksp[i]));
728: }
729: if (osm->pmat) {
730: if (osm->n_local_true > 0) PetscCall(MatDestroySubMatrices(osm->n_local_true, &osm->pmat));
731: }
732: if (osm->lrestriction) {
733: PetscCall(VecScatterDestroy(&osm->restriction));
734: for (PetscInt i = 0; i < osm->n_local_true; i++) {
735: PetscCall(VecScatterDestroy(&osm->lrestriction[i]));
736: if (osm->lprolongation) PetscCall(VecScatterDestroy(&osm->lprolongation[i]));
737: PetscCall(VecDestroy(&osm->x[i]));
738: PetscCall(VecDestroy(&osm->y[i]));
739: }
740: PetscCall(PetscFree(osm->lrestriction));
741: PetscCall(PetscFree(osm->lprolongation));
742: PetscCall(PetscFree(osm->x));
743: PetscCall(PetscFree(osm->y));
744: }
745: PetscCall(PCASMDestroySubdomains(osm->n_local_true, &osm->is, &osm->is_local));
746: PetscCall(ISDestroy(&osm->lis));
747: PetscCall(VecDestroy(&osm->lx));
748: PetscCall(VecDestroy(&osm->ly));
749: PetscCall(MatDestroySubMatrices(osm->n_local_true, &osm->lmats));
750: PetscCall(PetscFree(osm->sub_mat_type));
751: PetscFunctionReturn(PETSC_SUCCESS);
752: }
754: static PetscErrorCode PCDestroy_ASM(PC pc)
755: {
756: PC_ASM *osm = (PC_ASM *)pc->data;
758: PetscFunctionBegin;
759: PetscCall(PCReset_ASM(pc));
760: if (osm->ksp) {
761: for (PetscInt i = 0; i < osm->n_local_true; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
762: PetscCall(PetscFree(osm->ksp));
763: }
764: PetscCall(PetscFree(pc->data));
766: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetLocalSubdomains_C", NULL));
767: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetTotalSubdomains_C", NULL));
768: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetOverlap_C", NULL));
769: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetType_C", NULL));
770: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetType_C", NULL));
771: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMWeightedSetScaling_C", NULL));
772: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMWeightedSetComputeScaling_C", NULL));
773: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetLocalType_C", NULL));
774: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetLocalType_C", NULL));
775: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetSortIndices_C", NULL));
776: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetSubKSP_C", NULL));
777: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetSubMatType_C", NULL));
778: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetSubMatType_C", NULL));
779: PetscFunctionReturn(PETSC_SUCCESS);
780: }
782: static PetscErrorCode PCSetFromOptions_ASM(PC pc, PetscOptionItems PetscOptionsObject)
783: {
784: PC_ASM *osm = (PC_ASM *)pc->data;
785: PetscInt blocks, ovl;
786: PetscBool flg;
787: PCASMType asmtype;
788: PCCompositeType loctype;
789: char sub_mat_type[256];
791: PetscFunctionBegin;
792: PetscOptionsHeadBegin(PetscOptionsObject, "Additive Schwarz options");
793: PetscCall(PetscOptionsBool("-pc_asm_dm_subdomains", "Use DMCreateDomainDecomposition() to define subdomains", "PCASMSetDMSubdomains", osm->dm_subdomains, &osm->dm_subdomains, &flg));
794: PetscCall(PetscOptionsInt("-pc_asm_blocks", "Number of subdomains", "PCASMSetTotalSubdomains", osm->n, &blocks, &flg));
795: if (flg) {
796: PetscCall(PCASMSetTotalSubdomains(pc, blocks, NULL, NULL));
797: osm->dm_subdomains = PETSC_FALSE;
798: }
799: PetscCall(PetscOptionsInt("-pc_asm_local_blocks", "Number of local subdomains", "PCASMSetLocalSubdomains", osm->n_local_true, &blocks, &flg));
800: if (flg) {
801: PetscCall(PCASMSetLocalSubdomains(pc, blocks, NULL, NULL));
802: osm->dm_subdomains = PETSC_FALSE;
803: }
804: PetscCall(PetscOptionsInt("-pc_asm_overlap", "Number of grid points overlap", "PCASMSetOverlap", osm->overlap, &ovl, &flg));
805: if (flg) {
806: PetscCall(PCASMSetOverlap(pc, ovl));
807: osm->dm_subdomains = PETSC_FALSE;
808: }
809: flg = PETSC_FALSE;
810: PetscCall(PetscOptionsEnum("-pc_asm_type", "Type of restriction/extension", "PCASMSetType", PCASMTypes, (PetscEnum)osm->type, (PetscEnum *)&asmtype, &flg));
811: if (flg) PetscCall(PCASMSetType(pc, asmtype));
812: flg = PETSC_FALSE;
813: PetscCall(PetscOptionsEnum("-pc_asm_local_type", "Type of local solver composition", "PCASMSetLocalType", PCCompositeTypes, (PetscEnum)osm->loctype, (PetscEnum *)&loctype, &flg));
814: if (flg) PetscCall(PCASMSetLocalType(pc, loctype));
815: PetscCall(PetscOptionsFList("-pc_asm_sub_mat_type", "Subsolve Matrix Type", "PCASMSetSubMatType", MatList, NULL, sub_mat_type, sizeof(sub_mat_type), &flg));
816: if (flg) PetscCall(PCASMSetSubMatType(pc, sub_mat_type));
817: PetscOptionsHeadEnd();
818: PetscFunctionReturn(PETSC_SUCCESS);
819: }
821: static PetscErrorCode PCASMSetLocalSubdomains_ASM(PC pc, PetscInt n, IS is[], IS is_local[])
822: {
823: PC_ASM *osm = (PC_ASM *)pc->data;
825: PetscFunctionBegin;
826: PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Each process must have 1 or more blocks, n = %" PetscInt_FMT, n);
827: PetscCheck(!pc->setupcalled || (n == osm->n_local_true && !is), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCASMSetLocalSubdomains() should be called before calling PCSetUp().");
829: if (!pc->setupcalled) {
830: if (is) {
831: for (PetscInt i = 0; i < n; i++) PetscCall(PetscObjectReference((PetscObject)is[i]));
832: }
833: if (is_local) {
834: for (PetscInt i = 0; i < n; i++) PetscCall(PetscObjectReference((PetscObject)is_local[i]));
835: }
836: PetscCall(PCASMDestroySubdomains(osm->n_local_true, &osm->is, &osm->is_local));
838: if (osm->ksp && osm->n_local_true != n) {
839: for (PetscInt i = 0; i < osm->n_local_true; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
840: PetscCall(PetscFree(osm->ksp));
841: }
843: osm->n_local_true = n;
844: if (is) {
845: PetscCall(PetscMalloc1(n, &osm->is));
846: for (PetscInt i = 0; i < n; i++) osm->is[i] = is[i];
847: /* Flag indicating that the user has set overlapping subdomains so PCASM should not increase their size. */
848: osm->overlap = -1;
849: }
850: if (is_local) {
851: PetscCall(PetscMalloc1(n, &osm->is_local));
852: for (PetscInt i = 0; i < n; i++) osm->is_local[i] = is_local[i];
853: if (!is) {
854: PetscCall(PetscMalloc1(osm->n_local_true, &osm->is));
855: for (PetscInt i = 0; i < osm->n_local_true; i++) {
856: if (osm->overlap > 0) { /* With positive overlap, osm->is[i] will be modified */
857: PetscCall(ISDuplicate(osm->is_local[i], &osm->is[i]));
858: PetscCall(ISCopy(osm->is_local[i], osm->is[i]));
859: } else {
860: PetscCall(PetscObjectReference((PetscObject)osm->is_local[i]));
861: osm->is[i] = osm->is_local[i];
862: }
863: }
864: }
865: }
866: }
867: PetscFunctionReturn(PETSC_SUCCESS);
868: }
870: static PetscErrorCode PCASMSetTotalSubdomains_ASM(PC pc, PetscInt N, IS *is, IS *is_local)
871: {
872: PC_ASM *osm = (PC_ASM *)pc->data;
873: PetscMPIInt rank, size;
874: PetscInt n;
876: PetscFunctionBegin;
877: PetscCheck(N >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Number of total blocks must be > 0, N = %" PetscInt_FMT, N);
878: PetscCheck(!is && !is_local, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Use PCASMSetLocalSubdomains() to set specific index sets, they cannot be set globally yet.");
880: /*
881: Split the subdomains equally among all processors
882: */
883: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
884: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
885: n = N / size + ((N % size) > rank);
886: PetscCheck(n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Process %d must have at least one block: total processors %d total blocks %" PetscInt_FMT, rank, size, N);
887: PetscCheck(!pc->setupcalled || n == osm->n_local_true, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PCASMSetTotalSubdomains() should be called before PCSetUp().");
888: if (!pc->setupcalled) {
889: PetscCall(PCASMDestroySubdomains(osm->n_local_true, &osm->is, &osm->is_local));
891: osm->n_local_true = n;
892: }
893: PetscFunctionReturn(PETSC_SUCCESS);
894: }
896: static PetscErrorCode PCASMSetOverlap_ASM(PC pc, PetscInt ovl)
897: {
898: PC_ASM *osm = (PC_ASM *)pc->data;
900: PetscFunctionBegin;
901: PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested");
902: PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCASMSetOverlap() should be called before PCSetUp().");
903: if (!pc->setupcalled) osm->overlap = ovl;
904: PetscFunctionReturn(PETSC_SUCCESS);
905: }
907: static PetscErrorCode PCASMSetType_ASM(PC pc, PCASMType type)
908: {
909: PC_ASM *osm = (PC_ASM *)pc->data;
911: PetscFunctionBegin;
912: PetscCheck(type != PC_ASM_WEIGHTED || osm->loctype == PC_COMPOSITE_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PC_ASM_WEIGHTED requires additive local composition");
913: osm->type = type;
914: osm->type_set = PETSC_TRUE;
915: PetscFunctionReturn(PETSC_SUCCESS);
916: }
918: static PetscErrorCode PCASMGetType_ASM(PC pc, PCASMType *type)
919: {
920: PC_ASM *osm = (PC_ASM *)pc->data;
922: PetscFunctionBegin;
923: *type = osm->type;
924: PetscFunctionReturn(PETSC_SUCCESS);
925: }
927: static PetscErrorCode PCASMWeightedSetComputeScaling_ASM(PC pc, PCASMWeightedComputeScalingFn *fn, PetscCtx ctx)
928: {
929: PC_ASM *osm = (PC_ASM *)pc->data;
931: PetscFunctionBegin;
932: osm->computescaling = fn;
933: osm->computescalingctx = ctx;
934: PetscFunctionReturn(PETSC_SUCCESS);
935: }
937: static PetscErrorCode PCASMWeightedSetScaling_ASM(PC pc, PetscInt n, Vec scaling[])
938: {
939: PC_ASM *osm = (PC_ASM *)pc->data;
940: Vec *newscaling;
941: VecType type;
942: PetscInt m, nvec;
943: PetscMPIInt size;
944: PetscBool match;
946: PetscFunctionBegin;
947: PetscCheck(osm->x, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Call PCSetUp() before PCASMWeightedSetScaling() so that the subdomain sizes and ordering are final");
948: PetscCheck(n == osm->n_local_true, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of scaling vectors %" PetscInt_FMT " must match number of local subdomains %" PetscInt_FMT, n, osm->n_local_true);
949: for (PetscInt i = 0; i < n; i++) {
950: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)scaling[i]), &size));
951: PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Scaling vector %" PetscInt_FMT " must have a single-process communicator; create it with MatCreateVecs() from PCASMGetLocalSubmatrices()", i);
952: PetscCall(VecGetSize(scaling[i], &nvec));
953: PetscCall(VecGetSize(osm->x[i], &m));
954: PetscCheck(nvec == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Scaling vector %" PetscInt_FMT " has size %" PetscInt_FMT ", expected %" PetscInt_FMT "; create it with MatCreateVecs() from PCASMGetLocalSubmatrices()", i, nvec, m);
955: PetscCall(VecGetType(osm->x[i], &type));
956: PetscCall(PetscObjectTypeCompare((PetscObject)scaling[i], type, &match));
957: PetscCheck(match, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Scaling vector %" PetscInt_FMT " must have local solver vector type %s; create it with MatCreateVecs() from PCASMGetLocalSubmatrices()", i, type);
958: }
959: PetscCall(PetscMalloc1(n, &newscaling));
960: for (PetscInt i = 0; i < n; i++) {
961: PetscCall(PetscObjectReference((PetscObject)scaling[i]));
962: newscaling[i] = scaling[i];
963: }
964: if (osm->scaling) {
965: for (PetscInt i = 0; i < n; i++) PetscCall(VecDestroy(&osm->scaling[i]));
966: PetscCall(PetscFree(osm->scaling));
967: }
968: osm->scaling = newscaling;
969: PetscFunctionReturn(PETSC_SUCCESS);
970: }
972: static PetscErrorCode PCASMSetLocalType_ASM(PC pc, PCCompositeType type)
973: {
974: PC_ASM *osm = (PC_ASM *)pc->data;
976: PetscFunctionBegin;
977: PetscCheck(type == PC_COMPOSITE_ADDITIVE || type == PC_COMPOSITE_MULTIPLICATIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Only supports additive or multiplicative as the local type");
978: PetscCheck(osm->type != PC_ASM_WEIGHTED || type == PC_COMPOSITE_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PC_ASM_WEIGHTED requires additive local composition");
979: osm->loctype = type;
980: PetscFunctionReturn(PETSC_SUCCESS);
981: }
983: static PetscErrorCode PCASMGetLocalType_ASM(PC pc, PCCompositeType *type)
984: {
985: PC_ASM *osm = (PC_ASM *)pc->data;
987: PetscFunctionBegin;
988: *type = osm->loctype;
989: PetscFunctionReturn(PETSC_SUCCESS);
990: }
992: static PetscErrorCode PCASMSetSortIndices_ASM(PC pc, PetscBool doSort)
993: {
994: PC_ASM *osm = (PC_ASM *)pc->data;
996: PetscFunctionBegin;
997: osm->sort_indices = doSort;
998: PetscFunctionReturn(PETSC_SUCCESS);
999: }
1001: static PetscErrorCode PCASMGetSubKSP_ASM(PC pc, PetscInt *n_local, PetscInt *first_local, KSP **ksp)
1002: {
1003: PC_ASM *osm = (PC_ASM *)pc->data;
1005: PetscFunctionBegin;
1006: PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Need to call PCSetUp() on PC (or KSPSetUp() on the outer KSP object) before calling here");
1008: if (n_local) *n_local = osm->n_local_true;
1009: if (first_local) {
1010: PetscCallMPI(MPI_Scan(&osm->n_local_true, first_local, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
1011: *first_local -= osm->n_local_true;
1012: }
1013: if (ksp) *ksp = osm->ksp;
1014: PetscFunctionReturn(PETSC_SUCCESS);
1015: }
1017: static PetscErrorCode PCASMGetSubMatType_ASM(PC pc, MatType *sub_mat_type)
1018: {
1019: PC_ASM *osm = (PC_ASM *)pc->data;
1021: PetscFunctionBegin;
1023: PetscAssertPointer(sub_mat_type, 2);
1024: *sub_mat_type = osm->sub_mat_type;
1025: PetscFunctionReturn(PETSC_SUCCESS);
1026: }
1028: static PetscErrorCode PCASMSetSubMatType_ASM(PC pc, MatType sub_mat_type)
1029: {
1030: PC_ASM *osm = (PC_ASM *)pc->data;
1032: PetscFunctionBegin;
1034: PetscCall(PetscFree(osm->sub_mat_type));
1035: PetscCall(PetscStrallocpy(sub_mat_type, (char **)&osm->sub_mat_type));
1036: PetscFunctionReturn(PETSC_SUCCESS);
1037: }
1039: /*@
1040: PCASMSetLocalSubdomains - Sets the local subdomains (for this processor only) for the additive Schwarz preconditioner `PCASM`.
1042: Collective
1044: Input Parameters:
1045: + pc - the preconditioner context
1046: . n - the number of subdomains for this processor (default value = 1)
1047: . is - the index set that defines the subdomains for this processor (or `NULL` for PETSc to determine subdomains)
1048: the values of the `is` array are copied so you can free the array (not the `IS` in the array) after this call
1049: - is_local - the index sets that define the local part of the subdomains for this processor, not used unless `PCASMType` is `PC_ASM_RESTRICT`
1050: (or `NULL` to not provide these). The values of the `is_local` array are copied so you can free the array
1051: (not the `IS` in the array) after this call
1053: Options Database Key:
1054: . -pc_asm_local_blocks blks - Sets number of local blocks
1056: Level: advanced
1058: Notes:
1059: The `IS` numbering is in the parallel, global numbering of the vector for both `is` and `is_local`
1061: By default the `PCASM` preconditioner uses 1 block per processor.
1063: Use `PCASMSetTotalSubdomains()` to set the subdomains for all processors.
1065: If `is_local` is provided and `PCASMType` is `PC_ASM_RESTRICT` then the solution only over the `is_local` region is interpolated
1066: back to form the global solution (this is the standard restricted additive Schwarz method, RASM)
1068: If `is_local` is provided and `PCASMType` is `PC_ASM_INTERPOLATE` or `PC_ASM_NONE` then an error is generated since there is
1069: no code to handle that case.
1071: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`, `PCASMGetSubKSP()`,
1072: `PCASMCreateSubdomains2D()`, `PCASMGetLocalSubdomains()`, `PCASMType`, `PCASMSetType()`, `PCGASM`
1073: @*/
1074: PetscErrorCode PCASMSetLocalSubdomains(PC pc, PetscInt n, IS is[], IS is_local[])
1075: {
1076: PetscFunctionBegin;
1078: PetscTryMethod(pc, "PCASMSetLocalSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, is, is_local));
1079: PetscFunctionReturn(PETSC_SUCCESS);
1080: }
1082: /*@
1083: PCASMSetTotalSubdomains - Sets the subdomains for all processors for the
1084: additive Schwarz preconditioner, `PCASM`.
1086: Collective, all MPI processes must pass in the same array of `IS`
1088: Input Parameters:
1089: + pc - the preconditioner context
1090: . N - the number of subdomains for all processors
1091: . is - the index sets that define the subdomains for all processors (or `NULL` to ask PETSc to determine the subdomains)
1092: the values of the `is` array are copied so you can free the array (not the `IS` in the array) after this call
1093: - is_local - the index sets that define the local part of the subdomains for this processor (or `NULL` to not provide this information)
1094: The values of the `is_local` array are copied so you can free the array (not the `IS` in the array) after this call
1096: Options Database Key:
1097: . -pc_asm_blocks blks - Sets total blocks
1099: Level: advanced
1101: Notes:
1102: Currently you cannot use this to set the actual subdomains with the argument `is` or `is_local`.
1104: By default the `PCASM` preconditioner uses 1 block per processor.
1106: These index sets cannot be destroyed until after completion of the
1107: linear solves for which the `PCASM` preconditioner is being used.
1109: Use `PCASMSetLocalSubdomains()` to set local subdomains.
1111: The `IS` numbering is in the parallel, global numbering of the vector for both is and is_local
1113: .seealso: [](ch_ksp), `PCASM`, `PCASMSetLocalSubdomains()`, `PCASMSetOverlap()`, `PCASMGetSubKSP()`,
1114: `PCASMCreateSubdomains2D()`, `PCGASM`
1115: @*/
1116: PetscErrorCode PCASMSetTotalSubdomains(PC pc, PetscInt N, IS is[], IS is_local[])
1117: {
1118: PetscFunctionBegin;
1120: PetscTryMethod(pc, "PCASMSetTotalSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, N, is, is_local));
1121: PetscFunctionReturn(PETSC_SUCCESS);
1122: }
1124: /*@
1125: PCASMSetOverlap - Sets the overlap between a pair of subdomains for the
1126: additive Schwarz preconditioner, `PCASM`.
1128: Logically Collective
1130: Input Parameters:
1131: + pc - the preconditioner context
1132: - ovl - the amount of overlap between subdomains (ovl >= 0, default value = 1)
1134: Options Database Key:
1135: . -pc_asm_overlap ovl - Sets overlap
1137: Level: intermediate
1139: Notes:
1140: By default the `PCASM` preconditioner uses 1 block per processor. To use
1141: multiple blocks per perocessor, see `PCASMSetTotalSubdomains()` and
1142: `PCASMSetLocalSubdomains()` (and the option -pc_asm_blocks <blks>).
1144: The overlap defaults to 1, so if one desires that no additional
1145: overlap be computed beyond what may have been set with a call to
1146: `PCASMSetTotalSubdomains()` or `PCASMSetLocalSubdomains()`, then ovl
1147: must be set to be 0. In particular, if one does not explicitly set
1148: the subdomains an application code, then all overlap would be computed
1149: internally by PETSc, and using an overlap of 0 would result in an `PCASM`
1150: variant that is equivalent to the block Jacobi preconditioner.
1152: The default algorithm used by PETSc to increase overlap is fast, but not scalable,
1153: use the option -mat_increase_overlap_scalable when the problem and number of processes is large.
1155: One can define initial index sets with any overlap via
1156: `PCASMSetLocalSubdomains()`; the routine
1157: `PCASMSetOverlap()` merely allows PETSc to extend that overlap further
1158: if desired.
1160: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetLocalSubdomains()`, `PCASMGetSubKSP()`,
1161: `PCASMCreateSubdomains2D()`, `PCASMGetLocalSubdomains()`, `MatIncreaseOverlap()`, `PCGASM`
1162: @*/
1163: PetscErrorCode PCASMSetOverlap(PC pc, PetscInt ovl)
1164: {
1165: PetscFunctionBegin;
1168: PetscTryMethod(pc, "PCASMSetOverlap_C", (PC, PetscInt), (pc, ovl));
1169: PetscFunctionReturn(PETSC_SUCCESS);
1170: }
1172: /*@
1173: PCASMSetType - Sets the type of restriction and interpolation used
1174: for local problems in the additive Schwarz method, `PCASM`.
1176: Logically Collective
1178: Input Parameters:
1179: + pc - the preconditioner context
1180: - type - variant of `PCASM`, one of
1181: .vb
1182: PC_ASM_NONE - local processor restriction and interpolation
1183: PC_ASM_RESTRICT - full restriction, local processor interpolation (default)
1184: PC_ASM_INTERPOLATE - full interpolation, local processor restriction
1185: PC_ASM_BASIC - full interpolation and restriction
1186: PC_ASM_WEIGHTED - full restriction and interpolation with user-provided diagonal scaling
1187: .ve
1189: Options Database Key:
1190: . -pc_asm_type (none|restrict|interpolate|basic|weighted) - Sets `PCASMType`
1192: Level: intermediate
1194: Note:
1195: if the is_local arguments are passed to `PCASMSetLocalSubdomains()` then they are used when `PC_ASM_RESTRICT` has been selected
1196: to limit the local processor interpolation. `PC_ASM_WEIGHTED` ignores these inner index sets and
1197: uses the weights supplied with `PCASMWeightedSetScaling()` instead. Weighted ASM requires additive local composition.
1199: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMGetSubKSP()`,
1200: `PCASMCreateSubdomains2D()`, `PCASMType`, `PCASMWeightedSetScaling()`, `PCASMSetLocalType()`, `PCASMGetLocalType()`, `PCGASM`
1201: @*/
1202: PetscErrorCode PCASMSetType(PC pc, PCASMType type)
1203: {
1204: PetscFunctionBegin;
1207: PetscTryMethod(pc, "PCASMSetType_C", (PC, PCASMType), (pc, type));
1208: PetscFunctionReturn(PETSC_SUCCESS);
1209: }
1211: /*@
1212: PCASMWeightedSetComputeScaling - Sets a callback to compute `PC_ASM_WEIGHTED` scaling during `PCSetUp()`.
1214: Logically Collective
1216: Input Parameters:
1217: + pc - the `PCASM` preconditioner
1218: . fn - function to fill each local scaling vector, or `NULL` to disable the callback
1219: - ctx - function context passed to `fn`
1221: Level: intermediate
1223: Notes:
1224: Register before setup and select `PC_ASM_WEIGHTED`.
1226: Whenever `PCSetUp()` rebuilds weighted ASM,
1227: `fn` is called once per local subdomain, after overlap expansion and index sorting, with a vector
1228: of the correct size and type.
1230: No explicit setup or vector allocation is needed by the caller.
1232: The callback overwrites any existing weights, including those supplied directly by `PCASMWeightedSetScaling()`.
1234: Registration does not trigger setup. Passing `NULL` leaves the current weights in place.
1236: The callback and context survive `PCReset()`. The caller owns `ctx` and must keep it valid
1237: until the callback is replaced or disabled, or the preconditioner is destroyed.
1239: Fortran Note:
1240: `fn` is a subroutine with arguments `(pc, local, scaling, ctx, ierr)`. Pass `PETSC_NULL_FUNCTION` to disable the callback.
1242: .seealso: [](ch_ksp), `PCASM`, `PCASMWeightedComputeScalingFn`, `PCASMWeightedSetScaling()`, `PCASMWeightedGetScaling()`, `PCASMSetType()`
1243: @*/
1244: PetscErrorCode PCASMWeightedSetComputeScaling(PC pc, PCASMWeightedComputeScalingFn *fn, PetscCtx ctx)
1245: {
1246: PetscFunctionBegin;
1248: PetscTryMethod(pc, "PCASMWeightedSetComputeScaling_C", (PC, PCASMWeightedComputeScalingFn *, PetscCtx), (pc, fn, ctx));
1249: PetscFunctionReturn(PETSC_SUCCESS);
1250: }
1252: /*@
1253: PCASMWeightedSetScaling - Sets the diagonal weights for the overlapping local corrections in weighted additive Schwarz.
1255: Logically Collective
1257: Input Parameters:
1258: + pc - the `PCASM` preconditioner
1259: . n - the number of local subdomains
1260: - scaling - one local scaling `Vec` per overlapping subdomain
1262: Level: intermediate
1264: Notes:
1265: Call `PCSetUp()` before this routine, then use `PCASMGetLocalSubdomains()` to obtain the final
1266: overlapping index sets. Each vector must have a single-process communicator and the same length
1267: and ordering as its corresponding overlapping index set, after overlap expansion and sorting.
1268: Its vector type must match the local solver vectors. Compatible vectors can be created with
1269: `MatCreateVecs()` from the corresponding matrix returned by `PCASMGetLocalSubmatrices()`.
1270: These are local subdomain vectors, not vectors in the parallel global layout.
1271: Select `PC_ASM_WEIGHTED` with `PCASMSetType()` or `-pc_asm_type weighted` to use the weights.
1272: The weights are ignored by the other ASM types.
1274: With restriction operators $R_i$, local solvers $A_i^{-1}$, and $D_i = \text{diag}(scaling[i])$, the action is
1275: $B = \sum_i R_i^T D_i A_i^{-1} R_i$.
1276: PETSc uses the supplied weights as-is, without checking whether they are real, non-negative,
1277: or satisfy $\sum_i R_i^T D_i R_i = I$.
1278: The PC increments the reference count of the vectors but does not copy them.
1279: `PCReset()` discards the weights along with the subdomains.
1280: Alternatively, use `PCASMWeightedSetComputeScaling()` to fill internally-created vectors during `PCSetUp()`.
1282: Example Usage:
1283: .vb
1284: KSPGetPC(ksp, &pc);
1285: PCASMSetType(pc, PC_ASM_WEIGHTED);
1286: PCSetUp(pc);
1287: PCASMGetLocalSubmatrices(pc, &n, &submat);
1288: PCASMGetLocalSubdomains(pc, NULL, &is, NULL);
1289: for (i = 0; i < n; i++) {
1290: MatCreateVecs(submat[i], &scaling[i], NULL); // a Vec of the right size and type
1291: // fill scaling[i] in the local ordering of is[i]
1292: }
1293: PCASMWeightedSetScaling(pc, n, scaling);
1294: .ve
1296: .seealso: [](ch_ksp), `PCASM`, `PCASMType`, `PCASMSetType()`, `PCASMWeightedGetScaling()`, `PCASMGetLocalSubdomains()`, `PCASMGetLocalSubmatrices()`, `PCASMSetLocalSubdomains()`, `PCASMSetLocalType()`
1297: @*/
1298: PetscErrorCode PCASMWeightedSetScaling(PC pc, PetscInt n, Vec scaling[])
1299: {
1300: PetscFunctionBegin;
1302: PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of scaling vectors must be nonnegative");
1303: if (n) PetscAssertPointer(scaling, 3);
1305: PetscTryMethod(pc, "PCASMWeightedSetScaling_C", (PC, PetscInt, Vec[]), (pc, n, scaling));
1306: PetscFunctionReturn(PETSC_SUCCESS);
1307: }
1309: /*@
1310: PCASMWeightedGetScaling - Gets the diagonal weights supplied with `PCASMWeightedSetScaling()` or computed by the function provided with `PCASMWeightedSetComputeScaling()`.
1312: Not Collective
1314: Input Parameter:
1315: . pc - the `PCASM` preconditioner
1317: Output Parameters:
1318: + n - if requested, the number of local subdomains for this processor, or zero if no weights have been supplied
1319: - scaling - if requested, the local scaling `Vec`, or `NULL` if none have been supplied
1321: Level: intermediate
1323: Note:
1324: The returned array and its vectors are owned by `pc`; do not free or destroy them. They
1325: are released by `PCReset()` and `PCDestroy()`, and replaced by a further call to
1326: `PCASMWeightedSetScaling()`.
1328: Fortran Note:
1329: Declare `scaling` as `Vec, pointer :: scaling(:)`. It is always returned and is disassociated when no weights
1330: have been supplied; `n` may be `PETSC_NULL_INTEGER`. There is no restore routine, and the pointer must not be
1331: used after `PCReset()`, `PCDestroy()`, or a further call to `PCASMWeightedSetScaling()`.
1333: .seealso: [](ch_ksp), `PCASM`, `PCASMType`, `PCASMWeightedSetScaling()`, `PCASMSetType()`, `PCASMGetLocalSubdomains()`
1334: @*/
1335: PetscErrorCode PCASMWeightedGetScaling(PC pc, PetscInt *n, Vec *scaling[])
1336: {
1337: PC_ASM *osm = (PC_ASM *)pc->data;
1338: PetscBool match;
1340: PetscFunctionBegin;
1342: if (n) PetscAssertPointer(n, 2);
1343: if (scaling) PetscAssertPointer(scaling, 3);
1344: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &match));
1345: PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "PC is not a PCASM");
1346: if (n) *n = osm->scaling ? osm->n_local_true : 0;
1347: if (scaling) *scaling = osm->scaling;
1348: PetscFunctionReturn(PETSC_SUCCESS);
1349: }
1351: /*@
1352: PCASMGetType - Gets the type of restriction and interpolation used
1353: for local problems in the additive Schwarz method, `PCASM`.
1355: Logically Collective
1357: Input Parameter:
1358: . pc - the preconditioner context
1360: Output Parameter:
1361: . type - variant of `PCASM`, one of
1362: .vb
1363: PC_ASM_NONE - local processor restriction and interpolation
1364: PC_ASM_RESTRICT - full restriction, local processor interpolation
1365: PC_ASM_INTERPOLATE - full interpolation, local processor restriction
1366: PC_ASM_BASIC - full interpolation and restriction
1367: PC_ASM_WEIGHTED - full restriction and interpolation with user-provided diagonal scaling
1368: .ve
1370: Options Database Key:
1371: . -pc_asm_type (none|restrict|interpolate|basic|weighted) - Sets `PCASM` type
1373: Level: intermediate
1375: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMGetSubKSP()`, `PCGASM`,
1376: `PCASMCreateSubdomains2D()`, `PCASMType`, `PCASMSetType()`, `PCASMSetLocalType()`, `PCASMGetLocalType()`
1377: @*/
1378: PetscErrorCode PCASMGetType(PC pc, PCASMType *type)
1379: {
1380: PetscFunctionBegin;
1382: PetscUseMethod(pc, "PCASMGetType_C", (PC, PCASMType *), (pc, type));
1383: PetscFunctionReturn(PETSC_SUCCESS);
1384: }
1386: /*@
1387: PCASMSetLocalType - Sets the type of composition used for local problems in the additive Schwarz method, `PCASM`.
1389: Logically Collective
1391: Input Parameters:
1392: + pc - the preconditioner context
1393: - type - type of composition, one of
1394: .vb
1395: PC_COMPOSITE_ADDITIVE - local additive combination
1396: PC_COMPOSITE_MULTIPLICATIVE - local multiplicative combination
1397: .ve
1399: Options Database Key:
1400: . -pc_asm_local_type [additive,multiplicative] - Sets local solver composition type
1402: Level: intermediate
1404: .seealso: [](ch_ksp), `PCASM`, `PCASMSetType()`, `PCASMGetType()`, `PCASMGetLocalType()`, `PCASMType`, `PCCompositeType`
1405: @*/
1406: PetscErrorCode PCASMSetLocalType(PC pc, PCCompositeType type)
1407: {
1408: PetscFunctionBegin;
1411: PetscTryMethod(pc, "PCASMSetLocalType_C", (PC, PCCompositeType), (pc, type));
1412: PetscFunctionReturn(PETSC_SUCCESS);
1413: }
1415: /*@
1416: PCASMGetLocalType - Gets the type of composition used for local problems in the additive Schwarz method, `PCASM`.
1418: Logically Collective
1420: Input Parameter:
1421: . pc - the preconditioner context
1423: Output Parameter:
1424: . type - type of composition, one of
1425: .vb
1426: PC_COMPOSITE_ADDITIVE - local additive combination
1427: PC_COMPOSITE_MULTIPLICATIVE - local multiplicative combination
1428: .ve
1430: Options Database Key:
1431: . -pc_asm_local_type [additive,multiplicative] - Sets local solver composition type
1433: Level: intermediate
1435: .seealso: [](ch_ksp), `PCASM`, `PCASMSetType()`, `PCASMGetType()`, `PCASMSetLocalType()`, `PCASMType`, `PCCompositeType`
1436: @*/
1437: PetscErrorCode PCASMGetLocalType(PC pc, PCCompositeType *type)
1438: {
1439: PetscFunctionBegin;
1441: PetscAssertPointer(type, 2);
1442: PetscUseMethod(pc, "PCASMGetLocalType_C", (PC, PCCompositeType *), (pc, type));
1443: PetscFunctionReturn(PETSC_SUCCESS);
1444: }
1446: /*@
1447: PCASMSetSortIndices - Determines whether subdomain indices are sorted.
1449: Logically Collective
1451: Input Parameters:
1452: + pc - the preconditioner context
1453: - doSort - sort the subdomain indices
1455: Level: intermediate
1457: .seealso: [](ch_ksp), `PCASM`, `PCASMSetLocalSubdomains()`, `PCASMSetTotalSubdomains()`, `PCASMGetSubKSP()`,
1458: `PCASMCreateSubdomains2D()`
1459: @*/
1460: PetscErrorCode PCASMSetSortIndices(PC pc, PetscBool doSort)
1461: {
1462: PetscFunctionBegin;
1465: PetscTryMethod(pc, "PCASMSetSortIndices_C", (PC, PetscBool), (pc, doSort));
1466: PetscFunctionReturn(PETSC_SUCCESS);
1467: }
1469: /*@
1470: PCASMGetSubKSP - Gets the local `KSP` contexts for all blocks on
1471: this processor.
1473: Collective iff first_local is requested
1475: Input Parameter:
1476: . pc - the preconditioner context
1478: Output Parameters:
1479: + n_local - the number of blocks on this processor or `NULL`
1480: . first_local - the global number of the first block on this processor or `NULL`, all processors must request or all must pass `NULL`
1481: - ksp - the array of `KSP` contexts
1483: Level: advanced
1485: Notes:
1486: After `PCASMGetSubKSP()` the array of `KSP`s is not to be freed.
1488: You must call `KSPSetUp()` before calling `PCASMGetSubKSP()`.
1490: Fortran Note:
1491: Call `PCASMRestoreSubKSP()` when access to the array of `KSP` is no longer needed. Pass `PETSC_NULL_KSP_POINTER` for `ksp` if not needed.
1493: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`,
1494: `PCASMCreateSubdomains2D()`
1495: @*/
1496: PetscErrorCode PCASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[])
1497: {
1498: PetscFunctionBegin;
1500: PetscUseMethod(pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
1501: PetscFunctionReturn(PETSC_SUCCESS);
1502: }
1504: /*MC
1505: PCASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
1506: its own `KSP` object, {cite}`dryja1987additive` and {cite}`1sbg`
1508: Options Database Keys:
1509: + -pc_asm_blocks blks - Sets total blocks. Defaults to one block per MPI process.
1510: . -pc_asm_overlap ovl - Sets overlap
1511: . -pc_asm_type (none|restrict|interpolate|basic|weighted) - Sets `PCASMType`, default is restrict. See `PCASMSetType()`
1512: . -pc_asm_dm_subdomains (true|false) - use subdomains defined by the `DM` with `DMCreateDomainDecomposition()`
1513: - -pc_asm_local_type (additive|multiplicative) - Sets `PCCompositeType`, default is additive. See `PCASMSetLocalType()`
1515: Level: beginner
1517: Notes:
1518: If you run with, for example, 3 blocks on 1 processor or 3 blocks on 3 processors you
1519: will get a different convergence rate due to the default option of `-pc_asm_type restrict`. Use
1520: `-pc_asm_type basic` to get the same convergence behavior
1522: Each processor can have one or more blocks, but a block cannot be shared by more
1523: than one processor. Use `PCGASM` for subdomains shared by multiple processes.
1525: To set options on the solvers for each block append `-sub_` to all the `KSP`, and `PC`
1526: options database keys. For example, `-sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly`
1528: To set the options on the solvers separate for each block call `PCASMGetSubKSP()`
1529: and set the options directly on the resulting `KSP` object (you can access its `PC` with `KSPGetPC()`)
1531: If the `PC` has an associated `DM`, then, by default, `DMCreateDomainDecomposition()` is used to create the subdomains
1533: Use `PCASMWeightedSetScaling()` with `PC_ASM_WEIGHTED` to supply a diagonal partition of unity on the overlapping subdomains.
1535: .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASMType`, `PCCompositeType`,
1536: `PCBJACOBI`, `PCASMGetSubKSP()`, `PCASMSetLocalSubdomains()`, `PCASMGetType()`, `PCASMSetLocalType()`, `PCASMGetLocalType()`,
1537: `PCASMSetTotalSubdomains()`, `PCSetModifySubMatrices()`, `PCASMSetOverlap()`, `PCASMSetType()`, `PCASMWeightedSetScaling()`
1538: M*/
1540: PETSC_EXTERN PetscErrorCode PCCreate_ASM(PC pc)
1541: {
1542: PC_ASM *osm;
1544: PetscFunctionBegin;
1545: PetscCall(PetscNew(&osm));
1547: osm->n = PETSC_DECIDE;
1548: osm->n_local = 0;
1549: osm->n_local_true = PETSC_DECIDE;
1550: osm->overlap = 1;
1551: osm->ksp = NULL;
1552: osm->restriction = NULL;
1553: osm->lprolongation = NULL;
1554: osm->lrestriction = NULL;
1555: osm->x = NULL;
1556: osm->y = NULL;
1557: osm->scaling = NULL;
1558: osm->is = NULL;
1559: osm->is_local = NULL;
1560: osm->mat = NULL;
1561: osm->pmat = NULL;
1562: osm->type = PC_ASM_RESTRICT;
1563: osm->loctype = PC_COMPOSITE_ADDITIVE;
1564: osm->sort_indices = PETSC_TRUE;
1565: osm->dm_subdomains = PETSC_FALSE;
1566: osm->sub_mat_type = NULL;
1568: pc->data = (void *)osm;
1569: pc->ops->apply = PCApply_ASM;
1570: pc->ops->matapply = PCMatApply_ASM;
1571: pc->ops->applytranspose = PCApplyTranspose_ASM;
1572: pc->ops->matapplytranspose = PCMatApplyTranspose_ASM;
1573: pc->ops->setup = PCSetUp_ASM;
1574: pc->ops->reset = PCReset_ASM;
1575: pc->ops->destroy = PCDestroy_ASM;
1576: pc->ops->setfromoptions = PCSetFromOptions_ASM;
1577: pc->ops->setuponblocks = PCSetUpOnBlocks_ASM;
1578: pc->ops->view = PCView_ASM;
1580: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetLocalSubdomains_C", PCASMSetLocalSubdomains_ASM));
1581: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetTotalSubdomains_C", PCASMSetTotalSubdomains_ASM));
1582: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetOverlap_C", PCASMSetOverlap_ASM));
1583: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetType_C", PCASMSetType_ASM));
1584: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetType_C", PCASMGetType_ASM));
1585: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMWeightedSetScaling_C", PCASMWeightedSetScaling_ASM));
1586: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMWeightedSetComputeScaling_C", PCASMWeightedSetComputeScaling_ASM));
1587: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetLocalType_C", PCASMSetLocalType_ASM));
1588: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetLocalType_C", PCASMGetLocalType_ASM));
1589: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetSortIndices_C", PCASMSetSortIndices_ASM));
1590: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetSubKSP_C", PCASMGetSubKSP_ASM));
1591: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMGetSubMatType_C", PCASMGetSubMatType_ASM));
1592: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCASMSetSubMatType_C", PCASMSetSubMatType_ASM));
1593: PetscFunctionReturn(PETSC_SUCCESS);
1594: }
1596: /*@
1597: PCASMCreateSubdomains - Creates the index sets for the overlapping Schwarz
1598: preconditioner, `PCASM`, for any problem on a general grid.
1600: Collective
1602: Input Parameters:
1603: + A - The global matrix operator
1604: - n - the number of local blocks
1606: Output Parameter:
1607: . outis - the array of index sets defining the subdomains
1609: Level: advanced
1611: Note:
1612: This generates nonoverlapping subdomains; the `PCASM` will generate the overlap
1613: from these if you use `PCASMSetLocalSubdomains()`
1615: Fortran Note:
1616: `outis` cannot be `PETSC_NULL_IS_POINTER`. Destroy the returned array with `PCASMDestroySubdomains()`,
1617: passing `PETSC_NULL_IS_POINTER` for `is_local` because no local index sets are created.
1619: .seealso: [](ch_ksp), `PCASM`, `PCASMSetLocalSubdomains()`, `PCASMDestroySubdomains()`
1620: @*/
1621: PetscErrorCode PCASMCreateSubdomains(Mat A, PetscInt n, IS *outis[])
1622: {
1623: MatPartitioning mpart;
1624: const char *prefix;
1625: PetscInt i, j, rstart, rend, bs;
1626: PetscBool hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE;
1627: Mat Ad = NULL, adj;
1628: IS ispart, isnumb, *is;
1630: PetscFunctionBegin;
1632: PetscAssertPointer(outis, 3);
1633: PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local blocks must be > 0, n = %" PetscInt_FMT, n);
1635: /* Get prefix, row distribution, and block size */
1636: PetscCall(MatGetOptionsPrefix(A, &prefix));
1637: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
1638: PetscCall(MatGetBlockSize(A, &bs));
1639: 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);
1641: /* Get diagonal block from matrix if possible */
1642: PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop));
1643: if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad));
1644: if (Ad) {
1645: PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij));
1646: if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij));
1647: }
1648: if (Ad && n > 1) {
1649: PetscBool match, done;
1650: /* Try to setup a good matrix partitioning if available */
1651: PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart));
1652: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
1653: PetscCall(MatPartitioningSetFromOptions(mpart));
1654: PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match));
1655: if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match));
1656: if (!match) { /* assume a "good" partitioner is available */
1657: PetscInt na;
1658: const PetscInt *ia, *ja;
1659: PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1660: if (done) {
1661: /* Build adjacency matrix by hand. Unfortunately a call to
1662: MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1663: remove the block-aij structure and we cannot expect
1664: MatPartitioning to split vertices as we need */
1665: PetscInt i, j, len, nnz, cnt, *iia = NULL, *jja = NULL;
1666: const PetscInt *row;
1667: nnz = 0;
1668: for (i = 0; i < na; i++) { /* count number of nonzeros */
1669: len = ia[i + 1] - ia[i];
1670: row = ja + ia[i];
1671: for (j = 0; j < len; j++) {
1672: if (row[j] == i) { /* don't count diagonal */
1673: len--;
1674: break;
1675: }
1676: }
1677: nnz += len;
1678: }
1679: PetscCall(PetscMalloc1(na + 1, &iia));
1680: PetscCall(PetscMalloc1(nnz, &jja));
1681: nnz = 0;
1682: iia[0] = 0;
1683: for (i = 0; i < na; i++) { /* fill adjacency */
1684: cnt = 0;
1685: len = ia[i + 1] - ia[i];
1686: row = ja + ia[i];
1687: for (j = 0; j < len; j++) {
1688: if (row[j] != i) { /* if not diagonal */
1689: jja[nnz + cnt++] = row[j];
1690: }
1691: }
1692: nnz += cnt;
1693: iia[i + 1] = nnz;
1694: }
1695: /* Partitioning of the adjacency matrix */
1696: PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj));
1697: PetscCall(MatPartitioningSetAdjacency(mpart, adj));
1698: PetscCall(MatPartitioningSetNParts(mpart, n));
1699: PetscCall(MatPartitioningApply(mpart, &ispart));
1700: PetscCall(ISPartitioningToNumbering(ispart, &isnumb));
1701: PetscCall(MatDestroy(&adj));
1702: foundpart = PETSC_TRUE;
1703: }
1704: PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1705: }
1706: PetscCall(MatPartitioningDestroy(&mpart));
1707: }
1709: PetscCall(PetscMalloc1(n, &is));
1710: *outis = is;
1712: if (!foundpart) {
1713: /* Partitioning by contiguous chunks of rows */
1715: PetscInt mbs = (rend - rstart) / bs;
1716: PetscInt start = rstart;
1717: for (i = 0; i < n; i++) {
1718: PetscInt count = (mbs / n + ((mbs % n) > i)) * bs;
1719: PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i]));
1720: start += count;
1721: }
1723: } else {
1724: /* Partitioning by adjacency of diagonal block */
1726: const PetscInt *numbering;
1727: PetscInt *count, nidx, *indices, *newidx, start = 0;
1728: /* Get node count in each partition */
1729: PetscCall(PetscMalloc1(n, &count));
1730: PetscCall(ISPartitioningCount(ispart, n, count));
1731: if (isbaij && bs > 1) { /* adjust for the block-aij case */
1732: for (i = 0; i < n; i++) count[i] *= bs;
1733: }
1734: /* Build indices from node numbering */
1735: PetscCall(ISGetLocalSize(isnumb, &nidx));
1736: PetscCall(PetscMalloc1(nidx, &indices));
1737: for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */
1738: PetscCall(ISGetIndices(isnumb, &numbering));
1739: PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices));
1740: PetscCall(ISRestoreIndices(isnumb, &numbering));
1741: if (isbaij && bs > 1) { /* adjust for the block-aij case */
1742: PetscCall(PetscMalloc1(nidx * bs, &newidx));
1743: for (i = 0; i < nidx; i++) {
1744: for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j;
1745: }
1746: PetscCall(PetscFree(indices));
1747: nidx *= bs;
1748: indices = newidx;
1749: }
1750: /* Shift to get global indices */
1751: for (i = 0; i < nidx; i++) indices[i] += rstart;
1753: /* Build the index sets for each block */
1754: for (i = 0; i < n; i++) {
1755: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i]));
1756: PetscCall(ISSort(is[i]));
1757: start += count[i];
1758: }
1760: PetscCall(PetscFree(count));
1761: PetscCall(PetscFree(indices));
1762: PetscCall(ISDestroy(&isnumb));
1763: PetscCall(ISDestroy(&ispart));
1764: }
1765: PetscFunctionReturn(PETSC_SUCCESS);
1766: }
1768: /*@
1769: PCASMDestroySubdomains - Destroys the index sets created with
1770: `PCASMCreateSubdomains()` or `PCASMCreateSubdomains2D()`. Should be called after setting subdomains with `PCASMSetLocalSubdomains()`.
1772: Collective
1774: Input Parameters:
1775: + n - the number of index sets
1776: . is - the array of index sets
1777: - is_local - the array of local index sets, can be `NULL`
1779: Level: advanced
1781: Note:
1782: `PCASMCreateSubdomains2D()` also creates an array of non-overlapping local index sets, which must be passed as `is_local`.
1783: `PCASMCreateSubdomains()` creates no local index sets, so pass `NULL` for `is_local`.
1785: Fortran Note:
1786: `is` cannot be `PETSC_NULL_IS_POINTER`. For arrays from `PCASMCreateSubdomains2D()`, pass the returned `is_local`;
1787: for arrays from `PCASMCreateSubdomains()`, pass `PETSC_NULL_IS_POINTER` for `is_local`.
1789: Developer Note:
1790: The `IS` arguments should be a *[]
1792: .seealso: [](ch_ksp), `PCASM`, `PCASMCreateSubdomains()`, `PCASMCreateSubdomains2D()`, `PCASMSetLocalSubdomains()`
1793: @*/
1794: PetscErrorCode PCASMDestroySubdomains(PetscInt n, IS *is[], IS *is_local[])
1795: {
1796: PetscInt i;
1798: PetscFunctionBegin;
1799: if (n <= 0) PetscFunctionReturn(PETSC_SUCCESS);
1800: if (*is) {
1801: PetscAssertPointer(*is, 2);
1802: for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*is)[i]));
1803: PetscCall(PetscFree(*is));
1804: }
1805: if (is_local && *is_local) {
1806: PetscAssertPointer(*is_local, 3);
1807: for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*is_local)[i]));
1808: PetscCall(PetscFree(*is_local));
1809: }
1810: PetscFunctionReturn(PETSC_SUCCESS);
1811: }
1813: /*@
1814: PCASMCreateSubdomains2D - Creates the index sets for the overlapping Schwarz
1815: preconditioner, `PCASM`, for a two-dimensional problem on a regular grid.
1817: Not Collective
1819: Input Parameters:
1820: + m - the number of mesh points in the x direction
1821: . n - the number of mesh points in the y direction
1822: . M - the number of subdomains in the x direction
1823: . N - the number of subdomains in the y direction
1824: . dof - degrees of freedom per node
1825: - overlap - overlap in mesh lines
1827: Output Parameters:
1828: + Nsub - the number of subdomains created
1829: . is - array of index sets defining overlapping (if overlap > 0) subdomains
1830: - is_local - array of index sets defining non-overlapping subdomains
1832: Level: advanced
1834: Note:
1835: Presently `PCAMSCreateSubdomains2d()` is valid only for sequential
1836: preconditioners. More general related routines are
1837: `PCASMSetTotalSubdomains()` and `PCASMSetLocalSubdomains()`.
1839: Fortran Note:
1840: Both `is` and `is_local` are created, so neither can be `PETSC_NULL_IS_POINTER`.
1842: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetLocalSubdomains()`, `PCASMGetSubKSP()`,
1843: `PCASMSetOverlap()`
1844: @*/
1845: PetscErrorCode PCASMCreateSubdomains2D(PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscInt dof, PetscInt overlap, PetscInt *Nsub, IS *is[], IS *is_local[])
1846: {
1847: PetscInt i, j, height, width, ystart, xstart, yleft, yright, xleft, xright, loc_outer;
1848: PetscInt nidx, *idx, loc, ii, jj, count;
1850: PetscFunctionBegin;
1851: PetscCheck(dof == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "dof must be 1");
1853: *Nsub = N * M;
1854: PetscCall(PetscMalloc1(*Nsub, is));
1855: PetscCall(PetscMalloc1(*Nsub, is_local));
1856: ystart = 0;
1857: loc_outer = 0;
1858: for (i = 0; i < N; i++) {
1859: height = n / N + ((n % N) > i); /* height of subdomain */
1860: PetscCheck(height >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many N subdomains for mesh dimension n");
1861: yleft = ystart - overlap;
1862: if (yleft < 0) yleft = 0;
1863: yright = ystart + height + overlap;
1864: if (yright > n) yright = n;
1865: xstart = 0;
1866: for (j = 0; j < M; j++) {
1867: width = m / M + ((m % M) > j); /* width of subdomain */
1868: PetscCheck(width >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many M subdomains for mesh dimension m");
1869: xleft = xstart - overlap;
1870: if (xleft < 0) xleft = 0;
1871: xright = xstart + width + overlap;
1872: if (xright > m) xright = m;
1873: nidx = (xright - xleft) * (yright - yleft);
1874: PetscCall(PetscMalloc1(nidx, &idx));
1875: loc = 0;
1876: for (ii = yleft; ii < yright; ii++) {
1877: count = m * ii + xleft;
1878: for (jj = xleft; jj < xright; jj++) idx[loc++] = count++;
1879: }
1880: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nidx, idx, PETSC_COPY_VALUES, (*is) + loc_outer));
1881: if (overlap == 0) {
1882: PetscCall(PetscObjectReference((PetscObject)(*is)[loc_outer]));
1884: (*is_local)[loc_outer] = (*is)[loc_outer];
1885: } else {
1886: for (loc = 0, ii = ystart; ii < ystart + height; ii++) {
1887: for (jj = xstart; jj < xstart + width; jj++) idx[loc++] = m * ii + jj;
1888: }
1889: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, loc, idx, PETSC_COPY_VALUES, *is_local + loc_outer));
1890: }
1891: PetscCall(PetscFree(idx));
1892: xstart += width;
1893: loc_outer++;
1894: }
1895: ystart += height;
1896: }
1897: for (i = 0; i < *Nsub; i++) PetscCall(ISSort((*is)[i]));
1898: PetscFunctionReturn(PETSC_SUCCESS);
1899: }
1901: /*@
1902: PCASMGetLocalSubdomains - Gets the local subdomains (for this processor
1903: only) for the additive Schwarz preconditioner, `PCASM`.
1905: Not Collective
1907: Input Parameter:
1908: . pc - the preconditioner context
1910: Output Parameters:
1911: + n - if requested, the number of subdomains for this processor (default value = 1)
1912: . is - if requested, the index sets that define the subdomains for this processor
1913: - is_local - if requested, the index sets that define the local part of the subdomains for this processor (can be `NULL`)
1915: Level: advanced
1917: Note:
1918: The `IS` numbering is in the parallel, global numbering of the vector.
1920: Fortran Note:
1921: Pass `PETSC_NULL_IS_POINTER` for `is` or `is_local` if not needed. A requested array that does not exist is returned disassociated.
1923: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`, `PCASMGetSubKSP()`,
1924: `PCASMCreateSubdomains2D()`, `PCASMSetLocalSubdomains()`, `PCASMGetLocalSubmatrices()`
1925: @*/
1926: PetscErrorCode PCASMGetLocalSubdomains(PC pc, PetscInt *n, IS *is[], IS *is_local[])
1927: {
1928: PC_ASM *osm = (PC_ASM *)pc->data;
1929: PetscBool match;
1931: PetscFunctionBegin;
1933: if (n) PetscAssertPointer(n, 2);
1934: if (is) PetscAssertPointer(is, 3);
1935: if (is_local) PetscAssertPointer(is_local, 4);
1936: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &match));
1937: PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "PC is not a PCASM");
1938: if (n) *n = osm->n_local_true;
1939: if (is) *is = osm->is;
1940: if (is_local) *is_local = osm->is_local;
1941: PetscFunctionReturn(PETSC_SUCCESS);
1942: }
1944: /*@
1945: PCASMGetLocalSubmatrices - Gets the local submatrices (for this processor
1946: only) for the additive Schwarz preconditioner, `PCASM`.
1948: Not Collective
1950: Input Parameter:
1951: . pc - the preconditioner context
1953: Output Parameters:
1954: + n - if requested, the number of matrices for this processor (default value = 1)
1955: - mat - if requested, the matrices
1957: Level: advanced
1959: Notes:
1960: Call after `PCSetUp()` (or `KSPSetUp()`) but before `PCApply()` and before `PCSetUpOnBlocks()`)
1962: Usually one would use `PCSetModifySubMatrices()` to change the submatrices in building the preconditioner.
1964: Fortran Note:
1965: Pass `PETSC_NULL_MAT_POINTER` for `mat` if not needed. If the `PC` is not a `PCASM`, `mat` is returned disassociated.
1967: .seealso: [](ch_ksp), `PCASM`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`, `PCASMGetSubKSP()`,
1968: `PCASMCreateSubdomains2D()`, `PCASMSetLocalSubdomains()`, `PCASMGetLocalSubdomains()`, `PCSetModifySubMatrices()`
1969: @*/
1970: PetscErrorCode PCASMGetLocalSubmatrices(PC pc, PetscInt *n, Mat *mat[])
1971: {
1972: PC_ASM *osm;
1973: PetscBool match;
1975: PetscFunctionBegin;
1977: if (n) PetscAssertPointer(n, 2);
1978: if (mat) PetscAssertPointer(mat, 3);
1979: PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp().");
1980: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &match));
1981: if (!match) {
1982: if (n) *n = 0;
1983: if (mat) *mat = NULL;
1984: } else {
1985: osm = (PC_ASM *)pc->data;
1986: if (n) *n = osm->n_local_true;
1987: if (mat) *mat = osm->pmat;
1988: }
1989: PetscFunctionReturn(PETSC_SUCCESS);
1990: }
1992: /*@
1993: PCASMSetDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible.
1995: Logically Collective
1997: Input Parameters:
1998: + pc - the preconditioner
1999: - flg - boolean indicating whether to use subdomains defined by the `DM`
2001: Options Database Key:
2002: . -pc_asm_dm_subdomains (true|false) - use subdomains defined by the `DM` with `DMCreateDomainDecomposition()`
2004: Level: intermediate
2006: Note:
2007: `PCASMSetTotalSubdomains()` and `PCASMSetOverlap()` take precedence over `PCASMSetDMSubdomains()`,
2008: so setting either of the first two effectively turns the latter off.
2010: Developer Note:
2011: This should be `PCASMSetUseDMSubdomains()`, similarly for the options database key
2013: .seealso: [](ch_ksp), `PCASM`, `PCASMGetDMSubdomains()`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`,
2014: `PCASMCreateSubdomains2D()`, `PCASMSetLocalSubdomains()`, `PCASMGetLocalSubdomains()`
2015: @*/
2016: PetscErrorCode PCASMSetDMSubdomains(PC pc, PetscBool flg)
2017: {
2018: PC_ASM *osm = (PC_ASM *)pc->data;
2019: PetscBool match;
2021: PetscFunctionBegin;
2024: PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC.");
2025: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &match));
2026: if (match) osm->dm_subdomains = flg;
2027: PetscFunctionReturn(PETSC_SUCCESS);
2028: }
2030: /*@
2031: PCASMGetDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible.
2033: Not Collective
2035: Input Parameter:
2036: . pc - the preconditioner
2038: Output Parameter:
2039: . flg - boolean indicating whether to use subdomains defined by the `DM`
2041: Level: intermediate
2043: Developer Note:
2044: This should be `PCASMSetUseDMSubdomains()`
2046: .seealso: [](ch_ksp), `PCASM`, `PCASMSetDMSubdomains()`, `PCASMSetTotalSubdomains()`, `PCASMSetOverlap()`,
2047: `PCASMCreateSubdomains2D()`, `PCASMSetLocalSubdomains()`, `PCASMGetLocalSubdomains()`
2048: @*/
2049: PetscErrorCode PCASMGetDMSubdomains(PC pc, PetscBool *flg)
2050: {
2051: PC_ASM *osm = (PC_ASM *)pc->data;
2052: PetscBool match;
2054: PetscFunctionBegin;
2056: PetscAssertPointer(flg, 2);
2057: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCASM, &match));
2058: if (match) *flg = osm->dm_subdomains;
2059: else *flg = PETSC_FALSE;
2060: PetscFunctionReturn(PETSC_SUCCESS);
2061: }
2063: /*@
2064: PCASMGetSubMatType - Gets the matrix type used for `PCASM` subsolves, as a string.
2066: Not Collective
2068: Input Parameter:
2069: . pc - the `PC`
2071: Output Parameter:
2072: . sub_mat_type - name of matrix type
2074: Level: advanced
2076: .seealso: [](ch_ksp), `PCASM`, `PCASMSetSubMatType()`, `PCSetType()`, `VecSetType()`, `MatType`, `Mat`
2077: @*/
2078: PetscErrorCode PCASMGetSubMatType(PC pc, MatType *sub_mat_type)
2079: {
2080: PetscFunctionBegin;
2082: PetscTryMethod(pc, "PCASMGetSubMatType_C", (PC, MatType *), (pc, sub_mat_type));
2083: PetscFunctionReturn(PETSC_SUCCESS);
2084: }
2086: /*@
2087: PCASMSetSubMatType - Set the type of matrix used for `PCASM` subsolves
2089: Collective
2091: Input Parameters:
2092: + pc - the `PC` object
2093: - sub_mat_type - the `MatType`
2095: Options Database Key:
2096: . -pc_asm_sub_mat_type sub_mat_type - Sets the matrix type used for subsolves, for example, seqaijviennacl.
2097: If you specify a base name like aijviennacl, the corresponding sequential type is assumed.
2099: Note:
2100: See `MatType` for available types
2102: Level: advanced
2104: .seealso: [](ch_ksp), `PCASM`, `PCASMGetSubMatType()`, `PCSetType()`, `VecSetType()`, `MatType`, `Mat`
2105: @*/
2106: PetscErrorCode PCASMSetSubMatType(PC pc, MatType sub_mat_type)
2107: {
2108: PetscFunctionBegin;
2110: PetscTryMethod(pc, "PCASMSetSubMatType_C", (PC, MatType), (pc, sub_mat_type));
2111: PetscFunctionReturn(PETSC_SUCCESS);
2112: }