xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision f88a03de40281234a9c101f39f0dee0d3418277d)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"   I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"  I*/
370034214SMatthew G. Knepley 
43c1f0c11SLawrence Mitchell /*@C
53c1f0c11SLawrence Mitchell   DMPlexSetAdjacencyUser - Define adjacency in the mesh using a user-provided callback
63c1f0c11SLawrence Mitchell 
73c1f0c11SLawrence Mitchell   Input Parameters:
83c1f0c11SLawrence Mitchell + dm      - The DM object
93c1f0c11SLawrence Mitchell . user    - The user callback, may be NULL (to clear the callback)
103c1f0c11SLawrence Mitchell - ctx     - context for callback evaluation, may be NULL
113c1f0c11SLawrence Mitchell 
123c1f0c11SLawrence Mitchell   Level: advanced
133c1f0c11SLawrence Mitchell 
143c1f0c11SLawrence Mitchell   Notes:
153c1f0c11SLawrence Mitchell      The caller of DMPlexGetAdjacency may need to arrange that a large enough array is available for the adjacency.
163c1f0c11SLawrence Mitchell 
173c1f0c11SLawrence Mitchell      Any setting here overrides other configuration of DMPlex adjacency determination.
183c1f0c11SLawrence Mitchell 
19db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexGetAdjacency()`, `DMPlexGetAdjacencyUser()`
203c1f0c11SLawrence Mitchell @*/
219371c9d4SSatish Balay PetscErrorCode DMPlexSetAdjacencyUser(DM dm, PetscErrorCode (*user)(DM, PetscInt, PetscInt *, PetscInt[], void *), void *ctx) {
223c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
233c1f0c11SLawrence Mitchell 
243c1f0c11SLawrence Mitchell   PetscFunctionBegin;
253c1f0c11SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
263c1f0c11SLawrence Mitchell   mesh->useradjacency    = user;
273c1f0c11SLawrence Mitchell   mesh->useradjacencyctx = ctx;
283c1f0c11SLawrence Mitchell   PetscFunctionReturn(0);
293c1f0c11SLawrence Mitchell }
303c1f0c11SLawrence Mitchell 
313c1f0c11SLawrence Mitchell /*@C
323c1f0c11SLawrence Mitchell   DMPlexGetAdjacencyUser - get the user-defined adjacency callback
333c1f0c11SLawrence Mitchell 
343c1f0c11SLawrence Mitchell   Input Parameter:
353c1f0c11SLawrence Mitchell . dm      - The DM object
363c1f0c11SLawrence Mitchell 
373c1f0c11SLawrence Mitchell   Output Parameters:
38ef1023bdSBarry Smith + user    - The callback
393c1f0c11SLawrence Mitchell - ctx     - context for callback evaluation
403c1f0c11SLawrence Mitchell 
413c1f0c11SLawrence Mitchell   Level: advanced
423c1f0c11SLawrence Mitchell 
43db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexGetAdjacency()`, `DMPlexSetAdjacencyUser()`
443c1f0c11SLawrence Mitchell @*/
459371c9d4SSatish Balay PetscErrorCode DMPlexGetAdjacencyUser(DM dm, PetscErrorCode (**user)(DM, PetscInt, PetscInt *, PetscInt[], void *), void **ctx) {
463c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
473c1f0c11SLawrence Mitchell 
483c1f0c11SLawrence Mitchell   PetscFunctionBegin;
493c1f0c11SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
503c1f0c11SLawrence Mitchell   if (user) *user = mesh->useradjacency;
513c1f0c11SLawrence Mitchell   if (ctx) *ctx = mesh->useradjacencyctx;
523c1f0c11SLawrence Mitchell   PetscFunctionReturn(0);
533c1f0c11SLawrence Mitchell }
543c1f0c11SLawrence Mitchell 
5570034214SMatthew G. Knepley /*@
56a17985deSToby Isaac   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.
578b0b4c70SToby Isaac 
588b0b4c70SToby Isaac   Input Parameters:
598b0b4c70SToby Isaac + dm      - The DM object
605b317d89SToby Isaac - useAnchors - Flag to use the constraints.  If PETSC_TRUE, then constrained points are omitted from DMPlexGetAdjacency(), and their anchor points appear in their place.
618b0b4c70SToby Isaac 
628b0b4c70SToby Isaac   Level: intermediate
638b0b4c70SToby Isaac 
64db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexSetAnchors()`
658b0b4c70SToby Isaac @*/
669371c9d4SSatish Balay PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors) {
678b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *)dm->data;
688b0b4c70SToby Isaac 
698b0b4c70SToby Isaac   PetscFunctionBegin;
708b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
715b317d89SToby Isaac   mesh->useAnchors = useAnchors;
728b0b4c70SToby Isaac   PetscFunctionReturn(0);
738b0b4c70SToby Isaac }
748b0b4c70SToby Isaac 
758b0b4c70SToby Isaac /*@
76a17985deSToby Isaac   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.
778b0b4c70SToby Isaac 
788b0b4c70SToby Isaac   Input Parameter:
798b0b4c70SToby Isaac . dm      - The DM object
808b0b4c70SToby Isaac 
818b0b4c70SToby Isaac   Output Parameter:
825b317d89SToby Isaac . useAnchors - Flag to use the closure.  If PETSC_TRUE, then constrained points are omitted from DMPlexGetAdjacency(), and their anchor points appear in their place.
838b0b4c70SToby Isaac 
848b0b4c70SToby Isaac   Level: intermediate
858b0b4c70SToby Isaac 
86db781477SPatrick Sanan .seealso: `DMPlexSetAdjacencyUseAnchors()`, `DMSetAdjacency()`, `DMGetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexSetAnchors()`
878b0b4c70SToby Isaac @*/
889371c9d4SSatish Balay PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors) {
898b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *)dm->data;
908b0b4c70SToby Isaac 
918b0b4c70SToby Isaac   PetscFunctionBegin;
928b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93064a246eSJacob Faibussowitsch   PetscValidBoolPointer(useAnchors, 2);
945b317d89SToby Isaac   *useAnchors = mesh->useAnchors;
958b0b4c70SToby Isaac   PetscFunctionReturn(0);
968b0b4c70SToby Isaac }
978b0b4c70SToby Isaac 
989371c9d4SSatish Balay static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) {
9970034214SMatthew G. Knepley   const PetscInt *cone   = NULL;
10070034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
10170034214SMatthew G. Knepley 
10270034214SMatthew G. Knepley   PetscFunctionBeginHot;
1039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
1049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, p, &cone));
1054b6b44bdSMatthew G. Knepley   for (c = 0; c <= coneSize; ++c) {
1064b6b44bdSMatthew G. Knepley     const PetscInt  point   = !c ? p : cone[c - 1];
10770034214SMatthew G. Knepley     const PetscInt *support = NULL;
10870034214SMatthew G. Knepley     PetscInt        supportSize, s, q;
10970034214SMatthew G. Knepley 
1109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
1119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, point, &support));
11270034214SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
113527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]), 0); ++q) {
11470034214SMatthew G. Knepley         if (support[s] == adj[q]) break;
11570034214SMatthew G. Knepley       }
11663a3b9bcSJacob Faibussowitsch       PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize);
11770034214SMatthew G. Knepley     }
11870034214SMatthew G. Knepley   }
11970034214SMatthew G. Knepley   *adjSize = numAdj;
12070034214SMatthew G. Knepley   PetscFunctionReturn(0);
12170034214SMatthew G. Knepley }
12270034214SMatthew G. Knepley 
1239371c9d4SSatish Balay static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) {
12470034214SMatthew G. Knepley   const PetscInt *support = NULL;
12570034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, supportSize, s;
12670034214SMatthew G. Knepley 
12770034214SMatthew G. Knepley   PetscFunctionBeginHot;
1289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
1299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSupport(dm, p, &support));
1304b6b44bdSMatthew G. Knepley   for (s = 0; s <= supportSize; ++s) {
1314b6b44bdSMatthew G. Knepley     const PetscInt  point = !s ? p : support[s - 1];
13270034214SMatthew G. Knepley     const PetscInt *cone  = NULL;
13370034214SMatthew G. Knepley     PetscInt        coneSize, c, q;
13470034214SMatthew G. Knepley 
1359566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
1369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, point, &cone));
13770034214SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
138527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]), 0); ++q) {
13970034214SMatthew G. Knepley         if (cone[c] == adj[q]) break;
14070034214SMatthew G. Knepley       }
14163a3b9bcSJacob Faibussowitsch       PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize);
14270034214SMatthew G. Knepley     }
14370034214SMatthew G. Knepley   }
14470034214SMatthew G. Knepley   *adjSize = numAdj;
14570034214SMatthew G. Knepley   PetscFunctionReturn(0);
14670034214SMatthew G. Knepley }
14770034214SMatthew G. Knepley 
1489371c9d4SSatish Balay static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[]) {
14970034214SMatthew G. Knepley   PetscInt *star   = NULL;
15070034214SMatthew G. Knepley   PetscInt  numAdj = 0, maxAdjSize = *adjSize, starSize, s;
15170034214SMatthew G. Knepley 
15270034214SMatthew G. Knepley   PetscFunctionBeginHot;
1539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star));
15470034214SMatthew G. Knepley   for (s = 0; s < starSize * 2; s += 2) {
15570034214SMatthew G. Knepley     const PetscInt *closure = NULL;
15670034214SMatthew G. Knepley     PetscInt        closureSize, c, q;
15770034214SMatthew G. Knepley 
1589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt **)&closure));
15970034214SMatthew G. Knepley     for (c = 0; c < closureSize * 2; c += 2) {
160527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]), 0); ++q) {
16170034214SMatthew G. Knepley         if (closure[c] == adj[q]) break;
16270034214SMatthew G. Knepley       }
16363a3b9bcSJacob Faibussowitsch       PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize);
16470034214SMatthew G. Knepley     }
1659566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt **)&closure));
16670034214SMatthew G. Knepley   }
1679566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star));
16870034214SMatthew G. Knepley   *adjSize = numAdj;
16970034214SMatthew G. Knepley   PetscFunctionReturn(0);
17070034214SMatthew G. Knepley }
17170034214SMatthew G. Knepley 
1729371c9d4SSatish Balay PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[]) {
17379bad331SMatthew G. Knepley   static PetscInt asiz       = 0;
1748b0b4c70SToby Isaac   PetscInt        maxAnchors = 1;
1758b0b4c70SToby Isaac   PetscInt        aStart = -1, aEnd = -1;
1768b0b4c70SToby Isaac   PetscInt        maxAdjSize;
1778b0b4c70SToby Isaac   PetscSection    aSec = NULL;
1788b0b4c70SToby Isaac   IS              aIS  = NULL;
1798b0b4c70SToby Isaac   const PetscInt *anchors;
1803c1f0c11SLawrence Mitchell   DM_Plex        *mesh = (DM_Plex *)dm->data;
18170034214SMatthew G. Knepley 
18270034214SMatthew G. Knepley   PetscFunctionBeginHot;
1835b317d89SToby Isaac   if (useAnchors) {
1849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS));
1858b0b4c70SToby Isaac     if (aSec) {
1869566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetMaxDof(aSec, &maxAnchors));
18724c766afSToby Isaac       maxAnchors = PetscMax(1, maxAnchors);
1889566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(aSec, &aStart, &aEnd));
1899566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(aIS, &anchors));
1908b0b4c70SToby Isaac     }
1918b0b4c70SToby Isaac   }
19279bad331SMatthew G. Knepley   if (!*adj) {
19324c766afSToby Isaac     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;
19479bad331SMatthew G. Knepley 
1959566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1969566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
197412e9a14SMatthew G. Knepley     depth = PetscMax(depth, -depth);
1989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetMaxSizes(dm, &maxC, &maxS));
19924c766afSToby Isaac     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC, depth + 1) - 1) / (maxC - 1)) : depth + 1;
20024c766afSToby Isaac     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS, depth + 1) - 1) / (maxS - 1)) : depth + 1;
20124c766afSToby Isaac     asiz          = PetscMax(PetscPowInt(maxS, depth) * coneSeries, PetscPowInt(maxC, depth) * supportSeries);
2028b0b4c70SToby Isaac     asiz *= maxAnchors;
20324c766afSToby Isaac     asiz = PetscMin(asiz, pEnd - pStart);
2049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(asiz, adj));
20579bad331SMatthew G. Knepley   }
20679bad331SMatthew G. Knepley   if (*adjSize < 0) *adjSize = asiz;
2078b0b4c70SToby Isaac   maxAdjSize = *adjSize;
2083c1f0c11SLawrence Mitchell   if (mesh->useradjacency) {
2099566063dSJacob Faibussowitsch     PetscCall((*mesh->useradjacency)(dm, p, adjSize, *adj, mesh->useradjacencyctx));
2103c1f0c11SLawrence Mitchell   } else if (useTransitiveClosure) {
2119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj));
21270034214SMatthew G. Knepley   } else if (useCone) {
2139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj));
21470034214SMatthew G. Knepley   } else {
2159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj));
21670034214SMatthew G. Knepley   }
2175b317d89SToby Isaac   if (useAnchors && aSec) {
2188b0b4c70SToby Isaac     PetscInt  origSize = *adjSize;
2198b0b4c70SToby Isaac     PetscInt  numAdj   = origSize;
2208b0b4c70SToby Isaac     PetscInt  i        = 0, j;
2218b0b4c70SToby Isaac     PetscInt *orig     = *adj;
2228b0b4c70SToby Isaac 
2238b0b4c70SToby Isaac     while (i < origSize) {
2248b0b4c70SToby Isaac       PetscInt p    = orig[i];
2258b0b4c70SToby Isaac       PetscInt aDof = 0;
2268b0b4c70SToby Isaac 
22748a46eb9SPierre Jolivet       if (p >= aStart && p < aEnd) PetscCall(PetscSectionGetDof(aSec, p, &aDof));
2288b0b4c70SToby Isaac       if (aDof) {
2298b0b4c70SToby Isaac         PetscInt aOff;
2308b0b4c70SToby Isaac         PetscInt s, q;
2318b0b4c70SToby Isaac 
232ad540459SPierre Jolivet         for (j = i + 1; j < numAdj; j++) orig[j - 1] = orig[j];
2338b0b4c70SToby Isaac         origSize--;
2348b0b4c70SToby Isaac         numAdj--;
2359566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(aSec, p, &aOff));
2368b0b4c70SToby Isaac         for (s = 0; s < aDof; ++s) {
237527e7439SSatish Balay           for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff + s]), 0); ++q) {
2388b0b4c70SToby Isaac             if (anchors[aOff + s] == orig[q]) break;
2398b0b4c70SToby Isaac           }
24063a3b9bcSJacob Faibussowitsch           PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize);
2418b0b4c70SToby Isaac         }
2429371c9d4SSatish Balay       } else {
2438b0b4c70SToby Isaac         i++;
2448b0b4c70SToby Isaac       }
2458b0b4c70SToby Isaac     }
2468b0b4c70SToby Isaac     *adjSize = numAdj;
2479566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(aIS, &anchors));
2488b0b4c70SToby Isaac   }
24970034214SMatthew G. Knepley   PetscFunctionReturn(0);
25070034214SMatthew G. Knepley }
25170034214SMatthew G. Knepley 
25270034214SMatthew G. Knepley /*@
25370034214SMatthew G. Knepley   DMPlexGetAdjacency - Return all points adjacent to the given point
25470034214SMatthew G. Knepley 
25570034214SMatthew G. Knepley   Input Parameters:
25670034214SMatthew G. Knepley + dm - The DM object
2576b867d5aSJose E. Roman - p  - The point
25870034214SMatthew G. Knepley 
2596b867d5aSJose E. Roman   Input/Output Parameters:
2606b867d5aSJose E. Roman + adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE;
2616b867d5aSJose E. Roman             on output the number of adjacent points
2626b867d5aSJose E. Roman - adj - Either NULL so that the array is allocated, or an existing array with size adjSize;
2636b867d5aSJose E. Roman         on output contains the adjacent points
26470034214SMatthew G. Knepley 
26570034214SMatthew G. Knepley   Level: advanced
26670034214SMatthew G. Knepley 
26795452b02SPatrick Sanan   Notes:
26895452b02SPatrick Sanan     The user must PetscFree the adj array if it was not passed in.
26970034214SMatthew G. Knepley 
270db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMCreateMatrix()`, `DMPlexPreallocateOperator()`
27170034214SMatthew G. Knepley @*/
2729371c9d4SSatish Balay PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[]) {
2731cf84007SMatthew G. Knepley   PetscBool useCone, useClosure, useAnchors;
27470034214SMatthew G. Knepley 
27570034214SMatthew G. Knepley   PetscFunctionBeginHot;
27670034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
277dadcf809SJacob Faibussowitsch   PetscValidIntPointer(adjSize, 3);
27870034214SMatthew G. Knepley   PetscValidPointer(adj, 4);
2799566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
2809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors));
2819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj));
28270034214SMatthew G. Knepley   PetscFunctionReturn(0);
28370034214SMatthew G. Knepley }
28408633170SToby Isaac 
285b0a623aaSMatthew G. Knepley /*@
286b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
287b0a623aaSMatthew G. Knepley 
288d083f849SBarry Smith   Collective on dm
289b0a623aaSMatthew G. Knepley 
290b0a623aaSMatthew G. Knepley   Input Parameters:
291b0a623aaSMatthew G. Knepley + dm      - The DM
2926b867d5aSJose E. Roman . sfPoint - The PetscSF which encodes point connectivity
2936b867d5aSJose E. Roman . rootRankSection -
2946b867d5aSJose E. Roman . rootRanks -
2956b867d5aSJose E. Roman . leftRankSection -
2966b867d5aSJose E. Roman - leafRanks -
297b0a623aaSMatthew G. Knepley 
298b0a623aaSMatthew G. Knepley   Output Parameters:
299b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
300b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
301b0a623aaSMatthew G. Knepley 
302b0a623aaSMatthew G. Knepley   Level: developer
303b0a623aaSMatthew G. Knepley 
304db781477SPatrick Sanan .seealso: `PetscSFCreate()`, `DMPlexCreateProcessSF()`
305b0a623aaSMatthew G. Knepley @*/
3069371c9d4SSatish Balay PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess) {
307b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
308b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
309b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
310b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
311b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
312b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
313b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
3149852e123SBarry Smith   PetscMPIInt        size, proc, rank;
315b0a623aaSMatthew G. Knepley 
316b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
317b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
318b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
319ad540459SPierre Jolivet   if (processRanks) PetscValidPointer(processRanks, 7);
320ad540459SPierre Jolivet   if (sfProcess) PetscValidPointer(sfProcess, 8);
3219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
3229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
3239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints));
3249566063dSJacob Faibussowitsch   PetscCall(PetscBTCreate(size, &neighbors));
3259566063dSJacob Faibussowitsch   PetscCall(PetscBTMemzero(size, neighbors));
326b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
3279566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(rootRankSection, &pStart, &pEnd));
3289566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(rootRanks, &nranks));
329b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
330b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
331b0a623aaSMatthew G. Knepley 
3329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(rootRankSection, p, &ndof));
3339566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(rootRankSection, p, &noff));
3349566063dSJacob Faibussowitsch     for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff + n]));
335b0a623aaSMatthew G. Knepley   }
3369566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(rootRanks, &nranks));
337b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
3389566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(leafRankSection, &pStart, &pEnd));
3399566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(leafRanks, &nranks));
340b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
341b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
342b0a623aaSMatthew G. Knepley 
3439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(leafRankSection, p, &ndof));
3449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(leafRankSection, p, &noff));
3459566063dSJacob Faibussowitsch     for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff + n]));
346b0a623aaSMatthew G. Knepley   }
3479566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(leafRanks, &nranks));
348b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
349ad540459SPierre Jolivet   for (l = 0; l < numLeaves; ++l) PetscBTSet(neighbors, remotePoints[l].rank);
350b0a623aaSMatthew G. Knepley   /* Calculate edges */
351b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
3529371c9d4SSatish Balay   for (proc = 0, numNeighbors = 0; proc < size; ++proc) {
3539371c9d4SSatish Balay     if (PetscBTLookup(neighbors, proc)) ++numNeighbors;
3549371c9d4SSatish Balay   }
3559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numNeighbors, &ranksNew));
3569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numNeighbors, &localPointsNew));
3579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numNeighbors, &remotePointsNew));
3589852e123SBarry Smith   for (proc = 0, n = 0; proc < size; ++proc) {
359b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
360b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
361b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
362b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
363b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
364b0a623aaSMatthew G. Knepley       ++n;
365b0a623aaSMatthew G. Knepley     }
366b0a623aaSMatthew G. Knepley   }
3679566063dSJacob Faibussowitsch   PetscCall(PetscBTDestroy(&neighbors));
3689566063dSJacob Faibussowitsch   if (processRanks) PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks));
3699566063dSJacob Faibussowitsch   else PetscCall(PetscFree(ranksNew));
370b0a623aaSMatthew G. Knepley   if (sfProcess) {
3719566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess));
3729566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*sfProcess, "Two-Sided Process SF"));
3739566063dSJacob Faibussowitsch     PetscCall(PetscSFSetFromOptions(*sfProcess));
3749566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER));
375b0a623aaSMatthew G. Knepley   }
376b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
377b0a623aaSMatthew G. Knepley }
378b0a623aaSMatthew G. Knepley 
379b0a623aaSMatthew G. Knepley /*@
380b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
381b0a623aaSMatthew G. Knepley 
382d083f849SBarry Smith   Collective on dm
383b0a623aaSMatthew G. Knepley 
384b0a623aaSMatthew G. Knepley   Input Parameter:
385b0a623aaSMatthew G. Knepley . dm - The DM
386b0a623aaSMatthew G. Knepley 
387b0a623aaSMatthew G. Knepley   Output Parameters:
388b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
389b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
390b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
391b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
392b0a623aaSMatthew G. Knepley 
393b0a623aaSMatthew G. Knepley   Level: developer
394b0a623aaSMatthew G. Knepley 
395db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()`
396b0a623aaSMatthew G. Knepley @*/
3979371c9d4SSatish Balay PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank) {
398b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
399b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
400b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
401b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
402b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
403b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
404b0a623aaSMatthew G. Knepley 
405b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
4069566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4079566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
4089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4099566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
410b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
4119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rootSection, "Root Section"));
4129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(rootSection, pStart, pEnd));
4139566063dSJacob Faibussowitsch   PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
4149566063dSJacob Faibussowitsch   PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
4159566063dSJacob Faibussowitsch   for (p = pStart; p < pEnd; ++p) PetscCall(PetscSectionSetDof(rootSection, p, rootdegree[p - pStart]));
4169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(rootSection));
417b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
4189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(rootSection, &nedges));
4199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(pEnd - pStart, &myrank));
4209566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nedges, &remoterank));
421b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd - pStart; ++p) myrank[p] = rank;
4229566063dSJacob Faibussowitsch   PetscCall(PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank));
4239566063dSJacob Faibussowitsch   PetscCall(PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank));
4249566063dSJacob Faibussowitsch   PetscCall(PetscFree(myrank));
4259566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank));
426b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
4279566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)leafSection, "Leaf Section"));
4289566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank));
429b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
430b0a623aaSMatthew G. Knepley }
431b0a623aaSMatthew G. Knepley 
432c506a872SMatthew G. Knepley #if 0
433c506a872SMatthew G. Knepley static PetscErrorCode DMPlexCopyOverlapLabels(DM dm, DM ndm)
434c506a872SMatthew G. Knepley {
435c506a872SMatthew G. Knepley   DM_Plex *mesh  = (DM_Plex *) dm->data;
436c506a872SMatthew G. Knepley   DM_Plex *nmesh = (DM_Plex *) ndm->data;
437c506a872SMatthew G. Knepley 
438c506a872SMatthew G. Knepley   PetscFunctionBegin;
439c506a872SMatthew G. Knepley   if (mesh->numOvLabels) {
440c506a872SMatthew G. Knepley     const char *name;
441c506a872SMatthew G. Knepley     PetscInt    l;
442c506a872SMatthew G. Knepley 
443c506a872SMatthew G. Knepley     nmesh->numOvLabels = mesh->numOvLabels;
444c506a872SMatthew G. Knepley     for (l = 0; l < mesh->numOvLabels; ++l) {
445c506a872SMatthew G. Knepley       PetscCall(PetscObjectGetName((PetscObject) mesh->ovLabels[l], &name));
446c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(ndm, name, &nmesh->ovLabels[l]));
447c506a872SMatthew G. Knepley       nmesh->ovValues[l] = mesh->ovValues[l];
448c506a872SMatthew G. Knepley     }
449c506a872SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject) mesh->ovExLabel, &name));
450c506a872SMatthew G. Knepley     PetscCall(DMGetLabel(ndm, name, &nmesh->ovExLabel));
451c506a872SMatthew G. Knepley     nmesh->ovExValue = mesh->ovExValue;
452c506a872SMatthew G. Knepley   }
453c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
454c506a872SMatthew G. Knepley }
455c506a872SMatthew G. Knepley #endif
456c506a872SMatthew G. Knepley 
457278397a0SMatthew G. Knepley /*@C
458c506a872SMatthew G. Knepley   DMPlexCreateOverlapLabel - Compute a label indicating what overlap points should be sent to new processes
459b0a623aaSMatthew G. Knepley 
460d083f849SBarry Smith   Collective on dm
461b0a623aaSMatthew G. Knepley 
462b0a623aaSMatthew G. Knepley   Input Parameters:
463b0a623aaSMatthew G. Knepley + dm          - The DM
46424d039d7SMichael Lange . levels      - Number of overlap levels
465b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
466b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
467b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
468b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
469b0a623aaSMatthew G. Knepley 
470064ec1f3Sprj-   Output Parameter:
471b4ec6ac8SBarry Smith . ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
472b0a623aaSMatthew G. Knepley 
473b0a623aaSMatthew G. Knepley   Level: developer
474b0a623aaSMatthew G. Knepley 
475c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabelFromLabels()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()`
476b0a623aaSMatthew G. Knepley @*/
4779371c9d4SSatish Balay PetscErrorCode DMPlexCreateOverlapLabel(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) {
478e540f424SMichael Lange   MPI_Comm           comm;
479b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
480874ddda9SLisandro Dalcin   PetscSF            sfPoint;
481b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
482b0a623aaSMatthew G. Knepley   const PetscInt    *local;
4831fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
484b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
4851fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
4869852e123SBarry Smith   PetscMPIInt        rank, size;
48731bc6364SLisandro Dalcin   PetscBool          flg;
488b0a623aaSMatthew G. Knepley 
489b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
4906ba1a4c7SVaclav Hapla   *ovLabel = NULL;
4919566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4929566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
4939566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
4946ba1a4c7SVaclav Hapla   if (size == 1) PetscFunctionReturn(0);
4959566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
4969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
497d1674c6dSMatthew Knepley   if (!levels) {
498d1674c6dSMatthew Knepley     /* Add owned points */
4999566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel));
5009566063dSJacob Faibussowitsch     for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank));
501d1674c6dSMatthew Knepley     PetscFunctionReturn(0);
502d1674c6dSMatthew Knepley   }
5039566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(leafSection, &sStart, &sEnd));
5049566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote));
5059566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank));
506b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
507b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
508b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
509b0a623aaSMatthew G. Knepley 
5109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj));
5119566063dSJacob Faibussowitsch     for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank));
512b0a623aaSMatthew G. Knepley   }
5139566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(rootrank, &rrank));
5149566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(leafrank, &nrank));
515b0a623aaSMatthew G. Knepley   /* Handle roots */
516b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
517b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
518b0a623aaSMatthew G. Knepley 
519b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
520b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
5219566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(leafSection, p, &neighbors));
522b0a623aaSMatthew G. Knepley       if (neighbors) {
5239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(leafSection, p, &noff));
5249566063dSJacob Faibussowitsch         PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj));
525b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
526b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff + n];
527b0a623aaSMatthew G. Knepley 
528b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
5299566063dSJacob Faibussowitsch           for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank));
530b0a623aaSMatthew G. Knepley         }
531b0a623aaSMatthew G. Knepley       }
532b0a623aaSMatthew G. Knepley     }
533b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
5349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(rootSection, p, &neighbors));
535b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
5369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(rootSection, p, &noff));
5379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj));
538b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
539b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff + n];
540b0a623aaSMatthew G. Knepley 
541b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
5429566063dSJacob Faibussowitsch       for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank));
543b0a623aaSMatthew G. Knepley     }
544b0a623aaSMatthew G. Knepley   }
5459566063dSJacob Faibussowitsch   PetscCall(PetscFree(adj));
5469566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(rootrank, &rrank));
5479566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(leafrank, &nrank));
54824d039d7SMichael Lange   /* Add additional overlap levels */
549be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
550be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
5519566063dSJacob Faibussowitsch     PetscCall(DMPlexPartitionLabelPropagate(dm, ovAdjByRank));
552be200f8dSMichael Lange     /* Add next level of point donations to the label */
5539566063dSJacob Faibussowitsch     PetscCall(DMPlexPartitionLabelAdjacency(dm, ovAdjByRank));
554be200f8dSMichael Lange   }
55526a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
5569566063dSJacob Faibussowitsch   PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank));
5579566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-overlap_view", &flg));
558e540f424SMichael Lange   if (flg) {
559825f8a23SLisandro Dalcin     PetscViewer viewer;
5609566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer));
5619566063dSJacob Faibussowitsch     PetscCall(DMLabelView(ovAdjByRank, viewer));
562b0a623aaSMatthew G. Knepley   }
563874ddda9SLisandro Dalcin   /* Invert sender to receiver label */
5649566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel));
566a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
5679566063dSJacob Faibussowitsch   for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank));
568e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
5699566063dSJacob Faibussowitsch     PetscCall(DMLabelClearValue(*ovLabel, local[l], rank));
5709566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank));
571e540f424SMichael Lange   }
572e540f424SMichael Lange   /* Clean up */
5739566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&ovAdjByRank));
574b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
575b0a623aaSMatthew G. Knepley }
57670034214SMatthew G. Knepley 
5779371c9d4SSatish Balay static PetscErrorCode HandlePoint_Private(DM dm, PetscInt p, PetscSection section, const PetscInt ranks[], PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], DMLabel ovAdjByRank) {
578c506a872SMatthew G. Knepley   PetscInt neighbors, el;
579c506a872SMatthew G. Knepley 
580c506a872SMatthew G. Knepley   PetscFunctionBegin;
581c506a872SMatthew G. Knepley   PetscCall(PetscSectionGetDof(section, p, &neighbors));
582c506a872SMatthew G. Knepley   if (neighbors) {
583c506a872SMatthew G. Knepley     PetscInt   *adj     = NULL;
584c506a872SMatthew G. Knepley     PetscInt    adjSize = PETSC_DETERMINE, noff, n, a;
585c506a872SMatthew G. Knepley     PetscMPIInt rank;
586c506a872SMatthew G. Knepley 
587c506a872SMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
588c506a872SMatthew G. Knepley     PetscCall(PetscSectionGetOffset(section, p, &noff));
589c506a872SMatthew G. Knepley     PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj));
590c506a872SMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
591c506a872SMatthew G. Knepley       const PetscInt remoteRank = ranks[noff + n];
592c506a872SMatthew G. Knepley 
593c506a872SMatthew G. Knepley       if (remoteRank == rank) continue;
594c506a872SMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {
595c506a872SMatthew G. Knepley         PetscBool insert = PETSC_TRUE;
596c506a872SMatthew G. Knepley 
597c506a872SMatthew G. Knepley         for (el = 0; el < numExLabels; ++el) {
598c506a872SMatthew G. Knepley           PetscInt exVal;
599c506a872SMatthew G. Knepley           PetscCall(DMLabelGetValue(exLabel[el], adj[a], &exVal));
6009371c9d4SSatish Balay           if (exVal == exValue[el]) {
6019371c9d4SSatish Balay             insert = PETSC_FALSE;
6029371c9d4SSatish Balay             break;
6039371c9d4SSatish Balay           }
604c506a872SMatthew G. Knepley         }
605c506a872SMatthew G. Knepley         if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank));
606c506a872SMatthew G. Knepley       }
607c506a872SMatthew G. Knepley     }
608*f88a03deSMatthew G. Knepley     PetscCall(PetscFree(adj));
609c506a872SMatthew G. Knepley   }
610c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
611c506a872SMatthew G. Knepley }
612c506a872SMatthew G. Knepley 
613c506a872SMatthew G. Knepley /*@C
614c506a872SMatthew G. Knepley   DMPlexCreateOverlapLabelFromLabels - Compute a label indicating what overlap points should be sent to new processes
615c506a872SMatthew G. Knepley 
616c506a872SMatthew G. Knepley   Collective on dm
617c506a872SMatthew G. Knepley 
618c506a872SMatthew G. Knepley   Input Parameters:
619c506a872SMatthew G. Knepley + dm          - The DM
620c506a872SMatthew G. Knepley . numLabels   - The number of labels to draw candidate points from
621c506a872SMatthew G. Knepley . label       - An array of labels containing candidate points
622c506a872SMatthew G. Knepley . value       - An array of label values marking the candidate points
623c506a872SMatthew G. Knepley . numExLabels - The number of labels to use for exclusion
624c506a872SMatthew G. Knepley . exLabel     - An array of labels indicating points to be excluded, or NULL
625c506a872SMatthew G. Knepley . exValue     - An array of label values to be excluded, or NULL
626c506a872SMatthew G. Knepley . rootSection - The number of leaves for a given root point
627c506a872SMatthew G. Knepley . rootrank    - The rank of each edge into the root point
628c506a872SMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
629c506a872SMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
630c506a872SMatthew G. Knepley 
631c506a872SMatthew G. Knepley   Output Parameter:
632c506a872SMatthew G. Knepley . ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
633c506a872SMatthew G. Knepley 
634c506a872SMatthew G. Knepley   Note:
635c506a872SMatthew G. Knepley   The candidate points are only added to the overlap if they are adjacent to a shared point
636c506a872SMatthew G. Knepley 
637c506a872SMatthew G. Knepley   Level: developer
638c506a872SMatthew G. Knepley 
639c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()`
640c506a872SMatthew G. Knepley @*/
6419371c9d4SSatish Balay PetscErrorCode DMPlexCreateOverlapLabelFromLabels(DM dm, PetscInt numLabels, const DMLabel label[], const PetscInt value[], PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) {
642c506a872SMatthew G. Knepley   MPI_Comm           comm;
643c506a872SMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
644c506a872SMatthew G. Knepley   PetscSF            sfPoint;
645c506a872SMatthew G. Knepley   const PetscSFNode *remote;
646c506a872SMatthew G. Knepley   const PetscInt    *local;
647c506a872SMatthew G. Knepley   const PetscInt    *nrank, *rrank;
648c506a872SMatthew G. Knepley   PetscInt          *adj = NULL;
649c506a872SMatthew G. Knepley   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l, el;
650c506a872SMatthew G. Knepley   PetscMPIInt        rank, size;
651c506a872SMatthew G. Knepley   PetscBool          flg;
652c506a872SMatthew G. Knepley 
653c506a872SMatthew G. Knepley   PetscFunctionBegin;
654c506a872SMatthew G. Knepley   *ovLabel = NULL;
655c506a872SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
656c506a872SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
657c506a872SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
658c506a872SMatthew G. Knepley   if (size == 1) PetscFunctionReturn(0);
659c506a872SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sfPoint));
660c506a872SMatthew G. Knepley   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
661c506a872SMatthew G. Knepley   PetscCall(PetscSectionGetChart(leafSection, &sStart, &sEnd));
662c506a872SMatthew G. Knepley   PetscCall(PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote));
663c506a872SMatthew G. Knepley   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank));
664c506a872SMatthew G. Knepley   PetscCall(ISGetIndices(rootrank, &rrank));
665c506a872SMatthew G. Knepley   PetscCall(ISGetIndices(leafrank, &nrank));
666c506a872SMatthew G. Knepley   for (l = 0; l < numLabels; ++l) {
667c506a872SMatthew G. Knepley     IS              valIS;
668c506a872SMatthew G. Knepley     const PetscInt *points;
669c506a872SMatthew G. Knepley     PetscInt        n;
670c506a872SMatthew G. Knepley 
671c506a872SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label[l], value[l], &valIS));
672c506a872SMatthew G. Knepley     if (!valIS) continue;
673c506a872SMatthew G. Knepley     PetscCall(ISGetIndices(valIS, &points));
674c506a872SMatthew G. Knepley     PetscCall(ISGetLocalSize(valIS, &n));
675c506a872SMatthew G. Knepley     for (PetscInt i = 0; i < n; ++i) {
676c506a872SMatthew G. Knepley       const PetscInt p = points[i];
677c506a872SMatthew G. Knepley 
678c506a872SMatthew G. Knepley       if ((p >= sStart) && (p < sEnd)) {
679c506a872SMatthew G. Knepley         PetscInt loc, adjSize = PETSC_DETERMINE;
680c506a872SMatthew G. Knepley 
681c506a872SMatthew G. Knepley         /* Handle leaves: shared with the root point */
682c506a872SMatthew G. Knepley         if (local) PetscCall(PetscFindInt(p, nleaves, local, &loc));
683c506a872SMatthew G. Knepley         else loc = (p >= 0 && p < nleaves) ? p : -1;
684c506a872SMatthew G. Knepley         if (loc >= 0) {
685c506a872SMatthew G. Knepley           const PetscInt remoteRank = remote[loc].rank;
686c506a872SMatthew G. Knepley 
687c506a872SMatthew G. Knepley           PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj));
688c506a872SMatthew G. Knepley           for (PetscInt a = 0; a < adjSize; ++a) {
689c506a872SMatthew G. Knepley             PetscBool insert = PETSC_TRUE;
690c506a872SMatthew G. Knepley 
691c506a872SMatthew G. Knepley             for (el = 0; el < numExLabels; ++el) {
692c506a872SMatthew G. Knepley               PetscInt exVal;
693c506a872SMatthew G. Knepley               PetscCall(DMLabelGetValue(exLabel[el], adj[a], &exVal));
6949371c9d4SSatish Balay               if (exVal == exValue[el]) {
6959371c9d4SSatish Balay                 insert = PETSC_FALSE;
6969371c9d4SSatish Balay                 break;
6979371c9d4SSatish Balay               }
698c506a872SMatthew G. Knepley             }
699c506a872SMatthew G. Knepley             if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank));
700c506a872SMatthew G. Knepley           }
701c506a872SMatthew G. Knepley         }
702c506a872SMatthew G. Knepley         /* Some leaves share a root with other leaves on different processes */
703c506a872SMatthew G. Knepley         HandlePoint_Private(dm, p, leafSection, nrank, numExLabels, exLabel, exValue, ovAdjByRank);
704c506a872SMatthew G. Knepley       }
705c506a872SMatthew G. Knepley       /* Roots are shared with leaves */
706c506a872SMatthew G. Knepley       HandlePoint_Private(dm, p, rootSection, rrank, numExLabels, exLabel, exValue, ovAdjByRank);
707c506a872SMatthew G. Knepley     }
708c506a872SMatthew G. Knepley     PetscCall(ISRestoreIndices(valIS, &points));
709c506a872SMatthew G. Knepley     PetscCall(ISDestroy(&valIS));
710c506a872SMatthew G. Knepley   }
711c506a872SMatthew G. Knepley   PetscCall(PetscFree(adj));
712c506a872SMatthew G. Knepley   PetscCall(ISRestoreIndices(rootrank, &rrank));
713c506a872SMatthew G. Knepley   PetscCall(ISRestoreIndices(leafrank, &nrank));
714c506a872SMatthew G. Knepley   /* We require the closure in the overlap */
715c506a872SMatthew G. Knepley   PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank));
716c506a872SMatthew G. Knepley   PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-overlap_view", &flg));
717c506a872SMatthew G. Knepley   if (flg) {
718c506a872SMatthew G. Knepley     PetscViewer viewer;
719c506a872SMatthew G. Knepley     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer));
720c506a872SMatthew G. Knepley     PetscCall(DMLabelView(ovAdjByRank, viewer));
721c506a872SMatthew G. Knepley   }
722c506a872SMatthew G. Knepley   /* Invert sender to receiver label */
723c506a872SMatthew G. Knepley   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel));
724c506a872SMatthew G. Knepley   PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel));
725c506a872SMatthew G. Knepley   /* Add owned points, except for shared local points */
726c506a872SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank));
727c506a872SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
728c506a872SMatthew G. Knepley     PetscCall(DMLabelClearValue(*ovLabel, local[l], rank));
729c506a872SMatthew G. Knepley     PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank));
730c506a872SMatthew G. Knepley   }
731c506a872SMatthew G. Knepley   /* Clean up */
732c506a872SMatthew G. Knepley   PetscCall(DMLabelDestroy(&ovAdjByRank));
733c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
734c506a872SMatthew G. Knepley }
735c506a872SMatthew G. Knepley 
73624cc2ca5SMatthew G. Knepley /*@C
73724cc2ca5SMatthew G. Knepley   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF
73824cc2ca5SMatthew G. Knepley 
739d083f849SBarry Smith   Collective on dm
74024cc2ca5SMatthew G. Knepley 
74124cc2ca5SMatthew G. Knepley   Input Parameters:
74224cc2ca5SMatthew G. Knepley + dm          - The DM
74324cc2ca5SMatthew G. Knepley - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes
74424cc2ca5SMatthew G. Knepley 
745064ec1f3Sprj-   Output Parameter:
746a2b725a8SWilliam Gropp . migrationSF - An SF that maps original points in old locations to points in new locations
74724cc2ca5SMatthew G. Knepley 
74824cc2ca5SMatthew G. Knepley   Level: developer
74924cc2ca5SMatthew G. Knepley 
750db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistributeOverlap()`, `DMPlexDistribute()`
75124cc2ca5SMatthew G. Knepley @*/
7529371c9d4SSatish Balay PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF) {
75346f9b1c3SMichael Lange   MPI_Comm           comm;
7549852e123SBarry Smith   PetscMPIInt        rank, size;
75546f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
75646f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
75746f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
75846f9b1c3SMichael Lange   PetscSFNode       *iremote;
75946f9b1c3SMichael Lange   PetscSF            pointSF;
76046f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
76146f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
76246f9b1c3SMichael Lange 
76346f9b1c3SMichael Lange   PetscFunctionBegin;
76446f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
7669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
7679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
7689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
76946f9b1c3SMichael Lange 
77046f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
7719566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote));
7729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths));
77346f9b1c3SMichael Lange   for (d = 0; d < dim + 1; d++) {
7749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
77546f9b1c3SMichael Lange     for (p = pStart; p < pEnd; p++) pointDepths[p] = d;
77646f9b1c3SMichael Lange   }
77746f9b1c3SMichael Lange   for (p = 0; p < nleaves; p++) remoteDepths[p] = -1;
7789566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths, MPI_REPLACE));
7799566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths, MPI_REPLACE));
78046f9b1c3SMichael Lange 
7812d4ee042Sprj-   /* Count received points in each stratum and compute the internal strata shift */
7829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(dim + 1, &depthRecv, dim + 1, &depthShift, dim + 1, &depthIdx));
78346f9b1c3SMichael Lange   for (d = 0; d < dim + 1; d++) depthRecv[d] = 0;
78446f9b1c3SMichael Lange   for (p = 0; p < nleaves; p++) depthRecv[remoteDepths[p]]++;
78546f9b1c3SMichael Lange   depthShift[dim] = 0;
78646f9b1c3SMichael Lange   for (d = 0; d < dim; d++) depthShift[d] = depthRecv[dim];
78746f9b1c3SMichael Lange   for (d = 1; d < dim; d++) depthShift[d] += depthRecv[0];
78846f9b1c3SMichael Lange   for (d = dim - 2; d > 0; d--) depthShift[d] += depthRecv[d + 1];
78946f9b1c3SMichael Lange   for (d = 0; d < dim + 1; d++) {
7909566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
79146f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
79246f9b1c3SMichael Lange   }
79346f9b1c3SMichael Lange 
79446f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
7959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
79646f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
7979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(newLeaves, &ilocal));
7989566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(newLeaves, &iremote));
79946f9b1c3SMichael Lange   /* First map local points to themselves */
80046f9b1c3SMichael Lange   for (d = 0; d < dim + 1; d++) {
8019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
80246f9b1c3SMichael Lange     for (p = pStart; p < pEnd; p++) {
80346f9b1c3SMichael Lange       point                = p + depthShift[d];
80446f9b1c3SMichael Lange       ilocal[point]        = point;
80546f9b1c3SMichael Lange       iremote[point].index = p;
80646f9b1c3SMichael Lange       iremote[point].rank  = rank;
80746f9b1c3SMichael Lange       depthIdx[d]++;
80846f9b1c3SMichael Lange     }
80946f9b1c3SMichael Lange   }
81046f9b1c3SMichael Lange 
81146f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
8129566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &pointSF));
8139566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote));
81446f9b1c3SMichael Lange   for (d = 0; d < dim + 1; d++) {
8159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
81646f9b1c3SMichael Lange     for (p = 0; p < numSharedPoints; p++) {
81746f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
81846f9b1c3SMichael Lange         point                = sharedLocal[p] + depthShift[d];
81946f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
82046f9b1c3SMichael Lange         iremote[point].rank  = sharedRemote[p].rank;
82146f9b1c3SMichael Lange       }
82246f9b1c3SMichael Lange     }
82346f9b1c3SMichael Lange   }
82446f9b1c3SMichael Lange 
82546f9b1c3SMichael Lange   /* Now add the incoming overlap points */
82646f9b1c3SMichael Lange   for (p = 0; p < nleaves; p++) {
82746f9b1c3SMichael Lange     point                = depthIdx[remoteDepths[p]];
82846f9b1c3SMichael Lange     ilocal[point]        = point;
82946f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
83046f9b1c3SMichael Lange     iremote[point].rank  = overlapRemote[p].rank;
83146f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
83246f9b1c3SMichael Lange   }
8339566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pointDepths, remoteDepths));
83446f9b1c3SMichael Lange 
8359566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, migrationSF));
8369566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*migrationSF, "Overlap Migration SF"));
8379566063dSJacob Faibussowitsch   PetscCall(PetscSFSetFromOptions(*migrationSF));
8389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
8399566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraph(*migrationSF, pEnd - pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
84046f9b1c3SMichael Lange 
8419566063dSJacob Faibussowitsch   PetscCall(PetscFree3(depthRecv, depthShift, depthIdx));
84270034214SMatthew G. Knepley   PetscFunctionReturn(0);
84370034214SMatthew G. Knepley }
84470034214SMatthew G. Knepley 
845a9f1d5b2SMichael Lange /*@
846f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
847a9f1d5b2SMichael Lange 
848064ec1f3Sprj-   Input Parameters:
849a9f1d5b2SMichael Lange + dm          - The DM
850a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
851a9f1d5b2SMichael Lange 
852a9f1d5b2SMichael Lange   Output Parameter:
853a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
854a9f1d5b2SMichael Lange 
855412e9a14SMatthew G. Knepley   Note:
856412e9a14SMatthew G. Knepley   This lexicographically sorts by (depth, cellType)
857412e9a14SMatthew G. Knepley 
858a9f1d5b2SMichael Lange   Level: developer
859a9f1d5b2SMichael Lange 
860db781477SPatrick Sanan .seealso: `DMPlexPartitionLabelCreateSF()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()`
861a9f1d5b2SMichael Lange @*/
8629371c9d4SSatish Balay PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF) {
863a9f1d5b2SMichael Lange   MPI_Comm           comm;
8649852e123SBarry Smith   PetscMPIInt        rank, size;
865412e9a14SMatthew G. Knepley   PetscInt           d, ldepth, depth, dim, p, pStart, pEnd, nroots, nleaves;
866412e9a14SMatthew G. Knepley   PetscSFNode       *pointDepths, *remoteDepths;
867412e9a14SMatthew G. Knepley   PetscInt          *ilocal;
868a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
869412e9a14SMatthew G. Knepley   PetscInt          *ctRecv, *ctShift, *ctIdx;
870a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
871a9f1d5b2SMichael Lange 
872a9f1d5b2SMichael Lange   PetscFunctionBegin;
873a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8749566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
8759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
8769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
8779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &ldepth));
8789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8791c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm));
88063a3b9bcSJacob Faibussowitsch   PetscCheck(!(ldepth >= 0) || !(depth != ldepth), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, ldepth, depth);
8819566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_PartStratSF, dm, 0, 0, 0));
882a9f1d5b2SMichael Lange 
883a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
8849566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote));
8859566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths));
8867fab53ddSMatthew G. Knepley   for (d = 0; d < depth + 1; ++d) {
8879566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
888f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
889412e9a14SMatthew G. Knepley       DMPolytopeType ct;
890f0e73a3dSToby Isaac 
8919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
892412e9a14SMatthew G. Knepley       pointDepths[p].index = d;
893412e9a14SMatthew G. Knepley       pointDepths[p].rank  = ct;
894f0e73a3dSToby Isaac     }
895412e9a14SMatthew G. Knepley   }
8969371c9d4SSatish Balay   for (p = 0; p < nleaves; ++p) {
8979371c9d4SSatish Balay     remoteDepths[p].index = -1;
8989371c9d4SSatish Balay     remoteDepths[p].rank  = -1;
8999371c9d4SSatish Balay   }
9009566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(sf, MPIU_2INT, pointDepths, remoteDepths, MPI_REPLACE));
9019566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(sf, MPIU_2INT, pointDepths, remoteDepths, MPI_REPLACE));
902412e9a14SMatthew G. Knepley   /* Count received points in each stratum and compute the internal strata shift */
9039566063dSJacob Faibussowitsch   PetscCall(PetscCalloc6(depth + 1, &depthRecv, depth + 1, &depthShift, depth + 1, &depthIdx, DM_NUM_POLYTOPES, &ctRecv, DM_NUM_POLYTOPES, &ctShift, DM_NUM_POLYTOPES, &ctIdx));
904412e9a14SMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
905412e9a14SMatthew G. Knepley     if (remoteDepths[p].rank < 0) {
906412e9a14SMatthew G. Knepley       ++depthRecv[remoteDepths[p].index];
907412e9a14SMatthew G. Knepley     } else {
908412e9a14SMatthew G. Knepley       ++ctRecv[remoteDepths[p].rank];
909412e9a14SMatthew G. Knepley     }
910412e9a14SMatthew G. Knepley   }
911412e9a14SMatthew G. Knepley   {
912412e9a14SMatthew G. Knepley     PetscInt depths[4], dims[4], shift = 0, i, c;
913412e9a14SMatthew G. Knepley 
9148238f61eSMatthew G. Knepley     /* Cells (depth), Vertices (0), Faces (depth-1), Edges (1)
9158238f61eSMatthew G. Knepley          Consider DM_POLYTOPE_FV_GHOST and DM_POLYTOPE_INTERIOR_GHOST as cells
9168238f61eSMatthew G. Knepley      */
9179371c9d4SSatish Balay     depths[0] = depth;
9189371c9d4SSatish Balay     depths[1] = 0;
9199371c9d4SSatish Balay     depths[2] = depth - 1;
9209371c9d4SSatish Balay     depths[3] = 1;
9219371c9d4SSatish Balay     dims[0]   = dim;
9229371c9d4SSatish Balay     dims[1]   = 0;
9239371c9d4SSatish Balay     dims[2]   = dim - 1;
9249371c9d4SSatish Balay     dims[3]   = 1;
925412e9a14SMatthew G. Knepley     for (i = 0; i <= depth; ++i) {
926412e9a14SMatthew G. Knepley       const PetscInt dep = depths[i];
927412e9a14SMatthew G. Knepley       const PetscInt dim = dims[i];
928412e9a14SMatthew G. Knepley 
929412e9a14SMatthew G. Knepley       for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
9308238f61eSMatthew G. Knepley         if (DMPolytopeTypeGetDim((DMPolytopeType)c) != dim && !(i == 0 && (c == DM_POLYTOPE_FV_GHOST || c == DM_POLYTOPE_INTERIOR_GHOST))) continue;
931412e9a14SMatthew G. Knepley         ctShift[c] = shift;
932412e9a14SMatthew G. Knepley         shift += ctRecv[c];
933412e9a14SMatthew G. Knepley       }
934412e9a14SMatthew G. Knepley       depthShift[dep] = shift;
935412e9a14SMatthew G. Knepley       shift += depthRecv[dep];
936412e9a14SMatthew G. Knepley     }
937412e9a14SMatthew G. Knepley     for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
938412e9a14SMatthew G. Knepley       const PetscInt ctDim = DMPolytopeTypeGetDim((DMPolytopeType)c);
939412e9a14SMatthew G. Knepley 
9408238f61eSMatthew G. Knepley       if ((ctDim < 0 || ctDim > dim) && (c != DM_POLYTOPE_FV_GHOST && c != DM_POLYTOPE_INTERIOR_GHOST)) {
941412e9a14SMatthew G. Knepley         ctShift[c] = shift;
942412e9a14SMatthew G. Knepley         shift += ctRecv[c];
943412e9a14SMatthew G. Knepley       }
944412e9a14SMatthew G. Knepley     }
945412e9a14SMatthew G. Knepley   }
946a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
9479566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nleaves, &ilocal));
9487fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
949412e9a14SMatthew G. Knepley     const PetscInt       dep = remoteDepths[p].index;
950412e9a14SMatthew G. Knepley     const DMPolytopeType ct  = (DMPolytopeType)remoteDepths[p].rank;
9517fab53ddSMatthew G. Knepley 
952412e9a14SMatthew G. Knepley     if ((PetscInt)ct < 0) {
9537fab53ddSMatthew G. Knepley       ilocal[p] = depthShift[dep] + depthIdx[dep];
954412e9a14SMatthew G. Knepley       ++depthIdx[dep];
955412e9a14SMatthew G. Knepley     } else {
956412e9a14SMatthew G. Knepley       ilocal[p] = ctShift[ct] + ctIdx[ct];
957412e9a14SMatthew G. Knepley       ++ctIdx[ct];
958412e9a14SMatthew G. Knepley     }
959a9f1d5b2SMichael Lange   }
9609566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, migrationSF));
9619566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*migrationSF, "Migration SF"));
9629566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, (PetscSFNode *)iremote, PETSC_COPY_VALUES));
9639566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pointDepths, remoteDepths));
9649566063dSJacob Faibussowitsch   PetscCall(PetscFree6(depthRecv, depthShift, depthIdx, ctRecv, ctShift, ctIdx));
9659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_PartStratSF, dm, 0, 0, 0));
966a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
967a9f1d5b2SMichael Lange }
968a9f1d5b2SMichael Lange 
96970034214SMatthew G. Knepley /*@
97070034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
97170034214SMatthew G. Knepley 
972d083f849SBarry Smith   Collective on dm
97370034214SMatthew G. Knepley 
97470034214SMatthew G. Knepley   Input Parameters:
97570034214SMatthew G. Knepley + dm - The DMPlex object
97670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
97770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
978cb15cd0eSMatthew G. Knepley - originalVec - The existing data in a local vector
97970034214SMatthew G. Knepley 
98070034214SMatthew G. Knepley   Output Parameters:
98170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
982cb15cd0eSMatthew G. Knepley - newVec - The new data in a local vector
98370034214SMatthew G. Knepley 
98470034214SMatthew G. Knepley   Level: developer
98570034214SMatthew G. Knepley 
986db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeFieldIS()`, `DMPlexDistributeData()`
98770034214SMatthew G. Knepley @*/
9889371c9d4SSatish Balay PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec) {
98970034214SMatthew G. Knepley   PetscSF      fieldSF;
99070034214SMatthew G. Knepley   PetscInt    *remoteOffsets, fieldSize;
99170034214SMatthew G. Knepley   PetscScalar *originalValues, *newValues;
99270034214SMatthew G. Knepley 
99370034214SMatthew G. Knepley   PetscFunctionBegin;
9949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeField, dm, 0, 0, 0));
9959566063dSJacob Faibussowitsch   PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection));
99670034214SMatthew G. Knepley 
9979566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize));
9989566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newVec, fieldSize, PETSC_DETERMINE));
9999566063dSJacob Faibussowitsch   PetscCall(VecSetType(newVec, dm->vectype));
100070034214SMatthew G. Knepley 
10019566063dSJacob Faibussowitsch   PetscCall(VecGetArray(originalVec, &originalValues));
10029566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newVec, &newValues));
10039566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF));
10049566063dSJacob Faibussowitsch   PetscCall(PetscFree(remoteOffsets));
10059566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues, MPI_REPLACE));
10069566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues, MPI_REPLACE));
10079566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&fieldSF));
10089566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newVec, &newValues));
10099566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(originalVec, &originalValues));
10109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeField, dm, 0, 0, 0));
101170034214SMatthew G. Knepley   PetscFunctionReturn(0);
101270034214SMatthew G. Knepley }
101370034214SMatthew G. Knepley 
101470034214SMatthew G. Knepley /*@
101570034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
101670034214SMatthew G. Knepley 
1017d083f849SBarry Smith   Collective on dm
101870034214SMatthew G. Knepley 
101970034214SMatthew G. Knepley   Input Parameters:
102070034214SMatthew G. Knepley + dm - The DMPlex object
102170034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
102270034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
102370034214SMatthew G. Knepley - originalIS - The existing data
102470034214SMatthew G. Knepley 
102570034214SMatthew G. Knepley   Output Parameters:
102670034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
102770034214SMatthew G. Knepley - newIS - The new data
102870034214SMatthew G. Knepley 
102970034214SMatthew G. Knepley   Level: developer
103070034214SMatthew G. Knepley 
1031db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()`, `DMPlexDistributeData()`
103270034214SMatthew G. Knepley @*/
10339371c9d4SSatish Balay PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS) {
103470034214SMatthew G. Knepley   PetscSF         fieldSF;
103570034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
103670034214SMatthew G. Knepley   const PetscInt *originalValues;
103770034214SMatthew G. Knepley 
103870034214SMatthew G. Knepley   PetscFunctionBegin;
10399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeField, dm, 0, 0, 0));
10409566063dSJacob Faibussowitsch   PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection));
104170034214SMatthew G. Knepley 
10429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize));
10439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(fieldSize, &newValues));
104470034214SMatthew G. Knepley 
10459566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(originalIS, &originalValues));
10469566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF));
10479566063dSJacob Faibussowitsch   PetscCall(PetscFree(remoteOffsets));
10489566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *)originalValues, newValues, MPI_REPLACE));
10499566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *)originalValues, newValues, MPI_REPLACE));
10509566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&fieldSF));
10519566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(originalIS, &originalValues));
10529566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS));
10539566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeField, dm, 0, 0, 0));
105470034214SMatthew G. Knepley   PetscFunctionReturn(0);
105570034214SMatthew G. Knepley }
105670034214SMatthew G. Knepley 
105770034214SMatthew G. Knepley /*@
105870034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
105970034214SMatthew G. Knepley 
1060d083f849SBarry Smith   Collective on dm
106170034214SMatthew G. Knepley 
106270034214SMatthew G. Knepley   Input Parameters:
106370034214SMatthew G. Knepley + dm - The DMPlex object
106470034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
106570034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
106670034214SMatthew G. Knepley . datatype - The type of data
106770034214SMatthew G. Knepley - originalData - The existing data
106870034214SMatthew G. Knepley 
106970034214SMatthew G. Knepley   Output Parameters:
107070034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
107170034214SMatthew G. Knepley - newData - The new data
107270034214SMatthew G. Knepley 
107370034214SMatthew G. Knepley   Level: developer
107470034214SMatthew G. Knepley 
1075db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()`
107670034214SMatthew G. Knepley @*/
10779371c9d4SSatish Balay PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData) {
107870034214SMatthew G. Knepley   PetscSF     fieldSF;
107970034214SMatthew G. Knepley   PetscInt   *remoteOffsets, fieldSize;
108070034214SMatthew G. Knepley   PetscMPIInt dataSize;
108170034214SMatthew G. Knepley 
108270034214SMatthew G. Knepley   PetscFunctionBegin;
10839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeData, dm, 0, 0, 0));
10849566063dSJacob Faibussowitsch   PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection));
108570034214SMatthew G. Knepley 
10869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize));
10879566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(datatype, &dataSize));
10889566063dSJacob Faibussowitsch   PetscCall(PetscMalloc(fieldSize * dataSize, newData));
108970034214SMatthew G. Knepley 
10909566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF));
10919566063dSJacob Faibussowitsch   PetscCall(PetscFree(remoteOffsets));
10929566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(fieldSF, datatype, originalData, *newData, MPI_REPLACE));
10939566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(fieldSF, datatype, originalData, *newData, MPI_REPLACE));
10949566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&fieldSF));
10959566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeData, dm, 0, 0, 0));
109670034214SMatthew G. Knepley   PetscFunctionReturn(0);
109770034214SMatthew G. Knepley }
109870034214SMatthew G. Knepley 
10999371c9d4SSatish Balay static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) {
1100cc71bff1SMichael Lange   DM_Plex     *pmesh = (DM_Plex *)(dmParallel)->data;
1101cc71bff1SMichael Lange   MPI_Comm     comm;
1102cc71bff1SMichael Lange   PetscSF      coneSF;
1103cc71bff1SMichael Lange   PetscSection originalConeSection, newConeSection;
1104ac04eaf7SToby Isaac   PetscInt    *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
1105cc71bff1SMichael Lange   PetscBool    flg;
1106cc71bff1SMichael Lange 
1107cc71bff1SMichael Lange   PetscFunctionBegin;
1108cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11090c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
11109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeCones, dm, 0, 0, 0));
1111cc71bff1SMichael Lange   /* Distribute cone section */
11129566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
11139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSection(dm, &originalConeSection));
11149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSection(dmParallel, &newConeSection));
11159566063dSJacob Faibussowitsch   PetscCall(PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection));
11169566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dmParallel));
1117cc71bff1SMichael Lange   /* Communicate and renumber cones */
11189566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF));
11199566063dSJacob Faibussowitsch   PetscCall(PetscFree(remoteOffsets));
11209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
1121ac04eaf7SToby Isaac   if (original) {
1122ac04eaf7SToby Isaac     PetscInt numCones;
1123ac04eaf7SToby Isaac 
11249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(originalConeSection, &numCones));
11259566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numCones, &globCones));
11269566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones));
1127367003a6SStefano Zampini   } else {
1128ac04eaf7SToby Isaac     globCones = cones;
1129ac04eaf7SToby Isaac   }
11309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dmParallel, &newCones));
11319566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones, MPI_REPLACE));
11329566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones, MPI_REPLACE));
11331baa6e33SBarry Smith   if (original) PetscCall(PetscFree(globCones));
11349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newConeSection, &newConesSize));
11359566063dSJacob Faibussowitsch   PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones));
113676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
11373533c52bSMatthew G. Knepley     PetscInt  p;
11383533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
11393533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
11409371c9d4SSatish Balay       if (newCones[p] < 0) {
11419371c9d4SSatish Balay         valid = PETSC_FALSE;
11429371c9d4SSatish Balay         PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Point %" PetscInt_FMT " not in overlap SF\n", PetscGlobalRank, p));
11439371c9d4SSatish Balay       }
11443533c52bSMatthew G. Knepley     }
114528b400f6SJacob Faibussowitsch     PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
11463533c52bSMatthew G. Knepley   }
11479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-cones_view", &flg));
1148cc71bff1SMichael Lange   if (flg) {
11499566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Serial Cone Section:\n"));
11509566063dSJacob Faibussowitsch     PetscCall(PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_(comm)));
11519566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Parallel Cone Section:\n"));
11529566063dSJacob Faibussowitsch     PetscCall(PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_(comm)));
11539566063dSJacob Faibussowitsch     PetscCall(PetscSFView(coneSF, NULL));
1154cc71bff1SMichael Lange   }
11559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientations(dm, &cones));
11569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientations(dmParallel, &newCones));
11579566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones, MPI_REPLACE));
11589566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones, MPI_REPLACE));
11599566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&coneSF));
11609566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeCones, dm, 0, 0, 0));
1161eaf898f9SPatrick Sanan   /* Create supports and stratify DMPlex */
1162cc71bff1SMichael Lange   {
1163cc71bff1SMichael Lange     PetscInt pStart, pEnd;
1164cc71bff1SMichael Lange 
11659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd));
11669566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pmesh->supportSection, pStart, pEnd));
1167cc71bff1SMichael Lange   }
11689566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dmParallel));
11699566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dmParallel));
11701cf84007SMatthew G. Knepley   {
11711cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
11721cf84007SMatthew G. Knepley 
11739566063dSJacob Faibussowitsch     PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
11749566063dSJacob Faibussowitsch     PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure));
11759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors));
11769566063dSJacob Faibussowitsch     PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors));
11771cf84007SMatthew G. Knepley   }
1178cc71bff1SMichael Lange   PetscFunctionReturn(0);
1179cc71bff1SMichael Lange }
1180cc71bff1SMichael Lange 
11819371c9d4SSatish Balay static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel) {
11820df0e737SMichael Lange   MPI_Comm         comm;
11839318fe57SMatthew G. Knepley   DM               cdm, cdmParallel;
11840df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
11850df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
11860df0e737SMichael Lange   PetscInt         bs;
11870df0e737SMichael Lange   const char      *name;
11884fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
11890df0e737SMichael Lange 
11900df0e737SMichael Lange   PetscFunctionBegin;
11910df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11920c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
11930df0e737SMichael Lange 
11949566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
11956858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
11966858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dmParallel, &cdmParallel));
11976858538eSMatthew G. Knepley   PetscCall(DMCopyDisc(cdm, cdmParallel));
11989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &originalCoordSection));
11999566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dmParallel, &newCoordSection));
12009566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &originalCoordinates));
12010df0e737SMichael Lange   if (originalCoordinates) {
12029566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
12039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)originalCoordinates, &name));
12049566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)newCoordinates, name));
12050df0e737SMichael Lange 
12069566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates));
12079566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dmParallel, newCoordinates));
12089566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(originalCoordinates, &bs));
12099566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(newCoordinates, bs));
12109566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&newCoordinates));
12110df0e737SMichael Lange   }
12126858538eSMatthew G. Knepley 
12136858538eSMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12144fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
12154fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dmParallel, maxCell, Lstart, L));
12166858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12176858538eSMatthew G. Knepley   if (cdm) {
12186858538eSMatthew G. Knepley     PetscCall(DMClone(dmParallel, &cdmParallel));
12196858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinateDM(dmParallel, cdmParallel));
12209566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(cdm, cdmParallel));
12216858538eSMatthew G. Knepley     PetscCall(DMDestroy(&cdmParallel));
12226858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateSection(dm, &originalCoordSection));
12236858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinatesLocal(dm, &originalCoordinates));
12246858538eSMatthew G. Knepley     PetscCall(PetscSectionCreate(comm, &newCoordSection));
12256858538eSMatthew G. Knepley     if (originalCoordinates) {
12266858538eSMatthew G. Knepley       PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
12276858538eSMatthew G. Knepley       PetscCall(PetscObjectGetName((PetscObject)originalCoordinates, &name));
12286858538eSMatthew G. Knepley       PetscCall(PetscObjectSetName((PetscObject)newCoordinates, name));
12296858538eSMatthew G. Knepley 
12306858538eSMatthew G. Knepley       PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates));
12316858538eSMatthew G. Knepley       PetscCall(VecGetBlockSize(originalCoordinates, &bs));
12326858538eSMatthew G. Knepley       PetscCall(VecSetBlockSize(newCoordinates, bs));
12336858538eSMatthew G. Knepley       PetscCall(DMSetCellCoordinateSection(dmParallel, bs, newCoordSection));
12346858538eSMatthew G. Knepley       PetscCall(DMSetCellCoordinatesLocal(dmParallel, newCoordinates));
12356858538eSMatthew G. Knepley       PetscCall(VecDestroy(&newCoordinates));
12366858538eSMatthew G. Knepley     }
12376858538eSMatthew G. Knepley     PetscCall(PetscSectionDestroy(&newCoordSection));
12386858538eSMatthew G. Knepley   }
12390df0e737SMichael Lange   PetscFunctionReturn(0);
12400df0e737SMichael Lange }
12410df0e737SMichael Lange 
12429371c9d4SSatish Balay static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel) {
1243df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
12440df0e737SMichael Lange   MPI_Comm         comm;
12457980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
12460df0e737SMichael Lange   PetscMPIInt      rank;
12477980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1248df0420ecSMatthew G. Knepley   PetscBool        hasLabels  = PETSC_FALSE, lsendDepth, sendDepth;
1249df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
12500df0e737SMichael Lange 
12510df0e737SMichael Lange   PetscFunctionBegin;
12520df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12530c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
12540c86c063SLisandro Dalcin 
12559566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeLabels, dm, 0, 0, 0));
12569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
12580df0e737SMichael Lange 
1259df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
12609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
12629566063dSJacob Faibussowitsch   if (depthLabel) PetscCall(PetscObjectStateGet((PetscObject)depthLabel, &depthState));
1263df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
12641c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm));
1265df0420ecSMatthew G. Knepley   if (sendDepth) {
12669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dmParallel, &dmParallel->depthLabel));
12679566063dSJacob Faibussowitsch     PetscCall(DMRemoveLabelBySelf(dmParallel, &dmParallel->depthLabel, PETSC_FALSE));
1268df0420ecSMatthew G. Knepley   }
1269d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
12709566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLocalLabels));
1271d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
12729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm));
1273627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
12745d80c0bfSVaclav Hapla   for (l = 0; l < numLabels; ++l) {
1275bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
127683e10cb3SLisandro Dalcin     PetscBool   isDepth, lisOutput     = PETSC_TRUE, isOutput;
1277d67d17b1SMatthew G. Knepley     const char *name = NULL;
12780df0e737SMichael Lange 
1279d67d17b1SMatthew G. Knepley     if (hasLabels) {
12809566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
12810df0e737SMichael Lange       /* Skip "depth" because it is recreated */
12829566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)label, &name));
12839566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &isDepth));
1284d67d17b1SMatthew G. Knepley     }
12859566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm));
128683e10cb3SLisandro Dalcin     if (isDepth && !sendDepth) continue;
12879566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, migrationSF, &labelNew));
128883e10cb3SLisandro Dalcin     if (isDepth) {
12897980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
12907980c9d4SMatthew G. Knepley       PetscInt gdepth;
12917980c9d4SMatthew G. Knepley 
12921c2dc1cbSBarry Smith       PetscCall(MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm));
129363a3b9bcSJacob Faibussowitsch       PetscCheck(!(depth >= 0) || !(gdepth != depth), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, depth, gdepth);
12947980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
12957980c9d4SMatthew G. Knepley         PetscBool has;
12967980c9d4SMatthew G. Knepley 
12979566063dSJacob Faibussowitsch         PetscCall(DMLabelHasStratum(labelNew, d, &has));
12989566063dSJacob Faibussowitsch         if (!has) PetscCall(DMLabelAddStratum(labelNew, d));
12997980c9d4SMatthew G. Knepley       }
13007980c9d4SMatthew G. Knepley     }
13019566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmParallel, labelNew));
130283e10cb3SLisandro Dalcin     /* Put the output flag in the new label */
13039566063dSJacob Faibussowitsch     if (hasLabels) PetscCall(DMGetLabelOutput(dm, name, &lisOutput));
13041c2dc1cbSBarry Smith     PetscCall(MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm));
13059566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)labelNew, &name));
13069566063dSJacob Faibussowitsch     PetscCall(DMSetLabelOutput(dmParallel, name, isOutput));
13079566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&labelNew));
13080df0e737SMichael Lange   }
1309695799ffSMatthew G. Knepley   {
1310695799ffSMatthew G. Knepley     DMLabel ctLabel;
1311695799ffSMatthew G. Knepley 
1312695799ffSMatthew G. Knepley     // Reset label for fast lookup
1313695799ffSMatthew G. Knepley     PetscCall(DMPlexGetCellTypeLabel(dmParallel, &ctLabel));
1314695799ffSMatthew G. Knepley     PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel));
1315695799ffSMatthew G. Knepley   }
13169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeLabels, dm, 0, 0, 0));
13170df0e737SMichael Lange   PetscFunctionReturn(0);
13180df0e737SMichael Lange }
13190df0e737SMichael Lange 
13209371c9d4SSatish Balay static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) {
132115078cd4SMichael Lange   DM_Plex     *mesh  = (DM_Plex *)dm->data;
132215078cd4SMichael Lange   DM_Plex     *pmesh = (DM_Plex *)(dmParallel)->data;
1323a6f36705SMichael Lange   MPI_Comm     comm;
1324a6f36705SMichael Lange   DM           refTree;
1325a6f36705SMichael Lange   PetscSection origParentSection, newParentSection;
1326a6f36705SMichael Lange   PetscInt    *origParents, *origChildIDs;
1327a6f36705SMichael Lange   PetscBool    flg;
1328a6f36705SMichael Lange 
1329a6f36705SMichael Lange   PetscFunctionBegin;
1330a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13310c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
13329566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1333a6f36705SMichael Lange 
1334a6f36705SMichael Lange   /* Set up tree */
13359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
13369566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmParallel, refTree));
13379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &origParentSection, &origParents, &origChildIDs, NULL, NULL));
1338a6f36705SMichael Lange   if (origParentSection) {
1339a6f36705SMichael Lange     PetscInt  pStart, pEnd;
134008633170SToby Isaac     PetscInt *newParents, *newChildIDs, *globParents;
1341a6f36705SMichael Lange     PetscInt *remoteOffsetsParents, newParentSize;
1342a6f36705SMichael Lange     PetscSF   parentSF;
1343a6f36705SMichael Lange 
13449566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd));
13459566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel), &newParentSection));
13469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(newParentSection, pStart, pEnd));
13479566063dSJacob Faibussowitsch     PetscCall(PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection));
13489566063dSJacob Faibussowitsch     PetscCall(PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF));
13499566063dSJacob Faibussowitsch     PetscCall(PetscFree(remoteOffsetsParents));
13509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(newParentSection, &newParentSize));
13519566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(newParentSize, &newParents, newParentSize, &newChildIDs));
135208633170SToby Isaac     if (original) {
135308633170SToby Isaac       PetscInt numParents;
135408633170SToby Isaac 
13559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(origParentSection, &numParents));
13569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numParents, &globParents));
13579566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents));
13589371c9d4SSatish Balay     } else {
135908633170SToby Isaac       globParents = origParents;
136008633170SToby Isaac     }
13619566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents, MPI_REPLACE));
13629566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents, MPI_REPLACE));
13631baa6e33SBarry Smith     if (original) PetscCall(PetscFree(globParents));
13649566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs, MPI_REPLACE));
13659566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs, MPI_REPLACE));
13669566063dSJacob Faibussowitsch     PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents));
136776bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
13684a54e071SToby Isaac       PetscInt  p;
13694a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
13704a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
13719371c9d4SSatish Balay         if (newParents[p] < 0) {
13729371c9d4SSatish Balay           valid = PETSC_FALSE;
13739371c9d4SSatish Balay           PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " not in overlap SF\n", p));
13749371c9d4SSatish Balay         }
13754a54e071SToby Isaac       }
137628b400f6SJacob Faibussowitsch       PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
13774a54e071SToby Isaac     }
13789566063dSJacob Faibussowitsch     PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-parents_view", &flg));
1379a6f36705SMichael Lange     if (flg) {
13809566063dSJacob Faibussowitsch       PetscCall(PetscPrintf(comm, "Serial Parent Section: \n"));
13819566063dSJacob Faibussowitsch       PetscCall(PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_(comm)));
13829566063dSJacob Faibussowitsch       PetscCall(PetscPrintf(comm, "Parallel Parent Section: \n"));
13839566063dSJacob Faibussowitsch       PetscCall(PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_(comm)));
13849566063dSJacob Faibussowitsch       PetscCall(PetscSFView(parentSF, NULL));
1385a6f36705SMichael Lange     }
13869566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmParallel, newParentSection, newParents, newChildIDs));
13879566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newParentSection));
13889566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newParents, newChildIDs));
13899566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&parentSF));
1390a6f36705SMichael Lange   }
139115078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1392a6f36705SMichael Lange   PetscFunctionReturn(0);
1393a6f36705SMichael Lange }
13940df0e737SMichael Lange 
13959371c9d4SSatish Balay PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel) {
13969852e123SBarry Smith   PetscMPIInt rank, size;
13974eca1733SMichael Lange   MPI_Comm    comm;
13984eca1733SMichael Lange 
13994eca1733SMichael Lange   PetscFunctionBegin;
14004eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14010c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
14024eca1733SMichael Lange 
14034eca1733SMichael Lange   /* Create point SF for parallel mesh */
14049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeSF, dm, 0, 0, 0));
14059566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
14069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
14079566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
14084eca1733SMichael Lange   {
14094eca1733SMichael Lange     const PetscInt *leaves;
14104eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
14114eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
14124eca1733SMichael Lange     PetscInt        pStart, pEnd;
14134eca1733SMichael Lange 
14149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd));
14159566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL));
14169566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &rowners, numLeaves, &lowners));
14174eca1733SMichael Lange     for (p = 0; p < numRoots; p++) {
14184eca1733SMichael Lange       rowners[p].rank  = -1;
14194eca1733SMichael Lange       rowners[p].index = -1;
14204eca1733SMichael Lange     }
14219566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE));
14229566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE));
14234eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14244eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
14254eca1733SMichael Lange         lowners[p].rank  = rank;
14264eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
14274eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
14284eca1733SMichael Lange         lowners[p].rank  = -2;
14294eca1733SMichael Lange         lowners[p].index = -2;
14304eca1733SMichael Lange       }
14314eca1733SMichael Lange     }
14324eca1733SMichael Lange     for (p = 0; p < numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
14334eca1733SMichael Lange       rowners[p].rank  = -3;
14344eca1733SMichael Lange       rowners[p].index = -3;
14354eca1733SMichael Lange     }
14369566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC));
14379566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC));
14389566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE));
14399566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE));
14404eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14411dca8a05SBarry Smith       PetscCheck(lowners[p].rank >= 0 && lowners[p].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cell partition corrupt: point not claimed");
14424eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
14434eca1733SMichael Lange     }
14449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGhostPoints, &ghostPoints));
14459566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGhostPoints, &remotePoints));
14464eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
14474eca1733SMichael Lange       if (lowners[p].rank != rank) {
14484eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
14494eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
14504eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
14514eca1733SMichael Lange         ++gp;
14524eca1733SMichael Lange       }
14534eca1733SMichael Lange     }
14549566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rowners, lowners));
14559566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER));
14569566063dSJacob Faibussowitsch     PetscCall(PetscSFSetFromOptions((dmParallel)->sf));
14574eca1733SMichael Lange   }
14581cf84007SMatthew G. Knepley   {
14591cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
14601cf84007SMatthew G. Knepley 
14619566063dSJacob Faibussowitsch     PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
14629566063dSJacob Faibussowitsch     PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure));
14639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors));
14649566063dSJacob Faibussowitsch     PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors));
14651cf84007SMatthew G. Knepley   }
14669566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeSF, dm, 0, 0, 0));
14674eca1733SMichael Lange   PetscFunctionReturn(0);
14684eca1733SMichael Lange }
14694eca1733SMichael Lange 
147098ba2d7fSLawrence Mitchell /*@
147198ba2d7fSLawrence Mitchell   DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition?
147298ba2d7fSLawrence Mitchell 
147398ba2d7fSLawrence Mitchell   Input Parameters:
147498ba2d7fSLawrence Mitchell + dm - The DMPlex object
147598ba2d7fSLawrence Mitchell - flg - Balance the partition?
147698ba2d7fSLawrence Mitchell 
147798ba2d7fSLawrence Mitchell   Level: intermediate
147898ba2d7fSLawrence Mitchell 
1479db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetPartitionBalance()`
148098ba2d7fSLawrence Mitchell @*/
14819371c9d4SSatish Balay PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg) {
148298ba2d7fSLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
148398ba2d7fSLawrence Mitchell 
148498ba2d7fSLawrence Mitchell   PetscFunctionBegin;
148598ba2d7fSLawrence Mitchell   mesh->partitionBalance = flg;
148698ba2d7fSLawrence Mitchell   PetscFunctionReturn(0);
148798ba2d7fSLawrence Mitchell }
148898ba2d7fSLawrence Mitchell 
148998ba2d7fSLawrence Mitchell /*@
149098ba2d7fSLawrence Mitchell   DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition?
149198ba2d7fSLawrence Mitchell 
149298ba2d7fSLawrence Mitchell   Input Parameter:
1493a2b725a8SWilliam Gropp . dm - The DMPlex object
149498ba2d7fSLawrence Mitchell 
149598ba2d7fSLawrence Mitchell   Output Parameter:
1496a2b725a8SWilliam Gropp . flg - Balance the partition?
149798ba2d7fSLawrence Mitchell 
149898ba2d7fSLawrence Mitchell   Level: intermediate
149998ba2d7fSLawrence Mitchell 
1500db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexSetPartitionBalance()`
150198ba2d7fSLawrence Mitchell @*/
15029371c9d4SSatish Balay PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg) {
150398ba2d7fSLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
150498ba2d7fSLawrence Mitchell 
150598ba2d7fSLawrence Mitchell   PetscFunctionBegin;
150698ba2d7fSLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1507534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
150898ba2d7fSLawrence Mitchell   *flg = mesh->partitionBalance;
150998ba2d7fSLawrence Mitchell   PetscFunctionReturn(0);
151098ba2d7fSLawrence Mitchell }
151198ba2d7fSLawrence Mitchell 
1512fc02256fSLawrence Mitchell typedef struct {
1513fc02256fSLawrence Mitchell   PetscInt vote, rank, index;
1514fc02256fSLawrence Mitchell } Petsc3Int;
1515fc02256fSLawrence Mitchell 
1516fc02256fSLawrence Mitchell /* MaxLoc, but carry a third piece of information around */
15179371c9d4SSatish Balay static void MPIAPI MaxLocCarry(void *in_, void *inout_, PetscMPIInt *len_, MPI_Datatype *dtype) {
1518fc02256fSLawrence Mitchell   Petsc3Int *a = (Petsc3Int *)inout_;
1519fc02256fSLawrence Mitchell   Petsc3Int *b = (Petsc3Int *)in_;
1520fc02256fSLawrence Mitchell   PetscInt   i, len = *len_;
1521fc02256fSLawrence Mitchell   for (i = 0; i < len; i++) {
1522fc02256fSLawrence Mitchell     if (a[i].vote < b[i].vote) {
1523fc02256fSLawrence Mitchell       a[i].vote  = b[i].vote;
1524fc02256fSLawrence Mitchell       a[i].rank  = b[i].rank;
1525fc02256fSLawrence Mitchell       a[i].index = b[i].index;
1526fc02256fSLawrence Mitchell     } else if (a[i].vote <= b[i].vote) {
1527fc02256fSLawrence Mitchell       if (a[i].rank >= b[i].rank) {
1528fc02256fSLawrence Mitchell         a[i].rank  = b[i].rank;
1529fc02256fSLawrence Mitchell         a[i].index = b[i].index;
1530fc02256fSLawrence Mitchell       }
1531fc02256fSLawrence Mitchell     }
1532fc02256fSLawrence Mitchell   }
1533fc02256fSLawrence Mitchell }
1534fc02256fSLawrence Mitchell 
1535f5bf2dbfSMichael Lange /*@C
1536a8c5bd36SStefano Zampini   DMPlexCreatePointSF - Build a point SF from an SF describing a point migration
1537f5bf2dbfSMichael Lange 
1538064ec1f3Sprj-   Input Parameters:
1539f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1540f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
1541d8d19677SJose E. Roman - ownership   - Flag causing a vote to determine point ownership
1542f5bf2dbfSMichael Lange 
1543f5bf2dbfSMichael Lange   Output Parameter:
1544d8d19677SJose E. Roman . pointSF     - The star forest describing the point overlap in the remapped DM
1545f5bf2dbfSMichael Lange 
15463618482eSVaclav Hapla   Notes:
15473618482eSVaclav Hapla   Output pointSF is guaranteed to have the array of local indices (leaves) sorted.
15483618482eSVaclav Hapla 
1549f5bf2dbfSMichael Lange   Level: developer
1550f5bf2dbfSMichael Lange 
1551db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()`
1552f5bf2dbfSMichael Lange @*/
15539371c9d4SSatish Balay PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF) {
155423193802SMatthew G. Knepley   PetscMPIInt        rank, size;
15551627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1556f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1557f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1558f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1559f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
156023193802SMatthew G. Knepley   Vec                shifts;
1561cae3e4f3SLawrence Mitchell   const PetscInt     numShifts  = 13759;
156223193802SMatthew G. Knepley   const PetscScalar *shift      = NULL;
156323193802SMatthew G. Knepley   const PetscBool    shiftDebug = PETSC_FALSE;
156498ba2d7fSLawrence Mitchell   PetscBool          balance;
1565f5bf2dbfSMichael Lange 
1566f5bf2dbfSMichael Lange   PetscFunctionBegin;
1567f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
15699566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
15709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreatePointSF, dm, 0, 0, 0));
1571f5bf2dbfSMichael Lange 
15729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPartitionBalance(dm, &balance));
15739566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots));
15749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes));
1575f5bf2dbfSMichael Lange   if (ownership) {
1576fc02256fSLawrence Mitchell     MPI_Op       op;
1577fc02256fSLawrence Mitchell     MPI_Datatype datatype;
1578fc02256fSLawrence Mitchell     Petsc3Int   *rootVote = NULL, *leafVote = NULL;
157923193802SMatthew G. Knepley     /* If balancing, we compute a random cyclic shift of the rank for each remote point. That way, the max will evenly distribute among ranks. */
158098ba2d7fSLawrence Mitchell     if (balance) {
158123193802SMatthew G. Knepley       PetscRandom r;
158223193802SMatthew G. Knepley 
15839566063dSJacob Faibussowitsch       PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r));
15849566063dSJacob Faibussowitsch       PetscCall(PetscRandomSetInterval(r, 0, 2467 * size));
15859566063dSJacob Faibussowitsch       PetscCall(VecCreate(PETSC_COMM_SELF, &shifts));
15869566063dSJacob Faibussowitsch       PetscCall(VecSetSizes(shifts, numShifts, numShifts));
15879566063dSJacob Faibussowitsch       PetscCall(VecSetType(shifts, VECSTANDARD));
15889566063dSJacob Faibussowitsch       PetscCall(VecSetRandom(shifts, r));
15899566063dSJacob Faibussowitsch       PetscCall(PetscRandomDestroy(&r));
15909566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(shifts, &shift));
159123193802SMatthew G. Knepley     }
159223193802SMatthew G. Knepley 
15939566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nroots, &rootVote));
15949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nleaves, &leafVote));
159523193802SMatthew G. Knepley     /* Point ownership vote: Process with highest rank owns shared points */
1596f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
159723193802SMatthew G. Knepley       if (shiftDebug) {
15989371c9d4SSatish Balay         PetscCall(PetscSynchronizedPrintf(PetscObjectComm((PetscObject)dm), "[%d] Point %" PetscInt_FMT " RemotePoint %" PetscInt_FMT " Shift %" PetscInt_FMT " MyRank %" PetscInt_FMT "\n", rank, leaves ? leaves[p] : p, roots[p].index,
15999371c9d4SSatish Balay                                           (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]), (rank + (shift ? (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]) : 0)) % size));
160023193802SMatthew G. Knepley       }
1601f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1602fc02256fSLawrence Mitchell       leafVote[p].vote  = (rank + (shift ? (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]) : 0)) % size;
1603fc02256fSLawrence Mitchell       leafVote[p].rank  = rank;
1604fc02256fSLawrence Mitchell       leafVote[p].index = p;
1605f5bf2dbfSMichael Lange     }
1606f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
16071627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1608fc02256fSLawrence Mitchell       rootVote[p].vote  = -3;
1609fc02256fSLawrence Mitchell       rootVote[p].rank  = -3;
1610fc02256fSLawrence Mitchell       rootVote[p].index = -3;
1611f5bf2dbfSMichael Lange     }
16129566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(3, MPIU_INT, &datatype));
16139566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&datatype));
16149566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Op_create(&MaxLocCarry, 1, &op));
16159566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceBegin(migrationSF, datatype, leafVote, rootVote, op));
16169566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(migrationSF, datatype, leafVote, rootVote, op));
16179566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Op_free(&op));
16189566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&datatype));
1619c091126eSLawrence Mitchell     for (p = 0; p < nroots; p++) {
1620fc02256fSLawrence Mitchell       rootNodes[p].rank  = rootVote[p].rank;
1621fc02256fSLawrence Mitchell       rootNodes[p].index = rootVote[p].index;
1622c091126eSLawrence Mitchell     }
16239566063dSJacob Faibussowitsch     PetscCall(PetscFree(leafVote));
16249566063dSJacob Faibussowitsch     PetscCall(PetscFree(rootVote));
1625f5bf2dbfSMichael Lange   } else {
1626f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1627f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1628f5bf2dbfSMichael Lange       rootNodes[p].rank  = rank;
1629fc02256fSLawrence Mitchell     }
1630f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1631f5bf2dbfSMichael Lange       /* Write new local id into old location */
1632ad540459SPierre Jolivet       if (roots[p].rank == rank) rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1633f5bf2dbfSMichael Lange     }
1634f5bf2dbfSMichael Lange   }
16359566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes, MPI_REPLACE));
16369566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes, MPI_REPLACE));
1637f5bf2dbfSMichael Lange 
163823193802SMatthew G. Knepley   for (npointLeaves = 0, p = 0; p < nleaves; p++) {
1639b0e1264bSMatthew G. Knepley     if (leafNodes[p].rank != rank) npointLeaves++;
164023193802SMatthew G. Knepley   }
16419566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npointLeaves, &pointLocal));
16429566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npointLeaves, &pointRemote));
1643f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1644b0e1264bSMatthew G. Knepley     if (leafNodes[p].rank != rank) {
16453618482eSVaclav Hapla       /* Note that pointLocal is automatically sorted as it is sublist of 0, ..., nleaves-1 */
1646f5bf2dbfSMichael Lange       pointLocal[idx]  = p;
1647f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1648f5bf2dbfSMichael Lange       idx++;
1649f5bf2dbfSMichael Lange     }
1650f5bf2dbfSMichael Lange   }
165123193802SMatthew G. Knepley   if (shift) {
16529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(shifts, &shift));
16539566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&shifts));
165423193802SMatthew G. Knepley   }
16559566063dSJacob Faibussowitsch   if (shiftDebug) PetscCall(PetscSynchronizedFlush(PetscObjectComm((PetscObject)dm), PETSC_STDOUT));
16569566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), pointSF));
16579566063dSJacob Faibussowitsch   PetscCall(PetscSFSetFromOptions(*pointSF));
16589566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER));
16599566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rootNodes, leafNodes));
16609566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreatePointSF, dm, 0, 0, 0));
16616c1ef331SVaclav Hapla   if (PetscDefined(USE_DEBUG)) PetscCall(DMPlexCheckPointSF(dm, *pointSF));
1662f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1663f5bf2dbfSMichael Lange }
1664f5bf2dbfSMichael Lange 
166515078cd4SMichael Lange /*@C
166615078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
166715078cd4SMichael Lange 
1668d083f849SBarry Smith   Collective on dm
166983655b49SVáclav Hapla 
1670064ec1f3Sprj-   Input Parameters:
167115078cd4SMichael Lange + dm       - The source DMPlex object
1672d8d19677SJose E. Roman - sf       - The star forest communication context describing the migration pattern
167315078cd4SMichael Lange 
167415078cd4SMichael Lange   Output Parameter:
1675d8d19677SJose E. Roman . targetDM - The target DMPlex object
167615078cd4SMichael Lange 
1677b9f40539SMichael Lange   Level: intermediate
167815078cd4SMichael Lange 
1679db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()`
168015078cd4SMichael Lange @*/
16819371c9d4SSatish Balay PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM) {
1682b9f40539SMichael Lange   MPI_Comm               comm;
1683cc1750acSStefano Zampini   PetscInt               dim, cdim, nroots;
1684b9f40539SMichael Lange   PetscSF                sfPoint;
168515078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1686ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
168715078cd4SMichael Lange 
168815078cd4SMichael Lange   PetscFunctionBegin;
168915078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16909566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0));
16919566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
16929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
16939566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(targetDM, dim));
16949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
16959566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(targetDM, cdim));
169615078cd4SMichael Lange 
1697bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
16989566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
16999566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
1700bfb0467fSMichael Lange   if (nroots >= 0) {
1701b9f40539SMichael Lange     IS        isOriginal;
1702ac04eaf7SToby Isaac     PetscInt  n, size, nleaves;
1703ac04eaf7SToby Isaac     PetscInt *numbering_orig, *numbering_new;
1704367003a6SStefano Zampini 
1705b9f40539SMichael Lange     /* Get the original point numbering */
17069566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePointNumbering(dm, &isOriginal));
17079566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal));
17089566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingGetSize(ltogOriginal, &size));
17099566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt **)&numbering_orig));
1710b9f40539SMichael Lange     /* Convert to positive global numbers */
17119371c9d4SSatish Balay     for (n = 0; n < size; n++) {
17129371c9d4SSatish Balay       if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n] + 1);
17139371c9d4SSatish Balay     }
1714b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
17159566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL));
17169566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nleaves, &numbering_new));
17179566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, numbering_orig, numbering_new, MPI_REPLACE));
17189566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, numbering_orig, numbering_new, MPI_REPLACE));
17199566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingCreate(comm, 1, nleaves, numbering_new, PETSC_OWN_POINTER, &ltogMigration));
17209566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt **)&numbering_orig));
17219566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&isOriginal));
172215078cd4SMichael Lange   } else {
1723bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
17249566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration));
172515078cd4SMichael Lange   }
1726a5a902f7SVaclav Hapla   PetscCall(PetscObjectSetName((PetscObject)ltogMigration, "Point renumbering for DM migration"));
1727a5a902f7SVaclav Hapla   PetscCall(ISLocalToGlobalMappingViewFromOptions(ltogMigration, (PetscObject)dm, "-partition_view"));
172815078cd4SMichael Lange   /* Migrate DM data to target DM */
17299566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM));
17309566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeLabels(dm, sf, targetDM));
17319566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeCoordinates(dm, sf, targetDM));
17329566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM));
17339566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&ltogOriginal));
17349566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&ltogMigration));
17359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0));
173615078cd4SMichael Lange   PetscFunctionReturn(0);
173715078cd4SMichael Lange }
173815078cd4SMichael Lange 
17393b8f15a2SMatthew G. Knepley /*@C
174070034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
174170034214SMatthew G. Knepley 
1742d083f849SBarry Smith   Collective on dm
174370034214SMatthew G. Knepley 
1744064ec1f3Sprj-   Input Parameters:
174570034214SMatthew G. Knepley + dm  - The original DMPlex object
174670034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
174770034214SMatthew G. Knepley 
1748064ec1f3Sprj-   Output Parameters:
1749f4ae5380SJed Brown + sf - The PetscSF used for point distribution, or NULL if not needed
1750f4ae5380SJed Brown - dmParallel - The distributed DMPlex object
175170034214SMatthew G. Knepley 
1752f4ae5380SJed Brown   Note: If the mesh was not distributed, the output dmParallel will be NULL.
175370034214SMatthew G. Knepley 
1754b0441da4SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
175570034214SMatthew G. Knepley   representation on the mesh.
175670034214SMatthew G. Knepley 
175770034214SMatthew G. Knepley   Level: intermediate
175870034214SMatthew G. Knepley 
1759db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexGetOverlap()`
176070034214SMatthew G. Knepley @*/
17619371c9d4SSatish Balay PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel) {
176270034214SMatthew G. Knepley   MPI_Comm         comm;
176315078cd4SMichael Lange   PetscPartitioner partitioner;
1764f8987ae8SMichael Lange   IS               cellPart;
1765f8987ae8SMichael Lange   PetscSection     cellPartSection;
1766cf86098cSMatthew G. Knepley   DM               dmCoord;
1767f8987ae8SMichael Lange   DMLabel          lblPartition, lblMigration;
1768874ddda9SLisandro Dalcin   PetscSF          sfMigration, sfStratified, sfPoint;
176998ba2d7fSLawrence Mitchell   PetscBool        flg, balance;
1770874ddda9SLisandro Dalcin   PetscMPIInt      rank, size;
177170034214SMatthew G. Knepley 
177270034214SMatthew G. Knepley   PetscFunctionBegin;
177370034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1774d5c515a1SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, overlap, 2);
1775d5c515a1SMatthew G. Knepley   if (sf) PetscValidPointer(sf, 3);
1776d5c515a1SMatthew G. Knepley   PetscValidPointer(dmParallel, 4);
177770034214SMatthew G. Knepley 
17780c86c063SLisandro Dalcin   if (sf) *sf = NULL;
17790c86c063SLisandro Dalcin   *dmParallel = NULL;
17809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
17819566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
17829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
17830c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
178470034214SMatthew G. Knepley 
17859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_Distribute, dm, 0, 0, 0));
178615078cd4SMichael Lange   /* Create cell partition */
17879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_Partition, dm, 0, 0, 0));
17889566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(comm, &cellPartSection));
17899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPartitioner(dm, &partitioner));
17909566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerDMPlexPartition(partitioner, dm, NULL, cellPartSection, &cellPart));
17919566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_PartSelf, dm, 0, 0, 0));
1792f8987ae8SMichael Lange   {
1793f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1794825f8a23SLisandro Dalcin     IS              is;
1795825f8a23SLisandro Dalcin     PetscHSetI      ht;
1796f8987ae8SMichael Lange     const PetscInt *points;
17978e330a33SStefano Zampini     PetscInt       *iranks;
17988e330a33SStefano Zampini     PetscInt        pStart, pEnd, proc, npoints, poff = 0, nranks;
1799825f8a23SLisandro Dalcin 
18009566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition));
1801825f8a23SLisandro Dalcin     /* Preallocate strata */
18029566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&ht));
18039566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd));
1804825f8a23SLisandro Dalcin     for (proc = pStart; proc < pEnd; proc++) {
18059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints));
18069566063dSJacob Faibussowitsch       if (npoints) PetscCall(PetscHSetIAdd(ht, proc));
1807825f8a23SLisandro Dalcin     }
18089566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(ht, &nranks));
18099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nranks, &iranks));
18109566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(ht, &poff, iranks));
18119566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&ht));
18129566063dSJacob Faibussowitsch     PetscCall(DMLabelAddStrata(lblPartition, nranks, iranks));
18139566063dSJacob Faibussowitsch     PetscCall(PetscFree(iranks));
1814825f8a23SLisandro Dalcin     /* Inline DMPlexPartitionLabelClosure() */
18159566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(cellPart, &points));
18169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd));
1817f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
18189566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints));
1819825f8a23SLisandro Dalcin       if (!npoints) continue;
18209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cellPartSection, proc, &poff));
18219566063dSJacob Faibussowitsch       PetscCall(DMPlexClosurePoints_Private(dm, npoints, points + poff, &is));
18229566063dSJacob Faibussowitsch       PetscCall(DMLabelSetStratumIS(lblPartition, proc, is));
18239566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&is));
1824f8987ae8SMichael Lange     }
18259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(cellPart, &points));
1826f8987ae8SMichael Lange   }
18279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_PartSelf, dm, 0, 0, 0));
18286e203dd9SStefano Zampini 
18299566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration));
18309566063dSJacob Faibussowitsch   PetscCall(DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration));
18319566063dSJacob Faibussowitsch   PetscCall(DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration));
18329566063dSJacob Faibussowitsch   PetscCall(DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified));
18339566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfMigration));
183443f7d02bSMichael Lange   sfMigration = sfStratified;
18359566063dSJacob Faibussowitsch   PetscCall(PetscSFSetUp(sfMigration));
18369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_Partition, dm, 0, 0, 0));
18379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-partition_view", &flg));
183870034214SMatthew G. Knepley   if (flg) {
18399566063dSJacob Faibussowitsch     PetscCall(DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_(comm)));
18409566063dSJacob Faibussowitsch     PetscCall(PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_(comm)));
184170034214SMatthew G. Knepley   }
1842f8987ae8SMichael Lange 
184315078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
18449566063dSJacob Faibussowitsch   PetscCall(DMPlexCreate(comm, dmParallel));
18459566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*dmParallel, "Parallel Mesh"));
18469566063dSJacob Faibussowitsch   PetscCall(DMPlexMigrate(dm, sfMigration, *dmParallel));
184770034214SMatthew G. Knepley 
1848a157612eSMichael Lange   /* Build the point SF without overlap */
18499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPartitionBalance(dm, &balance));
18509566063dSJacob Faibussowitsch   PetscCall(DMPlexSetPartitionBalance(*dmParallel, balance));
18519566063dSJacob Faibussowitsch   PetscCall(DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint));
18529566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*dmParallel, sfPoint));
18539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(*dmParallel, &dmCoord));
18549566063dSJacob Faibussowitsch   if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint));
18559566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscSFView(sfPoint, NULL));
185670034214SMatthew G. Knepley 
1857a157612eSMichael Lange   if (overlap > 0) {
185815078cd4SMichael Lange     DM                 dmOverlap;
185957f290daSMatthew G. Knepley     PetscInt           nroots, nleaves, noldleaves, l;
186057f290daSMatthew G. Knepley     const PetscInt    *oldLeaves;
186157f290daSMatthew G. Knepley     PetscSFNode       *newRemote, *permRemote;
186215078cd4SMichael Lange     const PetscSFNode *oldRemote;
186315078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1864524e35f8SStefano Zampini 
1865a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
18669566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap));
18679566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dmParallel));
1868a157612eSMichael Lange     *dmParallel = dmOverlap;
1869c389ea9fSToby Isaac     if (flg) {
18709566063dSJacob Faibussowitsch       PetscCall(PetscPrintf(comm, "Overlap Migration SF:\n"));
18719566063dSJacob Faibussowitsch       PetscCall(PetscSFView(sfOverlap, NULL));
1872c389ea9fSToby Isaac     }
187343331d4aSMichael Lange 
1874f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
18759566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfMigration, &nroots, &noldleaves, &oldLeaves, &oldRemote));
18769566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL));
18779566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nleaves, &newRemote));
187857f290daSMatthew G. Knepley     /* oldRemote: original root point mapping to original leaf point
187957f290daSMatthew G. Knepley        newRemote: original leaf point mapping to overlapped leaf point */
188057f290daSMatthew G. Knepley     if (oldLeaves) {
188173e69a6aSMatthew G. Knepley       /* After stratification, the migration remotes may not be in root (canonical) order, so we reorder using the leaf numbering */
18829566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(noldleaves, &permRemote));
188357f290daSMatthew G. Knepley       for (l = 0; l < noldleaves; ++l) permRemote[oldLeaves[l]] = oldRemote[l];
188457f290daSMatthew G. Knepley       oldRemote = permRemote;
188557f290daSMatthew G. Knepley     }
18869566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote, MPI_REPLACE));
18879566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote, MPI_REPLACE));
18889566063dSJacob Faibussowitsch     if (oldLeaves) PetscCall(PetscFree(oldRemote));
18899566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(comm, &sfOverlapPoint));
18909566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER));
18919566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&sfOverlap));
18929566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&sfMigration));
189315078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1894c389ea9fSToby Isaac   }
1895bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
18969566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lblPartition));
18979566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lblMigration));
18989566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&cellPartSection));
18999566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&cellPart));
190045480ffeSMatthew G. Knepley   /* Copy discretization */
19019566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, *dmParallel));
190266fe0bfeSMatthew G. Knepley   /* Create sfNatural */
190366fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
190466fe0bfeSMatthew G. Knepley     PetscSection section;
190566fe0bfeSMatthew G. Knepley 
1906fc6a3818SBlaise Bourdin     PetscCall(DMSetUseNatural(*dmParallel, PETSC_TRUE));
1907fc6a3818SBlaise Bourdin     PetscCall(DMGetLocalSection(dm, &section));
1908fc6a3818SBlaise Bourdin 
19098aee0f92SAlexis Marboeuf     /* First DM with useNatural = PETSC_TRUE is considered natural */
19108aee0f92SAlexis Marboeuf     /* sfMigration and sfNatural are respectively the point and dofs SFs mapping to this natural DM */
1911fc6a3818SBlaise Bourdin     /* Compose with a previous sfNatural if present */
1912fc6a3818SBlaise Bourdin     if (dm->sfNatural) {
1913fc6a3818SBlaise Bourdin       PetscSF natSF;
1914fc6a3818SBlaise Bourdin 
1915fc6a3818SBlaise Bourdin       PetscCall(DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &natSF));
1916fc6a3818SBlaise Bourdin       PetscCall(PetscSFCompose(dm->sfNatural, natSF, &(*dmParallel)->sfNatural));
1917fc6a3818SBlaise Bourdin       PetscCall(PetscSFDestroy(&natSF));
1918fc6a3818SBlaise Bourdin     } else {
1919fc6a3818SBlaise Bourdin       PetscCall(DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural));
1920fc6a3818SBlaise Bourdin     }
19218aee0f92SAlexis Marboeuf     /* Compose with a previous sfMigration if present */
19228aee0f92SAlexis Marboeuf     if (dm->sfMigration) {
19238aee0f92SAlexis Marboeuf       PetscSF naturalPointSF;
19248aee0f92SAlexis Marboeuf 
19258aee0f92SAlexis Marboeuf       PetscCall(PetscSFCompose(dm->sfMigration, sfMigration, &naturalPointSF));
19268aee0f92SAlexis Marboeuf       PetscCall(PetscSFDestroy(&sfMigration));
19278aee0f92SAlexis Marboeuf       sfMigration = naturalPointSF;
19288aee0f92SAlexis Marboeuf     }
192966fe0bfeSMatthew G. Knepley   }
19305de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *dmParallel));
1931721cbd76SMatthew G. Knepley   /* Cleanup */
19329371c9d4SSatish Balay   if (sf) {
19339371c9d4SSatish Balay     *sf = sfMigration;
19349371c9d4SSatish Balay   } else PetscCall(PetscSFDestroy(&sfMigration));
19359566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
19369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_Distribute, dm, 0, 0, 0));
193770034214SMatthew G. Knepley   PetscFunctionReturn(0);
193870034214SMatthew G. Knepley }
1939a157612eSMichael Lange 
1940a157612eSMichael Lange /*@C
194124cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1942a157612eSMichael Lange 
1943d083f849SBarry Smith   Collective on dm
1944a157612eSMichael Lange 
1945064ec1f3Sprj-   Input Parameters:
1946064ec1f3Sprj- + dm  - The non-overlapping distributed DMPlex object
194757fe9a49SVaclav Hapla - overlap - The overlap of partitions (the same on all ranks)
1948a157612eSMichael Lange 
1949064ec1f3Sprj-   Output Parameters:
1950a157612eSMichael Lange + sf - The PetscSF used for point distribution
1951a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1952a157612eSMichael Lange 
1953dccdeccaSVaclav Hapla   Notes:
1954dccdeccaSVaclav Hapla   If the mesh was not distributed, the return value is NULL.
1955a157612eSMichael Lange 
1956b0441da4SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
1957a157612eSMichael Lange   representation on the mesh.
1958a157612eSMichael Lange 
1959c506a872SMatthew G. Knepley   Options Database Keys:
1960c506a872SMatthew G. Knepley + -dm_plex_overlap_labels <name1,name2,...> - List of overlap label names
1961c506a872SMatthew G. Knepley . -dm_plex_overlap_values <int1,int2,...>   - List of overlap label values
1962c506a872SMatthew G. Knepley . -dm_plex_overlap_exclude_label <name>     - Label used to exclude points from overlap
1963c506a872SMatthew G. Knepley - -dm_plex_overlap_exclude_value <int>      - Label value used to exclude points from overlap
1964c506a872SMatthew G. Knepley 
1965dccdeccaSVaclav Hapla   Level: advanced
1966a157612eSMichael Lange 
1967db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexCreateOverlapLabel()`, `DMPlexGetOverlap()`
1968a157612eSMichael Lange @*/
19699371c9d4SSatish Balay PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap) {
1970c506a872SMatthew G. Knepley   DM_Plex     *mesh = (DM_Plex *)dm->data;
1971a157612eSMichael Lange   MPI_Comm     comm;
19723567eaeeSMatthew G. Knepley   PetscMPIInt  size, rank;
1973a157612eSMichael Lange   PetscSection rootSection, leafSection;
1974a157612eSMichael Lange   IS           rootrank, leafrank;
1975cf86098cSMatthew G. Knepley   DM           dmCoord;
1976a9f1d5b2SMichael Lange   DMLabel      lblOverlap;
1977f5bf2dbfSMichael Lange   PetscSF      sfOverlap, sfStratified, sfPoint;
1978a157612eSMichael Lange 
1979a157612eSMichael Lange   PetscFunctionBegin;
1980a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
198157fe9a49SVaclav Hapla   PetscValidLogicalCollectiveInt(dm, overlap, 2);
1982a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1983a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1984a157612eSMichael Lange 
19850c86c063SLisandro Dalcin   if (sf) *sf = NULL;
19860c86c063SLisandro Dalcin   *dmOverlap = NULL;
19879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
19889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
19899566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
19900c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
1991c506a872SMatthew G. Knepley   {
1992c506a872SMatthew G. Knepley     // We need to get options for the _already_distributed mesh, so it must be done here
1993c506a872SMatthew G. Knepley     PetscInt    overlap;
1994c506a872SMatthew G. Knepley     const char *prefix;
1995c506a872SMatthew G. Knepley     char        oldPrefix[PETSC_MAX_PATH_LEN];
1996a157612eSMichael Lange 
1997c506a872SMatthew G. Knepley     oldPrefix[0] = '\0';
1998c506a872SMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
1999c506a872SMatthew G. Knepley     PetscCall(PetscStrcpy(oldPrefix, prefix));
2000c506a872SMatthew G. Knepley     PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, "dist_"));
2001c506a872SMatthew G. Knepley     PetscCall(DMPlexGetOverlap(dm, &overlap));
2002c506a872SMatthew G. Knepley     PetscObjectOptionsBegin((PetscObject)dm);
2003dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
2004c506a872SMatthew G. Knepley     PetscOptionsEnd();
2005c506a872SMatthew G. Knepley     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix[0] == '\0' ? NULL : oldPrefix));
2006c506a872SMatthew G. Knepley   }
20079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0));
2008a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
20099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_Partition, dm, 0, 0, 0));
20109566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(comm, &rootSection));
20119566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(comm, &leafSection));
20129566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank));
2013c506a872SMatthew G. Knepley   if (mesh->numOvLabels) PetscCall(DMPlexCreateOverlapLabelFromLabels(dm, mesh->numOvLabels, mesh->ovLabels, mesh->ovValues, mesh->numOvExLabels, mesh->ovExLabels, mesh->ovExValues, rootSection, rootrank, leafSection, leafrank, &lblOverlap));
2014c506a872SMatthew G. Knepley   else PetscCall(DMPlexCreateOverlapLabel(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap));
2015a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
20169566063dSJacob Faibussowitsch   PetscCall(DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap));
20179566063dSJacob Faibussowitsch   PetscCall(DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified));
20189566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfOverlap));
2019a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
20209566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfOverlap, "Overlap SF"));
20219566063dSJacob Faibussowitsch   PetscCall(PetscSFSetFromOptions(sfOverlap));
2022a9f1d5b2SMichael Lange 
20239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&rootSection));
20249566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&leafSection));
20259566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&rootrank));
20269566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&leafrank));
20279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_Partition, dm, 0, 0, 0));
2028a157612eSMichael Lange 
2029a157612eSMichael Lange   /* Build the overlapping DM */
20309566063dSJacob Faibussowitsch   PetscCall(DMPlexCreate(comm, dmOverlap));
20319566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*dmOverlap, "Parallel Mesh"));
20329566063dSJacob Faibussowitsch   PetscCall(DMPlexMigrate(dm, sfOverlap, *dmOverlap));
2033cb54e036SVaclav Hapla   /* Store the overlap in the new DM */
203460667520SVaclav Hapla   PetscCall(DMPlexSetOverlap_Plex(*dmOverlap, dm, overlap));
2035f5bf2dbfSMichael Lange   /* Build the new point SF */
20369566063dSJacob Faibussowitsch   PetscCall(DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint));
20379566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*dmOverlap, sfPoint));
20389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(*dmOverlap, &dmCoord));
20399566063dSJacob Faibussowitsch   if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint));
20406858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(*dmOverlap, &dmCoord));
20416858538eSMatthew G. Knepley   if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint));
20429566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
2043a157612eSMichael Lange   /* Cleanup overlap partition */
20449566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lblOverlap));
2045a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
20469566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfOverlap));
20479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0));
2048a157612eSMichael Lange   PetscFunctionReturn(0);
2049a157612eSMichael Lange }
20506462276dSToby Isaac 
20519371c9d4SSatish Balay PetscErrorCode DMPlexGetOverlap_Plex(DM dm, PetscInt *overlap) {
2052cb54e036SVaclav Hapla   DM_Plex *mesh = (DM_Plex *)dm->data;
2053cb54e036SVaclav Hapla 
2054cb54e036SVaclav Hapla   PetscFunctionBegin;
2055cb54e036SVaclav Hapla   *overlap = mesh->overlap;
2056cb54e036SVaclav Hapla   PetscFunctionReturn(0);
2057cb54e036SVaclav Hapla }
2058cb54e036SVaclav Hapla 
20599371c9d4SSatish Balay PetscErrorCode DMPlexSetOverlap_Plex(DM dm, DM dmSrc, PetscInt overlap) {
206060667520SVaclav Hapla   DM_Plex *mesh    = NULL;
206160667520SVaclav Hapla   DM_Plex *meshSrc = NULL;
206260667520SVaclav Hapla 
206360667520SVaclav Hapla   PetscFunctionBegin;
206460667520SVaclav Hapla   PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX);
206560667520SVaclav Hapla   mesh          = (DM_Plex *)dm->data;
206660667520SVaclav Hapla   mesh->overlap = overlap;
206760667520SVaclav Hapla   if (dmSrc) {
206860667520SVaclav Hapla     PetscValidHeaderSpecificType(dmSrc, DM_CLASSID, 2, DMPLEX);
206960667520SVaclav Hapla     meshSrc = (DM_Plex *)dmSrc->data;
207060667520SVaclav Hapla     mesh->overlap += meshSrc->overlap;
207160667520SVaclav Hapla   }
207260667520SVaclav Hapla   PetscFunctionReturn(0);
207360667520SVaclav Hapla }
207460667520SVaclav Hapla 
2075cb54e036SVaclav Hapla /*@
2076c506a872SMatthew G. Knepley   DMPlexGetOverlap - Get the width of the cell overlap
2077cb54e036SVaclav Hapla 
2078cb54e036SVaclav Hapla   Not collective
2079cb54e036SVaclav Hapla 
2080cb54e036SVaclav Hapla   Input Parameter:
2081cb54e036SVaclav Hapla . dm   - The DM
2082cb54e036SVaclav Hapla 
2083064ec1f3Sprj-   Output Parameter:
2084c506a872SMatthew G. Knepley . overlap - the width of the cell overlap
2085cb54e036SVaclav Hapla 
2086cb54e036SVaclav Hapla   Level: intermediate
2087cb54e036SVaclav Hapla 
2088c506a872SMatthew G. Knepley .seealso: `DMPlexSetOverlap()`, `DMPlexDistribute()`
2089cb54e036SVaclav Hapla @*/
20909371c9d4SSatish Balay PetscErrorCode DMPlexGetOverlap(DM dm, PetscInt *overlap) {
2091cb54e036SVaclav Hapla   PetscFunctionBegin;
2092cb54e036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2093c506a872SMatthew G. Knepley   PetscValidIntPointer(overlap, 2);
2094cac4c232SBarry Smith   PetscUseMethod(dm, "DMPlexGetOverlap_C", (DM, PetscInt *), (dm, overlap));
2095cb54e036SVaclav Hapla   PetscFunctionReturn(0);
2096cb54e036SVaclav Hapla }
2097cb54e036SVaclav Hapla 
2098c506a872SMatthew G. Knepley /*@
2099c506a872SMatthew G. Knepley   DMPlexSetOverlap - Set the width of the cell overlap
2100c506a872SMatthew G. Knepley 
2101c506a872SMatthew G. Knepley   Logically collective
2102c506a872SMatthew G. Knepley 
2103c506a872SMatthew G. Knepley   Input Parameters:
2104c506a872SMatthew G. Knepley + dm      - The DM
2105c506a872SMatthew G. Knepley . dmSrc   - The DM that produced this one, or NULL
2106c506a872SMatthew G. Knepley - overlap - the width of the cell overlap
2107c506a872SMatthew G. Knepley 
2108c506a872SMatthew G. Knepley   Note:
2109c506a872SMatthew G. Knepley   The overlap from dmSrc is added to dm
2110c506a872SMatthew G. Knepley 
2111c506a872SMatthew G. Knepley   Level: intermediate
2112c506a872SMatthew G. Knepley 
2113c506a872SMatthew G. Knepley .seealso: `DMPlexGetOverlap()`, `DMPlexDistribute()`
2114c506a872SMatthew G. Knepley @*/
21159371c9d4SSatish Balay PetscErrorCode DMPlexSetOverlap(DM dm, DM dmSrc, PetscInt overlap) {
2116c506a872SMatthew G. Knepley   PetscFunctionBegin;
2117c506a872SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2118c506a872SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, overlap, 3);
2119c506a872SMatthew G. Knepley   PetscTryMethod(dm, "DMPlexSetOverlap_C", (DM, DM, PetscInt), (dm, dmSrc, overlap));
2120c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
2121c506a872SMatthew G. Knepley }
2122c506a872SMatthew G. Knepley 
21239371c9d4SSatish Balay PetscErrorCode DMPlexDistributeSetDefault_Plex(DM dm, PetscBool dist) {
2124e600fa54SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
2125e600fa54SMatthew G. Knepley 
2126e600fa54SMatthew G. Knepley   PetscFunctionBegin;
2127e600fa54SMatthew G. Knepley   mesh->distDefault = dist;
2128e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
2129e600fa54SMatthew G. Knepley }
2130e600fa54SMatthew G. Knepley 
2131e600fa54SMatthew G. Knepley /*@
2132e600fa54SMatthew G. Knepley   DMPlexDistributeSetDefault - Set flag indicating whether the DM should be distributed by default
2133e600fa54SMatthew G. Knepley 
2134e600fa54SMatthew G. Knepley   Logically collective
2135e600fa54SMatthew G. Knepley 
2136e600fa54SMatthew G. Knepley   Input Parameters:
2137e600fa54SMatthew G. Knepley + dm   - The DM
2138e600fa54SMatthew G. Knepley - dist - Flag for distribution
2139e600fa54SMatthew G. Knepley 
2140e600fa54SMatthew G. Knepley   Level: intermediate
2141e600fa54SMatthew G. Knepley 
21429e70506fSSatish Balay .seealso: `DMPlexDistributeGetDefault()`, `DMPlexDistribute()`
2143e600fa54SMatthew G. Knepley @*/
21449371c9d4SSatish Balay PetscErrorCode DMPlexDistributeSetDefault(DM dm, PetscBool dist) {
2145e600fa54SMatthew G. Knepley   PetscFunctionBegin;
2146e600fa54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2147e600fa54SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, dist, 2);
2148cac4c232SBarry Smith   PetscTryMethod(dm, "DMPlexDistributeSetDefault_C", (DM, PetscBool), (dm, dist));
2149e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
2150e600fa54SMatthew G. Knepley }
2151e600fa54SMatthew G. Knepley 
21529371c9d4SSatish Balay PetscErrorCode DMPlexDistributeGetDefault_Plex(DM dm, PetscBool *dist) {
2153e600fa54SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
2154e600fa54SMatthew G. Knepley 
2155e600fa54SMatthew G. Knepley   PetscFunctionBegin;
2156e600fa54SMatthew G. Knepley   *dist = mesh->distDefault;
2157e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
2158e600fa54SMatthew G. Knepley }
2159e600fa54SMatthew G. Knepley 
2160e600fa54SMatthew G. Knepley /*@
2161e600fa54SMatthew G. Knepley   DMPlexDistributeGetDefault - Get flag indicating whether the DM should be distributed by default
2162e600fa54SMatthew G. Knepley 
2163e600fa54SMatthew G. Knepley   Not collective
2164e600fa54SMatthew G. Knepley 
2165e600fa54SMatthew G. Knepley   Input Parameter:
2166e600fa54SMatthew G. Knepley . dm   - The DM
2167e600fa54SMatthew G. Knepley 
2168e600fa54SMatthew G. Knepley   Output Parameter:
2169e600fa54SMatthew G. Knepley . dist - Flag for distribution
2170e600fa54SMatthew G. Knepley 
2171e600fa54SMatthew G. Knepley   Level: intermediate
2172e600fa54SMatthew G. Knepley 
21739e70506fSSatish Balay .seealso: `DMPlexDistributeSetDefault()`, `DMPlexDistribute()`
2174e600fa54SMatthew G. Knepley @*/
21759371c9d4SSatish Balay PetscErrorCode DMPlexDistributeGetDefault(DM dm, PetscBool *dist) {
2176e600fa54SMatthew G. Knepley   PetscFunctionBegin;
2177e600fa54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2178e600fa54SMatthew G. Knepley   PetscValidBoolPointer(dist, 2);
2179cac4c232SBarry Smith   PetscUseMethod(dm, "DMPlexDistributeGetDefault_C", (DM, PetscBool *), (dm, dist));
2180e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
2181e600fa54SMatthew G. Knepley }
2182e600fa54SMatthew G. Knepley 
21836462276dSToby Isaac /*@C
21846462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
21856462276dSToby Isaac   root process of the original's communicator.
21866462276dSToby Isaac 
2187d083f849SBarry Smith   Collective on dm
218883655b49SVáclav Hapla 
2189064ec1f3Sprj-   Input Parameter:
21906462276dSToby Isaac . dm - the original DMPlex object
21916462276dSToby Isaac 
21926462276dSToby Isaac   Output Parameters:
2193a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional)
2194a13df41bSStefano Zampini - gatherMesh - the gathered DM object, or NULL
21956462276dSToby Isaac 
21966462276dSToby Isaac   Level: intermediate
21976462276dSToby Isaac 
2198db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetRedundantDM()`
21996462276dSToby Isaac @*/
22009371c9d4SSatish Balay PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh) {
22016462276dSToby Isaac   MPI_Comm         comm;
22026462276dSToby Isaac   PetscMPIInt      size;
22036462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
22046462276dSToby Isaac 
22056462276dSToby Isaac   PetscFunctionBegin;
22066462276dSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2207064a246eSJacob Faibussowitsch   PetscValidPointer(gatherMesh, 3);
22080c86c063SLisandro Dalcin   *gatherMesh = NULL;
2209a13df41bSStefano Zampini   if (sf) *sf = NULL;
22106462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
22119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
22126462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
22139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPartitioner(dm, &oldPart));
22149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)oldPart));
22159566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(comm, &gatherPart));
22169566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetType(gatherPart, PETSCPARTITIONERGATHER));
22179566063dSJacob Faibussowitsch   PetscCall(DMPlexSetPartitioner(dm, gatherPart));
22189566063dSJacob Faibussowitsch   PetscCall(DMPlexDistribute(dm, 0, sf, gatherMesh));
2219a13df41bSStefano Zampini 
22209566063dSJacob Faibussowitsch   PetscCall(DMPlexSetPartitioner(dm, oldPart));
22219566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerDestroy(&gatherPart));
22229566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerDestroy(&oldPart));
22236462276dSToby Isaac   PetscFunctionReturn(0);
22246462276dSToby Isaac }
22256462276dSToby Isaac 
22266462276dSToby Isaac /*@C
22276462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
22286462276dSToby Isaac 
2229d083f849SBarry Smith   Collective on dm
223083655b49SVáclav Hapla 
2231064ec1f3Sprj-   Input Parameter:
22326462276dSToby Isaac . dm - the original DMPlex object
22336462276dSToby Isaac 
22346462276dSToby Isaac   Output Parameters:
2235a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional)
2236a13df41bSStefano Zampini - redundantMesh - the redundant DM object, or NULL
22376462276dSToby Isaac 
22386462276dSToby Isaac   Level: intermediate
22396462276dSToby Isaac 
2240db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetGatherDM()`
22416462276dSToby Isaac @*/
22429371c9d4SSatish Balay PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh) {
22436462276dSToby Isaac   MPI_Comm     comm;
22446462276dSToby Isaac   PetscMPIInt  size, rank;
22456462276dSToby Isaac   PetscInt     pStart, pEnd, p;
22466462276dSToby Isaac   PetscInt     numPoints = -1;
2247a13df41bSStefano Zampini   PetscSF      migrationSF, sfPoint, gatherSF;
22486462276dSToby Isaac   DM           gatherDM, dmCoord;
22496462276dSToby Isaac   PetscSFNode *points;
22506462276dSToby Isaac 
22516462276dSToby Isaac   PetscFunctionBegin;
22526462276dSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2253064a246eSJacob Faibussowitsch   PetscValidPointer(redundantMesh, 3);
22540c86c063SLisandro Dalcin   *redundantMesh = NULL;
22556462276dSToby Isaac   comm           = PetscObjectComm((PetscObject)dm);
22569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
225768dbc166SMatthew G. Knepley   if (size == 1) {
22589566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
225968dbc166SMatthew G. Knepley     *redundantMesh = dm;
2260a13df41bSStefano Zampini     if (sf) *sf = NULL;
226168dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
226268dbc166SMatthew G. Knepley   }
22639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGatherDM(dm, &gatherSF, &gatherDM));
22646462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
22659566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
22669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(gatherDM, &pStart, &pEnd));
22676462276dSToby Isaac   numPoints = pEnd - pStart;
22689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(&numPoints, 1, MPIU_INT, 0, comm));
22699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints, &points));
22709566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &migrationSF));
22716462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
22726462276dSToby Isaac     points[p].index = p;
22736462276dSToby Isaac     points[p].rank  = 0;
22746462276dSToby Isaac   }
22759566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraph(migrationSF, pEnd - pStart, numPoints, NULL, PETSC_OWN_POINTER, points, PETSC_OWN_POINTER));
22769566063dSJacob Faibussowitsch   PetscCall(DMPlexCreate(comm, redundantMesh));
22779566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*redundantMesh, "Redundant Mesh"));
22789566063dSJacob Faibussowitsch   PetscCall(DMPlexMigrate(gatherDM, migrationSF, *redundantMesh));
22792e28cf0cSVaclav Hapla   /* This is to express that all point are in overlap */
22802e28cf0cSVaclav Hapla   PetscCall(DMPlexSetOverlap_Plex(*redundantMesh, NULL, PETSC_MAX_INT));
22819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint));
22829566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*redundantMesh, sfPoint));
22839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(*redundantMesh, &dmCoord));
22849566063dSJacob Faibussowitsch   if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint));
22859566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
2286a13df41bSStefano Zampini   if (sf) {
2287a13df41bSStefano Zampini     PetscSF tsf;
2288a13df41bSStefano Zampini 
22899566063dSJacob Faibussowitsch     PetscCall(PetscSFCompose(gatherSF, migrationSF, &tsf));
22909566063dSJacob Faibussowitsch     PetscCall(DMPlexStratifyMigrationSF(dm, tsf, sf));
22919566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&tsf));
2292a13df41bSStefano Zampini   }
22939566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&migrationSF));
22949566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&gatherSF));
22959566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&gatherDM));
22969566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, *redundantMesh));
22975de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *redundantMesh));
22986462276dSToby Isaac   PetscFunctionReturn(0);
22996462276dSToby Isaac }
23005fa78c88SVaclav Hapla 
23015fa78c88SVaclav Hapla /*@
23025fa78c88SVaclav Hapla   DMPlexIsDistributed - Find out whether this DM is distributed, i.e. more than one rank owns some points.
23035fa78c88SVaclav Hapla 
23045fa78c88SVaclav Hapla   Collective
23055fa78c88SVaclav Hapla 
23065fa78c88SVaclav Hapla   Input Parameter:
23075fa78c88SVaclav Hapla . dm      - The DM object
23085fa78c88SVaclav Hapla 
23095fa78c88SVaclav Hapla   Output Parameter:
23105fa78c88SVaclav Hapla . distributed - Flag whether the DM is distributed
23115fa78c88SVaclav Hapla 
23125fa78c88SVaclav Hapla   Level: intermediate
23135fa78c88SVaclav Hapla 
23145fa78c88SVaclav Hapla   Notes:
23155fa78c88SVaclav Hapla   This currently finds out whether at least two ranks have any DAG points.
23165fa78c88SVaclav Hapla   This involves MPI_Allreduce() with one integer.
23175fa78c88SVaclav Hapla   The result is currently not stashed so every call to this routine involves this global communication.
23185fa78c88SVaclav Hapla 
2319db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetOverlap()`, `DMPlexIsInterpolated()`
23205fa78c88SVaclav Hapla @*/
23219371c9d4SSatish Balay PetscErrorCode DMPlexIsDistributed(DM dm, PetscBool *distributed) {
23225fa78c88SVaclav Hapla   PetscInt    pStart, pEnd, count;
23235fa78c88SVaclav Hapla   MPI_Comm    comm;
232435d70e31SStefano Zampini   PetscMPIInt size;
23255fa78c88SVaclav Hapla 
23265fa78c88SVaclav Hapla   PetscFunctionBegin;
23275fa78c88SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2328dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(distributed, 2);
23299566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
23309566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
23319371c9d4SSatish Balay   if (size == 1) {
23329371c9d4SSatish Balay     *distributed = PETSC_FALSE;
23339371c9d4SSatish Balay     PetscFunctionReturn(0);
23349371c9d4SSatish Balay   }
23359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
233635d70e31SStefano Zampini   count = (pEnd - pStart) > 0 ? 1 : 0;
23379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &count, 1, MPIU_INT, MPI_SUM, comm));
23385fa78c88SVaclav Hapla   *distributed = count > 1 ? PETSC_TRUE : PETSC_FALSE;
23395fa78c88SVaclav Hapla   PetscFunctionReturn(0);
23405fa78c88SVaclav Hapla }
23411d1f2f2aSksagiyam 
23421d1f2f2aSksagiyam /*@C
23431d1f2f2aSksagiyam   DMPlexDistributionSetName - Set the name of the specific parallel distribution
23441d1f2f2aSksagiyam 
23451d1f2f2aSksagiyam   Input Parameters:
23461d1f2f2aSksagiyam + dm   - The DM
23471d1f2f2aSksagiyam - name - The name of the specific parallel distribution
23481d1f2f2aSksagiyam 
23491d1f2f2aSksagiyam   Note:
23501d1f2f2aSksagiyam   If distribution name is set when saving, DMPlexTopologyView() saves the plex's
23511d1f2f2aSksagiyam   parallel distribution (i.e., partition, ownership, and local ordering of points) under
23521d1f2f2aSksagiyam   this name. Conversely, if distribution name is set when loading, DMPlexTopologyLoad()
23531d1f2f2aSksagiyam   loads the parallel distribution stored in file under this name.
23541d1f2f2aSksagiyam 
23551d1f2f2aSksagiyam   Level: developer
23561d1f2f2aSksagiyam 
23571d1f2f2aSksagiyam .seealso: `DMPlexDistributionGetName()`, `DMPlexTopologyView()`, `DMPlexTopologyLoad()`
23581d1f2f2aSksagiyam @*/
23591d1f2f2aSksagiyam PetscErrorCode DMPlexDistributionSetName(DM dm, const char name[]) {
23601d1f2f2aSksagiyam   DM_Plex *mesh = (DM_Plex *)dm->data;
23611d1f2f2aSksagiyam 
23621d1f2f2aSksagiyam   PetscFunctionBegin;
23631d1f2f2aSksagiyam   PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX);
23641d1f2f2aSksagiyam   if (name) PetscValidCharPointer(name, 2);
23651d1f2f2aSksagiyam   PetscCall(PetscFree(mesh->distributionName));
23661d1f2f2aSksagiyam   PetscCall(PetscStrallocpy(name, &mesh->distributionName));
23671d1f2f2aSksagiyam   PetscFunctionReturn(0);
23681d1f2f2aSksagiyam }
23691d1f2f2aSksagiyam 
23701d1f2f2aSksagiyam /*@C
23711d1f2f2aSksagiyam   DMPlexDistributionGetName - Retrieve the name of the specific parallel distribution
23721d1f2f2aSksagiyam 
23731d1f2f2aSksagiyam   Input Parameter:
23741d1f2f2aSksagiyam . dm - The DM
23751d1f2f2aSksagiyam 
23761d1f2f2aSksagiyam   Output Parameter:
23771d1f2f2aSksagiyam . name - The name of the specific parallel distribution
23781d1f2f2aSksagiyam 
23791d1f2f2aSksagiyam   Note:
23801d1f2f2aSksagiyam   If distribution name is set when saving, DMPlexTopologyView() saves the plex's
23811d1f2f2aSksagiyam   parallel distribution (i.e., partition, ownership, and local ordering of points) under
23821d1f2f2aSksagiyam   this name. Conversely, if distribution name is set when loading, DMPlexTopologyLoad()
23831d1f2f2aSksagiyam   loads the parallel distribution stored in file under this name.
23841d1f2f2aSksagiyam 
23851d1f2f2aSksagiyam   Level: developer
23861d1f2f2aSksagiyam 
23871d1f2f2aSksagiyam .seealso: `DMPlexDistributionSetName()`, `DMPlexTopologyView()`, `DMPlexTopologyLoad()`
23881d1f2f2aSksagiyam @*/
23891d1f2f2aSksagiyam PetscErrorCode DMPlexDistributionGetName(DM dm, const char *name[]) {
23901d1f2f2aSksagiyam   DM_Plex *mesh = (DM_Plex *)dm->data;
23911d1f2f2aSksagiyam 
23921d1f2f2aSksagiyam   PetscFunctionBegin;
23931d1f2f2aSksagiyam   PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX);
23941d1f2f2aSksagiyam   PetscValidPointer(name, 2);
23951d1f2f2aSksagiyam   *name = mesh->distributionName;
23961d1f2f2aSksagiyam   PetscFunctionReturn(0);
23971d1f2f2aSksagiyam }
2398