Actual source code: plexdistribute.c

petsc-master 2019-08-22
Report Typos and Errors
  1:  #include <petsc/private/dmpleximpl.h>
  2:  #include <petsc/private/dmlabelimpl.h>

  4: /*@C
  5:   DMPlexSetAdjacencyUser - Define adjacency in the mesh using a user-provided callback

  7:   Input Parameters:
  8: + dm      - The DM object
  9: . user    - The user callback, may be NULL (to clear the callback)
 10: - ctx     - context for callback evaluation, may be NULL

 12:   Level: advanced

 14:   Notes:
 15:      The caller of DMPlexGetAdjacency may need to arrange that a large enough array is available for the adjacency.

 17:      Any setting here overrides other configuration of DMPlex adjacency determination.

 19: .seealso: DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexGetAdjacency(), DMPlexGetAdjacencyUser()
 20: @*/
 21: PetscErrorCode DMPlexSetAdjacencyUser(DM dm,PetscErrorCode (*user)(DM,PetscInt,PetscInt*,PetscInt[],void*),void *ctx)
 22: {
 23:   DM_Plex *mesh = (DM_Plex *)dm->data;

 27:   mesh->useradjacency = user;
 28:   mesh->useradjacencyctx = ctx;
 29:   return(0);
 30: }

 32: /*@C
 33:   DMPlexGetAdjacencyUser - get the user-defined adjacency callback

 35:   Input Parameter:
 36: . dm      - The DM object

 38:   Output Parameters:
 39: - user    - The user callback
 40: - ctx     - context for callback evaluation

 42:   Level: advanced

 44: .seealso: DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexGetAdjacency(), DMPlexSetAdjacencyUser()
 45: @*/
 46: PetscErrorCode DMPlexGetAdjacencyUser(DM dm, PetscErrorCode (**user)(DM,PetscInt,PetscInt*,PetscInt[],void*), void **ctx)
 47: {
 48:   DM_Plex *mesh = (DM_Plex *)dm->data;

 52:   if (user) *user = mesh->useradjacency;
 53:   if (ctx) *ctx = mesh->useradjacencyctx;
 54:   return(0);
 55: }

 57: /*@
 58:   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.

 60:   Input Parameters:
 61: + dm      - The DM object
 62: - useAnchors - Flag to use the constraints.  If PETSC_TRUE, then constrained points are omitted from DMPlexGetAdjacency(), and their anchor points appear in their place.

 64:   Level: intermediate

 66: .seealso: DMGetAdjacency(), DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
 67: @*/
 68: PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors)
 69: {
 70:   DM_Plex *mesh = (DM_Plex *) dm->data;

 74:   mesh->useAnchors = useAnchors;
 75:   return(0);
 76: }

 78: /*@
 79:   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.

 81:   Input Parameter:
 82: . dm      - The DM object

 84:   Output Parameter:
 85: . useAnchors - Flag to use the closure.  If PETSC_TRUE, then constrained points are omitted from DMPlexGetAdjacency(), and their anchor points appear in their place.

 87:   Level: intermediate

 89: .seealso: DMPlexSetAdjacencyUseAnchors(), DMSetAdjacency(), DMGetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
 90: @*/
 91: PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors)
 92: {
 93:   DM_Plex *mesh = (DM_Plex *) dm->data;

 98:   *useAnchors = mesh->useAnchors;
 99:   return(0);
100: }

102: static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
103: {
104:   const PetscInt *cone = NULL;
105:   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
106:   PetscErrorCode  ierr;

109:   DMPlexGetConeSize(dm, p, &coneSize);
110:   DMPlexGetCone(dm, p, &cone);
111:   for (c = 0; c <= coneSize; ++c) {
112:     const PetscInt  point   = !c ? p : cone[c-1];
113:     const PetscInt *support = NULL;
114:     PetscInt        supportSize, s, q;

116:     DMPlexGetSupportSize(dm, point, &supportSize);
117:     DMPlexGetSupport(dm, point, &support);
118:     for (s = 0; s < supportSize; ++s) {
119:       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]),0); ++q) {
120:         if (support[s] == adj[q]) break;
121:       }
122:       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
123:     }
124:   }
125:   *adjSize = numAdj;
126:   return(0);
127: }

129: static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
130: {
131:   const PetscInt *support = NULL;
132:   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
133:   PetscErrorCode  ierr;

136:   DMPlexGetSupportSize(dm, p, &supportSize);
137:   DMPlexGetSupport(dm, p, &support);
138:   for (s = 0; s <= supportSize; ++s) {
139:     const PetscInt  point = !s ? p : support[s-1];
140:     const PetscInt *cone  = NULL;
141:     PetscInt        coneSize, c, q;

143:     DMPlexGetConeSize(dm, point, &coneSize);
144:     DMPlexGetCone(dm, point, &cone);
145:     for (c = 0; c < coneSize; ++c) {
146:       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]),0); ++q) {
147:         if (cone[c] == adj[q]) break;
148:       }
149:       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
150:     }
151:   }
152:   *adjSize = numAdj;
153:   return(0);
154: }

156: static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[])
157: {
158:   PetscInt      *star = NULL;
159:   PetscInt       numAdj = 0, maxAdjSize = *adjSize, starSize, s;

163:   DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);
164:   for (s = 0; s < starSize*2; s += 2) {
165:     const PetscInt *closure = NULL;
166:     PetscInt        closureSize, c, q;

168:     DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);
169:     for (c = 0; c < closureSize*2; c += 2) {
170:       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]),0); ++q) {
171:         if (closure[c] == adj[q]) break;
172:       }
173:       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
174:     }
175:     DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);
176:   }
177:   DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);
178:   *adjSize = numAdj;
179:   return(0);
180: }

182: PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[])
183: {
184:   static PetscInt asiz = 0;
185:   PetscInt maxAnchors = 1;
186:   PetscInt aStart = -1, aEnd = -1;
187:   PetscInt maxAdjSize;
188:   PetscSection aSec = NULL;
189:   IS aIS = NULL;
190:   const PetscInt *anchors;
191:   DM_Plex *mesh = (DM_Plex *)dm->data;
192:   PetscErrorCode  ierr;

195:   if (useAnchors) {
196:     DMPlexGetAnchors(dm,&aSec,&aIS);
197:     if (aSec) {
198:       PetscSectionGetMaxDof(aSec,&maxAnchors);
199:       maxAnchors = PetscMax(1,maxAnchors);
200:       PetscSectionGetChart(aSec,&aStart,&aEnd);
201:       ISGetIndices(aIS,&anchors);
202:     }
203:   }
204:   if (!*adj) {
205:     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;

207:     DMPlexGetChart(dm, &pStart,&pEnd);
208:     DMPlexGetDepth(dm, &depth);
209:     DMPlexGetMaxSizes(dm, &maxC, &maxS);
210:     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1;
211:     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1;
212:     asiz  = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries);
213:     asiz *= maxAnchors;
214:     asiz  = PetscMin(asiz,pEnd-pStart);
215:     PetscMalloc1(asiz,adj);
216:   }
217:   if (*adjSize < 0) *adjSize = asiz;
218:   maxAdjSize = *adjSize;
219:   if (mesh->useradjacency) {
220:     mesh->useradjacency(dm, p, adjSize, *adj, mesh->useradjacencyctx);
221:   } else if (useTransitiveClosure) {
222:     DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);
223:   } else if (useCone) {
224:     DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);
225:   } else {
226:     DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);
227:   }
228:   if (useAnchors && aSec) {
229:     PetscInt origSize = *adjSize;
230:     PetscInt numAdj = origSize;
231:     PetscInt i = 0, j;
232:     PetscInt *orig = *adj;

234:     while (i < origSize) {
235:       PetscInt p = orig[i];
236:       PetscInt aDof = 0;

238:       if (p >= aStart && p < aEnd) {
239:         PetscSectionGetDof(aSec,p,&aDof);
240:       }
241:       if (aDof) {
242:         PetscInt aOff;
243:         PetscInt s, q;

245:         for (j = i + 1; j < numAdj; j++) {
246:           orig[j - 1] = orig[j];
247:         }
248:         origSize--;
249:         numAdj--;
250:         PetscSectionGetOffset(aSec,p,&aOff);
251:         for (s = 0; s < aDof; ++s) {
252:           for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) {
253:             if (anchors[aOff+s] == orig[q]) break;
254:           }
255:           if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
256:         }
257:       }
258:       else {
259:         i++;
260:       }
261:     }
262:     *adjSize = numAdj;
263:     ISRestoreIndices(aIS,&anchors);
264:   }
265:   return(0);
266: }

268: /*@
269:   DMPlexGetAdjacency - Return all points adjacent to the given point

271:   Input Parameters:
272: + dm - The DM object
273: . p  - The point
274: . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE
275: - adj - Either NULL so that the array is allocated, or an existing array with size adjSize

277:   Output Parameters:
278: + adjSize - The number of adjacent points
279: - adj - The adjacent points

281:   Level: advanced

283:   Notes:
284:     The user must PetscFree the adj array if it was not passed in.

286: .seealso: DMSetAdjacency(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
287: @*/
288: PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
289: {
290:   PetscBool      useCone, useClosure, useAnchors;

297:   DMGetBasicAdjacency(dm, &useCone, &useClosure);
298:   DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);
299:   DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);
300:   return(0);
301: }

303: /*@
304:   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity

306:   Collective on dm

308:   Input Parameters:
309: + dm      - The DM
310: - sfPoint - The PetscSF which encodes point connectivity

312:   Output Parameters:
313: + processRanks - A list of process neighbors, or NULL
314: - sfProcess    - An SF encoding the two-sided process connectivity, or NULL

316:   Level: developer

318: .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
319: @*/
320: PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
321: {
322:   const PetscSFNode *remotePoints;
323:   PetscInt          *localPointsNew;
324:   PetscSFNode       *remotePointsNew;
325:   const PetscInt    *nranks;
326:   PetscInt          *ranksNew;
327:   PetscBT            neighbors;
328:   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
329:   PetscMPIInt        size, proc, rank;
330:   PetscErrorCode     ierr;

337:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
338:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
339:   PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);
340:   PetscBTCreate(size, &neighbors);
341:   PetscBTMemzero(size, neighbors);
342:   /* Compute root-to-leaf process connectivity */
343:   PetscSectionGetChart(rootRankSection, &pStart, &pEnd);
344:   ISGetIndices(rootRanks, &nranks);
345:   for (p = pStart; p < pEnd; ++p) {
346:     PetscInt ndof, noff, n;

348:     PetscSectionGetDof(rootRankSection, p, &ndof);
349:     PetscSectionGetOffset(rootRankSection, p, &noff);
350:     for (n = 0; n < ndof; ++n) {PetscBTSet(neighbors, nranks[noff+n]);}
351:   }
352:   ISRestoreIndices(rootRanks, &nranks);
353:   /* Compute leaf-to-neighbor process connectivity */
354:   PetscSectionGetChart(leafRankSection, &pStart, &pEnd);
355:   ISGetIndices(leafRanks, &nranks);
356:   for (p = pStart; p < pEnd; ++p) {
357:     PetscInt ndof, noff, n;

359:     PetscSectionGetDof(leafRankSection, p, &ndof);
360:     PetscSectionGetOffset(leafRankSection, p, &noff);
361:     for (n = 0; n < ndof; ++n) {PetscBTSet(neighbors, nranks[noff+n]);}
362:   }
363:   ISRestoreIndices(leafRanks, &nranks);
364:   /* Compute leaf-to-root process connectivity */
365:   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
366:   /* Calculate edges */
367:   PetscBTClear(neighbors, rank);
368:   for(proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
369:   PetscMalloc1(numNeighbors, &ranksNew);
370:   PetscMalloc1(numNeighbors, &localPointsNew);
371:   PetscMalloc1(numNeighbors, &remotePointsNew);
372:   for(proc = 0, n = 0; proc < size; ++proc) {
373:     if (PetscBTLookup(neighbors, proc)) {
374:       ranksNew[n]              = proc;
375:       localPointsNew[n]        = proc;
376:       remotePointsNew[n].index = rank;
377:       remotePointsNew[n].rank  = proc;
378:       ++n;
379:     }
380:   }
381:   PetscBTDestroy(&neighbors);
382:   if (processRanks) {ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);}
383:   else              {PetscFree(ranksNew);}
384:   if (sfProcess) {
385:     PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);
386:     PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");
387:     PetscSFSetFromOptions(*sfProcess);
388:     PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);
389:   }
390:   return(0);
391: }

393: /*@
394:   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.

396:   Collective on dm

398:   Input Parameter:
399: . dm - The DM

401:   Output Parameters:
402: + rootSection - The number of leaves for a given root point
403: . rootrank    - The rank of each edge into the root point
404: . leafSection - The number of processes sharing a given leaf point
405: - leafrank    - The rank of each process sharing a leaf point

407:   Level: developer

409: .seealso: DMPlexCreateOverlap()
410: @*/
411: PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
412: {
413:   MPI_Comm        comm;
414:   PetscSF         sfPoint;
415:   const PetscInt *rootdegree;
416:   PetscInt       *myrank, *remoterank;
417:   PetscInt        pStart, pEnd, p, nedges;
418:   PetscMPIInt     rank;
419:   PetscErrorCode  ierr;

422:   PetscObjectGetComm((PetscObject) dm, &comm);
423:   MPI_Comm_rank(comm, &rank);
424:   DMPlexGetChart(dm, &pStart, &pEnd);
425:   DMGetPointSF(dm, &sfPoint);
426:   /* Compute number of leaves for each root */
427:   PetscObjectSetName((PetscObject) rootSection, "Root Section");
428:   PetscSectionSetChart(rootSection, pStart, pEnd);
429:   PetscSFComputeDegreeBegin(sfPoint, &rootdegree);
430:   PetscSFComputeDegreeEnd(sfPoint, &rootdegree);
431:   for (p = pStart; p < pEnd; ++p) {PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);}
432:   PetscSectionSetUp(rootSection);
433:   /* Gather rank of each leaf to root */
434:   PetscSectionGetStorageSize(rootSection, &nedges);
435:   PetscMalloc1(pEnd-pStart, &myrank);
436:   PetscMalloc1(nedges,  &remoterank);
437:   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
438:   PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);
439:   PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);
440:   PetscFree(myrank);
441:   ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);
442:   /* Distribute remote ranks to leaves */
443:   PetscObjectSetName((PetscObject) leafSection, "Leaf Section");
444:   DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);
445:   return(0);
446: }

448: /*@C
449:   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.

451:   Collective on dm

453:   Input Parameters:
454: + dm          - The DM
455: . levels      - Number of overlap levels
456: . rootSection - The number of leaves for a given root point
457: . rootrank    - The rank of each edge into the root point
458: . leafSection - The number of processes sharing a given leaf point
459: - leafrank    - The rank of each process sharing a leaf point

461:   Output Parameter:
462: . ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings

464:   Level: developer

466: .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
467: @*/
468: PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
469: {
470:   MPI_Comm           comm;
471:   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
472:   PetscSF            sfPoint;
473:   const PetscSFNode *remote;
474:   const PetscInt    *local;
475:   const PetscInt    *nrank, *rrank;
476:   PetscInt          *adj = NULL;
477:   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
478:   PetscMPIInt        rank, size;
479:   PetscBool          flg;
480:   PetscErrorCode     ierr;

483:   PetscObjectGetComm((PetscObject) dm, &comm);
484:   MPI_Comm_size(comm, &size);
485:   MPI_Comm_rank(comm, &rank);
486:   DMGetPointSF(dm, &sfPoint);
487:   DMPlexGetChart(dm, &pStart, &pEnd);
488:   PetscSectionGetChart(leafSection, &sStart, &sEnd);
489:   PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);
490:   DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank);
491:   /* Handle leaves: shared with the root point */
492:   for (l = 0; l < nleaves; ++l) {
493:     PetscInt adjSize = PETSC_DETERMINE, a;

495:     DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj);
496:     for (a = 0; a < adjSize; ++a) {DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);}
497:   }
498:   ISGetIndices(rootrank, &rrank);
499:   ISGetIndices(leafrank, &nrank);
500:   /* Handle roots */
501:   for (p = pStart; p < pEnd; ++p) {
502:     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;

504:     if ((p >= sStart) && (p < sEnd)) {
505:       /* Some leaves share a root with other leaves on different processes */
506:       PetscSectionGetDof(leafSection, p, &neighbors);
507:       if (neighbors) {
508:         PetscSectionGetOffset(leafSection, p, &noff);
509:         DMPlexGetAdjacency(dm, p, &adjSize, &adj);
510:         for (n = 0; n < neighbors; ++n) {
511:           const PetscInt remoteRank = nrank[noff+n];

513:           if (remoteRank == rank) continue;
514:           for (a = 0; a < adjSize; ++a) {DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);}
515:         }
516:       }
517:     }
518:     /* Roots are shared with leaves */
519:     PetscSectionGetDof(rootSection, p, &neighbors);
520:     if (!neighbors) continue;
521:     PetscSectionGetOffset(rootSection, p, &noff);
522:     DMPlexGetAdjacency(dm, p, &adjSize, &adj);
523:     for (n = 0; n < neighbors; ++n) {
524:       const PetscInt remoteRank = rrank[noff+n];

526:       if (remoteRank == rank) continue;
527:       for (a = 0; a < adjSize; ++a) {DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);}
528:     }
529:   }
530:   PetscFree(adj);
531:   ISRestoreIndices(rootrank, &rrank);
532:   ISRestoreIndices(leafrank, &nrank);
533:   /* Add additional overlap levels */
534:   for (l = 1; l < levels; l++) {
535:     /* Propagate point donations over SF to capture remote connections */
536:     DMPlexPartitionLabelPropagate(dm, ovAdjByRank);
537:     /* Add next level of point donations to the label */
538:     DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);
539:   }
540:   /* We require the closure in the overlap */
541:   DMPlexPartitionLabelClosure(dm, ovAdjByRank);
542:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);
543:   if (flg) {
544:     PetscViewer viewer;
545:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer);
546:     DMLabelView(ovAdjByRank, viewer);
547:   }
548:   /* Invert sender to receiver label */
549:   DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel);
550:   DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel);
551:   /* Add owned points, except for shared local points */
552:   for (p = pStart; p < pEnd; ++p) {DMLabelSetValue(*ovLabel, p, rank);}
553:   for (l = 0; l < nleaves; ++l) {
554:     DMLabelClearValue(*ovLabel, local[l], rank);
555:     DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);
556:   }
557:   /* Clean up */
558:   DMLabelDestroy(&ovAdjByRank);
559:   return(0);
560: }

562: /*@C
563:   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF

565:   Collective on dm

567:   Input Parameters:
568: + dm          - The DM
569: - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes

571:   Output Parameters:
572: . migrationSF - An SF that maps original points in old locations to points in new locations

574:   Level: developer

576: .seealso: DMPlexCreateOverlap(), DMPlexDistribute()
577: @*/
578: PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
579: {
580:   MPI_Comm           comm;
581:   PetscMPIInt        rank, size;
582:   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
583:   PetscInt          *pointDepths, *remoteDepths, *ilocal;
584:   PetscInt          *depthRecv, *depthShift, *depthIdx;
585:   PetscSFNode       *iremote;
586:   PetscSF            pointSF;
587:   const PetscInt    *sharedLocal;
588:   const PetscSFNode *overlapRemote, *sharedRemote;
589:   PetscErrorCode     ierr;

593:   PetscObjectGetComm((PetscObject)dm, &comm);
594:   MPI_Comm_rank(comm, &rank);
595:   MPI_Comm_size(comm, &size);
596:   DMGetDimension(dm, &dim);

598:   /* Before building the migration SF we need to know the new stratum offsets */
599:   PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);
600:   PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);
601:   for (d=0; d<dim+1; d++) {
602:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
603:     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
604:   }
605:   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
606:   PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);
607:   PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);

609:   /* Count recevied points in each stratum and compute the internal strata shift */
610:   PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);
611:   for (d=0; d<dim+1; d++) depthRecv[d]=0;
612:   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
613:   depthShift[dim] = 0;
614:   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
615:   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
616:   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
617:   for (d=0; d<dim+1; d++) {
618:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
619:     depthIdx[d] = pStart + depthShift[d];
620:   }

622:   /* Form the overlap SF build an SF that describes the full overlap migration SF */
623:   DMPlexGetChart(dm, &pStart, &pEnd);
624:   newLeaves = pEnd - pStart + nleaves;
625:   PetscMalloc1(newLeaves, &ilocal);
626:   PetscMalloc1(newLeaves, &iremote);
627:   /* First map local points to themselves */
628:   for (d=0; d<dim+1; d++) {
629:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
630:     for (p=pStart; p<pEnd; p++) {
631:       point = p + depthShift[d];
632:       ilocal[point] = point;
633:       iremote[point].index = p;
634:       iremote[point].rank = rank;
635:       depthIdx[d]++;
636:     }
637:   }

639:   /* Add in the remote roots for currently shared points */
640:   DMGetPointSF(dm, &pointSF);
641:   PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);
642:   for (d=0; d<dim+1; d++) {
643:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
644:     for (p=0; p<numSharedPoints; p++) {
645:       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
646:         point = sharedLocal[p] + depthShift[d];
647:         iremote[point].index = sharedRemote[p].index;
648:         iremote[point].rank = sharedRemote[p].rank;
649:       }
650:     }
651:   }

653:   /* Now add the incoming overlap points */
654:   for (p=0; p<nleaves; p++) {
655:     point = depthIdx[remoteDepths[p]];
656:     ilocal[point] = point;
657:     iremote[point].index = overlapRemote[p].index;
658:     iremote[point].rank = overlapRemote[p].rank;
659:     depthIdx[remoteDepths[p]]++;
660:   }
661:   PetscFree2(pointDepths,remoteDepths);

663:   PetscSFCreate(comm, migrationSF);
664:   PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");
665:   PetscSFSetFromOptions(*migrationSF);
666:   DMPlexGetChart(dm, &pStart, &pEnd);
667:   PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);

669:   PetscFree3(depthRecv, depthShift, depthIdx);
670:   return(0);
671: }

673: /*@
674:   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.

676:   Input Parameter:
677: + dm          - The DM
678: - sf          - A star forest with non-ordered leaves, usually defining a DM point migration

680:   Output Parameter:
681: . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified

683:   Level: developer

685: .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
686: @*/
687: PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
688: {
689:   MPI_Comm           comm;
690:   PetscMPIInt        rank, size;
691:   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
692:   PetscInt          *pointDepths, *remoteDepths, *ilocal;
693:   PetscInt          *depthRecv, *depthShift, *depthIdx;
694:   PetscInt           hybEnd[4];
695:   const PetscSFNode *iremote;
696:   PetscErrorCode     ierr;

700:   PetscObjectGetComm((PetscObject) dm, &comm);
701:   MPI_Comm_rank(comm, &rank);
702:   MPI_Comm_size(comm, &size);
703:   DMPlexGetDepth(dm, &ldepth);
704:   MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
705:   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
706:   PetscLogEventBegin(DMPLEX_PartStratSF,dm,0,0,0);

708:   /* Before building the migration SF we need to know the new stratum offsets */
709:   PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);
710:   PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);
711:   DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[PetscMax(depth-1,0)],&hybEnd[1],&hybEnd[0]);
712:   for (d = 0; d < depth+1; ++d) {
713:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
714:     for (p = pStart; p < pEnd; ++p) {
715:       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
716:         pointDepths[p] = 2 * d;
717:       } else {
718:         pointDepths[p] = 2 * d + 1;
719:       }
720:     }
721:   }
722:   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
723:   PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);
724:   PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);
725:   /* Count received points in each stratum and compute the internal strata shift */
726:   PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);
727:   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
728:   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
729:   depthShift[2*depth+1] = 0;
730:   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
731:   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
732:   depthShift[0] += depthRecv[1];
733:   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
734:   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
735:   for (d = 2 * depth-1; d > 2; --d) {
736:     PetscInt e;

738:     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
739:   }
740:   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
741:   /* Derive a new local permutation based on stratified indices */
742:   PetscMalloc1(nleaves, &ilocal);
743:   for (p = 0; p < nleaves; ++p) {
744:     const PetscInt dep = remoteDepths[p];

746:     ilocal[p] = depthShift[dep] + depthIdx[dep];
747:     depthIdx[dep]++;
748:   }
749:   PetscSFCreate(comm, migrationSF);
750:   PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");
751:   PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);
752:   PetscFree2(pointDepths,remoteDepths);
753:   PetscFree3(depthRecv, depthShift, depthIdx);
754:   PetscLogEventEnd(DMPLEX_PartStratSF,dm,0,0,0);
755:   return(0);
756: }

758: /*@
759:   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution

761:   Collective on dm

763:   Input Parameters:
764: + dm - The DMPlex object
765: . pointSF - The PetscSF describing the communication pattern
766: . originalSection - The PetscSection for existing data layout
767: - originalVec - The existing data in a local vector

769:   Output Parameters:
770: + newSection - The PetscSF describing the new data layout
771: - newVec - The new data in a local vector

773:   Level: developer

775: .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
776: @*/
777: PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
778: {
779:   PetscSF        fieldSF;
780:   PetscInt      *remoteOffsets, fieldSize;
781:   PetscScalar   *originalValues, *newValues;

785:   PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);
786:   PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);

788:   PetscSectionGetStorageSize(newSection, &fieldSize);
789:   VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);
790:   VecSetType(newVec,dm->vectype);

792:   VecGetArray(originalVec, &originalValues);
793:   VecGetArray(newVec, &newValues);
794:   PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);
795:   PetscFree(remoteOffsets);
796:   PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);
797:   PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);
798:   PetscSFDestroy(&fieldSF);
799:   VecRestoreArray(newVec, &newValues);
800:   VecRestoreArray(originalVec, &originalValues);
801:   PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);
802:   return(0);
803: }

805: /*@
806:   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution

808:   Collective on dm

810:   Input Parameters:
811: + dm - The DMPlex object
812: . pointSF - The PetscSF describing the communication pattern
813: . originalSection - The PetscSection for existing data layout
814: - originalIS - The existing data

816:   Output Parameters:
817: + newSection - The PetscSF describing the new data layout
818: - newIS - The new data

820:   Level: developer

822: .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
823: @*/
824: PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
825: {
826:   PetscSF         fieldSF;
827:   PetscInt       *newValues, *remoteOffsets, fieldSize;
828:   const PetscInt *originalValues;
829:   PetscErrorCode  ierr;

832:   PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);
833:   PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);

835:   PetscSectionGetStorageSize(newSection, &fieldSize);
836:   PetscMalloc1(fieldSize, &newValues);

838:   ISGetIndices(originalIS, &originalValues);
839:   PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);
840:   PetscFree(remoteOffsets);
841:   PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);
842:   PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);
843:   PetscSFDestroy(&fieldSF);
844:   ISRestoreIndices(originalIS, &originalValues);
845:   ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);
846:   PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);
847:   return(0);
848: }

850: /*@
851:   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution

853:   Collective on dm

855:   Input Parameters:
856: + dm - The DMPlex object
857: . pointSF - The PetscSF describing the communication pattern
858: . originalSection - The PetscSection for existing data layout
859: . datatype - The type of data
860: - originalData - The existing data

862:   Output Parameters:
863: + newSection - The PetscSection describing the new data layout
864: - newData - The new data

866:   Level: developer

868: .seealso: DMPlexDistribute(), DMPlexDistributeField()
869: @*/
870: PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
871: {
872:   PetscSF        fieldSF;
873:   PetscInt      *remoteOffsets, fieldSize;
874:   PetscMPIInt    dataSize;

878:   PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);
879:   PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);

881:   PetscSectionGetStorageSize(newSection, &fieldSize);
882:   MPI_Type_size(datatype, &dataSize);
883:   PetscMalloc(fieldSize * dataSize, newData);

885:   PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);
886:   PetscFree(remoteOffsets);
887:   PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);
888:   PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);
889:   PetscSFDestroy(&fieldSF);
890:   PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);
891:   return(0);
892: }

894: static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
895: {
896:   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
897:   MPI_Comm               comm;
898:   PetscSF                coneSF;
899:   PetscSection           originalConeSection, newConeSection;
900:   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
901:   PetscBool              flg;
902:   PetscErrorCode         ierr;

907:   PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);
908:   /* Distribute cone section */
909:   PetscObjectGetComm((PetscObject)dm, &comm);
910:   DMPlexGetConeSection(dm, &originalConeSection);
911:   DMPlexGetConeSection(dmParallel, &newConeSection);
912:   PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);
913:   DMSetUp(dmParallel);
914:   {
915:     PetscInt pStart, pEnd, p;

917:     PetscSectionGetChart(newConeSection, &pStart, &pEnd);
918:     for (p = pStart; p < pEnd; ++p) {
919:       PetscInt coneSize;
920:       PetscSectionGetDof(newConeSection, p, &coneSize);
921:       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
922:     }
923:   }
924:   /* Communicate and renumber cones */
925:   PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);
926:   PetscFree(remoteOffsets);
927:   DMPlexGetCones(dm, &cones);
928:   if (original) {
929:     PetscInt numCones;

931:     PetscSectionGetStorageSize(originalConeSection,&numCones);
932:     PetscMalloc1(numCones,&globCones);
933:     ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);
934:   } else {
935:     globCones = cones;
936:   }
937:   DMPlexGetCones(dmParallel, &newCones);
938:   PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);
939:   PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);
940:   if (original) {
941:     PetscFree(globCones);
942:   }
943:   PetscSectionGetStorageSize(newConeSection, &newConesSize);
944:   ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);
945: #if defined(PETSC_USE_DEBUG)
946:   {
947:     PetscInt  p;
948:     PetscBool valid = PETSC_TRUE;
949:     for (p = 0; p < newConesSize; ++p) {
950:       if (newCones[p] < 0) {valid = PETSC_FALSE; PetscPrintf(PETSC_COMM_SELF, "[%d] Point %D not in overlap SF\n", PetscGlobalRank,p);}
951:     }
952:     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
953:   }
954: #endif
955:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);
956:   if (flg) {
957:     PetscPrintf(comm, "Serial Cone Section:\n");
958:     PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_(comm));
959:     PetscPrintf(comm, "Parallel Cone Section:\n");
960:     PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_(comm));
961:     PetscSFView(coneSF, NULL);
962:   }
963:   DMPlexGetConeOrientations(dm, &cones);
964:   DMPlexGetConeOrientations(dmParallel, &newCones);
965:   PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);
966:   PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);
967:   PetscSFDestroy(&coneSF);
968:   PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);
969:   /* Create supports and stratify DMPlex */
970:   {
971:     PetscInt pStart, pEnd;

973:     PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);
974:     PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);
975:   }
976:   DMPlexSymmetrize(dmParallel);
977:   DMPlexStratify(dmParallel);
978:   {
979:     PetscBool useCone, useClosure, useAnchors;

981:     DMGetBasicAdjacency(dm, &useCone, &useClosure);
982:     DMSetBasicAdjacency(dmParallel, useCone, useClosure);
983:     DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);
984:     DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);
985:   }
986:   return(0);
987: }

989: static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
990: {
991:   MPI_Comm         comm;
992:   PetscSection     originalCoordSection, newCoordSection;
993:   Vec              originalCoordinates, newCoordinates;
994:   PetscInt         bs;
995:   PetscBool        isper;
996:   const char      *name;
997:   const PetscReal *maxCell, *L;
998:   const DMBoundaryType *bd;
999:   PetscErrorCode   ierr;


1005:   PetscObjectGetComm((PetscObject)dm, &comm);
1006:   DMGetCoordinateSection(dm, &originalCoordSection);
1007:   DMGetCoordinateSection(dmParallel, &newCoordSection);
1008:   DMGetCoordinatesLocal(dm, &originalCoordinates);
1009:   if (originalCoordinates) {
1010:     VecCreate(PETSC_COMM_SELF, &newCoordinates);
1011:     PetscObjectGetName((PetscObject) originalCoordinates, &name);
1012:     PetscObjectSetName((PetscObject) newCoordinates, name);

1014:     DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);
1015:     DMSetCoordinatesLocal(dmParallel, newCoordinates);
1016:     VecGetBlockSize(originalCoordinates, &bs);
1017:     VecSetBlockSize(newCoordinates, bs);
1018:     VecDestroy(&newCoordinates);
1019:   }
1020:   DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);
1021:   DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);
1022:   return(0);
1023: }

1025: static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
1026: {
1027:   DM_Plex         *mesh = (DM_Plex*) dm->data;
1028:   MPI_Comm         comm;
1029:   DMLabel          depthLabel;
1030:   PetscMPIInt      rank;
1031:   PetscInt         depth, d, numLabels, numLocalLabels, l;
1032:   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1033:   PetscObjectState depthState = -1;
1034:   PetscErrorCode   ierr;


1040:   PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);
1041:   PetscObjectGetComm((PetscObject)dm, &comm);
1042:   MPI_Comm_rank(comm, &rank);

1044:   /* If the user has changed the depth label, communicate it instead */
1045:   DMPlexGetDepth(dm, &depth);
1046:   DMPlexGetDepthLabel(dm, &depthLabel);
1047:   if (depthLabel) {PetscObjectStateGet((PetscObject) depthLabel, &depthState);}
1048:   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1049:   MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);
1050:   if (sendDepth) {
1051:     DMRemoveLabel(dmParallel, "depth", &depthLabel);
1052:     DMLabelDestroy(&depthLabel);
1053:   }
1054:   /* Everyone must have either the same number of labels, or none */
1055:   DMGetNumLabels(dm, &numLocalLabels);
1056:   numLabels = numLocalLabels;
1057:   MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);
1058:   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1059:   for (l = numLabels-1; l >= 0; --l) {
1060:     DMLabel     label = NULL, labelNew = NULL;
1061:     PetscBool   isDepth, lisOutput = PETSC_TRUE, isOutput;
1062:     const char *name = NULL;

1064:     if (hasLabels) {
1065:       DMGetLabelByNum(dm, l, &label);
1066:       /* Skip "depth" because it is recreated */
1067:       PetscObjectGetName((PetscObject) label, &name);
1068:       PetscStrcmp(name, "depth", &isDepth);
1069:     }
1070:     MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm);
1071:     if (isDepth && !sendDepth) continue;
1072:     DMLabelDistribute(label, migrationSF, &labelNew);
1073:     if (isDepth) {
1074:       /* Put in any missing strata which can occur if users are managing the depth label themselves */
1075:       PetscInt gdepth;

1077:       MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);
1078:       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
1079:       for (d = 0; d <= gdepth; ++d) {
1080:         PetscBool has;

1082:         DMLabelHasStratum(labelNew, d, &has);
1083:         if (!has) {DMLabelAddStratum(labelNew, d);}
1084:       }
1085:     }
1086:     DMAddLabel(dmParallel, labelNew);
1087:     /* Put the output flag in the new label */
1088:     if (hasLabels) {DMGetLabelOutput(dm, name, &lisOutput);}
1089:     MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm);
1090:     PetscObjectGetName((PetscObject) labelNew, &name);
1091:     DMSetLabelOutput(dmParallel, name, isOutput);
1092:   }
1093:   PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);
1094:   return(0);
1095: }

1097: static PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
1098: {
1099:   DM_Plex        *mesh  = (DM_Plex*) dm->data;
1100:   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1101:   PetscBool      *isHybrid, *isHybridParallel;
1102:   PetscInt        dim, depth, d;
1103:   PetscInt        pStart, pEnd, pStartP, pEndP;
1104:   PetscErrorCode  ierr;


1110:   DMGetDimension(dm, &dim);
1111:   DMPlexGetDepth(dm, &depth);
1112:   DMPlexGetChart(dm,&pStart,&pEnd);
1113:   DMPlexGetChart(dmParallel,&pStartP,&pEndP);
1114:   PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);
1115:   for (d = 0; d <= depth; d++) {
1116:     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];

1118:     if (hybridMax >= 0) {
1119:       PetscInt sStart, sEnd, p;

1121:       DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);
1122:       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
1123:     }
1124:   }
1125:   PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);
1126:   PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);
1127:   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1128:   for (d = 0; d <= depth; d++) {
1129:     PetscInt sStart, sEnd, p, dd;

1131:     DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);
1132:     dd = (depth == 1 && d == 1) ? dim : d;
1133:     for (p = sStart; p < sEnd; p++) {
1134:       if (isHybridParallel[p-pStartP]) {
1135:         pmesh->hybridPointMax[dd] = p;
1136:         break;
1137:       }
1138:     }
1139:   }
1140:   PetscFree2(isHybrid,isHybridParallel);
1141:   return(0);
1142: }

1144: static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1145: {
1146:   DM_Plex        *mesh  = (DM_Plex*) dm->data;
1147:   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1148:   MPI_Comm        comm;
1149:   DM              refTree;
1150:   PetscSection    origParentSection, newParentSection;
1151:   PetscInt        *origParents, *origChildIDs;
1152:   PetscBool       flg;
1153:   PetscErrorCode  ierr;

1158:   PetscObjectGetComm((PetscObject)dm, &comm);

1160:   /* Set up tree */
1161:   DMPlexGetReferenceTree(dm,&refTree);
1162:   DMPlexSetReferenceTree(dmParallel,refTree);
1163:   DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);
1164:   if (origParentSection) {
1165:     PetscInt        pStart, pEnd;
1166:     PetscInt        *newParents, *newChildIDs, *globParents;
1167:     PetscInt        *remoteOffsetsParents, newParentSize;
1168:     PetscSF         parentSF;

1170:     DMPlexGetChart(dmParallel, &pStart, &pEnd);
1171:     PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);
1172:     PetscSectionSetChart(newParentSection,pStart,pEnd);
1173:     PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);
1174:     PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);
1175:     PetscFree(remoteOffsetsParents);
1176:     PetscSectionGetStorageSize(newParentSection,&newParentSize);
1177:     PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);
1178:     if (original) {
1179:       PetscInt numParents;

1181:       PetscSectionGetStorageSize(origParentSection,&numParents);
1182:       PetscMalloc1(numParents,&globParents);
1183:       ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);
1184:     }
1185:     else {
1186:       globParents = origParents;
1187:     }
1188:     PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);
1189:     PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);
1190:     if (original) {
1191:       PetscFree(globParents);
1192:     }
1193:     PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);
1194:     PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);
1195:     ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);
1196: #if defined(PETSC_USE_DEBUG)
1197:     {
1198:       PetscInt  p;
1199:       PetscBool valid = PETSC_TRUE;
1200:       for (p = 0; p < newParentSize; ++p) {
1201:         if (newParents[p] < 0) {valid = PETSC_FALSE; PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);}
1202:       }
1203:       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
1204:     }
1205: #endif
1206:     PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);
1207:     if (flg) {
1208:       PetscPrintf(comm, "Serial Parent Section: \n");
1209:       PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_(comm));
1210:       PetscPrintf(comm, "Parallel Parent Section: \n");
1211:       PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_(comm));
1212:       PetscSFView(parentSF, NULL);
1213:     }
1214:     DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);
1215:     PetscSectionDestroy(&newParentSection);
1216:     PetscFree2(newParents,newChildIDs);
1217:     PetscSFDestroy(&parentSF);
1218:   }
1219:   pmesh->useAnchors = mesh->useAnchors;
1220:   return(0);
1221: }

1223: PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
1224: {
1225:   PetscMPIInt            rank, size;
1226:   MPI_Comm               comm;
1227:   PetscErrorCode         ierr;


1233:   /* Create point SF for parallel mesh */
1234:   PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);
1235:   PetscObjectGetComm((PetscObject)dm, &comm);
1236:   MPI_Comm_rank(comm, &rank);
1237:   MPI_Comm_size(comm, &size);
1238:   {
1239:     const PetscInt *leaves;
1240:     PetscSFNode    *remotePoints, *rowners, *lowners;
1241:     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
1242:     PetscInt        pStart, pEnd;

1244:     DMPlexGetChart(dmParallel, &pStart, &pEnd);
1245:     PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);
1246:     PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);
1247:     for (p=0; p<numRoots; p++) {
1248:       rowners[p].rank  = -1;
1249:       rowners[p].index = -1;
1250:     }
1251:     PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);
1252:     PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);
1253:     for (p = 0; p < numLeaves; ++p) {
1254:       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
1255:         lowners[p].rank  = rank;
1256:         lowners[p].index = leaves ? leaves[p] : p;
1257:       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
1258:         lowners[p].rank  = -2;
1259:         lowners[p].index = -2;
1260:       }
1261:     }
1262:     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
1263:       rowners[p].rank  = -3;
1264:       rowners[p].index = -3;
1265:     }
1266:     PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);
1267:     PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);
1268:     PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);
1269:     PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);
1270:     for (p = 0; p < numLeaves; ++p) {
1271:       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
1272:       if (lowners[p].rank != rank) ++numGhostPoints;
1273:     }
1274:     PetscMalloc1(numGhostPoints, &ghostPoints);
1275:     PetscMalloc1(numGhostPoints, &remotePoints);
1276:     for (p = 0, gp = 0; p < numLeaves; ++p) {
1277:       if (lowners[p].rank != rank) {
1278:         ghostPoints[gp]        = leaves ? leaves[p] : p;
1279:         remotePoints[gp].rank  = lowners[p].rank;
1280:         remotePoints[gp].index = lowners[p].index;
1281:         ++gp;
1282:       }
1283:     }
1284:     PetscFree2(rowners,lowners);
1285:     PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);
1286:     PetscSFSetFromOptions((dmParallel)->sf);
1287:   }
1288:   {
1289:     PetscBool useCone, useClosure, useAnchors;

1291:     DMGetBasicAdjacency(dm, &useCone, &useClosure);
1292:     DMSetBasicAdjacency(dmParallel, useCone, useClosure);
1293:     DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);
1294:     DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);
1295:   }
1296:   PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);
1297:   return(0);
1298: }

1300: /*@
1301:   DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition?

1303:   Input Parameters:
1304: + dm - The DMPlex object
1305: - flg - Balance the partition?

1307:   Level: intermediate

1309: .seealso: DMPlexDistribute(), DMPlexGetPartitionBalance()
1310: @*/
1311: PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg)
1312: {
1313:   DM_Plex *mesh = (DM_Plex *)dm->data;

1316:   mesh->partitionBalance = flg;
1317:   return(0);
1318: }

1320: /*@
1321:   DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition?

1323:   Input Parameter:
1324: . dm - The DMPlex object

1326:   Output Parameter:
1327: . flg - Balance the partition?

1329:   Level: intermediate

1331: .seealso: DMPlexDistribute(), DMPlexSetPartitionBalance()
1332: @*/
1333: PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg)
1334: {
1335:   DM_Plex *mesh = (DM_Plex *)dm->data;

1340:   *flg = mesh->partitionBalance;
1341:   return(0);
1342: }

1344: /*@C
1345:   DMPlexCreatePointSF - Build a point SF from an SF describing a point migration

1347:   Input Parameter:
1348: + dm          - The source DMPlex object
1349: . migrationSF - The star forest that describes the parallel point remapping
1350: . ownership   - Flag causing a vote to determine point ownership

1352:   Output Parameter:
1353: - pointSF     - The star forest describing the point overlap in the remapped DM

1355:   Level: developer

1357: .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1358: @*/
1359: PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1360: {
1361:   PetscMPIInt        rank, size;
1362:   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1363:   PetscInt          *pointLocal;
1364:   const PetscInt    *leaves;
1365:   const PetscSFNode *roots;
1366:   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1367:   Vec                shifts;
1368:   const PetscInt     numShifts = 13759;
1369:   const PetscScalar *shift = NULL;
1370:   const PetscBool    shiftDebug = PETSC_FALSE;
1371:   PetscBool          balance;
1372:   PetscErrorCode     ierr;

1376:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1377:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
1378:   PetscLogEventBegin(DMPLEX_CreatePointSF,dm,0,0,0);

1380:   DMPlexGetPartitionBalance(dm, &balance);
1381:   PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);
1382:   PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);
1383:   if (ownership) {
1384:     /* If balancing, we compute a random cyclic shift of the rank for each remote point. That way, the max will evenly distribute among ranks. */
1385:     if (balance) {
1386:       PetscRandom r;

1388:       PetscRandomCreate(PETSC_COMM_SELF, &r);
1389:       PetscRandomSetInterval(r, 0, 2467*size);
1390:       VecCreate(PETSC_COMM_SELF, &shifts);
1391:       VecSetSizes(shifts, numShifts, numShifts);
1392:       VecSetType(shifts, VECSTANDARD);
1393:       VecSetRandom(shifts, r);
1394:       PetscRandomDestroy(&r);
1395:       VecGetArrayRead(shifts, &shift);
1396:     }

1398:     /* Point ownership vote: Process with highest rank owns shared points */
1399:     for (p = 0; p < nleaves; ++p) {
1400:       if (shiftDebug) {
1401:         PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d] Point %D RemotePoint %D Shift %D MyRank %D\n", rank, leaves ? leaves[p] : p, roots[p].index, (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]), (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size);
1402:       }
1403:       /* Either put in a bid or we know we own it */
1404:       leafNodes[p].rank  = (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size;
1405:       leafNodes[p].index = p;
1406:     }
1407:     for (p = 0; p < nroots; p++) {
1408:       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1409:       rootNodes[p].rank  = -3;
1410:       rootNodes[p].index = -3;
1411:     }
1412:     PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);
1413:     PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);
1414:     if (balance) {
1415:       /* We've voted, now we need to get the rank.  When we're balancing the partition, the "rank" in rootNotes is not
1416:        * the rank but rather (rank + random)%size.  So we do another reduction, voting the same way, but sending the
1417:        * rank instead of the index. */
1418:       PetscSFNode *rootRanks = NULL;
1419:       PetscMalloc1(nroots, &rootRanks);
1420:       for (p = 0; p < nroots; p++) {
1421:         rootRanks[p].rank = -3;
1422:         rootRanks[p].index = -3;
1423:       }
1424:       for (p = 0; p < nleaves; p++) leafNodes[p].index = rank;
1425:       PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootRanks, MPI_MAXLOC);
1426:       PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootRanks, MPI_MAXLOC);
1427:       for (p = 0; p < nroots; p++) rootNodes[p].rank = rootRanks[p].index;
1428:       PetscFree(rootRanks);
1429:     }
1430:   } else {
1431:     for (p = 0; p < nroots; p++) {
1432:       rootNodes[p].index = -1;
1433:       rootNodes[p].rank = rank;
1434:     };
1435:     for (p = 0; p < nleaves; p++) {
1436:       /* Write new local id into old location */
1437:       if (roots[p].rank == rank) {
1438:         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1439:       }
1440:     }
1441:   }
1442:   PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);
1443:   PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);

1445:   for (npointLeaves = 0, p = 0; p < nleaves; p++) {
1446:     if (leafNodes[p].rank != rank) npointLeaves++;
1447:   }
1448:   PetscMalloc1(npointLeaves, &pointLocal);
1449:   PetscMalloc1(npointLeaves, &pointRemote);
1450:   for (idx = 0, p = 0; p < nleaves; p++) {
1451:     if (leafNodes[p].rank != rank) {
1452:       pointLocal[idx] = p;
1453:       pointRemote[idx] = leafNodes[p];
1454:       idx++;
1455:     }
1456:   }
1457:   if (shift) {
1458:     VecRestoreArrayRead(shifts, &shift);
1459:     VecDestroy(&shifts);
1460:   }
1461:   if (shiftDebug) {PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), PETSC_STDOUT);}
1462:   PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);
1463:   PetscSFSetFromOptions(*pointSF);
1464:   PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);
1465:   PetscFree2(rootNodes, leafNodes);
1466:   PetscLogEventEnd(DMPLEX_CreatePointSF,dm,0,0,0);
1467:   return(0);
1468: }

1470: /*@C
1471:   DMPlexMigrate  - Migrates internal DM data over the supplied star forest

1473:   Collective on dm

1475:   Input Parameter:
1476: + dm       - The source DMPlex object
1477: . sf       - The star forest communication context describing the migration pattern

1479:   Output Parameter:
1480: - targetDM - The target DMPlex object

1482:   Level: intermediate

1484: .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1485: @*/
1486: PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
1487: {
1488:   MPI_Comm               comm;
1489:   PetscInt               dim, cdim, nroots;
1490:   PetscSF                sfPoint;
1491:   ISLocalToGlobalMapping ltogMigration;
1492:   ISLocalToGlobalMapping ltogOriginal = NULL;
1493:   PetscBool              flg;
1494:   PetscErrorCode         ierr;

1498:   PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);
1499:   PetscObjectGetComm((PetscObject) dm, &comm);
1500:   DMGetDimension(dm, &dim);
1501:   DMSetDimension(targetDM, dim);
1502:   DMGetCoordinateDim(dm, &cdim);
1503:   DMSetCoordinateDim(targetDM, cdim);

1505:   /* Check for a one-to-all distribution pattern */
1506:   DMGetPointSF(dm, &sfPoint);
1507:   PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);
1508:   if (nroots >= 0) {
1509:     IS        isOriginal;
1510:     PetscInt  n, size, nleaves;
1511:     PetscInt  *numbering_orig, *numbering_new;

1513:     /* Get the original point numbering */
1514:     DMPlexCreatePointNumbering(dm, &isOriginal);
1515:     ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);
1516:     ISLocalToGlobalMappingGetSize(ltogOriginal, &size);
1517:     ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);
1518:     /* Convert to positive global numbers */
1519:     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1520:     /* Derive the new local-to-global mapping from the old one */
1521:     PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);
1522:     PetscMalloc1(nleaves, &numbering_new);
1523:     PetscSFBcastBegin(sf, MPIU_INT, numbering_orig, numbering_new);
1524:     PetscSFBcastEnd(sf, MPIU_INT, numbering_orig, numbering_new);
1525:     ISLocalToGlobalMappingCreate(comm, 1, nleaves, numbering_new, PETSC_OWN_POINTER, &ltogMigration);
1526:     ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);
1527:     ISDestroy(&isOriginal);
1528:   } else {
1529:     /* One-to-all distribution pattern: We can derive LToG from SF */
1530:     ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);
1531:   }
1532:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);
1533:   if (flg) {
1534:     PetscPrintf(comm, "Point renumbering for DM migration:\n");
1535:     ISLocalToGlobalMappingView(ltogMigration, NULL);
1536:   }
1537:   /* Migrate DM data to target DM */
1538:   DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);
1539:   DMPlexDistributeLabels(dm, sf, targetDM);
1540:   DMPlexDistributeCoordinates(dm, sf, targetDM);
1541:   DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);
1542:   DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);
1543:   ISLocalToGlobalMappingDestroy(&ltogOriginal);
1544:   ISLocalToGlobalMappingDestroy(&ltogMigration);
1545:   PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);
1546:   return(0);
1547: }

1549: /*@C
1550:   DMPlexDistribute - Distributes the mesh and any associated sections.

1552:   Collective on dm

1554:   Input Parameter:
1555: + dm  - The original DMPlex object
1556: - overlap - The overlap of partitions, 0 is the default

1558:   Output Parameter:
1559: + sf - The PetscSF used for point distribution, or NULL if not needed
1560: - dmParallel - The distributed DMPlex object

1562:   Note: If the mesh was not distributed, the output dmParallel will be NULL.

1564:   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
1565:   representation on the mesh.

1567:   Level: intermediate

1569: .seealso: DMPlexCreate(), DMSetAdjacency()
1570: @*/
1571: PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
1572: {
1573:   MPI_Comm               comm;
1574:   PetscPartitioner       partitioner;
1575:   IS                     cellPart;
1576:   PetscSection           cellPartSection;
1577:   DM                     dmCoord;
1578:   DMLabel                lblPartition, lblMigration;
1579:   PetscSF                sfMigration, sfStratified, sfPoint;
1580:   PetscBool              flg, balance;
1581:   PetscMPIInt            rank, size;
1582:   PetscErrorCode         ierr;


1590:   if (sf) *sf = NULL;
1591:   *dmParallel = NULL;
1592:   PetscObjectGetComm((PetscObject)dm,&comm);
1593:   MPI_Comm_rank(comm, &rank);
1594:   MPI_Comm_size(comm, &size);
1595:   if (size == 1) return(0);

1597:   PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);
1598:   /* Create cell partition */
1599:   PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);
1600:   PetscSectionCreate(comm, &cellPartSection);
1601:   DMPlexGetPartitioner(dm, &partitioner);
1602:   PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);
1603:   PetscLogEventBegin(DMPLEX_PartSelf,dm,0,0,0);
1604:   {
1605:     /* Convert partition to DMLabel */
1606:     IS             is;
1607:     PetscHSetI     ht;
1608:     const PetscInt *points;
1609:     PetscInt       *iranks;
1610:     PetscInt       pStart, pEnd, proc, npoints, poff = 0, nranks;

1612:     DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition);
1613:     /* Preallocate strata */
1614:     PetscHSetICreate(&ht);
1615:     PetscSectionGetChart(cellPartSection, &pStart, &pEnd);
1616:     for (proc = pStart; proc < pEnd; proc++) {
1617:       PetscSectionGetDof(cellPartSection, proc, &npoints);
1618:       if (npoints) {PetscHSetIAdd(ht, proc);}
1619:     }
1620:     PetscHSetIGetSize(ht, &nranks);
1621:     PetscMalloc1(nranks, &iranks);
1622:     PetscHSetIGetElems(ht, &poff, iranks);
1623:     PetscHSetIDestroy(&ht);
1624:     DMLabelAddStrata(lblPartition, nranks, iranks);
1625:     PetscFree(iranks);
1626:     /* Inline DMPlexPartitionLabelClosure() */
1627:     ISGetIndices(cellPart, &points);
1628:     PetscSectionGetChart(cellPartSection, &pStart, &pEnd);
1629:     for (proc = pStart; proc < pEnd; proc++) {
1630:       PetscSectionGetDof(cellPartSection, proc, &npoints);
1631:       if (!npoints) continue;
1632:       PetscSectionGetOffset(cellPartSection, proc, &poff);
1633:       DMPlexClosurePoints_Private(dm, npoints, points+poff, &is);
1634:       DMLabelSetStratumIS(lblPartition, proc, is);
1635:       ISDestroy(&is);
1636:     }
1637:     ISRestoreIndices(cellPart, &points);
1638:   }
1639:   PetscLogEventEnd(DMPLEX_PartSelf,dm,0,0,0);

1641:   DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration);
1642:   DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration);
1643:   DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);
1644:   DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);
1645:   PetscSFDestroy(&sfMigration);
1646:   sfMigration = sfStratified;
1647:   PetscSFSetUp(sfMigration);
1648:   PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);
1649:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);
1650:   if (flg) {
1651:     DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_(comm));
1652:     PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_(comm));
1653:   }

1655:   /* Create non-overlapping parallel DM and migrate internal data */
1656:   DMPlexCreate(comm, dmParallel);
1657:   PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");
1658:   DMPlexMigrate(dm, sfMigration, *dmParallel);

1660:   /* Build the point SF without overlap */
1661:   DMPlexGetPartitionBalance(dm, &balance);
1662:   DMPlexSetPartitionBalance(*dmParallel, balance);
1663:   DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);
1664:   DMSetPointSF(*dmParallel, sfPoint);
1665:   DMGetCoordinateDM(*dmParallel, &dmCoord);
1666:   if (dmCoord) {DMSetPointSF(dmCoord, sfPoint);}
1667:   if (flg) {PetscSFView(sfPoint, NULL);}

1669:   if (overlap > 0) {
1670:     DM                 dmOverlap;
1671:     PetscInt           nroots, nleaves, noldleaves, l;
1672:     const PetscInt    *oldLeaves;
1673:     PetscSFNode       *newRemote, *permRemote;
1674:     const PetscSFNode *oldRemote;
1675:     PetscSF            sfOverlap, sfOverlapPoint;

1677:     /* Add the partition overlap to the distributed DM */
1678:     DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);
1679:     DMDestroy(dmParallel);
1680:     *dmParallel = dmOverlap;
1681:     if (flg) {
1682:       PetscPrintf(comm, "Overlap Migration SF:\n");
1683:       PetscSFView(sfOverlap, NULL);
1684:     }

1686:     /* Re-map the migration SF to establish the full migration pattern */
1687:     PetscSFGetGraph(sfMigration, &nroots, &noldleaves, &oldLeaves, &oldRemote);
1688:     PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);
1689:     PetscMalloc1(nleaves, &newRemote);
1690:     /* oldRemote: original root point mapping to original leaf point
1691:        newRemote: original leaf point mapping to overlapped leaf point */
1692:     if (oldLeaves) {
1693:       /* After stratification, the migration remotes may not be in root (canonical) order, so we reorder using the leaf numbering */
1694:       PetscMalloc1(noldleaves, &permRemote);
1695:       for (l = 0; l < noldleaves; ++l) permRemote[oldLeaves[l]] = oldRemote[l];
1696:       oldRemote = permRemote;
1697:     }
1698:     PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);
1699:     PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);
1700:     if (oldLeaves) {PetscFree(oldRemote);}
1701:     PetscSFCreate(comm, &sfOverlapPoint);
1702:     PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);
1703:     PetscSFDestroy(&sfOverlap);
1704:     PetscSFDestroy(&sfMigration);
1705:     sfMigration = sfOverlapPoint;
1706:   }
1707:   /* Cleanup Partition */
1708:   DMLabelDestroy(&lblPartition);
1709:   DMLabelDestroy(&lblMigration);
1710:   PetscSectionDestroy(&cellPartSection);
1711:   ISDestroy(&cellPart);
1712:   /* Copy BC */
1713:   DMCopyBoundary(dm, *dmParallel);
1714:   /* Create sfNatural */
1715:   if (dm->useNatural) {
1716:     PetscSection section;

1718:     DMGetLocalSection(dm, &section);
1719:     DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);
1720:     DMSetUseNatural(*dmParallel, PETSC_TRUE);
1721:   }
1722:   /* Cleanup */
1723:   if (sf) {*sf = sfMigration;}
1724:   else    {PetscSFDestroy(&sfMigration);}
1725:   PetscSFDestroy(&sfPoint);
1726:   PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);
1727:   return(0);
1728: }

1730: /*@C
1731:   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.

1733:   Collective on dm

1735:   Input Parameter:
1736: + dm  - The non-overlapping distrbuted DMPlex object
1737: - overlap - The overlap of partitions

1739:   Output Parameter:
1740: + sf - The PetscSF used for point distribution
1741: - dmOverlap - The overlapping distributed DMPlex object, or NULL

1743:   Note: If the mesh was not distributed, the return value is NULL.

1745:   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
1746:   representation on the mesh.

1748:   Level: intermediate

1750: .seealso: DMPlexCreate(), DMSetAdjacency()
1751: @*/
1752: PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1753: {
1754:   MPI_Comm               comm;
1755:   PetscMPIInt            size, rank;
1756:   PetscSection           rootSection, leafSection;
1757:   IS                     rootrank, leafrank;
1758:   DM                     dmCoord;
1759:   DMLabel                lblOverlap;
1760:   PetscSF                sfOverlap, sfStratified, sfPoint;
1761:   PetscErrorCode         ierr;


1768:   if (sf) *sf = NULL;
1769:   *dmOverlap  = NULL;
1770:   PetscObjectGetComm((PetscObject)dm,&comm);
1771:   MPI_Comm_size(comm, &size);
1772:   MPI_Comm_rank(comm, &rank);
1773:   if (size == 1) return(0);

1775:   PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);
1776:   /* Compute point overlap with neighbouring processes on the distributed DM */
1777:   PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);
1778:   PetscSectionCreate(comm, &rootSection);
1779:   PetscSectionCreate(comm, &leafSection);
1780:   DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);
1781:   DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);
1782:   /* Convert overlap label to stratified migration SF */
1783:   DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);
1784:   DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);
1785:   PetscSFDestroy(&sfOverlap);
1786:   sfOverlap = sfStratified;
1787:   PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");
1788:   PetscSFSetFromOptions(sfOverlap);

1790:   PetscSectionDestroy(&rootSection);
1791:   PetscSectionDestroy(&leafSection);
1792:   ISDestroy(&rootrank);
1793:   ISDestroy(&leafrank);
1794:   PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);

1796:   /* Build the overlapping DM */
1797:   DMPlexCreate(comm, dmOverlap);
1798:   PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");
1799:   DMPlexMigrate(dm, sfOverlap, *dmOverlap);
1800:   /* Build the new point SF */
1801:   DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);
1802:   DMSetPointSF(*dmOverlap, sfPoint);
1803:   DMGetCoordinateDM(*dmOverlap, &dmCoord);
1804:   if (dmCoord) {DMSetPointSF(dmCoord, sfPoint);}
1805:   PetscSFDestroy(&sfPoint);
1806:   /* Cleanup overlap partition */
1807:   DMLabelDestroy(&lblOverlap);
1808:   if (sf) *sf = sfOverlap;
1809:   else    {PetscSFDestroy(&sfOverlap);}
1810:   PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);
1811:   return(0);
1812: }

1814: /*@C
1815:   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
1816:   root process of the original's communicator.

1818:   Collective on dm

1820:   Input Parameters:
1821: . dm - the original DMPlex object

1823:   Output Parameters:
1824: + sf - the PetscSF used for point distribution (optional)
1825: - gatherMesh - the gathered DM object, or NULL

1827:   Level: intermediate

1829: .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
1830: @*/
1831: PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh)
1832: {
1833:   MPI_Comm       comm;
1834:   PetscMPIInt    size;
1835:   PetscPartitioner oldPart, gatherPart;

1841:   *gatherMesh = NULL;
1842:   if (sf) *sf = NULL;
1843:   comm = PetscObjectComm((PetscObject)dm);
1844:   MPI_Comm_size(comm,&size);
1845:   if (size == 1) return(0);
1846:   DMPlexGetPartitioner(dm,&oldPart);
1847:   PetscObjectReference((PetscObject)oldPart);
1848:   PetscPartitionerCreate(comm,&gatherPart);
1849:   PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);
1850:   DMPlexSetPartitioner(dm,gatherPart);
1851:   DMPlexDistribute(dm,0,sf,gatherMesh);

1853:   DMPlexSetPartitioner(dm,oldPart);
1854:   PetscPartitionerDestroy(&gatherPart);
1855:   PetscPartitionerDestroy(&oldPart);
1856:   return(0);
1857: }

1859: /*@C
1860:   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.

1862:   Collective on dm

1864:   Input Parameters:
1865: . dm - the original DMPlex object

1867:   Output Parameters:
1868: + sf - the PetscSF used for point distribution (optional)
1869: - redundantMesh - the redundant DM object, or NULL

1871:   Level: intermediate

1873: .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
1874: @*/
1875: PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh)
1876: {
1877:   MPI_Comm       comm;
1878:   PetscMPIInt    size, rank;
1879:   PetscInt       pStart, pEnd, p;
1880:   PetscInt       numPoints = -1;
1881:   PetscSF        migrationSF, sfPoint, gatherSF;
1882:   DM             gatherDM, dmCoord;
1883:   PetscSFNode    *points;

1889:   *redundantMesh = NULL;
1890:   comm = PetscObjectComm((PetscObject)dm);
1891:   MPI_Comm_size(comm,&size);
1892:   if (size == 1) {
1893:     PetscObjectReference((PetscObject) dm);
1894:     *redundantMesh = dm;
1895:     if (sf) *sf = NULL;
1896:     return(0);
1897:   }
1898:   DMPlexGetGatherDM(dm,&gatherSF,&gatherDM);
1899:   if (!gatherDM) return(0);
1900:   MPI_Comm_rank(comm,&rank);
1901:   DMPlexGetChart(gatherDM,&pStart,&pEnd);
1902:   numPoints = pEnd - pStart;
1903:   MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);
1904:   PetscMalloc1(numPoints,&points);
1905:   PetscSFCreate(comm,&migrationSF);
1906:   for (p = 0; p < numPoints; p++) {
1907:     points[p].index = p;
1908:     points[p].rank  = 0;
1909:   }
1910:   PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);
1911:   DMPlexCreate(comm, redundantMesh);
1912:   PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");
1913:   DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);
1914:   DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);
1915:   DMSetPointSF(*redundantMesh, sfPoint);
1916:   DMGetCoordinateDM(*redundantMesh, &dmCoord);
1917:   if (dmCoord) {DMSetPointSF(dmCoord, sfPoint);}
1918:   PetscSFDestroy(&sfPoint);
1919:   if (sf) {
1920:     PetscSF tsf;

1922:     PetscSFCompose(gatherSF,migrationSF,&tsf);
1923:     DMPlexStratifyMigrationSF(dm, tsf, sf);
1924:     PetscSFDestroy(&tsf);
1925:   }
1926:   PetscSFDestroy(&migrationSF);
1927:   PetscSFDestroy(&gatherSF);
1928:   DMDestroy(&gatherDM);
1929:   return(0);
1930: }