xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision 874ddda940a90e345a5ab44474cbbe9166734fb6)
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 
19b0441da4SMatthew G. Knepley .seealso: DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexGetAdjacency(), DMPlexGetAdjacencyUser()
203c1f0c11SLawrence Mitchell @*/
213c1f0c11SLawrence Mitchell PetscErrorCode DMPlexSetAdjacencyUser(DM dm,PetscErrorCode (*user)(DM,PetscInt,PetscInt*,PetscInt[],void*),void *ctx)
223c1f0c11SLawrence Mitchell {
233c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
243c1f0c11SLawrence Mitchell 
253c1f0c11SLawrence Mitchell   PetscFunctionBegin;
263c1f0c11SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
273c1f0c11SLawrence Mitchell   mesh->useradjacency = user;
283c1f0c11SLawrence Mitchell   mesh->useradjacencyctx = ctx;
293c1f0c11SLawrence Mitchell   PetscFunctionReturn(0);
303c1f0c11SLawrence Mitchell }
313c1f0c11SLawrence Mitchell 
323c1f0c11SLawrence Mitchell /*@C
333c1f0c11SLawrence Mitchell   DMPlexGetAdjacencyUser - get the user-defined adjacency callback
343c1f0c11SLawrence Mitchell 
353c1f0c11SLawrence Mitchell   Input Parameter:
363c1f0c11SLawrence Mitchell . dm      - The DM object
373c1f0c11SLawrence Mitchell 
383c1f0c11SLawrence Mitchell   Output Parameters:
393c1f0c11SLawrence Mitchell - user    - The user callback
403c1f0c11SLawrence Mitchell - ctx     - context for callback evaluation
413c1f0c11SLawrence Mitchell 
423c1f0c11SLawrence Mitchell   Level: advanced
433c1f0c11SLawrence Mitchell 
44b0441da4SMatthew G. Knepley .seealso: DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexGetAdjacency(), DMPlexSetAdjacencyUser()
453c1f0c11SLawrence Mitchell @*/
463c1f0c11SLawrence Mitchell PetscErrorCode DMPlexGetAdjacencyUser(DM dm, PetscErrorCode (**user)(DM,PetscInt,PetscInt*,PetscInt[],void*), void **ctx)
473c1f0c11SLawrence Mitchell {
483c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
493c1f0c11SLawrence Mitchell 
503c1f0c11SLawrence Mitchell   PetscFunctionBegin;
513c1f0c11SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
523c1f0c11SLawrence Mitchell   if (user) *user = mesh->useradjacency;
533c1f0c11SLawrence Mitchell   if (ctx) *ctx = mesh->useradjacencyctx;
543c1f0c11SLawrence Mitchell   PetscFunctionReturn(0);
553c1f0c11SLawrence Mitchell }
563c1f0c11SLawrence Mitchell 
5770034214SMatthew G. Knepley /*@
58a17985deSToby Isaac   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.
598b0b4c70SToby Isaac 
608b0b4c70SToby Isaac   Input Parameters:
618b0b4c70SToby Isaac + dm      - The DM object
625b317d89SToby 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.
638b0b4c70SToby Isaac 
648b0b4c70SToby Isaac   Level: intermediate
658b0b4c70SToby Isaac 
66b0441da4SMatthew G. Knepley .seealso: DMGetAdjacency(), DMSetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
678b0b4c70SToby Isaac @*/
685b317d89SToby Isaac PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors)
698b0b4c70SToby Isaac {
708b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
718b0b4c70SToby Isaac 
728b0b4c70SToby Isaac   PetscFunctionBegin;
738b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
745b317d89SToby Isaac   mesh->useAnchors = useAnchors;
758b0b4c70SToby Isaac   PetscFunctionReturn(0);
768b0b4c70SToby Isaac }
778b0b4c70SToby Isaac 
788b0b4c70SToby Isaac /*@
79a17985deSToby Isaac   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.
808b0b4c70SToby Isaac 
818b0b4c70SToby Isaac   Input Parameter:
828b0b4c70SToby Isaac . dm      - The DM object
838b0b4c70SToby Isaac 
848b0b4c70SToby Isaac   Output Parameter:
855b317d89SToby 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.
868b0b4c70SToby Isaac 
878b0b4c70SToby Isaac   Level: intermediate
888b0b4c70SToby Isaac 
89b0441da4SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseAnchors(), DMSetAdjacency(), DMGetAdjacency(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
908b0b4c70SToby Isaac @*/
915b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors)
928b0b4c70SToby Isaac {
938b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
948b0b4c70SToby Isaac 
958b0b4c70SToby Isaac   PetscFunctionBegin;
968b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
975b317d89SToby Isaac   PetscValidIntPointer(useAnchors, 2);
985b317d89SToby Isaac   *useAnchors = mesh->useAnchors;
998b0b4c70SToby Isaac   PetscFunctionReturn(0);
1008b0b4c70SToby Isaac }
1018b0b4c70SToby Isaac 
10270034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
10370034214SMatthew G. Knepley {
10470034214SMatthew G. Knepley   const PetscInt *cone = NULL;
10570034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
10670034214SMatthew G. Knepley   PetscErrorCode  ierr;
10770034214SMatthew G. Knepley 
10870034214SMatthew G. Knepley   PetscFunctionBeginHot;
10970034214SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
11070034214SMatthew G. Knepley   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1114b6b44bdSMatthew G. Knepley   for (c = 0; c <= coneSize; ++c) {
1124b6b44bdSMatthew G. Knepley     const PetscInt  point   = !c ? p : cone[c-1];
11370034214SMatthew G. Knepley     const PetscInt *support = NULL;
11470034214SMatthew G. Knepley     PetscInt        supportSize, s, q;
11570034214SMatthew G. Knepley 
1164b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
1174b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
11870034214SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
119527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]),0); ++q) {
12070034214SMatthew G. Knepley         if (support[s] == adj[q]) break;
12170034214SMatthew G. Knepley       }
12270034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
12370034214SMatthew G. Knepley     }
12470034214SMatthew G. Knepley   }
12570034214SMatthew G. Knepley   *adjSize = numAdj;
12670034214SMatthew G. Knepley   PetscFunctionReturn(0);
12770034214SMatthew G. Knepley }
12870034214SMatthew G. Knepley 
12970034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
13070034214SMatthew G. Knepley {
13170034214SMatthew G. Knepley   const PetscInt *support = NULL;
13270034214SMatthew G. Knepley   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
13370034214SMatthew G. Knepley   PetscErrorCode  ierr;
13470034214SMatthew G. Knepley 
13570034214SMatthew G. Knepley   PetscFunctionBeginHot;
13670034214SMatthew G. Knepley   ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
13770034214SMatthew G. Knepley   ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
1384b6b44bdSMatthew G. Knepley   for (s = 0; s <= supportSize; ++s) {
1394b6b44bdSMatthew G. Knepley     const PetscInt  point = !s ? p : support[s-1];
14070034214SMatthew G. Knepley     const PetscInt *cone  = NULL;
14170034214SMatthew G. Knepley     PetscInt        coneSize, c, q;
14270034214SMatthew G. Knepley 
1434b6b44bdSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1444b6b44bdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
14570034214SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
146527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]),0); ++q) {
14770034214SMatthew G. Knepley         if (cone[c] == adj[q]) break;
14870034214SMatthew G. Knepley       }
14970034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
15070034214SMatthew G. Knepley     }
15170034214SMatthew G. Knepley   }
15270034214SMatthew G. Knepley   *adjSize = numAdj;
15370034214SMatthew G. Knepley   PetscFunctionReturn(0);
15470034214SMatthew G. Knepley }
15570034214SMatthew G. Knepley 
15670034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[])
15770034214SMatthew G. Knepley {
15870034214SMatthew G. Knepley   PetscInt      *star = NULL;
15970034214SMatthew G. Knepley   PetscInt       numAdj = 0, maxAdjSize = *adjSize, starSize, s;
16070034214SMatthew G. Knepley   PetscErrorCode ierr;
16170034214SMatthew G. Knepley 
16270034214SMatthew G. Knepley   PetscFunctionBeginHot;
16370034214SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
16470034214SMatthew G. Knepley   for (s = 0; s < starSize*2; s += 2) {
16570034214SMatthew G. Knepley     const PetscInt *closure = NULL;
16670034214SMatthew G. Knepley     PetscInt        closureSize, c, q;
16770034214SMatthew G. Knepley 
16870034214SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
16970034214SMatthew G. Knepley     for (c = 0; c < closureSize*2; c += 2) {
170527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]),0); ++q) {
17170034214SMatthew G. Knepley         if (closure[c] == adj[q]) break;
17270034214SMatthew G. Knepley       }
17370034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
17470034214SMatthew G. Knepley     }
17570034214SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
17670034214SMatthew G. Knepley   }
17770034214SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
17870034214SMatthew G. Knepley   *adjSize = numAdj;
17970034214SMatthew G. Knepley   PetscFunctionReturn(0);
18070034214SMatthew G. Knepley }
18170034214SMatthew G. Knepley 
1825b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[])
18370034214SMatthew G. Knepley {
18479bad331SMatthew G. Knepley   static PetscInt asiz = 0;
1858b0b4c70SToby Isaac   PetscInt maxAnchors = 1;
1868b0b4c70SToby Isaac   PetscInt aStart = -1, aEnd = -1;
1878b0b4c70SToby Isaac   PetscInt maxAdjSize;
1888b0b4c70SToby Isaac   PetscSection aSec = NULL;
1898b0b4c70SToby Isaac   IS aIS = NULL;
1908b0b4c70SToby Isaac   const PetscInt *anchors;
1913c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
19270034214SMatthew G. Knepley   PetscErrorCode  ierr;
19370034214SMatthew G. Knepley 
19470034214SMatthew G. Knepley   PetscFunctionBeginHot;
1955b317d89SToby Isaac   if (useAnchors) {
196a17985deSToby Isaac     ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
1978b0b4c70SToby Isaac     if (aSec) {
1988b0b4c70SToby Isaac       ierr = PetscSectionGetMaxDof(aSec,&maxAnchors);CHKERRQ(ierr);
19924c766afSToby Isaac       maxAnchors = PetscMax(1,maxAnchors);
2008b0b4c70SToby Isaac       ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
2018b0b4c70SToby Isaac       ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
2028b0b4c70SToby Isaac     }
2038b0b4c70SToby Isaac   }
20479bad331SMatthew G. Knepley   if (!*adj) {
20524c766afSToby Isaac     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;
20679bad331SMatthew G. Knepley 
20724c766afSToby Isaac     ierr  = DMPlexGetChart(dm, &pStart,&pEnd);CHKERRQ(ierr);
20879bad331SMatthew G. Knepley     ierr  = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
20924c766afSToby Isaac     ierr  = DMPlexGetMaxSizes(dm, &maxC, &maxS);CHKERRQ(ierr);
21024c766afSToby Isaac     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1;
21124c766afSToby Isaac     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1;
21224c766afSToby Isaac     asiz  = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries);
2138b0b4c70SToby Isaac     asiz *= maxAnchors;
21424c766afSToby Isaac     asiz  = PetscMin(asiz,pEnd-pStart);
21579bad331SMatthew G. Knepley     ierr  = PetscMalloc1(asiz,adj);CHKERRQ(ierr);
21679bad331SMatthew G. Knepley   }
21779bad331SMatthew G. Knepley   if (*adjSize < 0) *adjSize = asiz;
2188b0b4c70SToby Isaac   maxAdjSize = *adjSize;
2193c1f0c11SLawrence Mitchell   if (mesh->useradjacency) {
2203c1f0c11SLawrence Mitchell     ierr = mesh->useradjacency(dm, p, adjSize, *adj, mesh->useradjacencyctx);CHKERRQ(ierr);
2213c1f0c11SLawrence Mitchell   } else if (useTransitiveClosure) {
22279bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);CHKERRQ(ierr);
22370034214SMatthew G. Knepley   } else if (useCone) {
22479bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
22570034214SMatthew G. Knepley   } else {
22679bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
22770034214SMatthew G. Knepley   }
2285b317d89SToby Isaac   if (useAnchors && aSec) {
2298b0b4c70SToby Isaac     PetscInt origSize = *adjSize;
2308b0b4c70SToby Isaac     PetscInt numAdj = origSize;
2318b0b4c70SToby Isaac     PetscInt i = 0, j;
2328b0b4c70SToby Isaac     PetscInt *orig = *adj;
2338b0b4c70SToby Isaac 
2348b0b4c70SToby Isaac     while (i < origSize) {
2358b0b4c70SToby Isaac       PetscInt p = orig[i];
2368b0b4c70SToby Isaac       PetscInt aDof = 0;
2378b0b4c70SToby Isaac 
2388b0b4c70SToby Isaac       if (p >= aStart && p < aEnd) {
2398b0b4c70SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
2408b0b4c70SToby Isaac       }
2418b0b4c70SToby Isaac       if (aDof) {
2428b0b4c70SToby Isaac         PetscInt aOff;
2438b0b4c70SToby Isaac         PetscInt s, q;
2448b0b4c70SToby Isaac 
2458b0b4c70SToby Isaac         for (j = i + 1; j < numAdj; j++) {
2468b0b4c70SToby Isaac           orig[j - 1] = orig[j];
2478b0b4c70SToby Isaac         }
2488b0b4c70SToby Isaac         origSize--;
2498b0b4c70SToby Isaac         numAdj--;
2508b0b4c70SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
2518b0b4c70SToby Isaac         for (s = 0; s < aDof; ++s) {
252527e7439SSatish Balay           for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) {
2538b0b4c70SToby Isaac             if (anchors[aOff+s] == orig[q]) break;
2548b0b4c70SToby Isaac           }
2558b0b4c70SToby Isaac           if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
2568b0b4c70SToby Isaac         }
2578b0b4c70SToby Isaac       }
2588b0b4c70SToby Isaac       else {
2598b0b4c70SToby Isaac         i++;
2608b0b4c70SToby Isaac       }
2618b0b4c70SToby Isaac     }
2628b0b4c70SToby Isaac     *adjSize = numAdj;
2638b0b4c70SToby Isaac     ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
2648b0b4c70SToby Isaac   }
26570034214SMatthew G. Knepley   PetscFunctionReturn(0);
26670034214SMatthew G. Knepley }
26770034214SMatthew G. Knepley 
26870034214SMatthew G. Knepley /*@
26970034214SMatthew G. Knepley   DMPlexGetAdjacency - Return all points adjacent to the given point
27070034214SMatthew G. Knepley 
27170034214SMatthew G. Knepley   Input Parameters:
27270034214SMatthew G. Knepley + dm - The DM object
27370034214SMatthew G. Knepley . p  - The point
27470034214SMatthew G. Knepley . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE
27570034214SMatthew G. Knepley - adj - Either NULL so that the array is allocated, or an existing array with size adjSize
27670034214SMatthew G. Knepley 
27770034214SMatthew G. Knepley   Output Parameters:
27870034214SMatthew G. Knepley + adjSize - The number of adjacent points
27970034214SMatthew G. Knepley - adj - The adjacent points
28070034214SMatthew G. Knepley 
28170034214SMatthew G. Knepley   Level: advanced
28270034214SMatthew G. Knepley 
28395452b02SPatrick Sanan   Notes:
28495452b02SPatrick Sanan     The user must PetscFree the adj array if it was not passed in.
28570034214SMatthew G. Knepley 
286b0441da4SMatthew G. Knepley .seealso: DMSetAdjacency(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
28770034214SMatthew G. Knepley @*/
28870034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
28970034214SMatthew G. Knepley {
2901cf84007SMatthew G. Knepley   PetscBool      useCone, useClosure, useAnchors;
29170034214SMatthew G. Knepley   PetscErrorCode ierr;
29270034214SMatthew G. Knepley 
29370034214SMatthew G. Knepley   PetscFunctionBeginHot;
29470034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29570034214SMatthew G. Knepley   PetscValidPointer(adjSize,3);
29670034214SMatthew G. Knepley   PetscValidPointer(adj,4);
297b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
2981cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
2991cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);CHKERRQ(ierr);
30070034214SMatthew G. Knepley   PetscFunctionReturn(0);
30170034214SMatthew G. Knepley }
30208633170SToby Isaac 
303b0a623aaSMatthew G. Knepley /*@
304b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
305b0a623aaSMatthew G. Knepley 
306b0a623aaSMatthew G. Knepley   Collective on DM
307b0a623aaSMatthew G. Knepley 
308b0a623aaSMatthew G. Knepley   Input Parameters:
309b0a623aaSMatthew G. Knepley + dm      - The DM
310b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity
311b0a623aaSMatthew G. Knepley 
312b0a623aaSMatthew G. Knepley   Output Parameters:
313b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
314b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
315b0a623aaSMatthew G. Knepley 
316b0a623aaSMatthew G. Knepley   Level: developer
317b0a623aaSMatthew G. Knepley 
318b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
319b0a623aaSMatthew G. Knepley @*/
320b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
321b0a623aaSMatthew G. Knepley {
322b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
323b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
324b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
325b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
326b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
327b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
328b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
3299852e123SBarry Smith   PetscMPIInt        size, proc, rank;
330b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
331b0a623aaSMatthew G. Knepley 
332b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
333b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
334b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
335b0a623aaSMatthew G. Knepley   if (processRanks) {PetscValidPointer(processRanks, 3);}
336b0a623aaSMatthew G. Knepley   if (sfProcess)    {PetscValidPointer(sfProcess, 4);}
3379852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
338b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
339b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr);
3409852e123SBarry Smith   ierr = PetscBTCreate(size, &neighbors);CHKERRQ(ierr);
3419852e123SBarry Smith   ierr = PetscBTMemzero(size, neighbors);CHKERRQ(ierr);
342b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
343b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr);
344b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr);
345b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
346b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
347b0a623aaSMatthew G. Knepley 
348b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr);
349b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr);
350302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
351b0a623aaSMatthew G. Knepley   }
352b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr);
353b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
354b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr);
355b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr);
356b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
357b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
358b0a623aaSMatthew G. Knepley 
359b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr);
360b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr);
361302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
362b0a623aaSMatthew G. Knepley   }
363b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr);
364b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
365b0a623aaSMatthew G. Knepley   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
366b0a623aaSMatthew G. Knepley   /* Calculate edges */
367b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
3689852e123SBarry Smith   for(proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
369b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr);
370b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr);
371b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr);
3729852e123SBarry Smith   for(proc = 0, n = 0; proc < size; ++proc) {
373b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
374b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
375b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
376b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
377b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
378b0a623aaSMatthew G. Knepley       ++n;
379b0a623aaSMatthew G. Knepley     }
380b0a623aaSMatthew G. Knepley   }
381b0a623aaSMatthew G. Knepley   ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr);
382b0a623aaSMatthew G. Knepley   if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);}
383b0a623aaSMatthew G. Knepley   else              {ierr = PetscFree(ranksNew);CHKERRQ(ierr);}
384b0a623aaSMatthew G. Knepley   if (sfProcess) {
385b0a623aaSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr);
386b0a623aaSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr);
387b0a623aaSMatthew G. Knepley     ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr);
3889852e123SBarry Smith     ierr = PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
389b0a623aaSMatthew G. Knepley   }
390b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
391b0a623aaSMatthew G. Knepley }
392b0a623aaSMatthew G. Knepley 
393b0a623aaSMatthew G. Knepley /*@
394b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
395b0a623aaSMatthew G. Knepley 
396b0a623aaSMatthew G. Knepley   Collective on DM
397b0a623aaSMatthew G. Knepley 
398b0a623aaSMatthew G. Knepley   Input Parameter:
399b0a623aaSMatthew G. Knepley . dm - The DM
400b0a623aaSMatthew G. Knepley 
401b0a623aaSMatthew G. Knepley   Output Parameters:
402b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
403b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
404b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
405b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
406b0a623aaSMatthew G. Knepley 
407b0a623aaSMatthew G. Knepley   Level: developer
408b0a623aaSMatthew G. Knepley 
409b0a623aaSMatthew G. Knepley .seealso: DMPlexCreateOverlap()
410b0a623aaSMatthew G. Knepley @*/
411b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
412b0a623aaSMatthew G. Knepley {
413b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
414b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
415b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
416b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
417b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
418b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
419b0a623aaSMatthew G. Knepley   PetscErrorCode  ierr;
420b0a623aaSMatthew G. Knepley 
421b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
422b0a623aaSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
423b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
424b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
425b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
426b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
427b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr);
428b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr);
429b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr);
430b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr);
431b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);}
432b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
433b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
434b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr);
435b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr);
436b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(nedges,  &remoterank);CHKERRQ(ierr);
437b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
438b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
439b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
440b0a623aaSMatthew G. Knepley   ierr = PetscFree(myrank);CHKERRQ(ierr);
441b0a623aaSMatthew G. Knepley   ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr);
442b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
443b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr);
444b0a623aaSMatthew G. Knepley   ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr);
445b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
446b0a623aaSMatthew G. Knepley }
447b0a623aaSMatthew G. Knepley 
448278397a0SMatthew G. Knepley /*@C
449b0a623aaSMatthew G. Knepley   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.
450b0a623aaSMatthew G. Knepley 
451b0a623aaSMatthew G. Knepley   Collective on DM
452b0a623aaSMatthew G. Knepley 
453b0a623aaSMatthew G. Knepley   Input Parameters:
454b0a623aaSMatthew G. Knepley + dm          - The DM
45524d039d7SMichael Lange . levels      - Number of overlap levels
456b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
457b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
458b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
459b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
460b0a623aaSMatthew G. Knepley 
461b0a623aaSMatthew G. Knepley   Output Parameters:
462a9f1d5b2SMichael Lange + ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
463b0a623aaSMatthew G. Knepley 
464b0a623aaSMatthew G. Knepley   Level: developer
465b0a623aaSMatthew G. Knepley 
4661fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
467b0a623aaSMatthew G. Knepley @*/
468a9f1d5b2SMichael Lange PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
469b0a623aaSMatthew G. Knepley {
470e540f424SMichael Lange   MPI_Comm           comm;
471b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
472*874ddda9SLisandro Dalcin   PetscSF            sfPoint;
473b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
474b0a623aaSMatthew G. Knepley   const PetscInt    *local;
4751fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
476b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
4771fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
4789852e123SBarry Smith   PetscMPIInt        rank, size;
47926a7d390SMatthew G. Knepley   PetscBool          useCone, useClosure, flg;
480b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
481b0a623aaSMatthew G. Knepley 
482b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
483e540f424SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
4849852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
485e540f424SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
486b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
487b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
488b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr);
489b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
490d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr);
491b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
492b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
493b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
494b0a623aaSMatthew G. Knepley 
495df20756eSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj);CHKERRQ(ierr);
496b0a623aaSMatthew G. Knepley     for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);}
497b0a623aaSMatthew G. Knepley   }
498b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr);
499b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr);
500b0a623aaSMatthew G. Knepley   /* Handle roots */
501b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
502b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
503b0a623aaSMatthew G. Knepley 
504b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
505b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
506b0a623aaSMatthew G. Knepley       ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr);
507b0a623aaSMatthew G. Knepley       if (neighbors) {
508b0a623aaSMatthew G. Knepley         ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr);
509b0a623aaSMatthew G. Knepley         ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
510b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
511b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff+n];
512b0a623aaSMatthew G. Knepley 
513b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
514b0a623aaSMatthew G. Knepley           for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
515b0a623aaSMatthew G. Knepley         }
516b0a623aaSMatthew G. Knepley       }
517b0a623aaSMatthew G. Knepley     }
518b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
519b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr);
520b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
521b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr);
522b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
523b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
524b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff+n];
525b0a623aaSMatthew G. Knepley 
526b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
527b0a623aaSMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
528b0a623aaSMatthew G. Knepley     }
529b0a623aaSMatthew G. Knepley   }
530b0a623aaSMatthew G. Knepley   ierr = PetscFree(adj);CHKERRQ(ierr);
531b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr);
532b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr);
53324d039d7SMichael Lange   /* Add additional overlap levels */
534be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
535be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
536be200f8dSMichael Lange     ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr);
537be200f8dSMichael Lange     /* Add next level of point donations to the label */
538be200f8dSMichael Lange     ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr);
539be200f8dSMichael Lange   }
54026a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
541b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
54226a7d390SMatthew G. Knepley   if (useCone || !useClosure) {
5435abbe4feSMichael Lange     ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr);
54426a7d390SMatthew G. Knepley   }
545c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr);
546e540f424SMichael Lange   if (flg) {
547825f8a23SLisandro Dalcin     PetscViewer viewer;
548825f8a23SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer);CHKERRQ(ierr);
549825f8a23SLisandro Dalcin     ierr = DMLabelView(ovAdjByRank, viewer);CHKERRQ(ierr);
550b0a623aaSMatthew G. Knepley   }
551*874ddda9SLisandro Dalcin   /* Invert sender to receiver label */
552d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel);CHKERRQ(ierr);
553*874ddda9SLisandro Dalcin   ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel);CHKERRQ(ierr);
554a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
555a9f1d5b2SMichael Lange   for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);}
556e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
557a9f1d5b2SMichael Lange     ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr);
558a9f1d5b2SMichael Lange     ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr);
559e540f424SMichael Lange   }
560e540f424SMichael Lange   /* Clean up */
5611fd9873aSMichael Lange   ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr);
562b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
563b0a623aaSMatthew G. Knepley }
56470034214SMatthew G. Knepley 
56524cc2ca5SMatthew G. Knepley /*@C
56624cc2ca5SMatthew G. Knepley   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF
56724cc2ca5SMatthew G. Knepley 
56824cc2ca5SMatthew G. Knepley   Collective on DM
56924cc2ca5SMatthew G. Knepley 
57024cc2ca5SMatthew G. Knepley   Input Parameters:
57124cc2ca5SMatthew G. Knepley + dm          - The DM
57224cc2ca5SMatthew G. Knepley - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes
57324cc2ca5SMatthew G. Knepley 
57424cc2ca5SMatthew G. Knepley   Output Parameters:
57524cc2ca5SMatthew G. Knepley + migrationSF - An SF that maps original points in old locations to points in new locations
57624cc2ca5SMatthew G. Knepley 
57724cc2ca5SMatthew G. Knepley   Level: developer
57824cc2ca5SMatthew G. Knepley 
57924cc2ca5SMatthew G. Knepley .seealso: DMPlexCreateOverlap(), DMPlexDistribute()
58024cc2ca5SMatthew G. Knepley @*/
58146f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
58246f9b1c3SMichael Lange {
58346f9b1c3SMichael Lange   MPI_Comm           comm;
5849852e123SBarry Smith   PetscMPIInt        rank, size;
58546f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
58646f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
58746f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
58846f9b1c3SMichael Lange   PetscSFNode       *iremote;
58946f9b1c3SMichael Lange   PetscSF            pointSF;
59046f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
59146f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
59246f9b1c3SMichael Lange   PetscErrorCode     ierr;
59346f9b1c3SMichael Lange 
59446f9b1c3SMichael Lange   PetscFunctionBegin;
59546f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59646f9b1c3SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
59746f9b1c3SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
5989852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
59946f9b1c3SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
60046f9b1c3SMichael Lange 
60146f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
60246f9b1c3SMichael Lange   ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr);
60346f9b1c3SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
60446f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
60546f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
60646f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
60746f9b1c3SMichael Lange   }
60846f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
60946f9b1c3SMichael Lange   ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
61046f9b1c3SMichael Lange   ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
61146f9b1c3SMichael Lange 
61246f9b1c3SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
61346f9b1c3SMichael Lange   ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr);
61446f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) depthRecv[d]=0;
61546f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
61646f9b1c3SMichael Lange   depthShift[dim] = 0;
61746f9b1c3SMichael Lange   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
61846f9b1c3SMichael Lange   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
61946f9b1c3SMichael Lange   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
62046f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
62146f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
62246f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
62346f9b1c3SMichael Lange   }
62446f9b1c3SMichael Lange 
62546f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
62646f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
62746f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
62809b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr);
62909b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr);
63046f9b1c3SMichael Lange   /* First map local points to themselves */
63146f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
63246f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
63346f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) {
63446f9b1c3SMichael Lange       point = p + depthShift[d];
63546f9b1c3SMichael Lange       ilocal[point] = point;
63646f9b1c3SMichael Lange       iremote[point].index = p;
63746f9b1c3SMichael Lange       iremote[point].rank = rank;
63846f9b1c3SMichael Lange       depthIdx[d]++;
63946f9b1c3SMichael Lange     }
64046f9b1c3SMichael Lange   }
64146f9b1c3SMichael Lange 
64246f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
64346f9b1c3SMichael Lange   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
64446f9b1c3SMichael Lange   ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr);
64546f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
64646f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
64746f9b1c3SMichael Lange     for (p=0; p<numSharedPoints; p++) {
64846f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
64946f9b1c3SMichael Lange         point = sharedLocal[p] + depthShift[d];
65046f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
65146f9b1c3SMichael Lange         iremote[point].rank = sharedRemote[p].rank;
65246f9b1c3SMichael Lange       }
65346f9b1c3SMichael Lange     }
65446f9b1c3SMichael Lange   }
65546f9b1c3SMichael Lange 
65646f9b1c3SMichael Lange   /* Now add the incoming overlap points */
65746f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) {
65846f9b1c3SMichael Lange     point = depthIdx[remoteDepths[p]];
65946f9b1c3SMichael Lange     ilocal[point] = point;
66046f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
66146f9b1c3SMichael Lange     iremote[point].rank = overlapRemote[p].rank;
66246f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
66346f9b1c3SMichael Lange   }
66415fff7beSMatthew G. Knepley   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
66546f9b1c3SMichael Lange 
66646f9b1c3SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
66746f9b1c3SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr);
66846f9b1c3SMichael Lange   ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr);
66946f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
67046f9b1c3SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
67146f9b1c3SMichael Lange 
67246f9b1c3SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
67370034214SMatthew G. Knepley   PetscFunctionReturn(0);
67470034214SMatthew G. Knepley }
67570034214SMatthew G. Knepley 
676a9f1d5b2SMichael Lange /*@
677f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
678a9f1d5b2SMichael Lange 
679a9f1d5b2SMichael Lange   Input Parameter:
680a9f1d5b2SMichael Lange + dm          - The DM
681a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
682a9f1d5b2SMichael Lange 
683a9f1d5b2SMichael Lange   Output Parameter:
684a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
685a9f1d5b2SMichael Lange 
686a9f1d5b2SMichael Lange   Level: developer
687a9f1d5b2SMichael Lange 
688a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
689a9f1d5b2SMichael Lange @*/
690a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
691a9f1d5b2SMichael Lange {
692a9f1d5b2SMichael Lange   MPI_Comm           comm;
6939852e123SBarry Smith   PetscMPIInt        rank, size;
6947fab53ddSMatthew G. Knepley   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
695a9f1d5b2SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
696a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
697f0e73a3dSToby Isaac   PetscInt           hybEnd[4];
698a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
699a9f1d5b2SMichael Lange   PetscErrorCode     ierr;
700a9f1d5b2SMichael Lange 
701a9f1d5b2SMichael Lange   PetscFunctionBegin;
702a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
703a9f1d5b2SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
704a9f1d5b2SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
7059852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
7067fab53ddSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr);
707b2566f29SBarry Smith   ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
7087fab53ddSMatthew G. Knepley   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
709a9f1d5b2SMichael Lange 
710a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
711a9f1d5b2SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr);
712a9f1d5b2SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
713f0e73a3dSToby Isaac   ierr = DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[depth-1],&hybEnd[1],&hybEnd[0]);CHKERRQ(ierr);
7147fab53ddSMatthew G. Knepley   for (d = 0; d < depth+1; ++d) {
715a9f1d5b2SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
716f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
717f0e73a3dSToby Isaac       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
718f0e73a3dSToby Isaac         pointDepths[p] = 2 * d;
719f0e73a3dSToby Isaac       } else {
720f0e73a3dSToby Isaac         pointDepths[p] = 2 * d + 1;
721f0e73a3dSToby Isaac       }
722f0e73a3dSToby Isaac     }
723a9f1d5b2SMichael Lange   }
7247fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
725a9f1d5b2SMichael Lange   ierr = PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
726a9f1d5b2SMichael Lange   ierr = PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
727a9f1d5b2SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
728f0e73a3dSToby Isaac   ierr = PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);CHKERRQ(ierr);
729f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
7307fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
731f0e73a3dSToby Isaac   depthShift[2*depth+1] = 0;
732f0e73a3dSToby Isaac   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
733f0e73a3dSToby Isaac   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
734f0e73a3dSToby Isaac   depthShift[0] += depthRecv[1];
735f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
736f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
737f0e73a3dSToby Isaac   for (d = 2 * depth-1; d > 2; --d) {
738f0e73a3dSToby Isaac     PetscInt e;
739f0e73a3dSToby Isaac 
740f0e73a3dSToby Isaac     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
741f0e73a3dSToby Isaac   }
742f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
743a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
744a9f1d5b2SMichael Lange   ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr);
7457fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
7467fab53ddSMatthew G. Knepley     const PetscInt dep = remoteDepths[p];
7477fab53ddSMatthew G. Knepley 
7487fab53ddSMatthew G. Knepley     ilocal[p] = depthShift[dep] + depthIdx[dep];
7497fab53ddSMatthew G. Knepley     depthIdx[dep]++;
750a9f1d5b2SMichael Lange   }
751a9f1d5b2SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
752a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr);
753a9f1d5b2SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr);
754a9f1d5b2SMichael Lange   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
755a9f1d5b2SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
756a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
757a9f1d5b2SMichael Lange }
758a9f1d5b2SMichael Lange 
75970034214SMatthew G. Knepley /*@
76070034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
76170034214SMatthew G. Knepley 
76270034214SMatthew G. Knepley   Collective on DM
76370034214SMatthew G. Knepley 
76470034214SMatthew G. Knepley   Input Parameters:
76570034214SMatthew G. Knepley + dm - The DMPlex object
76670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
76770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
76870034214SMatthew G. Knepley - originalVec - The existing data
76970034214SMatthew G. Knepley 
77070034214SMatthew G. Knepley   Output Parameters:
77170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
77270034214SMatthew G. Knepley - newVec - The new data
77370034214SMatthew G. Knepley 
77470034214SMatthew G. Knepley   Level: developer
77570034214SMatthew G. Knepley 
77670034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
77770034214SMatthew G. Knepley @*/
77870034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
77970034214SMatthew G. Knepley {
78070034214SMatthew G. Knepley   PetscSF        fieldSF;
78170034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
78270034214SMatthew G. Knepley   PetscScalar   *originalValues, *newValues;
78370034214SMatthew G. Knepley   PetscErrorCode ierr;
78470034214SMatthew G. Knepley 
78570034214SMatthew G. Knepley   PetscFunctionBegin;
78670034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
78770034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
78870034214SMatthew G. Knepley 
78970034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
79070034214SMatthew G. Knepley   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
79170034214SMatthew G. Knepley   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
79270034214SMatthew G. Knepley 
79370034214SMatthew G. Knepley   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
79470034214SMatthew G. Knepley   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
79570034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
7968a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
79770034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
79870034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
79970034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
80070034214SMatthew G. Knepley   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
80170034214SMatthew G. Knepley   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
80270034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
80370034214SMatthew G. Knepley   PetscFunctionReturn(0);
80470034214SMatthew G. Knepley }
80570034214SMatthew G. Knepley 
80670034214SMatthew G. Knepley /*@
80770034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
80870034214SMatthew G. Knepley 
80970034214SMatthew G. Knepley   Collective on DM
81070034214SMatthew G. Knepley 
81170034214SMatthew G. Knepley   Input Parameters:
81270034214SMatthew G. Knepley + dm - The DMPlex object
81370034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
81470034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
81570034214SMatthew G. Knepley - originalIS - The existing data
81670034214SMatthew G. Knepley 
81770034214SMatthew G. Knepley   Output Parameters:
81870034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
81970034214SMatthew G. Knepley - newIS - The new data
82070034214SMatthew G. Knepley 
82170034214SMatthew G. Knepley   Level: developer
82270034214SMatthew G. Knepley 
82370034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
82470034214SMatthew G. Knepley @*/
82570034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
82670034214SMatthew G. Knepley {
82770034214SMatthew G. Knepley   PetscSF         fieldSF;
82870034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
82970034214SMatthew G. Knepley   const PetscInt *originalValues;
83070034214SMatthew G. Knepley   PetscErrorCode  ierr;
83170034214SMatthew G. Knepley 
83270034214SMatthew G. Knepley   PetscFunctionBegin;
83370034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
83470034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
83570034214SMatthew G. Knepley 
83670034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
837854ce69bSBarry Smith   ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr);
83870034214SMatthew G. Knepley 
83970034214SMatthew G. Knepley   ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr);
84070034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
8418a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
842aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
843aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
84470034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
84570034214SMatthew G. Knepley   ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr);
84670034214SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr);
84770034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
84870034214SMatthew G. Knepley   PetscFunctionReturn(0);
84970034214SMatthew G. Knepley }
85070034214SMatthew G. Knepley 
85170034214SMatthew G. Knepley /*@
85270034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
85370034214SMatthew G. Knepley 
85470034214SMatthew G. Knepley   Collective on DM
85570034214SMatthew G. Knepley 
85670034214SMatthew G. Knepley   Input Parameters:
85770034214SMatthew G. Knepley + dm - The DMPlex object
85870034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
85970034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
86070034214SMatthew G. Knepley . datatype - The type of data
86170034214SMatthew G. Knepley - originalData - The existing data
86270034214SMatthew G. Knepley 
86370034214SMatthew G. Knepley   Output Parameters:
86470034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
86570034214SMatthew G. Knepley - newData - The new data
86670034214SMatthew G. Knepley 
86770034214SMatthew G. Knepley   Level: developer
86870034214SMatthew G. Knepley 
86970034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
87070034214SMatthew G. Knepley @*/
87170034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
87270034214SMatthew G. Knepley {
87370034214SMatthew G. Knepley   PetscSF        fieldSF;
87470034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
87570034214SMatthew G. Knepley   PetscMPIInt    dataSize;
87670034214SMatthew G. Knepley   PetscErrorCode ierr;
87770034214SMatthew G. Knepley 
87870034214SMatthew G. Knepley   PetscFunctionBegin;
87970034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
88070034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
88170034214SMatthew G. Knepley 
88270034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
88370034214SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
88470034214SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
88570034214SMatthew G. Knepley 
88670034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
8878a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
88870034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
88970034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
89070034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
89170034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
89270034214SMatthew G. Knepley   PetscFunctionReturn(0);
89370034214SMatthew G. Knepley }
89470034214SMatthew G. Knepley 
89524cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
896cc71bff1SMichael Lange {
897cc71bff1SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
898cc71bff1SMichael Lange   MPI_Comm               comm;
899cc71bff1SMichael Lange   PetscSF                coneSF;
900cc71bff1SMichael Lange   PetscSection           originalConeSection, newConeSection;
901ac04eaf7SToby Isaac   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
902cc71bff1SMichael Lange   PetscBool              flg;
903cc71bff1SMichael Lange   PetscErrorCode         ierr;
904cc71bff1SMichael Lange 
905cc71bff1SMichael Lange   PetscFunctionBegin;
906cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9070c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
908cc71bff1SMichael Lange 
9090c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
910cc71bff1SMichael Lange   /* Distribute cone section */
911cc71bff1SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
912cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
913cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr);
914cc71bff1SMichael Lange   ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
915cc71bff1SMichael Lange   ierr = DMSetUp(dmParallel);CHKERRQ(ierr);
916cc71bff1SMichael Lange   {
917cc71bff1SMichael Lange     PetscInt pStart, pEnd, p;
918cc71bff1SMichael Lange 
919cc71bff1SMichael Lange     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
920cc71bff1SMichael Lange     for (p = pStart; p < pEnd; ++p) {
921cc71bff1SMichael Lange       PetscInt coneSize;
922cc71bff1SMichael Lange       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
923cc71bff1SMichael Lange       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
924cc71bff1SMichael Lange     }
925cc71bff1SMichael Lange   }
926cc71bff1SMichael Lange   /* Communicate and renumber cones */
927cc71bff1SMichael Lange   ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
9288a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
929cc71bff1SMichael Lange   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
930ac04eaf7SToby Isaac   if (original) {
931ac04eaf7SToby Isaac     PetscInt numCones;
932ac04eaf7SToby Isaac 
933367003a6SStefano Zampini     ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr);
934367003a6SStefano Zampini     ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr);
935ac04eaf7SToby Isaac     ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr);
936367003a6SStefano Zampini   } else {
937ac04eaf7SToby Isaac     globCones = cones;
938ac04eaf7SToby Isaac   }
939cc71bff1SMichael Lange   ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr);
940ac04eaf7SToby Isaac   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
941ac04eaf7SToby Isaac   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
942ac04eaf7SToby Isaac   if (original) {
943ac04eaf7SToby Isaac     ierr = PetscFree(globCones);CHKERRQ(ierr);
944ac04eaf7SToby Isaac   }
945cc71bff1SMichael Lange   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
946cc71bff1SMichael Lange   ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
94762795ce3SStefano Zampini #if defined(PETSC_USE_DEBUG)
9483533c52bSMatthew G. Knepley   {
9493533c52bSMatthew G. Knepley     PetscInt  p;
9503533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
9513533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
952367003a6SStefano Zampini       if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] Point %D not in overlap SF\n", PetscGlobalRank,p);CHKERRQ(ierr);}
9533533c52bSMatthew G. Knepley     }
9543533c52bSMatthew G. Knepley     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
9553533c52bSMatthew G. Knepley   }
9563533c52bSMatthew G. Knepley #endif
957c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
958cc71bff1SMichael Lange   if (flg) {
959cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
960cc71bff1SMichael Lange     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
961cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
962cc71bff1SMichael Lange     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
963cc71bff1SMichael Lange     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
964cc71bff1SMichael Lange   }
965cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
966cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr);
967cc71bff1SMichael Lange   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
968cc71bff1SMichael Lange   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
969cc71bff1SMichael Lange   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
970cc71bff1SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
971eaf898f9SPatrick Sanan   /* Create supports and stratify DMPlex */
972cc71bff1SMichael Lange   {
973cc71bff1SMichael Lange     PetscInt pStart, pEnd;
974cc71bff1SMichael Lange 
975cc71bff1SMichael Lange     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
976cc71bff1SMichael Lange     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
977cc71bff1SMichael Lange   }
978cc71bff1SMichael Lange   ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr);
979cc71bff1SMichael Lange   ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr);
9801cf84007SMatthew G. Knepley   {
9811cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
9821cf84007SMatthew G. Knepley 
983b0441da4SMatthew G. Knepley     ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
984b0441da4SMatthew G. Knepley     ierr = DMSetBasicAdjacency(dmParallel, useCone, useClosure);CHKERRQ(ierr);
9851cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
9861cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
9871cf84007SMatthew G. Knepley   }
988cc71bff1SMichael Lange   PetscFunctionReturn(0);
989cc71bff1SMichael Lange }
990cc71bff1SMichael Lange 
99124cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
9920df0e737SMichael Lange {
9930df0e737SMichael Lange   MPI_Comm         comm;
9940df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
9950df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
9960df0e737SMichael Lange   PetscInt         bs;
99790b157c4SStefano Zampini   PetscBool        isper;
9980df0e737SMichael Lange   const char      *name;
9990df0e737SMichael Lange   const PetscReal *maxCell, *L;
10005dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
10010df0e737SMichael Lange   PetscErrorCode   ierr;
10020df0e737SMichael Lange 
10030df0e737SMichael Lange   PetscFunctionBegin;
10040df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10050c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
10060df0e737SMichael Lange 
10070df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
10080df0e737SMichael Lange   ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
10090df0e737SMichael Lange   ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr);
10100df0e737SMichael Lange   ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
10110df0e737SMichael Lange   if (originalCoordinates) {
10128b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
10130df0e737SMichael Lange     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
10140df0e737SMichael Lange     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
10150df0e737SMichael Lange 
10160df0e737SMichael Lange     ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
10170df0e737SMichael Lange     ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr);
10180df0e737SMichael Lange     ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr);
10190df0e737SMichael Lange     ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr);
10200df0e737SMichael Lange     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
10210df0e737SMichael Lange   }
102290b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
102390b157c4SStefano Zampini   ierr = DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);CHKERRQ(ierr);
10240df0e737SMichael Lange   PetscFunctionReturn(0);
10250df0e737SMichael Lange }
10260df0e737SMichael Lange 
1027d995df53SMatthew G. Knepley /* Here we are assuming that process 0 always has everything */
102824cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
10290df0e737SMichael Lange {
1030df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex*) dm->data;
10310df0e737SMichael Lange   MPI_Comm         comm;
10327980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
10330df0e737SMichael Lange   PetscMPIInt      rank;
10347980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1035df0420ecSMatthew G. Knepley   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1036df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
10370df0e737SMichael Lange   PetscErrorCode   ierr;
10380df0e737SMichael Lange 
10390df0e737SMichael Lange   PetscFunctionBegin;
10400df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10410c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
10420c86c063SLisandro Dalcin 
10430df0e737SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
10440df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
10450df0e737SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
10460df0e737SMichael Lange 
1047df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
10487980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
10497980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1050d67d17b1SMatthew G. Knepley   if (depthLabel) {ierr = PetscObjectStateGet((PetscObject) depthLabel, &depthState);CHKERRQ(ierr);}
1051df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1052b2566f29SBarry Smith   ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
1053df0420ecSMatthew G. Knepley   if (sendDepth) {
10547980c9d4SMatthew G. Knepley     ierr = DMRemoveLabel(dmParallel, "depth", &depthLabel);CHKERRQ(ierr);
10557980c9d4SMatthew G. Knepley     ierr = DMLabelDestroy(&depthLabel);CHKERRQ(ierr);
1056df0420ecSMatthew G. Knepley   }
1057d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
1058c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr);
1059d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
10600df0e737SMichael Lange   ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1061627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1062bdd2d751SMichael Lange   for (l = numLabels-1; l >= 0; --l) {
1063bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
106483e10cb3SLisandro Dalcin     PetscBool   isDepth, lisOutput = PETSC_TRUE, isOutput;
1065d67d17b1SMatthew G. Knepley     const char *name = NULL;
10660df0e737SMichael Lange 
1067d67d17b1SMatthew G. Knepley     if (hasLabels) {
1068d67d17b1SMatthew G. Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
10690df0e737SMichael Lange       /* Skip "depth" because it is recreated */
1070d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
1071d67d17b1SMatthew G. Knepley       ierr = PetscStrcmp(name, "depth", &isDepth);CHKERRQ(ierr);
1072d67d17b1SMatthew G. Knepley     }
107383e10cb3SLisandro Dalcin     ierr = MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
107483e10cb3SLisandro Dalcin     if (isDepth && !sendDepth) continue;
1075bdd2d751SMichael Lange     ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr);
107683e10cb3SLisandro Dalcin     if (isDepth) {
10777980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
10787980c9d4SMatthew G. Knepley       PetscInt gdepth;
10797980c9d4SMatthew G. Knepley 
10807980c9d4SMatthew G. Knepley       ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
10817980c9d4SMatthew G. Knepley       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
10827980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
10837980c9d4SMatthew G. Knepley         PetscBool has;
10847980c9d4SMatthew G. Knepley 
10857980c9d4SMatthew G. Knepley         ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr);
108683e10cb3SLisandro Dalcin         if (!has) {ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);}
10877980c9d4SMatthew G. Knepley       }
10887980c9d4SMatthew G. Knepley     }
1089c58f1c22SToby Isaac     ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr);
109083e10cb3SLisandro Dalcin     /* Put the output flag in the new label */
1091d67d17b1SMatthew G. Knepley     if (hasLabels) {ierr = DMGetLabelOutput(dm, name, &lisOutput);CHKERRQ(ierr);}
109283e10cb3SLisandro Dalcin     ierr = MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
1093d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) labelNew, &name);CHKERRQ(ierr);
1094d67d17b1SMatthew G. Knepley     ierr = DMSetLabelOutput(dmParallel, name, isOutput);CHKERRQ(ierr);
10950df0e737SMichael Lange   }
10960df0e737SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
10970df0e737SMichael Lange   PetscFunctionReturn(0);
10980df0e737SMichael Lange }
10990df0e737SMichael Lange 
110024cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
11019734c634SMichael Lange {
11029734c634SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
11039734c634SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1104f0e73a3dSToby Isaac   PetscBool      *isHybrid, *isHybridParallel;
1105f0e73a3dSToby Isaac   PetscInt        dim, depth, d;
1106f0e73a3dSToby Isaac   PetscInt        pStart, pEnd, pStartP, pEndP;
11079734c634SMichael Lange   PetscErrorCode  ierr;
11089734c634SMichael Lange 
11099734c634SMichael Lange   PetscFunctionBegin;
11109734c634SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11110c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
11129734c634SMichael Lange 
11139734c634SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11149734c634SMichael Lange   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1115f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1116f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dmParallel,&pStartP,&pEndP);CHKERRQ(ierr);
1117f0e73a3dSToby Isaac   ierr = PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);CHKERRQ(ierr);
1118f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1119f0e73a3dSToby Isaac     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];
11209734c634SMichael Lange 
1121f0e73a3dSToby Isaac     if (hybridMax >= 0) {
1122f0e73a3dSToby Isaac       PetscInt sStart, sEnd, p;
11239734c634SMichael Lange 
1124f0e73a3dSToby Isaac       ierr = DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);CHKERRQ(ierr);
1125f0e73a3dSToby Isaac       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
11269734c634SMichael Lange     }
11279734c634SMichael Lange   }
1128f0e73a3dSToby Isaac   ierr = PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1129f0e73a3dSToby Isaac   ierr = PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1130f0e73a3dSToby Isaac   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1131f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1132f0e73a3dSToby Isaac     PetscInt sStart, sEnd, p, dd;
1133f0e73a3dSToby Isaac 
1134f0e73a3dSToby Isaac     ierr = DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);CHKERRQ(ierr);
1135f0e73a3dSToby Isaac     dd = (depth == 1 && d == 1) ? dim : d;
1136f0e73a3dSToby Isaac     for (p = sStart; p < sEnd; p++) {
1137f0e73a3dSToby Isaac       if (isHybridParallel[p-pStartP]) {
1138f0e73a3dSToby Isaac         pmesh->hybridPointMax[dd] = p;
1139f0e73a3dSToby Isaac         break;
1140f0e73a3dSToby Isaac       }
1141f0e73a3dSToby Isaac     }
1142f0e73a3dSToby Isaac   }
1143f0e73a3dSToby Isaac   ierr = PetscFree2(isHybrid,isHybridParallel);CHKERRQ(ierr);
11449734c634SMichael Lange   PetscFunctionReturn(0);
11459734c634SMichael Lange }
11460df0e737SMichael Lange 
114724cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1148a6f36705SMichael Lange {
114915078cd4SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
115015078cd4SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1151a6f36705SMichael Lange   MPI_Comm        comm;
1152a6f36705SMichael Lange   DM              refTree;
1153a6f36705SMichael Lange   PetscSection    origParentSection, newParentSection;
1154a6f36705SMichael Lange   PetscInt        *origParents, *origChildIDs;
1155a6f36705SMichael Lange   PetscBool       flg;
1156a6f36705SMichael Lange   PetscErrorCode  ierr;
1157a6f36705SMichael Lange 
1158a6f36705SMichael Lange   PetscFunctionBegin;
1159a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11600c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
1161a6f36705SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1162a6f36705SMichael Lange 
1163a6f36705SMichael Lange   /* Set up tree */
1164a6f36705SMichael Lange   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1165a6f36705SMichael Lange   ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr);
1166a6f36705SMichael Lange   ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr);
1167a6f36705SMichael Lange   if (origParentSection) {
1168a6f36705SMichael Lange     PetscInt        pStart, pEnd;
116908633170SToby Isaac     PetscInt        *newParents, *newChildIDs, *globParents;
1170a6f36705SMichael Lange     PetscInt        *remoteOffsetsParents, newParentSize;
1171a6f36705SMichael Lange     PetscSF         parentSF;
1172a6f36705SMichael Lange 
1173a6f36705SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
1174a6f36705SMichael Lange     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr);
1175a6f36705SMichael Lange     ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr);
1176a6f36705SMichael Lange     ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr);
1177a6f36705SMichael Lange     ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr);
11788a713f3bSLawrence Mitchell     ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr);
1179a6f36705SMichael Lange     ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr);
1180a6f36705SMichael Lange     ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr);
118108633170SToby Isaac     if (original) {
118208633170SToby Isaac       PetscInt numParents;
118308633170SToby Isaac 
118408633170SToby Isaac       ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr);
118508633170SToby Isaac       ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr);
118608633170SToby Isaac       ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr);
118708633170SToby Isaac     }
118808633170SToby Isaac     else {
118908633170SToby Isaac       globParents = origParents;
119008633170SToby Isaac     }
119108633170SToby Isaac     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
119208633170SToby Isaac     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
119308633170SToby Isaac     if (original) {
119408633170SToby Isaac       ierr = PetscFree(globParents);CHKERRQ(ierr);
119508633170SToby Isaac     }
1196a6f36705SMichael Lange     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1197a6f36705SMichael Lange     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1198a6f36705SMichael Lange     ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr);
119962795ce3SStefano Zampini #if defined(PETSC_USE_DEBUG)
12004a54e071SToby Isaac     {
12014a54e071SToby Isaac       PetscInt  p;
12024a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
12034a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
12044a54e071SToby Isaac         if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
12054a54e071SToby Isaac       }
12064a54e071SToby Isaac       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
12074a54e071SToby Isaac     }
12084a54e071SToby Isaac #endif
1209c5929fdfSBarry Smith     ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr);
1210a6f36705SMichael Lange     if (flg) {
1211a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr);
1212a6f36705SMichael Lange       ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1213a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr);
1214a6f36705SMichael Lange       ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1215a6f36705SMichael Lange       ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr);
1216a6f36705SMichael Lange     }
1217a6f36705SMichael Lange     ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr);
1218a6f36705SMichael Lange     ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr);
1219a6f36705SMichael Lange     ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr);
1220a6f36705SMichael Lange     ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr);
1221a6f36705SMichael Lange   }
122215078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1223a6f36705SMichael Lange   PetscFunctionReturn(0);
1224a6f36705SMichael Lange }
12250df0e737SMichael Lange 
122624cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
12274eca1733SMichael Lange {
12289852e123SBarry Smith   PetscMPIInt            rank, size;
12294eca1733SMichael Lange   MPI_Comm               comm;
12304eca1733SMichael Lange   PetscErrorCode         ierr;
12314eca1733SMichael Lange 
12324eca1733SMichael Lange   PetscFunctionBegin;
12334eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12340c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
12354eca1733SMichael Lange 
12364eca1733SMichael Lange   /* Create point SF for parallel mesh */
12374eca1733SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
12384eca1733SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
12394eca1733SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
12409852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
12414eca1733SMichael Lange   {
12424eca1733SMichael Lange     const PetscInt *leaves;
12434eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
12444eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
12454eca1733SMichael Lange     PetscInt        pStart, pEnd;
12464eca1733SMichael Lange 
12474eca1733SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
12484eca1733SMichael Lange     ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
12494eca1733SMichael Lange     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
12504eca1733SMichael Lange     for (p=0; p<numRoots; p++) {
12514eca1733SMichael Lange       rowners[p].rank  = -1;
12524eca1733SMichael Lange       rowners[p].index = -1;
12534eca1733SMichael Lange     }
12544eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
12554eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
12564eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
12574eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
12584eca1733SMichael Lange         lowners[p].rank  = rank;
12594eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
12604eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
12614eca1733SMichael Lange         lowners[p].rank  = -2;
12624eca1733SMichael Lange         lowners[p].index = -2;
12634eca1733SMichael Lange       }
12644eca1733SMichael Lange     }
12654eca1733SMichael Lange     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
12664eca1733SMichael Lange       rowners[p].rank  = -3;
12674eca1733SMichael Lange       rowners[p].index = -3;
12684eca1733SMichael Lange     }
12694eca1733SMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
12704eca1733SMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
12714eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
12724eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
12734eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
12744eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
12754eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
12764eca1733SMichael Lange     }
12774eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr);
12784eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
12794eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
12804eca1733SMichael Lange       if (lowners[p].rank != rank) {
12814eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
12824eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
12834eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
12844eca1733SMichael Lange         ++gp;
12854eca1733SMichael Lange       }
12864eca1733SMichael Lange     }
12874eca1733SMichael Lange     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
12884eca1733SMichael Lange     ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
12894eca1733SMichael Lange     ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr);
12904eca1733SMichael Lange   }
12911cf84007SMatthew G. Knepley   {
12921cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
12931cf84007SMatthew G. Knepley 
1294b0441da4SMatthew G. Knepley     ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
1295b0441da4SMatthew G. Knepley     ierr = DMSetBasicAdjacency(dmParallel, useCone, useClosure);CHKERRQ(ierr);
12961cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
12971cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
12981cf84007SMatthew G. Knepley   }
12994eca1733SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13004eca1733SMichael Lange   PetscFunctionReturn(0);
13014eca1733SMichael Lange }
13024eca1733SMichael Lange 
130398ba2d7fSLawrence Mitchell /*@
130498ba2d7fSLawrence Mitchell   DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition?
130598ba2d7fSLawrence Mitchell 
130698ba2d7fSLawrence Mitchell   Input Parameters:
130798ba2d7fSLawrence Mitchell + dm - The DMPlex object
130898ba2d7fSLawrence Mitchell - flg - Balance the partition?
130998ba2d7fSLawrence Mitchell 
131098ba2d7fSLawrence Mitchell   Level: intermediate
131198ba2d7fSLawrence Mitchell 
131298ba2d7fSLawrence Mitchell .seealso: DMPlexDistribute(), DMPlexGetPartitionBalance()
131398ba2d7fSLawrence Mitchell @*/
131498ba2d7fSLawrence Mitchell PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg)
131598ba2d7fSLawrence Mitchell {
131698ba2d7fSLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
131798ba2d7fSLawrence Mitchell 
131898ba2d7fSLawrence Mitchell   PetscFunctionBegin;
131998ba2d7fSLawrence Mitchell   mesh->partitionBalance = flg;
132098ba2d7fSLawrence Mitchell   PetscFunctionReturn(0);
132198ba2d7fSLawrence Mitchell }
132298ba2d7fSLawrence Mitchell 
132398ba2d7fSLawrence Mitchell /*@
132498ba2d7fSLawrence Mitchell   DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition?
132598ba2d7fSLawrence Mitchell 
132698ba2d7fSLawrence Mitchell   Input Parameter:
132798ba2d7fSLawrence Mitchell + dm - The DMPlex object
132898ba2d7fSLawrence Mitchell 
132998ba2d7fSLawrence Mitchell   Output Parameter:
133098ba2d7fSLawrence Mitchell + flg - Balance the partition?
133198ba2d7fSLawrence Mitchell 
133298ba2d7fSLawrence Mitchell   Level: intermediate
133398ba2d7fSLawrence Mitchell 
133498ba2d7fSLawrence Mitchell .seealso: DMPlexDistribute(), DMPlexSetPartitionBalance()
133598ba2d7fSLawrence Mitchell @*/
133698ba2d7fSLawrence Mitchell PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg)
133798ba2d7fSLawrence Mitchell {
133898ba2d7fSLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
133998ba2d7fSLawrence Mitchell 
134098ba2d7fSLawrence Mitchell   PetscFunctionBegin;
134198ba2d7fSLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
134298ba2d7fSLawrence Mitchell   PetscValidIntPointer(flg, 2);
134398ba2d7fSLawrence Mitchell   *flg = mesh->partitionBalance;
134498ba2d7fSLawrence Mitchell   PetscFunctionReturn(0);
134598ba2d7fSLawrence Mitchell }
134698ba2d7fSLawrence Mitchell 
1347f5bf2dbfSMichael Lange /*@C
1348f5bf2dbfSMichael Lange   DMPlexDerivePointSF - Build a point SF from an SF describing a point migration
1349f5bf2dbfSMichael Lange 
1350f5bf2dbfSMichael Lange   Input Parameter:
1351f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1352f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
13531627f6ccSMichael Lange . ownership   - Flag causing a vote to determine point ownership
1354f5bf2dbfSMichael Lange 
1355f5bf2dbfSMichael Lange   Output Parameter:
1356f5bf2dbfSMichael Lange - pointSF     - The star forest describing the point overlap in the remapped DM
1357f5bf2dbfSMichael Lange 
1358f5bf2dbfSMichael Lange   Level: developer
1359f5bf2dbfSMichael Lange 
1360f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1361f5bf2dbfSMichael Lange @*/
1362f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1363f5bf2dbfSMichael Lange {
136423193802SMatthew G. Knepley   PetscMPIInt        rank, size;
13651627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1366f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1367f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1368f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1369f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
137023193802SMatthew G. Knepley   Vec                shifts;
1371cae3e4f3SLawrence Mitchell   const PetscInt     numShifts = 13759;
137223193802SMatthew G. Knepley   const PetscScalar *shift = NULL;
137323193802SMatthew G. Knepley   const PetscBool    shiftDebug = PETSC_FALSE;
137498ba2d7fSLawrence Mitchell   PetscBool          balance;
1375f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1376f5bf2dbfSMichael Lange 
1377f5bf2dbfSMichael Lange   PetscFunctionBegin;
1378f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1379f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1380ab0bc224SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
1381f5bf2dbfSMichael Lange 
138298ba2d7fSLawrence Mitchell   ierr = DMPlexGetPartitionBalance(dm, &balance);CHKERRQ(ierr);
1383f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1384f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1385f5bf2dbfSMichael Lange   if (ownership) {
138623193802SMatthew 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. */
138798ba2d7fSLawrence Mitchell     if (balance) {
138823193802SMatthew G. Knepley       PetscRandom r;
138923193802SMatthew G. Knepley 
139023193802SMatthew G. Knepley       ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
1391cae3e4f3SLawrence Mitchell       ierr = PetscRandomSetInterval(r, 0, 2467*size);CHKERRQ(ierr);
139223193802SMatthew G. Knepley       ierr = VecCreate(PETSC_COMM_SELF, &shifts);CHKERRQ(ierr);
139323193802SMatthew G. Knepley       ierr = VecSetSizes(shifts, numShifts, numShifts);CHKERRQ(ierr);
139423193802SMatthew G. Knepley       ierr = VecSetType(shifts, VECSTANDARD);CHKERRQ(ierr);
139523193802SMatthew G. Knepley       ierr = VecSetRandom(shifts, r);CHKERRQ(ierr);
139623193802SMatthew G. Knepley       ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
139723193802SMatthew G. Knepley       ierr = VecGetArrayRead(shifts, &shift);CHKERRQ(ierr);
139823193802SMatthew G. Knepley     }
139923193802SMatthew G. Knepley 
140023193802SMatthew G. Knepley     /* Point ownership vote: Process with highest rank owns shared points */
1401f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
140223193802SMatthew G. Knepley       if (shiftDebug) {
1403e7bcd635SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d] Point %D RemotePoint %D Shift %D MyRank %D\n", rank, leaves ? leaves[p] : p, roots[p].index, (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]), (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size);CHKERRQ(ierr);
140423193802SMatthew G. Knepley       }
1405f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1406e7bcd635SMatthew G. Knepley       leafNodes[p].rank  = (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size;
140743f7d02bSMichael Lange       leafNodes[p].index = p;
1408f5bf2dbfSMichael Lange     }
1409f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
14101627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1411f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1412f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1413f5bf2dbfSMichael Lange     }
1414f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1415f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
141698ba2d7fSLawrence Mitchell     if (balance) {
1417c091126eSLawrence Mitchell       /* We've voted, now we need to get the rank.  When we're balancing the partition, the "rank" in rootNotes is not
1418c091126eSLawrence Mitchell        * the rank but rather (rank + random)%size.  So we do another reduction, voting the same way, but sending the
1419c091126eSLawrence Mitchell        * rank instead of the index. */
1420c091126eSLawrence Mitchell       PetscSFNode *rootRanks = NULL;
1421c091126eSLawrence Mitchell       ierr = PetscMalloc1(nroots, &rootRanks);CHKERRQ(ierr);
1422c091126eSLawrence Mitchell       for (p = 0; p < nroots; p++) {
1423c091126eSLawrence Mitchell         rootRanks[p].rank = -3;
1424c091126eSLawrence Mitchell         rootRanks[p].index = -3;
1425c091126eSLawrence Mitchell       }
1426c091126eSLawrence Mitchell       for (p = 0; p < nleaves; p++) leafNodes[p].index = rank;
1427c091126eSLawrence Mitchell       ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootRanks, MPI_MAXLOC);CHKERRQ(ierr);
1428c091126eSLawrence Mitchell       ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootRanks, MPI_MAXLOC);CHKERRQ(ierr);
1429c091126eSLawrence Mitchell       for (p = 0; p < nroots; p++) rootNodes[p].rank = rootRanks[p].index;
1430c091126eSLawrence Mitchell       ierr = PetscFree(rootRanks);CHKERRQ(ierr);
1431c091126eSLawrence Mitchell     }
1432f5bf2dbfSMichael Lange   } else {
1433f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1434f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1435f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1436f5bf2dbfSMichael Lange     };
1437f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1438f5bf2dbfSMichael Lange       /* Write new local id into old location */
1439f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
14406462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1441f5bf2dbfSMichael Lange       }
1442f5bf2dbfSMichael Lange     }
1443f5bf2dbfSMichael Lange   }
1444f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1445f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1446f5bf2dbfSMichael Lange 
144723193802SMatthew G. Knepley   for (npointLeaves = 0, p = 0; p < nleaves; p++) {
1448b0e1264bSMatthew G. Knepley     if (leafNodes[p].rank != rank) npointLeaves++;
144923193802SMatthew G. Knepley   }
14501627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
14511627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1452f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1453b0e1264bSMatthew G. Knepley     if (leafNodes[p].rank != rank) {
1454f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1455f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1456f5bf2dbfSMichael Lange       idx++;
1457f5bf2dbfSMichael Lange     }
1458f5bf2dbfSMichael Lange   }
145923193802SMatthew G. Knepley   if (shift) {
146023193802SMatthew G. Knepley     ierr = VecRestoreArrayRead(shifts, &shift);CHKERRQ(ierr);
146123193802SMatthew G. Knepley     ierr = VecDestroy(&shifts);CHKERRQ(ierr);
146223193802SMatthew G. Knepley   }
146323193802SMatthew G. Knepley   if (shiftDebug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), PETSC_STDOUT);CHKERRQ(ierr);}
1464f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
14651627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1466f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1467f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1468f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1469f5bf2dbfSMichael Lange }
1470f5bf2dbfSMichael Lange 
147115078cd4SMichael Lange /*@C
147215078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
147315078cd4SMichael Lange 
147483655b49SVáclav Hapla   Collective on DM and PetscSF
147583655b49SVáclav Hapla 
147615078cd4SMichael Lange   Input Parameter:
147715078cd4SMichael Lange + dm       - The source DMPlex object
14781627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
147915078cd4SMichael Lange 
148015078cd4SMichael Lange   Output Parameter:
148115078cd4SMichael Lange - targetDM - The target DMPlex object
148215078cd4SMichael Lange 
1483b9f40539SMichael Lange   Level: intermediate
148415078cd4SMichael Lange 
148515078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
148615078cd4SMichael Lange @*/
1487b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
148815078cd4SMichael Lange {
1489b9f40539SMichael Lange   MPI_Comm               comm;
1490cc1750acSStefano Zampini   PetscInt               dim, cdim, nroots;
1491b9f40539SMichael Lange   PetscSF                sfPoint;
149215078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1493ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1494b9f40539SMichael Lange   PetscBool              flg;
149515078cd4SMichael Lange   PetscErrorCode         ierr;
149615078cd4SMichael Lange 
149715078cd4SMichael Lange   PetscFunctionBegin;
149815078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14991b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1500b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
150115078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
150215078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
1503cc1750acSStefano Zampini   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1504cc1750acSStefano Zampini   ierr = DMSetCoordinateDim(targetDM, cdim);CHKERRQ(ierr);
150515078cd4SMichael Lange 
1506bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1507b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1508b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1509bfb0467fSMichael Lange   if (nroots >= 0) {
1510b9f40539SMichael Lange     IS        isOriginal;
1511ac04eaf7SToby Isaac     PetscInt  n, size, nleaves;
1512ac04eaf7SToby Isaac     PetscInt  *numbering_orig, *numbering_new;
1513367003a6SStefano Zampini 
1514b9f40539SMichael Lange     /* Get the original point numbering */
1515b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1516b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1517b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1518b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1519b9f40539SMichael Lange     /* Convert to positive global numbers */
1520b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1521b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1522b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1523b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1524b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1525b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1526b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1527b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1528b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
152915078cd4SMichael Lange   } else {
1530bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
153115078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
153215078cd4SMichael Lange   }
1533c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1534b9f40539SMichael Lange   if (flg) {
1535b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1536b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1537b9f40539SMichael Lange   }
153815078cd4SMichael Lange   /* Migrate DM data to target DM */
1539ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
154015078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
15410dc1530dSSander Arens   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
154215078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
154308633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1544ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1545302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
15461b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
154715078cd4SMichael Lange   PetscFunctionReturn(0);
154815078cd4SMichael Lange }
154915078cd4SMichael Lange 
1550825f8a23SLisandro Dalcin PETSC_INTERN PetscErrorCode DMPlexPartitionLabelClosure_Private(DM,DMLabel,PetscInt,PetscInt,const PetscInt[],IS*);
1551825f8a23SLisandro Dalcin 
15523b8f15a2SMatthew G. Knepley /*@C
155370034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
155470034214SMatthew G. Knepley 
155583655b49SVáclav Hapla   Collective on DM
155670034214SMatthew G. Knepley 
155770034214SMatthew G. Knepley   Input Parameter:
155870034214SMatthew G. Knepley + dm  - The original DMPlex object
155970034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
156070034214SMatthew G. Knepley 
156170034214SMatthew G. Knepley   Output Parameter:
1562f4ae5380SJed Brown + sf - The PetscSF used for point distribution, or NULL if not needed
1563f4ae5380SJed Brown - dmParallel - The distributed DMPlex object
156470034214SMatthew G. Knepley 
1565f4ae5380SJed Brown   Note: If the mesh was not distributed, the output dmParallel will be NULL.
156670034214SMatthew G. Knepley 
1567b0441da4SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
156870034214SMatthew G. Knepley   representation on the mesh.
156970034214SMatthew G. Knepley 
157070034214SMatthew G. Knepley   Level: intermediate
157170034214SMatthew G. Knepley 
157270034214SMatthew G. Knepley .keywords: mesh, elements
1573b0441da4SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMSetAdjacency()
157470034214SMatthew G. Knepley @*/
157580cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
157670034214SMatthew G. Knepley {
157770034214SMatthew G. Knepley   MPI_Comm               comm;
157815078cd4SMichael Lange   PetscPartitioner       partitioner;
1579f8987ae8SMichael Lange   IS                     cellPart;
1580f8987ae8SMichael Lange   PetscSection           cellPartSection;
1581cf86098cSMatthew G. Knepley   DM                     dmCoord;
1582f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
1583*874ddda9SLisandro Dalcin   PetscSF                sfMigration, sfStratified, sfPoint;
158498ba2d7fSLawrence Mitchell   PetscBool              flg, balance;
1585*874ddda9SLisandro Dalcin   PetscMPIInt            rank, size;
158670034214SMatthew G. Knepley   PetscErrorCode         ierr;
158770034214SMatthew G. Knepley 
158870034214SMatthew G. Knepley   PetscFunctionBegin;
158970034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1590d5c515a1SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, overlap, 2);
1591d5c515a1SMatthew G. Knepley   if (sf) PetscValidPointer(sf,3);
1592d5c515a1SMatthew G. Knepley   PetscValidPointer(dmParallel,4);
159370034214SMatthew G. Knepley 
15940c86c063SLisandro Dalcin   if (sf) *sf = NULL;
15950c86c063SLisandro Dalcin   *dmParallel = NULL;
159670034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
159770034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
15989852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
15990c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
160070034214SMatthew G. Knepley 
16010c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
160215078cd4SMichael Lange   /* Create cell partition */
160367eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
160477623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
160515078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
160615078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1607f8987ae8SMichael Lange   {
1608f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1609825f8a23SLisandro Dalcin     IS         is;
1610825f8a23SLisandro Dalcin     PetscHSetI ht;
1611825f8a23SLisandro Dalcin     PetscInt pStart, pEnd, proc, npoints, poff = 0, nranks, *iranks;
1612f8987ae8SMichael Lange     const PetscInt *points;
1613825f8a23SLisandro Dalcin 
1614d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition);CHKERRQ(ierr);
1615825f8a23SLisandro Dalcin     /* Preallocate strata */
1616825f8a23SLisandro Dalcin     ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
1617825f8a23SLisandro Dalcin     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1618825f8a23SLisandro Dalcin     for (proc = pStart; proc < pEnd; proc++) {
1619825f8a23SLisandro Dalcin       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1620825f8a23SLisandro Dalcin       if (npoints) {ierr = PetscHSetIAdd(ht, proc);CHKERRQ(ierr);}
1621825f8a23SLisandro Dalcin     }
1622825f8a23SLisandro Dalcin     ierr = PetscHSetIGetSize(ht, &nranks);CHKERRQ(ierr);
1623825f8a23SLisandro Dalcin     ierr = PetscMalloc1(nranks, &iranks);CHKERRQ(ierr);
1624825f8a23SLisandro Dalcin     ierr = PetscHSetIGetElems(ht, &poff, iranks);CHKERRQ(ierr);
1625825f8a23SLisandro Dalcin     ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
1626825f8a23SLisandro Dalcin     ierr = DMLabelAddStrata(lblPartition, nranks, iranks);CHKERRQ(ierr);
1627825f8a23SLisandro Dalcin     ierr = PetscFree(iranks);CHKERRQ(ierr);
1628825f8a23SLisandro Dalcin     /* Inline DMPlexPartitionLabelClosure() */
1629f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1630f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1631f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1632f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1633825f8a23SLisandro Dalcin       if (!npoints) continue;
1634825f8a23SLisandro Dalcin       ierr = PetscSectionGetOffset(cellPartSection, proc, &poff);CHKERRQ(ierr);
1635825f8a23SLisandro Dalcin       ierr = DMPlexPartitionLabelClosure_Private(dm, lblPartition, proc, npoints, points+poff, &is);CHKERRQ(ierr);
1636825f8a23SLisandro Dalcin       ierr = DMLabelSetStratumIS(lblPartition, proc, is);CHKERRQ(ierr);
1637825f8a23SLisandro Dalcin       ierr = ISDestroy(&is);CHKERRQ(ierr);
1638f8987ae8SMichael Lange     }
1639f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1640f8987ae8SMichael Lange   }
1641d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration);CHKERRQ(ierr);
1642*874ddda9SLisandro Dalcin   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration);CHKERRQ(ierr);
1643302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
164443f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
164543f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
164643f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
164743f7d02bSMichael Lange   sfMigration = sfStratified;
164867eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1649c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
165070034214SMatthew G. Knepley   if (flg) {
1651f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1652f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
165370034214SMatthew G. Knepley   }
1654f8987ae8SMichael Lange 
165515078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
165670034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
165770034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1658b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
165970034214SMatthew G. Knepley 
1660a157612eSMichael Lange   /* Build the point SF without overlap */
166198ba2d7fSLawrence Mitchell   ierr = DMPlexGetPartitionBalance(dm, &balance);CHKERRQ(ierr);
166298ba2d7fSLawrence Mitchell   ierr = DMPlexSetPartitionBalance(*dmParallel, balance);CHKERRQ(ierr);
16631627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1664f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1665cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1666cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1667f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
166870034214SMatthew G. Knepley 
1669a157612eSMichael Lange   if (overlap > 0) {
167015078cd4SMichael Lange     DM                 dmOverlap;
167115078cd4SMichael Lange     PetscInt           nroots, nleaves;
167215078cd4SMichael Lange     PetscSFNode       *newRemote;
167315078cd4SMichael Lange     const PetscSFNode *oldRemote;
167415078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1675a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1676b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1677a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1678a157612eSMichael Lange     *dmParallel = dmOverlap;
1679c389ea9fSToby Isaac     if (flg) {
16803d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
168115078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1682c389ea9fSToby Isaac     }
168343331d4aSMichael Lange 
1684f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1685f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
168615078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
168743331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
168815078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
168915078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
169015078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
169115078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
169215078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1693f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
169415078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1695c389ea9fSToby Isaac   }
1696bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1697f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1698f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
16994eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
17004eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1701721cbd76SMatthew G. Knepley   /* Copy BC */
1702a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
170366fe0bfeSMatthew G. Knepley   /* Create sfNatural */
170466fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
170566fe0bfeSMatthew G. Knepley     PetscSection section;
170666fe0bfeSMatthew G. Knepley 
1707e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
17080c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
17090e663481SBlaise Bourdin     ierr = DMSetUseNatural(*dmParallel, PETSC_TRUE);CHKERRQ(ierr);
171066fe0bfeSMatthew G. Knepley   }
1711721cbd76SMatthew G. Knepley   /* Cleanup */
1712f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
1713f8987ae8SMichael Lange   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1714f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
171570034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
171670034214SMatthew G. Knepley   PetscFunctionReturn(0);
171770034214SMatthew G. Knepley }
1718a157612eSMichael Lange 
1719a157612eSMichael Lange /*@C
172024cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1721a157612eSMichael Lange 
172283655b49SVáclav Hapla   Collective on DM
1723a157612eSMichael Lange 
1724a157612eSMichael Lange   Input Parameter:
1725a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
17260c86c063SLisandro Dalcin - overlap - The overlap of partitions
1727a157612eSMichael Lange 
1728a157612eSMichael Lange   Output Parameter:
1729a157612eSMichael Lange + sf - The PetscSF used for point distribution
1730a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1731a157612eSMichael Lange 
1732a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1733a157612eSMichael Lange 
1734b0441da4SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
1735a157612eSMichael Lange   representation on the mesh.
1736a157612eSMichael Lange 
1737a157612eSMichael Lange   Level: intermediate
1738a157612eSMichael Lange 
1739a157612eSMichael Lange .keywords: mesh, elements
1740b0441da4SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMSetAdjacency()
1741a157612eSMichael Lange @*/
1742b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1743a157612eSMichael Lange {
1744a157612eSMichael Lange   MPI_Comm               comm;
17453567eaeeSMatthew G. Knepley   PetscMPIInt            size, rank;
1746a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1747a157612eSMichael Lange   IS                     rootrank, leafrank;
1748cf86098cSMatthew G. Knepley   DM                     dmCoord;
1749a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1750f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1751a157612eSMichael Lange   PetscErrorCode         ierr;
1752a157612eSMichael Lange 
1753a157612eSMichael Lange   PetscFunctionBegin;
1754a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1755a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1756a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1757a157612eSMichael Lange 
17580c86c063SLisandro Dalcin   if (sf) *sf = NULL;
17590c86c063SLisandro Dalcin   *dmOverlap  = NULL;
1760a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
17613567eaeeSMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1762a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
17630c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
1764a157612eSMichael Lange 
17650c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1766a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
176767eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1768a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1769a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1770a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1771a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1772a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1773a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1774a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1775a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1776a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1777a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1778a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1779a9f1d5b2SMichael Lange 
178015fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
178115fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
178215fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
178315fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
178467eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1785a157612eSMichael Lange 
1786a157612eSMichael Lange   /* Build the overlapping DM */
1787a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1788a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1789a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1790f5bf2dbfSMichael Lange   /* Build the new point SF */
17911627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1792f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1793cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1794cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1795f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1796a157612eSMichael Lange   /* Cleanup overlap partition */
1797a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1798a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1799a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
18001b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1801a157612eSMichael Lange   PetscFunctionReturn(0);
1802a157612eSMichael Lange }
18036462276dSToby Isaac 
18046462276dSToby Isaac /*@C
18056462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
18066462276dSToby Isaac   root process of the original's communicator.
18076462276dSToby Isaac 
180883655b49SVáclav Hapla   Collective on DM
180983655b49SVáclav Hapla 
18106462276dSToby Isaac   Input Parameters:
18116462276dSToby Isaac . dm - the original DMPlex object
18126462276dSToby Isaac 
18136462276dSToby Isaac   Output Parameters:
1814a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional)
1815a13df41bSStefano Zampini - gatherMesh - the gathered DM object, or NULL
18166462276dSToby Isaac 
18176462276dSToby Isaac   Level: intermediate
18186462276dSToby Isaac 
18196462276dSToby Isaac .keywords: mesh
18206462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
18216462276dSToby Isaac @*/
1822a13df41bSStefano Zampini PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh)
18236462276dSToby Isaac {
18246462276dSToby Isaac   MPI_Comm       comm;
18256462276dSToby Isaac   PetscMPIInt    size;
18266462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
18276462276dSToby Isaac   PetscErrorCode ierr;
18286462276dSToby Isaac 
18296462276dSToby Isaac   PetscFunctionBegin;
18306462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18310c86c063SLisandro Dalcin   PetscValidPointer(gatherMesh,2);
18320c86c063SLisandro Dalcin   *gatherMesh = NULL;
1833a13df41bSStefano Zampini   if (sf) *sf = NULL;
18346462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18356462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18366462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18376462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
18386462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
18396462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
18406462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
18416462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
1842a13df41bSStefano Zampini   ierr = DMPlexDistribute(dm,0,sf,gatherMesh);CHKERRQ(ierr);
1843a13df41bSStefano Zampini 
18446462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
18456462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
18466462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
18476462276dSToby Isaac   PetscFunctionReturn(0);
18486462276dSToby Isaac }
18496462276dSToby Isaac 
18506462276dSToby Isaac /*@C
18516462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
18526462276dSToby Isaac 
185383655b49SVáclav Hapla   Collective on DM
185483655b49SVáclav Hapla 
18556462276dSToby Isaac   Input Parameters:
18566462276dSToby Isaac . dm - the original DMPlex object
18576462276dSToby Isaac 
18586462276dSToby Isaac   Output Parameters:
1859a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional)
1860a13df41bSStefano Zampini - redundantMesh - the redundant DM object, or NULL
18616462276dSToby Isaac 
18626462276dSToby Isaac   Level: intermediate
18636462276dSToby Isaac 
18646462276dSToby Isaac .keywords: mesh
18656462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
18666462276dSToby Isaac @*/
1867a13df41bSStefano Zampini PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh)
18686462276dSToby Isaac {
18696462276dSToby Isaac   MPI_Comm       comm;
18706462276dSToby Isaac   PetscMPIInt    size, rank;
18716462276dSToby Isaac   PetscInt       pStart, pEnd, p;
18726462276dSToby Isaac   PetscInt       numPoints = -1;
1873a13df41bSStefano Zampini   PetscSF        migrationSF, sfPoint, gatherSF;
18746462276dSToby Isaac   DM             gatherDM, dmCoord;
18756462276dSToby Isaac   PetscSFNode    *points;
18766462276dSToby Isaac   PetscErrorCode ierr;
18776462276dSToby Isaac 
18786462276dSToby Isaac   PetscFunctionBegin;
18796462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18800c86c063SLisandro Dalcin   PetscValidPointer(redundantMesh,2);
18810c86c063SLisandro Dalcin   *redundantMesh = NULL;
18826462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18836462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
188468dbc166SMatthew G. Knepley   if (size == 1) {
188568dbc166SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
188668dbc166SMatthew G. Knepley     *redundantMesh = dm;
1887a13df41bSStefano Zampini     if (sf) *sf = NULL;
188868dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
188968dbc166SMatthew G. Knepley   }
1890a13df41bSStefano Zampini   ierr = DMPlexGetGatherDM(dm,&gatherSF,&gatherDM);CHKERRQ(ierr);
18916462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
18926462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
18936462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
18946462276dSToby Isaac   numPoints = pEnd - pStart;
18956462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
18966462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
18976462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
18986462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
18996462276dSToby Isaac     points[p].index = p;
19006462276dSToby Isaac     points[p].rank  = 0;
19016462276dSToby Isaac   }
19026462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
19036462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
19046462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
19056462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
19066462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
19076462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
19086462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
19096462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
19106462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1911a13df41bSStefano Zampini   if (sf) {
1912a13df41bSStefano Zampini     PetscSF tsf;
1913a13df41bSStefano Zampini 
1914a13df41bSStefano Zampini     ierr = PetscSFCompose(gatherSF,migrationSF,&tsf);CHKERRQ(ierr);
1915a13df41bSStefano Zampini     ierr = DMPlexStratifyMigrationSF(dm, tsf, sf);CHKERRQ(ierr);
1916a13df41bSStefano Zampini     ierr = PetscSFDestroy(&tsf);CHKERRQ(ierr);
1917a13df41bSStefano Zampini   }
19186462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
1919a13df41bSStefano Zampini   ierr = PetscSFDestroy(&gatherSF);CHKERRQ(ierr);
19206462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
19216462276dSToby Isaac   PetscFunctionReturn(0);
19226462276dSToby Isaac }
1923