1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I "petscdmlabel.h" I*/ 370034214SMatthew G. Knepley 43c1f0c11SLawrence Mitchell /*@C 53c1f0c11SLawrence Mitchell DMPlexSetAdjacencyUser - Define adjacency in the mesh using a user-provided callback 63c1f0c11SLawrence Mitchell 73c1f0c11SLawrence Mitchell Input Parameters: 83c1f0c11SLawrence Mitchell + dm - The DM object 93c1f0c11SLawrence Mitchell . user - The user callback, may be NULL (to clear the callback) 103c1f0c11SLawrence Mitchell - ctx - context for callback evaluation, may be NULL 113c1f0c11SLawrence Mitchell 123c1f0c11SLawrence Mitchell Level: advanced 133c1f0c11SLawrence Mitchell 143c1f0c11SLawrence Mitchell Notes: 153c1f0c11SLawrence Mitchell The caller of DMPlexGetAdjacency may need to arrange that a large enough array is available for the adjacency. 163c1f0c11SLawrence Mitchell 173c1f0c11SLawrence Mitchell Any setting here overrides other configuration of DMPlex adjacency determination. 183c1f0c11SLawrence Mitchell 19db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexGetAdjacency()`, `DMPlexGetAdjacencyUser()` 203c1f0c11SLawrence Mitchell @*/ 21d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetAdjacencyUser(DM dm, PetscErrorCode (*user)(DM, PetscInt, PetscInt *, PetscInt[], void *), void *ctx) 22d71ae5a4SJacob Faibussowitsch { 233c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 243c1f0c11SLawrence Mitchell 253c1f0c11SLawrence Mitchell PetscFunctionBegin; 263c1f0c11SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 273c1f0c11SLawrence Mitchell mesh->useradjacency = user; 283c1f0c11SLawrence Mitchell mesh->useradjacencyctx = ctx; 293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 303c1f0c11SLawrence Mitchell } 313c1f0c11SLawrence Mitchell 323c1f0c11SLawrence Mitchell /*@C 333c1f0c11SLawrence Mitchell DMPlexGetAdjacencyUser - get the user-defined adjacency callback 343c1f0c11SLawrence Mitchell 353c1f0c11SLawrence Mitchell Input Parameter: 363c1f0c11SLawrence Mitchell . dm - The DM object 373c1f0c11SLawrence Mitchell 383c1f0c11SLawrence Mitchell Output Parameters: 39ef1023bdSBarry Smith + user - The callback 403c1f0c11SLawrence Mitchell - ctx - context for callback evaluation 413c1f0c11SLawrence Mitchell 423c1f0c11SLawrence Mitchell Level: advanced 433c1f0c11SLawrence Mitchell 44db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexGetAdjacency()`, `DMPlexSetAdjacencyUser()` 453c1f0c11SLawrence Mitchell @*/ 46d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetAdjacencyUser(DM dm, PetscErrorCode (**user)(DM, PetscInt, PetscInt *, PetscInt[], void *), void **ctx) 47d71ae5a4SJacob Faibussowitsch { 483c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 493c1f0c11SLawrence Mitchell 503c1f0c11SLawrence Mitchell PetscFunctionBegin; 513c1f0c11SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 523c1f0c11SLawrence Mitchell if (user) *user = mesh->useradjacency; 533c1f0c11SLawrence Mitchell if (ctx) *ctx = mesh->useradjacencyctx; 543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 553c1f0c11SLawrence Mitchell } 563c1f0c11SLawrence Mitchell 5770034214SMatthew G. Knepley /*@ 58a17985deSToby Isaac DMPlexSetAdjacencyUseAnchors - Define adjacency in the mesh using the point-to-point constraints. 598b0b4c70SToby Isaac 608b0b4c70SToby Isaac Input Parameters: 618b0b4c70SToby Isaac + dm - The DM object 625b317d89SToby 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. 638b0b4c70SToby Isaac 648b0b4c70SToby Isaac Level: intermediate 658b0b4c70SToby Isaac 66db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexSetAnchors()` 678b0b4c70SToby Isaac @*/ 68d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors) 69d71ae5a4SJacob Faibussowitsch { 708b0b4c70SToby Isaac DM_Plex *mesh = (DM_Plex *)dm->data; 718b0b4c70SToby Isaac 728b0b4c70SToby Isaac PetscFunctionBegin; 738b0b4c70SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 745b317d89SToby Isaac mesh->useAnchors = useAnchors; 753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 768b0b4c70SToby Isaac } 778b0b4c70SToby Isaac 788b0b4c70SToby Isaac /*@ 79a17985deSToby Isaac DMPlexGetAdjacencyUseAnchors - Query whether adjacency in the mesh uses the point-to-point constraints. 808b0b4c70SToby Isaac 818b0b4c70SToby Isaac Input Parameter: 828b0b4c70SToby Isaac . dm - The DM object 838b0b4c70SToby Isaac 848b0b4c70SToby Isaac Output Parameter: 855b317d89SToby 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. 868b0b4c70SToby Isaac 878b0b4c70SToby Isaac Level: intermediate 888b0b4c70SToby Isaac 89db781477SPatrick Sanan .seealso: `DMPlexSetAdjacencyUseAnchors()`, `DMSetAdjacency()`, `DMGetAdjacency()`, `DMPlexDistribute()`, `DMPlexPreallocateOperator()`, `DMPlexSetAnchors()` 908b0b4c70SToby Isaac @*/ 91d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors) 92d71ae5a4SJacob Faibussowitsch { 938b0b4c70SToby Isaac DM_Plex *mesh = (DM_Plex *)dm->data; 948b0b4c70SToby Isaac 958b0b4c70SToby Isaac PetscFunctionBegin; 968b0b4c70SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 97064a246eSJacob Faibussowitsch PetscValidBoolPointer(useAnchors, 2); 985b317d89SToby Isaac *useAnchors = mesh->useAnchors; 993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1008b0b4c70SToby Isaac } 1018b0b4c70SToby Isaac 102d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) 103d71ae5a4SJacob Faibussowitsch { 10470034214SMatthew G. Knepley const PetscInt *cone = NULL; 10570034214SMatthew G. Knepley PetscInt numAdj = 0, maxAdjSize = *adjSize, coneSize, c; 10670034214SMatthew G. Knepley 10770034214SMatthew G. Knepley PetscFunctionBeginHot; 1089566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, p, &coneSize)); 1099566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, p, &cone)); 1104b6b44bdSMatthew G. Knepley for (c = 0; c <= coneSize; ++c) { 1114b6b44bdSMatthew G. Knepley const PetscInt point = !c ? p : cone[c - 1]; 11270034214SMatthew G. Knepley const PetscInt *support = NULL; 11370034214SMatthew G. Knepley PetscInt supportSize, s, q; 11470034214SMatthew G. Knepley 1159566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 1169566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 11770034214SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 118527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]), 0); ++q) { 11970034214SMatthew G. Knepley if (support[s] == adj[q]) break; 12070034214SMatthew G. Knepley } 12163a3b9bcSJacob Faibussowitsch PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize); 12270034214SMatthew G. Knepley } 12370034214SMatthew G. Knepley } 12470034214SMatthew G. Knepley *adjSize = numAdj; 1253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12670034214SMatthew G. Knepley } 12770034214SMatthew G. Knepley 128d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) 129d71ae5a4SJacob Faibussowitsch { 13070034214SMatthew G. Knepley const PetscInt *support = NULL; 13170034214SMatthew G. Knepley PetscInt numAdj = 0, maxAdjSize = *adjSize, supportSize, s; 13270034214SMatthew G. Knepley 13370034214SMatthew G. Knepley PetscFunctionBeginHot; 1349566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, p, &supportSize)); 1359566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, p, &support)); 1364b6b44bdSMatthew G. Knepley for (s = 0; s <= supportSize; ++s) { 1374b6b44bdSMatthew G. Knepley const PetscInt point = !s ? p : support[s - 1]; 13870034214SMatthew G. Knepley const PetscInt *cone = NULL; 13970034214SMatthew G. Knepley PetscInt coneSize, c, q; 14070034214SMatthew G. Knepley 1419566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, point, &coneSize)); 1429566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, point, &cone)); 14370034214SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 144527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]), 0); ++q) { 14570034214SMatthew G. Knepley if (cone[c] == adj[q]) break; 14670034214SMatthew G. Knepley } 14763a3b9bcSJacob Faibussowitsch PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize); 14870034214SMatthew G. Knepley } 14970034214SMatthew G. Knepley } 15070034214SMatthew G. Knepley *adjSize = numAdj; 1513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15270034214SMatthew G. Knepley } 15370034214SMatthew G. Knepley 154d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[]) 155d71ae5a4SJacob Faibussowitsch { 15670034214SMatthew G. Knepley PetscInt *star = NULL; 15770034214SMatthew G. Knepley PetscInt numAdj = 0, maxAdjSize = *adjSize, starSize, s; 15870034214SMatthew G. Knepley 15970034214SMatthew G. Knepley PetscFunctionBeginHot; 1609566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star)); 16170034214SMatthew G. Knepley for (s = 0; s < starSize * 2; s += 2) { 16270034214SMatthew G. Knepley const PetscInt *closure = NULL; 16370034214SMatthew G. Knepley PetscInt closureSize, c, q; 16470034214SMatthew G. Knepley 1659566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt **)&closure)); 16670034214SMatthew G. Knepley for (c = 0; c < closureSize * 2; c += 2) { 167527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]), 0); ++q) { 16870034214SMatthew G. Knepley if (closure[c] == adj[q]) break; 16970034214SMatthew G. Knepley } 17063a3b9bcSJacob Faibussowitsch PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize); 17170034214SMatthew G. Knepley } 1729566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt **)&closure)); 17370034214SMatthew G. Knepley } 1749566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star)); 17570034214SMatthew G. Knepley *adjSize = numAdj; 1763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 17770034214SMatthew G. Knepley } 17870034214SMatthew G. Knepley 179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[]) 180d71ae5a4SJacob Faibussowitsch { 18179bad331SMatthew G. Knepley static PetscInt asiz = 0; 1828b0b4c70SToby Isaac PetscInt maxAnchors = 1; 1838b0b4c70SToby Isaac PetscInt aStart = -1, aEnd = -1; 1848b0b4c70SToby Isaac PetscInt maxAdjSize; 1858b0b4c70SToby Isaac PetscSection aSec = NULL; 1868b0b4c70SToby Isaac IS aIS = NULL; 1878b0b4c70SToby Isaac const PetscInt *anchors; 1883c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 18970034214SMatthew G. Knepley 19070034214SMatthew G. Knepley PetscFunctionBeginHot; 1915b317d89SToby Isaac if (useAnchors) { 1929566063dSJacob Faibussowitsch PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS)); 1938b0b4c70SToby Isaac if (aSec) { 1949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetMaxDof(aSec, &maxAnchors)); 19524c766afSToby Isaac maxAnchors = PetscMax(1, maxAnchors); 1969566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(aSec, &aStart, &aEnd)); 1979566063dSJacob Faibussowitsch PetscCall(ISGetIndices(aIS, &anchors)); 1988b0b4c70SToby Isaac } 1998b0b4c70SToby Isaac } 20079bad331SMatthew G. Knepley if (!*adj) { 20124c766afSToby Isaac PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd; 20279bad331SMatthew G. Knepley 2039566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 2049566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 205412e9a14SMatthew G. Knepley depth = PetscMax(depth, -depth); 2069566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxC, &maxS)); 20724c766afSToby Isaac coneSeries = (maxC > 1) ? ((PetscPowInt(maxC, depth + 1) - 1) / (maxC - 1)) : depth + 1; 20824c766afSToby Isaac supportSeries = (maxS > 1) ? ((PetscPowInt(maxS, depth + 1) - 1) / (maxS - 1)) : depth + 1; 20924c766afSToby Isaac asiz = PetscMax(PetscPowInt(maxS, depth) * coneSeries, PetscPowInt(maxC, depth) * supportSeries); 2108b0b4c70SToby Isaac asiz *= maxAnchors; 21124c766afSToby Isaac asiz = PetscMin(asiz, pEnd - pStart); 2129566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(asiz, adj)); 21379bad331SMatthew G. Knepley } 21479bad331SMatthew G. Knepley if (*adjSize < 0) *adjSize = asiz; 2158b0b4c70SToby Isaac maxAdjSize = *adjSize; 2163c1f0c11SLawrence Mitchell if (mesh->useradjacency) { 2179566063dSJacob Faibussowitsch PetscCall((*mesh->useradjacency)(dm, p, adjSize, *adj, mesh->useradjacencyctx)); 2183c1f0c11SLawrence Mitchell } else if (useTransitiveClosure) { 2199566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj)); 22070034214SMatthew G. Knepley } else if (useCone) { 2219566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj)); 22270034214SMatthew G. Knepley } else { 2239566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj)); 22470034214SMatthew G. Knepley } 2255b317d89SToby Isaac if (useAnchors && aSec) { 2268b0b4c70SToby Isaac PetscInt origSize = *adjSize; 2278b0b4c70SToby Isaac PetscInt numAdj = origSize; 2288b0b4c70SToby Isaac PetscInt i = 0, j; 2298b0b4c70SToby Isaac PetscInt *orig = *adj; 2308b0b4c70SToby Isaac 2318b0b4c70SToby Isaac while (i < origSize) { 2328b0b4c70SToby Isaac PetscInt p = orig[i]; 2338b0b4c70SToby Isaac PetscInt aDof = 0; 2348b0b4c70SToby Isaac 23548a46eb9SPierre Jolivet if (p >= aStart && p < aEnd) PetscCall(PetscSectionGetDof(aSec, p, &aDof)); 2368b0b4c70SToby Isaac if (aDof) { 2378b0b4c70SToby Isaac PetscInt aOff; 2388b0b4c70SToby Isaac PetscInt s, q; 2398b0b4c70SToby Isaac 240ad540459SPierre Jolivet for (j = i + 1; j < numAdj; j++) orig[j - 1] = orig[j]; 2418b0b4c70SToby Isaac origSize--; 2428b0b4c70SToby Isaac numAdj--; 2439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(aSec, p, &aOff)); 2448b0b4c70SToby Isaac for (s = 0; s < aDof; ++s) { 245527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff + s]), 0); ++q) { 2468b0b4c70SToby Isaac if (anchors[aOff + s] == orig[q]) break; 2478b0b4c70SToby Isaac } 24863a3b9bcSJacob Faibussowitsch PetscCheck(numAdj <= maxAdjSize, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize); 2498b0b4c70SToby Isaac } 2509371c9d4SSatish Balay } else { 2518b0b4c70SToby Isaac i++; 2528b0b4c70SToby Isaac } 2538b0b4c70SToby Isaac } 2548b0b4c70SToby Isaac *adjSize = numAdj; 2559566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(aIS, &anchors)); 2568b0b4c70SToby Isaac } 2573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 25870034214SMatthew G. Knepley } 25970034214SMatthew G. Knepley 26070034214SMatthew G. Knepley /*@ 26170034214SMatthew G. Knepley DMPlexGetAdjacency - Return all points adjacent to the given point 26270034214SMatthew G. Knepley 26370034214SMatthew G. Knepley Input Parameters: 26470034214SMatthew G. Knepley + dm - The DM object 2656b867d5aSJose E. Roman - p - The point 26670034214SMatthew G. Knepley 2676b867d5aSJose E. Roman Input/Output Parameters: 2686b867d5aSJose E. Roman + adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE; 2696b867d5aSJose E. Roman on output the number of adjacent points 2706b867d5aSJose E. Roman - adj - Either NULL so that the array is allocated, or an existing array with size adjSize; 2716b867d5aSJose E. Roman on output contains the adjacent points 27270034214SMatthew G. Knepley 27370034214SMatthew G. Knepley Level: advanced 27470034214SMatthew G. Knepley 27595452b02SPatrick Sanan Notes: 27695452b02SPatrick Sanan The user must PetscFree the adj array if it was not passed in. 27770034214SMatthew G. Knepley 278db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMCreateMatrix()`, `DMPlexPreallocateOperator()` 27970034214SMatthew G. Knepley @*/ 280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[]) 281d71ae5a4SJacob Faibussowitsch { 2821cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 28370034214SMatthew G. Knepley 28470034214SMatthew G. Knepley PetscFunctionBeginHot; 28570034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 286dadcf809SJacob Faibussowitsch PetscValidIntPointer(adjSize, 3); 28770034214SMatthew G. Knepley PetscValidPointer(adj, 4); 2889566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 2899566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 2909566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj)); 2913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29270034214SMatthew G. Knepley } 29308633170SToby Isaac 294b0a623aaSMatthew G. Knepley /*@ 295b0a623aaSMatthew G. Knepley DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity 296b0a623aaSMatthew G. Knepley 297d083f849SBarry Smith Collective on dm 298b0a623aaSMatthew G. Knepley 299b0a623aaSMatthew G. Knepley Input Parameters: 300b0a623aaSMatthew G. Knepley + dm - The DM 3016b867d5aSJose E. Roman . sfPoint - The PetscSF which encodes point connectivity 3026b867d5aSJose E. Roman . rootRankSection - 3036b867d5aSJose E. Roman . rootRanks - 3046b867d5aSJose E. Roman . leftRankSection - 3056b867d5aSJose E. Roman - leafRanks - 306b0a623aaSMatthew G. Knepley 307b0a623aaSMatthew G. Knepley Output Parameters: 308b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL 309b0a623aaSMatthew G. Knepley - sfProcess - An SF encoding the two-sided process connectivity, or NULL 310b0a623aaSMatthew G. Knepley 311b0a623aaSMatthew G. Knepley Level: developer 312b0a623aaSMatthew G. Knepley 313db781477SPatrick Sanan .seealso: `PetscSFCreate()`, `DMPlexCreateProcessSF()` 314b0a623aaSMatthew G. Knepley @*/ 315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess) 316d71ae5a4SJacob Faibussowitsch { 317b0a623aaSMatthew G. Knepley const PetscSFNode *remotePoints; 318b0a623aaSMatthew G. Knepley PetscInt *localPointsNew; 319b0a623aaSMatthew G. Knepley PetscSFNode *remotePointsNew; 320b0a623aaSMatthew G. Knepley const PetscInt *nranks; 321b0a623aaSMatthew G. Knepley PetscInt *ranksNew; 322b0a623aaSMatthew G. Knepley PetscBT neighbors; 323b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, numLeaves, l, numNeighbors, n; 3249852e123SBarry Smith PetscMPIInt size, proc, rank; 325b0a623aaSMatthew G. Knepley 326b0a623aaSMatthew G. Knepley PetscFunctionBegin; 327b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 328b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2); 329ad540459SPierre Jolivet if (processRanks) PetscValidPointer(processRanks, 7); 330ad540459SPierre Jolivet if (sfProcess) PetscValidPointer(sfProcess, 8); 3319566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size)); 3329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 3339566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints)); 3349566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(size, &neighbors)); 3359566063dSJacob Faibussowitsch PetscCall(PetscBTMemzero(size, neighbors)); 336b0a623aaSMatthew G. Knepley /* Compute root-to-leaf process connectivity */ 3379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(rootRankSection, &pStart, &pEnd)); 3389566063dSJacob Faibussowitsch PetscCall(ISGetIndices(rootRanks, &nranks)); 339b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 340b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 341b0a623aaSMatthew G. Knepley 3429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(rootRankSection, p, &ndof)); 3439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(rootRankSection, p, &noff)); 3449566063dSJacob Faibussowitsch for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff + n])); 345b0a623aaSMatthew G. Knepley } 3469566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(rootRanks, &nranks)); 347b0a623aaSMatthew G. Knepley /* Compute leaf-to-neighbor process connectivity */ 3489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(leafRankSection, &pStart, &pEnd)); 3499566063dSJacob Faibussowitsch PetscCall(ISGetIndices(leafRanks, &nranks)); 350b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 351b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 352b0a623aaSMatthew G. Knepley 3539566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(leafRankSection, p, &ndof)); 3549566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(leafRankSection, p, &noff)); 3559566063dSJacob Faibussowitsch for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff + n])); 356b0a623aaSMatthew G. Knepley } 3579566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(leafRanks, &nranks)); 358b0a623aaSMatthew G. Knepley /* Compute leaf-to-root process connectivity */ 3593ba16761SJacob Faibussowitsch for (l = 0; l < numLeaves; ++l) PetscCall(PetscBTSet(neighbors, remotePoints[l].rank)); 360b0a623aaSMatthew G. Knepley /* Calculate edges */ 3613ba16761SJacob Faibussowitsch PetscCall(PetscBTClear(neighbors, rank)); 3629371c9d4SSatish Balay for (proc = 0, numNeighbors = 0; proc < size; ++proc) { 3639371c9d4SSatish Balay if (PetscBTLookup(neighbors, proc)) ++numNeighbors; 3649371c9d4SSatish Balay } 3659566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &ranksNew)); 3669566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &localPointsNew)); 3679566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &remotePointsNew)); 3689852e123SBarry Smith for (proc = 0, n = 0; proc < size; ++proc) { 369b0a623aaSMatthew G. Knepley if (PetscBTLookup(neighbors, proc)) { 370b0a623aaSMatthew G. Knepley ranksNew[n] = proc; 371b0a623aaSMatthew G. Knepley localPointsNew[n] = proc; 372b0a623aaSMatthew G. Knepley remotePointsNew[n].index = rank; 373b0a623aaSMatthew G. Knepley remotePointsNew[n].rank = proc; 374b0a623aaSMatthew G. Knepley ++n; 375b0a623aaSMatthew G. Knepley } 376b0a623aaSMatthew G. Knepley } 3779566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&neighbors)); 3789566063dSJacob Faibussowitsch if (processRanks) PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks)); 3799566063dSJacob Faibussowitsch else PetscCall(PetscFree(ranksNew)); 380b0a623aaSMatthew G. Knepley if (sfProcess) { 3819566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess)); 3829566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*sfProcess, "Two-Sided Process SF")); 3839566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*sfProcess)); 3849566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER)); 385b0a623aaSMatthew G. Knepley } 3863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 387b0a623aaSMatthew G. Knepley } 388b0a623aaSMatthew G. Knepley 389b0a623aaSMatthew G. Knepley /*@ 390b0a623aaSMatthew G. Knepley DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF. 391b0a623aaSMatthew G. Knepley 392d083f849SBarry Smith Collective on dm 393b0a623aaSMatthew G. Knepley 394b0a623aaSMatthew G. Knepley Input Parameter: 395b0a623aaSMatthew G. Knepley . dm - The DM 396b0a623aaSMatthew G. Knepley 397b0a623aaSMatthew G. Knepley Output Parameters: 398b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point 399b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 400b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 401b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 402b0a623aaSMatthew G. Knepley 403b0a623aaSMatthew G. Knepley Level: developer 404b0a623aaSMatthew G. Knepley 405db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 406b0a623aaSMatthew G. Knepley @*/ 407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank) 408d71ae5a4SJacob Faibussowitsch { 409b0a623aaSMatthew G. Knepley MPI_Comm comm; 410b0a623aaSMatthew G. Knepley PetscSF sfPoint; 411b0a623aaSMatthew G. Knepley const PetscInt *rootdegree; 412b0a623aaSMatthew G. Knepley PetscInt *myrank, *remoterank; 413b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, nedges; 414b0a623aaSMatthew G. Knepley PetscMPIInt rank; 415b0a623aaSMatthew G. Knepley 416b0a623aaSMatthew G. Knepley PetscFunctionBegin; 4179566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 4189566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 4199566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 4209566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 421b0a623aaSMatthew G. Knepley /* Compute number of leaves for each root */ 4229566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)rootSection, "Root Section")); 4239566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(rootSection, pStart, pEnd)); 4249566063dSJacob Faibussowitsch PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree)); 4259566063dSJacob Faibussowitsch PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree)); 4269566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(PetscSectionSetDof(rootSection, p, rootdegree[p - pStart])); 4279566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(rootSection)); 428b0a623aaSMatthew G. Knepley /* Gather rank of each leaf to root */ 4299566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(rootSection, &nedges)); 4309566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(pEnd - pStart, &myrank)); 4319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nedges, &remoterank)); 432b0a623aaSMatthew G. Knepley for (p = 0; p < pEnd - pStart; ++p) myrank[p] = rank; 4339566063dSJacob Faibussowitsch PetscCall(PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank)); 4349566063dSJacob Faibussowitsch PetscCall(PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank)); 4359566063dSJacob Faibussowitsch PetscCall(PetscFree(myrank)); 4369566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank)); 437b0a623aaSMatthew G. Knepley /* Distribute remote ranks to leaves */ 4389566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)leafSection, "Leaf Section")); 4399566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank)); 4403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 441b0a623aaSMatthew G. Knepley } 442b0a623aaSMatthew G. Knepley 443c506a872SMatthew G. Knepley #if 0 444c506a872SMatthew G. Knepley static PetscErrorCode DMPlexCopyOverlapLabels(DM dm, DM ndm) 445c506a872SMatthew G. Knepley { 446c506a872SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 447c506a872SMatthew G. Knepley DM_Plex *nmesh = (DM_Plex *) ndm->data; 448c506a872SMatthew G. Knepley 449c506a872SMatthew G. Knepley PetscFunctionBegin; 450c506a872SMatthew G. Knepley if (mesh->numOvLabels) { 451c506a872SMatthew G. Knepley const char *name; 452c506a872SMatthew G. Knepley PetscInt l; 453c506a872SMatthew G. Knepley 454c506a872SMatthew G. Knepley nmesh->numOvLabels = mesh->numOvLabels; 455c506a872SMatthew G. Knepley for (l = 0; l < mesh->numOvLabels; ++l) { 456c506a872SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject) mesh->ovLabels[l], &name)); 457c506a872SMatthew G. Knepley PetscCall(DMGetLabel(ndm, name, &nmesh->ovLabels[l])); 458c506a872SMatthew G. Knepley nmesh->ovValues[l] = mesh->ovValues[l]; 459c506a872SMatthew G. Knepley } 460c506a872SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject) mesh->ovExLabel, &name)); 461c506a872SMatthew G. Knepley PetscCall(DMGetLabel(ndm, name, &nmesh->ovExLabel)); 462c506a872SMatthew G. Knepley nmesh->ovExValue = mesh->ovExValue; 463c506a872SMatthew G. Knepley } 4643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 465c506a872SMatthew G. Knepley } 466c506a872SMatthew G. Knepley #endif 467c506a872SMatthew G. Knepley 468278397a0SMatthew G. Knepley /*@C 469c506a872SMatthew G. Knepley DMPlexCreateOverlapLabel - Compute a label indicating what overlap points should be sent to new processes 470b0a623aaSMatthew G. Knepley 471d083f849SBarry Smith Collective on dm 472b0a623aaSMatthew G. Knepley 473b0a623aaSMatthew G. Knepley Input Parameters: 474b0a623aaSMatthew G. Knepley + dm - The DM 47524d039d7SMichael Lange . levels - Number of overlap levels 476b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point 477b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 478b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 479b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 480b0a623aaSMatthew G. Knepley 481064ec1f3Sprj- Output Parameter: 482b4ec6ac8SBarry Smith . ovLabel - DMLabel containing remote overlap contributions as point/rank pairings 483b0a623aaSMatthew G. Knepley 484b0a623aaSMatthew G. Knepley Level: developer 485b0a623aaSMatthew G. Knepley 486c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabelFromLabels()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()` 487b0a623aaSMatthew G. Knepley @*/ 488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateOverlapLabel(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) 489d71ae5a4SJacob Faibussowitsch { 490e540f424SMichael Lange MPI_Comm comm; 491b0a623aaSMatthew G. Knepley DMLabel ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */ 492874ddda9SLisandro Dalcin PetscSF sfPoint; 493b0a623aaSMatthew G. Knepley const PetscSFNode *remote; 494b0a623aaSMatthew G. Knepley const PetscInt *local; 4951fd9873aSMichael Lange const PetscInt *nrank, *rrank; 496b0a623aaSMatthew G. Knepley PetscInt *adj = NULL; 4971fd9873aSMichael Lange PetscInt pStart, pEnd, p, sStart, sEnd, nleaves, l; 4989852e123SBarry Smith PetscMPIInt rank, size; 49931bc6364SLisandro Dalcin PetscBool flg; 500b0a623aaSMatthew G. Knepley 501b0a623aaSMatthew G. Knepley PetscFunctionBegin; 5026ba1a4c7SVaclav Hapla *ovLabel = NULL; 5039566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 5049566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 5059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 5063ba16761SJacob Faibussowitsch if (size == 1) PetscFunctionReturn(PETSC_SUCCESS); 5079566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 5089566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 509d1674c6dSMatthew Knepley if (!levels) { 510d1674c6dSMatthew Knepley /* Add owned points */ 5119566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 5129566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 5133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 514d1674c6dSMatthew Knepley } 5159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(leafSection, &sStart, &sEnd)); 5169566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote)); 5179566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank)); 518b0a623aaSMatthew G. Knepley /* Handle leaves: shared with the root point */ 519b0a623aaSMatthew G. Knepley for (l = 0; l < nleaves; ++l) { 520b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, a; 521b0a623aaSMatthew G. Knepley 5229566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj)); 5239566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank)); 524b0a623aaSMatthew G. Knepley } 5259566063dSJacob Faibussowitsch PetscCall(ISGetIndices(rootrank, &rrank)); 5269566063dSJacob Faibussowitsch PetscCall(ISGetIndices(leafrank, &nrank)); 527b0a623aaSMatthew G. Knepley /* Handle roots */ 528b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 529b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a; 530b0a623aaSMatthew G. Knepley 531b0a623aaSMatthew G. Knepley if ((p >= sStart) && (p < sEnd)) { 532b0a623aaSMatthew G. Knepley /* Some leaves share a root with other leaves on different processes */ 5339566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(leafSection, p, &neighbors)); 534b0a623aaSMatthew G. Knepley if (neighbors) { 5359566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(leafSection, p, &noff)); 5369566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 537b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 538b0a623aaSMatthew G. Knepley const PetscInt remoteRank = nrank[noff + n]; 539b0a623aaSMatthew G. Knepley 540b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 5419566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 542b0a623aaSMatthew G. Knepley } 543b0a623aaSMatthew G. Knepley } 544b0a623aaSMatthew G. Knepley } 545b0a623aaSMatthew G. Knepley /* Roots are shared with leaves */ 5469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(rootSection, p, &neighbors)); 547b0a623aaSMatthew G. Knepley if (!neighbors) continue; 5489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(rootSection, p, &noff)); 5499566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 550b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 551b0a623aaSMatthew G. Knepley const PetscInt remoteRank = rrank[noff + n]; 552b0a623aaSMatthew G. Knepley 553b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 5549566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 555b0a623aaSMatthew G. Knepley } 556b0a623aaSMatthew G. Knepley } 5579566063dSJacob Faibussowitsch PetscCall(PetscFree(adj)); 5589566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(rootrank, &rrank)); 5599566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(leafrank, &nrank)); 56024d039d7SMichael Lange /* Add additional overlap levels */ 561be200f8dSMichael Lange for (l = 1; l < levels; l++) { 562be200f8dSMichael Lange /* Propagate point donations over SF to capture remote connections */ 5639566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelPropagate(dm, ovAdjByRank)); 564be200f8dSMichael Lange /* Add next level of point donations to the label */ 5659566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelAdjacency(dm, ovAdjByRank)); 566be200f8dSMichael Lange } 56726a7d390SMatthew G. Knepley /* We require the closure in the overlap */ 5689566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank)); 5699566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-overlap_view", &flg)); 570e540f424SMichael Lange if (flg) { 571825f8a23SLisandro Dalcin PetscViewer viewer; 5729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer)); 5739566063dSJacob Faibussowitsch PetscCall(DMLabelView(ovAdjByRank, viewer)); 574b0a623aaSMatthew G. Knepley } 575874ddda9SLisandro Dalcin /* Invert sender to receiver label */ 5769566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 5779566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel)); 578a9f1d5b2SMichael Lange /* Add owned points, except for shared local points */ 5799566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 580e540f424SMichael Lange for (l = 0; l < nleaves; ++l) { 5819566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(*ovLabel, local[l], rank)); 5829566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank)); 583e540f424SMichael Lange } 584e540f424SMichael Lange /* Clean up */ 5859566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&ovAdjByRank)); 5863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 587b0a623aaSMatthew G. Knepley } 58870034214SMatthew G. Knepley 589d71ae5a4SJacob Faibussowitsch static PetscErrorCode HandlePoint_Private(DM dm, PetscInt p, PetscSection section, const PetscInt ranks[], PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], DMLabel ovAdjByRank) 590d71ae5a4SJacob Faibussowitsch { 591c506a872SMatthew G. Knepley PetscInt neighbors, el; 592c506a872SMatthew G. Knepley 593c506a872SMatthew G. Knepley PetscFunctionBegin; 594c506a872SMatthew G. Knepley PetscCall(PetscSectionGetDof(section, p, &neighbors)); 595c506a872SMatthew G. Knepley if (neighbors) { 596c506a872SMatthew G. Knepley PetscInt *adj = NULL; 597c506a872SMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, noff, n, a; 598c506a872SMatthew G. Knepley PetscMPIInt rank; 599c506a872SMatthew G. Knepley 600c506a872SMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 601c506a872SMatthew G. Knepley PetscCall(PetscSectionGetOffset(section, p, &noff)); 602c506a872SMatthew G. Knepley PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 603c506a872SMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 604c506a872SMatthew G. Knepley const PetscInt remoteRank = ranks[noff + n]; 605c506a872SMatthew G. Knepley 606c506a872SMatthew G. Knepley if (remoteRank == rank) continue; 607c506a872SMatthew G. Knepley for (a = 0; a < adjSize; ++a) { 608c506a872SMatthew G. Knepley PetscBool insert = PETSC_TRUE; 609c506a872SMatthew G. Knepley 610c506a872SMatthew G. Knepley for (el = 0; el < numExLabels; ++el) { 611c506a872SMatthew G. Knepley PetscInt exVal; 612c506a872SMatthew G. Knepley PetscCall(DMLabelGetValue(exLabel[el], adj[a], &exVal)); 6139371c9d4SSatish Balay if (exVal == exValue[el]) { 6149371c9d4SSatish Balay insert = PETSC_FALSE; 6159371c9d4SSatish Balay break; 6169371c9d4SSatish Balay } 617c506a872SMatthew G. Knepley } 618c506a872SMatthew G. Knepley if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 619c506a872SMatthew G. Knepley } 620c506a872SMatthew G. Knepley } 621f88a03deSMatthew G. Knepley PetscCall(PetscFree(adj)); 622c506a872SMatthew G. Knepley } 6233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 624c506a872SMatthew G. Knepley } 625c506a872SMatthew G. Knepley 626c506a872SMatthew G. Knepley /*@C 627c506a872SMatthew G. Knepley DMPlexCreateOverlapLabelFromLabels - Compute a label indicating what overlap points should be sent to new processes 628c506a872SMatthew G. Knepley 629c506a872SMatthew G. Knepley Collective on dm 630c506a872SMatthew G. Knepley 631c506a872SMatthew G. Knepley Input Parameters: 632c506a872SMatthew G. Knepley + dm - The DM 633c506a872SMatthew G. Knepley . numLabels - The number of labels to draw candidate points from 634c506a872SMatthew G. Knepley . label - An array of labels containing candidate points 635c506a872SMatthew G. Knepley . value - An array of label values marking the candidate points 636c506a872SMatthew G. Knepley . numExLabels - The number of labels to use for exclusion 637c506a872SMatthew G. Knepley . exLabel - An array of labels indicating points to be excluded, or NULL 638c506a872SMatthew G. Knepley . exValue - An array of label values to be excluded, or NULL 639c506a872SMatthew G. Knepley . rootSection - The number of leaves for a given root point 640c506a872SMatthew G. Knepley . rootrank - The rank of each edge into the root point 641c506a872SMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 642c506a872SMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 643c506a872SMatthew G. Knepley 644c506a872SMatthew G. Knepley Output Parameter: 645c506a872SMatthew G. Knepley . ovLabel - DMLabel containing remote overlap contributions as point/rank pairings 646c506a872SMatthew G. Knepley 647c506a872SMatthew G. Knepley Note: 648c506a872SMatthew G. Knepley The candidate points are only added to the overlap if they are adjacent to a shared point 649c506a872SMatthew G. Knepley 650c506a872SMatthew G. Knepley Level: developer 651c506a872SMatthew G. Knepley 652c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()` 653c506a872SMatthew G. Knepley @*/ 654d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateOverlapLabelFromLabels(DM dm, PetscInt numLabels, const DMLabel label[], const PetscInt value[], PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) 655d71ae5a4SJacob Faibussowitsch { 656c506a872SMatthew G. Knepley MPI_Comm comm; 657c506a872SMatthew G. Knepley DMLabel ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */ 658c506a872SMatthew G. Knepley PetscSF sfPoint; 659c506a872SMatthew G. Knepley const PetscSFNode *remote; 660c506a872SMatthew G. Knepley const PetscInt *local; 661c506a872SMatthew G. Knepley const PetscInt *nrank, *rrank; 662c506a872SMatthew G. Knepley PetscInt *adj = NULL; 663c506a872SMatthew G. Knepley PetscInt pStart, pEnd, p, sStart, sEnd, nleaves, l, el; 664c506a872SMatthew G. Knepley PetscMPIInt rank, size; 665c506a872SMatthew G. Knepley PetscBool flg; 666c506a872SMatthew G. Knepley 667c506a872SMatthew G. Knepley PetscFunctionBegin; 668c506a872SMatthew G. Knepley *ovLabel = NULL; 669c506a872SMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 670c506a872SMatthew G. Knepley PetscCallMPI(MPI_Comm_size(comm, &size)); 671c506a872SMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(comm, &rank)); 6723ba16761SJacob Faibussowitsch if (size == 1) PetscFunctionReturn(PETSC_SUCCESS); 673c506a872SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sfPoint)); 674c506a872SMatthew G. Knepley PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 675c506a872SMatthew G. Knepley PetscCall(PetscSectionGetChart(leafSection, &sStart, &sEnd)); 676c506a872SMatthew G. Knepley PetscCall(PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote)); 677c506a872SMatthew G. Knepley PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank)); 678c506a872SMatthew G. Knepley PetscCall(ISGetIndices(rootrank, &rrank)); 679c506a872SMatthew G. Knepley PetscCall(ISGetIndices(leafrank, &nrank)); 680c506a872SMatthew G. Knepley for (l = 0; l < numLabels; ++l) { 681c506a872SMatthew G. Knepley IS valIS; 682c506a872SMatthew G. Knepley const PetscInt *points; 683c506a872SMatthew G. Knepley PetscInt n; 684c506a872SMatthew G. Knepley 685c506a872SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label[l], value[l], &valIS)); 686c506a872SMatthew G. Knepley if (!valIS) continue; 687c506a872SMatthew G. Knepley PetscCall(ISGetIndices(valIS, &points)); 688c506a872SMatthew G. Knepley PetscCall(ISGetLocalSize(valIS, &n)); 689c506a872SMatthew G. Knepley for (PetscInt i = 0; i < n; ++i) { 690c506a872SMatthew G. Knepley const PetscInt p = points[i]; 691c506a872SMatthew G. Knepley 692c506a872SMatthew G. Knepley if ((p >= sStart) && (p < sEnd)) { 693c506a872SMatthew G. Knepley PetscInt loc, adjSize = PETSC_DETERMINE; 694c506a872SMatthew G. Knepley 695c506a872SMatthew G. Knepley /* Handle leaves: shared with the root point */ 696c506a872SMatthew G. Knepley if (local) PetscCall(PetscFindInt(p, nleaves, local, &loc)); 697c506a872SMatthew G. Knepley else loc = (p >= 0 && p < nleaves) ? p : -1; 698c506a872SMatthew G. Knepley if (loc >= 0) { 699c506a872SMatthew G. Knepley const PetscInt remoteRank = remote[loc].rank; 700c506a872SMatthew G. Knepley 701c506a872SMatthew G. Knepley PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 702c506a872SMatthew G. Knepley for (PetscInt a = 0; a < adjSize; ++a) { 703c506a872SMatthew G. Knepley PetscBool insert = PETSC_TRUE; 704c506a872SMatthew G. Knepley 705c506a872SMatthew G. Knepley for (el = 0; el < numExLabels; ++el) { 706c506a872SMatthew G. Knepley PetscInt exVal; 707c506a872SMatthew G. Knepley PetscCall(DMLabelGetValue(exLabel[el], adj[a], &exVal)); 7089371c9d4SSatish Balay if (exVal == exValue[el]) { 7099371c9d4SSatish Balay insert = PETSC_FALSE; 7109371c9d4SSatish Balay break; 7119371c9d4SSatish Balay } 712c506a872SMatthew G. Knepley } 713c506a872SMatthew G. Knepley if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 714c506a872SMatthew G. Knepley } 715c506a872SMatthew G. Knepley } 716c506a872SMatthew G. Knepley /* Some leaves share a root with other leaves on different processes */ 7173ba16761SJacob Faibussowitsch PetscCall(HandlePoint_Private(dm, p, leafSection, nrank, numExLabels, exLabel, exValue, ovAdjByRank)); 718c506a872SMatthew G. Knepley } 719c506a872SMatthew G. Knepley /* Roots are shared with leaves */ 7203ba16761SJacob Faibussowitsch PetscCall(HandlePoint_Private(dm, p, rootSection, rrank, numExLabels, exLabel, exValue, ovAdjByRank)); 721c506a872SMatthew G. Knepley } 722c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(valIS, &points)); 723c506a872SMatthew G. Knepley PetscCall(ISDestroy(&valIS)); 724c506a872SMatthew G. Knepley } 725c506a872SMatthew G. Knepley PetscCall(PetscFree(adj)); 726c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(rootrank, &rrank)); 727c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(leafrank, &nrank)); 728c506a872SMatthew G. Knepley /* We require the closure in the overlap */ 729c506a872SMatthew G. Knepley PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank)); 730c506a872SMatthew G. Knepley PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-overlap_view", &flg)); 731c506a872SMatthew G. Knepley if (flg) { 732c506a872SMatthew G. Knepley PetscViewer viewer; 733c506a872SMatthew G. Knepley PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer)); 734c506a872SMatthew G. Knepley PetscCall(DMLabelView(ovAdjByRank, viewer)); 735c506a872SMatthew G. Knepley } 736c506a872SMatthew G. Knepley /* Invert sender to receiver label */ 737c506a872SMatthew G. Knepley PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 738c506a872SMatthew G. Knepley PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel)); 739c506a872SMatthew G. Knepley /* Add owned points, except for shared local points */ 740c506a872SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 741c506a872SMatthew G. Knepley for (l = 0; l < nleaves; ++l) { 742c506a872SMatthew G. Knepley PetscCall(DMLabelClearValue(*ovLabel, local[l], rank)); 743c506a872SMatthew G. Knepley PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank)); 744c506a872SMatthew G. Knepley } 745c506a872SMatthew G. Knepley /* Clean up */ 746c506a872SMatthew G. Knepley PetscCall(DMLabelDestroy(&ovAdjByRank)); 7473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 748c506a872SMatthew G. Knepley } 749c506a872SMatthew G. Knepley 75024cc2ca5SMatthew G. Knepley /*@C 75124cc2ca5SMatthew G. Knepley DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF 75224cc2ca5SMatthew G. Knepley 753d083f849SBarry Smith Collective on dm 75424cc2ca5SMatthew G. Knepley 75524cc2ca5SMatthew G. Knepley Input Parameters: 75624cc2ca5SMatthew G. Knepley + dm - The DM 75724cc2ca5SMatthew G. Knepley - overlapSF - The SF mapping ghost points in overlap to owner points on other processes 75824cc2ca5SMatthew G. Knepley 759064ec1f3Sprj- Output Parameter: 760a2b725a8SWilliam Gropp . migrationSF - An SF that maps original points in old locations to points in new locations 76124cc2ca5SMatthew G. Knepley 76224cc2ca5SMatthew G. Knepley Level: developer 76324cc2ca5SMatthew G. Knepley 764db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistributeOverlap()`, `DMPlexDistribute()` 76524cc2ca5SMatthew G. Knepley @*/ 766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF) 767d71ae5a4SJacob Faibussowitsch { 76846f9b1c3SMichael Lange MPI_Comm comm; 7699852e123SBarry Smith PetscMPIInt rank, size; 77046f9b1c3SMichael Lange PetscInt d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints; 77146f9b1c3SMichael Lange PetscInt *pointDepths, *remoteDepths, *ilocal; 77246f9b1c3SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 77346f9b1c3SMichael Lange PetscSFNode *iremote; 77446f9b1c3SMichael Lange PetscSF pointSF; 77546f9b1c3SMichael Lange const PetscInt *sharedLocal; 77646f9b1c3SMichael Lange const PetscSFNode *overlapRemote, *sharedRemote; 77746f9b1c3SMichael Lange 77846f9b1c3SMichael Lange PetscFunctionBegin; 77946f9b1c3SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 7809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 7819566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 7829566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 7839566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 78446f9b1c3SMichael Lange 78546f9b1c3SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 7869566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote)); 7879566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths)); 78846f9b1c3SMichael Lange for (d = 0; d < dim + 1; d++) { 7899566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 79046f9b1c3SMichael Lange for (p = pStart; p < pEnd; p++) pointDepths[p] = d; 79146f9b1c3SMichael Lange } 79246f9b1c3SMichael Lange for (p = 0; p < nleaves; p++) remoteDepths[p] = -1; 7939566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths, MPI_REPLACE)); 7949566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths, MPI_REPLACE)); 79546f9b1c3SMichael Lange 7962d4ee042Sprj- /* Count received points in each stratum and compute the internal strata shift */ 7979566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(dim + 1, &depthRecv, dim + 1, &depthShift, dim + 1, &depthIdx)); 79846f9b1c3SMichael Lange for (d = 0; d < dim + 1; d++) depthRecv[d] = 0; 79946f9b1c3SMichael Lange for (p = 0; p < nleaves; p++) depthRecv[remoteDepths[p]]++; 80046f9b1c3SMichael Lange depthShift[dim] = 0; 80146f9b1c3SMichael Lange for (d = 0; d < dim; d++) depthShift[d] = depthRecv[dim]; 80246f9b1c3SMichael Lange for (d = 1; d < dim; d++) depthShift[d] += depthRecv[0]; 80346f9b1c3SMichael Lange for (d = dim - 2; d > 0; d--) depthShift[d] += depthRecv[d + 1]; 80446f9b1c3SMichael Lange for (d = 0; d < dim + 1; d++) { 8059566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 80646f9b1c3SMichael Lange depthIdx[d] = pStart + depthShift[d]; 80746f9b1c3SMichael Lange } 80846f9b1c3SMichael Lange 80946f9b1c3SMichael Lange /* Form the overlap SF build an SF that describes the full overlap migration SF */ 8109566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 81146f9b1c3SMichael Lange newLeaves = pEnd - pStart + nleaves; 8129566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(newLeaves, &ilocal)); 8139566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(newLeaves, &iremote)); 81446f9b1c3SMichael Lange /* First map local points to themselves */ 81546f9b1c3SMichael Lange for (d = 0; d < dim + 1; d++) { 8169566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 81746f9b1c3SMichael Lange for (p = pStart; p < pEnd; p++) { 81846f9b1c3SMichael Lange point = p + depthShift[d]; 81946f9b1c3SMichael Lange ilocal[point] = point; 82046f9b1c3SMichael Lange iremote[point].index = p; 82146f9b1c3SMichael Lange iremote[point].rank = rank; 82246f9b1c3SMichael Lange depthIdx[d]++; 82346f9b1c3SMichael Lange } 82446f9b1c3SMichael Lange } 82546f9b1c3SMichael Lange 82646f9b1c3SMichael Lange /* Add in the remote roots for currently shared points */ 8279566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &pointSF)); 8289566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote)); 82946f9b1c3SMichael Lange for (d = 0; d < dim + 1; d++) { 8309566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 83146f9b1c3SMichael Lange for (p = 0; p < numSharedPoints; p++) { 83246f9b1c3SMichael Lange if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) { 83346f9b1c3SMichael Lange point = sharedLocal[p] + depthShift[d]; 83446f9b1c3SMichael Lange iremote[point].index = sharedRemote[p].index; 83546f9b1c3SMichael Lange iremote[point].rank = sharedRemote[p].rank; 83646f9b1c3SMichael Lange } 83746f9b1c3SMichael Lange } 83846f9b1c3SMichael Lange } 83946f9b1c3SMichael Lange 84046f9b1c3SMichael Lange /* Now add the incoming overlap points */ 84146f9b1c3SMichael Lange for (p = 0; p < nleaves; p++) { 84246f9b1c3SMichael Lange point = depthIdx[remoteDepths[p]]; 84346f9b1c3SMichael Lange ilocal[point] = point; 84446f9b1c3SMichael Lange iremote[point].index = overlapRemote[p].index; 84546f9b1c3SMichael Lange iremote[point].rank = overlapRemote[p].rank; 84646f9b1c3SMichael Lange depthIdx[remoteDepths[p]]++; 84746f9b1c3SMichael Lange } 8489566063dSJacob Faibussowitsch PetscCall(PetscFree2(pointDepths, remoteDepths)); 84946f9b1c3SMichael Lange 8509566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, migrationSF)); 8519566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*migrationSF, "Overlap Migration SF")); 8529566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*migrationSF)); 8539566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 8549566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*migrationSF, pEnd - pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER)); 85546f9b1c3SMichael Lange 8569566063dSJacob Faibussowitsch PetscCall(PetscFree3(depthRecv, depthShift, depthIdx)); 8573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 85870034214SMatthew G. Knepley } 85970034214SMatthew G. Knepley 860a9f1d5b2SMichael Lange /*@ 861f0e73a3dSToby Isaac DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification. 862a9f1d5b2SMichael Lange 863064ec1f3Sprj- Input Parameters: 864a9f1d5b2SMichael Lange + dm - The DM 865a9f1d5b2SMichael Lange - sf - A star forest with non-ordered leaves, usually defining a DM point migration 866a9f1d5b2SMichael Lange 867a9f1d5b2SMichael Lange Output Parameter: 868a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified 869a9f1d5b2SMichael Lange 870412e9a14SMatthew G. Knepley Note: 871412e9a14SMatthew G. Knepley This lexicographically sorts by (depth, cellType) 872412e9a14SMatthew G. Knepley 873a9f1d5b2SMichael Lange Level: developer 874a9f1d5b2SMichael Lange 875db781477SPatrick Sanan .seealso: `DMPlexPartitionLabelCreateSF()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 876a9f1d5b2SMichael Lange @*/ 877d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF) 878d71ae5a4SJacob Faibussowitsch { 879a9f1d5b2SMichael Lange MPI_Comm comm; 8809852e123SBarry Smith PetscMPIInt rank, size; 881412e9a14SMatthew G. Knepley PetscInt d, ldepth, depth, dim, p, pStart, pEnd, nroots, nleaves; 882412e9a14SMatthew G. Knepley PetscSFNode *pointDepths, *remoteDepths; 883412e9a14SMatthew G. Knepley PetscInt *ilocal; 884a9f1d5b2SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 885412e9a14SMatthew G. Knepley PetscInt *ctRecv, *ctShift, *ctIdx; 886a9f1d5b2SMichael Lange const PetscSFNode *iremote; 887a9f1d5b2SMichael Lange 888a9f1d5b2SMichael Lange PetscFunctionBegin; 889a9f1d5b2SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8909566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 8919566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 8929566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 8939566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &ldepth)); 8949566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 8951c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm)); 89663a3b9bcSJacob Faibussowitsch PetscCheck(!(ldepth >= 0) || !(depth != ldepth), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, ldepth, depth); 8979566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_PartStratSF, dm, 0, 0, 0)); 898a9f1d5b2SMichael Lange 899a9f1d5b2SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 9009566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote)); 9019566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths)); 9027fab53ddSMatthew G. Knepley for (d = 0; d < depth + 1; ++d) { 9039566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 904f0e73a3dSToby Isaac for (p = pStart; p < pEnd; ++p) { 905412e9a14SMatthew G. Knepley DMPolytopeType ct; 906f0e73a3dSToby Isaac 9079566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, p, &ct)); 908412e9a14SMatthew G. Knepley pointDepths[p].index = d; 909412e9a14SMatthew G. Knepley pointDepths[p].rank = ct; 910f0e73a3dSToby Isaac } 911412e9a14SMatthew G. Knepley } 9129371c9d4SSatish Balay for (p = 0; p < nleaves; ++p) { 9139371c9d4SSatish Balay remoteDepths[p].index = -1; 9149371c9d4SSatish Balay remoteDepths[p].rank = -1; 9159371c9d4SSatish Balay } 9169566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sf, MPIU_2INT, pointDepths, remoteDepths, MPI_REPLACE)); 9179566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_2INT, pointDepths, remoteDepths, MPI_REPLACE)); 918412e9a14SMatthew G. Knepley /* Count received points in each stratum and compute the internal strata shift */ 9199566063dSJacob Faibussowitsch PetscCall(PetscCalloc6(depth + 1, &depthRecv, depth + 1, &depthShift, depth + 1, &depthIdx, DM_NUM_POLYTOPES, &ctRecv, DM_NUM_POLYTOPES, &ctShift, DM_NUM_POLYTOPES, &ctIdx)); 920412e9a14SMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 921412e9a14SMatthew G. Knepley if (remoteDepths[p].rank < 0) { 922412e9a14SMatthew G. Knepley ++depthRecv[remoteDepths[p].index]; 923412e9a14SMatthew G. Knepley } else { 924412e9a14SMatthew G. Knepley ++ctRecv[remoteDepths[p].rank]; 925412e9a14SMatthew G. Knepley } 926412e9a14SMatthew G. Knepley } 927412e9a14SMatthew G. Knepley { 928412e9a14SMatthew G. Knepley PetscInt depths[4], dims[4], shift = 0, i, c; 929412e9a14SMatthew G. Knepley 9308238f61eSMatthew G. Knepley /* Cells (depth), Vertices (0), Faces (depth-1), Edges (1) 9318238f61eSMatthew G. Knepley Consider DM_POLYTOPE_FV_GHOST and DM_POLYTOPE_INTERIOR_GHOST as cells 9328238f61eSMatthew G. Knepley */ 9339371c9d4SSatish Balay depths[0] = depth; 9349371c9d4SSatish Balay depths[1] = 0; 9359371c9d4SSatish Balay depths[2] = depth - 1; 9369371c9d4SSatish Balay depths[3] = 1; 9379371c9d4SSatish Balay dims[0] = dim; 9389371c9d4SSatish Balay dims[1] = 0; 9399371c9d4SSatish Balay dims[2] = dim - 1; 9409371c9d4SSatish Balay dims[3] = 1; 941412e9a14SMatthew G. Knepley for (i = 0; i <= depth; ++i) { 942412e9a14SMatthew G. Knepley const PetscInt dep = depths[i]; 943412e9a14SMatthew G. Knepley const PetscInt dim = dims[i]; 944412e9a14SMatthew G. Knepley 945412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 9468238f61eSMatthew G. Knepley if (DMPolytopeTypeGetDim((DMPolytopeType)c) != dim && !(i == 0 && (c == DM_POLYTOPE_FV_GHOST || c == DM_POLYTOPE_INTERIOR_GHOST))) continue; 947412e9a14SMatthew G. Knepley ctShift[c] = shift; 948412e9a14SMatthew G. Knepley shift += ctRecv[c]; 949412e9a14SMatthew G. Knepley } 950412e9a14SMatthew G. Knepley depthShift[dep] = shift; 951412e9a14SMatthew G. Knepley shift += depthRecv[dep]; 952412e9a14SMatthew G. Knepley } 953412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 954412e9a14SMatthew G. Knepley const PetscInt ctDim = DMPolytopeTypeGetDim((DMPolytopeType)c); 955412e9a14SMatthew G. Knepley 9568238f61eSMatthew G. Knepley if ((ctDim < 0 || ctDim > dim) && (c != DM_POLYTOPE_FV_GHOST && c != DM_POLYTOPE_INTERIOR_GHOST)) { 957412e9a14SMatthew G. Knepley ctShift[c] = shift; 958412e9a14SMatthew G. Knepley shift += ctRecv[c]; 959412e9a14SMatthew G. Knepley } 960412e9a14SMatthew G. Knepley } 961412e9a14SMatthew G. Knepley } 962a9f1d5b2SMichael Lange /* Derive a new local permutation based on stratified indices */ 9639566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &ilocal)); 9647fab53ddSMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 965412e9a14SMatthew G. Knepley const PetscInt dep = remoteDepths[p].index; 966412e9a14SMatthew G. Knepley const DMPolytopeType ct = (DMPolytopeType)remoteDepths[p].rank; 9677fab53ddSMatthew G. Knepley 968412e9a14SMatthew G. Knepley if ((PetscInt)ct < 0) { 9697fab53ddSMatthew G. Knepley ilocal[p] = depthShift[dep] + depthIdx[dep]; 970412e9a14SMatthew G. Knepley ++depthIdx[dep]; 971412e9a14SMatthew G. Knepley } else { 972412e9a14SMatthew G. Knepley ilocal[p] = ctShift[ct] + ctIdx[ct]; 973412e9a14SMatthew G. Knepley ++ctIdx[ct]; 974412e9a14SMatthew G. Knepley } 975a9f1d5b2SMichael Lange } 9769566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, migrationSF)); 9779566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*migrationSF, "Migration SF")); 9789566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, (PetscSFNode *)iremote, PETSC_COPY_VALUES)); 9799566063dSJacob Faibussowitsch PetscCall(PetscFree2(pointDepths, remoteDepths)); 9809566063dSJacob Faibussowitsch PetscCall(PetscFree6(depthRecv, depthShift, depthIdx, ctRecv, ctShift, ctIdx)); 9819566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_PartStratSF, dm, 0, 0, 0)); 9823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 983a9f1d5b2SMichael Lange } 984a9f1d5b2SMichael Lange 98570034214SMatthew G. Knepley /*@ 98670034214SMatthew G. Knepley DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 98770034214SMatthew G. Knepley 988d083f849SBarry Smith Collective on dm 98970034214SMatthew G. Knepley 99070034214SMatthew G. Knepley Input Parameters: 99170034214SMatthew G. Knepley + dm - The DMPlex object 99270034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 99370034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 994cb15cd0eSMatthew G. Knepley - originalVec - The existing data in a local vector 99570034214SMatthew G. Knepley 99670034214SMatthew G. Knepley Output Parameters: 99770034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 998cb15cd0eSMatthew G. Knepley - newVec - The new data in a local vector 99970034214SMatthew G. Knepley 100070034214SMatthew G. Knepley Level: developer 100170034214SMatthew G. Knepley 1002db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeFieldIS()`, `DMPlexDistributeData()` 100370034214SMatthew G. Knepley @*/ 1004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec) 1005d71ae5a4SJacob Faibussowitsch { 100670034214SMatthew G. Knepley PetscSF fieldSF; 100770034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 100870034214SMatthew G. Knepley PetscScalar *originalValues, *newValues; 100970034214SMatthew G. Knepley 101070034214SMatthew G. Knepley PetscFunctionBegin; 10119566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeField, dm, 0, 0, 0)); 10129566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 101370034214SMatthew G. Knepley 10149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 10159566063dSJacob Faibussowitsch PetscCall(VecSetSizes(newVec, fieldSize, PETSC_DETERMINE)); 10169566063dSJacob Faibussowitsch PetscCall(VecSetType(newVec, dm->vectype)); 101770034214SMatthew G. Knepley 10189566063dSJacob Faibussowitsch PetscCall(VecGetArray(originalVec, &originalValues)); 10199566063dSJacob Faibussowitsch PetscCall(VecGetArray(newVec, &newValues)); 10209566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 10219566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 10229566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues, MPI_REPLACE)); 10239566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues, MPI_REPLACE)); 10249566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 10259566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(newVec, &newValues)); 10269566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(originalVec, &originalValues)); 10279566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeField, dm, 0, 0, 0)); 10283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 102970034214SMatthew G. Knepley } 103070034214SMatthew G. Knepley 103170034214SMatthew G. Knepley /*@ 103270034214SMatthew G. Knepley DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 103370034214SMatthew G. Knepley 1034d083f849SBarry Smith Collective on dm 103570034214SMatthew G. Knepley 103670034214SMatthew G. Knepley Input Parameters: 103770034214SMatthew G. Knepley + dm - The DMPlex object 103870034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 103970034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 104070034214SMatthew G. Knepley - originalIS - The existing data 104170034214SMatthew G. Knepley 104270034214SMatthew G. Knepley Output Parameters: 104370034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 104470034214SMatthew G. Knepley - newIS - The new data 104570034214SMatthew G. Knepley 104670034214SMatthew G. Knepley Level: developer 104770034214SMatthew G. Knepley 1048db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()`, `DMPlexDistributeData()` 104970034214SMatthew G. Knepley @*/ 1050d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS) 1051d71ae5a4SJacob Faibussowitsch { 105270034214SMatthew G. Knepley PetscSF fieldSF; 105370034214SMatthew G. Knepley PetscInt *newValues, *remoteOffsets, fieldSize; 105470034214SMatthew G. Knepley const PetscInt *originalValues; 105570034214SMatthew G. Knepley 105670034214SMatthew G. Knepley PetscFunctionBegin; 10579566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeField, dm, 0, 0, 0)); 10589566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 105970034214SMatthew G. Knepley 10609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 10619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fieldSize, &newValues)); 106270034214SMatthew G. Knepley 10639566063dSJacob Faibussowitsch PetscCall(ISGetIndices(originalIS, &originalValues)); 10649566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 10659566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 10669566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *)originalValues, newValues, MPI_REPLACE)); 10679566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *)originalValues, newValues, MPI_REPLACE)); 10689566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 10699566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(originalIS, &originalValues)); 10709566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS)); 10719566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeField, dm, 0, 0, 0)); 10723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 107370034214SMatthew G. Knepley } 107470034214SMatthew G. Knepley 107570034214SMatthew G. Knepley /*@ 107670034214SMatthew G. Knepley DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 107770034214SMatthew G. Knepley 1078d083f849SBarry Smith Collective on dm 107970034214SMatthew G. Knepley 108070034214SMatthew G. Knepley Input Parameters: 108170034214SMatthew G. Knepley + dm - The DMPlex object 108270034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 108370034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 108470034214SMatthew G. Knepley . datatype - The type of data 108570034214SMatthew G. Knepley - originalData - The existing data 108670034214SMatthew G. Knepley 108770034214SMatthew G. Knepley Output Parameters: 108870034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout 108970034214SMatthew G. Knepley - newData - The new data 109070034214SMatthew G. Knepley 109170034214SMatthew G. Knepley Level: developer 109270034214SMatthew G. Knepley 1093db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()` 109470034214SMatthew G. Knepley @*/ 1095d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData) 1096d71ae5a4SJacob Faibussowitsch { 109770034214SMatthew G. Knepley PetscSF fieldSF; 109870034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 109970034214SMatthew G. Knepley PetscMPIInt dataSize; 110070034214SMatthew G. Knepley 110170034214SMatthew G. Knepley PetscFunctionBegin; 11029566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeData, dm, 0, 0, 0)); 11039566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 110470034214SMatthew G. Knepley 11059566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 11069566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_size(datatype, &dataSize)); 11079566063dSJacob Faibussowitsch PetscCall(PetscMalloc(fieldSize * dataSize, newData)); 110870034214SMatthew G. Knepley 11099566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 11109566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 11119566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, datatype, originalData, *newData, MPI_REPLACE)); 11129566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, datatype, originalData, *newData, MPI_REPLACE)); 11139566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 11149566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeData, dm, 0, 0, 0)); 11153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 111670034214SMatthew G. Knepley } 111770034214SMatthew G. Knepley 1118d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 1119d71ae5a4SJacob Faibussowitsch { 1120cc71bff1SMichael Lange DM_Plex *pmesh = (DM_Plex *)(dmParallel)->data; 1121cc71bff1SMichael Lange MPI_Comm comm; 1122cc71bff1SMichael Lange PetscSF coneSF; 1123cc71bff1SMichael Lange PetscSection originalConeSection, newConeSection; 1124ac04eaf7SToby Isaac PetscInt *remoteOffsets, *cones, *globCones, *newCones, newConesSize; 1125cc71bff1SMichael Lange PetscBool flg; 1126cc71bff1SMichael Lange 1127cc71bff1SMichael Lange PetscFunctionBegin; 1128cc71bff1SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11290c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 11309566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeCones, dm, 0, 0, 0)); 1131cc71bff1SMichael Lange /* Distribute cone section */ 11329566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 11339566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSection(dm, &originalConeSection)); 11349566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSection(dmParallel, &newConeSection)); 11359566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection)); 11369566063dSJacob Faibussowitsch PetscCall(DMSetUp(dmParallel)); 1137cc71bff1SMichael Lange /* Communicate and renumber cones */ 11389566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF)); 11399566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 11409566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dm, &cones)); 1141ac04eaf7SToby Isaac if (original) { 1142ac04eaf7SToby Isaac PetscInt numCones; 1143ac04eaf7SToby Isaac 11449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(originalConeSection, &numCones)); 11459566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCones, &globCones)); 11469566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones)); 1147367003a6SStefano Zampini } else { 1148ac04eaf7SToby Isaac globCones = cones; 1149ac04eaf7SToby Isaac } 11509566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dmParallel, &newCones)); 11519566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones, MPI_REPLACE)); 11529566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones, MPI_REPLACE)); 11531baa6e33SBarry Smith if (original) PetscCall(PetscFree(globCones)); 11549566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newConeSection, &newConesSize)); 11559566063dSJacob Faibussowitsch PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones)); 115676bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 11573533c52bSMatthew G. Knepley PetscInt p; 11583533c52bSMatthew G. Knepley PetscBool valid = PETSC_TRUE; 11593533c52bSMatthew G. Knepley for (p = 0; p < newConesSize; ++p) { 11609371c9d4SSatish Balay if (newCones[p] < 0) { 11619371c9d4SSatish Balay valid = PETSC_FALSE; 11629371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Point %" PetscInt_FMT " not in overlap SF\n", PetscGlobalRank, p)); 11639371c9d4SSatish Balay } 11643533c52bSMatthew G. Knepley } 116528b400f6SJacob Faibussowitsch PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 11663533c52bSMatthew G. Knepley } 11679566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-cones_view", &flg)); 1168cc71bff1SMichael Lange if (flg) { 11699566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Serial Cone Section:\n")); 11709566063dSJacob Faibussowitsch PetscCall(PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_(comm))); 11719566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Parallel Cone Section:\n")); 11729566063dSJacob Faibussowitsch PetscCall(PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_(comm))); 11739566063dSJacob Faibussowitsch PetscCall(PetscSFView(coneSF, NULL)); 1174cc71bff1SMichael Lange } 11759566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientations(dm, &cones)); 11769566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientations(dmParallel, &newCones)); 11779566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones, MPI_REPLACE)); 11789566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones, MPI_REPLACE)); 11799566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&coneSF)); 11809566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeCones, dm, 0, 0, 0)); 1181eaf898f9SPatrick Sanan /* Create supports and stratify DMPlex */ 1182cc71bff1SMichael Lange { 1183cc71bff1SMichael Lange PetscInt pStart, pEnd; 1184cc71bff1SMichael Lange 11859566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd)); 11869566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pmesh->supportSection, pStart, pEnd)); 1187cc71bff1SMichael Lange } 11889566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dmParallel)); 11899566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dmParallel)); 11901cf84007SMatthew G. Knepley { 11911cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 11921cf84007SMatthew G. Knepley 11939566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 11949566063dSJacob Faibussowitsch PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure)); 11959566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 11969566063dSJacob Faibussowitsch PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors)); 11971cf84007SMatthew G. Knepley } 11983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1199cc71bff1SMichael Lange } 1200cc71bff1SMichael Lange 1201d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel) 1202d71ae5a4SJacob Faibussowitsch { 12030df0e737SMichael Lange MPI_Comm comm; 12049318fe57SMatthew G. Knepley DM cdm, cdmParallel; 12050df0e737SMichael Lange PetscSection originalCoordSection, newCoordSection; 12060df0e737SMichael Lange Vec originalCoordinates, newCoordinates; 12070df0e737SMichael Lange PetscInt bs; 12080df0e737SMichael Lange const char *name; 12094fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 12100df0e737SMichael Lange 12110df0e737SMichael Lange PetscFunctionBegin; 12120df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 12130c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 12140df0e737SMichael Lange 12159566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12166858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 12176858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dmParallel, &cdmParallel)); 12186858538eSMatthew G. Knepley PetscCall(DMCopyDisc(cdm, cdmParallel)); 12199566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &originalCoordSection)); 12209566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dmParallel, &newCoordSection)); 12219566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &originalCoordinates)); 12220df0e737SMichael Lange if (originalCoordinates) { 12239566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates)); 12249566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)originalCoordinates, &name)); 12259566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)newCoordinates, name)); 12260df0e737SMichael Lange 12279566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates)); 12289566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmParallel, newCoordinates)); 12299566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(originalCoordinates, &bs)); 12309566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(newCoordinates, bs)); 12319566063dSJacob Faibussowitsch PetscCall(VecDestroy(&newCoordinates)); 12320df0e737SMichael Lange } 12336858538eSMatthew G. Knepley 12346858538eSMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12354fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L)); 12364fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dmParallel, maxCell, Lstart, L)); 12376858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 12386858538eSMatthew G. Knepley if (cdm) { 12396858538eSMatthew G. Knepley PetscCall(DMClone(dmParallel, &cdmParallel)); 12406858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateDM(dmParallel, cdmParallel)); 12419566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(cdm, cdmParallel)); 12426858538eSMatthew G. Knepley PetscCall(DMDestroy(&cdmParallel)); 12436858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateSection(dm, &originalCoordSection)); 12446858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dm, &originalCoordinates)); 12456858538eSMatthew G. Knepley PetscCall(PetscSectionCreate(comm, &newCoordSection)); 12466858538eSMatthew G. Knepley if (originalCoordinates) { 12476858538eSMatthew G. Knepley PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates)); 12486858538eSMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject)originalCoordinates, &name)); 12496858538eSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)newCoordinates, name)); 12506858538eSMatthew G. Knepley 12516858538eSMatthew G. Knepley PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates)); 12526858538eSMatthew G. Knepley PetscCall(VecGetBlockSize(originalCoordinates, &bs)); 12536858538eSMatthew G. Knepley PetscCall(VecSetBlockSize(newCoordinates, bs)); 12546858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateSection(dmParallel, bs, newCoordSection)); 12556858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dmParallel, newCoordinates)); 12566858538eSMatthew G. Knepley PetscCall(VecDestroy(&newCoordinates)); 12576858538eSMatthew G. Knepley } 12586858538eSMatthew G. Knepley PetscCall(PetscSectionDestroy(&newCoordSection)); 12596858538eSMatthew G. Knepley } 12603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12610df0e737SMichael Lange } 12620df0e737SMichael Lange 1263d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel) 1264d71ae5a4SJacob Faibussowitsch { 1265df0420ecSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 12660df0e737SMichael Lange MPI_Comm comm; 12677980c9d4SMatthew G. Knepley DMLabel depthLabel; 12680df0e737SMichael Lange PetscMPIInt rank; 12697980c9d4SMatthew G. Knepley PetscInt depth, d, numLabels, numLocalLabels, l; 1270df0420ecSMatthew G. Knepley PetscBool hasLabels = PETSC_FALSE, lsendDepth, sendDepth; 1271df0420ecSMatthew G. Knepley PetscObjectState depthState = -1; 12720df0e737SMichael Lange 12730df0e737SMichael Lange PetscFunctionBegin; 12740df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 12750c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 12760c86c063SLisandro Dalcin 12779566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeLabels, dm, 0, 0, 0)); 12789566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12799566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 12800df0e737SMichael Lange 1281df0420ecSMatthew G. Knepley /* If the user has changed the depth label, communicate it instead */ 12829566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 12839566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 12849566063dSJacob Faibussowitsch if (depthLabel) PetscCall(PetscObjectStateGet((PetscObject)depthLabel, &depthState)); 1285df0420ecSMatthew G. Knepley lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE; 12861c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm)); 1287df0420ecSMatthew G. Knepley if (sendDepth) { 12889566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dmParallel, &dmParallel->depthLabel)); 12899566063dSJacob Faibussowitsch PetscCall(DMRemoveLabelBySelf(dmParallel, &dmParallel->depthLabel, PETSC_FALSE)); 1290df0420ecSMatthew G. Knepley } 1291d995df53SMatthew G. Knepley /* Everyone must have either the same number of labels, or none */ 12929566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &numLocalLabels)); 1293d995df53SMatthew G. Knepley numLabels = numLocalLabels; 12949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm)); 1295627847f0SMatthew G. Knepley if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE; 12965d80c0bfSVaclav Hapla for (l = 0; l < numLabels; ++l) { 1297bdd2d751SMichael Lange DMLabel label = NULL, labelNew = NULL; 129883e10cb3SLisandro Dalcin PetscBool isDepth, lisOutput = PETSC_TRUE, isOutput; 1299d67d17b1SMatthew G. Knepley const char *name = NULL; 13000df0e737SMichael Lange 1301d67d17b1SMatthew G. Knepley if (hasLabels) { 13029566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm, l, &label)); 13030df0e737SMichael Lange /* Skip "depth" because it is recreated */ 13049566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)label, &name)); 13059566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "depth", &isDepth)); 1306*bbcf679cSJacob Faibussowitsch } else { 1307*bbcf679cSJacob Faibussowitsch isDepth = PETSC_FALSE; 1308d67d17b1SMatthew G. Knepley } 13099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm)); 131083e10cb3SLisandro Dalcin if (isDepth && !sendDepth) continue; 13119566063dSJacob Faibussowitsch PetscCall(DMLabelDistribute(label, migrationSF, &labelNew)); 131283e10cb3SLisandro Dalcin if (isDepth) { 13137980c9d4SMatthew G. Knepley /* Put in any missing strata which can occur if users are managing the depth label themselves */ 13147980c9d4SMatthew G. Knepley PetscInt gdepth; 13157980c9d4SMatthew G. Knepley 13161c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm)); 131763a3b9bcSJacob Faibussowitsch PetscCheck(!(depth >= 0) || !(gdepth != depth), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, depth, gdepth); 13187980c9d4SMatthew G. Knepley for (d = 0; d <= gdepth; ++d) { 13197980c9d4SMatthew G. Knepley PetscBool has; 13207980c9d4SMatthew G. Knepley 13219566063dSJacob Faibussowitsch PetscCall(DMLabelHasStratum(labelNew, d, &has)); 13229566063dSJacob Faibussowitsch if (!has) PetscCall(DMLabelAddStratum(labelNew, d)); 13237980c9d4SMatthew G. Knepley } 13247980c9d4SMatthew G. Knepley } 13259566063dSJacob Faibussowitsch PetscCall(DMAddLabel(dmParallel, labelNew)); 132683e10cb3SLisandro Dalcin /* Put the output flag in the new label */ 13279566063dSJacob Faibussowitsch if (hasLabels) PetscCall(DMGetLabelOutput(dm, name, &lisOutput)); 13281c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm)); 13299566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)labelNew, &name)); 13309566063dSJacob Faibussowitsch PetscCall(DMSetLabelOutput(dmParallel, name, isOutput)); 13319566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&labelNew)); 13320df0e737SMichael Lange } 1333695799ffSMatthew G. Knepley { 1334695799ffSMatthew G. Knepley DMLabel ctLabel; 1335695799ffSMatthew G. Knepley 1336695799ffSMatthew G. Knepley // Reset label for fast lookup 1337695799ffSMatthew G. Knepley PetscCall(DMPlexGetCellTypeLabel(dmParallel, &ctLabel)); 1338695799ffSMatthew G. Knepley PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel)); 1339695799ffSMatthew G. Knepley } 13409566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeLabels, dm, 0, 0, 0)); 13413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13420df0e737SMichael Lange } 13430df0e737SMichael Lange 1344d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 1345d71ae5a4SJacob Faibussowitsch { 134615078cd4SMichael Lange DM_Plex *mesh = (DM_Plex *)dm->data; 134715078cd4SMichael Lange DM_Plex *pmesh = (DM_Plex *)(dmParallel)->data; 1348a6f36705SMichael Lange MPI_Comm comm; 1349a6f36705SMichael Lange DM refTree; 1350a6f36705SMichael Lange PetscSection origParentSection, newParentSection; 1351a6f36705SMichael Lange PetscInt *origParents, *origChildIDs; 1352a6f36705SMichael Lange PetscBool flg; 1353a6f36705SMichael Lange 1354a6f36705SMichael Lange PetscFunctionBegin; 1355a6f36705SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 13560c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 13579566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 1358a6f36705SMichael Lange 1359a6f36705SMichael Lange /* Set up tree */ 13609566063dSJacob Faibussowitsch PetscCall(DMPlexGetReferenceTree(dm, &refTree)); 13619566063dSJacob Faibussowitsch PetscCall(DMPlexSetReferenceTree(dmParallel, refTree)); 13629566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm, &origParentSection, &origParents, &origChildIDs, NULL, NULL)); 1363a6f36705SMichael Lange if (origParentSection) { 1364a6f36705SMichael Lange PetscInt pStart, pEnd; 136508633170SToby Isaac PetscInt *newParents, *newChildIDs, *globParents; 1366a6f36705SMichael Lange PetscInt *remoteOffsetsParents, newParentSize; 1367a6f36705SMichael Lange PetscSF parentSF; 1368a6f36705SMichael Lange 13699566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd)); 13709566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel), &newParentSection)); 13719566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(newParentSection, pStart, pEnd)); 13729566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection)); 13739566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF)); 13749566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsetsParents)); 13759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newParentSection, &newParentSize)); 13769566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(newParentSize, &newParents, newParentSize, &newChildIDs)); 137708633170SToby Isaac if (original) { 137808633170SToby Isaac PetscInt numParents; 137908633170SToby Isaac 13809566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(origParentSection, &numParents)); 13819566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numParents, &globParents)); 13829566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents)); 13839371c9d4SSatish Balay } else { 138408633170SToby Isaac globParents = origParents; 138508633170SToby Isaac } 13869566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents, MPI_REPLACE)); 13879566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents, MPI_REPLACE)); 13881baa6e33SBarry Smith if (original) PetscCall(PetscFree(globParents)); 13899566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs, MPI_REPLACE)); 13909566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs, MPI_REPLACE)); 13919566063dSJacob Faibussowitsch PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents)); 139276bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 13934a54e071SToby Isaac PetscInt p; 13944a54e071SToby Isaac PetscBool valid = PETSC_TRUE; 13954a54e071SToby Isaac for (p = 0; p < newParentSize; ++p) { 13969371c9d4SSatish Balay if (newParents[p] < 0) { 13979371c9d4SSatish Balay valid = PETSC_FALSE; 13989371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " not in overlap SF\n", p)); 13999371c9d4SSatish Balay } 14004a54e071SToby Isaac } 140128b400f6SJacob Faibussowitsch PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 14024a54e071SToby Isaac } 14039566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-parents_view", &flg)); 1404a6f36705SMichael Lange if (flg) { 14059566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Serial Parent Section: \n")); 14069566063dSJacob Faibussowitsch PetscCall(PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_(comm))); 14079566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Parallel Parent Section: \n")); 14089566063dSJacob Faibussowitsch PetscCall(PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_(comm))); 14099566063dSJacob Faibussowitsch PetscCall(PetscSFView(parentSF, NULL)); 1410a6f36705SMichael Lange } 14119566063dSJacob Faibussowitsch PetscCall(DMPlexSetTree(dmParallel, newParentSection, newParents, newChildIDs)); 14129566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&newParentSection)); 14139566063dSJacob Faibussowitsch PetscCall(PetscFree2(newParents, newChildIDs)); 14149566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&parentSF)); 1415a6f36705SMichael Lange } 141615078cd4SMichael Lange pmesh->useAnchors = mesh->useAnchors; 14173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1418a6f36705SMichael Lange } 14190df0e737SMichael Lange 1420d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel) 1421d71ae5a4SJacob Faibussowitsch { 14229852e123SBarry Smith PetscMPIInt rank, size; 14234eca1733SMichael Lange MPI_Comm comm; 14244eca1733SMichael Lange 14254eca1733SMichael Lange PetscFunctionBegin; 14264eca1733SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14270c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 14284eca1733SMichael Lange 14294eca1733SMichael Lange /* Create point SF for parallel mesh */ 14309566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeSF, dm, 0, 0, 0)); 14319566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 14329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 14339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 14344eca1733SMichael Lange { 14354eca1733SMichael Lange const PetscInt *leaves; 14364eca1733SMichael Lange PetscSFNode *remotePoints, *rowners, *lowners; 14374eca1733SMichael Lange PetscInt numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints; 14384eca1733SMichael Lange PetscInt pStart, pEnd; 14394eca1733SMichael Lange 14409566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd)); 14419566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL)); 14429566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(numRoots, &rowners, numLeaves, &lowners)); 14434eca1733SMichael Lange for (p = 0; p < numRoots; p++) { 14444eca1733SMichael Lange rowners[p].rank = -1; 14454eca1733SMichael Lange rowners[p].index = -1; 14464eca1733SMichael Lange } 14479566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE)); 14489566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE)); 14494eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 14504eca1733SMichael Lange if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */ 14514eca1733SMichael Lange lowners[p].rank = rank; 14524eca1733SMichael Lange lowners[p].index = leaves ? leaves[p] : p; 14534eca1733SMichael Lange } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */ 14544eca1733SMichael Lange lowners[p].rank = -2; 14554eca1733SMichael Lange lowners[p].index = -2; 14564eca1733SMichael Lange } 14574eca1733SMichael Lange } 14584eca1733SMichael Lange for (p = 0; p < numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */ 14594eca1733SMichael Lange rowners[p].rank = -3; 14604eca1733SMichael Lange rowners[p].index = -3; 14614eca1733SMichael Lange } 14629566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC)); 14639566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC)); 14649566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE)); 14659566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners, MPI_REPLACE)); 14664eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 14671dca8a05SBarry Smith PetscCheck(lowners[p].rank >= 0 && lowners[p].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cell partition corrupt: point not claimed"); 14684eca1733SMichael Lange if (lowners[p].rank != rank) ++numGhostPoints; 14694eca1733SMichael Lange } 14709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGhostPoints, &ghostPoints)); 14719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGhostPoints, &remotePoints)); 14724eca1733SMichael Lange for (p = 0, gp = 0; p < numLeaves; ++p) { 14734eca1733SMichael Lange if (lowners[p].rank != rank) { 14744eca1733SMichael Lange ghostPoints[gp] = leaves ? leaves[p] : p; 14754eca1733SMichael Lange remotePoints[gp].rank = lowners[p].rank; 14764eca1733SMichael Lange remotePoints[gp].index = lowners[p].index; 14774eca1733SMichael Lange ++gp; 14784eca1733SMichael Lange } 14794eca1733SMichael Lange } 14809566063dSJacob Faibussowitsch PetscCall(PetscFree2(rowners, lowners)); 14819566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER)); 14829566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions((dmParallel)->sf)); 14834eca1733SMichael Lange } 14841cf84007SMatthew G. Knepley { 14851cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 14861cf84007SMatthew G. Knepley 14879566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 14889566063dSJacob Faibussowitsch PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure)); 14899566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 14909566063dSJacob Faibussowitsch PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors)); 14911cf84007SMatthew G. Knepley } 14929566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeSF, dm, 0, 0, 0)); 14933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14944eca1733SMichael Lange } 14954eca1733SMichael Lange 149698ba2d7fSLawrence Mitchell /*@ 149798ba2d7fSLawrence Mitchell DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition? 149898ba2d7fSLawrence Mitchell 149998ba2d7fSLawrence Mitchell Input Parameters: 150098ba2d7fSLawrence Mitchell + dm - The DMPlex object 150198ba2d7fSLawrence Mitchell - flg - Balance the partition? 150298ba2d7fSLawrence Mitchell 150398ba2d7fSLawrence Mitchell Level: intermediate 150498ba2d7fSLawrence Mitchell 1505db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetPartitionBalance()` 150698ba2d7fSLawrence Mitchell @*/ 1507d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg) 1508d71ae5a4SJacob Faibussowitsch { 150998ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 151098ba2d7fSLawrence Mitchell 151198ba2d7fSLawrence Mitchell PetscFunctionBegin; 151298ba2d7fSLawrence Mitchell mesh->partitionBalance = flg; 15133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 151498ba2d7fSLawrence Mitchell } 151598ba2d7fSLawrence Mitchell 151698ba2d7fSLawrence Mitchell /*@ 151798ba2d7fSLawrence Mitchell DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition? 151898ba2d7fSLawrence Mitchell 151998ba2d7fSLawrence Mitchell Input Parameter: 1520a2b725a8SWilliam Gropp . dm - The DMPlex object 152198ba2d7fSLawrence Mitchell 152298ba2d7fSLawrence Mitchell Output Parameter: 1523a2b725a8SWilliam Gropp . flg - Balance the partition? 152498ba2d7fSLawrence Mitchell 152598ba2d7fSLawrence Mitchell Level: intermediate 152698ba2d7fSLawrence Mitchell 1527db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexSetPartitionBalance()` 152898ba2d7fSLawrence Mitchell @*/ 1529d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg) 1530d71ae5a4SJacob Faibussowitsch { 153198ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 153298ba2d7fSLawrence Mitchell 153398ba2d7fSLawrence Mitchell PetscFunctionBegin; 153498ba2d7fSLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1535534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 153698ba2d7fSLawrence Mitchell *flg = mesh->partitionBalance; 15373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 153898ba2d7fSLawrence Mitchell } 153998ba2d7fSLawrence Mitchell 1540fc02256fSLawrence Mitchell typedef struct { 1541fc02256fSLawrence Mitchell PetscInt vote, rank, index; 1542fc02256fSLawrence Mitchell } Petsc3Int; 1543fc02256fSLawrence Mitchell 1544fc02256fSLawrence Mitchell /* MaxLoc, but carry a third piece of information around */ 1545d71ae5a4SJacob Faibussowitsch static void MPIAPI MaxLocCarry(void *in_, void *inout_, PetscMPIInt *len_, MPI_Datatype *dtype) 1546d71ae5a4SJacob Faibussowitsch { 1547fc02256fSLawrence Mitchell Petsc3Int *a = (Petsc3Int *)inout_; 1548fc02256fSLawrence Mitchell Petsc3Int *b = (Petsc3Int *)in_; 1549fc02256fSLawrence Mitchell PetscInt i, len = *len_; 1550fc02256fSLawrence Mitchell for (i = 0; i < len; i++) { 1551fc02256fSLawrence Mitchell if (a[i].vote < b[i].vote) { 1552fc02256fSLawrence Mitchell a[i].vote = b[i].vote; 1553fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1554fc02256fSLawrence Mitchell a[i].index = b[i].index; 1555fc02256fSLawrence Mitchell } else if (a[i].vote <= b[i].vote) { 1556fc02256fSLawrence Mitchell if (a[i].rank >= b[i].rank) { 1557fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1558fc02256fSLawrence Mitchell a[i].index = b[i].index; 1559fc02256fSLawrence Mitchell } 1560fc02256fSLawrence Mitchell } 1561fc02256fSLawrence Mitchell } 1562fc02256fSLawrence Mitchell } 1563fc02256fSLawrence Mitchell 1564f5bf2dbfSMichael Lange /*@C 1565a8c5bd36SStefano Zampini DMPlexCreatePointSF - Build a point SF from an SF describing a point migration 1566f5bf2dbfSMichael Lange 1567064ec1f3Sprj- Input Parameters: 1568f5bf2dbfSMichael Lange + dm - The source DMPlex object 1569f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping 1570d8d19677SJose E. Roman - ownership - Flag causing a vote to determine point ownership 1571f5bf2dbfSMichael Lange 1572f5bf2dbfSMichael Lange Output Parameter: 1573d8d19677SJose E. Roman . pointSF - The star forest describing the point overlap in the remapped DM 1574f5bf2dbfSMichael Lange 15753618482eSVaclav Hapla Notes: 15763618482eSVaclav Hapla Output pointSF is guaranteed to have the array of local indices (leaves) sorted. 15773618482eSVaclav Hapla 1578f5bf2dbfSMichael Lange Level: developer 1579f5bf2dbfSMichael Lange 1580db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 1581f5bf2dbfSMichael Lange @*/ 1582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF) 1583d71ae5a4SJacob Faibussowitsch { 158423193802SMatthew G. Knepley PetscMPIInt rank, size; 15851627f6ccSMichael Lange PetscInt p, nroots, nleaves, idx, npointLeaves; 1586f5bf2dbfSMichael Lange PetscInt *pointLocal; 1587f5bf2dbfSMichael Lange const PetscInt *leaves; 1588f5bf2dbfSMichael Lange const PetscSFNode *roots; 1589f5bf2dbfSMichael Lange PetscSFNode *rootNodes, *leafNodes, *pointRemote; 159023193802SMatthew G. Knepley Vec shifts; 1591cae3e4f3SLawrence Mitchell const PetscInt numShifts = 13759; 159223193802SMatthew G. Knepley const PetscScalar *shift = NULL; 159323193802SMatthew G. Knepley const PetscBool shiftDebug = PETSC_FALSE; 159498ba2d7fSLawrence Mitchell PetscBool balance; 1595f5bf2dbfSMichael Lange 1596f5bf2dbfSMichael Lange PetscFunctionBegin; 1597f5bf2dbfSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 15989566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 15999566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size)); 16009566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_CreatePointSF, dm, 0, 0, 0)); 1601f5bf2dbfSMichael Lange 16029566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitionBalance(dm, &balance)); 16039566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots)); 16049566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes)); 1605f5bf2dbfSMichael Lange if (ownership) { 1606fc02256fSLawrence Mitchell MPI_Op op; 1607fc02256fSLawrence Mitchell MPI_Datatype datatype; 1608fc02256fSLawrence Mitchell Petsc3Int *rootVote = NULL, *leafVote = NULL; 160923193802SMatthew G. Knepley /* If balancing, we compute a random cyclic shift of the rank for each remote point. That way, the max will evenly distribute among ranks. */ 161098ba2d7fSLawrence Mitchell if (balance) { 161123193802SMatthew G. Knepley PetscRandom r; 161223193802SMatthew G. Knepley 16139566063dSJacob Faibussowitsch PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r)); 16149566063dSJacob Faibussowitsch PetscCall(PetscRandomSetInterval(r, 0, 2467 * size)); 16159566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &shifts)); 16169566063dSJacob Faibussowitsch PetscCall(VecSetSizes(shifts, numShifts, numShifts)); 16179566063dSJacob Faibussowitsch PetscCall(VecSetType(shifts, VECSTANDARD)); 16189566063dSJacob Faibussowitsch PetscCall(VecSetRandom(shifts, r)); 16199566063dSJacob Faibussowitsch PetscCall(PetscRandomDestroy(&r)); 16209566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(shifts, &shift)); 162123193802SMatthew G. Knepley } 162223193802SMatthew G. Knepley 16239566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nroots, &rootVote)); 16249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &leafVote)); 162523193802SMatthew G. Knepley /* Point ownership vote: Process with highest rank owns shared points */ 1626f5bf2dbfSMichael Lange for (p = 0; p < nleaves; ++p) { 162723193802SMatthew G. Knepley if (shiftDebug) { 16289371c9d4SSatish Balay PetscCall(PetscSynchronizedPrintf(PetscObjectComm((PetscObject)dm), "[%d] Point %" PetscInt_FMT " RemotePoint %" PetscInt_FMT " Shift %" PetscInt_FMT " MyRank %" PetscInt_FMT "\n", rank, leaves ? leaves[p] : p, roots[p].index, 16299371c9d4SSatish Balay (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]), (rank + (shift ? (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]) : 0)) % size)); 163023193802SMatthew G. Knepley } 1631f5bf2dbfSMichael Lange /* Either put in a bid or we know we own it */ 1632fc02256fSLawrence Mitchell leafVote[p].vote = (rank + (shift ? (PetscInt)PetscRealPart(shift[roots[p].index % numShifts]) : 0)) % size; 1633fc02256fSLawrence Mitchell leafVote[p].rank = rank; 1634fc02256fSLawrence Mitchell leafVote[p].index = p; 1635f5bf2dbfSMichael Lange } 1636f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 16371627f6ccSMichael Lange /* Root must not participate in the reduction, flag so that MAXLOC does not use */ 1638fc02256fSLawrence Mitchell rootVote[p].vote = -3; 1639fc02256fSLawrence Mitchell rootVote[p].rank = -3; 1640fc02256fSLawrence Mitchell rootVote[p].index = -3; 1641f5bf2dbfSMichael Lange } 16429566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(3, MPIU_INT, &datatype)); 16439566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&datatype)); 16449566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_create(&MaxLocCarry, 1, &op)); 16459566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(migrationSF, datatype, leafVote, rootVote, op)); 16469566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(migrationSF, datatype, leafVote, rootVote, op)); 16479566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_free(&op)); 16489566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&datatype)); 1649c091126eSLawrence Mitchell for (p = 0; p < nroots; p++) { 1650fc02256fSLawrence Mitchell rootNodes[p].rank = rootVote[p].rank; 1651fc02256fSLawrence Mitchell rootNodes[p].index = rootVote[p].index; 1652c091126eSLawrence Mitchell } 16539566063dSJacob Faibussowitsch PetscCall(PetscFree(leafVote)); 16549566063dSJacob Faibussowitsch PetscCall(PetscFree(rootVote)); 1655f5bf2dbfSMichael Lange } else { 1656f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 1657f5bf2dbfSMichael Lange rootNodes[p].index = -1; 1658f5bf2dbfSMichael Lange rootNodes[p].rank = rank; 1659fc02256fSLawrence Mitchell } 1660f5bf2dbfSMichael Lange for (p = 0; p < nleaves; p++) { 1661f5bf2dbfSMichael Lange /* Write new local id into old location */ 1662ad540459SPierre Jolivet if (roots[p].rank == rank) rootNodes[roots[p].index].index = leaves ? leaves[p] : p; 1663f5bf2dbfSMichael Lange } 1664f5bf2dbfSMichael Lange } 16659566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes, MPI_REPLACE)); 16669566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes, MPI_REPLACE)); 1667f5bf2dbfSMichael Lange 166823193802SMatthew G. Knepley for (npointLeaves = 0, p = 0; p < nleaves; p++) { 1669b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) npointLeaves++; 167023193802SMatthew G. Knepley } 16719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointLeaves, &pointLocal)); 16729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointLeaves, &pointRemote)); 1673f5bf2dbfSMichael Lange for (idx = 0, p = 0; p < nleaves; p++) { 1674b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) { 16753618482eSVaclav Hapla /* Note that pointLocal is automatically sorted as it is sublist of 0, ..., nleaves-1 */ 1676f5bf2dbfSMichael Lange pointLocal[idx] = p; 1677f5bf2dbfSMichael Lange pointRemote[idx] = leafNodes[p]; 1678f5bf2dbfSMichael Lange idx++; 1679f5bf2dbfSMichael Lange } 1680f5bf2dbfSMichael Lange } 168123193802SMatthew G. Knepley if (shift) { 16829566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(shifts, &shift)); 16839566063dSJacob Faibussowitsch PetscCall(VecDestroy(&shifts)); 168423193802SMatthew G. Knepley } 16859566063dSJacob Faibussowitsch if (shiftDebug) PetscCall(PetscSynchronizedFlush(PetscObjectComm((PetscObject)dm), PETSC_STDOUT)); 16869566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), pointSF)); 16879566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*pointSF)); 16889566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER)); 16899566063dSJacob Faibussowitsch PetscCall(PetscFree2(rootNodes, leafNodes)); 16909566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_CreatePointSF, dm, 0, 0, 0)); 1691d7d32a9aSMatthew G. Knepley if (PetscDefined(USE_DEBUG)) PetscCall(DMPlexCheckPointSF(dm, *pointSF, PETSC_FALSE)); 16923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1693f5bf2dbfSMichael Lange } 1694f5bf2dbfSMichael Lange 169515078cd4SMichael Lange /*@C 169615078cd4SMichael Lange DMPlexMigrate - Migrates internal DM data over the supplied star forest 169715078cd4SMichael Lange 1698d083f849SBarry Smith Collective on dm 169983655b49SVáclav Hapla 1700064ec1f3Sprj- Input Parameters: 170115078cd4SMichael Lange + dm - The source DMPlex object 1702d8d19677SJose E. Roman - sf - The star forest communication context describing the migration pattern 170315078cd4SMichael Lange 170415078cd4SMichael Lange Output Parameter: 1705d8d19677SJose E. Roman . targetDM - The target DMPlex object 170615078cd4SMichael Lange 1707b9f40539SMichael Lange Level: intermediate 170815078cd4SMichael Lange 1709db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 171015078cd4SMichael Lange @*/ 1711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM) 1712d71ae5a4SJacob Faibussowitsch { 1713b9f40539SMichael Lange MPI_Comm comm; 1714cc1750acSStefano Zampini PetscInt dim, cdim, nroots; 1715b9f40539SMichael Lange PetscSF sfPoint; 171615078cd4SMichael Lange ISLocalToGlobalMapping ltogMigration; 1717ac04eaf7SToby Isaac ISLocalToGlobalMapping ltogOriginal = NULL; 171815078cd4SMichael Lange 171915078cd4SMichael Lange PetscFunctionBegin; 172015078cd4SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17219566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0)); 17229566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 17239566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 17249566063dSJacob Faibussowitsch PetscCall(DMSetDimension(targetDM, dim)); 17259566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 17269566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(targetDM, cdim)); 172715078cd4SMichael Lange 1728bfb0467fSMichael Lange /* Check for a one-to-all distribution pattern */ 17299566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 17309566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL)); 1731bfb0467fSMichael Lange if (nroots >= 0) { 1732b9f40539SMichael Lange IS isOriginal; 1733ac04eaf7SToby Isaac PetscInt n, size, nleaves; 1734ac04eaf7SToby Isaac PetscInt *numbering_orig, *numbering_new; 1735367003a6SStefano Zampini 1736b9f40539SMichael Lange /* Get the original point numbering */ 17379566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointNumbering(dm, &isOriginal)); 17389566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(isOriginal, <ogOriginal)); 17399566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltogOriginal, &size)); 17409566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt **)&numbering_orig)); 1741b9f40539SMichael Lange /* Convert to positive global numbers */ 17429371c9d4SSatish Balay for (n = 0; n < size; n++) { 17439371c9d4SSatish Balay if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n] + 1); 17449371c9d4SSatish Balay } 1745b9f40539SMichael Lange /* Derive the new local-to-global mapping from the old one */ 17469566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL)); 17479566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &numbering_new)); 17489566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sf, MPIU_INT, numbering_orig, numbering_new, MPI_REPLACE)); 17499566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_INT, numbering_orig, numbering_new, MPI_REPLACE)); 17509566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreate(comm, 1, nleaves, numbering_new, PETSC_OWN_POINTER, <ogMigration)); 17519566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt **)&numbering_orig)); 17529566063dSJacob Faibussowitsch PetscCall(ISDestroy(&isOriginal)); 175315078cd4SMichael Lange } else { 1754bfb0467fSMichael Lange /* One-to-all distribution pattern: We can derive LToG from SF */ 17559566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateSF(sf, 0, <ogMigration)); 175615078cd4SMichael Lange } 1757a5a902f7SVaclav Hapla PetscCall(PetscObjectSetName((PetscObject)ltogMigration, "Point renumbering for DM migration")); 1758a5a902f7SVaclav Hapla PetscCall(ISLocalToGlobalMappingViewFromOptions(ltogMigration, (PetscObject)dm, "-partition_view")); 175915078cd4SMichael Lange /* Migrate DM data to target DM */ 17609566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM)); 17619566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeLabels(dm, sf, targetDM)); 17629566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeCoordinates(dm, sf, targetDM)); 17639566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM)); 17649566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<ogOriginal)); 17659566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<ogMigration)); 17669566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0)); 17673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 176815078cd4SMichael Lange } 176915078cd4SMichael Lange 17703b8f15a2SMatthew G. Knepley /*@C 177170034214SMatthew G. Knepley DMPlexDistribute - Distributes the mesh and any associated sections. 177270034214SMatthew G. Knepley 1773d083f849SBarry Smith Collective on dm 177470034214SMatthew G. Knepley 1775064ec1f3Sprj- Input Parameters: 177670034214SMatthew G. Knepley + dm - The original DMPlex object 177770034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default 177870034214SMatthew G. Knepley 1779064ec1f3Sprj- Output Parameters: 1780f4ae5380SJed Brown + sf - The PetscSF used for point distribution, or NULL if not needed 1781f4ae5380SJed Brown - dmParallel - The distributed DMPlex object 178270034214SMatthew G. Knepley 1783f4ae5380SJed Brown Note: If the mesh was not distributed, the output dmParallel will be NULL. 178470034214SMatthew G. Knepley 1785b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 178670034214SMatthew G. Knepley representation on the mesh. 178770034214SMatthew G. Knepley 178870034214SMatthew G. Knepley Level: intermediate 178970034214SMatthew G. Knepley 1790db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexGetOverlap()` 179170034214SMatthew G. Knepley @*/ 1792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel) 1793d71ae5a4SJacob Faibussowitsch { 179470034214SMatthew G. Knepley MPI_Comm comm; 179515078cd4SMichael Lange PetscPartitioner partitioner; 1796f8987ae8SMichael Lange IS cellPart; 1797f8987ae8SMichael Lange PetscSection cellPartSection; 1798cf86098cSMatthew G. Knepley DM dmCoord; 1799f8987ae8SMichael Lange DMLabel lblPartition, lblMigration; 1800874ddda9SLisandro Dalcin PetscSF sfMigration, sfStratified, sfPoint; 180198ba2d7fSLawrence Mitchell PetscBool flg, balance; 1802874ddda9SLisandro Dalcin PetscMPIInt rank, size; 180370034214SMatthew G. Knepley 180470034214SMatthew G. Knepley PetscFunctionBegin; 180570034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1806d5c515a1SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, overlap, 2); 1807d5c515a1SMatthew G. Knepley if (sf) PetscValidPointer(sf, 3); 1808d5c515a1SMatthew G. Knepley PetscValidPointer(dmParallel, 4); 180970034214SMatthew G. Knepley 18100c86c063SLisandro Dalcin if (sf) *sf = NULL; 18110c86c063SLisandro Dalcin *dmParallel = NULL; 18129566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 18139566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 18149566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 18153ba16761SJacob Faibussowitsch if (size == 1) PetscFunctionReturn(PETSC_SUCCESS); 181670034214SMatthew G. Knepley 18179566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Distribute, dm, 0, 0, 0)); 181815078cd4SMichael Lange /* Create cell partition */ 18199566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Partition, dm, 0, 0, 0)); 18209566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &cellPartSection)); 18219566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm, &partitioner)); 18229566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDMPlexPartition(partitioner, dm, NULL, cellPartSection, &cellPart)); 18239566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_PartSelf, dm, 0, 0, 0)); 1824f8987ae8SMichael Lange { 1825f8987ae8SMichael Lange /* Convert partition to DMLabel */ 1826825f8a23SLisandro Dalcin IS is; 1827825f8a23SLisandro Dalcin PetscHSetI ht; 1828f8987ae8SMichael Lange const PetscInt *points; 18298e330a33SStefano Zampini PetscInt *iranks; 18308e330a33SStefano Zampini PetscInt pStart, pEnd, proc, npoints, poff = 0, nranks; 1831825f8a23SLisandro Dalcin 18329566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition)); 1833825f8a23SLisandro Dalcin /* Preallocate strata */ 18349566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ht)); 18359566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd)); 1836825f8a23SLisandro Dalcin for (proc = pStart; proc < pEnd; proc++) { 18379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints)); 18389566063dSJacob Faibussowitsch if (npoints) PetscCall(PetscHSetIAdd(ht, proc)); 1839825f8a23SLisandro Dalcin } 18409566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ht, &nranks)); 18419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nranks, &iranks)); 18429566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ht, &poff, iranks)); 18439566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ht)); 18449566063dSJacob Faibussowitsch PetscCall(DMLabelAddStrata(lblPartition, nranks, iranks)); 18459566063dSJacob Faibussowitsch PetscCall(PetscFree(iranks)); 1846825f8a23SLisandro Dalcin /* Inline DMPlexPartitionLabelClosure() */ 18479566063dSJacob Faibussowitsch PetscCall(ISGetIndices(cellPart, &points)); 18489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd)); 1849f8987ae8SMichael Lange for (proc = pStart; proc < pEnd; proc++) { 18509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints)); 1851825f8a23SLisandro Dalcin if (!npoints) continue; 18529566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellPartSection, proc, &poff)); 18539566063dSJacob Faibussowitsch PetscCall(DMPlexClosurePoints_Private(dm, npoints, points + poff, &is)); 18549566063dSJacob Faibussowitsch PetscCall(DMLabelSetStratumIS(lblPartition, proc, is)); 18559566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 1856f8987ae8SMichael Lange } 18579566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(cellPart, &points)); 1858f8987ae8SMichael Lange } 18599566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_PartSelf, dm, 0, 0, 0)); 18606e203dd9SStefano Zampini 18619566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration)); 18629566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration)); 18639566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration)); 18649566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified)); 18659566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfMigration)); 186643f7d02bSMichael Lange sfMigration = sfStratified; 18679566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sfMigration)); 18689566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Partition, dm, 0, 0, 0)); 18699566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-partition_view", &flg)); 187070034214SMatthew G. Knepley if (flg) { 18719566063dSJacob Faibussowitsch PetscCall(DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_(comm))); 18729566063dSJacob Faibussowitsch PetscCall(PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_(comm))); 187370034214SMatthew G. Knepley } 1874f8987ae8SMichael Lange 187515078cd4SMichael Lange /* Create non-overlapping parallel DM and migrate internal data */ 18769566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, dmParallel)); 18779566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*dmParallel, "Parallel Mesh")); 18789566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(dm, sfMigration, *dmParallel)); 187970034214SMatthew G. Knepley 1880a157612eSMichael Lange /* Build the point SF without overlap */ 18819566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitionBalance(dm, &balance)); 18829566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitionBalance(*dmParallel, balance)); 18839566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint)); 18849566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*dmParallel, sfPoint)); 18855f06a3ddSJed Brown PetscCall(DMPlexMigrateIsoperiodicFaceSF_Internal(dm, *dmParallel, sfMigration)); 18869566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dmParallel, &dmCoord)); 18879566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 18889566063dSJacob Faibussowitsch if (flg) PetscCall(PetscSFView(sfPoint, NULL)); 188970034214SMatthew G. Knepley 1890a157612eSMichael Lange if (overlap > 0) { 189115078cd4SMichael Lange DM dmOverlap; 189257f290daSMatthew G. Knepley PetscInt nroots, nleaves, noldleaves, l; 189357f290daSMatthew G. Knepley const PetscInt *oldLeaves; 189457f290daSMatthew G. Knepley PetscSFNode *newRemote, *permRemote; 189515078cd4SMichael Lange const PetscSFNode *oldRemote; 189615078cd4SMichael Lange PetscSF sfOverlap, sfOverlapPoint; 1897524e35f8SStefano Zampini 1898a157612eSMichael Lange /* Add the partition overlap to the distributed DM */ 18999566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap)); 19009566063dSJacob Faibussowitsch PetscCall(DMDestroy(dmParallel)); 1901a157612eSMichael Lange *dmParallel = dmOverlap; 1902c389ea9fSToby Isaac if (flg) { 19039566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Overlap Migration SF:\n")); 19049566063dSJacob Faibussowitsch PetscCall(PetscSFView(sfOverlap, NULL)); 1905c389ea9fSToby Isaac } 190643331d4aSMichael Lange 1907f8987ae8SMichael Lange /* Re-map the migration SF to establish the full migration pattern */ 19089566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfMigration, &nroots, &noldleaves, &oldLeaves, &oldRemote)); 19099566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL)); 19109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &newRemote)); 191157f290daSMatthew G. Knepley /* oldRemote: original root point mapping to original leaf point 191257f290daSMatthew G. Knepley newRemote: original leaf point mapping to overlapped leaf point */ 191357f290daSMatthew G. Knepley if (oldLeaves) { 191473e69a6aSMatthew G. Knepley /* After stratification, the migration remotes may not be in root (canonical) order, so we reorder using the leaf numbering */ 19159566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(noldleaves, &permRemote)); 191657f290daSMatthew G. Knepley for (l = 0; l < noldleaves; ++l) permRemote[oldLeaves[l]] = oldRemote[l]; 191757f290daSMatthew G. Knepley oldRemote = permRemote; 191857f290daSMatthew G. Knepley } 19199566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote, MPI_REPLACE)); 19209566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote, MPI_REPLACE)); 19219566063dSJacob Faibussowitsch if (oldLeaves) PetscCall(PetscFree(oldRemote)); 19229566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, &sfOverlapPoint)); 19239566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER)); 19249566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfOverlap)); 19259566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfMigration)); 192615078cd4SMichael Lange sfMigration = sfOverlapPoint; 1927c389ea9fSToby Isaac } 1928bf5244c7SMatthew G. Knepley /* Cleanup Partition */ 19299566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblPartition)); 19309566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblMigration)); 19319566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&cellPartSection)); 19329566063dSJacob Faibussowitsch PetscCall(ISDestroy(&cellPart)); 193345480ffeSMatthew G. Knepley /* Copy discretization */ 19349566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, *dmParallel)); 193566fe0bfeSMatthew G. Knepley /* Create sfNatural */ 193666fe0bfeSMatthew G. Knepley if (dm->useNatural) { 193766fe0bfeSMatthew G. Knepley PetscSection section; 193866fe0bfeSMatthew G. Knepley 1939fc6a3818SBlaise Bourdin PetscCall(DMSetUseNatural(*dmParallel, PETSC_TRUE)); 1940fc6a3818SBlaise Bourdin PetscCall(DMGetLocalSection(dm, §ion)); 1941fc6a3818SBlaise Bourdin 19428aee0f92SAlexis Marboeuf /* First DM with useNatural = PETSC_TRUE is considered natural */ 19438aee0f92SAlexis Marboeuf /* sfMigration and sfNatural are respectively the point and dofs SFs mapping to this natural DM */ 1944fc6a3818SBlaise Bourdin /* Compose with a previous sfNatural if present */ 1945fc6a3818SBlaise Bourdin if (dm->sfNatural) { 1946fc6a3818SBlaise Bourdin PetscSF natSF; 1947fc6a3818SBlaise Bourdin 1948fc6a3818SBlaise Bourdin PetscCall(DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &natSF)); 1949fc6a3818SBlaise Bourdin PetscCall(PetscSFCompose(dm->sfNatural, natSF, &(*dmParallel)->sfNatural)); 1950fc6a3818SBlaise Bourdin PetscCall(PetscSFDestroy(&natSF)); 1951fc6a3818SBlaise Bourdin } else { 1952fc6a3818SBlaise Bourdin PetscCall(DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural)); 1953fc6a3818SBlaise Bourdin } 19548aee0f92SAlexis Marboeuf /* Compose with a previous sfMigration if present */ 19558aee0f92SAlexis Marboeuf if (dm->sfMigration) { 19568aee0f92SAlexis Marboeuf PetscSF naturalPointSF; 19578aee0f92SAlexis Marboeuf 19588aee0f92SAlexis Marboeuf PetscCall(PetscSFCompose(dm->sfMigration, sfMigration, &naturalPointSF)); 19598aee0f92SAlexis Marboeuf PetscCall(PetscSFDestroy(&sfMigration)); 19608aee0f92SAlexis Marboeuf sfMigration = naturalPointSF; 19618aee0f92SAlexis Marboeuf } 196266fe0bfeSMatthew G. Knepley } 19635de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *dmParallel)); 1964721cbd76SMatthew G. Knepley /* Cleanup */ 19659371c9d4SSatish Balay if (sf) { 19669371c9d4SSatish Balay *sf = sfMigration; 19679371c9d4SSatish Balay } else PetscCall(PetscSFDestroy(&sfMigration)); 19689566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 19699566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Distribute, dm, 0, 0, 0)); 19703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 197170034214SMatthew G. Knepley } 1972a157612eSMichael Lange 1973a157612eSMichael Lange /*@C 197424cc2ca5SMatthew G. Knepley DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM. 1975a157612eSMichael Lange 1976d083f849SBarry Smith Collective on dm 1977a157612eSMichael Lange 1978064ec1f3Sprj- Input Parameters: 1979064ec1f3Sprj- + dm - The non-overlapping distributed DMPlex object 198057fe9a49SVaclav Hapla - overlap - The overlap of partitions (the same on all ranks) 1981a157612eSMichael Lange 1982064ec1f3Sprj- Output Parameters: 1983a157612eSMichael Lange + sf - The PetscSF used for point distribution 1984a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL 1985a157612eSMichael Lange 1986dccdeccaSVaclav Hapla Notes: 1987dccdeccaSVaclav Hapla If the mesh was not distributed, the return value is NULL. 1988a157612eSMichael Lange 1989b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 1990a157612eSMichael Lange representation on the mesh. 1991a157612eSMichael Lange 1992c506a872SMatthew G. Knepley Options Database Keys: 1993c506a872SMatthew G. Knepley + -dm_plex_overlap_labels <name1,name2,...> - List of overlap label names 1994c506a872SMatthew G. Knepley . -dm_plex_overlap_values <int1,int2,...> - List of overlap label values 1995c506a872SMatthew G. Knepley . -dm_plex_overlap_exclude_label <name> - Label used to exclude points from overlap 1996c506a872SMatthew G. Knepley - -dm_plex_overlap_exclude_value <int> - Label value used to exclude points from overlap 1997c506a872SMatthew G. Knepley 1998dccdeccaSVaclav Hapla Level: advanced 1999a157612eSMichael Lange 2000db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexCreateOverlapLabel()`, `DMPlexGetOverlap()` 2001a157612eSMichael Lange @*/ 2002d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap) 2003d71ae5a4SJacob Faibussowitsch { 2004c506a872SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 2005a157612eSMichael Lange MPI_Comm comm; 20063567eaeeSMatthew G. Knepley PetscMPIInt size, rank; 2007a157612eSMichael Lange PetscSection rootSection, leafSection; 2008a157612eSMichael Lange IS rootrank, leafrank; 2009cf86098cSMatthew G. Knepley DM dmCoord; 2010a9f1d5b2SMichael Lange DMLabel lblOverlap; 2011f5bf2dbfSMichael Lange PetscSF sfOverlap, sfStratified, sfPoint; 2012a157612eSMichael Lange 2013a157612eSMichael Lange PetscFunctionBegin; 2014a157612eSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 201557fe9a49SVaclav Hapla PetscValidLogicalCollectiveInt(dm, overlap, 2); 2016a157612eSMichael Lange if (sf) PetscValidPointer(sf, 3); 2017a157612eSMichael Lange PetscValidPointer(dmOverlap, 4); 2018a157612eSMichael Lange 20190c86c063SLisandro Dalcin if (sf) *sf = NULL; 20200c86c063SLisandro Dalcin *dmOverlap = NULL; 20219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 20229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 20239566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 20243ba16761SJacob Faibussowitsch if (size == 1) PetscFunctionReturn(PETSC_SUCCESS); 2025c506a872SMatthew G. Knepley { 2026c506a872SMatthew G. Knepley // We need to get options for the _already_distributed mesh, so it must be done here 2027c506a872SMatthew G. Knepley PetscInt overlap; 2028c506a872SMatthew G. Knepley const char *prefix; 2029c506a872SMatthew G. Knepley char oldPrefix[PETSC_MAX_PATH_LEN]; 2030a157612eSMichael Lange 2031c506a872SMatthew G. Knepley oldPrefix[0] = '\0'; 2032c506a872SMatthew G. Knepley PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix)); 2033c506a872SMatthew G. Knepley PetscCall(PetscStrcpy(oldPrefix, prefix)); 2034c506a872SMatthew G. Knepley PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, "dist_")); 2035c506a872SMatthew G. Knepley PetscCall(DMPlexGetOverlap(dm, &overlap)); 2036c506a872SMatthew G. Knepley PetscObjectOptionsBegin((PetscObject)dm); 2037dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap)); 2038c506a872SMatthew G. Knepley PetscOptionsEnd(); 2039c506a872SMatthew G. Knepley PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix[0] == '\0' ? NULL : oldPrefix)); 2040c506a872SMatthew G. Knepley } 20419566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0)); 2042a157612eSMichael Lange /* Compute point overlap with neighbouring processes on the distributed DM */ 20439566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Partition, dm, 0, 0, 0)); 20449566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &rootSection)); 20459566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &leafSection)); 20469566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank)); 2047c506a872SMatthew G. Knepley if (mesh->numOvLabels) PetscCall(DMPlexCreateOverlapLabelFromLabels(dm, mesh->numOvLabels, mesh->ovLabels, mesh->ovValues, mesh->numOvExLabels, mesh->ovExLabels, mesh->ovExValues, rootSection, rootrank, leafSection, leafrank, &lblOverlap)); 2048c506a872SMatthew G. Knepley else PetscCall(DMPlexCreateOverlapLabel(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap)); 2049a9f1d5b2SMichael Lange /* Convert overlap label to stratified migration SF */ 20509566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap)); 20519566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified)); 20529566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfOverlap)); 2053a9f1d5b2SMichael Lange sfOverlap = sfStratified; 20549566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)sfOverlap, "Overlap SF")); 20559566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(sfOverlap)); 2056a9f1d5b2SMichael Lange 20579566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&rootSection)); 20589566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&leafSection)); 20599566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rootrank)); 20609566063dSJacob Faibussowitsch PetscCall(ISDestroy(&leafrank)); 20619566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Partition, dm, 0, 0, 0)); 2062a157612eSMichael Lange 2063a157612eSMichael Lange /* Build the overlapping DM */ 20649566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, dmOverlap)); 20659566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*dmOverlap, "Parallel Mesh")); 20669566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(dm, sfOverlap, *dmOverlap)); 2067cb54e036SVaclav Hapla /* Store the overlap in the new DM */ 206860667520SVaclav Hapla PetscCall(DMPlexSetOverlap_Plex(*dmOverlap, dm, overlap)); 2069f5bf2dbfSMichael Lange /* Build the new point SF */ 20709566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint)); 20719566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*dmOverlap, sfPoint)); 20729566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dmOverlap, &dmCoord)); 20739566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 20746858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(*dmOverlap, &dmCoord)); 20756858538eSMatthew G. Knepley if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 20769566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 2077a157612eSMichael Lange /* Cleanup overlap partition */ 20789566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblOverlap)); 2079a9f1d5b2SMichael Lange if (sf) *sf = sfOverlap; 20809566063dSJacob Faibussowitsch else PetscCall(PetscSFDestroy(&sfOverlap)); 20819566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0)); 20823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2083a157612eSMichael Lange } 20846462276dSToby Isaac 2085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOverlap_Plex(DM dm, PetscInt *overlap) 2086d71ae5a4SJacob Faibussowitsch { 2087cb54e036SVaclav Hapla DM_Plex *mesh = (DM_Plex *)dm->data; 2088cb54e036SVaclav Hapla 2089cb54e036SVaclav Hapla PetscFunctionBegin; 2090cb54e036SVaclav Hapla *overlap = mesh->overlap; 20913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2092cb54e036SVaclav Hapla } 2093cb54e036SVaclav Hapla 2094d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOverlap_Plex(DM dm, DM dmSrc, PetscInt overlap) 2095d71ae5a4SJacob Faibussowitsch { 209660667520SVaclav Hapla DM_Plex *mesh = NULL; 209760667520SVaclav Hapla DM_Plex *meshSrc = NULL; 209860667520SVaclav Hapla 209960667520SVaclav Hapla PetscFunctionBegin; 210060667520SVaclav Hapla PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX); 210160667520SVaclav Hapla mesh = (DM_Plex *)dm->data; 210260667520SVaclav Hapla mesh->overlap = overlap; 210360667520SVaclav Hapla if (dmSrc) { 210460667520SVaclav Hapla PetscValidHeaderSpecificType(dmSrc, DM_CLASSID, 2, DMPLEX); 210560667520SVaclav Hapla meshSrc = (DM_Plex *)dmSrc->data; 210660667520SVaclav Hapla mesh->overlap += meshSrc->overlap; 210760667520SVaclav Hapla } 21083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 210960667520SVaclav Hapla } 211060667520SVaclav Hapla 2111cb54e036SVaclav Hapla /*@ 2112c506a872SMatthew G. Knepley DMPlexGetOverlap - Get the width of the cell overlap 2113cb54e036SVaclav Hapla 2114cb54e036SVaclav Hapla Not collective 2115cb54e036SVaclav Hapla 2116cb54e036SVaclav Hapla Input Parameter: 2117cb54e036SVaclav Hapla . dm - The DM 2118cb54e036SVaclav Hapla 2119064ec1f3Sprj- Output Parameter: 2120c506a872SMatthew G. Knepley . overlap - the width of the cell overlap 2121cb54e036SVaclav Hapla 2122cb54e036SVaclav Hapla Level: intermediate 2123cb54e036SVaclav Hapla 2124c506a872SMatthew G. Knepley .seealso: `DMPlexSetOverlap()`, `DMPlexDistribute()` 2125cb54e036SVaclav Hapla @*/ 2126d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOverlap(DM dm, PetscInt *overlap) 2127d71ae5a4SJacob Faibussowitsch { 2128cb54e036SVaclav Hapla PetscFunctionBegin; 2129cb54e036SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2130c506a872SMatthew G. Knepley PetscValidIntPointer(overlap, 2); 2131cac4c232SBarry Smith PetscUseMethod(dm, "DMPlexGetOverlap_C", (DM, PetscInt *), (dm, overlap)); 21323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2133cb54e036SVaclav Hapla } 2134cb54e036SVaclav Hapla 2135c506a872SMatthew G. Knepley /*@ 2136c506a872SMatthew G. Knepley DMPlexSetOverlap - Set the width of the cell overlap 2137c506a872SMatthew G. Knepley 2138c506a872SMatthew G. Knepley Logically collective 2139c506a872SMatthew G. Knepley 2140c506a872SMatthew G. Knepley Input Parameters: 2141c506a872SMatthew G. Knepley + dm - The DM 2142c506a872SMatthew G. Knepley . dmSrc - The DM that produced this one, or NULL 2143c506a872SMatthew G. Knepley - overlap - the width of the cell overlap 2144c506a872SMatthew G. Knepley 2145c506a872SMatthew G. Knepley Note: 2146c506a872SMatthew G. Knepley The overlap from dmSrc is added to dm 2147c506a872SMatthew G. Knepley 2148c506a872SMatthew G. Knepley Level: intermediate 2149c506a872SMatthew G. Knepley 2150c506a872SMatthew G. Knepley .seealso: `DMPlexGetOverlap()`, `DMPlexDistribute()` 2151c506a872SMatthew G. Knepley @*/ 2152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOverlap(DM dm, DM dmSrc, PetscInt overlap) 2153d71ae5a4SJacob Faibussowitsch { 2154c506a872SMatthew G. Knepley PetscFunctionBegin; 2155c506a872SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2156c506a872SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, overlap, 3); 2157c506a872SMatthew G. Knepley PetscTryMethod(dm, "DMPlexSetOverlap_C", (DM, DM, PetscInt), (dm, dmSrc, overlap)); 21583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2159c506a872SMatthew G. Knepley } 2160c506a872SMatthew G. Knepley 2161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeSetDefault_Plex(DM dm, PetscBool dist) 2162d71ae5a4SJacob Faibussowitsch { 2163e600fa54SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 2164e600fa54SMatthew G. Knepley 2165e600fa54SMatthew G. Knepley PetscFunctionBegin; 2166e600fa54SMatthew G. Knepley mesh->distDefault = dist; 21673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2168e600fa54SMatthew G. Knepley } 2169e600fa54SMatthew G. Knepley 2170e600fa54SMatthew G. Knepley /*@ 2171e600fa54SMatthew G. Knepley DMPlexDistributeSetDefault - Set flag indicating whether the DM should be distributed by default 2172e600fa54SMatthew G. Knepley 2173e600fa54SMatthew G. Knepley Logically collective 2174e600fa54SMatthew G. Knepley 2175e600fa54SMatthew G. Knepley Input Parameters: 2176e600fa54SMatthew G. Knepley + dm - The DM 2177e600fa54SMatthew G. Knepley - dist - Flag for distribution 2178e600fa54SMatthew G. Knepley 2179e600fa54SMatthew G. Knepley Level: intermediate 2180e600fa54SMatthew G. Knepley 21819e70506fSSatish Balay .seealso: `DMPlexDistributeGetDefault()`, `DMPlexDistribute()` 2182e600fa54SMatthew G. Knepley @*/ 2183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeSetDefault(DM dm, PetscBool dist) 2184d71ae5a4SJacob Faibussowitsch { 2185e600fa54SMatthew G. Knepley PetscFunctionBegin; 2186e600fa54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2187e600fa54SMatthew G. Knepley PetscValidLogicalCollectiveBool(dm, dist, 2); 2188cac4c232SBarry Smith PetscTryMethod(dm, "DMPlexDistributeSetDefault_C", (DM, PetscBool), (dm, dist)); 21893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2190e600fa54SMatthew G. Knepley } 2191e600fa54SMatthew G. Knepley 2192d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeGetDefault_Plex(DM dm, PetscBool *dist) 2193d71ae5a4SJacob Faibussowitsch { 2194e600fa54SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 2195e600fa54SMatthew G. Knepley 2196e600fa54SMatthew G. Knepley PetscFunctionBegin; 2197e600fa54SMatthew G. Knepley *dist = mesh->distDefault; 21983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2199e600fa54SMatthew G. Knepley } 2200e600fa54SMatthew G. Knepley 2201e600fa54SMatthew G. Knepley /*@ 2202e600fa54SMatthew G. Knepley DMPlexDistributeGetDefault - Get flag indicating whether the DM should be distributed by default 2203e600fa54SMatthew G. Knepley 2204e600fa54SMatthew G. Knepley Not collective 2205e600fa54SMatthew G. Knepley 2206e600fa54SMatthew G. Knepley Input Parameter: 2207e600fa54SMatthew G. Knepley . dm - The DM 2208e600fa54SMatthew G. Knepley 2209e600fa54SMatthew G. Knepley Output Parameter: 2210e600fa54SMatthew G. Knepley . dist - Flag for distribution 2211e600fa54SMatthew G. Knepley 2212e600fa54SMatthew G. Knepley Level: intermediate 2213e600fa54SMatthew G. Knepley 22149e70506fSSatish Balay .seealso: `DMPlexDistributeSetDefault()`, `DMPlexDistribute()` 2215e600fa54SMatthew G. Knepley @*/ 2216d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributeGetDefault(DM dm, PetscBool *dist) 2217d71ae5a4SJacob Faibussowitsch { 2218e600fa54SMatthew G. Knepley PetscFunctionBegin; 2219e600fa54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2220e600fa54SMatthew G. Knepley PetscValidBoolPointer(dist, 2); 2221cac4c232SBarry Smith PetscUseMethod(dm, "DMPlexDistributeGetDefault_C", (DM, PetscBool *), (dm, dist)); 22223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2223e600fa54SMatthew G. Knepley } 2224e600fa54SMatthew G. Knepley 22256462276dSToby Isaac /*@C 22266462276dSToby Isaac DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the 22276462276dSToby Isaac root process of the original's communicator. 22286462276dSToby Isaac 2229d083f849SBarry Smith Collective on dm 223083655b49SVáclav Hapla 2231064ec1f3Sprj- Input Parameter: 22326462276dSToby Isaac . dm - the original DMPlex object 22336462276dSToby Isaac 22346462276dSToby Isaac Output Parameters: 2235a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 2236a13df41bSStefano Zampini - gatherMesh - the gathered DM object, or NULL 22376462276dSToby Isaac 22386462276dSToby Isaac Level: intermediate 22396462276dSToby Isaac 2240db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetRedundantDM()` 22416462276dSToby Isaac @*/ 2242d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh) 2243d71ae5a4SJacob Faibussowitsch { 22446462276dSToby Isaac MPI_Comm comm; 22456462276dSToby Isaac PetscMPIInt size; 22466462276dSToby Isaac PetscPartitioner oldPart, gatherPart; 22476462276dSToby Isaac 22486462276dSToby Isaac PetscFunctionBegin; 22496462276dSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2250064a246eSJacob Faibussowitsch PetscValidPointer(gatherMesh, 3); 22510c86c063SLisandro Dalcin *gatherMesh = NULL; 2252a13df41bSStefano Zampini if (sf) *sf = NULL; 22536462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 22549566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 22553ba16761SJacob Faibussowitsch if (size == 1) PetscFunctionReturn(PETSC_SUCCESS); 22569566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm, &oldPart)); 22579566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)oldPart)); 22589566063dSJacob Faibussowitsch PetscCall(PetscPartitionerCreate(comm, &gatherPart)); 22599566063dSJacob Faibussowitsch PetscCall(PetscPartitionerSetType(gatherPart, PETSCPARTITIONERGATHER)); 22609566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitioner(dm, gatherPart)); 22619566063dSJacob Faibussowitsch PetscCall(DMPlexDistribute(dm, 0, sf, gatherMesh)); 2262a13df41bSStefano Zampini 22639566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitioner(dm, oldPart)); 22649566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDestroy(&gatherPart)); 22659566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDestroy(&oldPart)); 22663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22676462276dSToby Isaac } 22686462276dSToby Isaac 22696462276dSToby Isaac /*@C 22706462276dSToby Isaac DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process. 22716462276dSToby Isaac 2272d083f849SBarry Smith Collective on dm 227383655b49SVáclav Hapla 2274064ec1f3Sprj- Input Parameter: 22756462276dSToby Isaac . dm - the original DMPlex object 22766462276dSToby Isaac 22776462276dSToby Isaac Output Parameters: 2278a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 2279a13df41bSStefano Zampini - redundantMesh - the redundant DM object, or NULL 22806462276dSToby Isaac 22816462276dSToby Isaac Level: intermediate 22826462276dSToby Isaac 2283db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetGatherDM()` 22846462276dSToby Isaac @*/ 2285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh) 2286d71ae5a4SJacob Faibussowitsch { 22876462276dSToby Isaac MPI_Comm comm; 22886462276dSToby Isaac PetscMPIInt size, rank; 22896462276dSToby Isaac PetscInt pStart, pEnd, p; 22906462276dSToby Isaac PetscInt numPoints = -1; 2291a13df41bSStefano Zampini PetscSF migrationSF, sfPoint, gatherSF; 22926462276dSToby Isaac DM gatherDM, dmCoord; 22936462276dSToby Isaac PetscSFNode *points; 22946462276dSToby Isaac 22956462276dSToby Isaac PetscFunctionBegin; 22966462276dSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2297064a246eSJacob Faibussowitsch PetscValidPointer(redundantMesh, 3); 22980c86c063SLisandro Dalcin *redundantMesh = NULL; 22996462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 23009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 230168dbc166SMatthew G. Knepley if (size == 1) { 23029566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 230368dbc166SMatthew G. Knepley *redundantMesh = dm; 2304a13df41bSStefano Zampini if (sf) *sf = NULL; 23053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 230668dbc166SMatthew G. Knepley } 23079566063dSJacob Faibussowitsch PetscCall(DMPlexGetGatherDM(dm, &gatherSF, &gatherDM)); 23083ba16761SJacob Faibussowitsch if (!gatherDM) PetscFunctionReturn(PETSC_SUCCESS); 23099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 23109566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(gatherDM, &pStart, &pEnd)); 23116462276dSToby Isaac numPoints = pEnd - pStart; 23129566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&numPoints, 1, MPIU_INT, 0, comm)); 23139566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &points)); 23149566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, &migrationSF)); 23156462276dSToby Isaac for (p = 0; p < numPoints; p++) { 23166462276dSToby Isaac points[p].index = p; 23176462276dSToby Isaac points[p].rank = 0; 23186462276dSToby Isaac } 23199566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(migrationSF, pEnd - pStart, numPoints, NULL, PETSC_OWN_POINTER, points, PETSC_OWN_POINTER)); 23209566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, redundantMesh)); 23219566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*redundantMesh, "Redundant Mesh")); 23229566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(gatherDM, migrationSF, *redundantMesh)); 23232e28cf0cSVaclav Hapla /* This is to express that all point are in overlap */ 23242e28cf0cSVaclav Hapla PetscCall(DMPlexSetOverlap_Plex(*redundantMesh, NULL, PETSC_MAX_INT)); 23259566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint)); 23269566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*redundantMesh, sfPoint)); 23279566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*redundantMesh, &dmCoord)); 23289566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 23299566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 2330a13df41bSStefano Zampini if (sf) { 2331a13df41bSStefano Zampini PetscSF tsf; 2332a13df41bSStefano Zampini 23339566063dSJacob Faibussowitsch PetscCall(PetscSFCompose(gatherSF, migrationSF, &tsf)); 23349566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, tsf, sf)); 23359566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&tsf)); 2336a13df41bSStefano Zampini } 23379566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&migrationSF)); 23389566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&gatherSF)); 23399566063dSJacob Faibussowitsch PetscCall(DMDestroy(&gatherDM)); 23409566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, *redundantMesh)); 23415de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *redundantMesh)); 23423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23436462276dSToby Isaac } 23445fa78c88SVaclav Hapla 23455fa78c88SVaclav Hapla /*@ 23465fa78c88SVaclav Hapla DMPlexIsDistributed - Find out whether this DM is distributed, i.e. more than one rank owns some points. 23475fa78c88SVaclav Hapla 23485fa78c88SVaclav Hapla Collective 23495fa78c88SVaclav Hapla 23505fa78c88SVaclav Hapla Input Parameter: 23515fa78c88SVaclav Hapla . dm - The DM object 23525fa78c88SVaclav Hapla 23535fa78c88SVaclav Hapla Output Parameter: 23545fa78c88SVaclav Hapla . distributed - Flag whether the DM is distributed 23555fa78c88SVaclav Hapla 23565fa78c88SVaclav Hapla Level: intermediate 23575fa78c88SVaclav Hapla 23585fa78c88SVaclav Hapla Notes: 23595fa78c88SVaclav Hapla This currently finds out whether at least two ranks have any DAG points. 23605fa78c88SVaclav Hapla This involves MPI_Allreduce() with one integer. 23615fa78c88SVaclav Hapla The result is currently not stashed so every call to this routine involves this global communication. 23625fa78c88SVaclav Hapla 2363db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetOverlap()`, `DMPlexIsInterpolated()` 23645fa78c88SVaclav Hapla @*/ 2365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexIsDistributed(DM dm, PetscBool *distributed) 2366d71ae5a4SJacob Faibussowitsch { 23675fa78c88SVaclav Hapla PetscInt pStart, pEnd, count; 23685fa78c88SVaclav Hapla MPI_Comm comm; 236935d70e31SStefano Zampini PetscMPIInt size; 23705fa78c88SVaclav Hapla 23715fa78c88SVaclav Hapla PetscFunctionBegin; 23725fa78c88SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2373dadcf809SJacob Faibussowitsch PetscValidBoolPointer(distributed, 2); 23749566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 23759566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 23769371c9d4SSatish Balay if (size == 1) { 23779371c9d4SSatish Balay *distributed = PETSC_FALSE; 23783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23799371c9d4SSatish Balay } 23809566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 238135d70e31SStefano Zampini count = (pEnd - pStart) > 0 ? 1 : 0; 23829566063dSJacob Faibussowitsch PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &count, 1, MPIU_INT, MPI_SUM, comm)); 23835fa78c88SVaclav Hapla *distributed = count > 1 ? PETSC_TRUE : PETSC_FALSE; 23843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23855fa78c88SVaclav Hapla } 23861d1f2f2aSksagiyam 23871d1f2f2aSksagiyam /*@C 23881d1f2f2aSksagiyam DMPlexDistributionSetName - Set the name of the specific parallel distribution 23891d1f2f2aSksagiyam 23901d1f2f2aSksagiyam Input Parameters: 23911d1f2f2aSksagiyam + dm - The DM 23921d1f2f2aSksagiyam - name - The name of the specific parallel distribution 23931d1f2f2aSksagiyam 23941d1f2f2aSksagiyam Note: 23951d1f2f2aSksagiyam If distribution name is set when saving, DMPlexTopologyView() saves the plex's 23961d1f2f2aSksagiyam parallel distribution (i.e., partition, ownership, and local ordering of points) under 23971d1f2f2aSksagiyam this name. Conversely, if distribution name is set when loading, DMPlexTopologyLoad() 23981d1f2f2aSksagiyam loads the parallel distribution stored in file under this name. 23991d1f2f2aSksagiyam 24001d1f2f2aSksagiyam Level: developer 24011d1f2f2aSksagiyam 24021d1f2f2aSksagiyam .seealso: `DMPlexDistributionGetName()`, `DMPlexTopologyView()`, `DMPlexTopologyLoad()` 24031d1f2f2aSksagiyam @*/ 2404d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributionSetName(DM dm, const char name[]) 2405d71ae5a4SJacob Faibussowitsch { 24061d1f2f2aSksagiyam DM_Plex *mesh = (DM_Plex *)dm->data; 24071d1f2f2aSksagiyam 24081d1f2f2aSksagiyam PetscFunctionBegin; 24091d1f2f2aSksagiyam PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX); 24101d1f2f2aSksagiyam if (name) PetscValidCharPointer(name, 2); 24111d1f2f2aSksagiyam PetscCall(PetscFree(mesh->distributionName)); 24121d1f2f2aSksagiyam PetscCall(PetscStrallocpy(name, &mesh->distributionName)); 24133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24141d1f2f2aSksagiyam } 24151d1f2f2aSksagiyam 24161d1f2f2aSksagiyam /*@C 24171d1f2f2aSksagiyam DMPlexDistributionGetName - Retrieve the name of the specific parallel distribution 24181d1f2f2aSksagiyam 24191d1f2f2aSksagiyam Input Parameter: 24201d1f2f2aSksagiyam . dm - The DM 24211d1f2f2aSksagiyam 24221d1f2f2aSksagiyam Output Parameter: 24231d1f2f2aSksagiyam . name - The name of the specific parallel distribution 24241d1f2f2aSksagiyam 24251d1f2f2aSksagiyam Note: 24261d1f2f2aSksagiyam If distribution name is set when saving, DMPlexTopologyView() saves the plex's 24271d1f2f2aSksagiyam parallel distribution (i.e., partition, ownership, and local ordering of points) under 24281d1f2f2aSksagiyam this name. Conversely, if distribution name is set when loading, DMPlexTopologyLoad() 24291d1f2f2aSksagiyam loads the parallel distribution stored in file under this name. 24301d1f2f2aSksagiyam 24311d1f2f2aSksagiyam Level: developer 24321d1f2f2aSksagiyam 24331d1f2f2aSksagiyam .seealso: `DMPlexDistributionSetName()`, `DMPlexTopologyView()`, `DMPlexTopologyLoad()` 24341d1f2f2aSksagiyam @*/ 2435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexDistributionGetName(DM dm, const char *name[]) 2436d71ae5a4SJacob Faibussowitsch { 24371d1f2f2aSksagiyam DM_Plex *mesh = (DM_Plex *)dm->data; 24381d1f2f2aSksagiyam 24391d1f2f2aSksagiyam PetscFunctionBegin; 24401d1f2f2aSksagiyam PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX); 24411d1f2f2aSksagiyam PetscValidPointer(name, 2); 24421d1f2f2aSksagiyam *name = mesh->distributionName; 24433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24441d1f2f2aSksagiyam } 2445