Actual source code: asm.c

petsc-master 2016-07-22
Report Typos and Errors
  2: /*
  3:   This file defines an additive Schwarz preconditioner for any Mat implementation.

  5:   Note that each processor may have any number of subdomains. But in order to
  6:   deal easily with the VecScatter(), we treat each processor as if it has the
  7:   same number of subdomains.

  9:        n - total number of true subdomains on all processors
 10:        n_local_true - actual number of subdomains on this processor
 11:        n_local = maximum over all processors of n_local_true
 12: */
 13: #include <petsc/private/pcimpl.h>     /*I "petscpc.h" I*/
 14: #include <petscdm.h>

 16: typedef struct {
 17:   PetscInt   n, n_local, n_local_true;
 18:   PetscInt   overlap;             /* overlap requested by user */
 19:   KSP        *ksp;                /* linear solvers for each block */
 20:   VecScatter *restriction;        /* mapping from global to subregion */
 21:   VecScatter *localization;       /* mapping from overlapping to non-overlapping subregion */
 22:   VecScatter *prolongation;       /* mapping from subregion to global */
 23:   Vec        *x,*y,*y_local;      /* work vectors */
 24:   IS         *is;                 /* index set that defines each overlapping subdomain */
 25:   IS         *is_local;           /* index set that defines each non-overlapping subdomain, may be NULL */
 26:   Mat        *mat,*pmat;          /* mat is not currently used */
 27:   PCASMType  type;                /* use reduced interpolation, restriction or both */
 28:   PetscBool  type_set;            /* if user set this value (so won't change it for symmetric problems) */
 29:   PetscBool  same_local_solves;   /* flag indicating whether all local solvers are same */
 30:   PetscBool  sort_indices;        /* flag to sort subdomain indices */
 31:   PetscBool  dm_subdomains;       /* whether DM is allowed to define subdomains */
 32:   PCCompositeType loctype;        /* the type of composition for local solves */
 33:   /* For multiplicative solve */
 34:   Mat       *lmats;               /* submatrices for overlapping multiplicative (process) subdomain */
 35:   Vec        lx, ly;              /* work vectors */
 36:   IS         lis;                 /* index set that defines each overlapping multiplicative (process) subdomain */
 37: } PC_ASM;

 41: static PetscErrorCode PCView_ASM(PC pc,PetscViewer viewer)
 42: {
 43:   PC_ASM         *osm = (PC_ASM*)pc->data;
 45:   PetscMPIInt    rank;
 46:   PetscInt       i,bsz;
 47:   PetscBool      iascii,isstring;
 48:   PetscViewer    sviewer;

 51:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
 52:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
 53:   if (iascii) {
 54:     char overlaps[256] = "user-defined overlap",blocks[256] = "total subdomain blocks not yet set";
 55:     if (osm->overlap >= 0) {PetscSNPrintf(overlaps,sizeof(overlaps),"amount of overlap = %D",osm->overlap);}
 56:     if (osm->n > 0) {PetscSNPrintf(blocks,sizeof(blocks),"total subdomain blocks = %D",osm->n);}
 57:     PetscViewerASCIIPrintf(viewer,"  Additive Schwarz: %s, %s\n",blocks,overlaps);
 58:     PetscViewerASCIIPrintf(viewer,"  Additive Schwarz: restriction/interpolation type - %s\n",PCASMTypes[osm->type]);
 59:     if (osm->loctype != PC_COMPOSITE_ADDITIVE) {PetscViewerASCIIPrintf(viewer,"  Additive Schwarz: local solve composition type - %s\n",PCCompositeTypes[osm->loctype]);}
 60:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
 61:     if (osm->same_local_solves) {
 62:       if (osm->ksp) {
 63:         PetscViewerASCIIPrintf(viewer,"  Local solve is same for all blocks, in the following KSP and PC objects:\n");
 64:         PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 65:         if (!rank) {
 66:           PetscViewerASCIIPushTab(viewer);
 67:           KSPView(osm->ksp[0],sviewer);
 68:           PetscViewerASCIIPopTab(viewer);
 69:         }
 70:         PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 71:       }
 72:     } else {
 73:       PetscViewerASCIIPushSynchronized(viewer);
 74:       PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] number of local blocks = %D\n",(int)rank,osm->n_local_true);
 75:       PetscViewerFlush(viewer);
 76:       PetscViewerASCIIPrintf(viewer,"  Local solve info for each block is in the following KSP and PC objects:\n");
 77:       PetscViewerASCIIPushTab(viewer);
 78:       PetscViewerASCIIPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
 79:       PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 80:       for (i=0; i<osm->n_local_true; i++) {
 81:         ISGetLocalSize(osm->is[i],&bsz);
 82:         PetscViewerASCIISynchronizedPrintf(sviewer,"[%d] local block number %D, size = %D\n",(int)rank,i,bsz);
 83:         KSPView(osm->ksp[i],sviewer);
 84:         PetscViewerASCIISynchronizedPrintf(sviewer,"- - - - - - - - - - - - - - - - - -\n");
 85:       }
 86:       PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 87:       PetscViewerASCIIPopTab(viewer);
 88:       PetscViewerFlush(viewer);
 89:       PetscViewerASCIIPopSynchronized(viewer);
 90:     }
 91:   } else if (isstring) {
 92:     PetscViewerStringSPrintf(viewer," blocks=%D, overlap=%D, type=%s",osm->n,osm->overlap,PCASMTypes[osm->type]);
 93:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 94:     if (osm->ksp) {KSPView(osm->ksp[0],sviewer);}
 95:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
 96:   }
 97:   return(0);
 98: }

102: static PetscErrorCode PCASMPrintSubdomains(PC pc)
103: {
104:   PC_ASM         *osm = (PC_ASM*)pc->data;
105:   const char     *prefix;
106:   char           fname[PETSC_MAX_PATH_LEN+1];
107:   PetscViewer    viewer, sviewer;
108:   char           *s;
109:   PetscInt       i,j,nidx;
110:   const PetscInt *idx;
111:   PetscMPIInt    rank, size;

115:   MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size);
116:   MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank);
117:   PCGetOptionsPrefix(pc,&prefix);
118:   PetscOptionsGetString(NULL,prefix,"-pc_asm_print_subdomains",fname,PETSC_MAX_PATH_LEN,NULL);
119:   if (fname[0] == 0) { PetscStrcpy(fname,"stdout"); };
120:   PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc),fname,&viewer);
121:   for (i=0; i<osm->n_local; i++) {
122:     if (i < osm->n_local_true) {
123:       ISGetLocalSize(osm->is[i],&nidx);
124:       ISGetIndices(osm->is[i],&idx);
125:       /* Print to a string viewer; no more than 15 characters per index plus 512 char for the header.*/
126:       PetscMalloc1(16*(nidx+1)+512, &s);
127:       PetscViewerStringOpen(PETSC_COMM_SELF, s, 16*(nidx+1)+512, &sviewer);
128:       PetscViewerStringSPrintf(sviewer, "[%D:%D] Subdomain %D with overlap:\n", rank, size, i);
129:       for (j=0; j<nidx; j++) {
130:         PetscViewerStringSPrintf(sviewer,"%D ",idx[j]);
131:       }
132:       ISRestoreIndices(osm->is[i],&idx);
133:       PetscViewerStringSPrintf(sviewer,"\n");
134:       PetscViewerDestroy(&sviewer);
135:       PetscViewerASCIIPushSynchronized(viewer);
136:       PetscViewerASCIISynchronizedPrintf(viewer, s);
137:       PetscViewerFlush(viewer);
138:       PetscViewerASCIIPopSynchronized(viewer);
139:       PetscFree(s);
140:       if (osm->is_local) {
141:         /* Print to a string viewer; no more than 15 characters per index plus 512 char for the header.*/
142:         PetscMalloc1(16*(nidx+1)+512, &s);
143:         PetscViewerStringOpen(PETSC_COMM_SELF, s, 16*(nidx+1)+512, &sviewer);
144:         PetscViewerStringSPrintf(sviewer, "[%D:%D] Subdomain %D without overlap:\n", rank, size, i);
145:         ISGetLocalSize(osm->is_local[i],&nidx);
146:         ISGetIndices(osm->is_local[i],&idx);
147:         for (j=0; j<nidx; j++) {
148:           PetscViewerStringSPrintf(sviewer,"%D ",idx[j]);
149:         }
150:         ISRestoreIndices(osm->is_local[i],&idx);
151:         PetscViewerStringSPrintf(sviewer,"\n");
152:         PetscViewerDestroy(&sviewer);
153:         PetscViewerASCIIPushSynchronized(viewer);
154:         PetscViewerASCIISynchronizedPrintf(viewer, s);
155:         PetscViewerFlush(viewer);
156:         PetscViewerASCIIPopSynchronized(viewer);
157:         PetscFree(s);
158:       }
159:     } else {
160:       /* Participate in collective viewer calls. */
161:       PetscViewerASCIIPushSynchronized(viewer);
162:       PetscViewerFlush(viewer);
163:       PetscViewerASCIIPopSynchronized(viewer);
164:       /* Assume either all ranks have is_local or none do. */
165:       if (osm->is_local) {
166:         PetscViewerASCIIPushSynchronized(viewer);
167:         PetscViewerFlush(viewer);
168:         PetscViewerASCIIPopSynchronized(viewer);
169:       }
170:     }
171:   }
172:   PetscViewerFlush(viewer);
173:   PetscViewerDestroy(&viewer);
174:   return(0);
175: }

179: static PetscErrorCode PCSetUp_ASM(PC pc)
180: {
181:   PC_ASM         *osm = (PC_ASM*)pc->data;
183:   PetscBool      symset,flg;
184:   PetscInt       i,m,m_local;
185:   MatReuse       scall = MAT_REUSE_MATRIX;
186:   IS             isl;
187:   KSP            ksp;
188:   PC             subpc;
189:   const char     *prefix,*pprefix;
190:   Vec            vec;
191:   DM             *domain_dm = NULL;

194:   if (!pc->setupcalled) {

196:     if (!osm->type_set) {
197:       MatIsSymmetricKnown(pc->pmat,&symset,&flg);
198:       if (symset && flg) osm->type = PC_ASM_BASIC;
199:     }

201:     /* Note: if subdomains have been set either via PCASMSetTotalSubdomains() or via PCASMSetLocalSubdomains(), osm->n_local_true will not be PETSC_DECIDE */
202:     if (osm->n_local_true == PETSC_DECIDE) {
203:       /* no subdomains given */
204:       /* try pc->dm first, if allowed */
205:       if (osm->dm_subdomains && pc->dm) {
206:         PetscInt  num_domains, d;
207:         char      **domain_names;
208:         IS        *inner_domain_is, *outer_domain_is;
209:         DMCreateDomainDecomposition(pc->dm, &num_domains, &domain_names, &inner_domain_is, &outer_domain_is, &domain_dm);
210:         if (num_domains) {
211:           PCASMSetLocalSubdomains(pc, num_domains, outer_domain_is, inner_domain_is);
212:         }
213:         for (d = 0; d < num_domains; ++d) {
214:           if (domain_names)    {PetscFree(domain_names[d]);}
215:           if (inner_domain_is) {ISDestroy(&inner_domain_is[d]);}
216:           if (outer_domain_is) {ISDestroy(&outer_domain_is[d]);}
217:         }
218:         PetscFree(domain_names);
219:         PetscFree(inner_domain_is);
220:         PetscFree(outer_domain_is);
221:       }
222:       if (osm->n_local_true == PETSC_DECIDE) {
223:         /* still no subdomains; use one subdomain per processor */
224:         osm->n_local_true = 1;
225:       }
226:     }
227:     { /* determine the global and max number of subdomains */
228:       struct {PetscInt max,sum;} inwork,outwork;
229:       inwork.max   = osm->n_local_true;
230:       inwork.sum   = osm->n_local_true;
231:       MPIU_Allreduce(&inwork,&outwork,1,MPIU_2INT,MPIU_MAXSUM_OP,PetscObjectComm((PetscObject)pc));
232:       osm->n_local = outwork.max;
233:       osm->n       = outwork.sum;
234:     }
235:     if (!osm->is) { /* create the index sets */
236:       PCASMCreateSubdomains(pc->pmat,osm->n_local_true,&osm->is);
237:     }
238:     if (osm->n_local_true > 1 && !osm->is_local) {
239:       PetscMalloc1(osm->n_local_true,&osm->is_local);
240:       for (i=0; i<osm->n_local_true; i++) {
241:         if (osm->overlap > 0) { /* With positive overlap, osm->is[i] will be modified */
242:           ISDuplicate(osm->is[i],&osm->is_local[i]);
243:           ISCopy(osm->is[i],osm->is_local[i]);
244:         } else {
245:           PetscObjectReference((PetscObject)osm->is[i]);
246:           osm->is_local[i] = osm->is[i];
247:         }
248:       }
249:     }
250:     PCGetOptionsPrefix(pc,&prefix);
251:     flg  = PETSC_FALSE;
252:     PetscOptionsGetBool(NULL,prefix,"-pc_asm_print_subdomains",&flg,NULL);
253:     if (flg) { PCASMPrintSubdomains(pc); }

255:     if (osm->overlap > 0) {
256:       /* Extend the "overlapping" regions by a number of steps */
257:       MatIncreaseOverlap(pc->pmat,osm->n_local_true,osm->is,osm->overlap);
258:     }
259:     if (osm->sort_indices) {
260:       for (i=0; i<osm->n_local_true; i++) {
261:         ISSort(osm->is[i]);
262:         if (osm->is_local) {
263:           ISSort(osm->is_local[i]);
264:         }
265:       }
266:     }
267:     /* Create the local work vectors and scatter contexts */
268:     MatCreateVecs(pc->pmat,&vec,0);
269:     PetscMalloc1(osm->n_local,&osm->restriction);
270:     if (osm->is_local) {PetscMalloc1(osm->n_local,&osm->localization);}
271:     PetscMalloc1(osm->n_local,&osm->prolongation);
272:     PetscMalloc1(osm->n_local,&osm->x);
273:     PetscMalloc1(osm->n_local,&osm->y);
274:     PetscMalloc1(osm->n_local,&osm->y_local);
275:     for (i=0; i<osm->n_local_true; ++i) {
276:       ISGetLocalSize(osm->is[i],&m);
277:       VecCreateSeq(PETSC_COMM_SELF,m,&osm->x[i]);
278:       ISCreateStride(PETSC_COMM_SELF,m,0,1,&isl);
279:       VecScatterCreate(vec,osm->is[i],osm->x[i],isl,&osm->restriction[i]);
280:       ISDestroy(&isl);
281:       VecDuplicate(osm->x[i],&osm->y[i]);
282:       if (osm->is_local) {
283:         ISLocalToGlobalMapping ltog;
284:         IS                     isll;
285:         const PetscInt         *idx_local;
286:         PetscInt               *idx,nout;

288:         ISLocalToGlobalMappingCreateIS(osm->is[i],&ltog);
289:         ISGetLocalSize(osm->is_local[i],&m_local);
290:         ISGetIndices(osm->is_local[i], &idx_local);
291:         PetscMalloc1(m_local,&idx);
292:         ISGlobalToLocalMappingApply(ltog,IS_GTOLM_DROP,m_local,idx_local,&nout,idx);
293:         ISLocalToGlobalMappingDestroy(&ltog);
294:         if (nout != m_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"is_local not a subset of is");
295:         ISRestoreIndices(osm->is_local[i], &idx_local);
296:         ISCreateGeneral(PETSC_COMM_SELF,m_local,idx,PETSC_OWN_POINTER,&isll);
297:         ISCreateStride(PETSC_COMM_SELF,m_local,0,1,&isl);
298:         VecCreateSeq(PETSC_COMM_SELF,m_local,&osm->y_local[i]);
299:         VecScatterCreate(osm->y[i],isll,osm->y_local[i],isl,&osm->localization[i]);
300:         ISDestroy(&isll);

302:         VecScatterCreate(vec,osm->is_local[i],osm->y_local[i],isl,&osm->prolongation[i]);
303:         ISDestroy(&isl);
304:       } else {
305:         VecGetLocalSize(vec,&m_local);

307:         osm->y_local[i] = osm->y[i];

309:         PetscObjectReference((PetscObject) osm->y[i]);

311:         osm->prolongation[i] = osm->restriction[i];

313:         PetscObjectReference((PetscObject) osm->restriction[i]);
314:       }
315:     }
316:     for (i=osm->n_local_true; i<osm->n_local; i++) {
317:       VecCreateSeq(PETSC_COMM_SELF,0,&osm->x[i]);
318:       VecDuplicate(osm->x[i],&osm->y[i]);
319:       VecDuplicate(osm->x[i],&osm->y_local[i]);
320:       ISCreateStride(PETSC_COMM_SELF,0,0,1,&isl);
321:       VecScatterCreate(vec,isl,osm->x[i],isl,&osm->restriction[i]);
322:       if (osm->is_local) {
323:         VecScatterCreate(osm->y[i],isl,osm->y_local[i],isl,&osm->localization[i]);
324:         VecScatterCreate(vec,isl,osm->x[i],isl,&osm->prolongation[i]);
325:       } else {
326:         osm->prolongation[i] = osm->restriction[i];
327:         PetscObjectReference((PetscObject) osm->restriction[i]);
328:       }
329:       ISDestroy(&isl);
330:     }
331:     VecDestroy(&vec);

333:     if (!osm->ksp) {
334:       /* Create the local solvers */
335:       PetscMalloc1(osm->n_local_true,&osm->ksp);
336:       if (domain_dm) {
337:         PetscInfo(pc,"Setting up ASM subproblems using the embedded DM\n");
338:       }
339:       for (i=0; i<osm->n_local_true; i++) {
340:         KSPCreate(PETSC_COMM_SELF,&ksp);
341:         KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
342:         PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
343:         PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
344:         KSPSetType(ksp,KSPPREONLY);
345:         KSPGetPC(ksp,&subpc);
346:         PCGetOptionsPrefix(pc,&prefix);
347:         KSPSetOptionsPrefix(ksp,prefix);
348:         KSPAppendOptionsPrefix(ksp,"sub_");
349:         if (domain_dm) {
350:           KSPSetDM(ksp, domain_dm[i]);
351:           KSPSetDMActive(ksp, PETSC_FALSE);
352:           DMDestroy(&domain_dm[i]);
353:         }
354:         osm->ksp[i] = ksp;
355:       }
356:       if (domain_dm) {
357:         PetscFree(domain_dm);
358:       }
359:     }
360:     if (osm->loctype == PC_COMPOSITE_MULTIPLICATIVE) {
361:       PetscInt m;

363:       ISConcatenate(PETSC_COMM_SELF, osm->n_local_true, osm->is, &osm->lis);
364:       ISSortRemoveDups(osm->lis);
365:       ISGetLocalSize(osm->lis, &m);
366:       VecCreateSeq(PETSC_COMM_SELF, m, &osm->lx);
367:       VecDuplicate(osm->lx, &osm->ly);
368:     }
369:     scall = MAT_INITIAL_MATRIX;
370:   } else {
371:     /*
372:        Destroy the blocks from the previous iteration
373:     */
374:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
375:       MatDestroyMatrices(osm->n_local_true,&osm->pmat);
376:       scall = MAT_INITIAL_MATRIX;
377:     }
378:   }

380:   /*
381:      Extract out the submatrices
382:   */
383:   MatGetSubMatrices(pc->pmat,osm->n_local_true,osm->is,osm->is,scall,&osm->pmat);
384:   if (scall == MAT_INITIAL_MATRIX) {
385:     PetscObjectGetOptionsPrefix((PetscObject)pc->pmat,&pprefix);
386:     for (i=0; i<osm->n_local_true; i++) {
387:       PetscLogObjectParent((PetscObject)pc,(PetscObject)osm->pmat[i]);
388:       PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i],pprefix);
389:     }
390:   }
391:   if (osm->loctype == PC_COMPOSITE_MULTIPLICATIVE) {
392:     IS      *cis;
393:     PetscInt c;

395:     PetscMalloc1(osm->n_local_true, &cis);
396:     for (c = 0; c < osm->n_local_true; ++c) cis[c] = osm->lis;
397:     MatGetSubMatrices(pc->pmat, osm->n_local_true, osm->is, cis, scall, &osm->lmats);
398:     PetscFree(cis);
399:   }

401:   /* Return control to the user so that the submatrices can be modified (e.g., to apply
402:      different boundary conditions for the submatrices than for the global problem) */
403:   PCModifySubMatrices(pc,osm->n_local_true,osm->is,osm->is,osm->pmat,pc->modifysubmatricesP);

405:   /*
406:      Loop over subdomains putting them into local ksp
407:   */
408:   for (i=0; i<osm->n_local_true; i++) {
409:     KSPSetOperators(osm->ksp[i],osm->pmat[i],osm->pmat[i]);
410:     if (!pc->setupcalled) {
411:       KSPSetFromOptions(osm->ksp[i]);
412:     }
413:   }
414:   return(0);
415: }

419: static PetscErrorCode PCSetUpOnBlocks_ASM(PC pc)
420: {
421:   PC_ASM             *osm = (PC_ASM*)pc->data;
422:   PetscErrorCode     ierr;
423:   PetscInt           i;
424:   KSPConvergedReason reason;

427:   for (i=0; i<osm->n_local_true; i++) {
428:     KSPSetUp(osm->ksp[i]);
429:     KSPGetConvergedReason(osm->ksp[i],&reason);
430:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
431:       pc->failedreason = PC_SUBPC_ERROR;
432:     }
433:   }
434:   return(0);
435: }

439: static PetscErrorCode PCApply_ASM(PC pc,Vec x,Vec y)
440: {
441:   PC_ASM         *osm = (PC_ASM*)pc->data;
443:   PetscInt       i,n_local = osm->n_local,n_local_true = osm->n_local_true;
444:   ScatterMode    forward = SCATTER_FORWARD,reverse = SCATTER_REVERSE;

447:   /*
448:      Support for limiting the restriction or interpolation to only local
449:      subdomain values (leaving the other values 0).
450:   */
451:   if (!(osm->type & PC_ASM_RESTRICT)) {
452:     forward = SCATTER_FORWARD_LOCAL;
453:     /* have to zero the work RHS since scatter may leave some slots empty */
454:     for (i=0; i<n_local_true; i++) {
455:       VecZeroEntries(osm->x[i]);
456:     }
457:   }
458:   if (!(osm->type & PC_ASM_INTERPOLATE)) reverse = SCATTER_REVERSE_LOCAL;

460:   switch (osm->loctype)
461:   {
462:   case PC_COMPOSITE_ADDITIVE:
463:     for (i=0; i<n_local; i++) {
464:       VecScatterBegin(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
465:     }
466:     VecZeroEntries(y);
467:     /* do the local solves */
468:     for (i=0; i<n_local_true; i++) {
469:       VecScatterEnd(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
470:       KSPSolve(osm->ksp[i],osm->x[i],osm->y[i]);
471:       if (osm->localization) {
472:         VecScatterBegin(osm->localization[i],osm->y[i],osm->y_local[i],INSERT_VALUES,forward);
473:         VecScatterEnd(osm->localization[i],osm->y[i],osm->y_local[i],INSERT_VALUES,forward);
474:       }
475:       VecScatterBegin(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
476:     }
477:     /* handle the rest of the scatters that do not have local solves */
478:     for (i=n_local_true; i<n_local; i++) {
479:       VecScatterEnd(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
480:       VecScatterBegin(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
481:     }
482:     for (i=0; i<n_local; i++) {
483:       VecScatterEnd(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
484:     }
485:     break;
486:   case PC_COMPOSITE_MULTIPLICATIVE:
487:     VecZeroEntries(y);
488:     /* do the local solves */
489:     for (i = 0; i < n_local_true; ++i) {
490:       if (i > 0) {
491:         /* Update rhs */
492:         VecScatterBegin(osm->restriction[i], osm->lx, osm->x[i], INSERT_VALUES, forward);
493:         VecScatterEnd(osm->restriction[i], osm->lx, osm->x[i], INSERT_VALUES, forward);
494:       } else {
495:         VecZeroEntries(osm->x[i]);
496:       }
497:       VecScatterBegin(osm->restriction[i], x, osm->x[i], ADD_VALUES, forward);
498:       VecScatterEnd(osm->restriction[i], x, osm->x[i], ADD_VALUES, forward);
499:       KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]);
500:       if (osm->localization) {
501:         VecScatterBegin(osm->localization[i], osm->y[i], osm->y_local[i], INSERT_VALUES, forward);
502:         VecScatterEnd(osm->localization[i], osm->y[i], osm->y_local[i], INSERT_VALUES, forward);
503:       }
504:       VecScatterBegin(osm->prolongation[i], osm->y_local[i], y, ADD_VALUES, reverse);
505:       VecScatterEnd(osm->prolongation[i], osm->y_local[i], y, ADD_VALUES, reverse);
506:       if (i < n_local_true-1) {
507:         VecSet(osm->ly, 0.0);
508:         VecScatterBegin(osm->prolongation[i], osm->y_local[i], osm->ly, INSERT_VALUES, reverse);
509:         VecScatterEnd(osm->prolongation[i], osm->y_local[i], osm->ly, INSERT_VALUES, reverse);
510:         VecScale(osm->ly, -1.0);
511:         MatMult(osm->lmats[i+1], osm->ly, osm->y[i+1]);
512:         VecScatterBegin(osm->restriction[i+1], osm->y[i+1], osm->lx, INSERT_VALUES, reverse);
513:         VecScatterEnd(osm->restriction[i+1], osm->y[i+1], osm->lx, INSERT_VALUES, reverse);
514:       }
515:     }
516:     /* handle the rest of the scatters that do not have local solves */
517:     for (i = n_local_true; i < n_local; ++i) {
518:       VecScatterBegin(osm->restriction[i], x, osm->x[i], INSERT_VALUES, forward);
519:       VecScatterEnd(osm->restriction[i], x, osm->x[i], INSERT_VALUES, forward);
520:       VecScatterBegin(osm->prolongation[i], osm->y_local[i], y, ADD_VALUES, reverse);
521:       VecScatterEnd(osm->prolongation[i], osm->y_local[i], y, ADD_VALUES, reverse);
522:     }
523:     break;
524:   default: SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Invalid local composition type: %s", PCCompositeTypes[osm->loctype]);
525:   }
526:   return(0);
527: }

531: static PetscErrorCode PCApplyTranspose_ASM(PC pc,Vec x,Vec y)
532: {
533:   PC_ASM         *osm = (PC_ASM*)pc->data;
535:   PetscInt       i,n_local = osm->n_local,n_local_true = osm->n_local_true;
536:   ScatterMode    forward = SCATTER_FORWARD,reverse = SCATTER_REVERSE;

539:   /*
540:      Support for limiting the restriction or interpolation to only local
541:      subdomain values (leaving the other values 0).

543:      Note: these are reversed from the PCApply_ASM() because we are applying the
544:      transpose of the three terms
545:   */
546:   if (!(osm->type & PC_ASM_INTERPOLATE)) {
547:     forward = SCATTER_FORWARD_LOCAL;
548:     /* have to zero the work RHS since scatter may leave some slots empty */
549:     for (i=0; i<n_local_true; i++) {
550:       VecZeroEntries(osm->x[i]);
551:     }
552:   }
553:   if (!(osm->type & PC_ASM_RESTRICT)) reverse = SCATTER_REVERSE_LOCAL;

555:   for (i=0; i<n_local; i++) {
556:     VecScatterBegin(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
557:   }
558:   VecZeroEntries(y);
559:   /* do the local solves */
560:   for (i=0; i<n_local_true; i++) {
561:     VecScatterEnd(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
562:     KSPSolveTranspose(osm->ksp[i],osm->x[i],osm->y[i]);
563:     if (osm->localization) {
564:       VecScatterBegin(osm->localization[i],osm->y[i],osm->y_local[i],INSERT_VALUES,forward);
565:       VecScatterEnd(osm->localization[i],osm->y[i],osm->y_local[i],INSERT_VALUES,forward);
566:     }
567:     VecScatterBegin(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
568:   }
569:   /* handle the rest of the scatters that do not have local solves */
570:   for (i=n_local_true; i<n_local; i++) {
571:     VecScatterEnd(osm->restriction[i],x,osm->x[i],INSERT_VALUES,forward);
572:     VecScatterBegin(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
573:   }
574:   for (i=0; i<n_local; i++) {
575:     VecScatterEnd(osm->prolongation[i],osm->y_local[i],y,ADD_VALUES,reverse);
576:   }
577:   return(0);
578: }

582: static PetscErrorCode PCReset_ASM(PC pc)
583: {
584:   PC_ASM         *osm = (PC_ASM*)pc->data;
586:   PetscInt       i;

589:   if (osm->ksp) {
590:     for (i=0; i<osm->n_local_true; i++) {
591:       KSPReset(osm->ksp[i]);
592:     }
593:   }
594:   if (osm->pmat) {
595:     if (osm->n_local_true > 0) {
596:       MatDestroyMatrices(osm->n_local_true,&osm->pmat);
597:     }
598:   }
599:   if (osm->restriction) {
600:     for (i=0; i<osm->n_local; i++) {
601:       VecScatterDestroy(&osm->restriction[i]);
602:       if (osm->localization) {VecScatterDestroy(&osm->localization[i]);}
603:       VecScatterDestroy(&osm->prolongation[i]);
604:       VecDestroy(&osm->x[i]);
605:       VecDestroy(&osm->y[i]);
606:       VecDestroy(&osm->y_local[i]);
607:     }
608:     PetscFree(osm->restriction);
609:     if (osm->localization) {PetscFree(osm->localization);}
610:     PetscFree(osm->prolongation);
611:     PetscFree(osm->x);
612:     PetscFree(osm->y);
613:     PetscFree(osm->y_local);
614:   }
615:   PCASMDestroySubdomains(osm->n_local_true,osm->is,osm->is_local);
616:   if (osm->loctype == PC_COMPOSITE_MULTIPLICATIVE) {
617:     ISDestroy(&osm->lis);
618:     MatDestroyMatrices(osm->n_local_true, &osm->lmats);
619:     VecDestroy(&osm->lx);
620:     VecDestroy(&osm->ly);
621:   }

623:   osm->is       = 0;
624:   osm->is_local = 0;
625:   return(0);
626: }

630: static PetscErrorCode PCDestroy_ASM(PC pc)
631: {
632:   PC_ASM         *osm = (PC_ASM*)pc->data;
634:   PetscInt       i;

637:   PCReset_ASM(pc);
638:   if (osm->ksp) {
639:     for (i=0; i<osm->n_local_true; i++) {
640:       KSPDestroy(&osm->ksp[i]);
641:     }
642:     PetscFree(osm->ksp);
643:   }
644:   PetscFree(pc->data);
645:   return(0);
646: }

650: static PetscErrorCode PCSetFromOptions_ASM(PetscOptionItems *PetscOptionsObject,PC pc)
651: {
652:   PC_ASM         *osm = (PC_ASM*)pc->data;
654:   PetscInt       blocks,ovl;
655:   PetscBool      symset,flg;
656:   PCASMType      asmtype;
657:   PCCompositeType loctype;

660:   /* set the type to symmetric if matrix is symmetric */
661:   if (!osm->type_set && pc->pmat) {
662:     MatIsSymmetricKnown(pc->pmat,&symset,&flg);
663:     if (symset && flg) osm->type = PC_ASM_BASIC;
664:   }
665:   PetscOptionsHead(PetscOptionsObject,"Additive Schwarz options");
666:   PetscOptionsBool("-pc_asm_dm_subdomains","Use DMCreateDomainDecomposition() to define subdomains","PCASMSetDMSubdomains",osm->dm_subdomains,&osm->dm_subdomains,&flg);
667:   PetscOptionsInt("-pc_asm_blocks","Number of subdomains","PCASMSetTotalSubdomains",osm->n,&blocks,&flg);
668:   if (flg) {
669:     PCASMSetTotalSubdomains(pc,blocks,NULL,NULL);
670:     osm->dm_subdomains = PETSC_FALSE;
671:   }
672:   PetscOptionsInt("-pc_asm_overlap","Number of grid points overlap","PCASMSetOverlap",osm->overlap,&ovl,&flg);
673:   if (flg) {
674:     PCASMSetOverlap(pc,ovl);
675:     osm->dm_subdomains = PETSC_FALSE;
676:   }
677:   flg  = PETSC_FALSE;
678:   PetscOptionsEnum("-pc_asm_type","Type of restriction/extension","PCASMSetType",PCASMTypes,(PetscEnum)osm->type,(PetscEnum*)&asmtype,&flg);
679:   if (flg) {PCASMSetType(pc,asmtype); }
680:   flg  = PETSC_FALSE;
681:   PetscOptionsEnum("-pc_asm_local_type","Type of local solver composition","PCASMSetLocalType",PCCompositeTypes,(PetscEnum)osm->loctype,(PetscEnum*)&loctype,&flg);
682:   if (flg) {PCASMSetLocalType(pc,loctype); }
683:   PetscOptionsTail();
684:   return(0);
685: }

687: /*------------------------------------------------------------------------------------*/

691: static PetscErrorCode  PCASMSetLocalSubdomains_ASM(PC pc,PetscInt n,IS is[],IS is_local[])
692: {
693:   PC_ASM         *osm = (PC_ASM*)pc->data;
695:   PetscInt       i;

698:   if (n < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Each process must have 1 or more blocks, n = %D",n);
699:   if (pc->setupcalled && (n != osm->n_local_true || is)) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"PCASMSetLocalSubdomains() should be called before calling PCSetUp().");

701:   if (!pc->setupcalled) {
702:     if (is) {
703:       for (i=0; i<n; i++) {PetscObjectReference((PetscObject)is[i]);}
704:     }
705:     if (is_local) {
706:       for (i=0; i<n; i++) {PetscObjectReference((PetscObject)is_local[i]);}
707:     }
708:     PCASMDestroySubdomains(osm->n_local_true,osm->is,osm->is_local);

710:     osm->n_local_true = n;
711:     osm->is           = 0;
712:     osm->is_local     = 0;
713:     if (is) {
714:       PetscMalloc1(n,&osm->is);
715:       for (i=0; i<n; i++) osm->is[i] = is[i];
716:       /* Flag indicating that the user has set overlapping subdomains so PCASM should not increase their size. */
717:       osm->overlap = -1;
718:     }
719:     if (is_local) {
720:       PetscMalloc1(n,&osm->is_local);
721:       for (i=0; i<n; i++) osm->is_local[i] = is_local[i];
722:       if (!is) {
723:         PetscMalloc1(osm->n_local_true,&osm->is);
724:         for (i=0; i<osm->n_local_true; i++) {
725:           if (osm->overlap > 0) { /* With positive overlap, osm->is[i] will be modified */
726:             ISDuplicate(osm->is_local[i],&osm->is[i]);
727:             ISCopy(osm->is_local[i],osm->is[i]);
728:           } else {
729:             PetscObjectReference((PetscObject)osm->is_local[i]);
730:             osm->is[i] = osm->is_local[i];
731:           }
732:         }
733:       }
734:     }
735:   }
736:   return(0);
737: }

741: static PetscErrorCode  PCASMSetTotalSubdomains_ASM(PC pc,PetscInt N,IS *is,IS *is_local)
742: {
743:   PC_ASM         *osm = (PC_ASM*)pc->data;
745:   PetscMPIInt    rank,size;
746:   PetscInt       n;

749:   if (N < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Number of total blocks must be > 0, N = %D",N);
750:   if (is || is_local) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Use PCASMSetLocalSubdomains() to set specific index sets\n\they cannot be set globally yet.");

752:   /*
753:      Split the subdomains equally among all processors
754:   */
755:   MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
756:   MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
757:   n    = N/size + ((N % size) > rank);
758:   if (!n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Process %d must have at least one block: total processors %d total blocks %D",(int)rank,(int)size,N);
759:   if (pc->setupcalled && n != osm->n_local_true) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"PCASMSetTotalSubdomains() should be called before PCSetUp().");
760:   if (!pc->setupcalled) {
761:     PCASMDestroySubdomains(osm->n_local_true,osm->is,osm->is_local);

763:     osm->n_local_true = n;
764:     osm->is           = 0;
765:     osm->is_local     = 0;
766:   }
767:   return(0);
768: }

772: static PetscErrorCode  PCASMSetOverlap_ASM(PC pc,PetscInt ovl)
773: {
774:   PC_ASM *osm = (PC_ASM*)pc->data;

777:   if (ovl < 0) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap value requested");
778:   if (pc->setupcalled && ovl != osm->overlap) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"PCASMSetOverlap() should be called before PCSetUp().");
779:   if (!pc->setupcalled) osm->overlap = ovl;
780:   return(0);
781: }

785: static PetscErrorCode  PCASMSetType_ASM(PC pc,PCASMType type)
786: {
787:   PC_ASM *osm = (PC_ASM*)pc->data;

790:   osm->type     = type;
791:   osm->type_set = PETSC_TRUE;
792:   return(0);
793: }

797: static PetscErrorCode  PCASMGetType_ASM(PC pc,PCASMType *type)
798: {
799:   PC_ASM *osm = (PC_ASM*)pc->data;

802:   *type = osm->type;
803:   return(0);
804: }

808: static PetscErrorCode  PCASMSetLocalType_ASM(PC pc, PCCompositeType type)
809: {
810:   PC_ASM *osm = (PC_ASM *) pc->data;

813:   osm->loctype = type;
814:   return(0);
815: }

819: static PetscErrorCode  PCASMGetLocalType_ASM(PC pc, PCCompositeType *type)
820: {
821:   PC_ASM *osm = (PC_ASM *) pc->data;

824:   *type = osm->loctype;
825:   return(0);
826: }

830: static PetscErrorCode  PCASMSetSortIndices_ASM(PC pc,PetscBool  doSort)
831: {
832:   PC_ASM *osm = (PC_ASM*)pc->data;

835:   osm->sort_indices = doSort;
836:   return(0);
837: }

841: static PetscErrorCode  PCASMGetSubKSP_ASM(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **ksp)
842: {
843:   PC_ASM         *osm = (PC_ASM*)pc->data;

847:   if (osm->n_local_true < 1) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Need to call PCSetUP() on PC (or KSPSetUp() on the outer KSP object) before calling here");

849:   if (n_local) *n_local = osm->n_local_true;
850:   if (first_local) {
851:     MPI_Scan(&osm->n_local_true,first_local,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));
852:     *first_local -= osm->n_local_true;
853:   }
854:   if (ksp) {
855:     /* Assume that local solves are now different; not necessarily
856:        true though!  This flag is used only for PCView_ASM() */
857:     *ksp                   = osm->ksp;
858:     osm->same_local_solves = PETSC_FALSE;
859:   }
860:   return(0);
861: }

865: /*@C
866:     PCASMSetLocalSubdomains - Sets the local subdomains (for this processor only) for the additive Schwarz preconditioner.

868:     Collective on PC

870:     Input Parameters:
871: +   pc - the preconditioner context
872: .   n - the number of subdomains for this processor (default value = 1)
873: .   is - the index set that defines the subdomains for this processor
874:          (or NULL for PETSc to determine subdomains)
875: -   is_local - the index sets that define the local part of the subdomains for this processor
876:          (or NULL to use the default of 1 subdomain per process)

878:     Notes:
879:     The IS numbering is in the parallel, global numbering of the vector for both is and is_local

881:     By default the ASM preconditioner uses 1 block per processor.

883:     Use PCASMSetTotalSubdomains() to set the subdomains for all processors.

885:     Level: advanced

887: .keywords: PC, ASM, set, local, subdomains, additive Schwarz

889: .seealso: PCASMSetTotalSubdomains(), PCASMSetOverlap(), PCASMGetSubKSP(),
890:           PCASMCreateSubdomains2D(), PCASMGetLocalSubdomains()
891: @*/
892: PetscErrorCode  PCASMSetLocalSubdomains(PC pc,PetscInt n,IS is[],IS is_local[])
893: {

898:   PetscTryMethod(pc,"PCASMSetLocalSubdomains_C",(PC,PetscInt,IS[],IS[]),(pc,n,is,is_local));
899:   return(0);
900: }

904: /*@C
905:     PCASMSetTotalSubdomains - Sets the subdomains for all processors for the
906:     additive Schwarz preconditioner.  Either all or no processors in the
907:     PC communicator must call this routine, with the same index sets.

909:     Collective on PC

911:     Input Parameters:
912: +   pc - the preconditioner context
913: .   N  - the number of subdomains for all processors
914: .   is - the index sets that define the subdomains for all processors
915:          (or NULL to ask PETSc to compe up with subdomains)
916: -   is_local - the index sets that define the local part of the subdomains for this processor
917:          (or NULL to use the default of 1 subdomain per process)

919:     Options Database Key:
920:     To set the total number of subdomain blocks rather than specify the
921:     index sets, use the option
922: .    -pc_asm_blocks <blks> - Sets total blocks

924:     Notes:
925:     Currently you cannot use this to set the actual subdomains with the argument is.

927:     By default the ASM preconditioner uses 1 block per processor.

929:     These index sets cannot be destroyed until after completion of the
930:     linear solves for which the ASM preconditioner is being used.

932:     Use PCASMSetLocalSubdomains() to set local subdomains.

934:     The IS numbering is in the parallel, global numbering of the vector for both is and is_local

936:     Level: advanced

938: .keywords: PC, ASM, set, total, global, subdomains, additive Schwarz

940: .seealso: PCASMSetLocalSubdomains(), PCASMSetOverlap(), PCASMGetSubKSP(),
941:           PCASMCreateSubdomains2D()
942: @*/
943: PetscErrorCode  PCASMSetTotalSubdomains(PC pc,PetscInt N,IS is[],IS is_local[])
944: {

949:   PetscTryMethod(pc,"PCASMSetTotalSubdomains_C",(PC,PetscInt,IS[],IS[]),(pc,N,is,is_local));
950:   return(0);
951: }

955: /*@
956:     PCASMSetOverlap - Sets the overlap between a pair of subdomains for the
957:     additive Schwarz preconditioner.  Either all or no processors in the
958:     PC communicator must call this routine. If MatIncreaseOverlap is used,
959:     use option -mat_increase_overlap when the problem size large.

961:     Logically Collective on PC

963:     Input Parameters:
964: +   pc  - the preconditioner context
965: -   ovl - the amount of overlap between subdomains (ovl >= 0, default value = 1)

967:     Options Database Key:
968: .   -pc_asm_overlap <ovl> - Sets overlap

970:     Notes:
971:     By default the ASM preconditioner uses 1 block per processor.  To use
972:     multiple blocks per perocessor, see PCASMSetTotalSubdomains() and
973:     PCASMSetLocalSubdomains() (and the option -pc_asm_blocks <blks>).

975:     The overlap defaults to 1, so if one desires that no additional
976:     overlap be computed beyond what may have been set with a call to
977:     PCASMSetTotalSubdomains() or PCASMSetLocalSubdomains(), then ovl
978:     must be set to be 0.  In particular, if one does not explicitly set
979:     the subdomains an application code, then all overlap would be computed
980:     internally by PETSc, and using an overlap of 0 would result in an ASM
981:     variant that is equivalent to the block Jacobi preconditioner.

983:     Note that one can define initial index sets with any overlap via
984:     PCASMSetTotalSubdomains() or PCASMSetLocalSubdomains(); the routine
985:     PCASMSetOverlap() merely allows PETSc to extend that overlap further
986:     if desired.

988:     Level: intermediate

990: .keywords: PC, ASM, set, overlap

992: .seealso: PCASMSetTotalSubdomains(), PCASMSetLocalSubdomains(), PCASMGetSubKSP(),
993:           PCASMCreateSubdomains2D(), PCASMGetLocalSubdomains()
994: @*/
995: PetscErrorCode  PCASMSetOverlap(PC pc,PetscInt ovl)
996: {

1002:   PetscTryMethod(pc,"PCASMSetOverlap_C",(PC,PetscInt),(pc,ovl));
1003:   return(0);
1004: }

1008: /*@
1009:     PCASMSetType - Sets the type of restriction and interpolation used
1010:     for local problems in the additive Schwarz method.

1012:     Logically Collective on PC

1014:     Input Parameters:
1015: +   pc  - the preconditioner context
1016: -   type - variant of ASM, one of
1017: .vb
1018:       PC_ASM_BASIC       - full interpolation and restriction
1019:       PC_ASM_RESTRICT    - full restriction, local processor interpolation
1020:       PC_ASM_INTERPOLATE - full interpolation, local processor restriction
1021:       PC_ASM_NONE        - local processor restriction and interpolation
1022: .ve

1024:     Options Database Key:
1025: .   -pc_asm_type [basic,restrict,interpolate,none] - Sets ASM type

1027:     Level: intermediate

1029: .keywords: PC, ASM, set, type

1031: .seealso: PCASMSetTotalSubdomains(), PCASMSetTotalSubdomains(), PCASMGetSubKSP(),
1032:           PCASMCreateSubdomains2D()
1033: @*/
1034: PetscErrorCode  PCASMSetType(PC pc,PCASMType type)
1035: {

1041:   PetscTryMethod(pc,"PCASMSetType_C",(PC,PCASMType),(pc,type));
1042:   return(0);
1043: }

1047: /*@
1048:     PCASMGetType - Gets the type of restriction and interpolation used
1049:     for local problems in the additive Schwarz method.

1051:     Logically Collective on PC

1053:     Input Parameter:
1054: .   pc  - the preconditioner context

1056:     Output Parameter:
1057: .   type - variant of ASM, one of

1059: .vb
1060:       PC_ASM_BASIC       - full interpolation and restriction
1061:       PC_ASM_RESTRICT    - full restriction, local processor interpolation
1062:       PC_ASM_INTERPOLATE - full interpolation, local processor restriction
1063:       PC_ASM_NONE        - local processor restriction and interpolation
1064: .ve

1066:     Options Database Key:
1067: .   -pc_asm_type [basic,restrict,interpolate,none] - Sets ASM type

1069:     Level: intermediate

1071: .keywords: PC, ASM, set, type

1073: .seealso: PCASMSetTotalSubdomains(), PCASMSetTotalSubdomains(), PCASMGetSubKSP(),
1074:           PCASMCreateSubdomains2D()
1075: @*/
1076: PetscErrorCode  PCASMGetType(PC pc,PCASMType *type)
1077: {

1082:   PetscUseMethod(pc,"PCASMGetType_C",(PC,PCASMType*),(pc,type));
1083:   return(0);
1084: }

1088: /*@
1089:   PCASMSetLocalType - Sets the type of composition used for local problems in the additive Schwarz method.

1091:   Logically Collective on PC

1093:   Input Parameters:
1094: + pc  - the preconditioner context
1095: - type - type of composition, one of
1096: .vb
1097:   PC_COMPOSITE_ADDITIVE       - local additive combination
1098:   PC_COMPOSITE_MULTIPLICATIVE - local multiplicative combination
1099: .ve

1101:   Options Database Key:
1102: . -pc_asm_local_type [additive,multiplicative] - Sets local solver composition type

1104:   Level: intermediate

1106: .seealso: PCASMSetType(), PCASMGetType(), PCASMGetLocalType(), PCASMCreate()
1107: @*/
1108: PetscErrorCode PCASMSetLocalType(PC pc, PCCompositeType type)
1109: {

1115:   PetscTryMethod(pc, "PCASMSetLocalType_C", (PC, PCCompositeType), (pc, type));
1116:   return(0);
1117: }

1121: /*@
1122:   PCASMGetLocalType - Gets the type of composition used for local problems in the additive Schwarz method.

1124:   Logically Collective on PC

1126:   Input Parameter:
1127: . pc  - the preconditioner context

1129:   Output Parameter:
1130: . type - type of composition, one of
1131: .vb
1132:   PC_COMPOSITE_ADDITIVE       - local additive combination
1133:   PC_COMPOSITE_MULTIPLICATIVE - local multiplicative combination
1134: .ve

1136:   Options Database Key:
1137: . -pc_asm_local_type [additive,multiplicative] - Sets local solver composition type

1139:   Level: intermediate

1141: .seealso: PCASMSetType(), PCASMGetType(), PCASMSetLocalType(), PCASMCreate()
1142: @*/
1143: PetscErrorCode PCASMGetLocalType(PC pc, PCCompositeType *type)
1144: {

1150:   PetscUseMethod(pc, "PCASMGetLocalType_C", (PC, PCCompositeType *), (pc, type));
1151:   return(0);
1152: }

1156: /*@
1157:     PCASMSetSortIndices - Determines whether subdomain indices are sorted.

1159:     Logically Collective on PC

1161:     Input Parameters:
1162: +   pc  - the preconditioner context
1163: -   doSort - sort the subdomain indices

1165:     Level: intermediate

1167: .keywords: PC, ASM, set, type

1169: .seealso: PCASMSetLocalSubdomains(), PCASMSetTotalSubdomains(), PCASMGetSubKSP(),
1170:           PCASMCreateSubdomains2D()
1171: @*/
1172: PetscErrorCode  PCASMSetSortIndices(PC pc,PetscBool doSort)
1173: {

1179:   PetscTryMethod(pc,"PCASMSetSortIndices_C",(PC,PetscBool),(pc,doSort));
1180:   return(0);
1181: }

1185: /*@C
1186:    PCASMGetSubKSP - Gets the local KSP contexts for all blocks on
1187:    this processor.

1189:    Collective on PC iff first_local is requested

1191:    Input Parameter:
1192: .  pc - the preconditioner context

1194:    Output Parameters:
1195: +  n_local - the number of blocks on this processor or NULL
1196: .  first_local - the global number of the first block on this processor or NULL,
1197:                  all processors must request or all must pass NULL
1198: -  ksp - the array of KSP contexts

1200:    Note:
1201:    After PCASMGetSubKSP() the array of KSPes is not to be freed.

1203:    Currently for some matrix implementations only 1 block per processor
1204:    is supported.

1206:    You must call KSPSetUp() before calling PCASMGetSubKSP().

1208:    Fortran note:
1209:    The output argument 'ksp' must be an array of sufficient length or NULL_OBJECT. The latter can be used to learn the necessary length.

1211:    Level: advanced

1213: .keywords: PC, ASM, additive Schwarz, get, sub, KSP, context

1215: .seealso: PCASMSetTotalSubdomains(), PCASMSetTotalSubdomains(), PCASMSetOverlap(),
1216:           PCASMCreateSubdomains2D(),
1217: @*/
1218: PetscErrorCode  PCASMGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP *ksp[])
1219: {

1224:   PetscUseMethod(pc,"PCASMGetSubKSP_C",(PC,PetscInt*,PetscInt*,KSP **),(pc,n_local,first_local,ksp));
1225:   return(0);
1226: }

1228: /* -------------------------------------------------------------------------------------*/
1229: /*MC
1230:    PCASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
1231:            its own KSP object.

1233:    Options Database Keys:
1234: +  -pc_asm_blocks <blks> - Sets total blocks
1235: .  -pc_asm_overlap <ovl> - Sets overlap
1236: -  -pc_asm_type [basic,restrict,interpolate,none] - Sets ASM type

1238:      IMPORTANT: If you run with, for example, 3 blocks on 1 processor or 3 blocks on 3 processors you
1239:       will get a different convergence rate due to the default option of -pc_asm_type restrict. Use
1240:       -pc_asm_type basic to use the standard ASM.

1242:    Notes: Each processor can have one or more blocks, but a block cannot be shared by more
1243:      than one processor. Defaults to one block per processor.

1245:      To set options on the solvers for each block append -sub_ to all the KSP, and PC
1246:         options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly

1248:      To set the options on the solvers separate for each block call PCASMGetSubKSP()
1249:          and set the options directly on the resulting KSP object (you can access its PC
1250:          with KSPGetPC())


1253:    Level: beginner

1255:    Concepts: additive Schwarz method

1257:     References:
1258: +   1. - M Dryja, OB Widlund, An additive variant of the Schwarz alternating method for the case of many subregions
1259:      Courant Institute, New York University Technical report
1260: -   1. - Barry Smith, Petter Bjorstad, and William Gropp, Domain Decompositions: Parallel Multilevel Methods for Elliptic Partial Differential Equations,
1261:     Cambridge University Press.

1263: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
1264:            PCBJACOBI, PCASMGetSubKSP(), PCASMSetLocalSubdomains(),
1265:            PCASMSetTotalSubdomains(), PCSetModifySubmatrices(), PCASMSetOverlap(), PCASMSetType()

1267: M*/

1271: PETSC_EXTERN PetscErrorCode PCCreate_ASM(PC pc)
1272: {
1274:   PC_ASM         *osm;

1277:   PetscNewLog(pc,&osm);

1279:   osm->n                 = PETSC_DECIDE;
1280:   osm->n_local           = 0;
1281:   osm->n_local_true      = PETSC_DECIDE;
1282:   osm->overlap           = 1;
1283:   osm->ksp               = 0;
1284:   osm->restriction       = 0;
1285:   osm->localization      = 0;
1286:   osm->prolongation      = 0;
1287:   osm->x                 = 0;
1288:   osm->y                 = 0;
1289:   osm->y_local           = 0;
1290:   osm->is                = 0;
1291:   osm->is_local          = 0;
1292:   osm->mat               = 0;
1293:   osm->pmat              = 0;
1294:   osm->type              = PC_ASM_RESTRICT;
1295:   osm->loctype           = PC_COMPOSITE_ADDITIVE;
1296:   osm->same_local_solves = PETSC_TRUE;
1297:   osm->sort_indices      = PETSC_TRUE;
1298:   osm->dm_subdomains     = PETSC_FALSE;

1300:   pc->data                 = (void*)osm;
1301:   pc->ops->apply           = PCApply_ASM;
1302:   pc->ops->applytranspose  = PCApplyTranspose_ASM;
1303:   pc->ops->setup           = PCSetUp_ASM;
1304:   pc->ops->reset           = PCReset_ASM;
1305:   pc->ops->destroy         = PCDestroy_ASM;
1306:   pc->ops->setfromoptions  = PCSetFromOptions_ASM;
1307:   pc->ops->setuponblocks   = PCSetUpOnBlocks_ASM;
1308:   pc->ops->view            = PCView_ASM;
1309:   pc->ops->applyrichardson = 0;

1311:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetLocalSubdomains_C",PCASMSetLocalSubdomains_ASM);
1312:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetTotalSubdomains_C",PCASMSetTotalSubdomains_ASM);
1313:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetOverlap_C",PCASMSetOverlap_ASM);
1314:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetType_C",PCASMSetType_ASM);
1315:   PetscObjectComposeFunction((PetscObject)pc,"PCASMGetType_C",PCASMGetType_ASM);
1316:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetLocalType_C",PCASMSetLocalType_ASM);
1317:   PetscObjectComposeFunction((PetscObject)pc,"PCASMGetLocalType_C",PCASMGetLocalType_ASM);
1318:   PetscObjectComposeFunction((PetscObject)pc,"PCASMSetSortIndices_C",PCASMSetSortIndices_ASM);
1319:   PetscObjectComposeFunction((PetscObject)pc,"PCASMGetSubKSP_C",PCASMGetSubKSP_ASM);
1320:   return(0);
1321: }

1325: /*@C
1326:    PCASMCreateSubdomains - Creates the index sets for the overlapping Schwarz
1327:    preconditioner for a any problem on a general grid.

1329:    Collective

1331:    Input Parameters:
1332: +  A - The global matrix operator
1333: -  n - the number of local blocks

1335:    Output Parameters:
1336: .  outis - the array of index sets defining the subdomains

1338:    Level: advanced

1340:    Note: this generates nonoverlapping subdomains; the PCASM will generate the overlap
1341:     from these if you use PCASMSetLocalSubdomains()

1343:     In the Fortran version you must provide the array outis[] already allocated of length n.

1345: .keywords: PC, ASM, additive Schwarz, create, subdomains, unstructured grid

1347: .seealso: PCASMSetLocalSubdomains(), PCASMDestroySubdomains()
1348: @*/
1349: PetscErrorCode  PCASMCreateSubdomains(Mat A, PetscInt n, IS* outis[])
1350: {
1351:   MatPartitioning mpart;
1352:   const char      *prefix;
1353:   PetscErrorCode  (*f)(Mat,Mat*);
1354:   PetscMPIInt     size;
1355:   PetscInt        i,j,rstart,rend,bs;
1356:   PetscBool       isbaij = PETSC_FALSE,foundpart = PETSC_FALSE;
1357:   Mat             Ad     = NULL, adj;
1358:   IS              ispart,isnumb,*is;
1359:   PetscErrorCode  ierr;

1364:   if (n < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"number of local blocks must be > 0, n = %D",n);

1366:   /* Get prefix, row distribution, and block size */
1367:   MatGetOptionsPrefix(A,&prefix);
1368:   MatGetOwnershipRange(A,&rstart,&rend);
1369:   MatGetBlockSize(A,&bs);
1370:   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);

1372:   /* Get diagonal block from matrix if possible */
1373:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
1374:   PetscObjectQueryFunction((PetscObject)A,"MatGetDiagonalBlock_C",&f);
1375:   if (f) {
1376:     MatGetDiagonalBlock(A,&Ad);
1377:   } else if (size == 1) {
1378:     Ad = A;
1379:   }
1380:   if (Ad) {
1381:     PetscObjectTypeCompare((PetscObject)Ad,MATSEQBAIJ,&isbaij);
1382:     if (!isbaij) {PetscObjectTypeCompare((PetscObject)Ad,MATSEQSBAIJ,&isbaij);}
1383:   }
1384:   if (Ad && n > 1) {
1385:     PetscBool match,done;
1386:     /* Try to setup a good matrix partitioning if available */
1387:     MatPartitioningCreate(PETSC_COMM_SELF,&mpart);
1388:     PetscObjectSetOptionsPrefix((PetscObject)mpart,prefix);
1389:     MatPartitioningSetFromOptions(mpart);
1390:     PetscObjectTypeCompare((PetscObject)mpart,MATPARTITIONINGCURRENT,&match);
1391:     if (!match) {
1392:       PetscObjectTypeCompare((PetscObject)mpart,MATPARTITIONINGSQUARE,&match);
1393:     }
1394:     if (!match) { /* assume a "good" partitioner is available */
1395:       PetscInt       na;
1396:       const PetscInt *ia,*ja;
1397:       MatGetRowIJ(Ad,0,PETSC_TRUE,isbaij,&na,&ia,&ja,&done);
1398:       if (done) {
1399:         /* Build adjacency matrix by hand. Unfortunately a call to
1400:            MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1401:            remove the block-aij structure and we cannot expect
1402:            MatPartitioning to split vertices as we need */
1403:         PetscInt       i,j,len,nnz,cnt,*iia=0,*jja=0;
1404:         const PetscInt *row;
1405:         nnz = 0;
1406:         for (i=0; i<na; i++) { /* count number of nonzeros */
1407:           len = ia[i+1] - ia[i];
1408:           row = ja + ia[i];
1409:           for (j=0; j<len; j++) {
1410:             if (row[j] == i) { /* don't count diagonal */
1411:               len--; break;
1412:             }
1413:           }
1414:           nnz += len;
1415:         }
1416:         PetscMalloc1(na+1,&iia);
1417:         PetscMalloc1(nnz,&jja);
1418:         nnz    = 0;
1419:         iia[0] = 0;
1420:         for (i=0; i<na; i++) { /* fill adjacency */
1421:           cnt = 0;
1422:           len = ia[i+1] - ia[i];
1423:           row = ja + ia[i];
1424:           for (j=0; j<len; j++) {
1425:             if (row[j] != i) { /* if not diagonal */
1426:               jja[nnz+cnt++] = row[j];
1427:             }
1428:           }
1429:           nnz     += cnt;
1430:           iia[i+1] = nnz;
1431:         }
1432:         /* Partitioning of the adjacency matrix */
1433:         MatCreateMPIAdj(PETSC_COMM_SELF,na,na,iia,jja,NULL,&adj);
1434:         MatPartitioningSetAdjacency(mpart,adj);
1435:         MatPartitioningSetNParts(mpart,n);
1436:         MatPartitioningApply(mpart,&ispart);
1437:         ISPartitioningToNumbering(ispart,&isnumb);
1438:         MatDestroy(&adj);
1439:         foundpart = PETSC_TRUE;
1440:       }
1441:       MatRestoreRowIJ(Ad,0,PETSC_TRUE,isbaij,&na,&ia,&ja,&done);
1442:     }
1443:     MatPartitioningDestroy(&mpart);
1444:   }

1446:   PetscMalloc1(n,&is);
1447:   *outis = is;

1449:   if (!foundpart) {

1451:     /* Partitioning by contiguous chunks of rows */

1453:     PetscInt mbs   = (rend-rstart)/bs;
1454:     PetscInt start = rstart;
1455:     for (i=0; i<n; i++) {
1456:       PetscInt count = (mbs/n + ((mbs % n) > i)) * bs;
1457:       ISCreateStride(PETSC_COMM_SELF,count,start,1,&is[i]);
1458:       start += count;
1459:     }

1461:   } else {

1463:     /* Partitioning by adjacency of diagonal block  */

1465:     const PetscInt *numbering;
1466:     PetscInt       *count,nidx,*indices,*newidx,start=0;
1467:     /* Get node count in each partition */
1468:     PetscMalloc1(n,&count);
1469:     ISPartitioningCount(ispart,n,count);
1470:     if (isbaij && bs > 1) { /* adjust for the block-aij case */
1471:       for (i=0; i<n; i++) count[i] *= bs;
1472:     }
1473:     /* Build indices from node numbering */
1474:     ISGetLocalSize(isnumb,&nidx);
1475:     PetscMalloc1(nidx,&indices);
1476:     for (i=0; i<nidx; i++) indices[i] = i; /* needs to be initialized */
1477:     ISGetIndices(isnumb,&numbering);
1478:     PetscSortIntWithPermutation(nidx,numbering,indices);
1479:     ISRestoreIndices(isnumb,&numbering);
1480:     if (isbaij && bs > 1) { /* adjust for the block-aij case */
1481:       PetscMalloc1(nidx*bs,&newidx);
1482:       for (i=0; i<nidx; i++) {
1483:         for (j=0; j<bs; j++) newidx[i*bs+j] = indices[i]*bs + j;
1484:       }
1485:       PetscFree(indices);
1486:       nidx   *= bs;
1487:       indices = newidx;
1488:     }
1489:     /* Shift to get global indices */
1490:     for (i=0; i<nidx; i++) indices[i] += rstart;

1492:     /* Build the index sets for each block */
1493:     for (i=0; i<n; i++) {
1494:       ISCreateGeneral(PETSC_COMM_SELF,count[i],&indices[start],PETSC_COPY_VALUES,&is[i]);
1495:       ISSort(is[i]);
1496:       start += count[i];
1497:     }

1499:     PetscFree(count);
1500:     PetscFree(indices);
1501:     ISDestroy(&isnumb);
1502:     ISDestroy(&ispart);

1504:   }
1505:   return(0);
1506: }

1510: /*@C
1511:    PCASMDestroySubdomains - Destroys the index sets created with
1512:    PCASMCreateSubdomains(). Should be called after setting subdomains
1513:    with PCASMSetLocalSubdomains().

1515:    Collective

1517:    Input Parameters:
1518: +  n - the number of index sets
1519: .  is - the array of index sets
1520: -  is_local - the array of local index sets, can be NULL

1522:    Level: advanced

1524: .keywords: PC, ASM, additive Schwarz, create, subdomains, unstructured grid

1526: .seealso: PCASMCreateSubdomains(), PCASMSetLocalSubdomains()
1527: @*/
1528: PetscErrorCode  PCASMDestroySubdomains(PetscInt n, IS is[], IS is_local[])
1529: {
1530:   PetscInt       i;

1534:   if (n <= 0) return(0);
1535:   if (is) {
1537:     for (i=0; i<n; i++) { ISDestroy(&is[i]); }
1538:     PetscFree(is);
1539:   }
1540:   if (is_local) {
1542:     for (i=0; i<n; i++) { ISDestroy(&is_local[i]); }
1543:     PetscFree(is_local);
1544:   }
1545:   return(0);
1546: }

1550: /*@
1551:    PCASMCreateSubdomains2D - Creates the index sets for the overlapping Schwarz
1552:    preconditioner for a two-dimensional problem on a regular grid.

1554:    Not Collective

1556:    Input Parameters:
1557: +  m, n - the number of mesh points in the x and y directions
1558: .  M, N - the number of subdomains in the x and y directions
1559: .  dof - degrees of freedom per node
1560: -  overlap - overlap in mesh lines

1562:    Output Parameters:
1563: +  Nsub - the number of subdomains created
1564: .  is - array of index sets defining overlapping (if overlap > 0) subdomains
1565: -  is_local - array of index sets defining non-overlapping subdomains

1567:    Note:
1568:    Presently PCAMSCreateSubdomains2d() is valid only for sequential
1569:    preconditioners.  More general related routines are
1570:    PCASMSetTotalSubdomains() and PCASMSetLocalSubdomains().

1572:    Level: advanced

1574: .keywords: PC, ASM, additive Schwarz, create, subdomains, 2D, regular grid

1576: .seealso: PCASMSetTotalSubdomains(), PCASMSetLocalSubdomains(), PCASMGetSubKSP(),
1577:           PCASMSetOverlap()
1578: @*/
1579: PetscErrorCode  PCASMCreateSubdomains2D(PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt dof,PetscInt overlap,PetscInt *Nsub,IS **is,IS **is_local)
1580: {
1581:   PetscInt       i,j,height,width,ystart,xstart,yleft,yright,xleft,xright,loc_outer;
1583:   PetscInt       nidx,*idx,loc,ii,jj,count;

1586:   if (dof != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP," ");

1588:   *Nsub     = N*M;
1589:   PetscMalloc1(*Nsub,is);
1590:   PetscMalloc1(*Nsub,is_local);
1591:   ystart    = 0;
1592:   loc_outer = 0;
1593:   for (i=0; i<N; i++) {
1594:     height = n/N + ((n % N) > i); /* height of subdomain */
1595:     if (height < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many N subdomains for mesh dimension n");
1596:     yleft  = ystart - overlap; if (yleft < 0) yleft = 0;
1597:     yright = ystart + height + overlap; if (yright > n) yright = n;
1598:     xstart = 0;
1599:     for (j=0; j<M; j++) {
1600:       width = m/M + ((m % M) > j); /* width of subdomain */
1601:       if (width < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many M subdomains for mesh dimension m");
1602:       xleft  = xstart - overlap; if (xleft < 0) xleft = 0;
1603:       xright = xstart + width + overlap; if (xright > m) xright = m;
1604:       nidx   = (xright - xleft)*(yright - yleft);
1605:       PetscMalloc1(nidx,&idx);
1606:       loc    = 0;
1607:       for (ii=yleft; ii<yright; ii++) {
1608:         count = m*ii + xleft;
1609:         for (jj=xleft; jj<xright; jj++) idx[loc++] = count++;
1610:       }
1611:       ISCreateGeneral(PETSC_COMM_SELF,nidx,idx,PETSC_COPY_VALUES,(*is)+loc_outer);
1612:       if (overlap == 0) {
1613:         PetscObjectReference((PetscObject)(*is)[loc_outer]);

1615:         (*is_local)[loc_outer] = (*is)[loc_outer];
1616:       } else {
1617:         for (loc=0,ii=ystart; ii<ystart+height; ii++) {
1618:           for (jj=xstart; jj<xstart+width; jj++) {
1619:             idx[loc++] = m*ii + jj;
1620:           }
1621:         }
1622:         ISCreateGeneral(PETSC_COMM_SELF,loc,idx,PETSC_COPY_VALUES,*is_local+loc_outer);
1623:       }
1624:       PetscFree(idx);
1625:       xstart += width;
1626:       loc_outer++;
1627:     }
1628:     ystart += height;
1629:   }
1630:   for (i=0; i<*Nsub; i++) { ISSort((*is)[i]); }
1631:   return(0);
1632: }

1636: /*@C
1637:     PCASMGetLocalSubdomains - Gets the local subdomains (for this processor
1638:     only) for the additive Schwarz preconditioner.

1640:     Not Collective

1642:     Input Parameter:
1643: .   pc - the preconditioner context

1645:     Output Parameters:
1646: +   n - the number of subdomains for this processor (default value = 1)
1647: .   is - the index sets that define the subdomains for this processor
1648: -   is_local - the index sets that define the local part of the subdomains for this processor (can be NULL)


1651:     Notes:
1652:     The IS numbering is in the parallel, global numbering of the vector.

1654:     Level: advanced

1656: .keywords: PC, ASM, set, local, subdomains, additive Schwarz

1658: .seealso: PCASMSetTotalSubdomains(), PCASMSetOverlap(), PCASMGetSubKSP(),
1659:           PCASMCreateSubdomains2D(), PCASMSetLocalSubdomains(), PCASMGetLocalSubmatrices()
1660: @*/
1661: PetscErrorCode  PCASMGetLocalSubdomains(PC pc,PetscInt *n,IS *is[],IS *is_local[])
1662: {
1663:   PC_ASM         *osm = (PC_ASM*)pc->data;
1665:   PetscBool      match;

1671:   PetscObjectTypeCompare((PetscObject)pc,PCASM,&match);
1672:   if (!match) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"PC is not a PCASM");
1673:   if (n) *n = osm->n_local_true;
1674:   if (is) *is = osm->is;
1675:   if (is_local) *is_local = osm->is_local;
1676:   return(0);
1677: }

1681: /*@C
1682:     PCASMGetLocalSubmatrices - Gets the local submatrices (for this processor
1683:     only) for the additive Schwarz preconditioner.

1685:     Not Collective

1687:     Input Parameter:
1688: .   pc - the preconditioner context

1690:     Output Parameters:
1691: +   n - the number of matrices for this processor (default value = 1)
1692: -   mat - the matrices


1695:     Level: advanced

1697:     Notes: Call after PCSetUp() (or KSPSetUp()) but before PCApply() (or KSPApply()) and before PCSetUpOnBlocks())

1699:            Usually one would use PCSetModifySubmatrices() to change the submatrices in building the preconditioner.

1701: .keywords: PC, ASM, set, local, subdomains, additive Schwarz, block Jacobi

1703: .seealso: PCASMSetTotalSubdomains(), PCASMSetOverlap(), PCASMGetSubKSP(),
1704:           PCASMCreateSubdomains2D(), PCASMSetLocalSubdomains(), PCASMGetLocalSubdomains(), PCSetModifySubmatrices()
1705: @*/
1706: PetscErrorCode  PCASMGetLocalSubmatrices(PC pc,PetscInt *n,Mat *mat[])
1707: {
1708:   PC_ASM         *osm;
1710:   PetscBool      match;

1716:   if (!pc->setupcalled) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call after KSPSetUP() or PCSetUp().");
1717:   PetscObjectTypeCompare((PetscObject)pc,PCASM,&match);
1718:   if (!match) {
1719:     if (n) *n = 0;
1720:     if (mat) *mat = NULL;
1721:   } else {
1722:     osm = (PC_ASM*)pc->data;
1723:     if (n) *n = osm->n_local_true;
1724:     if (mat) *mat = osm->pmat;
1725:   }
1726:   return(0);
1727: }

1731: /*@
1732:     PCASMSetDMSubdomains - Indicates whether to use DMCreateDomainDecomposition() to define the subdomains, whenever possible.
1733:     Logically Collective

1735:     Input Parameter:
1736: +   pc  - the preconditioner
1737: -   flg - boolean indicating whether to use subdomains defined by the DM

1739:     Options Database Key:
1740: .   -pc_asm_dm_subdomains

1742:     Level: intermediate

1744:     Notes:
1745:     PCASMSetTotalSubdomains() and PCASMSetOverlap() take precedence over PCASMSetDMSubdomains(),
1746:     so setting either of the first two effectively turns the latter off.

1748: .keywords: PC, ASM, DM, set, subdomains, additive Schwarz

1750: .seealso: PCASMGetDMSubdomains(), PCASMSetTotalSubdomains(), PCASMSetOverlap()
1751:           PCASMCreateSubdomains2D(), PCASMSetLocalSubdomains(), PCASMGetLocalSubdomains()
1752: @*/
1753: PetscErrorCode  PCASMSetDMSubdomains(PC pc,PetscBool flg)
1754: {
1755:   PC_ASM         *osm = (PC_ASM*)pc->data;
1757:   PetscBool      match;

1762:   if (pc->setupcalled) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for a setup PC.");
1763:   PetscObjectTypeCompare((PetscObject)pc,PCASM,&match);
1764:   if (match) {
1765:     osm->dm_subdomains = flg;
1766:   }
1767:   return(0);
1768: }

1772: /*@
1773:     PCASMGetDMSubdomains - Returns flag indicating whether to use DMCreateDomainDecomposition() to define the subdomains, whenever possible.
1774:     Not Collective

1776:     Input Parameter:
1777: .   pc  - the preconditioner

1779:     Output Parameter:
1780: .   flg - boolean indicating whether to use subdomains defined by the DM

1782:     Level: intermediate

1784: .keywords: PC, ASM, DM, set, subdomains, additive Schwarz

1786: .seealso: PCASMSetDMSubdomains(), PCASMSetTotalSubdomains(), PCASMSetOverlap()
1787:           PCASMCreateSubdomains2D(), PCASMSetLocalSubdomains(), PCASMGetLocalSubdomains()
1788: @*/
1789: PetscErrorCode  PCASMGetDMSubdomains(PC pc,PetscBool* flg)
1790: {
1791:   PC_ASM         *osm = (PC_ASM*)pc->data;
1793:   PetscBool      match;

1798:   PetscObjectTypeCompare((PetscObject)pc,PCASM,&match);
1799:   if (match) {
1800:     if (flg) *flg = osm->dm_subdomains;
1801:   }
1802:   return(0);
1803: }