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