xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision 83e10cb34261e4fdd22a24eb37dc5332cfb226bd)
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 
193c1f0c11SLawrence Mitchell .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), 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 
443c1f0c11SLawrence Mitchell .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), 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 /*@
5870034214SMatthew G. Knepley   DMPlexSetAdjacencyUseCone - Define adjacency in the mesh using either the cone or the support first
5970034214SMatthew G. Knepley 
6070034214SMatthew G. Knepley   Input Parameters:
6170034214SMatthew G. Knepley + dm      - The DM object
6270034214SMatthew G. Knepley - useCone - Flag to use the cone first
6370034214SMatthew G. Knepley 
6470034214SMatthew G. Knepley   Level: intermediate
6570034214SMatthew G. Knepley 
6670034214SMatthew G. Knepley   Notes:
6770034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
684b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
6970034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
7070034214SMatthew G. Knepley 
7170034214SMatthew G. Knepley .seealso: DMPlexGetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexGetAdjacencyUseClosure(), DMPlexDistribute(), DMPlexPreallocateOperator()
7270034214SMatthew G. Knepley @*/
7370034214SMatthew G. Knepley PetscErrorCode DMPlexSetAdjacencyUseCone(DM dm, PetscBool useCone)
7470034214SMatthew G. Knepley {
751cf84007SMatthew G. Knepley   PetscDS        prob;
761cf84007SMatthew G. Knepley   PetscBool      useClosure;
771cf84007SMatthew G. Knepley   PetscInt       Nf;
781cf84007SMatthew G. Knepley   PetscErrorCode ierr;
7970034214SMatthew G. Knepley 
8070034214SMatthew G. Knepley   PetscFunctionBegin;
811cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
821cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
8306cc46feSMatthew G. Knepley   if (!Nf) {
8406cc46feSMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, PETSC_DEFAULT, NULL, &useClosure);CHKERRQ(ierr);
8506cc46feSMatthew G. Knepley     ierr = PetscDSSetAdjacency(prob, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
8606cc46feSMatthew G. Knepley   } else {
871cf84007SMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, 0, NULL, &useClosure);CHKERRQ(ierr);
881cf84007SMatthew G. Knepley     ierr = PetscDSSetAdjacency(prob, 0, useCone, useClosure);CHKERRQ(ierr);
8906cc46feSMatthew G. Knepley   }
9070034214SMatthew G. Knepley   PetscFunctionReturn(0);
9170034214SMatthew G. Knepley }
9270034214SMatthew G. Knepley 
9370034214SMatthew G. Knepley /*@
9470034214SMatthew G. Knepley   DMPlexGetAdjacencyUseCone - Query whether adjacency in the mesh uses the cone or the support first
9570034214SMatthew G. Knepley 
9670034214SMatthew G. Knepley   Input Parameter:
9770034214SMatthew G. Knepley . dm      - The DM object
9870034214SMatthew G. Knepley 
9970034214SMatthew G. Knepley   Output Parameter:
10070034214SMatthew G. Knepley . useCone - Flag to use the cone first
10170034214SMatthew G. Knepley 
10270034214SMatthew G. Knepley   Level: intermediate
10370034214SMatthew G. Knepley 
10470034214SMatthew G. Knepley   Notes:
10570034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1064b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
10770034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
10870034214SMatthew G. Knepley 
10970034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexGetAdjacencyUseClosure(), DMPlexDistribute(), DMPlexPreallocateOperator()
11070034214SMatthew G. Knepley @*/
11170034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseCone(DM dm, PetscBool *useCone)
11270034214SMatthew G. Knepley {
1131cf84007SMatthew G. Knepley   PetscDS        prob;
1141cf84007SMatthew G. Knepley   PetscInt       Nf;
1151cf84007SMatthew G. Knepley   PetscErrorCode ierr;
11670034214SMatthew G. Knepley 
11770034214SMatthew G. Knepley   PetscFunctionBegin;
1181cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1191cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
12006cc46feSMatthew G. Knepley   if (!Nf) {
12106cc46feSMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, PETSC_DEFAULT, useCone, NULL);CHKERRQ(ierr);
12206cc46feSMatthew G. Knepley   } else {
1231cf84007SMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, 0, useCone, NULL);CHKERRQ(ierr);
12406cc46feSMatthew G. Knepley   }
12570034214SMatthew G. Knepley   PetscFunctionReturn(0);
12670034214SMatthew G. Knepley }
12770034214SMatthew G. Knepley 
12870034214SMatthew G. Knepley /*@
12970034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure - Define adjacency in the mesh using the transitive closure
13070034214SMatthew G. Knepley 
13170034214SMatthew G. Knepley   Input Parameters:
13270034214SMatthew G. Knepley + dm      - The DM object
13370034214SMatthew G. Knepley - useClosure - Flag to use the closure
13470034214SMatthew G. Knepley 
13570034214SMatthew G. Knepley   Level: intermediate
13670034214SMatthew G. Knepley 
13770034214SMatthew G. Knepley   Notes:
13870034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1394b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
14070034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
14170034214SMatthew G. Knepley 
14270034214SMatthew G. Knepley .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
14370034214SMatthew G. Knepley @*/
14470034214SMatthew G. Knepley PetscErrorCode DMPlexSetAdjacencyUseClosure(DM dm, PetscBool useClosure)
14570034214SMatthew G. Knepley {
1461cf84007SMatthew G. Knepley   PetscDS        prob;
1471cf84007SMatthew G. Knepley   PetscBool      useCone;
1481cf84007SMatthew G. Knepley   PetscInt       Nf;
1491cf84007SMatthew G. Knepley   PetscErrorCode ierr;
15070034214SMatthew G. Knepley 
15170034214SMatthew G. Knepley   PetscFunctionBegin;
1521cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1531cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
15406cc46feSMatthew G. Knepley   if (!Nf) {
15506cc46feSMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, PETSC_DEFAULT, &useCone, NULL);CHKERRQ(ierr);
15606cc46feSMatthew G. Knepley     ierr = PetscDSSetAdjacency(prob, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
15706cc46feSMatthew G. Knepley   } else {
1581cf84007SMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, 0, &useCone, NULL);CHKERRQ(ierr);
1591cf84007SMatthew G. Knepley     ierr = PetscDSSetAdjacency(prob, 0, useCone, useClosure);CHKERRQ(ierr);
16006cc46feSMatthew G. Knepley   }
16170034214SMatthew G. Knepley   PetscFunctionReturn(0);
16270034214SMatthew G. Knepley }
16370034214SMatthew G. Knepley 
16470034214SMatthew G. Knepley /*@
16570034214SMatthew G. Knepley   DMPlexGetAdjacencyUseClosure - Query whether adjacency in the mesh uses the transitive closure
16670034214SMatthew G. Knepley 
16770034214SMatthew G. Knepley   Input Parameter:
16870034214SMatthew G. Knepley . dm      - The DM object
16970034214SMatthew G. Knepley 
17070034214SMatthew G. Knepley   Output Parameter:
17170034214SMatthew G. Knepley . useClosure - Flag to use the closure
17270034214SMatthew G. Knepley 
17370034214SMatthew G. Knepley   Level: intermediate
17470034214SMatthew G. Knepley 
17570034214SMatthew G. Knepley   Notes:
17670034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1774b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
17870034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
17970034214SMatthew G. Knepley 
18070034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
18170034214SMatthew G. Knepley @*/
18270034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseClosure(DM dm, PetscBool *useClosure)
18370034214SMatthew G. Knepley {
1841cf84007SMatthew G. Knepley   PetscDS        prob;
1851cf84007SMatthew G. Knepley   PetscInt       Nf;
1861cf84007SMatthew G. Knepley   PetscErrorCode ierr;
18770034214SMatthew G. Knepley 
18870034214SMatthew G. Knepley   PetscFunctionBegin;
1891cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1901cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
19106cc46feSMatthew G. Knepley   if (!Nf) {
19206cc46feSMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, PETSC_DEFAULT, NULL, useClosure);CHKERRQ(ierr);
19306cc46feSMatthew G. Knepley   } else {
1941cf84007SMatthew G. Knepley     ierr = PetscDSGetAdjacency(prob, 0, NULL, useClosure);CHKERRQ(ierr);
19506cc46feSMatthew G. Knepley   }
19670034214SMatthew G. Knepley   PetscFunctionReturn(0);
19770034214SMatthew G. Knepley }
19870034214SMatthew G. Knepley 
1998b0b4c70SToby Isaac /*@
200a17985deSToby Isaac   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.
2018b0b4c70SToby Isaac 
2028b0b4c70SToby Isaac   Input Parameters:
2038b0b4c70SToby Isaac + dm      - The DM object
2045b317d89SToby 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.
2058b0b4c70SToby Isaac 
2068b0b4c70SToby Isaac   Level: intermediate
2078b0b4c70SToby Isaac 
208a17985deSToby Isaac .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
2098b0b4c70SToby Isaac @*/
2105b317d89SToby Isaac PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors)
2118b0b4c70SToby Isaac {
2128b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
2138b0b4c70SToby Isaac 
2148b0b4c70SToby Isaac   PetscFunctionBegin;
2158b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2165b317d89SToby Isaac   mesh->useAnchors = useAnchors;
2178b0b4c70SToby Isaac   PetscFunctionReturn(0);
2188b0b4c70SToby Isaac }
2198b0b4c70SToby Isaac 
2208b0b4c70SToby Isaac /*@
221a17985deSToby Isaac   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.
2228b0b4c70SToby Isaac 
2238b0b4c70SToby Isaac   Input Parameter:
2248b0b4c70SToby Isaac . dm      - The DM object
2258b0b4c70SToby Isaac 
2268b0b4c70SToby Isaac   Output Parameter:
2275b317d89SToby 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.
2288b0b4c70SToby Isaac 
2298b0b4c70SToby Isaac   Level: intermediate
2308b0b4c70SToby Isaac 
231a17985deSToby Isaac .seealso: DMPlexSetAdjacencyUseAnchors(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
2328b0b4c70SToby Isaac @*/
2335b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors)
2348b0b4c70SToby Isaac {
2358b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
2368b0b4c70SToby Isaac 
2378b0b4c70SToby Isaac   PetscFunctionBegin;
2388b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2395b317d89SToby Isaac   PetscValidIntPointer(useAnchors, 2);
2405b317d89SToby Isaac   *useAnchors = mesh->useAnchors;
2418b0b4c70SToby Isaac   PetscFunctionReturn(0);
2428b0b4c70SToby Isaac }
2438b0b4c70SToby Isaac 
24470034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
24570034214SMatthew G. Knepley {
24670034214SMatthew G. Knepley   const PetscInt *cone = NULL;
24770034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
24870034214SMatthew G. Knepley   PetscErrorCode  ierr;
24970034214SMatthew G. Knepley 
25070034214SMatthew G. Knepley   PetscFunctionBeginHot;
25170034214SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
25270034214SMatthew G. Knepley   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
2534b6b44bdSMatthew G. Knepley   for (c = 0; c <= coneSize; ++c) {
2544b6b44bdSMatthew G. Knepley     const PetscInt  point   = !c ? p : cone[c-1];
25570034214SMatthew G. Knepley     const PetscInt *support = NULL;
25670034214SMatthew G. Knepley     PetscInt        supportSize, s, q;
25770034214SMatthew G. Knepley 
2584b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
2594b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
26070034214SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
261527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]),0); ++q) {
26270034214SMatthew G. Knepley         if (support[s] == adj[q]) break;
26370034214SMatthew G. Knepley       }
26470034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
26570034214SMatthew G. Knepley     }
26670034214SMatthew G. Knepley   }
26770034214SMatthew G. Knepley   *adjSize = numAdj;
26870034214SMatthew G. Knepley   PetscFunctionReturn(0);
26970034214SMatthew G. Knepley }
27070034214SMatthew G. Knepley 
27170034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
27270034214SMatthew G. Knepley {
27370034214SMatthew G. Knepley   const PetscInt *support = NULL;
27470034214SMatthew G. Knepley   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
27570034214SMatthew G. Knepley   PetscErrorCode  ierr;
27670034214SMatthew G. Knepley 
27770034214SMatthew G. Knepley   PetscFunctionBeginHot;
27870034214SMatthew G. Knepley   ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
27970034214SMatthew G. Knepley   ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
2804b6b44bdSMatthew G. Knepley   for (s = 0; s <= supportSize; ++s) {
2814b6b44bdSMatthew G. Knepley     const PetscInt  point = !s ? p : support[s-1];
28270034214SMatthew G. Knepley     const PetscInt *cone  = NULL;
28370034214SMatthew G. Knepley     PetscInt        coneSize, c, q;
28470034214SMatthew G. Knepley 
2854b6b44bdSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2864b6b44bdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
28770034214SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
288527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]),0); ++q) {
28970034214SMatthew G. Knepley         if (cone[c] == adj[q]) break;
29070034214SMatthew G. Knepley       }
29170034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
29270034214SMatthew G. Knepley     }
29370034214SMatthew G. Knepley   }
29470034214SMatthew G. Knepley   *adjSize = numAdj;
29570034214SMatthew G. Knepley   PetscFunctionReturn(0);
29670034214SMatthew G. Knepley }
29770034214SMatthew G. Knepley 
29870034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[])
29970034214SMatthew G. Knepley {
30070034214SMatthew G. Knepley   PetscInt      *star = NULL;
30170034214SMatthew G. Knepley   PetscInt       numAdj = 0, maxAdjSize = *adjSize, starSize, s;
30270034214SMatthew G. Knepley   PetscErrorCode ierr;
30370034214SMatthew G. Knepley 
30470034214SMatthew G. Knepley   PetscFunctionBeginHot;
30570034214SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
30670034214SMatthew G. Knepley   for (s = 0; s < starSize*2; s += 2) {
30770034214SMatthew G. Knepley     const PetscInt *closure = NULL;
30870034214SMatthew G. Knepley     PetscInt        closureSize, c, q;
30970034214SMatthew G. Knepley 
31070034214SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
31170034214SMatthew G. Knepley     for (c = 0; c < closureSize*2; c += 2) {
312527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]),0); ++q) {
31370034214SMatthew G. Knepley         if (closure[c] == adj[q]) break;
31470034214SMatthew G. Knepley       }
31570034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
31670034214SMatthew G. Knepley     }
31770034214SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
31870034214SMatthew G. Knepley   }
31970034214SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
32070034214SMatthew G. Knepley   *adjSize = numAdj;
32170034214SMatthew G. Knepley   PetscFunctionReturn(0);
32270034214SMatthew G. Knepley }
32370034214SMatthew G. Knepley 
3245b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[])
32570034214SMatthew G. Knepley {
32679bad331SMatthew G. Knepley   static PetscInt asiz = 0;
3278b0b4c70SToby Isaac   PetscInt maxAnchors = 1;
3288b0b4c70SToby Isaac   PetscInt aStart = -1, aEnd = -1;
3298b0b4c70SToby Isaac   PetscInt maxAdjSize;
3308b0b4c70SToby Isaac   PetscSection aSec = NULL;
3318b0b4c70SToby Isaac   IS aIS = NULL;
3328b0b4c70SToby Isaac   const PetscInt *anchors;
3333c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
33470034214SMatthew G. Knepley   PetscErrorCode  ierr;
33570034214SMatthew G. Knepley 
33670034214SMatthew G. Knepley   PetscFunctionBeginHot;
3375b317d89SToby Isaac   if (useAnchors) {
338a17985deSToby Isaac     ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
3398b0b4c70SToby Isaac     if (aSec) {
3408b0b4c70SToby Isaac       ierr = PetscSectionGetMaxDof(aSec,&maxAnchors);CHKERRQ(ierr);
34124c766afSToby Isaac       maxAnchors = PetscMax(1,maxAnchors);
3428b0b4c70SToby Isaac       ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
3438b0b4c70SToby Isaac       ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
3448b0b4c70SToby Isaac     }
3458b0b4c70SToby Isaac   }
34679bad331SMatthew G. Knepley   if (!*adj) {
34724c766afSToby Isaac     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;
34879bad331SMatthew G. Knepley 
34924c766afSToby Isaac     ierr  = DMPlexGetChart(dm, &pStart,&pEnd);CHKERRQ(ierr);
35079bad331SMatthew G. Knepley     ierr  = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
35124c766afSToby Isaac     ierr  = DMPlexGetMaxSizes(dm, &maxC, &maxS);CHKERRQ(ierr);
35224c766afSToby Isaac     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1;
35324c766afSToby Isaac     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1;
35424c766afSToby Isaac     asiz  = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries);
3558b0b4c70SToby Isaac     asiz *= maxAnchors;
35624c766afSToby Isaac     asiz  = PetscMin(asiz,pEnd-pStart);
35779bad331SMatthew G. Knepley     ierr  = PetscMalloc1(asiz,adj);CHKERRQ(ierr);
35879bad331SMatthew G. Knepley   }
35979bad331SMatthew G. Knepley   if (*adjSize < 0) *adjSize = asiz;
3608b0b4c70SToby Isaac   maxAdjSize = *adjSize;
3613c1f0c11SLawrence Mitchell   if (mesh->useradjacency) {
3623c1f0c11SLawrence Mitchell     ierr = mesh->useradjacency(dm, p, adjSize, *adj, mesh->useradjacencyctx);CHKERRQ(ierr);
3633c1f0c11SLawrence Mitchell   } else if (useTransitiveClosure) {
36479bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);CHKERRQ(ierr);
36570034214SMatthew G. Knepley   } else if (useCone) {
36679bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
36770034214SMatthew G. Knepley   } else {
36879bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
36970034214SMatthew G. Knepley   }
3705b317d89SToby Isaac   if (useAnchors && aSec) {
3718b0b4c70SToby Isaac     PetscInt origSize = *adjSize;
3728b0b4c70SToby Isaac     PetscInt numAdj = origSize;
3738b0b4c70SToby Isaac     PetscInt i = 0, j;
3748b0b4c70SToby Isaac     PetscInt *orig = *adj;
3758b0b4c70SToby Isaac 
3768b0b4c70SToby Isaac     while (i < origSize) {
3778b0b4c70SToby Isaac       PetscInt p = orig[i];
3788b0b4c70SToby Isaac       PetscInt aDof = 0;
3798b0b4c70SToby Isaac 
3808b0b4c70SToby Isaac       if (p >= aStart && p < aEnd) {
3818b0b4c70SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
3828b0b4c70SToby Isaac       }
3838b0b4c70SToby Isaac       if (aDof) {
3848b0b4c70SToby Isaac         PetscInt aOff;
3858b0b4c70SToby Isaac         PetscInt s, q;
3868b0b4c70SToby Isaac 
3878b0b4c70SToby Isaac         for (j = i + 1; j < numAdj; j++) {
3888b0b4c70SToby Isaac           orig[j - 1] = orig[j];
3898b0b4c70SToby Isaac         }
3908b0b4c70SToby Isaac         origSize--;
3918b0b4c70SToby Isaac         numAdj--;
3928b0b4c70SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
3938b0b4c70SToby Isaac         for (s = 0; s < aDof; ++s) {
394527e7439SSatish Balay           for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) {
3958b0b4c70SToby Isaac             if (anchors[aOff+s] == orig[q]) break;
3968b0b4c70SToby Isaac           }
3978b0b4c70SToby Isaac           if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
3988b0b4c70SToby Isaac         }
3998b0b4c70SToby Isaac       }
4008b0b4c70SToby Isaac       else {
4018b0b4c70SToby Isaac         i++;
4028b0b4c70SToby Isaac       }
4038b0b4c70SToby Isaac     }
4048b0b4c70SToby Isaac     *adjSize = numAdj;
4058b0b4c70SToby Isaac     ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
4068b0b4c70SToby Isaac   }
40770034214SMatthew G. Knepley   PetscFunctionReturn(0);
40870034214SMatthew G. Knepley }
40970034214SMatthew G. Knepley 
41070034214SMatthew G. Knepley /*@
41170034214SMatthew G. Knepley   DMPlexGetAdjacency - Return all points adjacent to the given point
41270034214SMatthew G. Knepley 
41370034214SMatthew G. Knepley   Input Parameters:
41470034214SMatthew G. Knepley + dm - The DM object
41570034214SMatthew G. Knepley . p  - The point
41670034214SMatthew G. Knepley . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE
41770034214SMatthew G. Knepley - adj - Either NULL so that the array is allocated, or an existing array with size adjSize
41870034214SMatthew G. Knepley 
41970034214SMatthew G. Knepley   Output Parameters:
42070034214SMatthew G. Knepley + adjSize - The number of adjacent points
42170034214SMatthew G. Knepley - adj - The adjacent points
42270034214SMatthew G. Knepley 
42370034214SMatthew G. Knepley   Level: advanced
42470034214SMatthew G. Knepley 
42570034214SMatthew G. Knepley   Notes: The user must PetscFree the adj array if it was not passed in.
42670034214SMatthew G. Knepley 
42770034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
42870034214SMatthew G. Knepley @*/
42970034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
43070034214SMatthew G. Knepley {
4311cf84007SMatthew G. Knepley   PetscBool      useCone, useClosure, useAnchors;
43270034214SMatthew G. Knepley   PetscErrorCode ierr;
43370034214SMatthew G. Knepley 
43470034214SMatthew G. Knepley   PetscFunctionBeginHot;
43570034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43670034214SMatthew G. Knepley   PetscValidPointer(adjSize,3);
43770034214SMatthew G. Knepley   PetscValidPointer(adj,4);
4381cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
4391cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
4401cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
4411cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);CHKERRQ(ierr);
44270034214SMatthew G. Knepley   PetscFunctionReturn(0);
44370034214SMatthew G. Knepley }
44408633170SToby Isaac 
445b0a623aaSMatthew G. Knepley /*@
446b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
447b0a623aaSMatthew G. Knepley 
448b0a623aaSMatthew G. Knepley   Collective on DM
449b0a623aaSMatthew G. Knepley 
450b0a623aaSMatthew G. Knepley   Input Parameters:
451b0a623aaSMatthew G. Knepley + dm      - The DM
452b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity
453b0a623aaSMatthew G. Knepley 
454b0a623aaSMatthew G. Knepley   Output Parameters:
455b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
456b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
457b0a623aaSMatthew G. Knepley 
458b0a623aaSMatthew G. Knepley   Level: developer
459b0a623aaSMatthew G. Knepley 
460b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
461b0a623aaSMatthew G. Knepley @*/
462b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
463b0a623aaSMatthew G. Knepley {
464b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
465b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
466b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
467b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
468b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
469b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
470b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
4719852e123SBarry Smith   PetscMPIInt        size, proc, rank;
472b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
473b0a623aaSMatthew G. Knepley 
474b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
475b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
477b0a623aaSMatthew G. Knepley   if (processRanks) {PetscValidPointer(processRanks, 3);}
478b0a623aaSMatthew G. Knepley   if (sfProcess)    {PetscValidPointer(sfProcess, 4);}
4799852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
480b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
481b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr);
4829852e123SBarry Smith   ierr = PetscBTCreate(size, &neighbors);CHKERRQ(ierr);
4839852e123SBarry Smith   ierr = PetscBTMemzero(size, neighbors);CHKERRQ(ierr);
484b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
485b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr);
486b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr);
487b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
488b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
489b0a623aaSMatthew G. Knepley 
490b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr);
491b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr);
492302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
493b0a623aaSMatthew G. Knepley   }
494b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr);
495b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
496b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr);
497b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr);
498b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
499b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
500b0a623aaSMatthew G. Knepley 
501b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr);
502b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr);
503302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
504b0a623aaSMatthew G. Knepley   }
505b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr);
506b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
507b0a623aaSMatthew G. Knepley   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
508b0a623aaSMatthew G. Knepley   /* Calculate edges */
509b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
5109852e123SBarry Smith   for(proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
511b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr);
512b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr);
513b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr);
5149852e123SBarry Smith   for(proc = 0, n = 0; proc < size; ++proc) {
515b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
516b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
517b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
518b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
519b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
520b0a623aaSMatthew G. Knepley       ++n;
521b0a623aaSMatthew G. Knepley     }
522b0a623aaSMatthew G. Knepley   }
523b0a623aaSMatthew G. Knepley   ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr);
524b0a623aaSMatthew G. Knepley   if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);}
525b0a623aaSMatthew G. Knepley   else              {ierr = PetscFree(ranksNew);CHKERRQ(ierr);}
526b0a623aaSMatthew G. Knepley   if (sfProcess) {
527b0a623aaSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr);
528b0a623aaSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr);
529b0a623aaSMatthew G. Knepley     ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr);
5309852e123SBarry Smith     ierr = PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
531b0a623aaSMatthew G. Knepley   }
532b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
533b0a623aaSMatthew G. Knepley }
534b0a623aaSMatthew G. Knepley 
535b0a623aaSMatthew G. Knepley /*@
536b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
537b0a623aaSMatthew G. Knepley 
538b0a623aaSMatthew G. Knepley   Collective on DM
539b0a623aaSMatthew G. Knepley 
540b0a623aaSMatthew G. Knepley   Input Parameter:
541b0a623aaSMatthew G. Knepley . dm - The DM
542b0a623aaSMatthew G. Knepley 
543b0a623aaSMatthew G. Knepley   Output Parameters:
544b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
545b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
546b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
547b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
548b0a623aaSMatthew G. Knepley 
549b0a623aaSMatthew G. Knepley   Level: developer
550b0a623aaSMatthew G. Knepley 
551b0a623aaSMatthew G. Knepley .seealso: DMPlexCreateOverlap()
552b0a623aaSMatthew G. Knepley @*/
553b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
554b0a623aaSMatthew G. Knepley {
555b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
556b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
557b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
558b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
559b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
560b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
561b0a623aaSMatthew G. Knepley   PetscErrorCode  ierr;
562b0a623aaSMatthew G. Knepley 
563b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
564b0a623aaSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
565b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
566b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
567b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
568b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
569b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr);
570b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr);
571b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr);
572b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr);
573b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);}
574b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
575b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
576b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr);
577b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr);
578b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(nedges,  &remoterank);CHKERRQ(ierr);
579b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
580b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
581b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
582b0a623aaSMatthew G. Knepley   ierr = PetscFree(myrank);CHKERRQ(ierr);
583b0a623aaSMatthew G. Knepley   ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr);
584b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
585b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr);
586b0a623aaSMatthew G. Knepley   ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr);
587b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
588b0a623aaSMatthew G. Knepley }
589b0a623aaSMatthew G. Knepley 
590278397a0SMatthew G. Knepley /*@C
591b0a623aaSMatthew G. Knepley   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.
592b0a623aaSMatthew G. Knepley 
593b0a623aaSMatthew G. Knepley   Collective on DM
594b0a623aaSMatthew G. Knepley 
595b0a623aaSMatthew G. Knepley   Input Parameters:
596b0a623aaSMatthew G. Knepley + dm          - The DM
59724d039d7SMichael Lange . levels      - Number of overlap levels
598b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
599b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
600b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
601b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
602b0a623aaSMatthew G. Knepley 
603b0a623aaSMatthew G. Knepley   Output Parameters:
604a9f1d5b2SMichael Lange + ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
605b0a623aaSMatthew G. Knepley 
606b0a623aaSMatthew G. Knepley   Level: developer
607b0a623aaSMatthew G. Knepley 
6081fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
609b0a623aaSMatthew G. Knepley @*/
610a9f1d5b2SMichael Lange PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
611b0a623aaSMatthew G. Knepley {
612e540f424SMichael Lange   MPI_Comm           comm;
613b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
614b0a623aaSMatthew G. Knepley   PetscSF            sfPoint, sfProc;
615b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
616b0a623aaSMatthew G. Knepley   const PetscInt    *local;
6171fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
618b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
6191fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
6209852e123SBarry Smith   PetscMPIInt        rank, size;
62126a7d390SMatthew G. Knepley   PetscBool          useCone, useClosure, flg;
622b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
623b0a623aaSMatthew G. Knepley 
624b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
625e540f424SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
6269852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
627e540f424SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
628b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
629b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
630b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr);
631b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
632b0a623aaSMatthew G. Knepley   ierr = DMLabelCreate("Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr);
633b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
634b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
635b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
636b0a623aaSMatthew G. Knepley 
637b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, local[l], &adjSize, &adj);CHKERRQ(ierr);
638b0a623aaSMatthew G. Knepley     for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);}
639b0a623aaSMatthew G. Knepley   }
640b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr);
641b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr);
642b0a623aaSMatthew G. Knepley   /* Handle roots */
643b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
644b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
645b0a623aaSMatthew G. Knepley 
646b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
647b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
648b0a623aaSMatthew G. Knepley       ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr);
649b0a623aaSMatthew G. Knepley       if (neighbors) {
650b0a623aaSMatthew G. Knepley         ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr);
651b0a623aaSMatthew G. Knepley         ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
652b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
653b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff+n];
654b0a623aaSMatthew G. Knepley 
655b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
656b0a623aaSMatthew G. Knepley           for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
657b0a623aaSMatthew G. Knepley         }
658b0a623aaSMatthew G. Knepley       }
659b0a623aaSMatthew G. Knepley     }
660b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
661b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr);
662b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
663b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr);
664b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
665b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
666b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff+n];
667b0a623aaSMatthew G. Knepley 
668b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
669b0a623aaSMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
670b0a623aaSMatthew G. Knepley     }
671b0a623aaSMatthew G. Knepley   }
672b0a623aaSMatthew G. Knepley   ierr = PetscFree(adj);CHKERRQ(ierr);
673b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr);
674b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr);
67524d039d7SMichael Lange   /* Add additional overlap levels */
676be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
677be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
678be200f8dSMichael Lange     ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr);
679be200f8dSMichael Lange     /* Add next level of point donations to the label */
680be200f8dSMichael Lange     ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr);
681be200f8dSMichael Lange   }
68226a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
68326a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
68426a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
68526a7d390SMatthew G. Knepley   if (useCone || !useClosure) {
6865abbe4feSMichael Lange     ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr);
68726a7d390SMatthew G. Knepley   }
688c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr);
689e540f424SMichael Lange   if (flg) {
690b0a623aaSMatthew G. Knepley     ierr = DMLabelView(ovAdjByRank, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
691b0a623aaSMatthew G. Knepley   }
69271a8c5fcSMichael Lange   /* Make global process SF and invert sender to receiver label */
69371a8c5fcSMichael Lange   {
69471a8c5fcSMichael Lange     /* Build a global process SF */
69571a8c5fcSMichael Lange     PetscSFNode *remoteProc;
6969852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
6979852e123SBarry Smith     for (p = 0; p < size; ++p) {
69871a8c5fcSMichael Lange       remoteProc[p].rank  = p;
69971a8c5fcSMichael Lange       remoteProc[p].index = rank;
70071a8c5fcSMichael Lange     }
70171a8c5fcSMichael Lange     ierr = PetscSFCreate(comm, &sfProc);CHKERRQ(ierr);
70271a8c5fcSMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProc, "Process SF");CHKERRQ(ierr);
7039852e123SBarry Smith     ierr = PetscSFSetGraph(sfProc, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
70471a8c5fcSMichael Lange   }
705a9f1d5b2SMichael Lange   ierr = DMLabelCreate("Overlap label", ovLabel);CHKERRQ(ierr);
706a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, sfProc, *ovLabel);CHKERRQ(ierr);
707a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
708a9f1d5b2SMichael Lange   for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);}
709e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
710a9f1d5b2SMichael Lange     ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr);
711a9f1d5b2SMichael Lange     ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr);
712e540f424SMichael Lange   }
713e540f424SMichael Lange   /* Clean up */
7141fd9873aSMichael Lange   ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr);
715b0a623aaSMatthew G. Knepley   ierr = PetscSFDestroy(&sfProc);CHKERRQ(ierr);
716b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
717b0a623aaSMatthew G. Knepley }
71870034214SMatthew G. Knepley 
71924cc2ca5SMatthew G. Knepley /*@C
72024cc2ca5SMatthew G. Knepley   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF
72124cc2ca5SMatthew G. Knepley 
72224cc2ca5SMatthew G. Knepley   Collective on DM
72324cc2ca5SMatthew G. Knepley 
72424cc2ca5SMatthew G. Knepley   Input Parameters:
72524cc2ca5SMatthew G. Knepley + dm          - The DM
72624cc2ca5SMatthew G. Knepley - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes
72724cc2ca5SMatthew G. Knepley 
72824cc2ca5SMatthew G. Knepley   Output Parameters:
72924cc2ca5SMatthew G. Knepley + migrationSF - An SF that maps original points in old locations to points in new locations
73024cc2ca5SMatthew G. Knepley 
73124cc2ca5SMatthew G. Knepley   Level: developer
73224cc2ca5SMatthew G. Knepley 
73324cc2ca5SMatthew G. Knepley .seealso: DMPlexCreateOverlap(), DMPlexDistribute()
73424cc2ca5SMatthew G. Knepley @*/
73546f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
73646f9b1c3SMichael Lange {
73746f9b1c3SMichael Lange   MPI_Comm           comm;
7389852e123SBarry Smith   PetscMPIInt        rank, size;
73946f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
74046f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
74146f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
74246f9b1c3SMichael Lange   PetscSFNode       *iremote;
74346f9b1c3SMichael Lange   PetscSF            pointSF;
74446f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
74546f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
74646f9b1c3SMichael Lange   PetscErrorCode     ierr;
74746f9b1c3SMichael Lange 
74846f9b1c3SMichael Lange   PetscFunctionBegin;
74946f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75046f9b1c3SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
75146f9b1c3SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
7529852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
75346f9b1c3SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
75446f9b1c3SMichael Lange 
75546f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
75646f9b1c3SMichael Lange   ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr);
75746f9b1c3SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
75846f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
75946f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
76046f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
76146f9b1c3SMichael Lange   }
76246f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
76346f9b1c3SMichael Lange   ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
76446f9b1c3SMichael Lange   ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
76546f9b1c3SMichael Lange 
76646f9b1c3SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
76746f9b1c3SMichael Lange   ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr);
76846f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) depthRecv[d]=0;
76946f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
77046f9b1c3SMichael Lange   depthShift[dim] = 0;
77146f9b1c3SMichael Lange   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
77246f9b1c3SMichael Lange   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
77346f9b1c3SMichael Lange   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
77446f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
77546f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
77646f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
77746f9b1c3SMichael Lange   }
77846f9b1c3SMichael Lange 
77946f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
78046f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
78146f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
78209b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr);
78309b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr);
78446f9b1c3SMichael Lange   /* First map local points to themselves */
78546f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
78646f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
78746f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) {
78846f9b1c3SMichael Lange       point = p + depthShift[d];
78946f9b1c3SMichael Lange       ilocal[point] = point;
79046f9b1c3SMichael Lange       iremote[point].index = p;
79146f9b1c3SMichael Lange       iremote[point].rank = rank;
79246f9b1c3SMichael Lange       depthIdx[d]++;
79346f9b1c3SMichael Lange     }
79446f9b1c3SMichael Lange   }
79546f9b1c3SMichael Lange 
79646f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
79746f9b1c3SMichael Lange   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
79846f9b1c3SMichael Lange   ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr);
79946f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
80046f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
80146f9b1c3SMichael Lange     for (p=0; p<numSharedPoints; p++) {
80246f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
80346f9b1c3SMichael Lange         point = sharedLocal[p] + depthShift[d];
80446f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
80546f9b1c3SMichael Lange         iremote[point].rank = sharedRemote[p].rank;
80646f9b1c3SMichael Lange       }
80746f9b1c3SMichael Lange     }
80846f9b1c3SMichael Lange   }
80946f9b1c3SMichael Lange 
81046f9b1c3SMichael Lange   /* Now add the incoming overlap points */
81146f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) {
81246f9b1c3SMichael Lange     point = depthIdx[remoteDepths[p]];
81346f9b1c3SMichael Lange     ilocal[point] = point;
81446f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
81546f9b1c3SMichael Lange     iremote[point].rank = overlapRemote[p].rank;
81646f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
81746f9b1c3SMichael Lange   }
81815fff7beSMatthew G. Knepley   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
81946f9b1c3SMichael Lange 
82046f9b1c3SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
82146f9b1c3SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr);
82246f9b1c3SMichael Lange   ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr);
82346f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
82446f9b1c3SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
82546f9b1c3SMichael Lange 
82646f9b1c3SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
82770034214SMatthew G. Knepley   PetscFunctionReturn(0);
82870034214SMatthew G. Knepley }
82970034214SMatthew G. Knepley 
830a9f1d5b2SMichael Lange /*@
831f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
832a9f1d5b2SMichael Lange 
833a9f1d5b2SMichael Lange   Input Parameter:
834a9f1d5b2SMichael Lange + dm          - The DM
835a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
836a9f1d5b2SMichael Lange 
837a9f1d5b2SMichael Lange   Output Parameter:
838a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
839a9f1d5b2SMichael Lange 
840a9f1d5b2SMichael Lange   Level: developer
841a9f1d5b2SMichael Lange 
842a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
843a9f1d5b2SMichael Lange @*/
844a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
845a9f1d5b2SMichael Lange {
846a9f1d5b2SMichael Lange   MPI_Comm           comm;
8479852e123SBarry Smith   PetscMPIInt        rank, size;
8487fab53ddSMatthew G. Knepley   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
849a9f1d5b2SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
850a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
851f0e73a3dSToby Isaac   PetscInt           hybEnd[4];
852a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
853a9f1d5b2SMichael Lange   PetscErrorCode     ierr;
854a9f1d5b2SMichael Lange 
855a9f1d5b2SMichael Lange   PetscFunctionBegin;
856a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
857a9f1d5b2SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
858a9f1d5b2SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
8599852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
8607fab53ddSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr);
861b2566f29SBarry Smith   ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
8627fab53ddSMatthew G. Knepley   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
863a9f1d5b2SMichael Lange 
864a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
865a9f1d5b2SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr);
866a9f1d5b2SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
867f0e73a3dSToby Isaac   ierr = DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[depth-1],&hybEnd[1],&hybEnd[0]);CHKERRQ(ierr);
8687fab53ddSMatthew G. Knepley   for (d = 0; d < depth+1; ++d) {
869a9f1d5b2SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
870f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
871f0e73a3dSToby Isaac       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
872f0e73a3dSToby Isaac         pointDepths[p] = 2 * d;
873f0e73a3dSToby Isaac       } else {
874f0e73a3dSToby Isaac         pointDepths[p] = 2 * d + 1;
875f0e73a3dSToby Isaac       }
876f0e73a3dSToby Isaac     }
877a9f1d5b2SMichael Lange   }
8787fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
879a9f1d5b2SMichael Lange   ierr = PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
880a9f1d5b2SMichael Lange   ierr = PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
881a9f1d5b2SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
882f0e73a3dSToby Isaac   ierr = PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);CHKERRQ(ierr);
883f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
8847fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
885f0e73a3dSToby Isaac   depthShift[2*depth+1] = 0;
886f0e73a3dSToby Isaac   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
887f0e73a3dSToby Isaac   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
888f0e73a3dSToby Isaac   depthShift[0] += depthRecv[1];
889f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
890f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
891f0e73a3dSToby Isaac   for (d = 2 * depth-1; d > 2; --d) {
892f0e73a3dSToby Isaac     PetscInt e;
893f0e73a3dSToby Isaac 
894f0e73a3dSToby Isaac     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
895f0e73a3dSToby Isaac   }
896f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
897a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
898a9f1d5b2SMichael Lange   ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr);
8997fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
9007fab53ddSMatthew G. Knepley     const PetscInt dep = remoteDepths[p];
9017fab53ddSMatthew G. Knepley 
9027fab53ddSMatthew G. Knepley     ilocal[p] = depthShift[dep] + depthIdx[dep];
9037fab53ddSMatthew G. Knepley     depthIdx[dep]++;
904a9f1d5b2SMichael Lange   }
905a9f1d5b2SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
906a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr);
907a9f1d5b2SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr);
908a9f1d5b2SMichael Lange   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
909a9f1d5b2SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
910a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
911a9f1d5b2SMichael Lange }
912a9f1d5b2SMichael Lange 
91370034214SMatthew G. Knepley /*@
91470034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
91570034214SMatthew G. Knepley 
91670034214SMatthew G. Knepley   Collective on DM
91770034214SMatthew G. Knepley 
91870034214SMatthew G. Knepley   Input Parameters:
91970034214SMatthew G. Knepley + dm - The DMPlex object
92070034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
92170034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
92270034214SMatthew G. Knepley - originalVec - The existing data
92370034214SMatthew G. Knepley 
92470034214SMatthew G. Knepley   Output Parameters:
92570034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
92670034214SMatthew G. Knepley - newVec - The new data
92770034214SMatthew G. Knepley 
92870034214SMatthew G. Knepley   Level: developer
92970034214SMatthew G. Knepley 
93070034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
93170034214SMatthew G. Knepley @*/
93270034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
93370034214SMatthew G. Knepley {
93470034214SMatthew G. Knepley   PetscSF        fieldSF;
93570034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
93670034214SMatthew G. Knepley   PetscScalar   *originalValues, *newValues;
93770034214SMatthew G. Knepley   PetscErrorCode ierr;
93870034214SMatthew G. Knepley 
93970034214SMatthew G. Knepley   PetscFunctionBegin;
94070034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
94170034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
94270034214SMatthew G. Knepley 
94370034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
94470034214SMatthew G. Knepley   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
94570034214SMatthew G. Knepley   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
94670034214SMatthew G. Knepley 
94770034214SMatthew G. Knepley   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
94870034214SMatthew G. Knepley   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
94970034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9508a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
95170034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
95270034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
95370034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
95470034214SMatthew G. Knepley   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
95570034214SMatthew G. Knepley   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
95670034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
95770034214SMatthew G. Knepley   PetscFunctionReturn(0);
95870034214SMatthew G. Knepley }
95970034214SMatthew G. Knepley 
96070034214SMatthew G. Knepley /*@
96170034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
96270034214SMatthew G. Knepley 
96370034214SMatthew G. Knepley   Collective on DM
96470034214SMatthew G. Knepley 
96570034214SMatthew G. Knepley   Input Parameters:
96670034214SMatthew G. Knepley + dm - The DMPlex object
96770034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
96870034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
96970034214SMatthew G. Knepley - originalIS - The existing data
97070034214SMatthew G. Knepley 
97170034214SMatthew G. Knepley   Output Parameters:
97270034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
97370034214SMatthew G. Knepley - newIS - The new data
97470034214SMatthew G. Knepley 
97570034214SMatthew G. Knepley   Level: developer
97670034214SMatthew G. Knepley 
97770034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
97870034214SMatthew G. Knepley @*/
97970034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
98070034214SMatthew G. Knepley {
98170034214SMatthew G. Knepley   PetscSF         fieldSF;
98270034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
98370034214SMatthew G. Knepley   const PetscInt *originalValues;
98470034214SMatthew G. Knepley   PetscErrorCode  ierr;
98570034214SMatthew G. Knepley 
98670034214SMatthew G. Knepley   PetscFunctionBegin;
98770034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
98870034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
98970034214SMatthew G. Knepley 
99070034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
991854ce69bSBarry Smith   ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr);
99270034214SMatthew G. Knepley 
99370034214SMatthew G. Knepley   ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr);
99470034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9958a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
996aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
997aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
99870034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
99970034214SMatthew G. Knepley   ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr);
100070034214SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr);
100170034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
100270034214SMatthew G. Knepley   PetscFunctionReturn(0);
100370034214SMatthew G. Knepley }
100470034214SMatthew G. Knepley 
100570034214SMatthew G. Knepley /*@
100670034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
100770034214SMatthew G. Knepley 
100870034214SMatthew G. Knepley   Collective on DM
100970034214SMatthew G. Knepley 
101070034214SMatthew G. Knepley   Input Parameters:
101170034214SMatthew G. Knepley + dm - The DMPlex object
101270034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
101370034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
101470034214SMatthew G. Knepley . datatype - The type of data
101570034214SMatthew G. Knepley - originalData - The existing data
101670034214SMatthew G. Knepley 
101770034214SMatthew G. Knepley   Output Parameters:
101870034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
101970034214SMatthew G. Knepley - newData - The new data
102070034214SMatthew G. Knepley 
102170034214SMatthew G. Knepley   Level: developer
102270034214SMatthew G. Knepley 
102370034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
102470034214SMatthew G. Knepley @*/
102570034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
102670034214SMatthew G. Knepley {
102770034214SMatthew G. Knepley   PetscSF        fieldSF;
102870034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
102970034214SMatthew G. Knepley   PetscMPIInt    dataSize;
103070034214SMatthew G. Knepley   PetscErrorCode ierr;
103170034214SMatthew G. Knepley 
103270034214SMatthew G. Knepley   PetscFunctionBegin;
103370034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
103470034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
103570034214SMatthew G. Knepley 
103670034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
103770034214SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
103870034214SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
103970034214SMatthew G. Knepley 
104070034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
10418a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
104270034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
104370034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
104470034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
104570034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
104670034214SMatthew G. Knepley   PetscFunctionReturn(0);
104770034214SMatthew G. Knepley }
104870034214SMatthew G. Knepley 
104924cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1050cc71bff1SMichael Lange {
1051cc71bff1SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
1052cc71bff1SMichael Lange   MPI_Comm               comm;
1053cc71bff1SMichael Lange   PetscSF                coneSF;
1054cc71bff1SMichael Lange   PetscSection           originalConeSection, newConeSection;
1055ac04eaf7SToby Isaac   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
1056cc71bff1SMichael Lange   PetscBool              flg;
1057cc71bff1SMichael Lange   PetscErrorCode         ierr;
1058cc71bff1SMichael Lange 
1059cc71bff1SMichael Lange   PetscFunctionBegin;
1060cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1061cc71bff1SMichael Lange   PetscValidPointer(dmParallel,4);
1062cc71bff1SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1063cc71bff1SMichael Lange 
1064cc71bff1SMichael Lange   /* Distribute cone section */
1065cc71bff1SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1066cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
1067cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr);
1068cc71bff1SMichael Lange   ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
1069cc71bff1SMichael Lange   ierr = DMSetUp(dmParallel);CHKERRQ(ierr);
1070cc71bff1SMichael Lange   {
1071cc71bff1SMichael Lange     PetscInt pStart, pEnd, p;
1072cc71bff1SMichael Lange 
1073cc71bff1SMichael Lange     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
1074cc71bff1SMichael Lange     for (p = pStart; p < pEnd; ++p) {
1075cc71bff1SMichael Lange       PetscInt coneSize;
1076cc71bff1SMichael Lange       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
1077cc71bff1SMichael Lange       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
1078cc71bff1SMichael Lange     }
1079cc71bff1SMichael Lange   }
1080cc71bff1SMichael Lange   /* Communicate and renumber cones */
1081cc71bff1SMichael Lange   ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
10828a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1083cc71bff1SMichael Lange   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
1084ac04eaf7SToby Isaac   if (original) {
1085ac04eaf7SToby Isaac     PetscInt numCones;
1086ac04eaf7SToby Isaac 
1087ac04eaf7SToby Isaac     ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr); ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr);
1088ac04eaf7SToby Isaac     ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr);
1089ac04eaf7SToby Isaac   }
1090ac04eaf7SToby Isaac   else {
1091ac04eaf7SToby Isaac     globCones = cones;
1092ac04eaf7SToby Isaac   }
1093cc71bff1SMichael Lange   ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr);
1094ac04eaf7SToby Isaac   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1095ac04eaf7SToby Isaac   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1096ac04eaf7SToby Isaac   if (original) {
1097ac04eaf7SToby Isaac     ierr = PetscFree(globCones);CHKERRQ(ierr);
1098ac04eaf7SToby Isaac   }
1099cc71bff1SMichael Lange   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
1100cc71bff1SMichael Lange   ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
11013533c52bSMatthew G. Knepley #if PETSC_USE_DEBUG
11023533c52bSMatthew G. Knepley   {
11033533c52bSMatthew G. Knepley     PetscInt  p;
11043533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
11053533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
11063533c52bSMatthew G. Knepley       if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
11073533c52bSMatthew G. Knepley     }
11083533c52bSMatthew G. Knepley     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
11093533c52bSMatthew G. Knepley   }
11103533c52bSMatthew G. Knepley #endif
1111c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
1112cc71bff1SMichael Lange   if (flg) {
1113cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
1114cc71bff1SMichael Lange     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1115cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
1116cc71bff1SMichael Lange     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1117cc71bff1SMichael Lange     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
1118cc71bff1SMichael Lange   }
1119cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
1120cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr);
1121cc71bff1SMichael Lange   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1122cc71bff1SMichael Lange   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1123cc71bff1SMichael Lange   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
1124cc71bff1SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1125eaf898f9SPatrick Sanan   /* Create supports and stratify DMPlex */
1126cc71bff1SMichael Lange   {
1127cc71bff1SMichael Lange     PetscInt pStart, pEnd;
1128cc71bff1SMichael Lange 
1129cc71bff1SMichael Lange     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1130cc71bff1SMichael Lange     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1131cc71bff1SMichael Lange   }
1132cc71bff1SMichael Lange   ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr);
1133cc71bff1SMichael Lange   ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr);
11341cf84007SMatthew G. Knepley   {
11351cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
11361cf84007SMatthew G. Knepley 
11371cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
11381cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
11391cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
11401cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
11411cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
11421cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
11431cf84007SMatthew G. Knepley   }
1144cc71bff1SMichael Lange   PetscFunctionReturn(0);
1145cc71bff1SMichael Lange }
1146cc71bff1SMichael Lange 
114724cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
11480df0e737SMichael Lange {
11490df0e737SMichael Lange   MPI_Comm         comm;
11500df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
11510df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
11520df0e737SMichael Lange   PetscInt         bs;
115390b157c4SStefano Zampini   PetscBool        isper;
11540df0e737SMichael Lange   const char      *name;
11550df0e737SMichael Lange   const PetscReal *maxCell, *L;
11565dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
11570df0e737SMichael Lange   PetscErrorCode   ierr;
11580df0e737SMichael Lange 
11590df0e737SMichael Lange   PetscFunctionBegin;
11600df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11610df0e737SMichael Lange   PetscValidPointer(dmParallel, 3);
11620df0e737SMichael Lange 
11630df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
11640df0e737SMichael Lange   ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
11650df0e737SMichael Lange   ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr);
11660df0e737SMichael Lange   ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
11670df0e737SMichael Lange   if (originalCoordinates) {
11688b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
11690df0e737SMichael Lange     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
11700df0e737SMichael Lange     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
11710df0e737SMichael Lange 
11720df0e737SMichael Lange     ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
11730df0e737SMichael Lange     ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr);
11740df0e737SMichael Lange     ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr);
11750df0e737SMichael Lange     ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr);
11760df0e737SMichael Lange     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
11770df0e737SMichael Lange   }
117890b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
117990b157c4SStefano Zampini   ierr = DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);CHKERRQ(ierr);
11800df0e737SMichael Lange   PetscFunctionReturn(0);
11810df0e737SMichael Lange }
11820df0e737SMichael Lange 
1183d995df53SMatthew G. Knepley /* Here we are assuming that process 0 always has everything */
118424cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
11850df0e737SMichael Lange {
1186df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex*) dm->data;
11870df0e737SMichael Lange   MPI_Comm         comm;
11887980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
11890df0e737SMichael Lange   PetscMPIInt      rank;
11907980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1191df0420ecSMatthew G. Knepley   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1192df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
11930df0e737SMichael Lange   PetscErrorCode   ierr;
11940df0e737SMichael Lange 
11950df0e737SMichael Lange   PetscFunctionBegin;
11960df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11972eb1fa14SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
11980df0e737SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
11990df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
12000df0e737SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
12010df0e737SMichael Lange 
1202df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
12037980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
12047980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
12057980c9d4SMatthew G. Knepley   if (depthLabel) {ierr = DMLabelGetState(depthLabel, &depthState);CHKERRQ(ierr);}
1206df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1207b2566f29SBarry Smith   ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
1208df0420ecSMatthew G. Knepley   if (sendDepth) {
12097980c9d4SMatthew G. Knepley     ierr = DMRemoveLabel(dmParallel, "depth", &depthLabel);CHKERRQ(ierr);
12107980c9d4SMatthew G. Knepley     ierr = DMLabelDestroy(&depthLabel);CHKERRQ(ierr);
1211df0420ecSMatthew G. Knepley   }
1212d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
1213c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr);
1214d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
12150df0e737SMichael Lange   ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1216627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1217bdd2d751SMichael Lange   for (l = numLabels-1; l >= 0; --l) {
1218bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
1219*83e10cb3SLisandro Dalcin     PetscBool   isDepth, lisOutput = PETSC_TRUE, isOutput;
12200df0e737SMichael Lange 
1221*83e10cb3SLisandro Dalcin     if (hasLabels) {ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);}
12220df0e737SMichael Lange     /* Skip "depth" because it is recreated */
1223*83e10cb3SLisandro Dalcin     if (hasLabels) {ierr = PetscStrcmp(label->name, "depth", &isDepth);CHKERRQ(ierr);}
1224*83e10cb3SLisandro Dalcin     ierr = MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
1225*83e10cb3SLisandro Dalcin     if (isDepth && !sendDepth) continue;
1226bdd2d751SMichael Lange     ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr);
1227*83e10cb3SLisandro Dalcin     if (isDepth) {
12287980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
12297980c9d4SMatthew G. Knepley       PetscInt gdepth;
12307980c9d4SMatthew G. Knepley 
12317980c9d4SMatthew G. Knepley       ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
12327980c9d4SMatthew G. Knepley       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
12337980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
12347980c9d4SMatthew G. Knepley         PetscBool has;
12357980c9d4SMatthew G. Knepley 
12367980c9d4SMatthew G. Knepley         ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr);
1237*83e10cb3SLisandro Dalcin         if (!has) {ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);}
12387980c9d4SMatthew G. Knepley       }
12397980c9d4SMatthew G. Knepley     }
1240c58f1c22SToby Isaac     ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr);
1241*83e10cb3SLisandro Dalcin     /* Put the output flag in the new label */
1242*83e10cb3SLisandro Dalcin     if (hasLabels) {ierr = DMGetLabelOutput(dm, label->name, &lisOutput);CHKERRQ(ierr);}
1243*83e10cb3SLisandro Dalcin     ierr = MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
1244*83e10cb3SLisandro Dalcin     ierr = DMSetLabelOutput(dmParallel, labelNew->name, isOutput);CHKERRQ(ierr);
12450df0e737SMichael Lange   }
12460df0e737SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
12470df0e737SMichael Lange   PetscFunctionReturn(0);
12480df0e737SMichael Lange }
12490df0e737SMichael Lange 
125024cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
12519734c634SMichael Lange {
12529734c634SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
12539734c634SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1254f0e73a3dSToby Isaac   PetscBool      *isHybrid, *isHybridParallel;
1255f0e73a3dSToby Isaac   PetscInt        dim, depth, d;
1256f0e73a3dSToby Isaac   PetscInt        pStart, pEnd, pStartP, pEndP;
12579734c634SMichael Lange   PetscErrorCode  ierr;
12589734c634SMichael Lange 
12599734c634SMichael Lange   PetscFunctionBegin;
12609734c634SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12619734c634SMichael Lange   PetscValidPointer(dmParallel, 4);
12629734c634SMichael Lange 
12639734c634SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12649734c634SMichael Lange   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1265f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1266f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dmParallel,&pStartP,&pEndP);CHKERRQ(ierr);
1267f0e73a3dSToby Isaac   ierr = PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);CHKERRQ(ierr);
1268f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1269f0e73a3dSToby Isaac     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];
12709734c634SMichael Lange 
1271f0e73a3dSToby Isaac     if (hybridMax >= 0) {
1272f0e73a3dSToby Isaac       PetscInt sStart, sEnd, p;
12739734c634SMichael Lange 
1274f0e73a3dSToby Isaac       ierr = DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);CHKERRQ(ierr);
1275f0e73a3dSToby Isaac       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
12769734c634SMichael Lange     }
12779734c634SMichael Lange   }
1278f0e73a3dSToby Isaac   ierr = PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1279f0e73a3dSToby Isaac   ierr = PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1280f0e73a3dSToby Isaac   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1281f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1282f0e73a3dSToby Isaac     PetscInt sStart, sEnd, p, dd;
1283f0e73a3dSToby Isaac 
1284f0e73a3dSToby Isaac     ierr = DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);CHKERRQ(ierr);
1285f0e73a3dSToby Isaac     dd = (depth == 1 && d == 1) ? dim : d;
1286f0e73a3dSToby Isaac     for (p = sStart; p < sEnd; p++) {
1287f0e73a3dSToby Isaac       if (isHybridParallel[p-pStartP]) {
1288f0e73a3dSToby Isaac         pmesh->hybridPointMax[dd] = p;
1289f0e73a3dSToby Isaac         break;
1290f0e73a3dSToby Isaac       }
1291f0e73a3dSToby Isaac     }
1292f0e73a3dSToby Isaac   }
1293f0e73a3dSToby Isaac   ierr = PetscFree2(isHybrid,isHybridParallel);CHKERRQ(ierr);
12949734c634SMichael Lange   PetscFunctionReturn(0);
12959734c634SMichael Lange }
12960df0e737SMichael Lange 
129724cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1298a6f36705SMichael Lange {
129915078cd4SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
130015078cd4SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1301a6f36705SMichael Lange   MPI_Comm        comm;
1302a6f36705SMichael Lange   DM              refTree;
1303a6f36705SMichael Lange   PetscSection    origParentSection, newParentSection;
1304a6f36705SMichael Lange   PetscInt        *origParents, *origChildIDs;
1305a6f36705SMichael Lange   PetscBool       flg;
1306a6f36705SMichael Lange   PetscErrorCode  ierr;
1307a6f36705SMichael Lange 
1308a6f36705SMichael Lange   PetscFunctionBegin;
1309a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1310a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 4);
1311a6f36705SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1312a6f36705SMichael Lange 
1313a6f36705SMichael Lange   /* Set up tree */
1314a6f36705SMichael Lange   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1315a6f36705SMichael Lange   ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr);
1316a6f36705SMichael Lange   ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr);
1317a6f36705SMichael Lange   if (origParentSection) {
1318a6f36705SMichael Lange     PetscInt        pStart, pEnd;
131908633170SToby Isaac     PetscInt        *newParents, *newChildIDs, *globParents;
1320a6f36705SMichael Lange     PetscInt        *remoteOffsetsParents, newParentSize;
1321a6f36705SMichael Lange     PetscSF         parentSF;
1322a6f36705SMichael Lange 
1323a6f36705SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
1324a6f36705SMichael Lange     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr);
1325a6f36705SMichael Lange     ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr);
1326a6f36705SMichael Lange     ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr);
1327a6f36705SMichael Lange     ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr);
13288a713f3bSLawrence Mitchell     ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr);
1329a6f36705SMichael Lange     ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr);
1330a6f36705SMichael Lange     ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr);
133108633170SToby Isaac     if (original) {
133208633170SToby Isaac       PetscInt numParents;
133308633170SToby Isaac 
133408633170SToby Isaac       ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr);
133508633170SToby Isaac       ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr);
133608633170SToby Isaac       ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr);
133708633170SToby Isaac     }
133808633170SToby Isaac     else {
133908633170SToby Isaac       globParents = origParents;
134008633170SToby Isaac     }
134108633170SToby Isaac     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
134208633170SToby Isaac     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
134308633170SToby Isaac     if (original) {
134408633170SToby Isaac       ierr = PetscFree(globParents);CHKERRQ(ierr);
134508633170SToby Isaac     }
1346a6f36705SMichael Lange     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1347a6f36705SMichael Lange     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1348a6f36705SMichael Lange     ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr);
13494a54e071SToby Isaac #if PETSC_USE_DEBUG
13504a54e071SToby Isaac     {
13514a54e071SToby Isaac       PetscInt  p;
13524a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
13534a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
13544a54e071SToby Isaac         if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
13554a54e071SToby Isaac       }
13564a54e071SToby Isaac       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
13574a54e071SToby Isaac     }
13584a54e071SToby Isaac #endif
1359c5929fdfSBarry Smith     ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr);
1360a6f36705SMichael Lange     if (flg) {
1361a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr);
1362a6f36705SMichael Lange       ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1363a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr);
1364a6f36705SMichael Lange       ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1365a6f36705SMichael Lange       ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr);
1366a6f36705SMichael Lange     }
1367a6f36705SMichael Lange     ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr);
1368a6f36705SMichael Lange     ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr);
1369a6f36705SMichael Lange     ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr);
1370a6f36705SMichael Lange     ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr);
1371a6f36705SMichael Lange   }
137215078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1373a6f36705SMichael Lange   PetscFunctionReturn(0);
1374a6f36705SMichael Lange }
13750df0e737SMichael Lange 
137624cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
13774eca1733SMichael Lange {
13789852e123SBarry Smith   PetscMPIInt            rank, size;
13794eca1733SMichael Lange   MPI_Comm               comm;
13804eca1733SMichael Lange   PetscErrorCode         ierr;
13814eca1733SMichael Lange 
13824eca1733SMichael Lange   PetscFunctionBegin;
13834eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13844eca1733SMichael Lange   PetscValidPointer(dmParallel,7);
13854eca1733SMichael Lange 
13864eca1733SMichael Lange   /* Create point SF for parallel mesh */
13874eca1733SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13884eca1733SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
13894eca1733SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
13909852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
13914eca1733SMichael Lange   {
13924eca1733SMichael Lange     const PetscInt *leaves;
13934eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
13944eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
13954eca1733SMichael Lange     PetscInt        pStart, pEnd;
13964eca1733SMichael Lange 
13974eca1733SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
13984eca1733SMichael Lange     ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
13994eca1733SMichael Lange     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
14004eca1733SMichael Lange     for (p=0; p<numRoots; p++) {
14014eca1733SMichael Lange       rowners[p].rank  = -1;
14024eca1733SMichael Lange       rowners[p].index = -1;
14034eca1733SMichael Lange     }
14044eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14054eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14064eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14074eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
14084eca1733SMichael Lange         lowners[p].rank  = rank;
14094eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
14104eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
14114eca1733SMichael Lange         lowners[p].rank  = -2;
14124eca1733SMichael Lange         lowners[p].index = -2;
14134eca1733SMichael Lange       }
14144eca1733SMichael Lange     }
14154eca1733SMichael Lange     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
14164eca1733SMichael Lange       rowners[p].rank  = -3;
14174eca1733SMichael Lange       rowners[p].index = -3;
14184eca1733SMichael Lange     }
14194eca1733SMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14204eca1733SMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14214eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14224eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14234eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14244eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
14254eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
14264eca1733SMichael Lange     }
14274eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr);
14284eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
14294eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
14304eca1733SMichael Lange       if (lowners[p].rank != rank) {
14314eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
14324eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
14334eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
14344eca1733SMichael Lange         ++gp;
14354eca1733SMichael Lange       }
14364eca1733SMichael Lange     }
14374eca1733SMichael Lange     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
14384eca1733SMichael Lange     ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
14394eca1733SMichael Lange     ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr);
14404eca1733SMichael Lange   }
14411cf84007SMatthew G. Knepley   {
14421cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
14431cf84007SMatthew G. Knepley 
14441cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
14451cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
14461cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
14471cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
14481cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
14491cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
14501cf84007SMatthew G. Knepley   }
14514eca1733SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
14524eca1733SMichael Lange   PetscFunctionReturn(0);
14534eca1733SMichael Lange }
14544eca1733SMichael Lange 
1455f5bf2dbfSMichael Lange /*@C
1456f5bf2dbfSMichael Lange   DMPlexDerivePointSF - Build a point SF from an SF describing a point migration
1457f5bf2dbfSMichael Lange 
1458f5bf2dbfSMichael Lange   Input Parameter:
1459f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1460f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
14611627f6ccSMichael Lange . ownership   - Flag causing a vote to determine point ownership
1462f5bf2dbfSMichael Lange 
1463f5bf2dbfSMichael Lange   Output Parameter:
1464f5bf2dbfSMichael Lange - pointSF     - The star forest describing the point overlap in the remapped DM
1465f5bf2dbfSMichael Lange 
1466f5bf2dbfSMichael Lange   Level: developer
1467f5bf2dbfSMichael Lange 
1468f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1469f5bf2dbfSMichael Lange @*/
1470f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1471f5bf2dbfSMichael Lange {
1472f5bf2dbfSMichael Lange   PetscMPIInt        rank;
14731627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1474f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1475f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1476f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1477f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1478f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1479f5bf2dbfSMichael Lange 
1480f5bf2dbfSMichael Lange   PetscFunctionBegin;
1481f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1482f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1483f5bf2dbfSMichael Lange 
1484f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1485f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1486f5bf2dbfSMichael Lange   if (ownership) {
14871627f6ccSMichael Lange     /* Point ownership vote: Process with highest rank ownes shared points */
1488f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
1489f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1490f5bf2dbfSMichael Lange       leafNodes[p].rank  = rank;
149143f7d02bSMichael Lange       leafNodes[p].index = p;
1492f5bf2dbfSMichael Lange     }
1493f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
14941627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1495f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1496f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1497f5bf2dbfSMichael Lange     }
1498f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1499f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1500f5bf2dbfSMichael Lange   } else {
1501f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1502f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1503f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1504f5bf2dbfSMichael Lange     };
1505f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1506f5bf2dbfSMichael Lange       /* Write new local id into old location */
1507f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
15086462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1509f5bf2dbfSMichael Lange       }
1510f5bf2dbfSMichael Lange     }
1511f5bf2dbfSMichael Lange   }
1512f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1513f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1514f5bf2dbfSMichael Lange 
1515f5bf2dbfSMichael Lange   for (npointLeaves = 0, p = 0; p < nleaves; p++) {if (leafNodes[p].rank != rank) npointLeaves++;}
15161627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
15171627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1518f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1519f5bf2dbfSMichael Lange     if (leafNodes[p].rank != rank) {
1520f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1521f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1522f5bf2dbfSMichael Lange       idx++;
1523f5bf2dbfSMichael Lange     }
1524f5bf2dbfSMichael Lange   }
1525f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
15261627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1527f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1528f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1529f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1530f5bf2dbfSMichael Lange }
1531f5bf2dbfSMichael Lange 
153215078cd4SMichael Lange /*@C
153315078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
153415078cd4SMichael Lange 
153515078cd4SMichael Lange   Input Parameter:
153615078cd4SMichael Lange + dm       - The source DMPlex object
15371627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
153815078cd4SMichael Lange 
153915078cd4SMichael Lange   Output Parameter:
154015078cd4SMichael Lange - targetDM - The target DMPlex object
154115078cd4SMichael Lange 
1542b9f40539SMichael Lange   Level: intermediate
154315078cd4SMichael Lange 
154415078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
154515078cd4SMichael Lange @*/
1546b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
154715078cd4SMichael Lange {
1548b9f40539SMichael Lange   MPI_Comm               comm;
1549b9f40539SMichael Lange   PetscInt               dim, nroots;
1550b9f40539SMichael Lange   PetscSF                sfPoint;
155115078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1552ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1553b9f40539SMichael Lange   PetscBool              flg;
155415078cd4SMichael Lange   PetscErrorCode         ierr;
155515078cd4SMichael Lange 
155615078cd4SMichael Lange   PetscFunctionBegin;
155715078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15581b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1559b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
156015078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
156115078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
156215078cd4SMichael Lange 
1563bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1564b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1565b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1566bfb0467fSMichael Lange   if (nroots >= 0) {
1567b9f40539SMichael Lange     IS                     isOriginal;
1568ac04eaf7SToby Isaac     PetscInt               n, size, nleaves;
1569ac04eaf7SToby Isaac     PetscInt              *numbering_orig, *numbering_new;
1570b9f40539SMichael Lange     /* Get the original point numbering */
1571b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1572b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1573b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1574b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1575b9f40539SMichael Lange     /* Convert to positive global numbers */
1576b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1577b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1578b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1579b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1580b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1581b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1582b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1583b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1584b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
158515078cd4SMichael Lange   } else {
1586bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
158715078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
158815078cd4SMichael Lange   }
1589c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1590b9f40539SMichael Lange   if (flg) {
1591b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1592b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1593b9f40539SMichael Lange   }
159415078cd4SMichael Lange   /* Migrate DM data to target DM */
1595ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
159615078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
15970dc1530dSSander Arens   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
159815078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
159908633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1600ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1601302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
16021b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
160315078cd4SMichael Lange   PetscFunctionReturn(0);
160415078cd4SMichael Lange }
160515078cd4SMichael Lange 
16063b8f15a2SMatthew G. Knepley /*@C
160770034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
160870034214SMatthew G. Knepley 
160970034214SMatthew G. Knepley   Not Collective
161070034214SMatthew G. Knepley 
161170034214SMatthew G. Knepley   Input Parameter:
161270034214SMatthew G. Knepley + dm  - The original DMPlex object
161370034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
161470034214SMatthew G. Knepley 
161570034214SMatthew G. Knepley   Output Parameter:
161670034214SMatthew G. Knepley + sf - The PetscSF used for point distribution
161770034214SMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
161870034214SMatthew G. Knepley 
161970034214SMatthew G. Knepley   Note: If the mesh was not distributed, the return value is NULL.
162070034214SMatthew G. Knepley 
162193a1dc33SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMPlexSetAdjacencyUseCone() and
162270034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
162370034214SMatthew G. Knepley   representation on the mesh.
162470034214SMatthew G. Knepley 
162570034214SMatthew G. Knepley   Level: intermediate
162670034214SMatthew G. Knepley 
162770034214SMatthew G. Knepley .keywords: mesh, elements
162870034214SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
162970034214SMatthew G. Knepley @*/
163080cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
163170034214SMatthew G. Knepley {
163270034214SMatthew G. Knepley   MPI_Comm               comm;
163315078cd4SMichael Lange   PetscPartitioner       partitioner;
1634f8987ae8SMichael Lange   IS                     cellPart;
1635f8987ae8SMichael Lange   PetscSection           cellPartSection;
1636cf86098cSMatthew G. Knepley   DM                     dmCoord;
1637f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
163843f7d02bSMichael Lange   PetscSF                sfProcess, sfMigration, sfStratified, sfPoint;
163970034214SMatthew G. Knepley   PetscBool              flg;
16409852e123SBarry Smith   PetscMPIInt            rank, size, p;
164170034214SMatthew G. Knepley   PetscErrorCode         ierr;
164270034214SMatthew G. Knepley 
164370034214SMatthew G. Knepley   PetscFunctionBegin;
164470034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
164570034214SMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
164670034214SMatthew G. Knepley   PetscValidPointer(dmParallel,5);
164770034214SMatthew G. Knepley 
164870034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
164970034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
165070034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
16519852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
165270034214SMatthew G. Knepley 
1653a6ba33f0SVaclav Hapla   if (sf) *sf = NULL;
165470034214SMatthew G. Knepley   *dmParallel = NULL;
16559852e123SBarry Smith   if (size == 1) {
16565f3267c8SKoos Huijssen     ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
16575f3267c8SKoos Huijssen     PetscFunctionReturn(0);
16585f3267c8SKoos Huijssen   }
165970034214SMatthew G. Knepley 
166015078cd4SMichael Lange   /* Create cell partition */
166177623264SMatthew G. Knepley   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
166277623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
166315078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
166415078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1665f8987ae8SMichael Lange   {
1666f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1667f8987ae8SMichael Lange     PetscInt proc, pStart, pEnd, npoints, poffset;
1668f8987ae8SMichael Lange     const PetscInt *points;
1669f8987ae8SMichael Lange     ierr = DMLabelCreate("Point Partition", &lblPartition);CHKERRQ(ierr);
1670f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1671f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1672f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1673f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1674f8987ae8SMichael Lange       ierr = PetscSectionGetOffset(cellPartSection, proc, &poffset);CHKERRQ(ierr);
1675f8987ae8SMichael Lange       for (p = poffset; p < poffset+npoints; p++) {
1676f8987ae8SMichael Lange         ierr = DMLabelSetValue(lblPartition, points[p], proc);CHKERRQ(ierr);
167770034214SMatthew G. Knepley       }
1678f8987ae8SMichael Lange     }
1679f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1680f8987ae8SMichael Lange   }
1681f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelClosure(dm, lblPartition);CHKERRQ(ierr);
1682f8987ae8SMichael Lange   {
1683f8987ae8SMichael Lange     /* Build a global process SF */
1684f8987ae8SMichael Lange     PetscSFNode *remoteProc;
16859852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
16869852e123SBarry Smith     for (p = 0; p < size; ++p) {
1687f8987ae8SMichael Lange       remoteProc[p].rank  = p;
1688f8987ae8SMichael Lange       remoteProc[p].index = rank;
1689f8987ae8SMichael Lange     }
1690f8987ae8SMichael Lange     ierr = PetscSFCreate(comm, &sfProcess);CHKERRQ(ierr);
1691f8987ae8SMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProcess, "Process SF");CHKERRQ(ierr);
16929852e123SBarry Smith     ierr = PetscSFSetGraph(sfProcess, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
1693f8987ae8SMichael Lange   }
1694f8987ae8SMichael Lange   ierr = DMLabelCreate("Point migration", &lblMigration);CHKERRQ(ierr);
1695f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, sfProcess, lblMigration);CHKERRQ(ierr);
1696302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
169743f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
169843f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
169943f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
170043f7d02bSMichael Lange   sfMigration = sfStratified;
1701f8987ae8SMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1702c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
170370034214SMatthew G. Knepley   if (flg) {
1704f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1705f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
170670034214SMatthew G. Knepley   }
1707f8987ae8SMichael Lange 
170815078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
170970034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
171070034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1711b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
171270034214SMatthew G. Knepley 
1713a157612eSMichael Lange   /* Build the point SF without overlap */
17141627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1715f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1716cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1717cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1718f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
171970034214SMatthew G. Knepley 
1720a157612eSMichael Lange   if (overlap > 0) {
172115078cd4SMichael Lange     DM                 dmOverlap;
172215078cd4SMichael Lange     PetscInt           nroots, nleaves;
172315078cd4SMichael Lange     PetscSFNode       *newRemote;
172415078cd4SMichael Lange     const PetscSFNode *oldRemote;
172515078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1726a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1727b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1728a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1729a157612eSMichael Lange     *dmParallel = dmOverlap;
1730c389ea9fSToby Isaac     if (flg) {
17313d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
173215078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1733c389ea9fSToby Isaac     }
173443331d4aSMichael Lange 
1735f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1736f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
173715078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
173843331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
173915078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
174015078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
174115078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
174215078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
174315078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1744f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
174515078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1746c389ea9fSToby Isaac   }
1747bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1748f8987ae8SMichael Lange   ierr = PetscSFDestroy(&sfProcess);CHKERRQ(ierr);
1749f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1750f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
17514eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
17524eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1753721cbd76SMatthew G. Knepley   /* Copy BC */
1754a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
175566fe0bfeSMatthew G. Knepley   /* Create sfNatural */
175666fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
175766fe0bfeSMatthew G. Knepley     PetscSection section;
175866fe0bfeSMatthew G. Knepley 
175966fe0bfeSMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
17600c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
17610e663481SBlaise Bourdin     ierr = DMSetUseNatural(*dmParallel, PETSC_TRUE);CHKERRQ(ierr);
176266fe0bfeSMatthew G. Knepley   }
1763721cbd76SMatthew G. Knepley   /* Cleanup */
1764f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
1765f8987ae8SMichael Lange   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1766f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
176770034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
176870034214SMatthew G. Knepley   PetscFunctionReturn(0);
176970034214SMatthew G. Knepley }
1770a157612eSMichael Lange 
1771a157612eSMichael Lange /*@C
177224cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1773a157612eSMichael Lange 
1774a157612eSMichael Lange   Not Collective
1775a157612eSMichael Lange 
1776a157612eSMichael Lange   Input Parameter:
1777a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
1778a157612eSMichael Lange - overlap - The overlap of partitions, 0 is the default
1779a157612eSMichael Lange 
1780a157612eSMichael Lange   Output Parameter:
1781a157612eSMichael Lange + sf - The PetscSF used for point distribution
1782a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1783a157612eSMichael Lange 
1784a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1785a157612eSMichael Lange 
1786a157612eSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
1787a157612eSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
1788a157612eSMichael Lange   representation on the mesh.
1789a157612eSMichael Lange 
1790a157612eSMichael Lange   Level: intermediate
1791a157612eSMichael Lange 
1792a157612eSMichael Lange .keywords: mesh, elements
1793a157612eSMichael Lange .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
1794a157612eSMichael Lange @*/
1795b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1796a157612eSMichael Lange {
1797a157612eSMichael Lange   MPI_Comm               comm;
17983567eaeeSMatthew G. Knepley   PetscMPIInt            size, rank;
1799a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1800a157612eSMichael Lange   IS                     rootrank, leafrank;
1801cf86098cSMatthew G. Knepley   DM                     dmCoord;
1802a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1803f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1804a157612eSMichael Lange   PetscErrorCode         ierr;
1805a157612eSMichael Lange 
1806a157612eSMichael Lange   PetscFunctionBegin;
1807a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1808a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1809a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1810a157612eSMichael Lange 
1811a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
18123567eaeeSMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1813a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
18143567eaeeSMatthew G. Knepley   if (size == 1) {*dmOverlap = NULL; PetscFunctionReturn(0);}
18153567eaeeSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1816a157612eSMichael Lange 
1817a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
1818a157612eSMichael Lange   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1819a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1820a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1821a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1822a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1823a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1824a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1825a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1826a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1827a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1828a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1829a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1830a9f1d5b2SMichael Lange 
183115fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
183215fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
183315fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
183415fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
1835a157612eSMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1836a157612eSMichael Lange 
1837a157612eSMichael Lange   /* Build the overlapping DM */
1838a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1839a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1840a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1841f5bf2dbfSMichael Lange   /* Build the new point SF */
18421627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1843f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1844cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1845cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1846f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1847a157612eSMichael Lange   /* Cleanup overlap partition */
1848a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1849a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1850a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
18511b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1852a157612eSMichael Lange   PetscFunctionReturn(0);
1853a157612eSMichael Lange }
18546462276dSToby Isaac 
18556462276dSToby Isaac /*@C
18566462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
18576462276dSToby Isaac   root process of the original's communicator.
18586462276dSToby Isaac 
18596462276dSToby Isaac   Input Parameters:
18606462276dSToby Isaac . dm - the original DMPlex object
18616462276dSToby Isaac 
18626462276dSToby Isaac   Output Parameters:
18636462276dSToby Isaac . gatherMesh - the gathered DM object, or NULL
18646462276dSToby Isaac 
18656462276dSToby Isaac   Level: intermediate
18666462276dSToby Isaac 
18676462276dSToby Isaac .keywords: mesh
18686462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
18696462276dSToby Isaac @*/
18706462276dSToby Isaac PetscErrorCode DMPlexGetGatherDM(DM dm, DM * gatherMesh)
18716462276dSToby Isaac {
18726462276dSToby Isaac   MPI_Comm       comm;
18736462276dSToby Isaac   PetscMPIInt    size;
18746462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
18756462276dSToby Isaac   PetscErrorCode ierr;
18766462276dSToby Isaac 
18776462276dSToby Isaac   PetscFunctionBegin;
18786462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18796462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18806462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18816462276dSToby Isaac   *gatherMesh = NULL;
18826462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18836462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
18846462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
18856462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
18866462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
18876462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
18886462276dSToby Isaac   ierr = DMPlexDistribute(dm,0,NULL,gatherMesh);CHKERRQ(ierr);
18896462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
18906462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
18916462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
18926462276dSToby Isaac   PetscFunctionReturn(0);
18936462276dSToby Isaac }
18946462276dSToby Isaac 
18956462276dSToby Isaac /*@C
18966462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
18976462276dSToby Isaac 
18986462276dSToby Isaac   Input Parameters:
18996462276dSToby Isaac . dm - the original DMPlex object
19006462276dSToby Isaac 
19016462276dSToby Isaac   Output Parameters:
19026462276dSToby Isaac . redundantMesh - the redundant DM object, or NULL
19036462276dSToby Isaac 
19046462276dSToby Isaac   Level: intermediate
19056462276dSToby Isaac 
19066462276dSToby Isaac .keywords: mesh
19076462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
19086462276dSToby Isaac @*/
19096462276dSToby Isaac PetscErrorCode DMPlexGetRedundantDM(DM dm, DM * redundantMesh)
19106462276dSToby Isaac {
19116462276dSToby Isaac   MPI_Comm       comm;
19126462276dSToby Isaac   PetscMPIInt    size, rank;
19136462276dSToby Isaac   PetscInt       pStart, pEnd, p;
19146462276dSToby Isaac   PetscInt       numPoints = -1;
19156462276dSToby Isaac   PetscSF        migrationSF, sfPoint;
19166462276dSToby Isaac   DM             gatherDM, dmCoord;
19176462276dSToby Isaac   PetscSFNode    *points;
19186462276dSToby Isaac   PetscErrorCode ierr;
19196462276dSToby Isaac 
19206462276dSToby Isaac   PetscFunctionBegin;
19216462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19226462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
19236462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19246462276dSToby Isaac   *redundantMesh = NULL;
192568dbc166SMatthew G. Knepley   if (size == 1) {
192668dbc166SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
192768dbc166SMatthew G. Knepley     *redundantMesh = dm;
192868dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
192968dbc166SMatthew G. Knepley   }
19306462276dSToby Isaac   ierr = DMPlexGetGatherDM(dm,&gatherDM);CHKERRQ(ierr);
19316462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
19326462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
19336462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
19346462276dSToby Isaac   numPoints = pEnd - pStart;
19356462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
19366462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
19376462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
19386462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
19396462276dSToby Isaac     points[p].index = p;
19406462276dSToby Isaac     points[p].rank  = 0;
19416462276dSToby Isaac   }
19426462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
19436462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
19446462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
19456462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
19466462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
19476462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
19486462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
19496462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
19506462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
19516462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
19526462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
19536462276dSToby Isaac   PetscFunctionReturn(0);
19546462276dSToby Isaac }
1955