xref: /petsc/src/dm/impls/plex/plexdistribute.c (revision f0e73a3def65061dd4c3009f6e41c1d325a1c4f7)
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 
470034214SMatthew G. Knepley #undef __FUNCT__
570034214SMatthew G. Knepley #define __FUNCT__ "DMPlexSetAdjacencyUseCone"
670034214SMatthew G. Knepley /*@
770034214SMatthew G. Knepley   DMPlexSetAdjacencyUseCone - Define adjacency in the mesh using either the cone or the support first
870034214SMatthew G. Knepley 
970034214SMatthew G. Knepley   Input Parameters:
1070034214SMatthew G. Knepley + dm      - The DM object
1170034214SMatthew G. Knepley - useCone - Flag to use the cone first
1270034214SMatthew G. Knepley 
1370034214SMatthew G. Knepley   Level: intermediate
1470034214SMatthew G. Knepley 
1570034214SMatthew G. Knepley   Notes:
1670034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
174b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
1870034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
1970034214SMatthew G. Knepley 
2070034214SMatthew G. Knepley .seealso: DMPlexGetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexGetAdjacencyUseClosure(), DMPlexDistribute(), DMPlexPreallocateOperator()
2170034214SMatthew G. Knepley @*/
2270034214SMatthew G. Knepley PetscErrorCode DMPlexSetAdjacencyUseCone(DM dm, PetscBool useCone)
2370034214SMatthew G. Knepley {
2470034214SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *) dm->data;
2570034214SMatthew G. Knepley 
2670034214SMatthew G. Knepley   PetscFunctionBegin;
2770034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2870034214SMatthew G. Knepley   mesh->useCone = useCone;
2970034214SMatthew G. Knepley   PetscFunctionReturn(0);
3070034214SMatthew G. Knepley }
3170034214SMatthew G. Knepley 
3270034214SMatthew G. Knepley #undef __FUNCT__
3370034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacencyUseCone"
3470034214SMatthew G. Knepley /*@
3570034214SMatthew G. Knepley   DMPlexGetAdjacencyUseCone - Query whether adjacency in the mesh uses the cone or the support first
3670034214SMatthew G. Knepley 
3770034214SMatthew G. Knepley   Input Parameter:
3870034214SMatthew G. Knepley . dm      - The DM object
3970034214SMatthew G. Knepley 
4070034214SMatthew G. Knepley   Output Parameter:
4170034214SMatthew G. Knepley . useCone - Flag to use the cone first
4270034214SMatthew G. Knepley 
4370034214SMatthew G. Knepley   Level: intermediate
4470034214SMatthew G. Knepley 
4570034214SMatthew G. Knepley   Notes:
4670034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
474b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4870034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4970034214SMatthew G. Knepley 
5070034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexGetAdjacencyUseClosure(), DMPlexDistribute(), DMPlexPreallocateOperator()
5170034214SMatthew G. Knepley @*/
5270034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseCone(DM dm, PetscBool *useCone)
5370034214SMatthew G. Knepley {
5470034214SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *) dm->data;
5570034214SMatthew G. Knepley 
5670034214SMatthew G. Knepley   PetscFunctionBegin;
5770034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5870034214SMatthew G. Knepley   PetscValidIntPointer(useCone, 2);
5970034214SMatthew G. Knepley   *useCone = mesh->useCone;
6070034214SMatthew G. Knepley   PetscFunctionReturn(0);
6170034214SMatthew G. Knepley }
6270034214SMatthew G. Knepley 
6370034214SMatthew G. Knepley #undef __FUNCT__
6470034214SMatthew G. Knepley #define __FUNCT__ "DMPlexSetAdjacencyUseClosure"
6570034214SMatthew G. Knepley /*@
6670034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure - Define adjacency in the mesh using the transitive closure
6770034214SMatthew G. Knepley 
6870034214SMatthew G. Knepley   Input Parameters:
6970034214SMatthew G. Knepley + dm      - The DM object
7070034214SMatthew G. Knepley - useClosure - Flag to use the closure
7170034214SMatthew G. Knepley 
7270034214SMatthew G. Knepley   Level: intermediate
7370034214SMatthew G. Knepley 
7470034214SMatthew G. Knepley   Notes:
7570034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
764b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
7770034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
7870034214SMatthew G. Knepley 
7970034214SMatthew G. Knepley .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
8070034214SMatthew G. Knepley @*/
8170034214SMatthew G. Knepley PetscErrorCode DMPlexSetAdjacencyUseClosure(DM dm, PetscBool useClosure)
8270034214SMatthew G. Knepley {
8370034214SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *) dm->data;
8470034214SMatthew G. Knepley 
8570034214SMatthew G. Knepley   PetscFunctionBegin;
8670034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8770034214SMatthew G. Knepley   mesh->useClosure = useClosure;
8870034214SMatthew G. Knepley   PetscFunctionReturn(0);
8970034214SMatthew G. Knepley }
9070034214SMatthew G. Knepley 
9170034214SMatthew G. Knepley #undef __FUNCT__
9270034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacencyUseClosure"
9370034214SMatthew G. Knepley /*@
9470034214SMatthew G. Knepley   DMPlexGetAdjacencyUseClosure - Query whether adjacency in the mesh uses the transitive closure
9570034214SMatthew G. Knepley 
9670034214SMatthew G. Knepley   Input Parameter:
9770034214SMatthew G. Knepley . dm      - The DM object
9870034214SMatthew G. Knepley 
9970034214SMatthew G. Knepley   Output Parameter:
10070034214SMatthew G. Knepley . useClosure - Flag to use the closure
10170034214SMatthew G. Knepley 
10270034214SMatthew G. Knepley   Level: intermediate
10370034214SMatthew G. Knepley 
10470034214SMatthew G. Knepley   Notes:
10570034214SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
1064b6b44bdSMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
10770034214SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
10870034214SMatthew G. Knepley 
10970034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator()
11070034214SMatthew G. Knepley @*/
11170034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacencyUseClosure(DM dm, PetscBool *useClosure)
11270034214SMatthew G. Knepley {
11370034214SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *) dm->data;
11470034214SMatthew G. Knepley 
11570034214SMatthew G. Knepley   PetscFunctionBegin;
11670034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11770034214SMatthew G. Knepley   PetscValidIntPointer(useClosure, 2);
11870034214SMatthew G. Knepley   *useClosure = mesh->useClosure;
11970034214SMatthew G. Knepley   PetscFunctionReturn(0);
12070034214SMatthew G. Knepley }
12170034214SMatthew G. Knepley 
12270034214SMatthew G. Knepley #undef __FUNCT__
123a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAdjacencyUseAnchors"
1248b0b4c70SToby Isaac /*@
125a17985deSToby Isaac   DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints.
1268b0b4c70SToby Isaac 
1278b0b4c70SToby Isaac   Input Parameters:
1288b0b4c70SToby Isaac + dm      - The DM object
1295b317d89SToby 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.
1308b0b4c70SToby Isaac 
1318b0b4c70SToby Isaac   Level: intermediate
1328b0b4c70SToby Isaac 
133a17985deSToby Isaac .seealso: DMPlexGetAdjacencyUseClosure(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
1348b0b4c70SToby Isaac @*/
1355b317d89SToby Isaac PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors)
1368b0b4c70SToby Isaac {
1378b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
1388b0b4c70SToby Isaac 
1398b0b4c70SToby Isaac   PetscFunctionBegin;
1408b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1415b317d89SToby Isaac   mesh->useAnchors = useAnchors;
1428b0b4c70SToby Isaac   PetscFunctionReturn(0);
1438b0b4c70SToby Isaac }
1448b0b4c70SToby Isaac 
1458b0b4c70SToby Isaac #undef __FUNCT__
146a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAdjacencyUseAnchors"
1478b0b4c70SToby Isaac /*@
148a17985deSToby Isaac   DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints.
1498b0b4c70SToby Isaac 
1508b0b4c70SToby Isaac   Input Parameter:
1518b0b4c70SToby Isaac . dm      - The DM object
1528b0b4c70SToby Isaac 
1538b0b4c70SToby Isaac   Output Parameter:
1545b317d89SToby 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.
1558b0b4c70SToby Isaac 
1568b0b4c70SToby Isaac   Level: intermediate
1578b0b4c70SToby Isaac 
158a17985deSToby Isaac .seealso: DMPlexSetAdjacencyUseAnchors(), DMPlexSetAdjacencyUseCone(), DMPlexGetAdjacencyUseCone(), DMPlexDistribute(), DMPlexPreallocateOperator(), DMPlexSetAnchors()
1598b0b4c70SToby Isaac @*/
1605b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors)
1618b0b4c70SToby Isaac {
1628b0b4c70SToby Isaac   DM_Plex *mesh = (DM_Plex *) dm->data;
1638b0b4c70SToby Isaac 
1648b0b4c70SToby Isaac   PetscFunctionBegin;
1658b0b4c70SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1665b317d89SToby Isaac   PetscValidIntPointer(useAnchors, 2);
1675b317d89SToby Isaac   *useAnchors = mesh->useAnchors;
1688b0b4c70SToby Isaac   PetscFunctionReturn(0);
1698b0b4c70SToby Isaac }
1708b0b4c70SToby Isaac 
1718b0b4c70SToby Isaac #undef __FUNCT__
17270034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacency_Cone_Internal"
17370034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
17470034214SMatthew G. Knepley {
17570034214SMatthew G. Knepley   const PetscInt *cone = NULL;
17670034214SMatthew G. Knepley   PetscInt        numAdj = 0, maxAdjSize = *adjSize, coneSize, c;
17770034214SMatthew G. Knepley   PetscErrorCode  ierr;
17870034214SMatthew G. Knepley 
17970034214SMatthew G. Knepley   PetscFunctionBeginHot;
18070034214SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
18170034214SMatthew G. Knepley   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1824b6b44bdSMatthew G. Knepley   for (c = 0; c <= coneSize; ++c) {
1834b6b44bdSMatthew G. Knepley     const PetscInt  point   = !c ? p : cone[c-1];
18470034214SMatthew G. Knepley     const PetscInt *support = NULL;
18570034214SMatthew G. Knepley     PetscInt        supportSize, s, q;
18670034214SMatthew G. Knepley 
1874b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
1884b6b44bdSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
18970034214SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
19070034214SMatthew G. Knepley       for (q = 0; q < numAdj || (adj[numAdj++] = support[s],0); ++q) {
19170034214SMatthew G. Knepley         if (support[s] == adj[q]) break;
19270034214SMatthew G. Knepley       }
19370034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
19470034214SMatthew G. Knepley     }
19570034214SMatthew G. Knepley   }
19670034214SMatthew G. Knepley   *adjSize = numAdj;
19770034214SMatthew G. Knepley   PetscFunctionReturn(0);
19870034214SMatthew G. Knepley }
19970034214SMatthew G. Knepley 
20070034214SMatthew G. Knepley #undef __FUNCT__
20170034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacency_Support_Internal"
20270034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[])
20370034214SMatthew G. Knepley {
20470034214SMatthew G. Knepley   const PetscInt *support = NULL;
20570034214SMatthew G. Knepley   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
20670034214SMatthew G. Knepley   PetscErrorCode  ierr;
20770034214SMatthew G. Knepley 
20870034214SMatthew G. Knepley   PetscFunctionBeginHot;
20970034214SMatthew G. Knepley   ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
21070034214SMatthew G. Knepley   ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
2114b6b44bdSMatthew G. Knepley   for (s = 0; s <= supportSize; ++s) {
2124b6b44bdSMatthew G. Knepley     const PetscInt  point = !s ? p : support[s-1];
21370034214SMatthew G. Knepley     const PetscInt *cone  = NULL;
21470034214SMatthew G. Knepley     PetscInt        coneSize, c, q;
21570034214SMatthew G. Knepley 
2164b6b44bdSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2174b6b44bdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
21870034214SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
21970034214SMatthew G. Knepley       for (q = 0; q < numAdj || (adj[numAdj++] = cone[c],0); ++q) {
22070034214SMatthew G. Knepley         if (cone[c] == adj[q]) break;
22170034214SMatthew G. Knepley       }
22270034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
22370034214SMatthew G. Knepley     }
22470034214SMatthew G. Knepley   }
22570034214SMatthew G. Knepley   *adjSize = numAdj;
22670034214SMatthew G. Knepley   PetscFunctionReturn(0);
22770034214SMatthew G. Knepley }
22870034214SMatthew G. Knepley 
22970034214SMatthew G. Knepley #undef __FUNCT__
23070034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacency_Transitive_Internal"
23170034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[])
23270034214SMatthew G. Knepley {
23370034214SMatthew G. Knepley   PetscInt      *star = NULL;
23470034214SMatthew G. Knepley   PetscInt       numAdj = 0, maxAdjSize = *adjSize, starSize, s;
23570034214SMatthew G. Knepley   PetscErrorCode ierr;
23670034214SMatthew G. Knepley 
23770034214SMatthew G. Knepley   PetscFunctionBeginHot;
23870034214SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
23970034214SMatthew G. Knepley   for (s = 0; s < starSize*2; s += 2) {
24070034214SMatthew G. Knepley     const PetscInt *closure = NULL;
24170034214SMatthew G. Knepley     PetscInt        closureSize, c, q;
24270034214SMatthew G. Knepley 
24370034214SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
24470034214SMatthew G. Knepley     for (c = 0; c < closureSize*2; c += 2) {
24570034214SMatthew G. Knepley       for (q = 0; q < numAdj || (adj[numAdj++] = closure[c],0); ++q) {
24670034214SMatthew G. Knepley         if (closure[c] == adj[q]) break;
24770034214SMatthew G. Knepley       }
24870034214SMatthew G. Knepley       if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
24970034214SMatthew G. Knepley     }
25070034214SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr);
25170034214SMatthew G. Knepley   }
25270034214SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr);
25370034214SMatthew G. Knepley   *adjSize = numAdj;
25470034214SMatthew G. Knepley   PetscFunctionReturn(0);
25570034214SMatthew G. Knepley }
25670034214SMatthew G. Knepley 
25770034214SMatthew G. Knepley #undef __FUNCT__
25870034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacency_Internal"
2595b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[])
26070034214SMatthew G. Knepley {
26179bad331SMatthew G. Knepley   static PetscInt asiz = 0;
2628b0b4c70SToby Isaac   PetscInt maxAnchors = 1;
2638b0b4c70SToby Isaac   PetscInt aStart = -1, aEnd = -1;
2648b0b4c70SToby Isaac   PetscInt maxAdjSize;
2658b0b4c70SToby Isaac   PetscSection aSec = NULL;
2668b0b4c70SToby Isaac   IS aIS = NULL;
2678b0b4c70SToby Isaac   const PetscInt *anchors;
26870034214SMatthew G. Knepley   PetscErrorCode  ierr;
26970034214SMatthew G. Knepley 
27070034214SMatthew G. Knepley   PetscFunctionBeginHot;
2715b317d89SToby Isaac   if (useAnchors) {
272a17985deSToby Isaac     ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
2738b0b4c70SToby Isaac     if (aSec) {
2748b0b4c70SToby Isaac       ierr = PetscSectionGetMaxDof(aSec,&maxAnchors);CHKERRQ(ierr);
27524c766afSToby Isaac       maxAnchors = PetscMax(1,maxAnchors);
2768b0b4c70SToby Isaac       ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
2778b0b4c70SToby Isaac       ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
2788b0b4c70SToby Isaac     }
2798b0b4c70SToby Isaac   }
28079bad331SMatthew G. Knepley   if (!*adj) {
28124c766afSToby Isaac     PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd;
28279bad331SMatthew G. Knepley 
28324c766afSToby Isaac     ierr  = DMPlexGetChart(dm, &pStart,&pEnd);CHKERRQ(ierr);
28479bad331SMatthew G. Knepley     ierr  = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
28524c766afSToby Isaac     ierr  = DMPlexGetMaxSizes(dm, &maxC, &maxS);CHKERRQ(ierr);
28624c766afSToby Isaac     coneSeries    = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1;
28724c766afSToby Isaac     supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1;
28824c766afSToby Isaac     asiz  = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries);
2898b0b4c70SToby Isaac     asiz *= maxAnchors;
29024c766afSToby Isaac     asiz  = PetscMin(asiz,pEnd-pStart);
29179bad331SMatthew G. Knepley     ierr  = PetscMalloc1(asiz,adj);CHKERRQ(ierr);
29279bad331SMatthew G. Knepley   }
29379bad331SMatthew G. Knepley   if (*adjSize < 0) *adjSize = asiz;
2948b0b4c70SToby Isaac   maxAdjSize = *adjSize;
29570034214SMatthew G. Knepley   if (useTransitiveClosure) {
29679bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);CHKERRQ(ierr);
29770034214SMatthew G. Knepley   } else if (useCone) {
29879bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
29970034214SMatthew G. Knepley   } else {
30079bad331SMatthew G. Knepley     ierr = DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr);
30170034214SMatthew G. Knepley   }
3025b317d89SToby Isaac   if (useAnchors && aSec) {
3038b0b4c70SToby Isaac     PetscInt origSize = *adjSize;
3048b0b4c70SToby Isaac     PetscInt numAdj = origSize;
3058b0b4c70SToby Isaac     PetscInt i = 0, j;
3068b0b4c70SToby Isaac     PetscInt *orig = *adj;
3078b0b4c70SToby Isaac 
3088b0b4c70SToby Isaac     while (i < origSize) {
3098b0b4c70SToby Isaac       PetscInt p = orig[i];
3108b0b4c70SToby Isaac       PetscInt aDof = 0;
3118b0b4c70SToby Isaac 
3128b0b4c70SToby Isaac       if (p >= aStart && p < aEnd) {
3138b0b4c70SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
3148b0b4c70SToby Isaac       }
3158b0b4c70SToby Isaac       if (aDof) {
3168b0b4c70SToby Isaac         PetscInt aOff;
3178b0b4c70SToby Isaac         PetscInt s, q;
3188b0b4c70SToby Isaac 
3198b0b4c70SToby Isaac         for (j = i + 1; j < numAdj; j++) {
3208b0b4c70SToby Isaac           orig[j - 1] = orig[j];
3218b0b4c70SToby Isaac         }
3228b0b4c70SToby Isaac         origSize--;
3238b0b4c70SToby Isaac         numAdj--;
3248b0b4c70SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
3258b0b4c70SToby Isaac         for (s = 0; s < aDof; ++s) {
3268b0b4c70SToby Isaac           for (q = 0; q < numAdj || (orig[numAdj++] = anchors[aOff+s],0); ++q) {
3278b0b4c70SToby Isaac             if (anchors[aOff+s] == orig[q]) break;
3288b0b4c70SToby Isaac           }
3298b0b4c70SToby Isaac           if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
3308b0b4c70SToby Isaac         }
3318b0b4c70SToby Isaac       }
3328b0b4c70SToby Isaac       else {
3338b0b4c70SToby Isaac         i++;
3348b0b4c70SToby Isaac       }
3358b0b4c70SToby Isaac     }
3368b0b4c70SToby Isaac     *adjSize = numAdj;
3378b0b4c70SToby Isaac     ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
3388b0b4c70SToby Isaac   }
33970034214SMatthew G. Knepley   PetscFunctionReturn(0);
34070034214SMatthew G. Knepley }
34170034214SMatthew G. Knepley 
34270034214SMatthew G. Knepley #undef __FUNCT__
34370034214SMatthew G. Knepley #define __FUNCT__ "DMPlexGetAdjacency"
34470034214SMatthew G. Knepley /*@
34570034214SMatthew G. Knepley   DMPlexGetAdjacency - Return all points adjacent to the given point
34670034214SMatthew G. Knepley 
34770034214SMatthew G. Knepley   Input Parameters:
34870034214SMatthew G. Knepley + dm - The DM object
34970034214SMatthew G. Knepley . p  - The point
35070034214SMatthew G. Knepley . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE
35170034214SMatthew G. Knepley - adj - Either NULL so that the array is allocated, or an existing array with size adjSize
35270034214SMatthew G. Knepley 
35370034214SMatthew G. Knepley   Output Parameters:
35470034214SMatthew G. Knepley + adjSize - The number of adjacent points
35570034214SMatthew G. Knepley - adj - The adjacent points
35670034214SMatthew G. Knepley 
35770034214SMatthew G. Knepley   Level: advanced
35870034214SMatthew G. Knepley 
35970034214SMatthew G. Knepley   Notes: The user must PetscFree the adj array if it was not passed in.
36070034214SMatthew G. Knepley 
36170034214SMatthew G. Knepley .seealso: DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator()
36270034214SMatthew G. Knepley @*/
36370034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[])
36470034214SMatthew G. Knepley {
36570034214SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
36670034214SMatthew G. Knepley   PetscErrorCode ierr;
36770034214SMatthew G. Knepley 
36870034214SMatthew G. Knepley   PetscFunctionBeginHot;
36970034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37070034214SMatthew G. Knepley   PetscValidPointer(adjSize,3);
37170034214SMatthew G. Knepley   PetscValidPointer(adj,4);
3725b317d89SToby Isaac   ierr = DMPlexGetAdjacency_Internal(dm, p, mesh->useCone, mesh->useClosure, mesh->useAnchors, adjSize, adj);CHKERRQ(ierr);
37370034214SMatthew G. Knepley   PetscFunctionReturn(0);
37470034214SMatthew G. Knepley }
37508633170SToby Isaac 
376b0a623aaSMatthew G. Knepley #undef __FUNCT__
377b0a623aaSMatthew G. Knepley #define __FUNCT__ "DMPlexCreateTwoSidedProcessSF"
378b0a623aaSMatthew G. Knepley /*@
379b0a623aaSMatthew G. Knepley   DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity
380b0a623aaSMatthew G. Knepley 
381b0a623aaSMatthew G. Knepley   Collective on DM
382b0a623aaSMatthew G. Knepley 
383b0a623aaSMatthew G. Knepley   Input Parameters:
384b0a623aaSMatthew G. Knepley + dm      - The DM
385b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity
386b0a623aaSMatthew G. Knepley 
387b0a623aaSMatthew G. Knepley   Output Parameters:
388b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL
389b0a623aaSMatthew G. Knepley - sfProcess    - An SF encoding the two-sided process connectivity, or NULL
390b0a623aaSMatthew G. Knepley 
391b0a623aaSMatthew G. Knepley   Level: developer
392b0a623aaSMatthew G. Knepley 
393b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF()
394b0a623aaSMatthew G. Knepley @*/
395b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess)
396b0a623aaSMatthew G. Knepley {
397b0a623aaSMatthew G. Knepley   const PetscSFNode *remotePoints;
398b0a623aaSMatthew G. Knepley   PetscInt          *localPointsNew;
399b0a623aaSMatthew G. Knepley   PetscSFNode       *remotePointsNew;
400b0a623aaSMatthew G. Knepley   const PetscInt    *nranks;
401b0a623aaSMatthew G. Knepley   PetscInt          *ranksNew;
402b0a623aaSMatthew G. Knepley   PetscBT            neighbors;
403b0a623aaSMatthew G. Knepley   PetscInt           pStart, pEnd, p, numLeaves, l, numNeighbors, n;
404b0a623aaSMatthew G. Knepley   PetscMPIInt        numProcs, proc, rank;
405b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
406b0a623aaSMatthew G. Knepley 
407b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
408b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409b0a623aaSMatthew G. Knepley   PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2);
410b0a623aaSMatthew G. Knepley   if (processRanks) {PetscValidPointer(processRanks, 3);}
411b0a623aaSMatthew G. Knepley   if (sfProcess)    {PetscValidPointer(sfProcess, 4);}
412b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
413b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
414b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr);
415b0a623aaSMatthew G. Knepley   ierr = PetscBTCreate(numProcs, &neighbors);CHKERRQ(ierr);
416b0a623aaSMatthew G. Knepley   ierr = PetscBTMemzero(numProcs, neighbors);CHKERRQ(ierr);
417b0a623aaSMatthew G. Knepley   /* Compute root-to-leaf process connectivity */
418b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr);
419b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr);
420b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
421b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
422b0a623aaSMatthew G. Knepley 
423b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr);
424b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr);
425302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
426b0a623aaSMatthew G. Knepley   }
427b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr);
428b0a623aaSMatthew G. Knepley   /* Compute leaf-to-neighbor process connectivity */
429b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr);
430b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr);
431b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
432b0a623aaSMatthew G. Knepley     PetscInt ndof, noff, n;
433b0a623aaSMatthew G. Knepley 
434b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr);
435b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr);
436302440fdSBarry Smith     for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);}
437b0a623aaSMatthew G. Knepley   }
438b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr);
439b0a623aaSMatthew G. Knepley   /* Compute leaf-to-root process connectivity */
440b0a623aaSMatthew G. Knepley   for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);}
441b0a623aaSMatthew G. Knepley   /* Calculate edges */
442b0a623aaSMatthew G. Knepley   PetscBTClear(neighbors, rank);
443b0a623aaSMatthew G. Knepley   for(proc = 0, numNeighbors = 0; proc < numProcs; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;}
444b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr);
445b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr);
446b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr);
447b0a623aaSMatthew G. Knepley   for(proc = 0, n = 0; proc < numProcs; ++proc) {
448b0a623aaSMatthew G. Knepley     if (PetscBTLookup(neighbors, proc)) {
449b0a623aaSMatthew G. Knepley       ranksNew[n]              = proc;
450b0a623aaSMatthew G. Knepley       localPointsNew[n]        = proc;
451b0a623aaSMatthew G. Knepley       remotePointsNew[n].index = rank;
452b0a623aaSMatthew G. Knepley       remotePointsNew[n].rank  = proc;
453b0a623aaSMatthew G. Knepley       ++n;
454b0a623aaSMatthew G. Knepley     }
455b0a623aaSMatthew G. Knepley   }
456b0a623aaSMatthew G. Knepley   ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr);
457b0a623aaSMatthew G. Knepley   if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);}
458b0a623aaSMatthew G. Knepley   else              {ierr = PetscFree(ranksNew);CHKERRQ(ierr);}
459b0a623aaSMatthew G. Knepley   if (sfProcess) {
460b0a623aaSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr);
461b0a623aaSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr);
462b0a623aaSMatthew G. Knepley     ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr);
463b0a623aaSMatthew G. Knepley     ierr = PetscSFSetGraph(*sfProcess, numProcs, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
464b0a623aaSMatthew G. Knepley   }
465b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
466b0a623aaSMatthew G. Knepley }
467b0a623aaSMatthew G. Knepley 
468b0a623aaSMatthew G. Knepley #undef __FUNCT__
469b0a623aaSMatthew G. Knepley #define __FUNCT__ "DMPlexDistributeOwnership"
470b0a623aaSMatthew G. Knepley /*@
471b0a623aaSMatthew G. Knepley   DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF.
472b0a623aaSMatthew G. Knepley 
473b0a623aaSMatthew G. Knepley   Collective on DM
474b0a623aaSMatthew G. Knepley 
475b0a623aaSMatthew G. Knepley   Input Parameter:
476b0a623aaSMatthew G. Knepley . dm - The DM
477b0a623aaSMatthew G. Knepley 
478b0a623aaSMatthew G. Knepley   Output Parameters:
479b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point
480b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
481b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
482b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
483b0a623aaSMatthew G. Knepley 
484b0a623aaSMatthew G. Knepley   Level: developer
485b0a623aaSMatthew G. Knepley 
486b0a623aaSMatthew G. Knepley .seealso: DMPlexCreateOverlap()
487b0a623aaSMatthew G. Knepley @*/
488b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank)
489b0a623aaSMatthew G. Knepley {
490b0a623aaSMatthew G. Knepley   MPI_Comm        comm;
491b0a623aaSMatthew G. Knepley   PetscSF         sfPoint;
492b0a623aaSMatthew G. Knepley   const PetscInt *rootdegree;
493b0a623aaSMatthew G. Knepley   PetscInt       *myrank, *remoterank;
494b0a623aaSMatthew G. Knepley   PetscInt        pStart, pEnd, p, nedges;
495b0a623aaSMatthew G. Knepley   PetscMPIInt     rank;
496b0a623aaSMatthew G. Knepley   PetscErrorCode  ierr;
497b0a623aaSMatthew G. Knepley 
498b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
499b0a623aaSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
500b0a623aaSMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
501b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
502b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
503b0a623aaSMatthew G. Knepley   /* Compute number of leaves for each root */
504b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr);
505b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr);
506b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr);
507b0a623aaSMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr);
508b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);}
509b0a623aaSMatthew G. Knepley   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
510b0a623aaSMatthew G. Knepley   /* Gather rank of each leaf to root */
511b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr);
512b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr);
513b0a623aaSMatthew G. Knepley   ierr = PetscMalloc1(nedges,  &remoterank);CHKERRQ(ierr);
514b0a623aaSMatthew G. Knepley   for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank;
515b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
516b0a623aaSMatthew G. Knepley   ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr);
517b0a623aaSMatthew G. Knepley   ierr = PetscFree(myrank);CHKERRQ(ierr);
518b0a623aaSMatthew G. Knepley   ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr);
519b0a623aaSMatthew G. Knepley   /* Distribute remote ranks to leaves */
520b0a623aaSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr);
521b0a623aaSMatthew G. Knepley   ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr);
522b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
523b0a623aaSMatthew G. Knepley }
524b0a623aaSMatthew G. Knepley 
525b0a623aaSMatthew G. Knepley #undef __FUNCT__
526b0a623aaSMatthew G. Knepley #define __FUNCT__ "DMPlexCreateOverlap"
527278397a0SMatthew G. Knepley /*@C
528b0a623aaSMatthew G. Knepley   DMPlexCreateOverlap - Compute owner information for shared points. This basically gets two-sided for an SF.
529b0a623aaSMatthew G. Knepley 
530b0a623aaSMatthew G. Knepley   Collective on DM
531b0a623aaSMatthew G. Knepley 
532b0a623aaSMatthew G. Knepley   Input Parameters:
533b0a623aaSMatthew G. Knepley + dm          - The DM
53424d039d7SMichael Lange . levels      - Number of overlap levels
535b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point
536b0a623aaSMatthew G. Knepley . rootrank    - The rank of each edge into the root point
537b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point
538b0a623aaSMatthew G. Knepley - leafrank    - The rank of each process sharing a leaf point
539b0a623aaSMatthew G. Knepley 
540b0a623aaSMatthew G. Knepley   Output Parameters:
541a9f1d5b2SMichael Lange + ovLabel     - DMLabel containing remote overlap contributions as point/rank pairings
542b0a623aaSMatthew G. Knepley 
543b0a623aaSMatthew G. Knepley   Level: developer
544b0a623aaSMatthew G. Knepley 
5451fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute()
546b0a623aaSMatthew G. Knepley @*/
547a9f1d5b2SMichael Lange PetscErrorCode DMPlexCreateOverlap(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel)
548b0a623aaSMatthew G. Knepley {
549e540f424SMichael Lange   MPI_Comm           comm;
550b0a623aaSMatthew G. Knepley   DMLabel            ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */
551b0a623aaSMatthew G. Knepley   PetscSF            sfPoint, sfProc;
552b0a623aaSMatthew G. Knepley   const PetscSFNode *remote;
553b0a623aaSMatthew G. Knepley   const PetscInt    *local;
5541fd9873aSMichael Lange   const PetscInt    *nrank, *rrank;
555b0a623aaSMatthew G. Knepley   PetscInt          *adj = NULL;
5561fd9873aSMichael Lange   PetscInt           pStart, pEnd, p, sStart, sEnd, nleaves, l;
557b0a623aaSMatthew G. Knepley   PetscMPIInt        rank, numProcs;
55826a7d390SMatthew G. Knepley   PetscBool          useCone, useClosure, flg;
559b0a623aaSMatthew G. Knepley   PetscErrorCode     ierr;
560b0a623aaSMatthew G. Knepley 
561b0a623aaSMatthew G. Knepley   PetscFunctionBegin;
562e540f424SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
563e540f424SMichael Lange   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
564e540f424SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
565b0a623aaSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
566b0a623aaSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
567b0a623aaSMatthew G. Knepley   ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr);
568b0a623aaSMatthew G. Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
569b0a623aaSMatthew G. Knepley   ierr = DMLabelCreate("Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr);
570b0a623aaSMatthew G. Knepley   /* Handle leaves: shared with the root point */
571b0a623aaSMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
572b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, a;
573b0a623aaSMatthew G. Knepley 
574b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, local[l], &adjSize, &adj);CHKERRQ(ierr);
575b0a623aaSMatthew G. Knepley     for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);}
576b0a623aaSMatthew G. Knepley   }
577b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr);
578b0a623aaSMatthew G. Knepley   ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr);
579b0a623aaSMatthew G. Knepley   /* Handle roots */
580b0a623aaSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
581b0a623aaSMatthew G. Knepley     PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a;
582b0a623aaSMatthew G. Knepley 
583b0a623aaSMatthew G. Knepley     if ((p >= sStart) && (p < sEnd)) {
584b0a623aaSMatthew G. Knepley       /* Some leaves share a root with other leaves on different processes */
585b0a623aaSMatthew G. Knepley       ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr);
586b0a623aaSMatthew G. Knepley       if (neighbors) {
587b0a623aaSMatthew G. Knepley         ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr);
588b0a623aaSMatthew G. Knepley         ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
589b0a623aaSMatthew G. Knepley         for (n = 0; n < neighbors; ++n) {
590b0a623aaSMatthew G. Knepley           const PetscInt remoteRank = nrank[noff+n];
591b0a623aaSMatthew G. Knepley 
592b0a623aaSMatthew G. Knepley           if (remoteRank == rank) continue;
593b0a623aaSMatthew G. Knepley           for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
594b0a623aaSMatthew G. Knepley         }
595b0a623aaSMatthew G. Knepley       }
596b0a623aaSMatthew G. Knepley     }
597b0a623aaSMatthew G. Knepley     /* Roots are shared with leaves */
598b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr);
599b0a623aaSMatthew G. Knepley     if (!neighbors) continue;
600b0a623aaSMatthew G. Knepley     ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr);
601b0a623aaSMatthew G. Knepley     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
602b0a623aaSMatthew G. Knepley     for (n = 0; n < neighbors; ++n) {
603b0a623aaSMatthew G. Knepley       const PetscInt remoteRank = rrank[noff+n];
604b0a623aaSMatthew G. Knepley 
605b0a623aaSMatthew G. Knepley       if (remoteRank == rank) continue;
606b0a623aaSMatthew G. Knepley       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);}
607b0a623aaSMatthew G. Knepley     }
608b0a623aaSMatthew G. Knepley   }
609b0a623aaSMatthew G. Knepley   ierr = PetscFree(adj);CHKERRQ(ierr);
610b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr);
611b0a623aaSMatthew G. Knepley   ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr);
61224d039d7SMichael Lange   /* Add additional overlap levels */
613be200f8dSMichael Lange   for (l = 1; l < levels; l++) {
614be200f8dSMichael Lange     /* Propagate point donations over SF to capture remote connections */
615be200f8dSMichael Lange     ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr);
616be200f8dSMichael Lange     /* Add next level of point donations to the label */
617be200f8dSMichael Lange     ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr);
618be200f8dSMichael Lange   }
61926a7d390SMatthew G. Knepley   /* We require the closure in the overlap */
62026a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
62126a7d390SMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
62226a7d390SMatthew G. Knepley   if (useCone || !useClosure) {
6235abbe4feSMichael Lange     ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr);
62426a7d390SMatthew G. Knepley   }
625c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr);
626e540f424SMichael Lange   if (flg) {
627b0a623aaSMatthew G. Knepley     ierr = DMLabelView(ovAdjByRank, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
628b0a623aaSMatthew G. Knepley   }
62971a8c5fcSMichael Lange   /* Make global process SF and invert sender to receiver label */
63071a8c5fcSMichael Lange   {
63171a8c5fcSMichael Lange     /* Build a global process SF */
63271a8c5fcSMichael Lange     PetscSFNode *remoteProc;
63371a8c5fcSMichael Lange     ierr = PetscMalloc1(numProcs, &remoteProc);CHKERRQ(ierr);
63471a8c5fcSMichael Lange     for (p = 0; p < numProcs; ++p) {
63571a8c5fcSMichael Lange       remoteProc[p].rank  = p;
63671a8c5fcSMichael Lange       remoteProc[p].index = rank;
63771a8c5fcSMichael Lange     }
63871a8c5fcSMichael Lange     ierr = PetscSFCreate(comm, &sfProc);CHKERRQ(ierr);
63971a8c5fcSMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProc, "Process SF");CHKERRQ(ierr);
64071a8c5fcSMichael Lange     ierr = PetscSFSetGraph(sfProc, numProcs, numProcs, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
64171a8c5fcSMichael Lange   }
642a9f1d5b2SMichael Lange   ierr = DMLabelCreate("Overlap label", ovLabel);CHKERRQ(ierr);
643a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, sfProc, *ovLabel);CHKERRQ(ierr);
644a9f1d5b2SMichael Lange   /* Add owned points, except for shared local points */
645a9f1d5b2SMichael Lange   for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);}
646e540f424SMichael Lange   for (l = 0; l < nleaves; ++l) {
647a9f1d5b2SMichael Lange     ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr);
648a9f1d5b2SMichael Lange     ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr);
649e540f424SMichael Lange   }
650e540f424SMichael Lange   /* Clean up */
6511fd9873aSMichael Lange   ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr);
652b0a623aaSMatthew G. Knepley   ierr = PetscSFDestroy(&sfProc);CHKERRQ(ierr);
653b0a623aaSMatthew G. Knepley   PetscFunctionReturn(0);
654b0a623aaSMatthew G. Knepley }
65570034214SMatthew G. Knepley 
65670034214SMatthew G. Knepley #undef __FUNCT__
65746f9b1c3SMichael Lange #define __FUNCT__ "DMPlexCreateOverlapMigrationSF"
65846f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF)
65946f9b1c3SMichael Lange {
66046f9b1c3SMichael Lange   MPI_Comm           comm;
66146f9b1c3SMichael Lange   PetscMPIInt        rank, numProcs;
66246f9b1c3SMichael Lange   PetscInt           d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints;
66346f9b1c3SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
66446f9b1c3SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
66546f9b1c3SMichael Lange   PetscSFNode       *iremote;
66646f9b1c3SMichael Lange   PetscSF            pointSF;
66746f9b1c3SMichael Lange   const PetscInt    *sharedLocal;
66846f9b1c3SMichael Lange   const PetscSFNode *overlapRemote, *sharedRemote;
66946f9b1c3SMichael Lange   PetscErrorCode     ierr;
67046f9b1c3SMichael Lange 
67146f9b1c3SMichael Lange   PetscFunctionBegin;
67246f9b1c3SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67346f9b1c3SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
67446f9b1c3SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
67546f9b1c3SMichael Lange   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
67646f9b1c3SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
67746f9b1c3SMichael Lange 
67846f9b1c3SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
67946f9b1c3SMichael Lange   ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr);
68046f9b1c3SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
68146f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
68246f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
68346f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) pointDepths[p] = d;
68446f9b1c3SMichael Lange   }
68546f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) remoteDepths[p] = -1;
68646f9b1c3SMichael Lange   ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
68746f9b1c3SMichael Lange   ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
68846f9b1c3SMichael Lange 
68946f9b1c3SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
69046f9b1c3SMichael Lange   ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr);
69146f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) depthRecv[d]=0;
69246f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++;
69346f9b1c3SMichael Lange   depthShift[dim] = 0;
69446f9b1c3SMichael Lange   for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim];
69546f9b1c3SMichael Lange   for (d=1; d<dim; d++) depthShift[d] += depthRecv[0];
69646f9b1c3SMichael Lange   for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1];
69746f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
69846f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
69946f9b1c3SMichael Lange     depthIdx[d] = pStart + depthShift[d];
70046f9b1c3SMichael Lange   }
70146f9b1c3SMichael Lange 
70246f9b1c3SMichael Lange   /* Form the overlap SF build an SF that describes the full overlap migration SF */
70346f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
70446f9b1c3SMichael Lange   newLeaves = pEnd - pStart + nleaves;
70509b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr);
70609b7985cSMatthew G. Knepley   ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr);
70746f9b1c3SMichael Lange   /* First map local points to themselves */
70846f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
70946f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
71046f9b1c3SMichael Lange     for (p=pStart; p<pEnd; p++) {
71146f9b1c3SMichael Lange       point = p + depthShift[d];
71246f9b1c3SMichael Lange       ilocal[point] = point;
71346f9b1c3SMichael Lange       iremote[point].index = p;
71446f9b1c3SMichael Lange       iremote[point].rank = rank;
71546f9b1c3SMichael Lange       depthIdx[d]++;
71646f9b1c3SMichael Lange     }
71746f9b1c3SMichael Lange   }
71846f9b1c3SMichael Lange 
71946f9b1c3SMichael Lange   /* Add in the remote roots for currently shared points */
72046f9b1c3SMichael Lange   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
72146f9b1c3SMichael Lange   ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr);
72246f9b1c3SMichael Lange   for (d=0; d<dim+1; d++) {
72346f9b1c3SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
72446f9b1c3SMichael Lange     for (p=0; p<numSharedPoints; p++) {
72546f9b1c3SMichael Lange       if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) {
72646f9b1c3SMichael Lange         point = sharedLocal[p] + depthShift[d];
72746f9b1c3SMichael Lange         iremote[point].index = sharedRemote[p].index;
72846f9b1c3SMichael Lange         iremote[point].rank = sharedRemote[p].rank;
72946f9b1c3SMichael Lange       }
73046f9b1c3SMichael Lange     }
73146f9b1c3SMichael Lange   }
73246f9b1c3SMichael Lange 
73346f9b1c3SMichael Lange   /* Now add the incoming overlap points */
73446f9b1c3SMichael Lange   for (p=0; p<nleaves; p++) {
73546f9b1c3SMichael Lange     point = depthIdx[remoteDepths[p]];
73646f9b1c3SMichael Lange     ilocal[point] = point;
73746f9b1c3SMichael Lange     iremote[point].index = overlapRemote[p].index;
73846f9b1c3SMichael Lange     iremote[point].rank = overlapRemote[p].rank;
73946f9b1c3SMichael Lange     depthIdx[remoteDepths[p]]++;
74046f9b1c3SMichael Lange   }
74115fff7beSMatthew G. Knepley   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
74246f9b1c3SMichael Lange 
74346f9b1c3SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
74446f9b1c3SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr);
74546f9b1c3SMichael Lange   ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr);
74646f9b1c3SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
74746f9b1c3SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
74846f9b1c3SMichael Lange 
74946f9b1c3SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
75070034214SMatthew G. Knepley   PetscFunctionReturn(0);
75170034214SMatthew G. Knepley }
75270034214SMatthew G. Knepley 
75370034214SMatthew G. Knepley #undef __FUNCT__
754a9f1d5b2SMichael Lange #define __FUNCT__ "DMPlexStratifyMigrationSF"
755a9f1d5b2SMichael Lange /*@
756*f0e73a3dSToby Isaac   DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification.
757a9f1d5b2SMichael Lange 
758a9f1d5b2SMichael Lange   Input Parameter:
759a9f1d5b2SMichael Lange + dm          - The DM
760a9f1d5b2SMichael Lange - sf          - A star forest with non-ordered leaves, usually defining a DM point migration
761a9f1d5b2SMichael Lange 
762a9f1d5b2SMichael Lange   Output Parameter:
763a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified
764a9f1d5b2SMichael Lange 
765a9f1d5b2SMichael Lange   Level: developer
766a9f1d5b2SMichael Lange 
767a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap()
768a9f1d5b2SMichael Lange @*/
769a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF)
770a9f1d5b2SMichael Lange {
771a9f1d5b2SMichael Lange   MPI_Comm           comm;
772a9f1d5b2SMichael Lange   PetscMPIInt        rank, numProcs;
7737fab53ddSMatthew G. Knepley   PetscInt           d, ldepth, depth, p, pStart, pEnd, nroots, nleaves;
774a9f1d5b2SMichael Lange   PetscInt          *pointDepths, *remoteDepths, *ilocal;
775a9f1d5b2SMichael Lange   PetscInt          *depthRecv, *depthShift, *depthIdx;
776*f0e73a3dSToby Isaac   PetscInt           hybEnd[4];
777a9f1d5b2SMichael Lange   const PetscSFNode *iremote;
778a9f1d5b2SMichael Lange   PetscErrorCode     ierr;
779a9f1d5b2SMichael Lange 
780a9f1d5b2SMichael Lange   PetscFunctionBegin;
781a9f1d5b2SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
782a9f1d5b2SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
783a9f1d5b2SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
784a9f1d5b2SMichael Lange   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
7857fab53ddSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr);
786b2566f29SBarry Smith   ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
7877fab53ddSMatthew G. Knepley   if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth);
788a9f1d5b2SMichael Lange 
789a9f1d5b2SMichael Lange   /* Before building the migration SF we need to know the new stratum offsets */
790a9f1d5b2SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr);
791a9f1d5b2SMichael Lange   ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr);
792*f0e73a3dSToby Isaac   ierr = DMPlexGetHybridBounds(dm,&hybEnd[depth],&hybEnd[depth-1],&hybEnd[1],&hybEnd[0]);CHKERRQ(ierr);
7937fab53ddSMatthew G. Knepley   for (d = 0; d < depth+1; ++d) {
794a9f1d5b2SMichael Lange     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
795*f0e73a3dSToby Isaac     for (p = pStart; p < pEnd; ++p) {
796*f0e73a3dSToby Isaac       if (hybEnd[d] >= 0 && p >= hybEnd[d]) { /* put in a separate value for hybrid points */
797*f0e73a3dSToby Isaac         pointDepths[p] = 2 * d;
798*f0e73a3dSToby Isaac       } else {
799*f0e73a3dSToby Isaac         pointDepths[p] = 2 * d + 1;
800*f0e73a3dSToby Isaac       }
801*f0e73a3dSToby Isaac     }
802a9f1d5b2SMichael Lange   }
8037fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) remoteDepths[p] = -1;
804a9f1d5b2SMichael Lange   ierr = PetscSFBcastBegin(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
805a9f1d5b2SMichael Lange   ierr = PetscSFBcastEnd(sf, MPIU_INT, pointDepths, remoteDepths);CHKERRQ(ierr);
806a9f1d5b2SMichael Lange   /* Count recevied points in each stratum and compute the internal strata shift */
807*f0e73a3dSToby Isaac   ierr = PetscMalloc3(2*(depth+1), &depthRecv, 2*(depth+1), &depthShift, 2*(depth+1), &depthIdx);CHKERRQ(ierr);
808*f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) depthRecv[d] = 0;
8097fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) depthRecv[remoteDepths[p]]++;
810*f0e73a3dSToby Isaac   depthShift[2*depth+1] = 0;
811*f0e73a3dSToby Isaac   for (d = 0; d < 2*depth+1; ++d) depthShift[d] = depthRecv[2 * depth + 1];
812*f0e73a3dSToby Isaac   for (d = 0; d < 2*depth; ++d) depthShift[d] += depthRecv[2 * depth];
813*f0e73a3dSToby Isaac   depthShift[0] += depthRecv[1];
814*f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[1];
815*f0e73a3dSToby Isaac   for (d = 2; d < 2*depth; ++d) depthShift[d] += depthRecv[0];
816*f0e73a3dSToby Isaac   for (d = 2 * depth-1; d > 2; --d) {
817*f0e73a3dSToby Isaac     PetscInt e;
818*f0e73a3dSToby Isaac 
819*f0e73a3dSToby Isaac     for (e = d -1; e > 1; --e) depthShift[e] += depthRecv[d];
820*f0e73a3dSToby Isaac   }
821*f0e73a3dSToby Isaac   for (d = 0; d < 2*(depth+1); ++d) {depthIdx[d] = 0;}
822a9f1d5b2SMichael Lange   /* Derive a new local permutation based on stratified indices */
823a9f1d5b2SMichael Lange   ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr);
8247fab53ddSMatthew G. Knepley   for (p = 0; p < nleaves; ++p) {
8257fab53ddSMatthew G. Knepley     const PetscInt dep = remoteDepths[p];
8267fab53ddSMatthew G. Knepley 
8277fab53ddSMatthew G. Knepley     ilocal[p] = depthShift[dep] + depthIdx[dep];
8287fab53ddSMatthew G. Knepley     depthIdx[dep]++;
829a9f1d5b2SMichael Lange   }
830a9f1d5b2SMichael Lange   ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr);
831a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr);
832a9f1d5b2SMichael Lange   ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr);
833a9f1d5b2SMichael Lange   ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr);
834a9f1d5b2SMichael Lange   ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr);
835a9f1d5b2SMichael Lange   PetscFunctionReturn(0);
836a9f1d5b2SMichael Lange }
837a9f1d5b2SMichael Lange 
838a9f1d5b2SMichael Lange #undef __FUNCT__
83970034214SMatthew G. Knepley #define __FUNCT__ "DMPlexDistributeField"
84070034214SMatthew G. Knepley /*@
84170034214SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
84270034214SMatthew G. Knepley 
84370034214SMatthew G. Knepley   Collective on DM
84470034214SMatthew G. Knepley 
84570034214SMatthew G. Knepley   Input Parameters:
84670034214SMatthew G. Knepley + dm - The DMPlex object
84770034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
84870034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
84970034214SMatthew G. Knepley - originalVec - The existing data
85070034214SMatthew G. Knepley 
85170034214SMatthew G. Knepley   Output Parameters:
85270034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
85370034214SMatthew G. Knepley - newVec - The new data
85470034214SMatthew G. Knepley 
85570034214SMatthew G. Knepley   Level: developer
85670034214SMatthew G. Knepley 
85770034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData()
85870034214SMatthew G. Knepley @*/
85970034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
86070034214SMatthew G. Knepley {
86170034214SMatthew G. Knepley   PetscSF        fieldSF;
86270034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
86370034214SMatthew G. Knepley   PetscScalar   *originalValues, *newValues;
86470034214SMatthew G. Knepley   PetscErrorCode ierr;
86570034214SMatthew G. Knepley 
86670034214SMatthew G. Knepley   PetscFunctionBegin;
86770034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
86870034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
86970034214SMatthew G. Knepley 
87070034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
87170034214SMatthew G. Knepley   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
87270034214SMatthew G. Knepley   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
87370034214SMatthew G. Knepley 
87470034214SMatthew G. Knepley   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
87570034214SMatthew G. Knepley   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
87670034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
8778a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
87870034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
87970034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
88070034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
88170034214SMatthew G. Knepley   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
88270034214SMatthew G. Knepley   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
88370034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
88470034214SMatthew G. Knepley   PetscFunctionReturn(0);
88570034214SMatthew G. Knepley }
88670034214SMatthew G. Knepley 
88770034214SMatthew G. Knepley #undef __FUNCT__
88870034214SMatthew G. Knepley #define __FUNCT__ "DMPlexDistributeFieldIS"
88970034214SMatthew G. Knepley /*@
89070034214SMatthew G. Knepley   DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
89170034214SMatthew G. Knepley 
89270034214SMatthew G. Knepley   Collective on DM
89370034214SMatthew G. Knepley 
89470034214SMatthew G. Knepley   Input Parameters:
89570034214SMatthew G. Knepley + dm - The DMPlex object
89670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
89770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
89870034214SMatthew G. Knepley - originalIS - The existing data
89970034214SMatthew G. Knepley 
90070034214SMatthew G. Knepley   Output Parameters:
90170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
90270034214SMatthew G. Knepley - newIS - The new data
90370034214SMatthew G. Knepley 
90470034214SMatthew G. Knepley   Level: developer
90570034214SMatthew G. Knepley 
90670034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData()
90770034214SMatthew G. Knepley @*/
90870034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS)
90970034214SMatthew G. Knepley {
91070034214SMatthew G. Knepley   PetscSF         fieldSF;
91170034214SMatthew G. Knepley   PetscInt       *newValues, *remoteOffsets, fieldSize;
91270034214SMatthew G. Knepley   const PetscInt *originalValues;
91370034214SMatthew G. Knepley   PetscErrorCode  ierr;
91470034214SMatthew G. Knepley 
91570034214SMatthew G. Knepley   PetscFunctionBegin;
91670034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
91770034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
91870034214SMatthew G. Knepley 
91970034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
920854ce69bSBarry Smith   ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr);
92170034214SMatthew G. Knepley 
92270034214SMatthew G. Knepley   ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr);
92370034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9248a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
925aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
926aaf8c182SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues);CHKERRQ(ierr);
92770034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
92870034214SMatthew G. Knepley   ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr);
92970034214SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr);
93070034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
93170034214SMatthew G. Knepley   PetscFunctionReturn(0);
93270034214SMatthew G. Knepley }
93370034214SMatthew G. Knepley 
93470034214SMatthew G. Knepley #undef __FUNCT__
93570034214SMatthew G. Knepley #define __FUNCT__ "DMPlexDistributeData"
93670034214SMatthew G. Knepley /*@
93770034214SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
93870034214SMatthew G. Knepley 
93970034214SMatthew G. Knepley   Collective on DM
94070034214SMatthew G. Knepley 
94170034214SMatthew G. Knepley   Input Parameters:
94270034214SMatthew G. Knepley + dm - The DMPlex object
94370034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
94470034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
94570034214SMatthew G. Knepley . datatype - The type of data
94670034214SMatthew G. Knepley - originalData - The existing data
94770034214SMatthew G. Knepley 
94870034214SMatthew G. Knepley   Output Parameters:
94970034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout
95070034214SMatthew G. Knepley - newData - The new data
95170034214SMatthew G. Knepley 
95270034214SMatthew G. Knepley   Level: developer
95370034214SMatthew G. Knepley 
95470034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
95570034214SMatthew G. Knepley @*/
95670034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
95770034214SMatthew G. Knepley {
95870034214SMatthew G. Knepley   PetscSF        fieldSF;
95970034214SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
96070034214SMatthew G. Knepley   PetscMPIInt    dataSize;
96170034214SMatthew G. Knepley   PetscErrorCode ierr;
96270034214SMatthew G. Knepley 
96370034214SMatthew G. Knepley   PetscFunctionBegin;
96470034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
96570034214SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
96670034214SMatthew G. Knepley 
96770034214SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
96870034214SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
96970034214SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
97070034214SMatthew G. Knepley 
97170034214SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
9728a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
97370034214SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
97470034214SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
97570034214SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
97670034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
97770034214SMatthew G. Knepley   PetscFunctionReturn(0);
97870034214SMatthew G. Knepley }
97970034214SMatthew G. Knepley 
98070034214SMatthew G. Knepley #undef __FUNCT__
981cc71bff1SMichael Lange #define __FUNCT__ "DMPlexDistributeCones"
982ac04eaf7SToby Isaac PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
983cc71bff1SMichael Lange {
984f5bf2dbfSMichael Lange   DM_Plex               *mesh  = (DM_Plex*) dm->data;
985cc71bff1SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
986cc71bff1SMichael Lange   MPI_Comm               comm;
987cc71bff1SMichael Lange   PetscSF                coneSF;
988cc71bff1SMichael Lange   PetscSection           originalConeSection, newConeSection;
989ac04eaf7SToby Isaac   PetscInt              *remoteOffsets, *cones, *globCones, *newCones, newConesSize;
990cc71bff1SMichael Lange   PetscBool              flg;
991cc71bff1SMichael Lange   PetscErrorCode         ierr;
992cc71bff1SMichael Lange 
993cc71bff1SMichael Lange   PetscFunctionBegin;
994cc71bff1SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
995cc71bff1SMichael Lange   PetscValidPointer(dmParallel,4);
996cc71bff1SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
997cc71bff1SMichael Lange 
998cc71bff1SMichael Lange   /* Distribute cone section */
999cc71bff1SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1000cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
1001cc71bff1SMichael Lange   ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr);
1002cc71bff1SMichael Lange   ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
1003cc71bff1SMichael Lange   ierr = DMSetUp(dmParallel);CHKERRQ(ierr);
1004cc71bff1SMichael Lange   {
1005cc71bff1SMichael Lange     PetscInt pStart, pEnd, p;
1006cc71bff1SMichael Lange 
1007cc71bff1SMichael Lange     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
1008cc71bff1SMichael Lange     for (p = pStart; p < pEnd; ++p) {
1009cc71bff1SMichael Lange       PetscInt coneSize;
1010cc71bff1SMichael Lange       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
1011cc71bff1SMichael Lange       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
1012cc71bff1SMichael Lange     }
1013cc71bff1SMichael Lange   }
1014cc71bff1SMichael Lange   /* Communicate and renumber cones */
1015cc71bff1SMichael Lange   ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
10168a713f3bSLawrence Mitchell   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1017cc71bff1SMichael Lange   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
1018ac04eaf7SToby Isaac   if (original) {
1019ac04eaf7SToby Isaac     PetscInt numCones;
1020ac04eaf7SToby Isaac 
1021ac04eaf7SToby Isaac     ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr); ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr);
1022ac04eaf7SToby Isaac     ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr);
1023ac04eaf7SToby Isaac   }
1024ac04eaf7SToby Isaac   else {
1025ac04eaf7SToby Isaac     globCones = cones;
1026ac04eaf7SToby Isaac   }
1027cc71bff1SMichael Lange   ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr);
1028ac04eaf7SToby Isaac   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1029ac04eaf7SToby Isaac   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones);CHKERRQ(ierr);
1030ac04eaf7SToby Isaac   if (original) {
1031ac04eaf7SToby Isaac     ierr = PetscFree(globCones);CHKERRQ(ierr);
1032ac04eaf7SToby Isaac   }
1033cc71bff1SMichael Lange   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
1034cc71bff1SMichael Lange   ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
10353533c52bSMatthew G. Knepley #if PETSC_USE_DEBUG
10363533c52bSMatthew G. Knepley   {
10373533c52bSMatthew G. Knepley     PetscInt  p;
10383533c52bSMatthew G. Knepley     PetscBool valid = PETSC_TRUE;
10393533c52bSMatthew G. Knepley     for (p = 0; p < newConesSize; ++p) {
10403533c52bSMatthew G. Knepley       if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
10413533c52bSMatthew G. Knepley     }
10423533c52bSMatthew G. Knepley     if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
10433533c52bSMatthew G. Knepley   }
10443533c52bSMatthew G. Knepley #endif
1045c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
1046cc71bff1SMichael Lange   if (flg) {
1047cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
1048cc71bff1SMichael Lange     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1049cc71bff1SMichael Lange     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
1050cc71bff1SMichael Lange     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1051cc71bff1SMichael Lange     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
1052cc71bff1SMichael Lange   }
1053cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
1054cc71bff1SMichael Lange   ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr);
1055cc71bff1SMichael Lange   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1056cc71bff1SMichael Lange   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
1057cc71bff1SMichael Lange   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
1058cc71bff1SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
1059cc71bff1SMichael Lange   /* Create supports and stratify sieve */
1060cc71bff1SMichael Lange   {
1061cc71bff1SMichael Lange     PetscInt pStart, pEnd;
1062cc71bff1SMichael Lange 
1063cc71bff1SMichael Lange     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1064cc71bff1SMichael Lange     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1065cc71bff1SMichael Lange   }
1066cc71bff1SMichael Lange   ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr);
1067cc71bff1SMichael Lange   ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr);
1068f5bf2dbfSMichael Lange   pmesh->useCone    = mesh->useCone;
1069f5bf2dbfSMichael Lange   pmesh->useClosure = mesh->useClosure;
1070cc71bff1SMichael Lange   PetscFunctionReturn(0);
1071cc71bff1SMichael Lange }
1072cc71bff1SMichael Lange 
1073cc71bff1SMichael Lange #undef __FUNCT__
10740df0e737SMichael Lange #define __FUNCT__ "DMPlexDistributeCoordinates"
10750df0e737SMichael Lange PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel)
10760df0e737SMichael Lange {
10770df0e737SMichael Lange   MPI_Comm         comm;
10780df0e737SMichael Lange   PetscSection     originalCoordSection, newCoordSection;
10790df0e737SMichael Lange   Vec              originalCoordinates, newCoordinates;
10800df0e737SMichael Lange   PetscInt         bs;
10810df0e737SMichael Lange   const char      *name;
10820df0e737SMichael Lange   const PetscReal *maxCell, *L;
10835dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
10840df0e737SMichael Lange   PetscErrorCode   ierr;
10850df0e737SMichael Lange 
10860df0e737SMichael Lange   PetscFunctionBegin;
10870df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10880df0e737SMichael Lange   PetscValidPointer(dmParallel, 3);
10890df0e737SMichael Lange 
10900df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
10910df0e737SMichael Lange   ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
10920df0e737SMichael Lange   ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr);
10930df0e737SMichael Lange   ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
10940df0e737SMichael Lange   if (originalCoordinates) {
10950df0e737SMichael Lange     ierr = VecCreate(comm, &newCoordinates);CHKERRQ(ierr);
10960df0e737SMichael Lange     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
10970df0e737SMichael Lange     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
10980df0e737SMichael Lange 
10990df0e737SMichael Lange     ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
11000df0e737SMichael Lange     ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr);
11010df0e737SMichael Lange     ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr);
11020df0e737SMichael Lange     ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr);
11030df0e737SMichael Lange     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
11040df0e737SMichael Lange   }
11055dc8c3f7SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
11065dc8c3f7SMatthew G. Knepley   if (L) {ierr = DMSetPeriodicity(dmParallel, maxCell, L, bd);CHKERRQ(ierr);}
11070df0e737SMichael Lange   PetscFunctionReturn(0);
11080df0e737SMichael Lange }
11090df0e737SMichael Lange 
11100df0e737SMichael Lange #undef __FUNCT__
11110df0e737SMichael Lange #define __FUNCT__ "DMPlexDistributeLabels"
1112d995df53SMatthew G. Knepley /* Here we are assuming that process 0 always has everything */
11132eb1fa14SMichael Lange PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel)
11140df0e737SMichael Lange {
1115df0420ecSMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex*) dm->data;
11160df0e737SMichael Lange   MPI_Comm         comm;
11177980c9d4SMatthew G. Knepley   DMLabel          depthLabel;
11180df0e737SMichael Lange   PetscMPIInt      rank;
11197980c9d4SMatthew G. Knepley   PetscInt         depth, d, numLabels, numLocalLabels, l;
1120df0420ecSMatthew G. Knepley   PetscBool        hasLabels = PETSC_FALSE, lsendDepth, sendDepth;
1121df0420ecSMatthew G. Knepley   PetscObjectState depthState = -1;
11220df0e737SMichael Lange   PetscErrorCode   ierr;
11230df0e737SMichael Lange 
11240df0e737SMichael Lange   PetscFunctionBegin;
11250df0e737SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11262eb1fa14SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
11270df0e737SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
11280df0e737SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
11290df0e737SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
11300df0e737SMichael Lange 
1131df0420ecSMatthew G. Knepley   /* If the user has changed the depth label, communicate it instead */
11327980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
11337980c9d4SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
11347980c9d4SMatthew G. Knepley   if (depthLabel) {ierr = DMLabelGetState(depthLabel, &depthState);CHKERRQ(ierr);}
1135df0420ecSMatthew G. Knepley   lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE;
1136b2566f29SBarry Smith   ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
1137df0420ecSMatthew G. Knepley   if (sendDepth) {
11387980c9d4SMatthew G. Knepley     ierr = DMRemoveLabel(dmParallel, "depth", &depthLabel);CHKERRQ(ierr);
11397980c9d4SMatthew G. Knepley     ierr = DMLabelDestroy(&depthLabel);CHKERRQ(ierr);
1140df0420ecSMatthew G. Knepley   }
1141d995df53SMatthew G. Knepley   /* Everyone must have either the same number of labels, or none */
1142c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr);
1143d995df53SMatthew G. Knepley   numLabels = numLocalLabels;
11440df0e737SMichael Lange   ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1145627847f0SMatthew G. Knepley   if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE;
1146bdd2d751SMichael Lange   for (l = numLabels-1; l >= 0; --l) {
1147bdd2d751SMichael Lange     DMLabel     label = NULL, labelNew = NULL;
11480df0e737SMichael Lange     PetscBool   isdepth;
11490df0e737SMichael Lange 
1150d995df53SMatthew G. Knepley     if (hasLabels) {
1151c58f1c22SToby Isaac       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
11520df0e737SMichael Lange       /* Skip "depth" because it is recreated */
1153bdd2d751SMichael Lange       ierr = PetscStrcmp(label->name, "depth", &isdepth);CHKERRQ(ierr);
1154bdd2d751SMichael Lange     }
11550df0e737SMichael Lange     ierr = MPI_Bcast(&isdepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
1156df0420ecSMatthew G. Knepley     if (isdepth && !sendDepth) continue;
1157bdd2d751SMichael Lange     ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr);
11587980c9d4SMatthew G. Knepley     if (isdepth) {
11597980c9d4SMatthew G. Knepley       /* Put in any missing strata which can occur if users are managing the depth label themselves */
11607980c9d4SMatthew G. Knepley       PetscInt gdepth;
11617980c9d4SMatthew G. Knepley 
11627980c9d4SMatthew G. Knepley       ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
11637980c9d4SMatthew G. Knepley       if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth);
11647980c9d4SMatthew G. Knepley       for (d = 0; d <= gdepth; ++d) {
11657980c9d4SMatthew G. Knepley         PetscBool has;
11667980c9d4SMatthew G. Knepley 
11677980c9d4SMatthew G. Knepley         ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr);
11687980c9d4SMatthew G. Knepley         if (!has) ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);
11697980c9d4SMatthew G. Knepley       }
11707980c9d4SMatthew G. Knepley     }
1171c58f1c22SToby Isaac     ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr);
11720df0e737SMichael Lange   }
11730df0e737SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
11740df0e737SMichael Lange   PetscFunctionReturn(0);
11750df0e737SMichael Lange }
11760df0e737SMichael Lange 
11779734c634SMichael Lange #undef __FUNCT__
11789734c634SMichael Lange #define __FUNCT__ "DMPlexDistributeSetupHybrid"
11799734c634SMichael Lange PetscErrorCode DMPlexDistributeSetupHybrid(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping renumbering, DM dmParallel)
11809734c634SMichael Lange {
11819734c634SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
11829734c634SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1183*f0e73a3dSToby Isaac   PetscBool      *isHybrid, *isHybridParallel;
1184*f0e73a3dSToby Isaac   PetscInt        dim, depth, d;
1185*f0e73a3dSToby Isaac   PetscInt        pStart, pEnd, pStartP, pEndP;
11869734c634SMichael Lange   PetscErrorCode  ierr;
11879734c634SMichael Lange 
11889734c634SMichael Lange   PetscFunctionBegin;
11899734c634SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11909734c634SMichael Lange   PetscValidPointer(dmParallel, 4);
11919734c634SMichael Lange 
11929734c634SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11939734c634SMichael Lange   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1194*f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1195*f0e73a3dSToby Isaac   ierr = DMPlexGetChart(dmParallel,&pStartP,&pEndP);CHKERRQ(ierr);
1196*f0e73a3dSToby Isaac   ierr = PetscCalloc2(pEnd-pStart,&isHybrid,pEndP-pStartP,&isHybridParallel);CHKERRQ(ierr);
1197*f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1198*f0e73a3dSToby Isaac     PetscInt hybridMax = (depth == 1 && d == 1) ? mesh->hybridPointMax[dim] : mesh->hybridPointMax[d];
11999734c634SMichael Lange 
1200*f0e73a3dSToby Isaac     if (hybridMax >= 0) {
1201*f0e73a3dSToby Isaac       PetscInt sStart, sEnd, p;
12029734c634SMichael Lange 
1203*f0e73a3dSToby Isaac       ierr = DMPlexGetDepthStratum(dm,d,&sStart,&sEnd);CHKERRQ(ierr);
1204*f0e73a3dSToby Isaac       for (p = hybridMax; p < sEnd; p++) isHybrid[p-pStart] = PETSC_TRUE;
12059734c634SMichael Lange     }
12069734c634SMichael Lange   }
1207*f0e73a3dSToby Isaac   ierr = PetscSFBcastBegin(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1208*f0e73a3dSToby Isaac   ierr = PetscSFBcastEnd(migrationSF,MPIU_BOOL,isHybrid,isHybridParallel);CHKERRQ(ierr);
1209*f0e73a3dSToby Isaac   for (d = 0; d <= dim; d++) pmesh->hybridPointMax[d] = -1;
1210*f0e73a3dSToby Isaac   for (d = 0; d <= depth; d++) {
1211*f0e73a3dSToby Isaac     PetscInt sStart, sEnd, p, dd;
1212*f0e73a3dSToby Isaac 
1213*f0e73a3dSToby Isaac     ierr = DMPlexGetDepthStratum(dmParallel,d,&sStart,&sEnd);CHKERRQ(ierr);
1214*f0e73a3dSToby Isaac     dd = (depth == 1 && d == 1) ? dim : d;
1215*f0e73a3dSToby Isaac     for (p = sStart; p < sEnd; p++) {
1216*f0e73a3dSToby Isaac       if (isHybridParallel[p-pStartP]) {
1217*f0e73a3dSToby Isaac         pmesh->hybridPointMax[dd] = p;
1218*f0e73a3dSToby Isaac         break;
1219*f0e73a3dSToby Isaac       }
1220*f0e73a3dSToby Isaac     }
1221*f0e73a3dSToby Isaac   }
1222*f0e73a3dSToby Isaac   ierr = PetscFree2(isHybrid,isHybridParallel);CHKERRQ(ierr);
12239734c634SMichael Lange   PetscFunctionReturn(0);
12249734c634SMichael Lange }
12250df0e737SMichael Lange 
1226a6f36705SMichael Lange #undef __FUNCT__
1227a6f36705SMichael Lange #define __FUNCT__ "DMPlexDistributeSetupTree"
122808633170SToby Isaac PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel)
1229a6f36705SMichael Lange {
123015078cd4SMichael Lange   DM_Plex        *mesh  = (DM_Plex*) dm->data;
123115078cd4SMichael Lange   DM_Plex        *pmesh = (DM_Plex*) (dmParallel)->data;
1232a6f36705SMichael Lange   MPI_Comm        comm;
1233a6f36705SMichael Lange   DM              refTree;
1234a6f36705SMichael Lange   PetscSection    origParentSection, newParentSection;
1235a6f36705SMichael Lange   PetscInt        *origParents, *origChildIDs;
1236a6f36705SMichael Lange   PetscBool       flg;
1237a6f36705SMichael Lange   PetscErrorCode  ierr;
1238a6f36705SMichael Lange 
1239a6f36705SMichael Lange   PetscFunctionBegin;
1240a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1241a6f36705SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 4);
1242a6f36705SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
1243a6f36705SMichael Lange 
1244a6f36705SMichael Lange   /* Set up tree */
1245a6f36705SMichael Lange   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1246a6f36705SMichael Lange   ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr);
1247a6f36705SMichael Lange   ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr);
1248a6f36705SMichael Lange   if (origParentSection) {
1249a6f36705SMichael Lange     PetscInt        pStart, pEnd;
125008633170SToby Isaac     PetscInt        *newParents, *newChildIDs, *globParents;
1251a6f36705SMichael Lange     PetscInt        *remoteOffsetsParents, newParentSize;
1252a6f36705SMichael Lange     PetscSF         parentSF;
1253a6f36705SMichael Lange 
1254a6f36705SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
1255a6f36705SMichael Lange     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr);
1256a6f36705SMichael Lange     ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr);
1257a6f36705SMichael Lange     ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr);
1258a6f36705SMichael Lange     ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr);
12598a713f3bSLawrence Mitchell     ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr);
1260a6f36705SMichael Lange     ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr);
1261a6f36705SMichael Lange     ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr);
126208633170SToby Isaac     if (original) {
126308633170SToby Isaac       PetscInt numParents;
126408633170SToby Isaac 
126508633170SToby Isaac       ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr);
126608633170SToby Isaac       ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr);
126708633170SToby Isaac       ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr);
126808633170SToby Isaac     }
126908633170SToby Isaac     else {
127008633170SToby Isaac       globParents = origParents;
127108633170SToby Isaac     }
127208633170SToby Isaac     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
127308633170SToby Isaac     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents);CHKERRQ(ierr);
127408633170SToby Isaac     if (original) {
127508633170SToby Isaac       ierr = PetscFree(globParents);CHKERRQ(ierr);
127608633170SToby Isaac     }
1277a6f36705SMichael Lange     ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1278a6f36705SMichael Lange     ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs);CHKERRQ(ierr);
1279a6f36705SMichael Lange     ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr);
12804a54e071SToby Isaac #if PETSC_USE_DEBUG
12814a54e071SToby Isaac     {
12824a54e071SToby Isaac       PetscInt  p;
12834a54e071SToby Isaac       PetscBool valid = PETSC_TRUE;
12844a54e071SToby Isaac       for (p = 0; p < newParentSize; ++p) {
12854a54e071SToby Isaac         if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);}
12864a54e071SToby Isaac       }
12874a54e071SToby Isaac       if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map");
12884a54e071SToby Isaac     }
12894a54e071SToby Isaac #endif
1290c5929fdfSBarry Smith     ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr);
1291a6f36705SMichael Lange     if (flg) {
1292a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr);
1293a6f36705SMichael Lange       ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1294a6f36705SMichael Lange       ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr);
1295a6f36705SMichael Lange       ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1296a6f36705SMichael Lange       ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr);
1297a6f36705SMichael Lange     }
1298a6f36705SMichael Lange     ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr);
1299a6f36705SMichael Lange     ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr);
1300a6f36705SMichael Lange     ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr);
1301a6f36705SMichael Lange     ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr);
1302a6f36705SMichael Lange   }
130315078cd4SMichael Lange   pmesh->useAnchors = mesh->useAnchors;
1304a6f36705SMichael Lange   PetscFunctionReturn(0);
1305a6f36705SMichael Lange }
13060df0e737SMichael Lange 
13070df0e737SMichael Lange #undef __FUNCT__
13084eca1733SMichael Lange #define __FUNCT__ "DMPlexDistributeSF"
1309f8987ae8SMichael Lange PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel)
13104eca1733SMichael Lange {
13114eca1733SMichael Lange   DM_Plex               *mesh  = (DM_Plex*) dm->data;
13124eca1733SMichael Lange   DM_Plex               *pmesh = (DM_Plex*) (dmParallel)->data;
13134eca1733SMichael Lange   PetscMPIInt            rank, numProcs;
13144eca1733SMichael Lange   MPI_Comm               comm;
13154eca1733SMichael Lange   PetscErrorCode         ierr;
13164eca1733SMichael Lange 
13174eca1733SMichael Lange   PetscFunctionBegin;
13184eca1733SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13194eca1733SMichael Lange   PetscValidPointer(dmParallel,7);
13204eca1733SMichael Lange 
13214eca1733SMichael Lange   /* Create point SF for parallel mesh */
13224eca1733SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13234eca1733SMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
13244eca1733SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
13254eca1733SMichael Lange   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
13264eca1733SMichael Lange   {
13274eca1733SMichael Lange     const PetscInt *leaves;
13284eca1733SMichael Lange     PetscSFNode    *remotePoints, *rowners, *lowners;
13294eca1733SMichael Lange     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
13304eca1733SMichael Lange     PetscInt        pStart, pEnd;
13314eca1733SMichael Lange 
13324eca1733SMichael Lange     ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
13334eca1733SMichael Lange     ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
13344eca1733SMichael Lange     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
13354eca1733SMichael Lange     for (p=0; p<numRoots; p++) {
13364eca1733SMichael Lange       rowners[p].rank  = -1;
13374eca1733SMichael Lange       rowners[p].index = -1;
13384eca1733SMichael Lange     }
13394eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13404eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13414eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
13424eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
13434eca1733SMichael Lange         lowners[p].rank  = rank;
13444eca1733SMichael Lange         lowners[p].index = leaves ? leaves[p] : p;
13454eca1733SMichael Lange       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
13464eca1733SMichael Lange         lowners[p].rank  = -2;
13474eca1733SMichael Lange         lowners[p].index = -2;
13484eca1733SMichael Lange       }
13494eca1733SMichael Lange     }
13504eca1733SMichael Lange     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
13514eca1733SMichael Lange       rowners[p].rank  = -3;
13524eca1733SMichael Lange       rowners[p].index = -3;
13534eca1733SMichael Lange     }
13544eca1733SMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
13554eca1733SMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
13564eca1733SMichael Lange     ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13574eca1733SMichael Lange     ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
13584eca1733SMichael Lange     for (p = 0; p < numLeaves; ++p) {
13594eca1733SMichael Lange       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
13604eca1733SMichael Lange       if (lowners[p].rank != rank) ++numGhostPoints;
13614eca1733SMichael Lange     }
13624eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr);
13634eca1733SMichael Lange     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
13644eca1733SMichael Lange     for (p = 0, gp = 0; p < numLeaves; ++p) {
13654eca1733SMichael Lange       if (lowners[p].rank != rank) {
13664eca1733SMichael Lange         ghostPoints[gp]        = leaves ? leaves[p] : p;
13674eca1733SMichael Lange         remotePoints[gp].rank  = lowners[p].rank;
13684eca1733SMichael Lange         remotePoints[gp].index = lowners[p].index;
13694eca1733SMichael Lange         ++gp;
13704eca1733SMichael Lange       }
13714eca1733SMichael Lange     }
13724eca1733SMichael Lange     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
13734eca1733SMichael Lange     ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
13744eca1733SMichael Lange     ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr);
13754eca1733SMichael Lange   }
13764eca1733SMichael Lange   pmesh->useCone    = mesh->useCone;
13774eca1733SMichael Lange   pmesh->useClosure = mesh->useClosure;
13784eca1733SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
13794eca1733SMichael Lange   PetscFunctionReturn(0);
13804eca1733SMichael Lange }
13814eca1733SMichael Lange 
13824eca1733SMichael Lange #undef __FUNCT__
1383f5bf2dbfSMichael Lange #define __FUNCT__ "DMPlexCreatePointSF"
1384f5bf2dbfSMichael Lange /*@C
1385f5bf2dbfSMichael Lange   DMPlexDerivePointSF - Build a point SF from an SF describing a point migration
1386f5bf2dbfSMichael Lange 
1387f5bf2dbfSMichael Lange   Input Parameter:
1388f5bf2dbfSMichael Lange + dm          - The source DMPlex object
1389f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping
13901627f6ccSMichael Lange . ownership   - Flag causing a vote to determine point ownership
1391f5bf2dbfSMichael Lange 
1392f5bf2dbfSMichael Lange   Output Parameter:
1393f5bf2dbfSMichael Lange - pointSF     - The star forest describing the point overlap in the remapped DM
1394f5bf2dbfSMichael Lange 
1395f5bf2dbfSMichael Lange   Level: developer
1396f5bf2dbfSMichael Lange 
1397f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
1398f5bf2dbfSMichael Lange @*/
1399f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF)
1400f5bf2dbfSMichael Lange {
1401f5bf2dbfSMichael Lange   PetscMPIInt        rank;
14021627f6ccSMichael Lange   PetscInt           p, nroots, nleaves, idx, npointLeaves;
1403f5bf2dbfSMichael Lange   PetscInt          *pointLocal;
1404f5bf2dbfSMichael Lange   const PetscInt    *leaves;
1405f5bf2dbfSMichael Lange   const PetscSFNode *roots;
1406f5bf2dbfSMichael Lange   PetscSFNode       *rootNodes, *leafNodes, *pointRemote;
1407f5bf2dbfSMichael Lange   PetscErrorCode     ierr;
1408f5bf2dbfSMichael Lange 
1409f5bf2dbfSMichael Lange   PetscFunctionBegin;
1410f5bf2dbfSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1411f5bf2dbfSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1412f5bf2dbfSMichael Lange 
1413f5bf2dbfSMichael Lange   ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr);
1414f5bf2dbfSMichael Lange   ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr);
1415f5bf2dbfSMichael Lange   if (ownership) {
14161627f6ccSMichael Lange     /* Point ownership vote: Process with highest rank ownes shared points */
1417f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; ++p) {
1418f5bf2dbfSMichael Lange       /* Either put in a bid or we know we own it */
1419f5bf2dbfSMichael Lange       leafNodes[p].rank  = rank;
142043f7d02bSMichael Lange       leafNodes[p].index = p;
1421f5bf2dbfSMichael Lange     }
1422f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
14231627f6ccSMichael Lange       /* Root must not participate in the reduction, flag so that MAXLOC does not use */
1424f5bf2dbfSMichael Lange       rootNodes[p].rank  = -3;
1425f5bf2dbfSMichael Lange       rootNodes[p].index = -3;
1426f5bf2dbfSMichael Lange     }
1427f5bf2dbfSMichael Lange     ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1428f5bf2dbfSMichael Lange     ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, leafNodes, rootNodes, MPI_MAXLOC);CHKERRQ(ierr);
1429f5bf2dbfSMichael Lange   } else {
1430f5bf2dbfSMichael Lange     for (p = 0; p < nroots; p++) {
1431f5bf2dbfSMichael Lange       rootNodes[p].index = -1;
1432f5bf2dbfSMichael Lange       rootNodes[p].rank = rank;
1433f5bf2dbfSMichael Lange     };
1434f5bf2dbfSMichael Lange     for (p = 0; p < nleaves; p++) {
1435f5bf2dbfSMichael Lange       /* Write new local id into old location */
1436f5bf2dbfSMichael Lange       if (roots[p].rank == rank) {
14376462276dSToby Isaac         rootNodes[roots[p].index].index = leaves ? leaves[p] : p;
1438f5bf2dbfSMichael Lange       }
1439f5bf2dbfSMichael Lange     }
1440f5bf2dbfSMichael Lange   }
1441f5bf2dbfSMichael Lange   ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1442f5bf2dbfSMichael Lange   ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes);CHKERRQ(ierr);
1443f5bf2dbfSMichael Lange 
1444f5bf2dbfSMichael Lange   for (npointLeaves = 0, p = 0; p < nleaves; p++) {if (leafNodes[p].rank != rank) npointLeaves++;}
14451627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr);
14461627f6ccSMichael Lange   ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr);
1447f5bf2dbfSMichael Lange   for (idx = 0, p = 0; p < nleaves; p++) {
1448f5bf2dbfSMichael Lange     if (leafNodes[p].rank != rank) {
1449f5bf2dbfSMichael Lange       pointLocal[idx] = p;
1450f5bf2dbfSMichael Lange       pointRemote[idx] = leafNodes[p];
1451f5bf2dbfSMichael Lange       idx++;
1452f5bf2dbfSMichael Lange     }
1453f5bf2dbfSMichael Lange   }
1454f5bf2dbfSMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr);
14551627f6ccSMichael Lange   ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr);
1456f5bf2dbfSMichael Lange   ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1457f5bf2dbfSMichael Lange   ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr);
1458f5bf2dbfSMichael Lange   PetscFunctionReturn(0);
1459f5bf2dbfSMichael Lange }
1460f5bf2dbfSMichael Lange 
1461f5bf2dbfSMichael Lange #undef __FUNCT__
146215078cd4SMichael Lange #define __FUNCT__ "DMPlexMigrate"
146315078cd4SMichael Lange /*@C
146415078cd4SMichael Lange   DMPlexMigrate  - Migrates internal DM data over the supplied star forest
146515078cd4SMichael Lange 
146615078cd4SMichael Lange   Input Parameter:
146715078cd4SMichael Lange + dm       - The source DMPlex object
14681627f6ccSMichael Lange . sf       - The star forest communication context describing the migration pattern
146915078cd4SMichael Lange 
147015078cd4SMichael Lange   Output Parameter:
147115078cd4SMichael Lange - targetDM - The target DMPlex object
147215078cd4SMichael Lange 
1473b9f40539SMichael Lange   Level: intermediate
147415078cd4SMichael Lange 
147515078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap()
147615078cd4SMichael Lange @*/
1477b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM)
147815078cd4SMichael Lange {
1479b9f40539SMichael Lange   MPI_Comm               comm;
1480b9f40539SMichael Lange   PetscInt               dim, nroots;
1481b9f40539SMichael Lange   PetscSF                sfPoint;
148215078cd4SMichael Lange   ISLocalToGlobalMapping ltogMigration;
1483ac04eaf7SToby Isaac   ISLocalToGlobalMapping ltogOriginal = NULL;
1484b9f40539SMichael Lange   PetscBool              flg;
148515078cd4SMichael Lange   PetscErrorCode         ierr;
148615078cd4SMichael Lange 
148715078cd4SMichael Lange   PetscFunctionBegin;
148815078cd4SMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14891b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
1490b9f40539SMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
149115078cd4SMichael Lange   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
149215078cd4SMichael Lange   ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr);
149315078cd4SMichael Lange 
1494bfb0467fSMichael Lange   /* Check for a one-to-all distribution pattern */
1495b9f40539SMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1496b9f40539SMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1497bfb0467fSMichael Lange   if (nroots >= 0) {
1498b9f40539SMichael Lange     IS                     isOriginal;
1499ac04eaf7SToby Isaac     PetscInt               n, size, nleaves;
1500ac04eaf7SToby Isaac     PetscInt              *numbering_orig, *numbering_new;
1501b9f40539SMichael Lange     /* Get the original point numbering */
1502b9f40539SMichael Lange     ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr);
1503b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(isOriginal, &ltogOriginal);CHKERRQ(ierr);
1504b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr);
1505b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1506b9f40539SMichael Lange     /* Convert to positive global numbers */
1507b9f40539SMichael Lange     for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);}
1508b9f40539SMichael Lange     /* Derive the new local-to-global mapping from the old one */
1509b9f40539SMichael Lange     ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
1510b9f40539SMichael Lange     ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr);
1511b9f40539SMichael Lange     ierr = PetscSFBcastBegin(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1512b9f40539SMichael Lange     ierr = PetscSFBcastEnd(sf, MPIU_INT, (PetscInt *) numbering_orig, numbering_new);CHKERRQ(ierr);
1513b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, (const PetscInt*) numbering_new, PETSC_OWN_POINTER, &ltogMigration);CHKERRQ(ierr);
1514b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr);
1515b9f40539SMichael Lange     ierr = ISDestroy(&isOriginal);CHKERRQ(ierr);
151615078cd4SMichael Lange   } else {
1517bfb0467fSMichael Lange     /* One-to-all distribution pattern: We can derive LToG from SF */
151815078cd4SMichael Lange     ierr = ISLocalToGlobalMappingCreateSF(sf, 0, &ltogMigration);CHKERRQ(ierr);
151915078cd4SMichael Lange   }
1520c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
1521b9f40539SMichael Lange   if (flg) {
1522b9f40539SMichael Lange     ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr);
1523b9f40539SMichael Lange     ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr);
1524b9f40539SMichael Lange   }
152515078cd4SMichael Lange   /* Migrate DM data to target DM */
1526ac04eaf7SToby Isaac   ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
152715078cd4SMichael Lange   ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr);
152815078cd4SMichael Lange   ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr);
152915078cd4SMichael Lange   ierr = DMPlexDistributeSetupHybrid(dm, sf, ltogMigration, targetDM);CHKERRQ(ierr);
153008633170SToby Isaac   ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr);
1531ac04eaf7SToby Isaac   ierr = ISLocalToGlobalMappingDestroy(&ltogOriginal);CHKERRQ(ierr);
1532302440fdSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&ltogMigration);CHKERRQ(ierr);
15331b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr);
153415078cd4SMichael Lange   PetscFunctionReturn(0);
153515078cd4SMichael Lange }
153615078cd4SMichael Lange 
153715078cd4SMichael Lange #undef __FUNCT__
153870034214SMatthew G. Knepley #define __FUNCT__ "DMPlexDistribute"
15393b8f15a2SMatthew G. Knepley /*@C
154070034214SMatthew G. Knepley   DMPlexDistribute - Distributes the mesh and any associated sections.
154170034214SMatthew G. Knepley 
154270034214SMatthew G. Knepley   Not Collective
154370034214SMatthew G. Knepley 
154470034214SMatthew G. Knepley   Input Parameter:
154570034214SMatthew G. Knepley + dm  - The original DMPlex object
154670034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default
154770034214SMatthew G. Knepley 
154870034214SMatthew G. Knepley   Output Parameter:
154970034214SMatthew G. Knepley + sf - The PetscSF used for point distribution
155070034214SMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
155170034214SMatthew G. Knepley 
155270034214SMatthew G. Knepley   Note: If the mesh was not distributed, the return value is NULL.
155370034214SMatthew G. Knepley 
155493a1dc33SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMPlexSetAdjacencyUseCone() and
155570034214SMatthew G. Knepley   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
155670034214SMatthew G. Knepley   representation on the mesh.
155770034214SMatthew G. Knepley 
155870034214SMatthew G. Knepley   Level: intermediate
155970034214SMatthew G. Knepley 
156070034214SMatthew G. Knepley .keywords: mesh, elements
156170034214SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
156270034214SMatthew G. Knepley @*/
156380cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel)
156470034214SMatthew G. Knepley {
156570034214SMatthew G. Knepley   MPI_Comm               comm;
156615078cd4SMichael Lange   PetscPartitioner       partitioner;
1567f8987ae8SMichael Lange   IS                     cellPart;
1568f8987ae8SMichael Lange   PetscSection           cellPartSection;
1569cf86098cSMatthew G. Knepley   DM                     dmCoord;
1570f8987ae8SMichael Lange   DMLabel                lblPartition, lblMigration;
157143f7d02bSMichael Lange   PetscSF                sfProcess, sfMigration, sfStratified, sfPoint;
157270034214SMatthew G. Knepley   PetscBool              flg;
157370034214SMatthew G. Knepley   PetscMPIInt            rank, numProcs, p;
157470034214SMatthew G. Knepley   PetscErrorCode         ierr;
157570034214SMatthew G. Knepley 
157670034214SMatthew G. Knepley   PetscFunctionBegin;
157770034214SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
157870034214SMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
157970034214SMatthew G. Knepley   PetscValidPointer(dmParallel,5);
158070034214SMatthew G. Knepley 
158170034214SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
158270034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
158370034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
158470034214SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
158570034214SMatthew G. Knepley 
158670034214SMatthew G. Knepley   *dmParallel = NULL;
158770034214SMatthew G. Knepley   if (numProcs == 1) PetscFunctionReturn(0);
158870034214SMatthew G. Knepley 
158915078cd4SMichael Lange   /* Create cell partition */
159077623264SMatthew G. Knepley   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
159177623264SMatthew G. Knepley   ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr);
159215078cd4SMichael Lange   ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr);
159315078cd4SMichael Lange   ierr = PetscPartitionerPartition(partitioner, dm, cellPartSection, &cellPart);CHKERRQ(ierr);
1594f8987ae8SMichael Lange   {
1595f8987ae8SMichael Lange     /* Convert partition to DMLabel */
1596f8987ae8SMichael Lange     PetscInt proc, pStart, pEnd, npoints, poffset;
1597f8987ae8SMichael Lange     const PetscInt *points;
1598f8987ae8SMichael Lange     ierr = DMLabelCreate("Point Partition", &lblPartition);CHKERRQ(ierr);
1599f8987ae8SMichael Lange     ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr);
1600f8987ae8SMichael Lange     ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr);
1601f8987ae8SMichael Lange     for (proc = pStart; proc < pEnd; proc++) {
1602f8987ae8SMichael Lange       ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr);
1603f8987ae8SMichael Lange       ierr = PetscSectionGetOffset(cellPartSection, proc, &poffset);CHKERRQ(ierr);
1604f8987ae8SMichael Lange       for (p = poffset; p < poffset+npoints; p++) {
1605f8987ae8SMichael Lange         ierr = DMLabelSetValue(lblPartition, points[p], proc);CHKERRQ(ierr);
160670034214SMatthew G. Knepley       }
1607f8987ae8SMichael Lange     }
1608f8987ae8SMichael Lange     ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr);
1609f8987ae8SMichael Lange   }
1610f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelClosure(dm, lblPartition);CHKERRQ(ierr);
1611f8987ae8SMichael Lange   {
1612f8987ae8SMichael Lange     /* Build a global process SF */
1613f8987ae8SMichael Lange     PetscSFNode *remoteProc;
1614f8987ae8SMichael Lange     ierr = PetscMalloc1(numProcs, &remoteProc);CHKERRQ(ierr);
1615f8987ae8SMichael Lange     for (p = 0; p < numProcs; ++p) {
1616f8987ae8SMichael Lange       remoteProc[p].rank  = p;
1617f8987ae8SMichael Lange       remoteProc[p].index = rank;
1618f8987ae8SMichael Lange     }
1619f8987ae8SMichael Lange     ierr = PetscSFCreate(comm, &sfProcess);CHKERRQ(ierr);
1620f8987ae8SMichael Lange     ierr = PetscObjectSetName((PetscObject) sfProcess, "Process SF");CHKERRQ(ierr);
1621f8987ae8SMichael Lange     ierr = PetscSFSetGraph(sfProcess, numProcs, numProcs, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);CHKERRQ(ierr);
1622f8987ae8SMichael Lange   }
1623f8987ae8SMichael Lange   ierr = DMLabelCreate("Point migration", &lblMigration);CHKERRQ(ierr);
1624f8987ae8SMichael Lange   ierr = DMPlexPartitionLabelInvert(dm, lblPartition, sfProcess, lblMigration);CHKERRQ(ierr);
1625302440fdSBarry Smith   ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr);
162643f7d02bSMichael Lange   /* Stratify the SF in case we are migrating an already parallel plex */
162743f7d02bSMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr);
162843f7d02bSMichael Lange   ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
162943f7d02bSMichael Lange   sfMigration = sfStratified;
1630f8987ae8SMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1631c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
163270034214SMatthew G. Knepley   if (flg) {
1633f8987ae8SMichael Lange     ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1634f8987ae8SMichael Lange     ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
163570034214SMatthew G. Knepley   }
1636f8987ae8SMichael Lange 
163715078cd4SMichael Lange   /* Create non-overlapping parallel DM and migrate internal data */
163870034214SMatthew G. Knepley   ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
163970034214SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
1640b9f40539SMichael Lange   ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr);
164170034214SMatthew G. Knepley 
1642a157612eSMichael Lange   /* Build the point SF without overlap */
16431627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr);
1644f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr);
1645cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr);
1646cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1647f5bf2dbfSMichael Lange   if (flg) {ierr = PetscSFView(sfPoint, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
164870034214SMatthew G. Knepley 
1649a157612eSMichael Lange   if (overlap > 0) {
165015078cd4SMichael Lange     DM                 dmOverlap;
165115078cd4SMichael Lange     PetscInt           nroots, nleaves;
165215078cd4SMichael Lange     PetscSFNode       *newRemote;
165315078cd4SMichael Lange     const PetscSFNode *oldRemote;
165415078cd4SMichael Lange     PetscSF            sfOverlap, sfOverlapPoint;
1655a157612eSMichael Lange     /* Add the partition overlap to the distributed DM */
1656b9f40539SMichael Lange     ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr);
1657a157612eSMichael Lange     ierr = DMDestroy(dmParallel);CHKERRQ(ierr);
1658a157612eSMichael Lange     *dmParallel = dmOverlap;
1659c389ea9fSToby Isaac     if (flg) {
16603d822a50SMichael Lange       ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr);
166115078cd4SMichael Lange       ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr);
1662c389ea9fSToby Isaac     }
166343331d4aSMichael Lange 
1664f8987ae8SMichael Lange     /* Re-map the migration SF to establish the full migration pattern */
1665f8987ae8SMichael Lange     ierr = PetscSFGetGraph(sfMigration, &nroots, NULL, NULL, &oldRemote);CHKERRQ(ierr);
166615078cd4SMichael Lange     ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr);
166743331d4aSMichael Lange     ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr);
166815078cd4SMichael Lange     ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
166915078cd4SMichael Lange     ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote);CHKERRQ(ierr);
167015078cd4SMichael Lange     ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr);
167115078cd4SMichael Lange     ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr);
167215078cd4SMichael Lange     ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1673f8987ae8SMichael Lange     ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);
167415078cd4SMichael Lange     sfMigration = sfOverlapPoint;
1675c389ea9fSToby Isaac   }
1676bf5244c7SMatthew G. Knepley   /* Cleanup Partition */
1677f8987ae8SMichael Lange   ierr = PetscSFDestroy(&sfProcess);CHKERRQ(ierr);
1678f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr);
1679f8987ae8SMichael Lange   ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr);
16804eca1733SMichael Lange   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
16814eca1733SMichael Lange   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
1682721cbd76SMatthew G. Knepley   /* Copy BC */
1683a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, *dmParallel);CHKERRQ(ierr);
168466fe0bfeSMatthew G. Knepley   /* Create sfNatural */
168566fe0bfeSMatthew G. Knepley   if (dm->useNatural) {
168666fe0bfeSMatthew G. Knepley     PetscSection section;
168766fe0bfeSMatthew G. Knepley 
168866fe0bfeSMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
16890c1f158bSMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr);
169066fe0bfeSMatthew G. Knepley   }
1691721cbd76SMatthew G. Knepley   /* Cleanup */
1692f8987ae8SMichael Lange   if (sf) {*sf = sfMigration;}
1693f8987ae8SMichael Lange   else    {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);}
1694f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
169570034214SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
169670034214SMatthew G. Knepley   PetscFunctionReturn(0);
169770034214SMatthew G. Knepley }
1698a157612eSMichael Lange 
1699a157612eSMichael Lange #undef __FUNCT__
1700a157612eSMichael Lange #define __FUNCT__ "DMPlexDistributeOverlap"
1701a157612eSMichael Lange /*@C
1702a157612eSMichael Lange   DMPlexDistribute - Add partition overlap to a distributed non-overlapping DM.
1703a157612eSMichael Lange 
1704a157612eSMichael Lange   Not Collective
1705a157612eSMichael Lange 
1706a157612eSMichael Lange   Input Parameter:
1707a157612eSMichael Lange + dm  - The non-overlapping distrbuted DMPlex object
1708a157612eSMichael Lange - overlap - The overlap of partitions, 0 is the default
1709a157612eSMichael Lange 
1710a157612eSMichael Lange   Output Parameter:
1711a157612eSMichael Lange + sf - The PetscSF used for point distribution
1712a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL
1713a157612eSMichael Lange 
1714a157612eSMichael Lange   Note: If the mesh was not distributed, the return value is NULL.
1715a157612eSMichael Lange 
1716a157612eSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
1717a157612eSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
1718a157612eSMichael Lange   representation on the mesh.
1719a157612eSMichael Lange 
1720a157612eSMichael Lange   Level: intermediate
1721a157612eSMichael Lange 
1722a157612eSMichael Lange .keywords: mesh, elements
1723a157612eSMichael Lange .seealso: DMPlexCreate(), DMPlexDistributeByFace(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
1724a157612eSMichael Lange @*/
1725b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap)
1726a157612eSMichael Lange {
1727a157612eSMichael Lange   MPI_Comm               comm;
1728a157612eSMichael Lange   PetscMPIInt            rank;
1729a157612eSMichael Lange   PetscSection           rootSection, leafSection;
1730a157612eSMichael Lange   IS                     rootrank, leafrank;
1731cf86098cSMatthew G. Knepley   DM                     dmCoord;
1732a9f1d5b2SMichael Lange   DMLabel                lblOverlap;
1733f5bf2dbfSMichael Lange   PetscSF                sfOverlap, sfStratified, sfPoint;
1734a157612eSMichael Lange   PetscErrorCode         ierr;
1735a157612eSMichael Lange 
1736a157612eSMichael Lange   PetscFunctionBegin;
1737a157612eSMichael Lange   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1738a157612eSMichael Lange   if (sf) PetscValidPointer(sf, 3);
1739a157612eSMichael Lange   PetscValidPointer(dmOverlap, 4);
1740a157612eSMichael Lange 
17411b858b30SMichael Lange   ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1742a157612eSMichael Lange   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1743a157612eSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1744a157612eSMichael Lange 
1745a157612eSMichael Lange   /* Compute point overlap with neighbouring processes on the distributed DM */
1746a157612eSMichael Lange   ierr = PetscLogEventBegin(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1747a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
1748a157612eSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
1749a157612eSMichael Lange   ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr);
1750a9f1d5b2SMichael Lange   ierr = DMPlexCreateOverlap(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr);
1751a9f1d5b2SMichael Lange   /* Convert overlap label to stratified migration SF */
1752a9f1d5b2SMichael Lange   ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr);
1753a9f1d5b2SMichael Lange   ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr);
1754a9f1d5b2SMichael Lange   ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);
1755a9f1d5b2SMichael Lange   sfOverlap = sfStratified;
1756a9f1d5b2SMichael Lange   ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr);
1757a9f1d5b2SMichael Lange   ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr);
1758a9f1d5b2SMichael Lange 
175915fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
176015fff7beSMatthew G. Knepley   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
176115fff7beSMatthew G. Knepley   ierr = ISDestroy(&rootrank);CHKERRQ(ierr);
176215fff7beSMatthew G. Knepley   ierr = ISDestroy(&leafrank);CHKERRQ(ierr);
1763a157612eSMichael Lange   ierr = PetscLogEventEnd(PETSCPARTITIONER_Partition,dm,0,0,0);CHKERRQ(ierr);
1764a157612eSMichael Lange 
1765a157612eSMichael Lange   /* Build the overlapping DM */
1766a157612eSMichael Lange   ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr);
1767a157612eSMichael Lange   ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr);
1768a9f1d5b2SMichael Lange   ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr);
1769f5bf2dbfSMichael Lange   /* Build the new point SF */
17701627f6ccSMichael Lange   ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
1771f5bf2dbfSMichael Lange   ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr);
1772cf86098cSMatthew G. Knepley   ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr);
1773cf86098cSMatthew G. Knepley   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
1774f5bf2dbfSMichael Lange   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
1775a157612eSMichael Lange   /* Cleanup overlap partition */
1776a9f1d5b2SMichael Lange   ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr);
1777a9f1d5b2SMichael Lange   if (sf) *sf = sfOverlap;
1778a9f1d5b2SMichael Lange   else    {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);}
17791b858b30SMichael Lange   ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr);
1780a157612eSMichael Lange   PetscFunctionReturn(0);
1781a157612eSMichael Lange }
17826462276dSToby Isaac 
17836462276dSToby Isaac #undef __FUNCT__
17846462276dSToby Isaac #define __FUNCT__ "DMPlexGetGatherDM"
17856462276dSToby Isaac /*@C
17866462276dSToby Isaac   DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the
17876462276dSToby Isaac   root process of the original's communicator.
17886462276dSToby Isaac 
17896462276dSToby Isaac   Input Parameters:
17906462276dSToby Isaac . dm - the original DMPlex object
17916462276dSToby Isaac 
17926462276dSToby Isaac   Output Parameters:
17936462276dSToby Isaac . gatherMesh - the gathered DM object, or NULL
17946462276dSToby Isaac 
17956462276dSToby Isaac   Level: intermediate
17966462276dSToby Isaac 
17976462276dSToby Isaac .keywords: mesh
17986462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM()
17996462276dSToby Isaac @*/
18006462276dSToby Isaac PetscErrorCode DMPlexGetGatherDM(DM dm, DM * gatherMesh)
18016462276dSToby Isaac {
18026462276dSToby Isaac   MPI_Comm       comm;
18036462276dSToby Isaac   PetscMPIInt    size;
18046462276dSToby Isaac   PetscPartitioner oldPart, gatherPart;
18056462276dSToby Isaac   PetscErrorCode ierr;
18066462276dSToby Isaac 
18076462276dSToby Isaac   PetscFunctionBegin;
18086462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18096462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18106462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18116462276dSToby Isaac   *gatherMesh = NULL;
18126462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18136462276dSToby Isaac   ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr);
18146462276dSToby Isaac   ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr);
18156462276dSToby Isaac   ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr);
18166462276dSToby Isaac   ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr);
18176462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr);
18186462276dSToby Isaac   ierr = DMPlexDistribute(dm,0,NULL,gatherMesh);CHKERRQ(ierr);
18196462276dSToby Isaac   ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr);
18206462276dSToby Isaac   ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr);
18216462276dSToby Isaac   ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr);
18226462276dSToby Isaac   PetscFunctionReturn(0);
18236462276dSToby Isaac }
18246462276dSToby Isaac 
18256462276dSToby Isaac #undef __FUNCT__
18266462276dSToby Isaac #define __FUNCT__ "DMPlexGetRedundantDM"
18276462276dSToby Isaac /*@C
18286462276dSToby Isaac   DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process.
18296462276dSToby Isaac 
18306462276dSToby Isaac   Input Parameters:
18316462276dSToby Isaac . dm - the original DMPlex object
18326462276dSToby Isaac 
18336462276dSToby Isaac   Output Parameters:
18346462276dSToby Isaac . redundantMesh - the redundant DM object, or NULL
18356462276dSToby Isaac 
18366462276dSToby Isaac   Level: intermediate
18376462276dSToby Isaac 
18386462276dSToby Isaac .keywords: mesh
18396462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM()
18406462276dSToby Isaac @*/
18416462276dSToby Isaac PetscErrorCode DMPlexGetRedundantDM(DM dm, DM * redundantMesh)
18426462276dSToby Isaac {
18436462276dSToby Isaac   MPI_Comm       comm;
18446462276dSToby Isaac   PetscMPIInt    size, rank;
18456462276dSToby Isaac   PetscInt       pStart, pEnd, p;
18466462276dSToby Isaac   PetscInt       numPoints = -1;
18476462276dSToby Isaac   PetscSF        migrationSF, sfPoint;
18486462276dSToby Isaac   DM             gatherDM, dmCoord;
18496462276dSToby Isaac   PetscSFNode    *points;
18506462276dSToby Isaac   PetscErrorCode ierr;
18516462276dSToby Isaac 
18526462276dSToby Isaac   PetscFunctionBegin;
18536462276dSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18546462276dSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
18556462276dSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18566462276dSToby Isaac   *redundantMesh = NULL;
18576462276dSToby Isaac   if (size == 1) PetscFunctionReturn(0);
18586462276dSToby Isaac   ierr = DMPlexGetGatherDM(dm,&gatherDM);CHKERRQ(ierr);
18596462276dSToby Isaac   if (!gatherDM) PetscFunctionReturn(0);
18606462276dSToby Isaac   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
18616462276dSToby Isaac   ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr);
18626462276dSToby Isaac   numPoints = pEnd - pStart;
18636462276dSToby Isaac   ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRQ(ierr);
18646462276dSToby Isaac   ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr);
18656462276dSToby Isaac   ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr);
18666462276dSToby Isaac   for (p = 0; p < numPoints; p++) {
18676462276dSToby Isaac     points[p].index = p;
18686462276dSToby Isaac     points[p].rank  = 0;
18696462276dSToby Isaac   }
18706462276dSToby Isaac   ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr);
18716462276dSToby Isaac   ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr);
18726462276dSToby Isaac   ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr);
18736462276dSToby Isaac   ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr);
18746462276dSToby Isaac   ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr);
18756462276dSToby Isaac   ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr);
18766462276dSToby Isaac   ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr);
18776462276dSToby Isaac   if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);}
18786462276dSToby Isaac   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
18796462276dSToby Isaac   ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr);
18806462276dSToby Isaac   ierr = DMDestroy(&gatherDM);CHKERRQ(ierr);
18816462276dSToby Isaac   PetscFunctionReturn(0);
18826462276dSToby Isaac }
1883