xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision 82effdf1d7727dcaeced90a90634a8d19f8bcf96)
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 
42595452b02SPatrick Sanan   Notes:
42695452b02SPatrick Sanan     The user must PetscFree the adj array if it was not passed in.
42770034214SMatthew G. Knepley 
42870034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
42970034214SMatthew G. Knepley @*/
43070034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
43170034214SMatthew G. Knepley {
4321cf84007SMatthew G. Knepley   PetscBool      useCone, useClosure, useAnchors;
43370034214SMatthew G. Knepley   PetscErrorCode ierr;
43470034214SMatthew G. Knepley 
43570034214SMatthew G. Knepley   PetscFunctionBeginHot;
43670034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43770034214SMatthew G. Knepley   PetscValidPointer(adjSize,3);
43870034214SMatthew G. Knepley   PetscValidPointer(adj,4);
4391cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
4401cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
4411cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
4421cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);CHKERRQ(ierr);
44370034214SMatthew G. Knepley   PetscFunctionReturn(0);
44470034214SMatthew G. Knepley }
44508633170SToby Isaac 
446b0a623aaSMatthew G. Knepley /*@
447b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
448b0a623aaSMatthew G. Knepley 
449b0a623aaSMatthew G. Knepley   Collective on DM
450b0a623aaSMatthew G. Knepley 
451b0a623aaSMatthew G. Knepley   Input Parameters:
452b0a623aaSMatthew G. Knepley + dm      - The DM
453b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity
454b0a623aaSMatthew G. Knepley 
455b0a623aaSMatthew G. Knepley   Output Parameters:
456b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
457b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
458b0a623aaSMatthew G. Knepley 
459b0a623aaSMatthew G. Knepley   Level: developer
460b0a623aaSMatthew G. Knepley 
461b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
462b0a623aaSMatthew G. Knepley @*/
463b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
464b0a623aaSMatthew G. Knepley {
465b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
466b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
467b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
468b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
469b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
470b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
471b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
4729852e123SBarry Smith   PetscMPIInt        size, proc, rank;
473b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
474b0a623aaSMatthew G. Knepley 
475b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
476b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
478b0a623aaSMatthew G. Knepley   if (processRanks) {PetscValidPointer(processRanks, 3);}
479b0a623aaSMatthew G. Knepley   if (sfProcess)    {PetscValidPointer(sfProcess, 4);}
4809852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
481b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
482b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr);
4839852e123SBarry Smith   ierr = PetscBTCreate(size, &neighbors);CHKERRQ(ierr);
4849852e123SBarry Smith   ierr = PetscBTMemzero(size, neighbors);CHKERRQ(ierr);
485b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
486b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr);
487b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr);
488b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
489b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
490b0a623aaSMatthew G. Knepley 
491b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr);
492b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr);
493302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
494b0a623aaSMatthew G. Knepley   }
495b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr);
496b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
497b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr);
498b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr);
499b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
500b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
501b0a623aaSMatthew G. Knepley 
502b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr);
503b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr);
504302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
505b0a623aaSMatthew G. Knepley   }
506b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr);
507b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
508b0a623aaSMatthew G. Knepley   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
509b0a623aaSMatthew G. Knepley   /* Calculate edges */
510b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
5119852e123SBarry Smith   for(proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
512b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr);
513b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr);
514b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr);
5159852e123SBarry Smith   for(proc = 0, n = 0; proc < size; ++proc) {
516b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
517b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
518b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
519b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
520b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
521b0a623aaSMatthew G. Knepley       ++n;
522b0a623aaSMatthew G. Knepley     }
523b0a623aaSMatthew G. Knepley   }
524b0a623aaSMatthew G. Knepley   ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr);
525b0a623aaSMatthew G. Knepley   if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);}
526b0a623aaSMatthew G. Knepley   else              {ierr = PetscFree(ranksNew);CHKERRQ(ierr);}
527b0a623aaSMatthew G. Knepley   if (sfProcess) {
528b0a623aaSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr);
529b0a623aaSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr);
530b0a623aaSMatthew G. Knepley     ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr);
5319852e123SBarry Smith     ierr = PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
532b0a623aaSMatthew G. Knepley   }
533b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
534b0a623aaSMatthew G. Knepley }
535b0a623aaSMatthew G. Knepley 
536b0a623aaSMatthew G. Knepley /*@
537b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
538b0a623aaSMatthew G. Knepley 
539b0a623aaSMatthew G. Knepley   Collective on DM
540b0a623aaSMatthew G. Knepley 
541b0a623aaSMatthew G. Knepley   Input Parameter:
542b0a623aaSMatthew G. Knepley . dm - The DM
543b0a623aaSMatthew G. Knepley 
544b0a623aaSMatthew G. Knepley   Output Parameters:
545b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
546b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
547b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
548b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
549b0a623aaSMatthew G. Knepley 
550b0a623aaSMatthew G. Knepley   Level: developer
551b0a623aaSMatthew G. Knepley 
552b0a623aaSMatthew G. Knepley .seealso: DMPlexCreateOverlap()
553b0a623aaSMatthew G. Knepley @*/
554b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
555b0a623aaSMatthew G. Knepley {
556b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
557b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
558b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
559b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
560b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
561b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
562b0a623aaSMatthew G. Knepley   PetscErrorCode  ierr;
563b0a623aaSMatthew G. Knepley 
564b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
565b0a623aaSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
566b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
567b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
568b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
569b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
570b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr);
571b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr);
572b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr);
573b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr);
574b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);}
575b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
576b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
577b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr);
578b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr);
579b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(nedges,  &remoterank);CHKERRQ(ierr);
580b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
581b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
582b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
583b0a623aaSMatthew G. Knepley   ierr = PetscFree(myrank);CHKERRQ(ierr);
584b0a623aaSMatthew G. Knepley   ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr);
585b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
586b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr);
587b0a623aaSMatthew G. Knepley   ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr);
588b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
589b0a623aaSMatthew G. Knepley }
590b0a623aaSMatthew G. Knepley 
591278397a0SMatthew G. Knepley /*@C
592b0a623aaSMatthew G. Knepley   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.
593b0a623aaSMatthew G. Knepley 
594b0a623aaSMatthew G. Knepley   Collective on DM
595b0a623aaSMatthew G. Knepley 
596b0a623aaSMatthew G. Knepley   Input Parameters:
597b0a623aaSMatthew G. Knepley + dm          - The DM
59824d039d7SMichael Lange . levels      - Number of overlap levels
599b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
600b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
601b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
602b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
603b0a623aaSMatthew G. Knepley 
604b0a623aaSMatthew G. Knepley   Output Parameters:
605a9f1d5b2SMichael Lange + ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
606b0a623aaSMatthew G. Knepley 
607b0a623aaSMatthew G. Knepley   Level: developer
608b0a623aaSMatthew G. Knepley 
6091fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
610b0a623aaSMatthew G. Knepley @*/
611a9f1d5b2SMichael Lange PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
612b0a623aaSMatthew G. Knepley {
613e540f424SMichael Lange   MPI_Comm           comm;
614b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
615b0a623aaSMatthew G. Knepley   PetscSF            sfPoint, sfProc;
616b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
617b0a623aaSMatthew G. Knepley   const PetscInt    *local;
6181fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
619b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
6201fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
6219852e123SBarry Smith   PetscMPIInt        rank, size;
62226a7d390SMatthew G. Knepley   PetscBool          useCone, useClosure, flg;
623b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
624b0a623aaSMatthew G. Knepley 
625b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
626e540f424SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
6279852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
628e540f424SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
629b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
630b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
631b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr);
632b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
633b0a623aaSMatthew G. Knepley   ierr = DMLabelCreate("Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr);
634b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
635b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
636b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
637b0a623aaSMatthew G. Knepley 
638b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, local[l], &adjSize, &adj);CHKERRQ(ierr);
639b0a623aaSMatthew G. Knepley     for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);}
640b0a623aaSMatthew G. Knepley   }
641b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr);
642b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr);
643b0a623aaSMatthew G. Knepley   /* Handle roots */
644b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
645b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
646b0a623aaSMatthew G. Knepley 
647b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
648b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
649b0a623aaSMatthew G. Knepley       ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr);
650b0a623aaSMatthew G. Knepley       if (neighbors) {
651b0a623aaSMatthew G. Knepley         ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr);
652b0a623aaSMatthew G. Knepley         ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
653b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
654b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff+n];
655b0a623aaSMatthew G. Knepley 
656b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
657b0a623aaSMatthew G. Knepley           for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
658b0a623aaSMatthew G. Knepley         }
659b0a623aaSMatthew G. Knepley       }
660b0a623aaSMatthew G. Knepley     }
661b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
662b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr);
663b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
664b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr);
665b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
666b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
667b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff+n];
668b0a623aaSMatthew G. Knepley 
669b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
670b0a623aaSMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
671b0a623aaSMatthew G. Knepley     }
672b0a623aaSMatthew G. Knepley   }
673b0a623aaSMatthew G. Knepley   ierr = PetscFree(adj);CHKERRQ(ierr);
674b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr);
675b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr);
67624d039d7SMichael Lange   /* Add additional overlap levels */
677be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
678be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
679be200f8dSMichael Lange     ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr);
680be200f8dSMichael Lange     /* Add next level of point donations to the label */
681be200f8dSMichael Lange     ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr);
682be200f8dSMichael Lange   }
68326a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
68426a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
68526a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
68626a7d390SMatthew G. Knepley   if (useCone || !useClosure) {
6875abbe4feSMichael Lange     ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr);
68826a7d390SMatthew G. Knepley   }
689c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr);
690e540f424SMichael Lange   if (flg) {
691b0a623aaSMatthew G. Knepley     ierr = DMLabelView(ovAdjByRank, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
692b0a623aaSMatthew G. Knepley   }
69371a8c5fcSMichael Lange   /* Make global process SF and invert sender to receiver label */
69471a8c5fcSMichael Lange   {
69571a8c5fcSMichael Lange     /* Build a global process SF */
69671a8c5fcSMichael Lange     PetscSFNode *remoteProc;
6979852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
6989852e123SBarry Smith     for (p = 0; p < size; ++p) {
69971a8c5fcSMichael Lange       remoteProc[p].rank  = p;
70071a8c5fcSMichael Lange       remoteProc[p].index = rank;
70171a8c5fcSMichael Lange     }
70271a8c5fcSMichael Lange     ierr = PetscSFCreate(comm, &sfProc);CHKERRQ(ierr);
70371a8c5fcSMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProc, "Process SF");CHKERRQ(ierr);
7049852e123SBarry Smith     ierr = PetscSFSetGraph(sfProc, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
70571a8c5fcSMichael Lange   }
706a9f1d5b2SMichael Lange   ierr = DMLabelCreate("Overlap label", ovLabel);CHKERRQ(ierr);
707a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, sfProc, *ovLabel);CHKERRQ(ierr);
708a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
709a9f1d5b2SMichael Lange   for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);}
710e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
711a9f1d5b2SMichael Lange     ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr);
712a9f1d5b2SMichael Lange     ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr);
713e540f424SMichael Lange   }
714e540f424SMichael Lange   /* Clean up */
7151fd9873aSMichael Lange   ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr);
716b0a623aaSMatthew G. Knepley   ierr = PetscSFDestroy(&sfProc);CHKERRQ(ierr);
717b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
718b0a623aaSMatthew G. Knepley }
71970034214SMatthew G. Knepley 
72024cc2ca5SMatthew G. Knepley /*@C
72124cc2ca5SMatthew G. Knepley   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF
72224cc2ca5SMatthew G. Knepley 
72324cc2ca5SMatthew G. Knepley   Collective on DM
72424cc2ca5SMatthew G. Knepley 
72524cc2ca5SMatthew G. Knepley   Input Parameters:
72624cc2ca5SMatthew G. Knepley + dm          - The DM
72724cc2ca5SMatthew G. Knepley - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes
72824cc2ca5SMatthew G. Knepley 
72924cc2ca5SMatthew G. Knepley   Output Parameters:
73024cc2ca5SMatthew G. Knepley + migrationSF - An SF that maps original points in old locations to points in new locations
73124cc2ca5SMatthew G. Knepley 
73224cc2ca5SMatthew G. Knepley   Level: developer
73324cc2ca5SMatthew G. Knepley 
73424cc2ca5SMatthew G. Knepley .seealso: DMPlexCreateOverlap(), DMPlexDistribute()
73524cc2ca5SMatthew G. Knepley @*/
73646f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
73746f9b1c3SMichael Lange {
73846f9b1c3SMichael Lange   MPI_Comm           comm;
7399852e123SBarry Smith   PetscMPIInt        rank, size;
74046f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
74146f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
74246f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
74346f9b1c3SMichael Lange   PetscSFNode       *iremote;
74446f9b1c3SMichael Lange   PetscSF            pointSF;
74546f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
74646f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
74746f9b1c3SMichael Lange   PetscErrorCode     ierr;
74846f9b1c3SMichael Lange 
74946f9b1c3SMichael Lange   PetscFunctionBegin;
75046f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75146f9b1c3SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
75246f9b1c3SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
7539852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
75446f9b1c3SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
75546f9b1c3SMichael Lange 
75646f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
75746f9b1c3SMichael Lange   ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr);
75846f9b1c3SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
75946f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
76046f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
76146f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
76246f9b1c3SMichael Lange   }
76346f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
76446f9b1c3SMichael Lange   ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
76546f9b1c3SMichael Lange   ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
76646f9b1c3SMichael Lange 
76746f9b1c3SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
76846f9b1c3SMichael Lange   ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr);
76946f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) depthRecv[d]=0;
77046f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
77146f9b1c3SMichael Lange   depthShift[dim] = 0;
77246f9b1c3SMichael Lange   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
77346f9b1c3SMichael Lange   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
77446f9b1c3SMichael Lange   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
77546f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
77646f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
77746f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
77846f9b1c3SMichael Lange   }
77946f9b1c3SMichael Lange 
78046f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
78146f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
78246f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
78309b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr);
78409b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr);
78546f9b1c3SMichael Lange   /* First map local points to themselves */
78646f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
78746f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
78846f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) {
78946f9b1c3SMichael Lange       point = p + depthShift[d];
79046f9b1c3SMichael Lange       ilocal[point] = point;
79146f9b1c3SMichael Lange       iremote[point].index = p;
79246f9b1c3SMichael Lange       iremote[point].rank = rank;
79346f9b1c3SMichael Lange       depthIdx[d]++;
79446f9b1c3SMichael Lange     }
79546f9b1c3SMichael Lange   }
79646f9b1c3SMichael Lange 
79746f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
79846f9b1c3SMichael Lange   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
79946f9b1c3SMichael Lange   ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr);
80046f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
80146f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
80246f9b1c3SMichael Lange     for (p=0; p<numSharedPoints; p++) {
80346f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
80446f9b1c3SMichael Lange         point = sharedLocal[p] + depthShift[d];
80546f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
80646f9b1c3SMichael Lange         iremote[point].rank = sharedRemote[p].rank;
80746f9b1c3SMichael Lange       }
80846f9b1c3SMichael Lange     }
80946f9b1c3SMichael Lange   }
81046f9b1c3SMichael Lange 
81146f9b1c3SMichael Lange   /* Now add the incoming overlap points */
81246f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) {
81346f9b1c3SMichael Lange     point = depthIdx[remoteDepths[p]];
81446f9b1c3SMichael Lange     ilocal[point] = point;
81546f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
81646f9b1c3SMichael Lange     iremote[point].rank = overlapRemote[p].rank;
81746f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
81846f9b1c3SMichael Lange   }
81915fff7beSMatthew G. Knepley   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
82046f9b1c3SMichael Lange 
82146f9b1c3SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
82246f9b1c3SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr);
82346f9b1c3SMichael Lange   ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr);
82446f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
82546f9b1c3SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
82646f9b1c3SMichael Lange 
82746f9b1c3SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
82870034214SMatthew G. Knepley   PetscFunctionReturn(0);
82970034214SMatthew G. Knepley }
83070034214SMatthew G. Knepley 
831a9f1d5b2SMichael Lange /*@
832f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
833a9f1d5b2SMichael Lange 
834a9f1d5b2SMichael Lange   Input Parameter:
835a9f1d5b2SMichael Lange + dm          - The DM
836a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
837a9f1d5b2SMichael Lange 
838a9f1d5b2SMichael Lange   Output Parameter:
839a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
840a9f1d5b2SMichael Lange 
841a9f1d5b2SMichael Lange   Level: developer
842a9f1d5b2SMichael Lange 
843a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
844a9f1d5b2SMichael Lange @*/
845a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
846a9f1d5b2SMichael Lange {
847a9f1d5b2SMichael Lange   MPI_Comm           comm;
8489852e123SBarry Smith   PetscMPIInt        rank, size;
8497fab53ddSMatthew G. Knepley   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
850a9f1d5b2SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
851a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
852f0e73a3dSToby Isaac   PetscInt           hybEnd[4];
853a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
854a9f1d5b2SMichael Lange   PetscErrorCode     ierr;
855a9f1d5b2SMichael Lange 
856a9f1d5b2SMichael Lange   PetscFunctionBegin;
857a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
858a9f1d5b2SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
859a9f1d5b2SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
8609852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
8617fab53ddSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr);
862b2566f29SBarry Smith   ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
8637fab53ddSMatthew G. Knepley   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
864a9f1d5b2SMichael Lange 
865a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
866a9f1d5b2SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr);
867a9f1d5b2SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
868f0e73a3dSToby Isaac   ierr = DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[depth-1],&hybEnd[1],&hybEnd[0]);CHKERRQ(ierr);
8697fab53ddSMatthew G. Knepley   for (d = 0; d < depth+1; ++d) {
870a9f1d5b2SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
871f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
872f0e73a3dSToby Isaac       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
873f0e73a3dSToby Isaac         pointDepths[p] = 2 * d;
874f0e73a3dSToby Isaac       } else {
875f0e73a3dSToby Isaac         pointDepths[p] = 2 * d + 1;
876f0e73a3dSToby Isaac       }
877f0e73a3dSToby Isaac     }
878a9f1d5b2SMichael Lange   }
8797fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
880a9f1d5b2SMichael Lange   ierr = PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
881a9f1d5b2SMichael Lange   ierr = PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
882a9f1d5b2SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
883f0e73a3dSToby Isaac   ierr = PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);CHKERRQ(ierr);
884f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
8857fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
886f0e73a3dSToby Isaac   depthShift[2*depth+1] = 0;
887f0e73a3dSToby Isaac   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
888f0e73a3dSToby Isaac   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
889f0e73a3dSToby Isaac   depthShift[0] += depthRecv[1];
890f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
891f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
892f0e73a3dSToby Isaac   for (d = 2 * depth-1; d > 2; --d) {
893f0e73a3dSToby Isaac     PetscInt e;
894f0e73a3dSToby Isaac 
895f0e73a3dSToby Isaac     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
896f0e73a3dSToby Isaac   }
897f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
898a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
899a9f1d5b2SMichael Lange   ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr);
9007fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
9017fab53ddSMatthew G. Knepley     const PetscInt dep = remoteDepths[p];
9027fab53ddSMatthew G. Knepley 
9037fab53ddSMatthew G. Knepley     ilocal[p] = depthShift[dep] + depthIdx[dep];
9047fab53ddSMatthew G. Knepley     depthIdx[dep]++;
905a9f1d5b2SMichael Lange   }
906a9f1d5b2SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
907a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr);
908a9f1d5b2SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr);
909a9f1d5b2SMichael Lange   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
910a9f1d5b2SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
911a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
912a9f1d5b2SMichael Lange }
913a9f1d5b2SMichael Lange 
91470034214SMatthew G. Knepley /*@
91570034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
91670034214SMatthew G. Knepley 
91770034214SMatthew G. Knepley   Collective on DM
91870034214SMatthew G. Knepley 
91970034214SMatthew G. Knepley   Input Parameters:
92070034214SMatthew G. Knepley + dm - The DMPlex object
92170034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
92270034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
92370034214SMatthew G. Knepley - originalVec - The existing data
92470034214SMatthew G. Knepley 
92570034214SMatthew G. Knepley   Output Parameters:
92670034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
92770034214SMatthew G. Knepley - newVec - The new data
92870034214SMatthew G. Knepley 
92970034214SMatthew G. Knepley   Level: developer
93070034214SMatthew G. Knepley 
93170034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
93270034214SMatthew G. Knepley @*/
93370034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
93470034214SMatthew G. Knepley {
93570034214SMatthew G. Knepley   PetscSF        fieldSF;
93670034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
93770034214SMatthew G. Knepley   PetscScalar   *originalValues, *newValues;
93870034214SMatthew G. Knepley   PetscErrorCode ierr;
93970034214SMatthew G. Knepley 
94070034214SMatthew G. Knepley   PetscFunctionBegin;
94170034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
94270034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
94370034214SMatthew G. Knepley 
94470034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
94570034214SMatthew G. Knepley   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
94670034214SMatthew G. Knepley   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
94770034214SMatthew G. Knepley 
94870034214SMatthew G. Knepley   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
94970034214SMatthew G. Knepley   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
95070034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9518a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
95270034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
95370034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
95470034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
95570034214SMatthew G. Knepley   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
95670034214SMatthew G. Knepley   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
95770034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
95870034214SMatthew G. Knepley   PetscFunctionReturn(0);
95970034214SMatthew G. Knepley }
96070034214SMatthew G. Knepley 
96170034214SMatthew G. Knepley /*@
96270034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
96370034214SMatthew G. Knepley 
96470034214SMatthew G. Knepley   Collective on DM
96570034214SMatthew G. Knepley 
96670034214SMatthew G. Knepley   Input Parameters:
96770034214SMatthew G. Knepley + dm - The DMPlex object
96870034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
96970034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
97070034214SMatthew G. Knepley - originalIS - The existing data
97170034214SMatthew G. Knepley 
97270034214SMatthew G. Knepley   Output Parameters:
97370034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
97470034214SMatthew G. Knepley - newIS - The new data
97570034214SMatthew G. Knepley 
97670034214SMatthew G. Knepley   Level: developer
97770034214SMatthew G. Knepley 
97870034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
97970034214SMatthew G. Knepley @*/
98070034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
98170034214SMatthew G. Knepley {
98270034214SMatthew G. Knepley   PetscSF         fieldSF;
98370034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
98470034214SMatthew G. Knepley   const PetscInt *originalValues;
98570034214SMatthew G. Knepley   PetscErrorCode  ierr;
98670034214SMatthew G. Knepley 
98770034214SMatthew G. Knepley   PetscFunctionBegin;
98870034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
98970034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
99070034214SMatthew G. Knepley 
99170034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
992854ce69bSBarry Smith   ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr);
99370034214SMatthew G. Knepley 
99470034214SMatthew G. Knepley   ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr);
99570034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9968a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
997aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
998aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
99970034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
100070034214SMatthew G. Knepley   ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr);
100170034214SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr);
100270034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
100370034214SMatthew G. Knepley   PetscFunctionReturn(0);
100470034214SMatthew G. Knepley }
100570034214SMatthew G. Knepley 
100670034214SMatthew G. Knepley /*@
100770034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
100870034214SMatthew G. Knepley 
100970034214SMatthew G. Knepley   Collective on DM
101070034214SMatthew G. Knepley 
101170034214SMatthew G. Knepley   Input Parameters:
101270034214SMatthew G. Knepley + dm - The DMPlex object
101370034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
101470034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
101570034214SMatthew G. Knepley . datatype - The type of data
101670034214SMatthew G. Knepley - originalData - The existing data
101770034214SMatthew G. Knepley 
101870034214SMatthew G. Knepley   Output Parameters:
101970034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
102070034214SMatthew G. Knepley - newData - The new data
102170034214SMatthew G. Knepley 
102270034214SMatthew G. Knepley   Level: developer
102370034214SMatthew G. Knepley 
102470034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
102570034214SMatthew G. Knepley @*/
102670034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
102770034214SMatthew G. Knepley {
102870034214SMatthew G. Knepley   PetscSF        fieldSF;
102970034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
103070034214SMatthew G. Knepley   PetscMPIInt    dataSize;
103170034214SMatthew G. Knepley   PetscErrorCode ierr;
103270034214SMatthew G. Knepley 
103370034214SMatthew G. Knepley   PetscFunctionBegin;
103470034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
103570034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
103670034214SMatthew G. Knepley 
103770034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
103870034214SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
103970034214SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
104070034214SMatthew G. Knepley 
104170034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
10428a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
104370034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
104470034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
104570034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
104670034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
104770034214SMatthew G. Knepley   PetscFunctionReturn(0);
104870034214SMatthew G. Knepley }
104970034214SMatthew G. Knepley 
105024cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1051cc71bff1SMichael Lange {
1052cc71bff1SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
1053cc71bff1SMichael Lange   MPI_Comm               comm;
1054cc71bff1SMichael Lange   PetscSF                coneSF;
1055cc71bff1SMichael Lange   PetscSection           originalConeSection, newConeSection;
1056ac04eaf7SToby Isaac   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
1057cc71bff1SMichael Lange   PetscBool              flg;
1058cc71bff1SMichael Lange   PetscErrorCode         ierr;
1059cc71bff1SMichael Lange 
1060cc71bff1SMichael Lange   PetscFunctionBegin;
1061cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10620c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
1063cc71bff1SMichael Lange 
10640c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1065cc71bff1SMichael Lange   /* Distribute cone section */
1066cc71bff1SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1067cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
1068cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr);
1069cc71bff1SMichael Lange   ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
1070cc71bff1SMichael Lange   ierr = DMSetUp(dmParallel);CHKERRQ(ierr);
1071cc71bff1SMichael Lange   {
1072cc71bff1SMichael Lange     PetscInt pStart, pEnd, p;
1073cc71bff1SMichael Lange 
1074cc71bff1SMichael Lange     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
1075cc71bff1SMichael Lange     for (p = pStart; p < pEnd; ++p) {
1076cc71bff1SMichael Lange       PetscInt coneSize;
1077cc71bff1SMichael Lange       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
1078cc71bff1SMichael Lange       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
1079cc71bff1SMichael Lange     }
1080cc71bff1SMichael Lange   }
1081cc71bff1SMichael Lange   /* Communicate and renumber cones */
1082cc71bff1SMichael Lange   ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
10838a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1084cc71bff1SMichael Lange   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
1085ac04eaf7SToby Isaac   if (original) {
1086ac04eaf7SToby Isaac     PetscInt numCones;
1087ac04eaf7SToby Isaac 
1088ac04eaf7SToby Isaac     ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr); ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr);
1089ac04eaf7SToby Isaac     ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr);
1090ac04eaf7SToby Isaac   }
1091ac04eaf7SToby Isaac   else {
1092ac04eaf7SToby Isaac     globCones = cones;
1093ac04eaf7SToby Isaac   }
1094cc71bff1SMichael Lange   ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr);
1095ac04eaf7SToby Isaac   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1096ac04eaf7SToby Isaac   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1097ac04eaf7SToby Isaac   if (original) {
1098ac04eaf7SToby Isaac     ierr = PetscFree(globCones);CHKERRQ(ierr);
1099ac04eaf7SToby Isaac   }
1100cc71bff1SMichael Lange   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
1101cc71bff1SMichael Lange   ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
11023533c52bSMatthew G. Knepley #if PETSC_USE_DEBUG
11033533c52bSMatthew G. Knepley   {
11043533c52bSMatthew G. Knepley     PetscInt  p;
11053533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
11063533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
11073533c52bSMatthew G. Knepley       if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
11083533c52bSMatthew G. Knepley     }
11093533c52bSMatthew G. Knepley     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
11103533c52bSMatthew G. Knepley   }
11113533c52bSMatthew G. Knepley #endif
1112c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
1113cc71bff1SMichael Lange   if (flg) {
1114cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
1115cc71bff1SMichael Lange     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1116cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
1117cc71bff1SMichael Lange     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1118cc71bff1SMichael Lange     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
1119cc71bff1SMichael Lange   }
1120cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
1121cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr);
1122cc71bff1SMichael Lange   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1123cc71bff1SMichael Lange   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1124cc71bff1SMichael Lange   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
1125cc71bff1SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1126eaf898f9SPatrick Sanan   /* Create supports and stratify DMPlex */
1127cc71bff1SMichael Lange   {
1128cc71bff1SMichael Lange     PetscInt pStart, pEnd;
1129cc71bff1SMichael Lange 
1130cc71bff1SMichael Lange     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1131cc71bff1SMichael Lange     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1132cc71bff1SMichael Lange   }
1133cc71bff1SMichael Lange   ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr);
1134cc71bff1SMichael Lange   ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr);
11351cf84007SMatthew G. Knepley   {
11361cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
11371cf84007SMatthew G. Knepley 
11381cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
11391cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
11401cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
11411cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
11421cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
11431cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
11441cf84007SMatthew G. Knepley   }
1145cc71bff1SMichael Lange   PetscFunctionReturn(0);
1146cc71bff1SMichael Lange }
1147cc71bff1SMichael Lange 
114824cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
11490df0e737SMichael Lange {
11500df0e737SMichael Lange   MPI_Comm         comm;
11510df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
11520df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
11530df0e737SMichael Lange   PetscInt         bs;
115490b157c4SStefano Zampini   PetscBool        isper;
11550df0e737SMichael Lange   const char      *name;
11560df0e737SMichael Lange   const PetscReal *maxCell, *L;
11575dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
11580df0e737SMichael Lange   PetscErrorCode   ierr;
11590df0e737SMichael Lange 
11600df0e737SMichael Lange   PetscFunctionBegin;
11610df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11620c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
11630df0e737SMichael Lange 
11640df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
11650df0e737SMichael Lange   ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
11660df0e737SMichael Lange   ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr);
11670df0e737SMichael Lange   ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
11680df0e737SMichael Lange   if (originalCoordinates) {
11698b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
11700df0e737SMichael Lange     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
11710df0e737SMichael Lange     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
11720df0e737SMichael Lange 
11730df0e737SMichael Lange     ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
11740df0e737SMichael Lange     ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr);
11750df0e737SMichael Lange     ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr);
11760df0e737SMichael Lange     ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr);
11770df0e737SMichael Lange     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
11780df0e737SMichael Lange   }
117990b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
118090b157c4SStefano Zampini   ierr = DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);CHKERRQ(ierr);
11810df0e737SMichael Lange   PetscFunctionReturn(0);
11820df0e737SMichael Lange }
11830df0e737SMichael Lange 
1184d995df53SMatthew G. Knepley /* Here we are assuming that process 0 always has everything */
118524cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
11860df0e737SMichael Lange {
1187df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex*) dm->data;
11880df0e737SMichael Lange   MPI_Comm         comm;
11897980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
11900df0e737SMichael Lange   PetscMPIInt      rank;
11917980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1192df0420ecSMatthew G. Knepley   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1193df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
11940df0e737SMichael Lange   PetscErrorCode   ierr;
11950df0e737SMichael Lange 
11960df0e737SMichael Lange   PetscFunctionBegin;
11970df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11980c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
11990c86c063SLisandro Dalcin 
12000df0e737SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
12010df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
12020df0e737SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
12030df0e737SMichael Lange 
1204df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
12057980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
12067980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
12077980c9d4SMatthew G. Knepley   if (depthLabel) {ierr = DMLabelGetState(depthLabel, &depthState);CHKERRQ(ierr);}
1208df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1209b2566f29SBarry Smith   ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
1210df0420ecSMatthew G. Knepley   if (sendDepth) {
12117980c9d4SMatthew G. Knepley     ierr = DMRemoveLabel(dmParallel, "depth", &depthLabel);CHKERRQ(ierr);
12127980c9d4SMatthew G. Knepley     ierr = DMLabelDestroy(&depthLabel);CHKERRQ(ierr);
1213df0420ecSMatthew G. Knepley   }
1214d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
1215c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr);
1216d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
12170df0e737SMichael Lange   ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1218627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1219bdd2d751SMichael Lange   for (l = numLabels-1; l >= 0; --l) {
1220bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
122183e10cb3SLisandro Dalcin     PetscBool   isDepth, lisOutput = PETSC_TRUE, isOutput;
12220df0e737SMichael Lange 
122383e10cb3SLisandro Dalcin     if (hasLabels) {ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);}
12240df0e737SMichael Lange     /* Skip "depth" because it is recreated */
122583e10cb3SLisandro Dalcin     if (hasLabels) {ierr = PetscStrcmp(label->name, "depth", &isDepth);CHKERRQ(ierr);}
122683e10cb3SLisandro Dalcin     ierr = MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
122783e10cb3SLisandro Dalcin     if (isDepth && !sendDepth) continue;
1228bdd2d751SMichael Lange     ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr);
122983e10cb3SLisandro Dalcin     if (isDepth) {
12307980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
12317980c9d4SMatthew G. Knepley       PetscInt gdepth;
12327980c9d4SMatthew G. Knepley 
12337980c9d4SMatthew G. Knepley       ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
12347980c9d4SMatthew G. Knepley       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
12357980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
12367980c9d4SMatthew G. Knepley         PetscBool has;
12377980c9d4SMatthew G. Knepley 
12387980c9d4SMatthew G. Knepley         ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr);
123983e10cb3SLisandro Dalcin         if (!has) {ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);}
12407980c9d4SMatthew G. Knepley       }
12417980c9d4SMatthew G. Knepley     }
1242c58f1c22SToby Isaac     ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr);
124383e10cb3SLisandro Dalcin     /* Put the output flag in the new label */
124483e10cb3SLisandro Dalcin     if (hasLabels) {ierr = DMGetLabelOutput(dm, label->name, &lisOutput);CHKERRQ(ierr);}
124583e10cb3SLisandro Dalcin     ierr = MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
124683e10cb3SLisandro Dalcin     ierr = DMSetLabelOutput(dmParallel, labelNew->name, isOutput);CHKERRQ(ierr);
12470df0e737SMichael Lange   }
12480df0e737SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
12490df0e737SMichael Lange   PetscFunctionReturn(0);
12500df0e737SMichael Lange }
12510df0e737SMichael Lange 
125224cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
12539734c634SMichael Lange {
12549734c634SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
12559734c634SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1256f0e73a3dSToby Isaac   PetscBool      *isHybrid, *isHybridParallel;
1257f0e73a3dSToby Isaac   PetscInt        dim, depth, d;
1258f0e73a3dSToby Isaac   PetscInt        pStart, pEnd, pStartP, pEndP;
12599734c634SMichael Lange   PetscErrorCode  ierr;
12609734c634SMichael Lange 
12619734c634SMichael Lange   PetscFunctionBegin;
12629734c634SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12630c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
12649734c634SMichael Lange 
12659734c634SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12669734c634SMichael Lange   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1267f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1268f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dmParallel,&pStartP,&pEndP);CHKERRQ(ierr);
1269f0e73a3dSToby Isaac   ierr = PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);CHKERRQ(ierr);
1270f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1271f0e73a3dSToby Isaac     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];
12729734c634SMichael Lange 
1273f0e73a3dSToby Isaac     if (hybridMax >= 0) {
1274f0e73a3dSToby Isaac       PetscInt sStart, sEnd, p;
12759734c634SMichael Lange 
1276f0e73a3dSToby Isaac       ierr = DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);CHKERRQ(ierr);
1277f0e73a3dSToby Isaac       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
12789734c634SMichael Lange     }
12799734c634SMichael Lange   }
1280f0e73a3dSToby Isaac   ierr = PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1281f0e73a3dSToby Isaac   ierr = PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1282f0e73a3dSToby Isaac   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1283f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1284f0e73a3dSToby Isaac     PetscInt sStart, sEnd, p, dd;
1285f0e73a3dSToby Isaac 
1286f0e73a3dSToby Isaac     ierr = DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);CHKERRQ(ierr);
1287f0e73a3dSToby Isaac     dd = (depth == 1 && d == 1) ? dim : d;
1288f0e73a3dSToby Isaac     for (p = sStart; p < sEnd; p++) {
1289f0e73a3dSToby Isaac       if (isHybridParallel[p-pStartP]) {
1290f0e73a3dSToby Isaac         pmesh->hybridPointMax[dd] = p;
1291f0e73a3dSToby Isaac         break;
1292f0e73a3dSToby Isaac       }
1293f0e73a3dSToby Isaac     }
1294f0e73a3dSToby Isaac   }
1295f0e73a3dSToby Isaac   ierr = PetscFree2(isHybrid,isHybridParallel);CHKERRQ(ierr);
12969734c634SMichael Lange   PetscFunctionReturn(0);
12979734c634SMichael Lange }
12980df0e737SMichael Lange 
129924cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1300a6f36705SMichael Lange {
130115078cd4SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
130215078cd4SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1303a6f36705SMichael Lange   MPI_Comm        comm;
1304a6f36705SMichael Lange   DM              refTree;
1305a6f36705SMichael Lange   PetscSection    origParentSection, newParentSection;
1306a6f36705SMichael Lange   PetscInt        *origParents, *origChildIDs;
1307a6f36705SMichael Lange   PetscBool       flg;
1308a6f36705SMichael Lange   PetscErrorCode  ierr;
1309a6f36705SMichael Lange 
1310a6f36705SMichael Lange   PetscFunctionBegin;
1311a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13120c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5);
1313a6f36705SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1314a6f36705SMichael Lange 
1315a6f36705SMichael Lange   /* Set up tree */
1316a6f36705SMichael Lange   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1317a6f36705SMichael Lange   ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr);
1318a6f36705SMichael Lange   ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr);
1319a6f36705SMichael Lange   if (origParentSection) {
1320a6f36705SMichael Lange     PetscInt        pStart, pEnd;
132108633170SToby Isaac     PetscInt        *newParents, *newChildIDs, *globParents;
1322a6f36705SMichael Lange     PetscInt        *remoteOffsetsParents, newParentSize;
1323a6f36705SMichael Lange     PetscSF         parentSF;
1324a6f36705SMichael Lange 
1325a6f36705SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
1326a6f36705SMichael Lange     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr);
1327a6f36705SMichael Lange     ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr);
1328a6f36705SMichael Lange     ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr);
1329a6f36705SMichael Lange     ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr);
13308a713f3bSLawrence Mitchell     ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr);
1331a6f36705SMichael Lange     ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr);
1332a6f36705SMichael Lange     ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr);
133308633170SToby Isaac     if (original) {
133408633170SToby Isaac       PetscInt numParents;
133508633170SToby Isaac 
133608633170SToby Isaac       ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr);
133708633170SToby Isaac       ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr);
133808633170SToby Isaac       ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr);
133908633170SToby Isaac     }
134008633170SToby Isaac     else {
134108633170SToby Isaac       globParents = origParents;
134208633170SToby Isaac     }
134308633170SToby Isaac     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
134408633170SToby Isaac     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
134508633170SToby Isaac     if (original) {
134608633170SToby Isaac       ierr = PetscFree(globParents);CHKERRQ(ierr);
134708633170SToby Isaac     }
1348a6f36705SMichael Lange     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1349a6f36705SMichael Lange     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1350a6f36705SMichael Lange     ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr);
13514a54e071SToby Isaac #if PETSC_USE_DEBUG
13524a54e071SToby Isaac     {
13534a54e071SToby Isaac       PetscInt  p;
13544a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
13554a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
13564a54e071SToby Isaac         if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
13574a54e071SToby Isaac       }
13584a54e071SToby Isaac       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
13594a54e071SToby Isaac     }
13604a54e071SToby Isaac #endif
1361c5929fdfSBarry Smith     ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr);
1362a6f36705SMichael Lange     if (flg) {
1363a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr);
1364a6f36705SMichael Lange       ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1365a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr);
1366a6f36705SMichael Lange       ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1367a6f36705SMichael Lange       ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr);
1368a6f36705SMichael Lange     }
1369a6f36705SMichael Lange     ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr);
1370a6f36705SMichael Lange     ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr);
1371a6f36705SMichael Lange     ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr);
1372a6f36705SMichael Lange     ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr);
1373a6f36705SMichael Lange   }
137415078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1375a6f36705SMichael Lange   PetscFunctionReturn(0);
1376a6f36705SMichael Lange }
13770df0e737SMichael Lange 
137824cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
13794eca1733SMichael Lange {
13809852e123SBarry Smith   PetscMPIInt            rank, size;
13814eca1733SMichael Lange   MPI_Comm               comm;
13824eca1733SMichael Lange   PetscErrorCode         ierr;
13834eca1733SMichael Lange 
13844eca1733SMichael Lange   PetscFunctionBegin;
13854eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13860c86c063SLisandro Dalcin   PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3);
13874eca1733SMichael Lange 
13884eca1733SMichael Lange   /* Create point SF for parallel mesh */
13894eca1733SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13904eca1733SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
13914eca1733SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
13929852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
13934eca1733SMichael Lange   {
13944eca1733SMichael Lange     const PetscInt *leaves;
13954eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
13964eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
13974eca1733SMichael Lange     PetscInt        pStart, pEnd;
13984eca1733SMichael Lange 
13994eca1733SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
14004eca1733SMichael Lange     ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
14014eca1733SMichael Lange     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
14024eca1733SMichael Lange     for (p=0; p<numRoots; p++) {
14034eca1733SMichael Lange       rowners[p].rank  = -1;
14044eca1733SMichael Lange       rowners[p].index = -1;
14054eca1733SMichael Lange     }
14064eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14074eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14084eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14094eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
14104eca1733SMichael Lange         lowners[p].rank  = rank;
14114eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
14124eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
14134eca1733SMichael Lange         lowners[p].rank  = -2;
14144eca1733SMichael Lange         lowners[p].index = -2;
14154eca1733SMichael Lange       }
14164eca1733SMichael Lange     }
14174eca1733SMichael Lange     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
14184eca1733SMichael Lange       rowners[p].rank  = -3;
14194eca1733SMichael Lange       rowners[p].index = -3;
14204eca1733SMichael Lange     }
14214eca1733SMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14224eca1733SMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14234eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14244eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14254eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14264eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
14274eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
14284eca1733SMichael Lange     }
14294eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr);
14304eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
14314eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
14324eca1733SMichael Lange       if (lowners[p].rank != rank) {
14334eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
14344eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
14354eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
14364eca1733SMichael Lange         ++gp;
14374eca1733SMichael Lange       }
14384eca1733SMichael Lange     }
14394eca1733SMichael Lange     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
14404eca1733SMichael Lange     ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
14414eca1733SMichael Lange     ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr);
14424eca1733SMichael Lange   }
14431cf84007SMatthew G. Knepley   {
14441cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
14451cf84007SMatthew G. Knepley 
14461cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
14471cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
14481cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
14491cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
14501cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
14511cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
14521cf84007SMatthew G. Knepley   }
14534eca1733SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
14544eca1733SMichael Lange   PetscFunctionReturn(0);
14554eca1733SMichael Lange }
14564eca1733SMichael Lange 
1457f5bf2dbfSMichael Lange /*@C
1458f5bf2dbfSMichael Lange   DMPlexDerivePointSF - Build a point SF from an SF describing a point migration
1459f5bf2dbfSMichael Lange 
1460f5bf2dbfSMichael Lange   Input Parameter:
1461f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1462f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
14631627f6ccSMichael Lange . ownership   - Flag causing a vote to determine point ownership
1464f5bf2dbfSMichael Lange 
1465f5bf2dbfSMichael Lange   Output Parameter:
1466f5bf2dbfSMichael Lange - pointSF     - The star forest describing the point overlap in the remapped DM
1467f5bf2dbfSMichael Lange 
1468f5bf2dbfSMichael Lange   Level: developer
1469f5bf2dbfSMichael Lange 
1470f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1471f5bf2dbfSMichael Lange @*/
1472f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1473f5bf2dbfSMichael Lange {
1474*82effdf1SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex *) dm->data;
1475*82effdf1SMatthew G. Knepley   PetscMPIInt        rank, size;
14761627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1477f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1478f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1479f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1480f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1481*82effdf1SMatthew G. Knepley   Vec                shifts;
1482*82effdf1SMatthew G. Knepley   const PetscInt     numShifts = 37; /* TODO Use larger prime */
1483*82effdf1SMatthew G. Knepley   const PetscScalar *shift = NULL;
1484*82effdf1SMatthew G. Knepley   const PetscBool    shiftDebug = PETSC_FALSE;
1485f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1486f5bf2dbfSMichael Lange 
1487f5bf2dbfSMichael Lange   PetscFunctionBegin;
1488f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1489f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1490*82effdf1SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
1491f5bf2dbfSMichael Lange 
1492f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1493f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1494f5bf2dbfSMichael Lange   if (ownership) {
1495*82effdf1SMatthew G. Knepley     /* If balancing, we compute a random cyclic shift of the rank for each remote point. That way, the max will evenly distribute among ranks. */
1496*82effdf1SMatthew G. Knepley     if (mesh->partitionBalance) {
1497*82effdf1SMatthew G. Knepley       PetscRandom r;
1498*82effdf1SMatthew G. Knepley 
1499*82effdf1SMatthew G. Knepley       ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
1500*82effdf1SMatthew G. Knepley       ierr = PetscRandomSetInterval(r, 0, 17*size);CHKERRQ(ierr);
1501*82effdf1SMatthew G. Knepley       ierr = VecCreate(PETSC_COMM_SELF, &shifts);CHKERRQ(ierr);
1502*82effdf1SMatthew G. Knepley       ierr = VecSetSizes(shifts, numShifts, numShifts);CHKERRQ(ierr);
1503*82effdf1SMatthew G. Knepley       ierr = VecSetType(shifts, VECSTANDARD);CHKERRQ(ierr);
1504*82effdf1SMatthew G. Knepley       ierr = VecSetRandom(shifts, r);CHKERRQ(ierr);
1505*82effdf1SMatthew G. Knepley       ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
1506*82effdf1SMatthew G. Knepley       ierr = VecGetArrayRead(shifts, &shift);CHKERRQ(ierr);
1507*82effdf1SMatthew G. Knepley     }
1508*82effdf1SMatthew G. Knepley 
1509*82effdf1SMatthew G. Knepley     /* Point ownership vote: Process with highest rank owns shared points */
1510f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
1511*82effdf1SMatthew G. Knepley       if (shiftDebug) {
1512*82effdf1SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d] Point %D RemotePoint %D Shift %D MyRank %D\n", rank, leaves ? leaves[p] : p, roots[p].index, (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]), (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size);CHKERRQ(ierr);
1513*82effdf1SMatthew G. Knepley       }
1514f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1515*82effdf1SMatthew G. Knepley       leafNodes[p].rank  = (rank + (shift ? (PetscInt) shift[roots[p].index%numShifts] : 0))%size;
151643f7d02bSMichael Lange       leafNodes[p].index = p;
1517f5bf2dbfSMichael Lange     }
1518f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
15191627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1520f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1521f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1522f5bf2dbfSMichael Lange     }
1523f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1524f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1525f5bf2dbfSMichael Lange   } else {
1526f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1527f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1528f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1529f5bf2dbfSMichael Lange     };
1530f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1531f5bf2dbfSMichael Lange       /* Write new local id into old location */
1532f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
15336462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1534f5bf2dbfSMichael Lange       }
1535f5bf2dbfSMichael Lange     }
1536f5bf2dbfSMichael Lange   }
1537f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1538f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1539f5bf2dbfSMichael Lange 
1540*82effdf1SMatthew G. Knepley   for (npointLeaves = 0, p = 0; p < nleaves; p++) {
1541*82effdf1SMatthew G. Knepley     if (shiftDebug) {
1542*82effdf1SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d] Root %D, Rank %D MyRank %D\n", rank, roots[p].index, leafNodes[p].rank, (rank + (shift ? (PetscInt) shift[roots[p].index%numShifts] : 0))%size);CHKERRQ(ierr);
1543*82effdf1SMatthew G. Knepley     }
1544*82effdf1SMatthew G. Knepley     if (leafNodes[p].rank != (rank + (shift ? (PetscInt) shift[roots[p].index%numShifts] : 0))%size) npointLeaves++;
1545*82effdf1SMatthew G. Knepley   }
15461627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
15471627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1548f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1549*82effdf1SMatthew G. Knepley     if (leafNodes[p].rank != (rank + (shift ? (PetscInt) shift[roots[p].index%numShifts] : 0))%size) {
1550f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1551f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1552f5bf2dbfSMichael Lange       idx++;
1553f5bf2dbfSMichael Lange     }
1554f5bf2dbfSMichael Lange   }
1555*82effdf1SMatthew G. Knepley   if (shift) {
1556*82effdf1SMatthew G. Knepley     ierr = VecRestoreArrayRead(shifts, &shift);CHKERRQ(ierr);
1557*82effdf1SMatthew G. Knepley     ierr = VecDestroy(&shifts);CHKERRQ(ierr);
1558*82effdf1SMatthew G. Knepley   }
1559*82effdf1SMatthew G. Knepley   if (shiftDebug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), PETSC_STDOUT);CHKERRQ(ierr);}
1560f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
15611627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1562f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1563f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1564f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1565f5bf2dbfSMichael Lange }
1566f5bf2dbfSMichael Lange 
156715078cd4SMichael Lange /*@C
156815078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
156915078cd4SMichael Lange 
157015078cd4SMichael Lange   Input Parameter:
157115078cd4SMichael Lange + dm       - The source DMPlex object
15721627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
157315078cd4SMichael Lange 
157415078cd4SMichael Lange   Output Parameter:
157515078cd4SMichael Lange - targetDM - The target DMPlex object
157615078cd4SMichael Lange 
1577b9f40539SMichael Lange   Level: intermediate
157815078cd4SMichael Lange 
157915078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
158015078cd4SMichael Lange @*/
1581b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
158215078cd4SMichael Lange {
1583b9f40539SMichael Lange   MPI_Comm               comm;
1584cc1750acSStefano Zampini   PetscInt               dim, cdim, nroots;
1585b9f40539SMichael Lange   PetscSF                sfPoint;
158615078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1587ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1588b9f40539SMichael Lange   PetscBool              flg;
158915078cd4SMichael Lange   PetscErrorCode         ierr;
159015078cd4SMichael Lange 
159115078cd4SMichael Lange   PetscFunctionBegin;
159215078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15931b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1594b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
159515078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
159615078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
1597cc1750acSStefano Zampini   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1598cc1750acSStefano Zampini   ierr = DMSetCoordinateDim(targetDM, cdim);CHKERRQ(ierr);
159915078cd4SMichael Lange 
1600bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1601b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1602b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1603bfb0467fSMichael Lange   if (nroots >= 0) {
1604b9f40539SMichael Lange     IS                     isOriginal;
1605ac04eaf7SToby Isaac     PetscInt               n, size, nleaves;
1606ac04eaf7SToby Isaac     PetscInt              *numbering_orig, *numbering_new;
1607b9f40539SMichael Lange     /* Get the original point numbering */
1608b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1609b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1610b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1611b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1612b9f40539SMichael Lange     /* Convert to positive global numbers */
1613b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1614b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1615b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1616b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1617b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1618b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1619b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1620b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1621b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
162215078cd4SMichael Lange   } else {
1623bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
162415078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
162515078cd4SMichael Lange   }
1626c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1627b9f40539SMichael Lange   if (flg) {
1628b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1629b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1630b9f40539SMichael Lange   }
163115078cd4SMichael Lange   /* Migrate DM data to target DM */
1632ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
163315078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
16340dc1530dSSander Arens   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
163515078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
163608633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1637ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1638302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
16391b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
164015078cd4SMichael Lange   PetscFunctionReturn(0);
164115078cd4SMichael Lange }
164215078cd4SMichael Lange 
16433b8f15a2SMatthew G. Knepley /*@C
164470034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
164570034214SMatthew G. Knepley 
164670034214SMatthew G. Knepley   Not Collective
164770034214SMatthew G. Knepley 
164870034214SMatthew G. Knepley   Input Parameter:
164970034214SMatthew G. Knepley + dm  - The original DMPlex object
165070034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
165170034214SMatthew G. Knepley 
165270034214SMatthew G. Knepley   Output Parameter:
165370034214SMatthew G. Knepley + sf - The PetscSF used for point distribution
165470034214SMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
165570034214SMatthew G. Knepley 
165670034214SMatthew G. Knepley   Note: If the mesh was not distributed, the return value is NULL.
165770034214SMatthew G. Knepley 
165893a1dc33SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMPlexSetAdjacencyUseCone() and
165970034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
166070034214SMatthew G. Knepley   representation on the mesh.
166170034214SMatthew G. Knepley 
166270034214SMatthew G. Knepley   Level: intermediate
166370034214SMatthew G. Knepley 
166470034214SMatthew G. Knepley .keywords: mesh, elements
166570034214SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
166670034214SMatthew G. Knepley @*/
166780cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
166870034214SMatthew G. Knepley {
166970034214SMatthew G. Knepley   MPI_Comm               comm;
167015078cd4SMichael Lange   PetscPartitioner       partitioner;
1671f8987ae8SMichael Lange   IS                     cellPart;
1672f8987ae8SMichael Lange   PetscSection           cellPartSection;
1673cf86098cSMatthew G. Knepley   DM                     dmCoord;
1674f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
167543f7d02bSMichael Lange   PetscSF                sfProcess, sfMigration, sfStratified, sfPoint;
167670034214SMatthew G. Knepley   PetscBool              flg;
16779852e123SBarry Smith   PetscMPIInt            rank, size, p;
167870034214SMatthew G. Knepley   PetscErrorCode         ierr;
167970034214SMatthew G. Knepley 
168070034214SMatthew G. Knepley   PetscFunctionBegin;
168170034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
168270034214SMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
168370034214SMatthew G. Knepley   PetscValidPointer(dmParallel,5);
168470034214SMatthew G. Knepley 
16850c86c063SLisandro Dalcin   if (sf) *sf = NULL;
16860c86c063SLisandro Dalcin   *dmParallel = NULL;
168770034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
168870034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
16899852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
16900c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
169170034214SMatthew G. Knepley 
16920c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
169315078cd4SMichael Lange   /* Create cell partition */
169467eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
169577623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
169615078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
169715078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1698f8987ae8SMichael Lange   {
1699f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1700f8987ae8SMichael Lange     PetscInt proc, pStart, pEnd, npoints, poffset;
1701f8987ae8SMichael Lange     const PetscInt *points;
1702f8987ae8SMichael Lange     ierr = DMLabelCreate("Point Partition", &lblPartition);CHKERRQ(ierr);
1703f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1704f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1705f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1706f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1707f8987ae8SMichael Lange       ierr = PetscSectionGetOffset(cellPartSection, proc, &poffset);CHKERRQ(ierr);
1708f8987ae8SMichael Lange       for (p = poffset; p < poffset+npoints; p++) {
1709f8987ae8SMichael Lange         ierr = DMLabelSetValue(lblPartition, points[p], proc);CHKERRQ(ierr);
171070034214SMatthew G. Knepley       }
1711f8987ae8SMichael Lange     }
1712f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1713f8987ae8SMichael Lange   }
1714f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelClosure(dm, lblPartition);CHKERRQ(ierr);
1715f8987ae8SMichael Lange   {
1716f8987ae8SMichael Lange     /* Build a global process SF */
1717f8987ae8SMichael Lange     PetscSFNode *remoteProc;
17189852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
17199852e123SBarry Smith     for (p = 0; p < size; ++p) {
1720f8987ae8SMichael Lange       remoteProc[p].rank  = p;
1721f8987ae8SMichael Lange       remoteProc[p].index = rank;
1722f8987ae8SMichael Lange     }
1723f8987ae8SMichael Lange     ierr = PetscSFCreate(comm, &sfProcess);CHKERRQ(ierr);
1724f8987ae8SMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProcess, "Process SF");CHKERRQ(ierr);
17259852e123SBarry Smith     ierr = PetscSFSetGraph(sfProcess, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
1726f8987ae8SMichael Lange   }
1727f8987ae8SMichael Lange   ierr = DMLabelCreate("Point migration", &lblMigration);CHKERRQ(ierr);
1728f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, sfProcess, lblMigration);CHKERRQ(ierr);
1729302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
173043f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
173143f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
173243f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
173343f7d02bSMichael Lange   sfMigration = sfStratified;
173467eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1735c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
173670034214SMatthew G. Knepley   if (flg) {
1737f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1738f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
173970034214SMatthew G. Knepley   }
1740f8987ae8SMichael Lange 
174115078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
174270034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
174370034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1744b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
174570034214SMatthew G. Knepley 
1746a157612eSMichael Lange   /* Build the point SF without overlap */
1747fef8a7d2SMatthew G. Knepley   ((DM_Plex*) (*dmParallel)->data)->partitionBalance = ((DM_Plex*) dm->data)->partitionBalance;
17481627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1749f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1750cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1751cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1752f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
175370034214SMatthew G. Knepley 
1754a157612eSMichael Lange   if (overlap > 0) {
175515078cd4SMichael Lange     DM                 dmOverlap;
175615078cd4SMichael Lange     PetscInt           nroots, nleaves;
175715078cd4SMichael Lange     PetscSFNode       *newRemote;
175815078cd4SMichael Lange     const PetscSFNode *oldRemote;
175915078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1760a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1761b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1762a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1763a157612eSMichael Lange     *dmParallel = dmOverlap;
1764c389ea9fSToby Isaac     if (flg) {
17653d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
176615078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1767c389ea9fSToby Isaac     }
176843331d4aSMichael Lange 
1769f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1770f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
177115078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
177243331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
177315078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
177415078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
177515078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
177615078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
177715078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1778f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
177915078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1780c389ea9fSToby Isaac   }
1781bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1782f8987ae8SMichael Lange   ierr = PetscSFDestroy(&sfProcess);CHKERRQ(ierr);
1783f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1784f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
17854eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
17864eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1787721cbd76SMatthew G. Knepley   /* Copy BC */
1788a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
178966fe0bfeSMatthew G. Knepley   /* Create sfNatural */
179066fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
179166fe0bfeSMatthew G. Knepley     PetscSection section;
179266fe0bfeSMatthew G. Knepley 
179366fe0bfeSMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
17940c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
17950e663481SBlaise Bourdin     ierr = DMSetUseNatural(*dmParallel, PETSC_TRUE);CHKERRQ(ierr);
179666fe0bfeSMatthew G. Knepley   }
1797721cbd76SMatthew G. Knepley   /* Cleanup */
1798f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
179956d056acSSatish Balay   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1800f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
180170034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
180270034214SMatthew G. Knepley   PetscFunctionReturn(0);
180370034214SMatthew G. Knepley }
1804a157612eSMichael Lange 
1805a157612eSMichael Lange /*@C
180624cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1807a157612eSMichael Lange 
1808a157612eSMichael Lange   Not Collective
1809a157612eSMichael Lange 
1810a157612eSMichael Lange   Input Parameter:
1811a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
18120c86c063SLisandro Dalcin - overlap - The overlap of partitions
1813a157612eSMichael Lange 
1814a157612eSMichael Lange   Output Parameter:
1815a157612eSMichael Lange + sf - The PetscSF used for point distribution
1816a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1817a157612eSMichael Lange 
1818a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1819a157612eSMichael Lange 
1820a157612eSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
1821a157612eSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
1822a157612eSMichael Lange   representation on the mesh.
1823a157612eSMichael Lange 
1824a157612eSMichael Lange   Level: intermediate
1825a157612eSMichael Lange 
1826a157612eSMichael Lange .keywords: mesh, elements
1827a157612eSMichael Lange .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
1828a157612eSMichael Lange @*/
1829b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1830a157612eSMichael Lange {
1831a157612eSMichael Lange   MPI_Comm               comm;
18323567eaeeSMatthew G. Knepley   PetscMPIInt            size, rank;
1833a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1834a157612eSMichael Lange   IS                     rootrank, leafrank;
1835cf86098cSMatthew G. Knepley   DM                     dmCoord;
1836a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1837f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1838a157612eSMichael Lange   PetscErrorCode         ierr;
1839a157612eSMichael Lange 
1840a157612eSMichael Lange   PetscFunctionBegin;
1841a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1842a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1843a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1844a157612eSMichael Lange 
18450c86c063SLisandro Dalcin   if (sf) *sf = NULL;
18460c86c063SLisandro Dalcin   *dmOverlap  = NULL;
1847a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
18483567eaeeSMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1849a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
18500c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
1851a157612eSMichael Lange 
18520c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1853a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
185467eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1855a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1856a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1857a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1858a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1859a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1860a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1861a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1862a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1863a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1864a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1865a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1866a9f1d5b2SMichael Lange 
186715fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
186815fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
186915fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
187015fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
187167eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1872a157612eSMichael Lange 
1873a157612eSMichael Lange   /* Build the overlapping DM */
1874a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1875a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1876a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1877f5bf2dbfSMichael Lange   /* Build the new point SF */
18781627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1879f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1880cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1881cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1882f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1883a157612eSMichael Lange   /* Cleanup overlap partition */
1884a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1885a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1886a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
18871b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1888a157612eSMichael Lange   PetscFunctionReturn(0);
1889a157612eSMichael Lange }
18906462276dSToby Isaac 
18916462276dSToby Isaac /*@C
18926462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
18936462276dSToby Isaac   root process of the original's communicator.
18946462276dSToby Isaac 
18956462276dSToby Isaac   Input Parameters:
18966462276dSToby Isaac . dm - the original DMPlex object
18976462276dSToby Isaac 
18986462276dSToby Isaac   Output Parameters:
18996462276dSToby Isaac . gatherMesh - the gathered DM object, or NULL
19006462276dSToby Isaac 
19016462276dSToby Isaac   Level: intermediate
19026462276dSToby Isaac 
19036462276dSToby Isaac .keywords: mesh
19046462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
19056462276dSToby Isaac @*/
19066462276dSToby Isaac PetscErrorCode DMPlexGetGatherDM(DM dm, DM *gatherMesh)
19076462276dSToby Isaac {
19086462276dSToby Isaac   MPI_Comm       comm;
19096462276dSToby Isaac   PetscMPIInt    size;
19106462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
19116462276dSToby Isaac   PetscErrorCode ierr;
19126462276dSToby Isaac 
19136462276dSToby Isaac   PetscFunctionBegin;
19146462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19150c86c063SLisandro Dalcin   PetscValidPointer(gatherMesh,2);
19160c86c063SLisandro Dalcin   *gatherMesh = NULL;
19176462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
19186462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19196462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
19206462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
19216462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
19226462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
19236462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
19246462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
19256462276dSToby Isaac   ierr = DMPlexDistribute(dm,0,NULL,gatherMesh);CHKERRQ(ierr);
19266462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
19276462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
19286462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
19296462276dSToby Isaac   PetscFunctionReturn(0);
19306462276dSToby Isaac }
19316462276dSToby Isaac 
19326462276dSToby Isaac /*@C
19336462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
19346462276dSToby Isaac 
19356462276dSToby Isaac   Input Parameters:
19366462276dSToby Isaac . dm - the original DMPlex object
19376462276dSToby Isaac 
19386462276dSToby Isaac   Output Parameters:
19396462276dSToby Isaac . redundantMesh - the redundant DM object, or NULL
19406462276dSToby Isaac 
19416462276dSToby Isaac   Level: intermediate
19426462276dSToby Isaac 
19436462276dSToby Isaac .keywords: mesh
19446462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
19456462276dSToby Isaac @*/
19466462276dSToby Isaac PetscErrorCode DMPlexGetRedundantDM(DM dm, DM *redundantMesh)
19476462276dSToby Isaac {
19486462276dSToby Isaac   MPI_Comm       comm;
19496462276dSToby Isaac   PetscMPIInt    size, rank;
19506462276dSToby Isaac   PetscInt       pStart, pEnd, p;
19516462276dSToby Isaac   PetscInt       numPoints = -1;
19526462276dSToby Isaac   PetscSF        migrationSF, sfPoint;
19536462276dSToby Isaac   DM             gatherDM, dmCoord;
19546462276dSToby Isaac   PetscSFNode    *points;
19556462276dSToby Isaac   PetscErrorCode ierr;
19566462276dSToby Isaac 
19576462276dSToby Isaac   PetscFunctionBegin;
19586462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19590c86c063SLisandro Dalcin   PetscValidPointer(redundantMesh,2);
19600c86c063SLisandro Dalcin   *redundantMesh = NULL;
19616462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
19626462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
196368dbc166SMatthew G. Knepley   if (size == 1) {
196468dbc166SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
196568dbc166SMatthew G. Knepley     *redundantMesh = dm;
196668dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
196768dbc166SMatthew G. Knepley   }
19686462276dSToby Isaac   ierr = DMPlexGetGatherDM(dm,&gatherDM);CHKERRQ(ierr);
19696462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
19706462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
19716462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
19726462276dSToby Isaac   numPoints = pEnd - pStart;
19736462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
19746462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
19756462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
19766462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
19776462276dSToby Isaac     points[p].index = p;
19786462276dSToby Isaac     points[p].rank  = 0;
19796462276dSToby Isaac   }
19806462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
19816462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
19826462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
19836462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
19846462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
19856462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
19866462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
19876462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
19886462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
19896462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
19906462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
19916462276dSToby Isaac   PetscFunctionReturn(0);
19926462276dSToby Isaac }
1993