xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision 1cf84007adf5c108639c0d32cd590b65feee66a2)
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 {
75*1cf84007SMatthew G. Knepley   PetscDS        prob;
76*1cf84007SMatthew G. Knepley   PetscBool      useClosure;
77*1cf84007SMatthew G. Knepley   PetscInt       Nf;
78*1cf84007SMatthew G. Knepley   PetscErrorCode ierr;
7970034214SMatthew G. Knepley 
8070034214SMatthew G. Knepley   PetscFunctionBegin;
81*1cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
82*1cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
83*1cf84007SMatthew G. Knepley   if (Nf < 1) {ierr = DMSetNumFields(dm, 1);CHKERRQ(ierr);}
84*1cf84007SMatthew G. Knepley   ierr = PetscDSGetAdjacency(prob, 0, NULL, &useClosure);CHKERRQ(ierr);
85*1cf84007SMatthew G. Knepley   ierr = PetscDSSetAdjacency(prob, 0, useCone, useClosure);CHKERRQ(ierr);
8670034214SMatthew G. Knepley   PetscFunctionReturn(0);
8770034214SMatthew G. Knepley }
8870034214SMatthew G. Knepley 
8970034214SMatthew G. Knepley /*@
9070034214SMatthew G. Knepley   DMPlexGetAdjacencyUseCone - Query whether adjacency in the mesh uses the cone or the support first
9170034214SMatthew G. Knepley 
9270034214SMatthew G. Knepley   Input Parameter:
9370034214SMatthew G. Knepley . dm      - The DM object
9470034214SMatthew G. Knepley 
9570034214SMatthew G. Knepley   Output Parameter:
9670034214SMatthew G. Knepley . useCone - Flag to use the cone first
9770034214SMatthew G. Knepley 
9870034214SMatthew G. Knepley   Level: intermediate
9970034214SMatthew G. Knepley 
10070034214SMatthew G. Knepley   Notes:
10170034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1024b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
10370034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
10470034214SMatthew G. Knepley 
10570034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexGetAdjacencyUseClosure(), DMPlexDistribute(), DMPlexPreallocateOperator()
10670034214SMatthew G. Knepley @*/
10770034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseCone(DM dm, PetscBool *useCone)
10870034214SMatthew G. Knepley {
109*1cf84007SMatthew G. Knepley   PetscDS        prob;
110*1cf84007SMatthew G. Knepley   PetscInt       Nf;
111*1cf84007SMatthew G. Knepley   PetscErrorCode ierr;
11270034214SMatthew G. Knepley 
11370034214SMatthew G. Knepley   PetscFunctionBegin;
114*1cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
115*1cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
116*1cf84007SMatthew G. Knepley   if (Nf < 1) {ierr = DMSetNumFields(dm, 1);CHKERRQ(ierr);}
117*1cf84007SMatthew G. Knepley   ierr = PetscDSGetAdjacency(prob, 0, useCone, NULL);CHKERRQ(ierr);
11870034214SMatthew G. Knepley   PetscFunctionReturn(0);
11970034214SMatthew G. Knepley }
12070034214SMatthew G. Knepley 
12170034214SMatthew G. Knepley /*@
12270034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure - Define adjacency in the mesh using the transitive closure
12370034214SMatthew G. Knepley 
12470034214SMatthew G. Knepley   Input Parameters:
12570034214SMatthew G. Knepley + dm      - The DM object
12670034214SMatthew G. Knepley - useClosure - Flag to use the closure
12770034214SMatthew G. Knepley 
12870034214SMatthew G. Knepley   Level: intermediate
12970034214SMatthew G. Knepley 
13070034214SMatthew G. Knepley   Notes:
13170034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1324b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
13370034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
13470034214SMatthew G. Knepley 
13570034214SMatthew G. Knepley .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
13670034214SMatthew G. Knepley @*/
13770034214SMatthew G. Knepley PetscErrorCode DMPlexSetAdjacencyUseClosure(DM dm, PetscBool useClosure)
13870034214SMatthew G. Knepley {
139*1cf84007SMatthew G. Knepley   PetscDS        prob;
140*1cf84007SMatthew G. Knepley   PetscBool      useCone;
141*1cf84007SMatthew G. Knepley   PetscInt       Nf;
142*1cf84007SMatthew G. Knepley   PetscErrorCode ierr;
14370034214SMatthew G. Knepley 
14470034214SMatthew G. Knepley   PetscFunctionBegin;
145*1cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
146*1cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
147*1cf84007SMatthew G. Knepley   if (Nf < 1) {ierr = DMSetNumFields(dm, 1);CHKERRQ(ierr);}
148*1cf84007SMatthew G. Knepley   ierr = PetscDSGetAdjacency(prob, 0, &useCone, NULL);CHKERRQ(ierr);
149*1cf84007SMatthew G. Knepley   ierr = PetscDSSetAdjacency(prob, 0, useCone, useClosure);CHKERRQ(ierr);
15070034214SMatthew G. Knepley   PetscFunctionReturn(0);
15170034214SMatthew G. Knepley }
15270034214SMatthew G. Knepley 
15370034214SMatthew G. Knepley /*@
15470034214SMatthew G. Knepley   DMPlexGetAdjacencyUseClosure - Query whether adjacency in the mesh uses the transitive closure
15570034214SMatthew G. Knepley 
15670034214SMatthew G. Knepley   Input Parameter:
15770034214SMatthew G. Knepley . dm      - The DM object
15870034214SMatthew G. Knepley 
15970034214SMatthew G. Knepley   Output Parameter:
16070034214SMatthew G. Knepley . useClosure - Flag to use the closure
16170034214SMatthew G. Knepley 
16270034214SMatthew G. Knepley   Level: intermediate
16370034214SMatthew G. Knepley 
16470034214SMatthew G. Knepley   Notes:
16570034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1664b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
16770034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
16870034214SMatthew G. Knepley 
16970034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
17070034214SMatthew G. Knepley @*/
17170034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseClosure(DM dm, PetscBool *useClosure)
17270034214SMatthew G. Knepley {
173*1cf84007SMatthew G. Knepley   PetscDS        prob;
174*1cf84007SMatthew G. Knepley   PetscInt       Nf;
175*1cf84007SMatthew G. Knepley   PetscErrorCode ierr;
17670034214SMatthew G. Knepley 
17770034214SMatthew G. Knepley   PetscFunctionBegin;
178*1cf84007SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
179*1cf84007SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
180*1cf84007SMatthew G. Knepley   if (Nf < 1) {ierr = DMSetNumFields(dm, 1);CHKERRQ(ierr);}
181*1cf84007SMatthew G. Knepley   ierr = PetscDSGetAdjacency(prob, 0, NULL, useClosure);CHKERRQ(ierr);
18270034214SMatthew G. Knepley   PetscFunctionReturn(0);
18370034214SMatthew G. Knepley }
18470034214SMatthew G. Knepley 
1858b0b4c70SToby Isaac /*@
186a17985deSToby Isaac   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.
1878b0b4c70SToby Isaac 
1888b0b4c70SToby Isaac   Input Parameters:
1898b0b4c70SToby Isaac + dm      - The DM object
1905b317d89SToby 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.
1918b0b4c70SToby Isaac 
1928b0b4c70SToby Isaac   Level: intermediate
1938b0b4c70SToby Isaac 
194a17985deSToby Isaac .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
1958b0b4c70SToby Isaac @*/
1965b317d89SToby Isaac PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors)
1978b0b4c70SToby Isaac {
1988b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
1998b0b4c70SToby Isaac 
2008b0b4c70SToby Isaac   PetscFunctionBegin;
2018b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2025b317d89SToby Isaac   mesh->useAnchors = useAnchors;
2038b0b4c70SToby Isaac   PetscFunctionReturn(0);
2048b0b4c70SToby Isaac }
2058b0b4c70SToby Isaac 
2068b0b4c70SToby Isaac /*@
207a17985deSToby Isaac   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.
2088b0b4c70SToby Isaac 
2098b0b4c70SToby Isaac   Input Parameter:
2108b0b4c70SToby Isaac . dm      - The DM object
2118b0b4c70SToby Isaac 
2128b0b4c70SToby Isaac   Output Parameter:
2135b317d89SToby 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.
2148b0b4c70SToby Isaac 
2158b0b4c70SToby Isaac   Level: intermediate
2168b0b4c70SToby Isaac 
217a17985deSToby Isaac .seealso: DMPlexSetAdjacencyUseAnchors(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
2188b0b4c70SToby Isaac @*/
2195b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors)
2208b0b4c70SToby Isaac {
2218b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
2228b0b4c70SToby Isaac 
2238b0b4c70SToby Isaac   PetscFunctionBegin;
2248b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2255b317d89SToby Isaac   PetscValidIntPointer(useAnchors, 2);
2265b317d89SToby Isaac   *useAnchors = mesh->useAnchors;
2278b0b4c70SToby Isaac   PetscFunctionReturn(0);
2288b0b4c70SToby Isaac }
2298b0b4c70SToby Isaac 
23070034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
23170034214SMatthew G. Knepley {
23270034214SMatthew G. Knepley   const PetscInt *cone = NULL;
23370034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
23470034214SMatthew G. Knepley   PetscErrorCode  ierr;
23570034214SMatthew G. Knepley 
23670034214SMatthew G. Knepley   PetscFunctionBeginHot;
23770034214SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
23870034214SMatthew G. Knepley   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
2394b6b44bdSMatthew G. Knepley   for (c = 0; c <= coneSize; ++c) {
2404b6b44bdSMatthew G. Knepley     const PetscInt  point   = !c ? p : cone[c-1];
24170034214SMatthew G. Knepley     const PetscInt *support = NULL;
24270034214SMatthew G. Knepley     PetscInt        supportSize, s, q;
24370034214SMatthew G. Knepley 
2444b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
2454b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
24670034214SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
247527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]),0); ++q) {
24870034214SMatthew G. Knepley         if (support[s] == adj[q]) break;
24970034214SMatthew G. Knepley       }
25070034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
25170034214SMatthew G. Knepley     }
25270034214SMatthew G. Knepley   }
25370034214SMatthew G. Knepley   *adjSize = numAdj;
25470034214SMatthew G. Knepley   PetscFunctionReturn(0);
25570034214SMatthew G. Knepley }
25670034214SMatthew G. Knepley 
25770034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
25870034214SMatthew G. Knepley {
25970034214SMatthew G. Knepley   const PetscInt *support = NULL;
26070034214SMatthew G. Knepley   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
26170034214SMatthew G. Knepley   PetscErrorCode  ierr;
26270034214SMatthew G. Knepley 
26370034214SMatthew G. Knepley   PetscFunctionBeginHot;
26470034214SMatthew G. Knepley   ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
26570034214SMatthew G. Knepley   ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
2664b6b44bdSMatthew G. Knepley   for (s = 0; s <= supportSize; ++s) {
2674b6b44bdSMatthew G. Knepley     const PetscInt  point = !s ? p : support[s-1];
26870034214SMatthew G. Knepley     const PetscInt *cone  = NULL;
26970034214SMatthew G. Knepley     PetscInt        coneSize, c, q;
27070034214SMatthew G. Knepley 
2714b6b44bdSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2724b6b44bdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
27370034214SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
274527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]),0); ++q) {
27570034214SMatthew G. Knepley         if (cone[c] == adj[q]) break;
27670034214SMatthew G. Knepley       }
27770034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
27870034214SMatthew G. Knepley     }
27970034214SMatthew G. Knepley   }
28070034214SMatthew G. Knepley   *adjSize = numAdj;
28170034214SMatthew G. Knepley   PetscFunctionReturn(0);
28270034214SMatthew G. Knepley }
28370034214SMatthew G. Knepley 
28470034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[])
28570034214SMatthew G. Knepley {
28670034214SMatthew G. Knepley   PetscInt      *star = NULL;
28770034214SMatthew G. Knepley   PetscInt       numAdj = 0, maxAdjSize = *adjSize, starSize, s;
28870034214SMatthew G. Knepley   PetscErrorCode ierr;
28970034214SMatthew G. Knepley 
29070034214SMatthew G. Knepley   PetscFunctionBeginHot;
29170034214SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
29270034214SMatthew G. Knepley   for (s = 0; s < starSize*2; s += 2) {
29370034214SMatthew G. Knepley     const PetscInt *closure = NULL;
29470034214SMatthew G. Knepley     PetscInt        closureSize, c, q;
29570034214SMatthew G. Knepley 
29670034214SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
29770034214SMatthew G. Knepley     for (c = 0; c < closureSize*2; c += 2) {
298527e7439SSatish Balay       for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]),0); ++q) {
29970034214SMatthew G. Knepley         if (closure[c] == adj[q]) break;
30070034214SMatthew G. Knepley       }
30170034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
30270034214SMatthew G. Knepley     }
30370034214SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
30470034214SMatthew G. Knepley   }
30570034214SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
30670034214SMatthew G. Knepley   *adjSize = numAdj;
30770034214SMatthew G. Knepley   PetscFunctionReturn(0);
30870034214SMatthew G. Knepley }
30970034214SMatthew G. Knepley 
3105b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[])
31170034214SMatthew G. Knepley {
31279bad331SMatthew G. Knepley   static PetscInt asiz = 0;
3138b0b4c70SToby Isaac   PetscInt maxAnchors = 1;
3148b0b4c70SToby Isaac   PetscInt aStart = -1, aEnd = -1;
3158b0b4c70SToby Isaac   PetscInt maxAdjSize;
3168b0b4c70SToby Isaac   PetscSection aSec = NULL;
3178b0b4c70SToby Isaac   IS aIS = NULL;
3188b0b4c70SToby Isaac   const PetscInt *anchors;
3193c1f0c11SLawrence Mitchell   DM_Plex *mesh = (DM_Plex *)dm->data;
32070034214SMatthew G. Knepley   PetscErrorCode  ierr;
32170034214SMatthew G. Knepley 
32270034214SMatthew G. Knepley   PetscFunctionBeginHot;
3235b317d89SToby Isaac   if (useAnchors) {
324a17985deSToby Isaac     ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
3258b0b4c70SToby Isaac     if (aSec) {
3268b0b4c70SToby Isaac       ierr = PetscSectionGetMaxDof(aSec,&maxAnchors);CHKERRQ(ierr);
32724c766afSToby Isaac       maxAnchors = PetscMax(1,maxAnchors);
3288b0b4c70SToby Isaac       ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
3298b0b4c70SToby Isaac       ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
3308b0b4c70SToby Isaac     }
3318b0b4c70SToby Isaac   }
33279bad331SMatthew G. Knepley   if (!*adj) {
33324c766afSToby Isaac     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;
33479bad331SMatthew G. Knepley 
33524c766afSToby Isaac     ierr  = DMPlexGetChart(dm, &pStart,&pEnd);CHKERRQ(ierr);
33679bad331SMatthew G. Knepley     ierr  = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
33724c766afSToby Isaac     ierr  = DMPlexGetMaxSizes(dm, &maxC, &maxS);CHKERRQ(ierr);
33824c766afSToby Isaac     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1;
33924c766afSToby Isaac     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1;
34024c766afSToby Isaac     asiz  = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries);
3418b0b4c70SToby Isaac     asiz *= maxAnchors;
34224c766afSToby Isaac     asiz  = PetscMin(asiz,pEnd-pStart);
34379bad331SMatthew G. Knepley     ierr  = PetscMalloc1(asiz,adj);CHKERRQ(ierr);
34479bad331SMatthew G. Knepley   }
34579bad331SMatthew G. Knepley   if (*adjSize < 0) *adjSize = asiz;
3468b0b4c70SToby Isaac   maxAdjSize = *adjSize;
3473c1f0c11SLawrence Mitchell   if (mesh->useradjacency) {
3483c1f0c11SLawrence Mitchell     ierr = mesh->useradjacency(dm, p, adjSize, *adj, mesh->useradjacencyctx);CHKERRQ(ierr);
3493c1f0c11SLawrence Mitchell   } else if (useTransitiveClosure) {
35079bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);CHKERRQ(ierr);
35170034214SMatthew G. Knepley   } else if (useCone) {
35279bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
35370034214SMatthew G. Knepley   } else {
35479bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
35570034214SMatthew G. Knepley   }
3565b317d89SToby Isaac   if (useAnchors && aSec) {
3578b0b4c70SToby Isaac     PetscInt origSize = *adjSize;
3588b0b4c70SToby Isaac     PetscInt numAdj = origSize;
3598b0b4c70SToby Isaac     PetscInt i = 0, j;
3608b0b4c70SToby Isaac     PetscInt *orig = *adj;
3618b0b4c70SToby Isaac 
3628b0b4c70SToby Isaac     while (i < origSize) {
3638b0b4c70SToby Isaac       PetscInt p = orig[i];
3648b0b4c70SToby Isaac       PetscInt aDof = 0;
3658b0b4c70SToby Isaac 
3668b0b4c70SToby Isaac       if (p >= aStart && p < aEnd) {
3678b0b4c70SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
3688b0b4c70SToby Isaac       }
3698b0b4c70SToby Isaac       if (aDof) {
3708b0b4c70SToby Isaac         PetscInt aOff;
3718b0b4c70SToby Isaac         PetscInt s, q;
3728b0b4c70SToby Isaac 
3738b0b4c70SToby Isaac         for (j = i + 1; j < numAdj; j++) {
3748b0b4c70SToby Isaac           orig[j - 1] = orig[j];
3758b0b4c70SToby Isaac         }
3768b0b4c70SToby Isaac         origSize--;
3778b0b4c70SToby Isaac         numAdj--;
3788b0b4c70SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
3798b0b4c70SToby Isaac         for (s = 0; s < aDof; ++s) {
380527e7439SSatish Balay           for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) {
3818b0b4c70SToby Isaac             if (anchors[aOff+s] == orig[q]) break;
3828b0b4c70SToby Isaac           }
3838b0b4c70SToby Isaac           if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
3848b0b4c70SToby Isaac         }
3858b0b4c70SToby Isaac       }
3868b0b4c70SToby Isaac       else {
3878b0b4c70SToby Isaac         i++;
3888b0b4c70SToby Isaac       }
3898b0b4c70SToby Isaac     }
3908b0b4c70SToby Isaac     *adjSize = numAdj;
3918b0b4c70SToby Isaac     ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
3928b0b4c70SToby Isaac   }
39370034214SMatthew G. Knepley   PetscFunctionReturn(0);
39470034214SMatthew G. Knepley }
39570034214SMatthew G. Knepley 
39670034214SMatthew G. Knepley /*@
39770034214SMatthew G. Knepley   DMPlexGetAdjacency - Return all points adjacent to the given point
39870034214SMatthew G. Knepley 
39970034214SMatthew G. Knepley   Input Parameters:
40070034214SMatthew G. Knepley + dm - The DM object
40170034214SMatthew G. Knepley . p  - The point
40270034214SMatthew G. Knepley . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE
40370034214SMatthew G. Knepley - adj - Either NULL so that the array is allocated, or an existing array with size adjSize
40470034214SMatthew G. Knepley 
40570034214SMatthew G. Knepley   Output Parameters:
40670034214SMatthew G. Knepley + adjSize - The number of adjacent points
40770034214SMatthew G. Knepley - adj - The adjacent points
40870034214SMatthew G. Knepley 
40970034214SMatthew G. Knepley   Level: advanced
41070034214SMatthew G. Knepley 
41170034214SMatthew G. Knepley   Notes: The user must PetscFree the adj array if it was not passed in.
41270034214SMatthew G. Knepley 
41370034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
41470034214SMatthew G. Knepley @*/
41570034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
41670034214SMatthew G. Knepley {
417*1cf84007SMatthew G. Knepley   PetscBool      useCone, useClosure, useAnchors;
41870034214SMatthew G. Knepley   PetscErrorCode ierr;
41970034214SMatthew G. Knepley 
42070034214SMatthew G. Knepley   PetscFunctionBeginHot;
42170034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42270034214SMatthew G. Knepley   PetscValidPointer(adjSize,3);
42370034214SMatthew G. Knepley   PetscValidPointer(adj,4);
424*1cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
425*1cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
426*1cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
427*1cf84007SMatthew G. Knepley   ierr = DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);CHKERRQ(ierr);
42870034214SMatthew G. Knepley   PetscFunctionReturn(0);
42970034214SMatthew G. Knepley }
43008633170SToby Isaac 
431b0a623aaSMatthew G. Knepley /*@
432b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
433b0a623aaSMatthew G. Knepley 
434b0a623aaSMatthew G. Knepley   Collective on DM
435b0a623aaSMatthew G. Knepley 
436b0a623aaSMatthew G. Knepley   Input Parameters:
437b0a623aaSMatthew G. Knepley + dm      - The DM
438b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity
439b0a623aaSMatthew G. Knepley 
440b0a623aaSMatthew G. Knepley   Output Parameters:
441b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
442b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
443b0a623aaSMatthew G. Knepley 
444b0a623aaSMatthew G. Knepley   Level: developer
445b0a623aaSMatthew G. Knepley 
446b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
447b0a623aaSMatthew G. Knepley @*/
448b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
449b0a623aaSMatthew G. Knepley {
450b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
451b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
452b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
453b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
454b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
455b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
456b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
4579852e123SBarry Smith   PetscMPIInt        size, proc, rank;
458b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
459b0a623aaSMatthew G. Knepley 
460b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
461b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
463b0a623aaSMatthew G. Knepley   if (processRanks) {PetscValidPointer(processRanks, 3);}
464b0a623aaSMatthew G. Knepley   if (sfProcess)    {PetscValidPointer(sfProcess, 4);}
4659852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
466b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
467b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr);
4689852e123SBarry Smith   ierr = PetscBTCreate(size, &neighbors);CHKERRQ(ierr);
4699852e123SBarry Smith   ierr = PetscBTMemzero(size, neighbors);CHKERRQ(ierr);
470b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
471b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr);
472b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr);
473b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
474b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
475b0a623aaSMatthew G. Knepley 
476b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr);
477b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr);
478302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
479b0a623aaSMatthew G. Knepley   }
480b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr);
481b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
482b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr);
483b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr);
484b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
485b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
486b0a623aaSMatthew G. Knepley 
487b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr);
488b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr);
489302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
490b0a623aaSMatthew G. Knepley   }
491b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr);
492b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
493b0a623aaSMatthew G. Knepley   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
494b0a623aaSMatthew G. Knepley   /* Calculate edges */
495b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
4969852e123SBarry Smith   for(proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
497b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr);
498b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr);
499b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr);
5009852e123SBarry Smith   for(proc = 0, n = 0; proc < size; ++proc) {
501b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
502b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
503b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
504b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
505b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
506b0a623aaSMatthew G. Knepley       ++n;
507b0a623aaSMatthew G. Knepley     }
508b0a623aaSMatthew G. Knepley   }
509b0a623aaSMatthew G. Knepley   ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr);
510b0a623aaSMatthew G. Knepley   if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);}
511b0a623aaSMatthew G. Knepley   else              {ierr = PetscFree(ranksNew);CHKERRQ(ierr);}
512b0a623aaSMatthew G. Knepley   if (sfProcess) {
513b0a623aaSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr);
514b0a623aaSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr);
515b0a623aaSMatthew G. Knepley     ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr);
5169852e123SBarry Smith     ierr = PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
517b0a623aaSMatthew G. Knepley   }
518b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
519b0a623aaSMatthew G. Knepley }
520b0a623aaSMatthew G. Knepley 
521b0a623aaSMatthew G. Knepley /*@
522b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
523b0a623aaSMatthew G. Knepley 
524b0a623aaSMatthew G. Knepley   Collective on DM
525b0a623aaSMatthew G. Knepley 
526b0a623aaSMatthew G. Knepley   Input Parameter:
527b0a623aaSMatthew G. Knepley . dm - The DM
528b0a623aaSMatthew G. Knepley 
529b0a623aaSMatthew G. Knepley   Output Parameters:
530b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
531b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
532b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
533b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
534b0a623aaSMatthew G. Knepley 
535b0a623aaSMatthew G. Knepley   Level: developer
536b0a623aaSMatthew G. Knepley 
537b0a623aaSMatthew G. Knepley .seealso: DMPlexCreateOverlap()
538b0a623aaSMatthew G. Knepley @*/
539b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
540b0a623aaSMatthew G. Knepley {
541b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
542b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
543b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
544b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
545b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
546b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
547b0a623aaSMatthew G. Knepley   PetscErrorCode  ierr;
548b0a623aaSMatthew G. Knepley 
549b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
550b0a623aaSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
551b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
552b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
553b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
554b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
555b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr);
556b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr);
557b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr);
558b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr);
559b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);}
560b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
561b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
562b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr);
563b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr);
564b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(nedges,  &remoterank);CHKERRQ(ierr);
565b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
566b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
567b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
568b0a623aaSMatthew G. Knepley   ierr = PetscFree(myrank);CHKERRQ(ierr);
569b0a623aaSMatthew G. Knepley   ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr);
570b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
571b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr);
572b0a623aaSMatthew G. Knepley   ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr);
573b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
574b0a623aaSMatthew G. Knepley }
575b0a623aaSMatthew G. Knepley 
576278397a0SMatthew G. Knepley /*@C
577b0a623aaSMatthew G. Knepley   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.
578b0a623aaSMatthew G. Knepley 
579b0a623aaSMatthew G. Knepley   Collective on DM
580b0a623aaSMatthew G. Knepley 
581b0a623aaSMatthew G. Knepley   Input Parameters:
582b0a623aaSMatthew G. Knepley + dm          - The DM
58324d039d7SMichael Lange . levels      - Number of overlap levels
584b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
585b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
586b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
587b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
588b0a623aaSMatthew G. Knepley 
589b0a623aaSMatthew G. Knepley   Output Parameters:
590a9f1d5b2SMichael Lange + ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
591b0a623aaSMatthew G. Knepley 
592b0a623aaSMatthew G. Knepley   Level: developer
593b0a623aaSMatthew G. Knepley 
5941fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
595b0a623aaSMatthew G. Knepley @*/
596a9f1d5b2SMichael Lange PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
597b0a623aaSMatthew G. Knepley {
598e540f424SMichael Lange   MPI_Comm           comm;
599b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
600b0a623aaSMatthew G. Knepley   PetscSF            sfPoint, sfProc;
601b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
602b0a623aaSMatthew G. Knepley   const PetscInt    *local;
6031fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
604b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
6051fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
6069852e123SBarry Smith   PetscMPIInt        rank, size;
60726a7d390SMatthew G. Knepley   PetscBool          useCone, useClosure, flg;
608b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
609b0a623aaSMatthew G. Knepley 
610b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
611e540f424SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
6129852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
613e540f424SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
614b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
615b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
616b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr);
617b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
618b0a623aaSMatthew G. Knepley   ierr = DMLabelCreate("Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr);
619b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
620b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
621b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
622b0a623aaSMatthew G. Knepley 
623b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, local[l], &adjSize, &adj);CHKERRQ(ierr);
624b0a623aaSMatthew G. Knepley     for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);}
625b0a623aaSMatthew G. Knepley   }
626b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr);
627b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr);
628b0a623aaSMatthew G. Knepley   /* Handle roots */
629b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
630b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
631b0a623aaSMatthew G. Knepley 
632b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
633b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
634b0a623aaSMatthew G. Knepley       ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr);
635b0a623aaSMatthew G. Knepley       if (neighbors) {
636b0a623aaSMatthew G. Knepley         ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr);
637b0a623aaSMatthew G. Knepley         ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
638b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
639b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff+n];
640b0a623aaSMatthew G. Knepley 
641b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
642b0a623aaSMatthew G. Knepley           for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
643b0a623aaSMatthew G. Knepley         }
644b0a623aaSMatthew G. Knepley       }
645b0a623aaSMatthew G. Knepley     }
646b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
647b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr);
648b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
649b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr);
650b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
651b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
652b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff+n];
653b0a623aaSMatthew G. Knepley 
654b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
655b0a623aaSMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
656b0a623aaSMatthew G. Knepley     }
657b0a623aaSMatthew G. Knepley   }
658b0a623aaSMatthew G. Knepley   ierr = PetscFree(adj);CHKERRQ(ierr);
659b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr);
660b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr);
66124d039d7SMichael Lange   /* Add additional overlap levels */
662be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
663be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
664be200f8dSMichael Lange     ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr);
665be200f8dSMichael Lange     /* Add next level of point donations to the label */
666be200f8dSMichael Lange     ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr);
667be200f8dSMichael Lange   }
66826a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
66926a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
67026a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
67126a7d390SMatthew G. Knepley   if (useCone || !useClosure) {
6725abbe4feSMichael Lange     ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr);
67326a7d390SMatthew G. Knepley   }
674c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr);
675e540f424SMichael Lange   if (flg) {
676b0a623aaSMatthew G. Knepley     ierr = DMLabelView(ovAdjByRank, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
677b0a623aaSMatthew G. Knepley   }
67871a8c5fcSMichael Lange   /* Make global process SF and invert sender to receiver label */
67971a8c5fcSMichael Lange   {
68071a8c5fcSMichael Lange     /* Build a global process SF */
68171a8c5fcSMichael Lange     PetscSFNode *remoteProc;
6829852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
6839852e123SBarry Smith     for (p = 0; p < size; ++p) {
68471a8c5fcSMichael Lange       remoteProc[p].rank  = p;
68571a8c5fcSMichael Lange       remoteProc[p].index = rank;
68671a8c5fcSMichael Lange     }
68771a8c5fcSMichael Lange     ierr = PetscSFCreate(comm, &sfProc);CHKERRQ(ierr);
68871a8c5fcSMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProc, "Process SF");CHKERRQ(ierr);
6899852e123SBarry Smith     ierr = PetscSFSetGraph(sfProc, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
69071a8c5fcSMichael Lange   }
691a9f1d5b2SMichael Lange   ierr = DMLabelCreate("Overlap label", ovLabel);CHKERRQ(ierr);
692a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, sfProc, *ovLabel);CHKERRQ(ierr);
693a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
694a9f1d5b2SMichael Lange   for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);}
695e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
696a9f1d5b2SMichael Lange     ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr);
697a9f1d5b2SMichael Lange     ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr);
698e540f424SMichael Lange   }
699e540f424SMichael Lange   /* Clean up */
7001fd9873aSMichael Lange   ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr);
701b0a623aaSMatthew G. Knepley   ierr = PetscSFDestroy(&sfProc);CHKERRQ(ierr);
702b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
703b0a623aaSMatthew G. Knepley }
70470034214SMatthew G. Knepley 
70524cc2ca5SMatthew G. Knepley /*@C
70624cc2ca5SMatthew G. Knepley   DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF
70724cc2ca5SMatthew G. Knepley 
70824cc2ca5SMatthew G. Knepley   Collective on DM
70924cc2ca5SMatthew G. Knepley 
71024cc2ca5SMatthew G. Knepley   Input Parameters:
71124cc2ca5SMatthew G. Knepley + dm          - The DM
71224cc2ca5SMatthew G. Knepley - overlapSF   - The SF mapping ghost points in overlap to owner points on other processes
71324cc2ca5SMatthew G. Knepley 
71424cc2ca5SMatthew G. Knepley   Output Parameters:
71524cc2ca5SMatthew G. Knepley + migrationSF - An SF that maps original points in old locations to points in new locations
71624cc2ca5SMatthew G. Knepley 
71724cc2ca5SMatthew G. Knepley   Level: developer
71824cc2ca5SMatthew G. Knepley 
71924cc2ca5SMatthew G. Knepley .seealso: DMPlexCreateOverlap(), DMPlexDistribute()
72024cc2ca5SMatthew G. Knepley @*/
72146f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
72246f9b1c3SMichael Lange {
72346f9b1c3SMichael Lange   MPI_Comm           comm;
7249852e123SBarry Smith   PetscMPIInt        rank, size;
72546f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
72646f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
72746f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
72846f9b1c3SMichael Lange   PetscSFNode       *iremote;
72946f9b1c3SMichael Lange   PetscSF            pointSF;
73046f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
73146f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
73246f9b1c3SMichael Lange   PetscErrorCode     ierr;
73346f9b1c3SMichael Lange 
73446f9b1c3SMichael Lange   PetscFunctionBegin;
73546f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73646f9b1c3SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
73746f9b1c3SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
7389852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
73946f9b1c3SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
74046f9b1c3SMichael Lange 
74146f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
74246f9b1c3SMichael Lange   ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr);
74346f9b1c3SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
74446f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
74546f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
74646f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
74746f9b1c3SMichael Lange   }
74846f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
74946f9b1c3SMichael Lange   ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
75046f9b1c3SMichael Lange   ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
75146f9b1c3SMichael Lange 
75246f9b1c3SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
75346f9b1c3SMichael Lange   ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr);
75446f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) depthRecv[d]=0;
75546f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
75646f9b1c3SMichael Lange   depthShift[dim] = 0;
75746f9b1c3SMichael Lange   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
75846f9b1c3SMichael Lange   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
75946f9b1c3SMichael Lange   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
76046f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
76146f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
76246f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
76346f9b1c3SMichael Lange   }
76446f9b1c3SMichael Lange 
76546f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
76646f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
76746f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
76809b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr);
76909b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr);
77046f9b1c3SMichael Lange   /* First map local points to themselves */
77146f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
77246f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
77346f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) {
77446f9b1c3SMichael Lange       point = p + depthShift[d];
77546f9b1c3SMichael Lange       ilocal[point] = point;
77646f9b1c3SMichael Lange       iremote[point].index = p;
77746f9b1c3SMichael Lange       iremote[point].rank = rank;
77846f9b1c3SMichael Lange       depthIdx[d]++;
77946f9b1c3SMichael Lange     }
78046f9b1c3SMichael Lange   }
78146f9b1c3SMichael Lange 
78246f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
78346f9b1c3SMichael Lange   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
78446f9b1c3SMichael Lange   ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr);
78546f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
78646f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
78746f9b1c3SMichael Lange     for (p=0; p<numSharedPoints; p++) {
78846f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
78946f9b1c3SMichael Lange         point = sharedLocal[p] + depthShift[d];
79046f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
79146f9b1c3SMichael Lange         iremote[point].rank = sharedRemote[p].rank;
79246f9b1c3SMichael Lange       }
79346f9b1c3SMichael Lange     }
79446f9b1c3SMichael Lange   }
79546f9b1c3SMichael Lange 
79646f9b1c3SMichael Lange   /* Now add the incoming overlap points */
79746f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) {
79846f9b1c3SMichael Lange     point = depthIdx[remoteDepths[p]];
79946f9b1c3SMichael Lange     ilocal[point] = point;
80046f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
80146f9b1c3SMichael Lange     iremote[point].rank = overlapRemote[p].rank;
80246f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
80346f9b1c3SMichael Lange   }
80415fff7beSMatthew G. Knepley   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
80546f9b1c3SMichael Lange 
80646f9b1c3SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
80746f9b1c3SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr);
80846f9b1c3SMichael Lange   ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr);
80946f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
81046f9b1c3SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
81146f9b1c3SMichael Lange 
81246f9b1c3SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
81370034214SMatthew G. Knepley   PetscFunctionReturn(0);
81470034214SMatthew G. Knepley }
81570034214SMatthew G. Knepley 
816a9f1d5b2SMichael Lange /*@
817f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
818a9f1d5b2SMichael Lange 
819a9f1d5b2SMichael Lange   Input Parameter:
820a9f1d5b2SMichael Lange + dm          - The DM
821a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
822a9f1d5b2SMichael Lange 
823a9f1d5b2SMichael Lange   Output Parameter:
824a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
825a9f1d5b2SMichael Lange 
826a9f1d5b2SMichael Lange   Level: developer
827a9f1d5b2SMichael Lange 
828a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
829a9f1d5b2SMichael Lange @*/
830a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
831a9f1d5b2SMichael Lange {
832a9f1d5b2SMichael Lange   MPI_Comm           comm;
8339852e123SBarry Smith   PetscMPIInt        rank, size;
8347fab53ddSMatthew G. Knepley   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
835a9f1d5b2SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
836a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
837f0e73a3dSToby Isaac   PetscInt           hybEnd[4];
838a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
839a9f1d5b2SMichael Lange   PetscErrorCode     ierr;
840a9f1d5b2SMichael Lange 
841a9f1d5b2SMichael Lange   PetscFunctionBegin;
842a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
843a9f1d5b2SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
844a9f1d5b2SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
8459852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
8467fab53ddSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr);
847b2566f29SBarry Smith   ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
8487fab53ddSMatthew G. Knepley   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
849a9f1d5b2SMichael Lange 
850a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
851a9f1d5b2SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr);
852a9f1d5b2SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
853f0e73a3dSToby Isaac   ierr = DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[depth-1],&hybEnd[1],&hybEnd[0]);CHKERRQ(ierr);
8547fab53ddSMatthew G. Knepley   for (d = 0; d < depth+1; ++d) {
855a9f1d5b2SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
856f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
857f0e73a3dSToby Isaac       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
858f0e73a3dSToby Isaac         pointDepths[p] = 2 * d;
859f0e73a3dSToby Isaac       } else {
860f0e73a3dSToby Isaac         pointDepths[p] = 2 * d + 1;
861f0e73a3dSToby Isaac       }
862f0e73a3dSToby Isaac     }
863a9f1d5b2SMichael Lange   }
8647fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
865a9f1d5b2SMichael Lange   ierr = PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
866a9f1d5b2SMichael Lange   ierr = PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
867a9f1d5b2SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
868f0e73a3dSToby Isaac   ierr = PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);CHKERRQ(ierr);
869f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
8707fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
871f0e73a3dSToby Isaac   depthShift[2*depth+1] = 0;
872f0e73a3dSToby Isaac   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
873f0e73a3dSToby Isaac   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
874f0e73a3dSToby Isaac   depthShift[0] += depthRecv[1];
875f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
876f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
877f0e73a3dSToby Isaac   for (d = 2 * depth-1; d > 2; --d) {
878f0e73a3dSToby Isaac     PetscInt e;
879f0e73a3dSToby Isaac 
880f0e73a3dSToby Isaac     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
881f0e73a3dSToby Isaac   }
882f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
883a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
884a9f1d5b2SMichael Lange   ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr);
8857fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
8867fab53ddSMatthew G. Knepley     const PetscInt dep = remoteDepths[p];
8877fab53ddSMatthew G. Knepley 
8887fab53ddSMatthew G. Knepley     ilocal[p] = depthShift[dep] + depthIdx[dep];
8897fab53ddSMatthew G. Knepley     depthIdx[dep]++;
890a9f1d5b2SMichael Lange   }
891a9f1d5b2SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
892a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr);
893a9f1d5b2SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr);
894a9f1d5b2SMichael Lange   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
895a9f1d5b2SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
896a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
897a9f1d5b2SMichael Lange }
898a9f1d5b2SMichael Lange 
89970034214SMatthew G. Knepley /*@
90070034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
90170034214SMatthew G. Knepley 
90270034214SMatthew G. Knepley   Collective on DM
90370034214SMatthew G. Knepley 
90470034214SMatthew G. Knepley   Input Parameters:
90570034214SMatthew G. Knepley + dm - The DMPlex object
90670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
90770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
90870034214SMatthew G. Knepley - originalVec - The existing data
90970034214SMatthew G. Knepley 
91070034214SMatthew G. Knepley   Output Parameters:
91170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
91270034214SMatthew G. Knepley - newVec - The new data
91370034214SMatthew G. Knepley 
91470034214SMatthew G. Knepley   Level: developer
91570034214SMatthew G. Knepley 
91670034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
91770034214SMatthew G. Knepley @*/
91870034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
91970034214SMatthew G. Knepley {
92070034214SMatthew G. Knepley   PetscSF        fieldSF;
92170034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
92270034214SMatthew G. Knepley   PetscScalar   *originalValues, *newValues;
92370034214SMatthew G. Knepley   PetscErrorCode ierr;
92470034214SMatthew G. Knepley 
92570034214SMatthew G. Knepley   PetscFunctionBegin;
92670034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
92770034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
92870034214SMatthew G. Knepley 
92970034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
93070034214SMatthew G. Knepley   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
93170034214SMatthew G. Knepley   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
93270034214SMatthew G. Knepley 
93370034214SMatthew G. Knepley   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
93470034214SMatthew G. Knepley   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
93570034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9368a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
93770034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
93870034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
93970034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
94070034214SMatthew G. Knepley   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
94170034214SMatthew G. Knepley   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
94270034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
94370034214SMatthew G. Knepley   PetscFunctionReturn(0);
94470034214SMatthew G. Knepley }
94570034214SMatthew G. Knepley 
94670034214SMatthew G. Knepley /*@
94770034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
94870034214SMatthew G. Knepley 
94970034214SMatthew G. Knepley   Collective on DM
95070034214SMatthew G. Knepley 
95170034214SMatthew G. Knepley   Input Parameters:
95270034214SMatthew G. Knepley + dm - The DMPlex object
95370034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
95470034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
95570034214SMatthew G. Knepley - originalIS - The existing data
95670034214SMatthew G. Knepley 
95770034214SMatthew G. Knepley   Output Parameters:
95870034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
95970034214SMatthew G. Knepley - newIS - The new data
96070034214SMatthew G. Knepley 
96170034214SMatthew G. Knepley   Level: developer
96270034214SMatthew G. Knepley 
96370034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
96470034214SMatthew G. Knepley @*/
96570034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
96670034214SMatthew G. Knepley {
96770034214SMatthew G. Knepley   PetscSF         fieldSF;
96870034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
96970034214SMatthew G. Knepley   const PetscInt *originalValues;
97070034214SMatthew G. Knepley   PetscErrorCode  ierr;
97170034214SMatthew G. Knepley 
97270034214SMatthew G. Knepley   PetscFunctionBegin;
97370034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
97470034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
97570034214SMatthew G. Knepley 
97670034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
977854ce69bSBarry Smith   ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr);
97870034214SMatthew G. Knepley 
97970034214SMatthew G. Knepley   ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr);
98070034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9818a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
982aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
983aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
98470034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
98570034214SMatthew G. Knepley   ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr);
98670034214SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr);
98770034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
98870034214SMatthew G. Knepley   PetscFunctionReturn(0);
98970034214SMatthew G. Knepley }
99070034214SMatthew G. Knepley 
99170034214SMatthew G. Knepley /*@
99270034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
99370034214SMatthew G. Knepley 
99470034214SMatthew G. Knepley   Collective on DM
99570034214SMatthew G. Knepley 
99670034214SMatthew G. Knepley   Input Parameters:
99770034214SMatthew G. Knepley + dm - The DMPlex object
99870034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
99970034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
100070034214SMatthew G. Knepley . datatype - The type of data
100170034214SMatthew G. Knepley - originalData - The existing data
100270034214SMatthew G. Knepley 
100370034214SMatthew G. Knepley   Output Parameters:
100470034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
100570034214SMatthew G. Knepley - newData - The new data
100670034214SMatthew G. Knepley 
100770034214SMatthew G. Knepley   Level: developer
100870034214SMatthew G. Knepley 
100970034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
101070034214SMatthew G. Knepley @*/
101170034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
101270034214SMatthew G. Knepley {
101370034214SMatthew G. Knepley   PetscSF        fieldSF;
101470034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
101570034214SMatthew G. Knepley   PetscMPIInt    dataSize;
101670034214SMatthew G. Knepley   PetscErrorCode ierr;
101770034214SMatthew G. Knepley 
101870034214SMatthew G. Knepley   PetscFunctionBegin;
101970034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
102070034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
102170034214SMatthew G. Knepley 
102270034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
102370034214SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
102470034214SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
102570034214SMatthew G. Knepley 
102670034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
10278a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
102870034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
102970034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
103070034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
103170034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
103270034214SMatthew G. Knepley   PetscFunctionReturn(0);
103370034214SMatthew G. Knepley }
103470034214SMatthew G. Knepley 
103524cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1036cc71bff1SMichael Lange {
1037cc71bff1SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
1038cc71bff1SMichael Lange   MPI_Comm               comm;
1039cc71bff1SMichael Lange   PetscSF                coneSF;
1040cc71bff1SMichael Lange   PetscSection           originalConeSection, newConeSection;
1041ac04eaf7SToby Isaac   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
1042cc71bff1SMichael Lange   PetscBool              flg;
1043cc71bff1SMichael Lange   PetscErrorCode         ierr;
1044cc71bff1SMichael Lange 
1045cc71bff1SMichael Lange   PetscFunctionBegin;
1046cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1047cc71bff1SMichael Lange   PetscValidPointer(dmParallel,4);
1048cc71bff1SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1049cc71bff1SMichael Lange 
1050cc71bff1SMichael Lange   /* Distribute cone section */
1051cc71bff1SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1052cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
1053cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr);
1054cc71bff1SMichael Lange   ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
1055cc71bff1SMichael Lange   ierr = DMSetUp(dmParallel);CHKERRQ(ierr);
1056cc71bff1SMichael Lange   {
1057cc71bff1SMichael Lange     PetscInt pStart, pEnd, p;
1058cc71bff1SMichael Lange 
1059cc71bff1SMichael Lange     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
1060cc71bff1SMichael Lange     for (p = pStart; p < pEnd; ++p) {
1061cc71bff1SMichael Lange       PetscInt coneSize;
1062cc71bff1SMichael Lange       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
1063cc71bff1SMichael Lange       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
1064cc71bff1SMichael Lange     }
1065cc71bff1SMichael Lange   }
1066cc71bff1SMichael Lange   /* Communicate and renumber cones */
1067cc71bff1SMichael Lange   ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
10688a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1069cc71bff1SMichael Lange   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
1070ac04eaf7SToby Isaac   if (original) {
1071ac04eaf7SToby Isaac     PetscInt numCones;
1072ac04eaf7SToby Isaac 
1073ac04eaf7SToby Isaac     ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr); ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr);
1074ac04eaf7SToby Isaac     ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr);
1075ac04eaf7SToby Isaac   }
1076ac04eaf7SToby Isaac   else {
1077ac04eaf7SToby Isaac     globCones = cones;
1078ac04eaf7SToby Isaac   }
1079cc71bff1SMichael Lange   ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr);
1080ac04eaf7SToby Isaac   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1081ac04eaf7SToby Isaac   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1082ac04eaf7SToby Isaac   if (original) {
1083ac04eaf7SToby Isaac     ierr = PetscFree(globCones);CHKERRQ(ierr);
1084ac04eaf7SToby Isaac   }
1085cc71bff1SMichael Lange   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
1086cc71bff1SMichael Lange   ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
10873533c52bSMatthew G. Knepley #if PETSC_USE_DEBUG
10883533c52bSMatthew G. Knepley   {
10893533c52bSMatthew G. Knepley     PetscInt  p;
10903533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
10913533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
10923533c52bSMatthew G. Knepley       if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
10933533c52bSMatthew G. Knepley     }
10943533c52bSMatthew G. Knepley     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
10953533c52bSMatthew G. Knepley   }
10963533c52bSMatthew G. Knepley #endif
1097c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
1098cc71bff1SMichael Lange   if (flg) {
1099cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
1100cc71bff1SMichael Lange     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1101cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
1102cc71bff1SMichael Lange     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1103cc71bff1SMichael Lange     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
1104cc71bff1SMichael Lange   }
1105cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
1106cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr);
1107cc71bff1SMichael Lange   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1108cc71bff1SMichael Lange   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1109cc71bff1SMichael Lange   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
1110cc71bff1SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1111eaf898f9SPatrick Sanan   /* Create supports and stratify DMPlex */
1112cc71bff1SMichael Lange   {
1113cc71bff1SMichael Lange     PetscInt pStart, pEnd;
1114cc71bff1SMichael Lange 
1115cc71bff1SMichael Lange     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1116cc71bff1SMichael Lange     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1117cc71bff1SMichael Lange   }
1118cc71bff1SMichael Lange   ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr);
1119cc71bff1SMichael Lange   ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr);
1120*1cf84007SMatthew G. Knepley   {
1121*1cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
1122*1cf84007SMatthew G. Knepley 
1123*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
1124*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
1125*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
1126*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
1127*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
1128*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
1129*1cf84007SMatthew G. Knepley   }
1130cc71bff1SMichael Lange   PetscFunctionReturn(0);
1131cc71bff1SMichael Lange }
1132cc71bff1SMichael Lange 
113324cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
11340df0e737SMichael Lange {
11350df0e737SMichael Lange   MPI_Comm         comm;
11360df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
11370df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
11380df0e737SMichael Lange   PetscInt         bs;
113990b157c4SStefano Zampini   PetscBool        isper;
11400df0e737SMichael Lange   const char      *name;
11410df0e737SMichael Lange   const PetscReal *maxCell, *L;
11425dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
11430df0e737SMichael Lange   PetscErrorCode   ierr;
11440df0e737SMichael Lange 
11450df0e737SMichael Lange   PetscFunctionBegin;
11460df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11470df0e737SMichael Lange   PetscValidPointer(dmParallel, 3);
11480df0e737SMichael Lange 
11490df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
11500df0e737SMichael Lange   ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
11510df0e737SMichael Lange   ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr);
11520df0e737SMichael Lange   ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
11530df0e737SMichael Lange   if (originalCoordinates) {
11548b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
11550df0e737SMichael Lange     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
11560df0e737SMichael Lange     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
11570df0e737SMichael Lange 
11580df0e737SMichael Lange     ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
11590df0e737SMichael Lange     ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr);
11600df0e737SMichael Lange     ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr);
11610df0e737SMichael Lange     ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr);
11620df0e737SMichael Lange     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
11630df0e737SMichael Lange   }
116490b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
116590b157c4SStefano Zampini   ierr = DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);CHKERRQ(ierr);
11660df0e737SMichael Lange   PetscFunctionReturn(0);
11670df0e737SMichael Lange }
11680df0e737SMichael Lange 
1169d995df53SMatthew G. Knepley /* Here we are assuming that process 0 always has everything */
117024cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
11710df0e737SMichael Lange {
1172df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex*) dm->data;
11730df0e737SMichael Lange   MPI_Comm         comm;
11747980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
11750df0e737SMichael Lange   PetscMPIInt      rank;
11767980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1177df0420ecSMatthew G. Knepley   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1178df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
11790df0e737SMichael Lange   PetscErrorCode   ierr;
11800df0e737SMichael Lange 
11810df0e737SMichael Lange   PetscFunctionBegin;
11820df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11832eb1fa14SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
11840df0e737SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
11850df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
11860df0e737SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
11870df0e737SMichael Lange 
1188df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
11897980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
11907980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
11917980c9d4SMatthew G. Knepley   if (depthLabel) {ierr = DMLabelGetState(depthLabel, &depthState);CHKERRQ(ierr);}
1192df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1193b2566f29SBarry Smith   ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
1194df0420ecSMatthew G. Knepley   if (sendDepth) {
11957980c9d4SMatthew G. Knepley     ierr = DMRemoveLabel(dmParallel, "depth", &depthLabel);CHKERRQ(ierr);
11967980c9d4SMatthew G. Knepley     ierr = DMLabelDestroy(&depthLabel);CHKERRQ(ierr);
1197df0420ecSMatthew G. Knepley   }
1198d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
1199c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr);
1200d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
12010df0e737SMichael Lange   ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1202627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1203bdd2d751SMichael Lange   for (l = numLabels-1; l >= 0; --l) {
1204bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
12050df0e737SMichael Lange     PetscBool   isdepth;
12060df0e737SMichael Lange 
1207d995df53SMatthew G. Knepley     if (hasLabels) {
1208c58f1c22SToby Isaac       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
12090df0e737SMichael Lange       /* Skip "depth" because it is recreated */
1210bdd2d751SMichael Lange       ierr = PetscStrcmp(label->name, "depth", &isdepth);CHKERRQ(ierr);
1211bdd2d751SMichael Lange     }
12120df0e737SMichael Lange     ierr = MPI_Bcast(&isdepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
1213df0420ecSMatthew G. Knepley     if (isdepth && !sendDepth) continue;
1214bdd2d751SMichael Lange     ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr);
12157980c9d4SMatthew G. Knepley     if (isdepth) {
12167980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
12177980c9d4SMatthew G. Knepley       PetscInt gdepth;
12187980c9d4SMatthew G. Knepley 
12197980c9d4SMatthew G. Knepley       ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
12207980c9d4SMatthew G. Knepley       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
12217980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
12227980c9d4SMatthew G. Knepley         PetscBool has;
12237980c9d4SMatthew G. Knepley 
12247980c9d4SMatthew G. Knepley         ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr);
12257980c9d4SMatthew G. Knepley         if (!has) ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);
12267980c9d4SMatthew G. Knepley       }
12277980c9d4SMatthew G. Knepley     }
1228c58f1c22SToby Isaac     ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr);
12290df0e737SMichael Lange   }
12300df0e737SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
12310df0e737SMichael Lange   PetscFunctionReturn(0);
12320df0e737SMichael Lange }
12330df0e737SMichael Lange 
123424cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
12359734c634SMichael Lange {
12369734c634SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
12379734c634SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1238f0e73a3dSToby Isaac   PetscBool      *isHybrid, *isHybridParallel;
1239f0e73a3dSToby Isaac   PetscInt        dim, depth, d;
1240f0e73a3dSToby Isaac   PetscInt        pStart, pEnd, pStartP, pEndP;
12419734c634SMichael Lange   PetscErrorCode  ierr;
12429734c634SMichael Lange 
12439734c634SMichael Lange   PetscFunctionBegin;
12449734c634SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12459734c634SMichael Lange   PetscValidPointer(dmParallel, 4);
12469734c634SMichael Lange 
12479734c634SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12489734c634SMichael Lange   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1249f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1250f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dmParallel,&pStartP,&pEndP);CHKERRQ(ierr);
1251f0e73a3dSToby Isaac   ierr = PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);CHKERRQ(ierr);
1252f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1253f0e73a3dSToby Isaac     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];
12549734c634SMichael Lange 
1255f0e73a3dSToby Isaac     if (hybridMax >= 0) {
1256f0e73a3dSToby Isaac       PetscInt sStart, sEnd, p;
12579734c634SMichael Lange 
1258f0e73a3dSToby Isaac       ierr = DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);CHKERRQ(ierr);
1259f0e73a3dSToby Isaac       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
12609734c634SMichael Lange     }
12619734c634SMichael Lange   }
1262f0e73a3dSToby Isaac   ierr = PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1263f0e73a3dSToby Isaac   ierr = PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1264f0e73a3dSToby Isaac   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1265f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1266f0e73a3dSToby Isaac     PetscInt sStart, sEnd, p, dd;
1267f0e73a3dSToby Isaac 
1268f0e73a3dSToby Isaac     ierr = DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);CHKERRQ(ierr);
1269f0e73a3dSToby Isaac     dd = (depth == 1 && d == 1) ? dim : d;
1270f0e73a3dSToby Isaac     for (p = sStart; p < sEnd; p++) {
1271f0e73a3dSToby Isaac       if (isHybridParallel[p-pStartP]) {
1272f0e73a3dSToby Isaac         pmesh->hybridPointMax[dd] = p;
1273f0e73a3dSToby Isaac         break;
1274f0e73a3dSToby Isaac       }
1275f0e73a3dSToby Isaac     }
1276f0e73a3dSToby Isaac   }
1277f0e73a3dSToby Isaac   ierr = PetscFree2(isHybrid,isHybridParallel);CHKERRQ(ierr);
12789734c634SMichael Lange   PetscFunctionReturn(0);
12799734c634SMichael Lange }
12800df0e737SMichael Lange 
128124cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1282a6f36705SMichael Lange {
128315078cd4SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
128415078cd4SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1285a6f36705SMichael Lange   MPI_Comm        comm;
1286a6f36705SMichael Lange   DM              refTree;
1287a6f36705SMichael Lange   PetscSection    origParentSection, newParentSection;
1288a6f36705SMichael Lange   PetscInt        *origParents, *origChildIDs;
1289a6f36705SMichael Lange   PetscBool       flg;
1290a6f36705SMichael Lange   PetscErrorCode  ierr;
1291a6f36705SMichael Lange 
1292a6f36705SMichael Lange   PetscFunctionBegin;
1293a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1294a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 4);
1295a6f36705SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1296a6f36705SMichael Lange 
1297a6f36705SMichael Lange   /* Set up tree */
1298a6f36705SMichael Lange   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1299a6f36705SMichael Lange   ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr);
1300a6f36705SMichael Lange   ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr);
1301a6f36705SMichael Lange   if (origParentSection) {
1302a6f36705SMichael Lange     PetscInt        pStart, pEnd;
130308633170SToby Isaac     PetscInt        *newParents, *newChildIDs, *globParents;
1304a6f36705SMichael Lange     PetscInt        *remoteOffsetsParents, newParentSize;
1305a6f36705SMichael Lange     PetscSF         parentSF;
1306a6f36705SMichael Lange 
1307a6f36705SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
1308a6f36705SMichael Lange     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr);
1309a6f36705SMichael Lange     ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr);
1310a6f36705SMichael Lange     ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr);
1311a6f36705SMichael Lange     ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr);
13128a713f3bSLawrence Mitchell     ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr);
1313a6f36705SMichael Lange     ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr);
1314a6f36705SMichael Lange     ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr);
131508633170SToby Isaac     if (original) {
131608633170SToby Isaac       PetscInt numParents;
131708633170SToby Isaac 
131808633170SToby Isaac       ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr);
131908633170SToby Isaac       ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr);
132008633170SToby Isaac       ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr);
132108633170SToby Isaac     }
132208633170SToby Isaac     else {
132308633170SToby Isaac       globParents = origParents;
132408633170SToby Isaac     }
132508633170SToby Isaac     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
132608633170SToby Isaac     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
132708633170SToby Isaac     if (original) {
132808633170SToby Isaac       ierr = PetscFree(globParents);CHKERRQ(ierr);
132908633170SToby Isaac     }
1330a6f36705SMichael Lange     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1331a6f36705SMichael Lange     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1332a6f36705SMichael Lange     ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr);
13334a54e071SToby Isaac #if PETSC_USE_DEBUG
13344a54e071SToby Isaac     {
13354a54e071SToby Isaac       PetscInt  p;
13364a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
13374a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
13384a54e071SToby Isaac         if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
13394a54e071SToby Isaac       }
13404a54e071SToby Isaac       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
13414a54e071SToby Isaac     }
13424a54e071SToby Isaac #endif
1343c5929fdfSBarry Smith     ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr);
1344a6f36705SMichael Lange     if (flg) {
1345a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr);
1346a6f36705SMichael Lange       ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1347a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr);
1348a6f36705SMichael Lange       ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1349a6f36705SMichael Lange       ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr);
1350a6f36705SMichael Lange     }
1351a6f36705SMichael Lange     ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr);
1352a6f36705SMichael Lange     ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr);
1353a6f36705SMichael Lange     ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr);
1354a6f36705SMichael Lange     ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr);
1355a6f36705SMichael Lange   }
135615078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1357a6f36705SMichael Lange   PetscFunctionReturn(0);
1358a6f36705SMichael Lange }
13590df0e737SMichael Lange 
136024cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
13614eca1733SMichael Lange {
13629852e123SBarry Smith   PetscMPIInt            rank, size;
13634eca1733SMichael Lange   MPI_Comm               comm;
13644eca1733SMichael Lange   PetscErrorCode         ierr;
13654eca1733SMichael Lange 
13664eca1733SMichael Lange   PetscFunctionBegin;
13674eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13684eca1733SMichael Lange   PetscValidPointer(dmParallel,7);
13694eca1733SMichael Lange 
13704eca1733SMichael Lange   /* Create point SF for parallel mesh */
13714eca1733SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13724eca1733SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
13734eca1733SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
13749852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
13754eca1733SMichael Lange   {
13764eca1733SMichael Lange     const PetscInt *leaves;
13774eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
13784eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
13794eca1733SMichael Lange     PetscInt        pStart, pEnd;
13804eca1733SMichael Lange 
13814eca1733SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
13824eca1733SMichael Lange     ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
13834eca1733SMichael Lange     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
13844eca1733SMichael Lange     for (p=0; p<numRoots; p++) {
13854eca1733SMichael Lange       rowners[p].rank  = -1;
13864eca1733SMichael Lange       rowners[p].index = -1;
13874eca1733SMichael Lange     }
13884eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13894eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13904eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
13914eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
13924eca1733SMichael Lange         lowners[p].rank  = rank;
13934eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
13944eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
13954eca1733SMichael Lange         lowners[p].rank  = -2;
13964eca1733SMichael Lange         lowners[p].index = -2;
13974eca1733SMichael Lange       }
13984eca1733SMichael Lange     }
13994eca1733SMichael Lange     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
14004eca1733SMichael Lange       rowners[p].rank  = -3;
14014eca1733SMichael Lange       rowners[p].index = -3;
14024eca1733SMichael Lange     }
14034eca1733SMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14044eca1733SMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
14054eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14064eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
14074eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
14084eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
14094eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
14104eca1733SMichael Lange     }
14114eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr);
14124eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
14134eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
14144eca1733SMichael Lange       if (lowners[p].rank != rank) {
14154eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
14164eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
14174eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
14184eca1733SMichael Lange         ++gp;
14194eca1733SMichael Lange       }
14204eca1733SMichael Lange     }
14214eca1733SMichael Lange     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
14224eca1733SMichael Lange     ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
14234eca1733SMichael Lange     ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr);
14244eca1733SMichael Lange   }
1425*1cf84007SMatthew G. Knepley   {
1426*1cf84007SMatthew G. Knepley     PetscBool useCone, useClosure, useAnchors;
1427*1cf84007SMatthew G. Knepley 
1428*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
1429*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
1430*1cf84007SMatthew G. Knepley     ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr);
1431*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseCone(dmParallel, useCone);CHKERRQ(ierr);
1432*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseClosure(dmParallel, useClosure);CHKERRQ(ierr);
1433*1cf84007SMatthew G. Knepley     ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr);
1434*1cf84007SMatthew G. Knepley   }
14354eca1733SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
14364eca1733SMichael Lange   PetscFunctionReturn(0);
14374eca1733SMichael Lange }
14384eca1733SMichael Lange 
1439f5bf2dbfSMichael Lange /*@C
1440f5bf2dbfSMichael Lange   DMPlexDerivePointSF - Build a point SF from an SF describing a point migration
1441f5bf2dbfSMichael Lange 
1442f5bf2dbfSMichael Lange   Input Parameter:
1443f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1444f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
14451627f6ccSMichael Lange . ownership   - Flag causing a vote to determine point ownership
1446f5bf2dbfSMichael Lange 
1447f5bf2dbfSMichael Lange   Output Parameter:
1448f5bf2dbfSMichael Lange - pointSF     - The star forest describing the point overlap in the remapped DM
1449f5bf2dbfSMichael Lange 
1450f5bf2dbfSMichael Lange   Level: developer
1451f5bf2dbfSMichael Lange 
1452f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1453f5bf2dbfSMichael Lange @*/
1454f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1455f5bf2dbfSMichael Lange {
1456f5bf2dbfSMichael Lange   PetscMPIInt        rank;
14571627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1458f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1459f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1460f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1461f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1462f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1463f5bf2dbfSMichael Lange 
1464f5bf2dbfSMichael Lange   PetscFunctionBegin;
1465f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1466f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1467f5bf2dbfSMichael Lange 
1468f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1469f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1470f5bf2dbfSMichael Lange   if (ownership) {
14711627f6ccSMichael Lange     /* Point ownership vote: Process with highest rank ownes shared points */
1472f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
1473f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1474f5bf2dbfSMichael Lange       leafNodes[p].rank  = rank;
147543f7d02bSMichael Lange       leafNodes[p].index = p;
1476f5bf2dbfSMichael Lange     }
1477f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
14781627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1479f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1480f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1481f5bf2dbfSMichael Lange     }
1482f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1483f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1484f5bf2dbfSMichael Lange   } else {
1485f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1486f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1487f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1488f5bf2dbfSMichael Lange     };
1489f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1490f5bf2dbfSMichael Lange       /* Write new local id into old location */
1491f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
14926462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1493f5bf2dbfSMichael Lange       }
1494f5bf2dbfSMichael Lange     }
1495f5bf2dbfSMichael Lange   }
1496f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1497f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1498f5bf2dbfSMichael Lange 
1499f5bf2dbfSMichael Lange   for (npointLeaves = 0, p = 0; p < nleaves; p++) {if (leafNodes[p].rank != rank) npointLeaves++;}
15001627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
15011627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1502f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1503f5bf2dbfSMichael Lange     if (leafNodes[p].rank != rank) {
1504f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1505f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1506f5bf2dbfSMichael Lange       idx++;
1507f5bf2dbfSMichael Lange     }
1508f5bf2dbfSMichael Lange   }
1509f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
15101627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1511f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1512f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1513f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1514f5bf2dbfSMichael Lange }
1515f5bf2dbfSMichael Lange 
151615078cd4SMichael Lange /*@C
151715078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
151815078cd4SMichael Lange 
151915078cd4SMichael Lange   Input Parameter:
152015078cd4SMichael Lange + dm       - The source DMPlex object
15211627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
152215078cd4SMichael Lange 
152315078cd4SMichael Lange   Output Parameter:
152415078cd4SMichael Lange - targetDM - The target DMPlex object
152515078cd4SMichael Lange 
1526b9f40539SMichael Lange   Level: intermediate
152715078cd4SMichael Lange 
152815078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
152915078cd4SMichael Lange @*/
1530b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
153115078cd4SMichael Lange {
1532b9f40539SMichael Lange   MPI_Comm               comm;
1533b9f40539SMichael Lange   PetscInt               dim, nroots;
1534b9f40539SMichael Lange   PetscSF                sfPoint;
153515078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1536ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1537b9f40539SMichael Lange   PetscBool              flg;
153815078cd4SMichael Lange   PetscErrorCode         ierr;
153915078cd4SMichael Lange 
154015078cd4SMichael Lange   PetscFunctionBegin;
154115078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15421b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1543b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
154415078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
154515078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
154615078cd4SMichael Lange 
1547bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1548b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1549b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1550bfb0467fSMichael Lange   if (nroots >= 0) {
1551b9f40539SMichael Lange     IS                     isOriginal;
1552ac04eaf7SToby Isaac     PetscInt               n, size, nleaves;
1553ac04eaf7SToby Isaac     PetscInt              *numbering_orig, *numbering_new;
1554b9f40539SMichael Lange     /* Get the original point numbering */
1555b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1556b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1557b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1558b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1559b9f40539SMichael Lange     /* Convert to positive global numbers */
1560b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1561b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1562b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1563b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1564b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1565b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1566b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1567b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1568b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
156915078cd4SMichael Lange   } else {
1570bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
157115078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
157215078cd4SMichael Lange   }
1573c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1574b9f40539SMichael Lange   if (flg) {
1575b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1576b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1577b9f40539SMichael Lange   }
157815078cd4SMichael Lange   /* Migrate DM data to target DM */
1579ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
158015078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
15810dc1530dSSander Arens   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
158215078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
158308633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1584ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1585302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
15861b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
158715078cd4SMichael Lange   PetscFunctionReturn(0);
158815078cd4SMichael Lange }
158915078cd4SMichael Lange 
15903b8f15a2SMatthew G. Knepley /*@C
159170034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
159270034214SMatthew G. Knepley 
159370034214SMatthew G. Knepley   Not Collective
159470034214SMatthew G. Knepley 
159570034214SMatthew G. Knepley   Input Parameter:
159670034214SMatthew G. Knepley + dm  - The original DMPlex object
159770034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
159870034214SMatthew G. Knepley 
159970034214SMatthew G. Knepley   Output Parameter:
160070034214SMatthew G. Knepley + sf - The PetscSF used for point distribution
160170034214SMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
160270034214SMatthew G. Knepley 
160370034214SMatthew G. Knepley   Note: If the mesh was not distributed, the return value is NULL.
160470034214SMatthew G. Knepley 
160593a1dc33SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMPlexSetAdjacencyUseCone() and
160670034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
160770034214SMatthew G. Knepley   representation on the mesh.
160870034214SMatthew G. Knepley 
160970034214SMatthew G. Knepley   Level: intermediate
161070034214SMatthew G. Knepley 
161170034214SMatthew G. Knepley .keywords: mesh, elements
161270034214SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
161370034214SMatthew G. Knepley @*/
161480cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
161570034214SMatthew G. Knepley {
161670034214SMatthew G. Knepley   MPI_Comm               comm;
161715078cd4SMichael Lange   PetscPartitioner       partitioner;
1618f8987ae8SMichael Lange   IS                     cellPart;
1619f8987ae8SMichael Lange   PetscSection           cellPartSection;
1620cf86098cSMatthew G. Knepley   DM                     dmCoord;
1621f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
162243f7d02bSMichael Lange   PetscSF                sfProcess, sfMigration, sfStratified, sfPoint;
162370034214SMatthew G. Knepley   PetscBool              flg;
16249852e123SBarry Smith   PetscMPIInt            rank, size, p;
162570034214SMatthew G. Knepley   PetscErrorCode         ierr;
162670034214SMatthew G. Knepley 
162770034214SMatthew G. Knepley   PetscFunctionBegin;
162870034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
162970034214SMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
163070034214SMatthew G. Knepley   PetscValidPointer(dmParallel,5);
163170034214SMatthew G. Knepley 
163270034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
163370034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
163470034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
16359852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
163670034214SMatthew G. Knepley 
1637a6ba33f0SVaclav Hapla   if (sf) *sf = NULL;
163870034214SMatthew G. Knepley   *dmParallel = NULL;
16399852e123SBarry Smith   if (size == 1) {
16405f3267c8SKoos Huijssen     ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
16415f3267c8SKoos Huijssen     PetscFunctionReturn(0);
16425f3267c8SKoos Huijssen   }
164370034214SMatthew G. Knepley 
164415078cd4SMichael Lange   /* Create cell partition */
164577623264SMatthew G. Knepley   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
164677623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
164715078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
164815078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1649f8987ae8SMichael Lange   {
1650f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1651f8987ae8SMichael Lange     PetscInt proc, pStart, pEnd, npoints, poffset;
1652f8987ae8SMichael Lange     const PetscInt *points;
1653f8987ae8SMichael Lange     ierr = DMLabelCreate("Point Partition", &lblPartition);CHKERRQ(ierr);
1654f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1655f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1656f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1657f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1658f8987ae8SMichael Lange       ierr = PetscSectionGetOffset(cellPartSection, proc, &poffset);CHKERRQ(ierr);
1659f8987ae8SMichael Lange       for (p = poffset; p < poffset+npoints; p++) {
1660f8987ae8SMichael Lange         ierr = DMLabelSetValue(lblPartition, points[p], proc);CHKERRQ(ierr);
166170034214SMatthew G. Knepley       }
1662f8987ae8SMichael Lange     }
1663f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1664f8987ae8SMichael Lange   }
1665f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelClosure(dm, lblPartition);CHKERRQ(ierr);
1666f8987ae8SMichael Lange   {
1667f8987ae8SMichael Lange     /* Build a global process SF */
1668f8987ae8SMichael Lange     PetscSFNode *remoteProc;
16699852e123SBarry Smith     ierr = PetscMalloc1(size, &remoteProc);CHKERRQ(ierr);
16709852e123SBarry Smith     for (p = 0; p < size; ++p) {
1671f8987ae8SMichael Lange       remoteProc[p].rank  = p;
1672f8987ae8SMichael Lange       remoteProc[p].index = rank;
1673f8987ae8SMichael Lange     }
1674f8987ae8SMichael Lange     ierr = PetscSFCreate(comm, &sfProcess);CHKERRQ(ierr);
1675f8987ae8SMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProcess, "Process SF");CHKERRQ(ierr);
16769852e123SBarry Smith     ierr = PetscSFSetGraph(sfProcess, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
1677f8987ae8SMichael Lange   }
1678f8987ae8SMichael Lange   ierr = DMLabelCreate("Point migration", &lblMigration);CHKERRQ(ierr);
1679f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, sfProcess, lblMigration);CHKERRQ(ierr);
1680302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
168143f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
168243f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
168343f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
168443f7d02bSMichael Lange   sfMigration = sfStratified;
1685f8987ae8SMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1686c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
168770034214SMatthew G. Knepley   if (flg) {
1688f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1689f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
169070034214SMatthew G. Knepley   }
1691f8987ae8SMichael Lange 
169215078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
169370034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
169470034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1695b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
169670034214SMatthew G. Knepley 
1697a157612eSMichael Lange   /* Build the point SF without overlap */
16981627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1699f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1700cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1701cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1702f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
170370034214SMatthew G. Knepley 
1704a157612eSMichael Lange   if (overlap > 0) {
170515078cd4SMichael Lange     DM                 dmOverlap;
170615078cd4SMichael Lange     PetscInt           nroots, nleaves;
170715078cd4SMichael Lange     PetscSFNode       *newRemote;
170815078cd4SMichael Lange     const PetscSFNode *oldRemote;
170915078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1710a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1711b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1712a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1713a157612eSMichael Lange     *dmParallel = dmOverlap;
1714c389ea9fSToby Isaac     if (flg) {
17153d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
171615078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1717c389ea9fSToby Isaac     }
171843331d4aSMichael Lange 
1719f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1720f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
172115078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
172243331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
172315078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
172415078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
172515078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
172615078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
172715078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1728f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
172915078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1730c389ea9fSToby Isaac   }
1731bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1732f8987ae8SMichael Lange   ierr = PetscSFDestroy(&sfProcess);CHKERRQ(ierr);
1733f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1734f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
17354eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
17364eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1737721cbd76SMatthew G. Knepley   /* Copy BC */
1738a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
173966fe0bfeSMatthew G. Knepley   /* Create sfNatural */
174066fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
174166fe0bfeSMatthew G. Knepley     PetscSection section;
174266fe0bfeSMatthew G. Knepley 
174366fe0bfeSMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
17440c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
174566fe0bfeSMatthew G. Knepley   }
1746721cbd76SMatthew G. Knepley   /* Cleanup */
1747f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
1748f8987ae8SMichael Lange   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1749f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
175070034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
175170034214SMatthew G. Knepley   PetscFunctionReturn(0);
175270034214SMatthew G. Knepley }
1753a157612eSMichael Lange 
1754a157612eSMichael Lange /*@C
175524cc2ca5SMatthew G. Knepley   DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM.
1756a157612eSMichael Lange 
1757a157612eSMichael Lange   Not Collective
1758a157612eSMichael Lange 
1759a157612eSMichael Lange   Input Parameter:
1760a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
1761a157612eSMichael Lange - overlap - The overlap of partitions, 0 is the default
1762a157612eSMichael Lange 
1763a157612eSMichael Lange   Output Parameter:
1764a157612eSMichael Lange + sf - The PetscSF used for point distribution
1765a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1766a157612eSMichael Lange 
1767a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1768a157612eSMichael Lange 
1769a157612eSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
1770a157612eSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
1771a157612eSMichael Lange   representation on the mesh.
1772a157612eSMichael Lange 
1773a157612eSMichael Lange   Level: intermediate
1774a157612eSMichael Lange 
1775a157612eSMichael Lange .keywords: mesh, elements
1776a157612eSMichael Lange .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
1777a157612eSMichael Lange @*/
1778b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1779a157612eSMichael Lange {
1780a157612eSMichael Lange   MPI_Comm               comm;
17813567eaeeSMatthew G. Knepley   PetscMPIInt            size, rank;
1782a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1783a157612eSMichael Lange   IS                     rootrank, leafrank;
1784cf86098cSMatthew G. Knepley   DM                     dmCoord;
1785a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1786f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1787a157612eSMichael Lange   PetscErrorCode         ierr;
1788a157612eSMichael Lange 
1789a157612eSMichael Lange   PetscFunctionBegin;
1790a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1791a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1792a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1793a157612eSMichael Lange 
1794a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
17953567eaeeSMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1796a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
17973567eaeeSMatthew G. Knepley   if (size == 1) {*dmOverlap = NULL; PetscFunctionReturn(0);}
17983567eaeeSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1799a157612eSMichael Lange 
1800a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
1801a157612eSMichael Lange   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1802a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1803a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1804a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1805a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1806a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1807a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1808a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1809a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1810a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1811a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1812a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1813a9f1d5b2SMichael Lange 
181415fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
181515fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
181615fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
181715fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
1818a157612eSMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1819a157612eSMichael Lange 
1820a157612eSMichael Lange   /* Build the overlapping DM */
1821a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1822a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1823a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1824f5bf2dbfSMichael Lange   /* Build the new point SF */
18251627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1826f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1827cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1828cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1829f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1830a157612eSMichael Lange   /* Cleanup overlap partition */
1831a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1832a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1833a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
18341b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1835a157612eSMichael Lange   PetscFunctionReturn(0);
1836a157612eSMichael Lange }
18376462276dSToby Isaac 
18386462276dSToby Isaac /*@C
18396462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
18406462276dSToby Isaac   root process of the original's communicator.
18416462276dSToby Isaac 
18426462276dSToby Isaac   Input Parameters:
18436462276dSToby Isaac . dm - the original DMPlex object
18446462276dSToby Isaac 
18456462276dSToby Isaac   Output Parameters:
18466462276dSToby Isaac . gatherMesh - the gathered DM object, or NULL
18476462276dSToby Isaac 
18486462276dSToby Isaac   Level: intermediate
18496462276dSToby Isaac 
18506462276dSToby Isaac .keywords: mesh
18516462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
18526462276dSToby Isaac @*/
18536462276dSToby Isaac PetscErrorCode DMPlexGetGatherDM(DM dm, DM * gatherMesh)
18546462276dSToby Isaac {
18556462276dSToby Isaac   MPI_Comm       comm;
18566462276dSToby Isaac   PetscMPIInt    size;
18576462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
18586462276dSToby Isaac   PetscErrorCode ierr;
18596462276dSToby Isaac 
18606462276dSToby Isaac   PetscFunctionBegin;
18616462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18626462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18636462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18646462276dSToby Isaac   *gatherMesh = NULL;
18656462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18666462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
18676462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
18686462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
18696462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
18706462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
18716462276dSToby Isaac   ierr = DMPlexDistribute(dm,0,NULL,gatherMesh);CHKERRQ(ierr);
18726462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
18736462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
18746462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
18756462276dSToby Isaac   PetscFunctionReturn(0);
18766462276dSToby Isaac }
18776462276dSToby Isaac 
18786462276dSToby Isaac /*@C
18796462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
18806462276dSToby Isaac 
18816462276dSToby Isaac   Input Parameters:
18826462276dSToby Isaac . dm - the original DMPlex object
18836462276dSToby Isaac 
18846462276dSToby Isaac   Output Parameters:
18856462276dSToby Isaac . redundantMesh - the redundant DM object, or NULL
18866462276dSToby Isaac 
18876462276dSToby Isaac   Level: intermediate
18886462276dSToby Isaac 
18896462276dSToby Isaac .keywords: mesh
18906462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
18916462276dSToby Isaac @*/
18926462276dSToby Isaac PetscErrorCode DMPlexGetRedundantDM(DM dm, DM * redundantMesh)
18936462276dSToby Isaac {
18946462276dSToby Isaac   MPI_Comm       comm;
18956462276dSToby Isaac   PetscMPIInt    size, rank;
18966462276dSToby Isaac   PetscInt       pStart, pEnd, p;
18976462276dSToby Isaac   PetscInt       numPoints = -1;
18986462276dSToby Isaac   PetscSF        migrationSF, sfPoint;
18996462276dSToby Isaac   DM             gatherDM, dmCoord;
19006462276dSToby Isaac   PetscSFNode    *points;
19016462276dSToby Isaac   PetscErrorCode ierr;
19026462276dSToby Isaac 
19036462276dSToby Isaac   PetscFunctionBegin;
19046462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19056462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
19066462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19076462276dSToby Isaac   *redundantMesh = NULL;
190868dbc166SMatthew G. Knepley   if (size == 1) {
190968dbc166SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
191068dbc166SMatthew G. Knepley     *redundantMesh = dm;
191168dbc166SMatthew G. Knepley     PetscFunctionReturn(0);
191268dbc166SMatthew G. Knepley   }
19136462276dSToby Isaac   ierr = DMPlexGetGatherDM(dm,&gatherDM);CHKERRQ(ierr);
19146462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
19156462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
19166462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
19176462276dSToby Isaac   numPoints = pEnd - pStart;
19186462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
19196462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
19206462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
19216462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
19226462276dSToby Isaac     points[p].index = p;
19236462276dSToby Isaac     points[p].rank  = 0;
19246462276dSToby Isaac   }
19256462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
19266462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
19276462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
19286462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
19296462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
19306462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
19316462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
19326462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
19336462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
19346462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
19356462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
19366462276dSToby Isaac   PetscFunctionReturn(0);
19376462276dSToby Isaac }
1938