xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision fef8a7d2fedf2f44b3883e5ff4b3df3e69e8cfa7)
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 {
1474f5bf2dbfSMichael Lange   PetscMPIInt        rank;
14751627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1476f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1477f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1478f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1479f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1480f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1481f5bf2dbfSMichael Lange 
1482f5bf2dbfSMichael Lange   PetscFunctionBegin;
1483f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1484f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1485f5bf2dbfSMichael Lange 
1486f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1487f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1488f5bf2dbfSMichael Lange   if (ownership) {
14891627f6ccSMichael Lange     /* Point ownership vote: Process with highest rank ownes shared points */
1490f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
1491f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1492f5bf2dbfSMichael Lange       leafNodes[p].rank  = rank;
149343f7d02bSMichael Lange       leafNodes[p].index = p;
1494f5bf2dbfSMichael Lange     }
1495f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
14961627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1497f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1498f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1499f5bf2dbfSMichael Lange     }
1500f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1501f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1502f5bf2dbfSMichael Lange   } else {
1503f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1504f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1505f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1506f5bf2dbfSMichael Lange     };
1507f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1508f5bf2dbfSMichael Lange       /* Write new local id into old location */
1509f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
15106462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1511f5bf2dbfSMichael Lange       }
1512f5bf2dbfSMichael Lange     }
1513f5bf2dbfSMichael Lange   }
1514f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1515f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1516f5bf2dbfSMichael Lange 
1517f5bf2dbfSMichael Lange   for (npointLeaves = 0, p = 0; p < nleaves; p++) {if (leafNodes[p].rank != rank) npointLeaves++;}
15181627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
15191627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1520f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1521f5bf2dbfSMichael Lange     if (leafNodes[p].rank != rank) {
1522f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1523f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1524f5bf2dbfSMichael Lange       idx++;
1525f5bf2dbfSMichael Lange     }
1526f5bf2dbfSMichael Lange   }
1527f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
15281627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1529f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1530f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1531f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1532f5bf2dbfSMichael Lange }
1533f5bf2dbfSMichael Lange 
153415078cd4SMichael Lange /*@C
153515078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
153615078cd4SMichael Lange 
153715078cd4SMichael Lange   Input Parameter:
153815078cd4SMichael Lange + dm       - The source DMPlex object
15391627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
154015078cd4SMichael Lange 
154115078cd4SMichael Lange   Output Parameter:
154215078cd4SMichael Lange - targetDM - The target DMPlex object
154315078cd4SMichael Lange 
1544b9f40539SMichael Lange   Level: intermediate
154515078cd4SMichael Lange 
154615078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
154715078cd4SMichael Lange @*/
1548b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
154915078cd4SMichael Lange {
1550b9f40539SMichael Lange   MPI_Comm               comm;
1551cc1750acSStefano Zampini   PetscInt               dim, cdim, nroots;
1552b9f40539SMichael Lange   PetscSF                sfPoint;
155315078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1554ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1555b9f40539SMichael Lange   PetscBool              flg;
155615078cd4SMichael Lange   PetscErrorCode         ierr;
155715078cd4SMichael Lange 
155815078cd4SMichael Lange   PetscFunctionBegin;
155915078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15601b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1561b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
156215078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
156315078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
1564cc1750acSStefano Zampini   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1565cc1750acSStefano Zampini   ierr = DMSetCoordinateDim(targetDM, cdim);CHKERRQ(ierr);
156615078cd4SMichael Lange 
1567bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1568b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1569b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1570bfb0467fSMichael Lange   if (nroots >= 0) {
1571b9f40539SMichael Lange     IS                     isOriginal;
1572ac04eaf7SToby Isaac     PetscInt               n, size, nleaves;
1573ac04eaf7SToby Isaac     PetscInt              *numbering_orig, *numbering_new;
1574b9f40539SMichael Lange     /* Get the original point numbering */
1575b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1576b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1577b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1578b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1579b9f40539SMichael Lange     /* Convert to positive global numbers */
1580b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1581b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1582b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1583b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1584b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1585b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1586b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1587b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1588b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
158915078cd4SMichael Lange   } else {
1590bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
159115078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
159215078cd4SMichael Lange   }
1593c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1594b9f40539SMichael Lange   if (flg) {
1595b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1596b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1597b9f40539SMichael Lange   }
159815078cd4SMichael Lange   /* Migrate DM data to target DM */
1599ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
160015078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
16010dc1530dSSander Arens   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
160215078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
160308633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1604ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1605302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
16061b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
160715078cd4SMichael Lange   PetscFunctionReturn(0);
160815078cd4SMichael Lange }
160915078cd4SMichael Lange 
16103b8f15a2SMatthew G. Knepley /*@C
161170034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
161270034214SMatthew G. Knepley 
161370034214SMatthew G. Knepley   Not Collective
161470034214SMatthew G. Knepley 
161570034214SMatthew G. Knepley   Input Parameter:
161670034214SMatthew G. Knepley + dm  - The original DMPlex object
161770034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
161870034214SMatthew G. Knepley 
161970034214SMatthew G. Knepley   Output Parameter:
162070034214SMatthew G. Knepley + sf - The PetscSF used for point distribution
162170034214SMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
162270034214SMatthew G. Knepley 
162370034214SMatthew G. Knepley   Note: If the mesh was not distributed, the return value is NULL.
162470034214SMatthew G. Knepley 
162593a1dc33SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMPlexSetAdjacencyUseCone() and
162670034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
162770034214SMatthew G. Knepley   representation on the mesh.
162870034214SMatthew G. Knepley 
162970034214SMatthew G. Knepley   Level: intermediate
163070034214SMatthew G. Knepley 
163170034214SMatthew G. Knepley .keywords: mesh, elements
163270034214SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
163370034214SMatthew G. Knepley @*/
163480cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
163570034214SMatthew G. Knepley {
163670034214SMatthew G. Knepley   MPI_Comm               comm;
163715078cd4SMichael Lange   PetscPartitioner       partitioner;
1638f8987ae8SMichael Lange   IS                     cellPart;
1639f8987ae8SMichael Lange   PetscSection           cellPartSection;
1640cf86098cSMatthew G. Knepley   DM                     dmCoord;
1641f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
164243f7d02bSMichael Lange   PetscSF                sfProcess, sfMigration, sfStratified, sfPoint;
164370034214SMatthew G. Knepley   PetscBool              flg;
16449852e123SBarry Smith   PetscMPIInt            rank, size, p;
164570034214SMatthew G. Knepley   PetscErrorCode         ierr;
164670034214SMatthew G. Knepley 
164770034214SMatthew G. Knepley   PetscFunctionBegin;
164870034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
164970034214SMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
165070034214SMatthew G. Knepley   PetscValidPointer(dmParallel,5);
165170034214SMatthew G. Knepley 
16520c86c063SLisandro Dalcin   if (sf) *sf = NULL;
16530c86c063SLisandro Dalcin   *dmParallel = NULL;
165470034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
165570034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
16569852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
16570c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
165870034214SMatthew G. Knepley 
16590c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
166015078cd4SMichael Lange   /* Create cell partition */
166167eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
166277623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
166315078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
166415078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1665f8987ae8SMichael Lange   {
1666f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1667f8987ae8SMichael Lange     PetscInt proc, pStart, pEnd, npoints, poffset;
1668f8987ae8SMichael Lange     const PetscInt *points;
1669f8987ae8SMichael Lange     ierr = DMLabelCreate("Point Partition", &lblPartition);CHKERRQ(ierr);
1670f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1671f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1672f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1673f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1674f8987ae8SMichael Lange       ierr = PetscSectionGetOffset(cellPartSection, proc, &poffset);CHKERRQ(ierr);
1675f8987ae8SMichael Lange       for (p = poffset; p < poffset+npoints; p++) {
1676f8987ae8SMichael Lange         ierr = DMLabelSetValue(lblPartition, points[p], proc);CHKERRQ(ierr);
167770034214SMatthew G. Knepley       }
1678f8987ae8SMichael Lange     }
1679f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1680f8987ae8SMichael Lange   }
1681f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelClosure(dm, lblPartition);CHKERRQ(ierr);
1682f8987ae8SMichael Lange   {
1683f8987ae8SMichael Lange     /* Build a global process SF */
1684f8987ae8SMichael Lange     PetscSFNode *remoteProc;
16859852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
16869852e123SBarry Smith     for (p = 0; p < size; ++p) {
1687f8987ae8SMichael Lange       remoteProc[p].rank  = p;
1688f8987ae8SMichael Lange       remoteProc[p].index = rank;
1689f8987ae8SMichael Lange     }
1690f8987ae8SMichael Lange     ierr = PetscSFCreate(comm, &sfProcess);CHKERRQ(ierr);
1691f8987ae8SMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProcess, "Process SF");CHKERRQ(ierr);
16929852e123SBarry Smith     ierr = PetscSFSetGraph(sfProcess, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
1693f8987ae8SMichael Lange   }
1694f8987ae8SMichael Lange   ierr = DMLabelCreate("Point migration", &lblMigration);CHKERRQ(ierr);
1695f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, sfProcess, lblMigration);CHKERRQ(ierr);
1696302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
169743f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
169843f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
169943f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
170043f7d02bSMichael Lange   sfMigration = sfStratified;
170167eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1702c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
170370034214SMatthew G. Knepley   if (flg) {
1704f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1705f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
170670034214SMatthew G. Knepley   }
1707f8987ae8SMichael Lange 
170815078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
170970034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
171070034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1711b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
171270034214SMatthew G. Knepley 
1713a157612eSMichael Lange   /* Build the point SF without overlap */
1714*fef8a7d2SMatthew G. Knepley   ((DM_Plex*) (*dmParallel)->data)->partitionBalance = ((DM_Plex*) dm->data)->partitionBalance;
17151627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1716f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1717cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1718cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1719f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
172070034214SMatthew G. Knepley 
1721a157612eSMichael Lange   if (overlap > 0) {
172215078cd4SMichael Lange     DM                 dmOverlap;
172315078cd4SMichael Lange     PetscInt           nroots, nleaves;
172415078cd4SMichael Lange     PetscSFNode       *newRemote;
172515078cd4SMichael Lange     const PetscSFNode *oldRemote;
172615078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1727a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1728b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1729a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1730a157612eSMichael Lange     *dmParallel = dmOverlap;
1731c389ea9fSToby Isaac     if (flg) {
17323d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
173315078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1734c389ea9fSToby Isaac     }
173543331d4aSMichael Lange 
1736f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1737f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
173815078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
173943331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
174015078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
174115078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
174215078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
174315078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
174415078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1745f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
174615078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1747c389ea9fSToby Isaac   }
1748bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1749f8987ae8SMichael Lange   ierr = PetscSFDestroy(&sfProcess);CHKERRQ(ierr);
1750f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1751f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
17524eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
17534eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1754721cbd76SMatthew G. Knepley   /* Copy BC */
1755a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
175666fe0bfeSMatthew G. Knepley   /* Create sfNatural */
175766fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
175866fe0bfeSMatthew G. Knepley     PetscSection section;
175966fe0bfeSMatthew G. Knepley 
176066fe0bfeSMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
17610c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
17620e663481SBlaise Bourdin     ierr = DMSetUseNatural(*dmParallel, PETSC_TRUE);CHKERRQ(ierr);
176366fe0bfeSMatthew G. Knepley   }
1764721cbd76SMatthew G. Knepley   /* Cleanup */
1765f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
176656d056acSSatish Balay   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1767f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
176870034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
176970034214SMatthew G. Knepley   PetscFunctionReturn(0);
177070034214SMatthew G. Knepley }
1771a157612eSMichael Lange 
1772a157612eSMichael Lange /*@C
177324cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1774a157612eSMichael Lange 
1775a157612eSMichael Lange   Not Collective
1776a157612eSMichael Lange 
1777a157612eSMichael Lange   Input Parameter:
1778a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
17790c86c063SLisandro Dalcin - overlap - The overlap of partitions
1780a157612eSMichael Lange 
1781a157612eSMichael Lange   Output Parameter:
1782a157612eSMichael Lange + sf - The PetscSF used for point distribution
1783a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1784a157612eSMichael Lange 
1785a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1786a157612eSMichael Lange 
1787a157612eSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
1788a157612eSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
1789a157612eSMichael Lange   representation on the mesh.
1790a157612eSMichael Lange 
1791a157612eSMichael Lange   Level: intermediate
1792a157612eSMichael Lange 
1793a157612eSMichael Lange .keywords: mesh, elements
1794a157612eSMichael Lange .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
1795a157612eSMichael Lange @*/
1796b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1797a157612eSMichael Lange {
1798a157612eSMichael Lange   MPI_Comm               comm;
17993567eaeeSMatthew G. Knepley   PetscMPIInt            size, rank;
1800a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1801a157612eSMichael Lange   IS                     rootrank, leafrank;
1802cf86098cSMatthew G. Knepley   DM                     dmCoord;
1803a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1804f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1805a157612eSMichael Lange   PetscErrorCode         ierr;
1806a157612eSMichael Lange 
1807a157612eSMichael Lange   PetscFunctionBegin;
1808a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1809a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1810a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1811a157612eSMichael Lange 
18120c86c063SLisandro Dalcin   if (sf) *sf = NULL;
18130c86c063SLisandro Dalcin   *dmOverlap  = NULL;
1814a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
18153567eaeeSMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1816a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
18170c86c063SLisandro Dalcin   if (size == 1) PetscFunctionReturn(0);
1818a157612eSMichael Lange 
18190c86c063SLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1820a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
182167eb269bSLisandro Dalcin   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1822a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1823a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1824a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1825a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1826a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1827a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1828a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1829a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1830a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1831a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1832a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1833a9f1d5b2SMichael Lange 
183415fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
183515fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
183615fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
183715fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
183867eb269bSLisandro Dalcin   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
1839a157612eSMichael Lange 
1840a157612eSMichael Lange   /* Build the overlapping DM */
1841a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1842a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1843a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1844f5bf2dbfSMichael Lange   /* Build the new point SF */
18451627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1846f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1847cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1848cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1849f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1850a157612eSMichael Lange   /* Cleanup overlap partition */
1851a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1852a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1853a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
18541b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1855a157612eSMichael Lange   PetscFunctionReturn(0);
1856a157612eSMichael Lange }
18576462276dSToby Isaac 
18586462276dSToby Isaac /*@C
18596462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
18606462276dSToby Isaac   root process of the original's communicator.
18616462276dSToby Isaac 
18626462276dSToby Isaac   Input Parameters:
18636462276dSToby Isaac . dm - the original DMPlex object
18646462276dSToby Isaac 
18656462276dSToby Isaac   Output Parameters:
18666462276dSToby Isaac . gatherMesh - the gathered DM object, or NULL
18676462276dSToby Isaac 
18686462276dSToby Isaac   Level: intermediate
18696462276dSToby Isaac 
18706462276dSToby Isaac .keywords: mesh
18716462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
18726462276dSToby Isaac @*/
18736462276dSToby Isaac PetscErrorCode DMPlexGetGatherDM(DM dm, DM *gatherMesh)
18746462276dSToby Isaac {
18756462276dSToby Isaac   MPI_Comm       comm;
18766462276dSToby Isaac   PetscMPIInt    size;
18776462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
18786462276dSToby Isaac   PetscErrorCode ierr;
18796462276dSToby Isaac 
18806462276dSToby Isaac   PetscFunctionBegin;
18816462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18820c86c063SLisandro Dalcin   PetscValidPointer(gatherMesh,2);
18830c86c063SLisandro Dalcin   *gatherMesh = NULL;
18846462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18856462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18866462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18876462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
18886462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
18896462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
18906462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
18916462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
18926462276dSToby Isaac   ierr = DMPlexDistribute(dm,0,NULL,gatherMesh);CHKERRQ(ierr);
18936462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
18946462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
18956462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
18966462276dSToby Isaac   PetscFunctionReturn(0);
18976462276dSToby Isaac }
18986462276dSToby Isaac 
18996462276dSToby Isaac /*@C
19006462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
19016462276dSToby Isaac 
19026462276dSToby Isaac   Input Parameters:
19036462276dSToby Isaac . dm - the original DMPlex object
19046462276dSToby Isaac 
19056462276dSToby Isaac   Output Parameters:
19066462276dSToby Isaac . redundantMesh - the redundant DM object, or NULL
19076462276dSToby Isaac 
19086462276dSToby Isaac   Level: intermediate
19096462276dSToby Isaac 
19106462276dSToby Isaac .keywords: mesh
19116462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
19126462276dSToby Isaac @*/
19136462276dSToby Isaac PetscErrorCode DMPlexGetRedundantDM(DM dm, DM *redundantMesh)
19146462276dSToby Isaac {
19156462276dSToby Isaac   MPI_Comm       comm;
19166462276dSToby Isaac   PetscMPIInt    size, rank;
19176462276dSToby Isaac   PetscInt       pStart, pEnd, p;
19186462276dSToby Isaac   PetscInt       numPoints = -1;
19196462276dSToby Isaac   PetscSF        migrationSF, sfPoint;
19206462276dSToby Isaac   DM             gatherDM, dmCoord;
19216462276dSToby Isaac   PetscSFNode    *points;
19226462276dSToby Isaac   PetscErrorCode ierr;
19236462276dSToby Isaac 
19246462276dSToby Isaac   PetscFunctionBegin;
19256462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19260c86c063SLisandro Dalcin   PetscValidPointer(redundantMesh,2);
19270c86c063SLisandro Dalcin   *redundantMesh = NULL;
19286462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
19296462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
193068dbc166SMatthew G. Knepley   if (size == 1) {
193168dbc166SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
193268dbc166SMatthew G. Knepley     *redundantMesh = dm;
193368dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
193468dbc166SMatthew G. Knepley   }
19356462276dSToby Isaac   ierr = DMPlexGetGatherDM(dm,&gatherDM);CHKERRQ(ierr);
19366462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
19376462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
19386462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
19396462276dSToby Isaac   numPoints = pEnd - pStart;
19406462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
19416462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
19426462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
19436462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
19446462276dSToby Isaac     points[p].index = p;
19456462276dSToby Isaac     points[p].rank  = 0;
19466462276dSToby Isaac   }
19476462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
19486462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
19496462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
19506462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
19516462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
19526462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
19536462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
19546462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
19556462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
19566462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
19576462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
19586462276dSToby Isaac   PetscFunctionReturn(0);
19596462276dSToby Isaac }
1960