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 @*/ 213c1f0c11SLawrence Mitchell PetscErrorCode DMPlexSetAdjacencyUser(DM dm,PetscErrorCode (*user)(DM,PetscInt,PetscInt*,PetscInt[],void*),void *ctx) 223c1f0c11SLawrence Mitchell { 233c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 243c1f0c11SLawrence Mitchell 253c1f0c11SLawrence Mitchell PetscFunctionBegin; 263c1f0c11SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 273c1f0c11SLawrence Mitchell mesh->useradjacency = user; 283c1f0c11SLawrence Mitchell mesh->useradjacencyctx = ctx; 293c1f0c11SLawrence Mitchell PetscFunctionReturn(0); 303c1f0c11SLawrence Mitchell } 313c1f0c11SLawrence Mitchell 323c1f0c11SLawrence Mitchell /*@C 333c1f0c11SLawrence Mitchell DMPlexGetAdjacencyUser - get the user-defined adjacency callback 343c1f0c11SLawrence Mitchell 353c1f0c11SLawrence Mitchell Input Parameter: 363c1f0c11SLawrence Mitchell . dm - The DM object 373c1f0c11SLawrence Mitchell 383c1f0c11SLawrence Mitchell Output Parameters: 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 @*/ 463c1f0c11SLawrence Mitchell PetscErrorCode DMPlexGetAdjacencyUser(DM dm, PetscErrorCode (**user)(DM,PetscInt,PetscInt*,PetscInt[],void*), void **ctx) 473c1f0c11SLawrence Mitchell { 483c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 493c1f0c11SLawrence Mitchell 503c1f0c11SLawrence Mitchell PetscFunctionBegin; 513c1f0c11SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 523c1f0c11SLawrence Mitchell if (user) *user = mesh->useradjacency; 533c1f0c11SLawrence Mitchell if (ctx) *ctx = mesh->useradjacencyctx; 543c1f0c11SLawrence Mitchell PetscFunctionReturn(0); 553c1f0c11SLawrence Mitchell } 563c1f0c11SLawrence Mitchell 5770034214SMatthew G. Knepley /*@ 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 @*/ 685b317d89SToby Isaac PetscErrorCode DMPlexSetAdjacencyUseAnchors(DM dm, PetscBool useAnchors) 698b0b4c70SToby Isaac { 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; 758b0b4c70SToby Isaac PetscFunctionReturn(0); 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 @*/ 915b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacencyUseAnchors(DM dm, PetscBool *useAnchors) 928b0b4c70SToby Isaac { 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; 998b0b4c70SToby Isaac PetscFunctionReturn(0); 1008b0b4c70SToby Isaac } 1018b0b4c70SToby Isaac 10270034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Cone_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) 10370034214SMatthew G. Knepley { 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; 12570034214SMatthew G. Knepley PetscFunctionReturn(0); 12670034214SMatthew G. Knepley } 12770034214SMatthew G. Knepley 12870034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) 12970034214SMatthew G. Knepley { 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; 15170034214SMatthew G. Knepley PetscFunctionReturn(0); 15270034214SMatthew G. Knepley } 15370034214SMatthew G. Knepley 15470034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[]) 15570034214SMatthew G. Knepley { 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; 17670034214SMatthew G. Knepley PetscFunctionReturn(0); 17770034214SMatthew G. Knepley } 17870034214SMatthew G. Knepley 1795b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[]) 18070034214SMatthew G. Knepley { 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 2358b0b4c70SToby Isaac if (p >= aStart && p < aEnd) { 2369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(aSec,p,&aDof)); 2378b0b4c70SToby Isaac } 2388b0b4c70SToby Isaac if (aDof) { 2398b0b4c70SToby Isaac PetscInt aOff; 2408b0b4c70SToby Isaac PetscInt s, q; 2418b0b4c70SToby Isaac 2428b0b4c70SToby Isaac for (j = i + 1; j < numAdj; j++) { 2438b0b4c70SToby Isaac orig[j - 1] = orig[j]; 2448b0b4c70SToby Isaac } 2458b0b4c70SToby Isaac origSize--; 2468b0b4c70SToby Isaac numAdj--; 2479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(aSec,p,&aOff)); 2488b0b4c70SToby Isaac for (s = 0; s < aDof; ++s) { 249527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) { 2508b0b4c70SToby Isaac if (anchors[aOff+s] == orig[q]) break; 2518b0b4c70SToby Isaac } 25263a3b9bcSJacob Faibussowitsch PetscCheck(numAdj <= maxAdjSize,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%" PetscInt_FMT ")", maxAdjSize); 2538b0b4c70SToby Isaac } 2548b0b4c70SToby Isaac } 2558b0b4c70SToby Isaac else { 2568b0b4c70SToby Isaac i++; 2578b0b4c70SToby Isaac } 2588b0b4c70SToby Isaac } 2598b0b4c70SToby Isaac *adjSize = numAdj; 2609566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(aIS,&anchors)); 2618b0b4c70SToby Isaac } 26270034214SMatthew G. Knepley PetscFunctionReturn(0); 26370034214SMatthew G. Knepley } 26470034214SMatthew G. Knepley 26570034214SMatthew G. Knepley /*@ 26670034214SMatthew G. Knepley DMPlexGetAdjacency - Return all points adjacent to the given point 26770034214SMatthew G. Knepley 26870034214SMatthew G. Knepley Input Parameters: 26970034214SMatthew G. Knepley + dm - The DM object 2706b867d5aSJose E. Roman - p - The point 27170034214SMatthew G. Knepley 2726b867d5aSJose E. Roman Input/Output Parameters: 2736b867d5aSJose E. Roman + adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE; 2746b867d5aSJose E. Roman on output the number of adjacent points 2756b867d5aSJose E. Roman - adj - Either NULL so that the array is allocated, or an existing array with size adjSize; 2766b867d5aSJose E. Roman on output contains the adjacent points 27770034214SMatthew G. Knepley 27870034214SMatthew G. Knepley Level: advanced 27970034214SMatthew G. Knepley 28095452b02SPatrick Sanan Notes: 28195452b02SPatrick Sanan The user must PetscFree the adj array if it was not passed in. 28270034214SMatthew G. Knepley 283db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMPlexDistribute()`, `DMCreateMatrix()`, `DMPlexPreallocateOperator()` 28470034214SMatthew G. Knepley @*/ 28570034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[]) 28670034214SMatthew G. Knepley { 2871cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 28870034214SMatthew G. Knepley 28970034214SMatthew G. Knepley PetscFunctionBeginHot; 29070034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 291dadcf809SJacob Faibussowitsch PetscValidIntPointer(adjSize,3); 29270034214SMatthew G. Knepley PetscValidPointer(adj,4); 2939566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 2949566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 2959566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj)); 29670034214SMatthew G. Knepley PetscFunctionReturn(0); 29770034214SMatthew G. Knepley } 29808633170SToby Isaac 299b0a623aaSMatthew G. Knepley /*@ 300b0a623aaSMatthew G. Knepley DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity 301b0a623aaSMatthew G. Knepley 302d083f849SBarry Smith Collective on dm 303b0a623aaSMatthew G. Knepley 304b0a623aaSMatthew G. Knepley Input Parameters: 305b0a623aaSMatthew G. Knepley + dm - The DM 3066b867d5aSJose E. Roman . sfPoint - The PetscSF which encodes point connectivity 3076b867d5aSJose E. Roman . rootRankSection - 3086b867d5aSJose E. Roman . rootRanks - 3096b867d5aSJose E. Roman . leftRankSection - 3106b867d5aSJose E. Roman - leafRanks - 311b0a623aaSMatthew G. Knepley 312b0a623aaSMatthew G. Knepley Output Parameters: 313b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL 314b0a623aaSMatthew G. Knepley - sfProcess - An SF encoding the two-sided process connectivity, or NULL 315b0a623aaSMatthew G. Knepley 316b0a623aaSMatthew G. Knepley Level: developer 317b0a623aaSMatthew G. Knepley 318db781477SPatrick Sanan .seealso: `PetscSFCreate()`, `DMPlexCreateProcessSF()` 319b0a623aaSMatthew G. Knepley @*/ 320b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess) 321b0a623aaSMatthew G. Knepley { 322b0a623aaSMatthew G. Knepley const PetscSFNode *remotePoints; 323b0a623aaSMatthew G. Knepley PetscInt *localPointsNew; 324b0a623aaSMatthew G. Knepley PetscSFNode *remotePointsNew; 325b0a623aaSMatthew G. Knepley const PetscInt *nranks; 326b0a623aaSMatthew G. Knepley PetscInt *ranksNew; 327b0a623aaSMatthew G. Knepley PetscBT neighbors; 328b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, numLeaves, l, numNeighbors, n; 3299852e123SBarry Smith PetscMPIInt size, proc, rank; 330b0a623aaSMatthew G. Knepley 331b0a623aaSMatthew G. Knepley PetscFunctionBegin; 332b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 333b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2); 334064a246eSJacob Faibussowitsch if (processRanks) {PetscValidPointer(processRanks, 7);} 335064a246eSJacob Faibussowitsch if (sfProcess) {PetscValidPointer(sfProcess, 8);} 3369566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size)); 3379566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank)); 3389566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints)); 3399566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(size, &neighbors)); 3409566063dSJacob Faibussowitsch PetscCall(PetscBTMemzero(size, neighbors)); 341b0a623aaSMatthew G. Knepley /* Compute root-to-leaf process connectivity */ 3429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(rootRankSection, &pStart, &pEnd)); 3439566063dSJacob Faibussowitsch PetscCall(ISGetIndices(rootRanks, &nranks)); 344b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 345b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 346b0a623aaSMatthew G. Knepley 3479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(rootRankSection, p, &ndof)); 3489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(rootRankSection, p, &noff)); 3499566063dSJacob Faibussowitsch for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff+n])); 350b0a623aaSMatthew G. Knepley } 3519566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(rootRanks, &nranks)); 352b0a623aaSMatthew G. Knepley /* Compute leaf-to-neighbor process connectivity */ 3539566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(leafRankSection, &pStart, &pEnd)); 3549566063dSJacob Faibussowitsch PetscCall(ISGetIndices(leafRanks, &nranks)); 355b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 356b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 357b0a623aaSMatthew G. Knepley 3589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(leafRankSection, p, &ndof)); 3599566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(leafRankSection, p, &noff)); 3609566063dSJacob Faibussowitsch for (n = 0; n < ndof; ++n) PetscCall(PetscBTSet(neighbors, nranks[noff+n])); 361b0a623aaSMatthew G. Knepley } 3629566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(leafRanks, &nranks)); 363b0a623aaSMatthew G. Knepley /* Compute leaf-to-root process connectivity */ 364b0a623aaSMatthew G. Knepley for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);} 365b0a623aaSMatthew G. Knepley /* Calculate edges */ 366b0a623aaSMatthew G. Knepley PetscBTClear(neighbors, rank); 3679852e123SBarry Smith for (proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;} 3689566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &ranksNew)); 3699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &localPointsNew)); 3709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numNeighbors, &remotePointsNew)); 3719852e123SBarry Smith for (proc = 0, n = 0; proc < size; ++proc) { 372b0a623aaSMatthew G. Knepley if (PetscBTLookup(neighbors, proc)) { 373b0a623aaSMatthew G. Knepley ranksNew[n] = proc; 374b0a623aaSMatthew G. Knepley localPointsNew[n] = proc; 375b0a623aaSMatthew G. Knepley remotePointsNew[n].index = rank; 376b0a623aaSMatthew G. Knepley remotePointsNew[n].rank = proc; 377b0a623aaSMatthew G. Knepley ++n; 378b0a623aaSMatthew G. Knepley } 379b0a623aaSMatthew G. Knepley } 3809566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&neighbors)); 3819566063dSJacob Faibussowitsch if (processRanks) PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks)); 3829566063dSJacob Faibussowitsch else PetscCall(PetscFree(ranksNew)); 383b0a623aaSMatthew G. Knepley if (sfProcess) { 3849566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess)); 3859566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF")); 3869566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*sfProcess)); 3879566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER)); 388b0a623aaSMatthew G. Knepley } 389b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 390b0a623aaSMatthew G. Knepley } 391b0a623aaSMatthew G. Knepley 392b0a623aaSMatthew G. Knepley /*@ 393b0a623aaSMatthew G. Knepley DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF. 394b0a623aaSMatthew G. Knepley 395d083f849SBarry Smith Collective on dm 396b0a623aaSMatthew G. Knepley 397b0a623aaSMatthew G. Knepley Input Parameter: 398b0a623aaSMatthew G. Knepley . dm - The DM 399b0a623aaSMatthew G. Knepley 400b0a623aaSMatthew G. Knepley Output Parameters: 401b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point 402b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 403b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 404b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 405b0a623aaSMatthew G. Knepley 406b0a623aaSMatthew G. Knepley Level: developer 407b0a623aaSMatthew G. Knepley 408db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 409b0a623aaSMatthew G. Knepley @*/ 410b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank) 411b0a623aaSMatthew G. Knepley { 412b0a623aaSMatthew G. Knepley MPI_Comm comm; 413b0a623aaSMatthew G. Knepley PetscSF sfPoint; 414b0a623aaSMatthew G. Knepley const PetscInt *rootdegree; 415b0a623aaSMatthew G. Knepley PetscInt *myrank, *remoterank; 416b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, nedges; 417b0a623aaSMatthew G. Knepley PetscMPIInt rank; 418b0a623aaSMatthew G. Knepley 419b0a623aaSMatthew G. Knepley PetscFunctionBegin; 4209566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 4219566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 4229566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 4239566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 424b0a623aaSMatthew G. Knepley /* Compute number of leaves for each root */ 4259566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) rootSection, "Root Section")); 4269566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(rootSection, pStart, pEnd)); 4279566063dSJacob Faibussowitsch PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree)); 4289566063dSJacob Faibussowitsch PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree)); 4299566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(PetscSectionSetDof(rootSection, p, rootdegree[p-pStart])); 4309566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(rootSection)); 431b0a623aaSMatthew G. Knepley /* Gather rank of each leaf to root */ 4329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(rootSection, &nedges)); 4339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(pEnd-pStart, &myrank)); 4349566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nedges, &remoterank)); 435b0a623aaSMatthew G. Knepley for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank; 4369566063dSJacob Faibussowitsch PetscCall(PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank)); 4379566063dSJacob Faibussowitsch PetscCall(PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank)); 4389566063dSJacob Faibussowitsch PetscCall(PetscFree(myrank)); 4399566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank)); 440b0a623aaSMatthew G. Knepley /* Distribute remote ranks to leaves */ 4419566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) leafSection, "Leaf Section")); 4429566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank)); 443b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 444b0a623aaSMatthew G. Knepley } 445b0a623aaSMatthew G. Knepley 446c506a872SMatthew G. Knepley #if 0 447c506a872SMatthew G. Knepley static PetscErrorCode DMPlexCopyOverlapLabels(DM dm, DM ndm) 448c506a872SMatthew G. Knepley { 449c506a872SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 450c506a872SMatthew G. Knepley DM_Plex *nmesh = (DM_Plex *) ndm->data; 451c506a872SMatthew G. Knepley 452c506a872SMatthew G. Knepley PetscFunctionBegin; 453c506a872SMatthew G. Knepley if (mesh->numOvLabels) { 454c506a872SMatthew G. Knepley const char *name; 455c506a872SMatthew G. Knepley PetscInt l; 456c506a872SMatthew G. Knepley 457c506a872SMatthew G. Knepley nmesh->numOvLabels = mesh->numOvLabels; 458c506a872SMatthew G. Knepley for (l = 0; l < mesh->numOvLabels; ++l) { 459c506a872SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject) mesh->ovLabels[l], &name)); 460c506a872SMatthew G. Knepley PetscCall(DMGetLabel(ndm, name, &nmesh->ovLabels[l])); 461c506a872SMatthew G. Knepley nmesh->ovValues[l] = mesh->ovValues[l]; 462c506a872SMatthew G. Knepley } 463c506a872SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject) mesh->ovExLabel, &name)); 464c506a872SMatthew G. Knepley PetscCall(DMGetLabel(ndm, name, &nmesh->ovExLabel)); 465c506a872SMatthew G. Knepley nmesh->ovExValue = mesh->ovExValue; 466c506a872SMatthew G. Knepley } 467c506a872SMatthew G. Knepley PetscFunctionReturn(0); 468c506a872SMatthew G. Knepley } 469c506a872SMatthew G. Knepley #endif 470c506a872SMatthew G. Knepley 471278397a0SMatthew G. Knepley /*@C 472c506a872SMatthew G. Knepley DMPlexCreateOverlapLabel - Compute a label indicating what overlap points should be sent to new processes 473b0a623aaSMatthew G. Knepley 474d083f849SBarry Smith Collective on dm 475b0a623aaSMatthew G. Knepley 476b0a623aaSMatthew G. Knepley Input Parameters: 477b0a623aaSMatthew G. Knepley + dm - The DM 47824d039d7SMichael Lange . levels - Number of overlap levels 479b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point 480b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 481b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 482b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 483b0a623aaSMatthew G. Knepley 484064ec1f3Sprj- Output Parameter: 485b4ec6ac8SBarry Smith . ovLabel - DMLabel containing remote overlap contributions as point/rank pairings 486b0a623aaSMatthew G. Knepley 487b0a623aaSMatthew G. Knepley Level: developer 488b0a623aaSMatthew G. Knepley 489c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabelFromLabels()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()` 490b0a623aaSMatthew G. Knepley @*/ 491dccdeccaSVaclav Hapla PetscErrorCode DMPlexCreateOverlapLabel(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) 492b0a623aaSMatthew G. Knepley { 493e540f424SMichael Lange MPI_Comm comm; 494b0a623aaSMatthew G. Knepley DMLabel ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */ 495874ddda9SLisandro Dalcin PetscSF sfPoint; 496b0a623aaSMatthew G. Knepley const PetscSFNode *remote; 497b0a623aaSMatthew G. Knepley const PetscInt *local; 4981fd9873aSMichael Lange const PetscInt *nrank, *rrank; 499b0a623aaSMatthew G. Knepley PetscInt *adj = NULL; 5001fd9873aSMichael Lange PetscInt pStart, pEnd, p, sStart, sEnd, nleaves, l; 5019852e123SBarry Smith PetscMPIInt rank, size; 50231bc6364SLisandro Dalcin PetscBool flg; 503b0a623aaSMatthew G. Knepley 504b0a623aaSMatthew G. Knepley PetscFunctionBegin; 5056ba1a4c7SVaclav Hapla *ovLabel = NULL; 5069566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 5079566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 5089566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 5096ba1a4c7SVaclav Hapla if (size == 1) PetscFunctionReturn(0); 5109566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 5119566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 512d1674c6dSMatthew Knepley if (!levels) { 513d1674c6dSMatthew Knepley /* Add owned points */ 5149566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 5159566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 516d1674c6dSMatthew Knepley PetscFunctionReturn(0); 517d1674c6dSMatthew Knepley } 5189566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(leafSection, &sStart, &sEnd)); 5199566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote)); 5209566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank)); 521b0a623aaSMatthew G. Knepley /* Handle leaves: shared with the root point */ 522b0a623aaSMatthew G. Knepley for (l = 0; l < nleaves; ++l) { 523b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, a; 524b0a623aaSMatthew G. Knepley 5259566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj)); 5269566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank)); 527b0a623aaSMatthew G. Knepley } 5289566063dSJacob Faibussowitsch PetscCall(ISGetIndices(rootrank, &rrank)); 5299566063dSJacob Faibussowitsch PetscCall(ISGetIndices(leafrank, &nrank)); 530b0a623aaSMatthew G. Knepley /* Handle roots */ 531b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 532b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a; 533b0a623aaSMatthew G. Knepley 534b0a623aaSMatthew G. Knepley if ((p >= sStart) && (p < sEnd)) { 535b0a623aaSMatthew G. Knepley /* Some leaves share a root with other leaves on different processes */ 5369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(leafSection, p, &neighbors)); 537b0a623aaSMatthew G. Knepley if (neighbors) { 5389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(leafSection, p, &noff)); 5399566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 540b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 541b0a623aaSMatthew G. Knepley const PetscInt remoteRank = nrank[noff+n]; 542b0a623aaSMatthew G. Knepley 543b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 5449566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 545b0a623aaSMatthew G. Knepley } 546b0a623aaSMatthew G. Knepley } 547b0a623aaSMatthew G. Knepley } 548b0a623aaSMatthew G. Knepley /* Roots are shared with leaves */ 5499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(rootSection, p, &neighbors)); 550b0a623aaSMatthew G. Knepley if (!neighbors) continue; 5519566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(rootSection, p, &noff)); 5529566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 553b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 554b0a623aaSMatthew G. Knepley const PetscInt remoteRank = rrank[noff+n]; 555b0a623aaSMatthew G. Knepley 556b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 5579566063dSJacob Faibussowitsch for (a = 0; a < adjSize; ++a) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 558b0a623aaSMatthew G. Knepley } 559b0a623aaSMatthew G. Knepley } 5609566063dSJacob Faibussowitsch PetscCall(PetscFree(adj)); 5619566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(rootrank, &rrank)); 5629566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(leafrank, &nrank)); 56324d039d7SMichael Lange /* Add additional overlap levels */ 564be200f8dSMichael Lange for (l = 1; l < levels; l++) { 565be200f8dSMichael Lange /* Propagate point donations over SF to capture remote connections */ 5669566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelPropagate(dm, ovAdjByRank)); 567be200f8dSMichael Lange /* Add next level of point donations to the label */ 5689566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelAdjacency(dm, ovAdjByRank)); 569be200f8dSMichael Lange } 57026a7d390SMatthew G. Knepley /* We require the closure in the overlap */ 5719566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank)); 5729566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg)); 573e540f424SMichael Lange if (flg) { 574825f8a23SLisandro Dalcin PetscViewer viewer; 5759566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer)); 5769566063dSJacob Faibussowitsch PetscCall(DMLabelView(ovAdjByRank, viewer)); 577b0a623aaSMatthew G. Knepley } 578874ddda9SLisandro Dalcin /* Invert sender to receiver label */ 5799566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 5809566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel)); 581a9f1d5b2SMichael Lange /* Add owned points, except for shared local points */ 5829566063dSJacob Faibussowitsch for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 583e540f424SMichael Lange for (l = 0; l < nleaves; ++l) { 5849566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(*ovLabel, local[l], rank)); 5859566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank)); 586e540f424SMichael Lange } 587e540f424SMichael Lange /* Clean up */ 5889566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&ovAdjByRank)); 589b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 590b0a623aaSMatthew G. Knepley } 59170034214SMatthew G. Knepley 592c506a872SMatthew G. Knepley static PetscErrorCode HandlePoint_Private(DM dm, PetscInt p, PetscSection section, const PetscInt ranks[], PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], DMLabel ovAdjByRank) 593c506a872SMatthew G. Knepley { 594c506a872SMatthew G. Knepley PetscInt neighbors, el; 595c506a872SMatthew G. Knepley 596c506a872SMatthew G. Knepley PetscFunctionBegin; 597c506a872SMatthew G. Knepley PetscCall(PetscSectionGetDof(section, p, &neighbors)); 598c506a872SMatthew G. Knepley if (neighbors) { 599c506a872SMatthew G. Knepley PetscInt *adj = NULL; 600c506a872SMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, noff, n, a; 601c506a872SMatthew G. Knepley PetscMPIInt rank; 602c506a872SMatthew G. Knepley 603c506a872SMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank)); 604c506a872SMatthew G. Knepley PetscCall(PetscSectionGetOffset(section, p, &noff)); 605c506a872SMatthew G. Knepley PetscCall(DMPlexGetAdjacency(dm, p, &adjSize, &adj)); 606c506a872SMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 607c506a872SMatthew G. Knepley const PetscInt remoteRank = ranks[noff+n]; 608c506a872SMatthew G. Knepley 609c506a872SMatthew G. Knepley if (remoteRank == rank) continue; 610c506a872SMatthew G. Knepley for (a = 0; a < adjSize; ++a) { 611c506a872SMatthew G. Knepley PetscBool insert = PETSC_TRUE; 612c506a872SMatthew G. Knepley 613c506a872SMatthew G. Knepley for (el = 0; el < numExLabels; ++el) { 614c506a872SMatthew G. Knepley PetscInt exVal; 615c506a872SMatthew G. Knepley PetscCall(DMLabelGetValue(exLabel[el], adj[a], &exVal)); 616c506a872SMatthew G. Knepley if (exVal == exValue[el]) {insert = PETSC_FALSE; break;} 617c506a872SMatthew G. Knepley } 618c506a872SMatthew G. Knepley if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 619c506a872SMatthew G. Knepley } 620c506a872SMatthew G. Knepley } 621c506a872SMatthew G. Knepley } 622c506a872SMatthew G. Knepley PetscFunctionReturn(0); 623c506a872SMatthew G. Knepley } 624c506a872SMatthew G. Knepley 625c506a872SMatthew G. Knepley /*@C 626c506a872SMatthew G. Knepley DMPlexCreateOverlapLabelFromLabels - Compute a label indicating what overlap points should be sent to new processes 627c506a872SMatthew G. Knepley 628c506a872SMatthew G. Knepley Collective on dm 629c506a872SMatthew G. Knepley 630c506a872SMatthew G. Knepley Input Parameters: 631c506a872SMatthew G. Knepley + dm - The DM 632c506a872SMatthew G. Knepley . numLabels - The number of labels to draw candidate points from 633c506a872SMatthew G. Knepley . label - An array of labels containing candidate points 634c506a872SMatthew G. Knepley . value - An array of label values marking the candidate points 635c506a872SMatthew G. Knepley . numExLabels - The number of labels to use for exclusion 636c506a872SMatthew G. Knepley . exLabel - An array of labels indicating points to be excluded, or NULL 637c506a872SMatthew G. Knepley . exValue - An array of label values to be excluded, or NULL 638c506a872SMatthew G. Knepley . rootSection - The number of leaves for a given root point 639c506a872SMatthew G. Knepley . rootrank - The rank of each edge into the root point 640c506a872SMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 641c506a872SMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 642c506a872SMatthew G. Knepley 643c506a872SMatthew G. Knepley Output Parameter: 644c506a872SMatthew G. Knepley . ovLabel - DMLabel containing remote overlap contributions as point/rank pairings 645c506a872SMatthew G. Knepley 646c506a872SMatthew G. Knepley Note: 647c506a872SMatthew G. Knepley The candidate points are only added to the overlap if they are adjacent to a shared point 648c506a872SMatthew G. Knepley 649c506a872SMatthew G. Knepley Level: developer 650c506a872SMatthew G. Knepley 651c506a872SMatthew G. Knepley .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexGetAdjacency()`, `DMPlexDistributeOwnership()`, `DMPlexDistribute()` 652c506a872SMatthew G. Knepley @*/ 653c506a872SMatthew G. Knepley PetscErrorCode DMPlexCreateOverlapLabelFromLabels(DM dm, PetscInt numLabels, const DMLabel label[], const PetscInt value[], 654c506a872SMatthew G. Knepley PetscInt numExLabels, const DMLabel exLabel[], const PetscInt exValue[], PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) 655c506a872SMatthew G. Knepley { 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)); 672c506a872SMatthew G. Knepley if (size == 1) PetscFunctionReturn(0); 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)); 708c506a872SMatthew G. Knepley if (exVal == exValue[el]) {insert = PETSC_FALSE; break;} 709c506a872SMatthew G. Knepley } 710c506a872SMatthew G. Knepley if (insert) PetscCall(DMLabelSetValue(ovAdjByRank, adj[a], remoteRank)); 711c506a872SMatthew G. Knepley } 712c506a872SMatthew G. Knepley } 713c506a872SMatthew G. Knepley /* Some leaves share a root with other leaves on different processes */ 714c506a872SMatthew G. Knepley HandlePoint_Private(dm, p, leafSection, nrank, numExLabels, exLabel, exValue, ovAdjByRank); 715c506a872SMatthew G. Knepley } 716c506a872SMatthew G. Knepley /* Roots are shared with leaves */ 717c506a872SMatthew G. Knepley HandlePoint_Private(dm, p, rootSection, rrank, numExLabels, exLabel, exValue, ovAdjByRank); 718c506a872SMatthew G. Knepley } 719c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(valIS, &points)); 720c506a872SMatthew G. Knepley PetscCall(ISDestroy(&valIS)); 721c506a872SMatthew G. Knepley } 722c506a872SMatthew G. Knepley PetscCall(PetscFree(adj)); 723c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(rootrank, &rrank)); 724c506a872SMatthew G. Knepley PetscCall(ISRestoreIndices(leafrank, &nrank)); 725c506a872SMatthew G. Knepley /* We require the closure in the overlap */ 726c506a872SMatthew G. Knepley PetscCall(DMPlexPartitionLabelClosure(dm, ovAdjByRank)); 727c506a872SMatthew G. Knepley PetscCall(PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg)); 728c506a872SMatthew G. Knepley if (flg) { 729c506a872SMatthew G. Knepley PetscViewer viewer; 730c506a872SMatthew G. Knepley PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer)); 731c506a872SMatthew G. Knepley PetscCall(DMLabelView(ovAdjByRank, viewer)); 732c506a872SMatthew G. Knepley } 733c506a872SMatthew G. Knepley /* Invert sender to receiver label */ 734c506a872SMatthew G. Knepley PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel)); 735c506a872SMatthew G. Knepley PetscCall(DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel)); 736c506a872SMatthew G. Knepley /* Add owned points, except for shared local points */ 737c506a872SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) PetscCall(DMLabelSetValue(*ovLabel, p, rank)); 738c506a872SMatthew G. Knepley for (l = 0; l < nleaves; ++l) { 739c506a872SMatthew G. Knepley PetscCall(DMLabelClearValue(*ovLabel, local[l], rank)); 740c506a872SMatthew G. Knepley PetscCall(DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank)); 741c506a872SMatthew G. Knepley } 742c506a872SMatthew G. Knepley /* Clean up */ 743c506a872SMatthew G. Knepley PetscCall(DMLabelDestroy(&ovAdjByRank)); 744c506a872SMatthew G. Knepley PetscFunctionReturn(0); 745c506a872SMatthew G. Knepley } 746c506a872SMatthew G. Knepley 74724cc2ca5SMatthew G. Knepley /*@C 74824cc2ca5SMatthew G. Knepley DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF 74924cc2ca5SMatthew G. Knepley 750d083f849SBarry Smith Collective on dm 75124cc2ca5SMatthew G. Knepley 75224cc2ca5SMatthew G. Knepley Input Parameters: 75324cc2ca5SMatthew G. Knepley + dm - The DM 75424cc2ca5SMatthew G. Knepley - overlapSF - The SF mapping ghost points in overlap to owner points on other processes 75524cc2ca5SMatthew G. Knepley 756064ec1f3Sprj- Output Parameter: 757a2b725a8SWilliam Gropp . migrationSF - An SF that maps original points in old locations to points in new locations 75824cc2ca5SMatthew G. Knepley 75924cc2ca5SMatthew G. Knepley Level: developer 76024cc2ca5SMatthew G. Knepley 761db781477SPatrick Sanan .seealso: `DMPlexCreateOverlapLabel()`, `DMPlexDistributeOverlap()`, `DMPlexDistribute()` 76224cc2ca5SMatthew G. Knepley @*/ 76346f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF) 76446f9b1c3SMichael Lange { 76546f9b1c3SMichael Lange MPI_Comm comm; 7669852e123SBarry Smith PetscMPIInt rank, size; 76746f9b1c3SMichael Lange PetscInt d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints; 76846f9b1c3SMichael Lange PetscInt *pointDepths, *remoteDepths, *ilocal; 76946f9b1c3SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 77046f9b1c3SMichael Lange PetscSFNode *iremote; 77146f9b1c3SMichael Lange PetscSF pointSF; 77246f9b1c3SMichael Lange const PetscInt *sharedLocal; 77346f9b1c3SMichael Lange const PetscSFNode *overlapRemote, *sharedRemote; 77446f9b1c3SMichael Lange 77546f9b1c3SMichael Lange PetscFunctionBegin; 77646f9b1c3SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 7779566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 7789566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 7799566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 7809566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 78146f9b1c3SMichael Lange 78246f9b1c3SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 7839566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote)); 7849566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths)); 78546f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 7869566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 78746f9b1c3SMichael Lange for (p=pStart; p<pEnd; p++) pointDepths[p] = d; 78846f9b1c3SMichael Lange } 78946f9b1c3SMichael Lange for (p=0; p<nleaves; p++) remoteDepths[p] = -1; 7909566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths,MPI_REPLACE)); 7919566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths,MPI_REPLACE)); 79246f9b1c3SMichael Lange 7932d4ee042Sprj- /* Count received points in each stratum and compute the internal strata shift */ 7949566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx)); 79546f9b1c3SMichael Lange for (d=0; d<dim+1; d++) depthRecv[d]=0; 79646f9b1c3SMichael Lange for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++; 79746f9b1c3SMichael Lange depthShift[dim] = 0; 79846f9b1c3SMichael Lange for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim]; 79946f9b1c3SMichael Lange for (d=1; d<dim; d++) depthShift[d] += depthRecv[0]; 80046f9b1c3SMichael Lange for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1]; 80146f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 8029566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 80346f9b1c3SMichael Lange depthIdx[d] = pStart + depthShift[d]; 80446f9b1c3SMichael Lange } 80546f9b1c3SMichael Lange 80646f9b1c3SMichael Lange /* Form the overlap SF build an SF that describes the full overlap migration SF */ 8079566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 80846f9b1c3SMichael Lange newLeaves = pEnd - pStart + nleaves; 8099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(newLeaves, &ilocal)); 8109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(newLeaves, &iremote)); 81146f9b1c3SMichael Lange /* First map local points to themselves */ 81246f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 8139566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 81446f9b1c3SMichael Lange for (p=pStart; p<pEnd; p++) { 81546f9b1c3SMichael Lange point = p + depthShift[d]; 81646f9b1c3SMichael Lange ilocal[point] = point; 81746f9b1c3SMichael Lange iremote[point].index = p; 81846f9b1c3SMichael Lange iremote[point].rank = rank; 81946f9b1c3SMichael Lange depthIdx[d]++; 82046f9b1c3SMichael Lange } 82146f9b1c3SMichael Lange } 82246f9b1c3SMichael Lange 82346f9b1c3SMichael Lange /* Add in the remote roots for currently shared points */ 8249566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &pointSF)); 8259566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote)); 82646f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 8279566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 82846f9b1c3SMichael Lange for (p=0; p<numSharedPoints; p++) { 82946f9b1c3SMichael Lange if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) { 83046f9b1c3SMichael Lange point = sharedLocal[p] + depthShift[d]; 83146f9b1c3SMichael Lange iremote[point].index = sharedRemote[p].index; 83246f9b1c3SMichael Lange iremote[point].rank = sharedRemote[p].rank; 83346f9b1c3SMichael Lange } 83446f9b1c3SMichael Lange } 83546f9b1c3SMichael Lange } 83646f9b1c3SMichael Lange 83746f9b1c3SMichael Lange /* Now add the incoming overlap points */ 83846f9b1c3SMichael Lange for (p=0; p<nleaves; p++) { 83946f9b1c3SMichael Lange point = depthIdx[remoteDepths[p]]; 84046f9b1c3SMichael Lange ilocal[point] = point; 84146f9b1c3SMichael Lange iremote[point].index = overlapRemote[p].index; 84246f9b1c3SMichael Lange iremote[point].rank = overlapRemote[p].rank; 84346f9b1c3SMichael Lange depthIdx[remoteDepths[p]]++; 84446f9b1c3SMichael Lange } 8459566063dSJacob Faibussowitsch PetscCall(PetscFree2(pointDepths,remoteDepths)); 84646f9b1c3SMichael Lange 8479566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, migrationSF)); 8489566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF")); 8499566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*migrationSF)); 8509566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 8519566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER)); 85246f9b1c3SMichael Lange 8539566063dSJacob Faibussowitsch PetscCall(PetscFree3(depthRecv, depthShift, depthIdx)); 85470034214SMatthew G. Knepley PetscFunctionReturn(0); 85570034214SMatthew G. Knepley } 85670034214SMatthew G. Knepley 857a9f1d5b2SMichael Lange /*@ 858f0e73a3dSToby Isaac DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification. 859a9f1d5b2SMichael Lange 860064ec1f3Sprj- Input Parameters: 861a9f1d5b2SMichael Lange + dm - The DM 862a9f1d5b2SMichael Lange - sf - A star forest with non-ordered leaves, usually defining a DM point migration 863a9f1d5b2SMichael Lange 864a9f1d5b2SMichael Lange Output Parameter: 865a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified 866a9f1d5b2SMichael Lange 867412e9a14SMatthew G. Knepley Note: 868412e9a14SMatthew G. Knepley This lexicographically sorts by (depth, cellType) 869412e9a14SMatthew G. Knepley 870a9f1d5b2SMichael Lange Level: developer 871a9f1d5b2SMichael Lange 872db781477SPatrick Sanan .seealso: `DMPlexPartitionLabelCreateSF()`, `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 873a9f1d5b2SMichael Lange @*/ 874a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF) 875a9f1d5b2SMichael Lange { 876a9f1d5b2SMichael Lange MPI_Comm comm; 8779852e123SBarry Smith PetscMPIInt rank, size; 878412e9a14SMatthew G. Knepley PetscInt d, ldepth, depth, dim, p, pStart, pEnd, nroots, nleaves; 879412e9a14SMatthew G. Knepley PetscSFNode *pointDepths, *remoteDepths; 880412e9a14SMatthew G. Knepley PetscInt *ilocal; 881a9f1d5b2SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 882412e9a14SMatthew G. Knepley PetscInt *ctRecv, *ctShift, *ctIdx; 883a9f1d5b2SMichael Lange const PetscSFNode *iremote; 884a9f1d5b2SMichael Lange 885a9f1d5b2SMichael Lange PetscFunctionBegin; 886a9f1d5b2SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 8889566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 8899566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 8909566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &ldepth)); 8919566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 8921c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm)); 89363a3b9bcSJacob Faibussowitsch PetscCheck(!(ldepth >= 0) || !(depth != ldepth),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, ldepth, depth); 8949566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_PartStratSF,dm,0,0,0)); 895a9f1d5b2SMichael Lange 896a9f1d5b2SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 8979566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote)); 8989566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths)); 8997fab53ddSMatthew G. Knepley for (d = 0; d < depth+1; ++d) { 9009566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 901f0e73a3dSToby Isaac for (p = pStart; p < pEnd; ++p) { 902412e9a14SMatthew G. Knepley DMPolytopeType ct; 903f0e73a3dSToby Isaac 9049566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, p, &ct)); 905412e9a14SMatthew G. Knepley pointDepths[p].index = d; 906412e9a14SMatthew G. Knepley pointDepths[p].rank = ct; 907f0e73a3dSToby Isaac } 908412e9a14SMatthew G. Knepley } 909412e9a14SMatthew G. Knepley for (p = 0; p < nleaves; ++p) {remoteDepths[p].index = -1; remoteDepths[p].rank = -1;} 9109566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sf, MPIU_2INT, pointDepths, remoteDepths,MPI_REPLACE)); 9119566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_2INT, pointDepths, remoteDepths,MPI_REPLACE)); 912412e9a14SMatthew G. Knepley /* Count received points in each stratum and compute the internal strata shift */ 9139566063dSJacob Faibussowitsch PetscCall(PetscCalloc6(depth+1, &depthRecv, depth+1, &depthShift, depth+1, &depthIdx, DM_NUM_POLYTOPES, &ctRecv, DM_NUM_POLYTOPES, &ctShift, DM_NUM_POLYTOPES, &ctIdx)); 914412e9a14SMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 915412e9a14SMatthew G. Knepley if (remoteDepths[p].rank < 0) { 916412e9a14SMatthew G. Knepley ++depthRecv[remoteDepths[p].index]; 917412e9a14SMatthew G. Knepley } else { 918412e9a14SMatthew G. Knepley ++ctRecv[remoteDepths[p].rank]; 919412e9a14SMatthew G. Knepley } 920412e9a14SMatthew G. Knepley } 921412e9a14SMatthew G. Knepley { 922412e9a14SMatthew G. Knepley PetscInt depths[4], dims[4], shift = 0, i, c; 923412e9a14SMatthew G. Knepley 9248238f61eSMatthew G. Knepley /* Cells (depth), Vertices (0), Faces (depth-1), Edges (1) 9258238f61eSMatthew G. Knepley Consider DM_POLYTOPE_FV_GHOST and DM_POLYTOPE_INTERIOR_GHOST as cells 9268238f61eSMatthew G. Knepley */ 927412e9a14SMatthew G. Knepley depths[0] = depth; depths[1] = 0; depths[2] = depth-1; depths[3] = 1; 928412e9a14SMatthew G. Knepley dims[0] = dim; dims[1] = 0; dims[2] = dim-1; dims[3] = 1; 929412e9a14SMatthew G. Knepley for (i = 0; i <= depth; ++i) { 930412e9a14SMatthew G. Knepley const PetscInt dep = depths[i]; 931412e9a14SMatthew G. Knepley const PetscInt dim = dims[i]; 932412e9a14SMatthew G. Knepley 933412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 9348238f61eSMatthew G. Knepley if (DMPolytopeTypeGetDim((DMPolytopeType) c) != dim && !(i == 0 && (c == DM_POLYTOPE_FV_GHOST || c == DM_POLYTOPE_INTERIOR_GHOST))) continue; 935412e9a14SMatthew G. Knepley ctShift[c] = shift; 936412e9a14SMatthew G. Knepley shift += ctRecv[c]; 937412e9a14SMatthew G. Knepley } 938412e9a14SMatthew G. Knepley depthShift[dep] = shift; 939412e9a14SMatthew G. Knepley shift += depthRecv[dep]; 940412e9a14SMatthew G. Knepley } 941412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 942412e9a14SMatthew G. Knepley const PetscInt ctDim = DMPolytopeTypeGetDim((DMPolytopeType) c); 943412e9a14SMatthew G. Knepley 9448238f61eSMatthew G. Knepley if ((ctDim < 0 || ctDim > dim) && (c != DM_POLYTOPE_FV_GHOST && c != DM_POLYTOPE_INTERIOR_GHOST)) { 945412e9a14SMatthew G. Knepley ctShift[c] = shift; 946412e9a14SMatthew G. Knepley shift += ctRecv[c]; 947412e9a14SMatthew G. Knepley } 948412e9a14SMatthew G. Knepley } 949412e9a14SMatthew G. Knepley } 950a9f1d5b2SMichael Lange /* Derive a new local permutation based on stratified indices */ 9519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &ilocal)); 9527fab53ddSMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 953412e9a14SMatthew G. Knepley const PetscInt dep = remoteDepths[p].index; 954412e9a14SMatthew G. Knepley const DMPolytopeType ct = (DMPolytopeType) remoteDepths[p].rank; 9557fab53ddSMatthew G. Knepley 956412e9a14SMatthew G. Knepley if ((PetscInt) ct < 0) { 9577fab53ddSMatthew G. Knepley ilocal[p] = depthShift[dep] + depthIdx[dep]; 958412e9a14SMatthew G. Knepley ++depthIdx[dep]; 959412e9a14SMatthew G. Knepley } else { 960412e9a14SMatthew G. Knepley ilocal[p] = ctShift[ct] + ctIdx[ct]; 961412e9a14SMatthew G. Knepley ++ctIdx[ct]; 962412e9a14SMatthew G. Knepley } 963a9f1d5b2SMichael Lange } 9649566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, migrationSF)); 9659566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *migrationSF, "Migration SF")); 9669566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, (PetscSFNode*)iremote, PETSC_COPY_VALUES)); 9679566063dSJacob Faibussowitsch PetscCall(PetscFree2(pointDepths,remoteDepths)); 9689566063dSJacob Faibussowitsch PetscCall(PetscFree6(depthRecv, depthShift, depthIdx, ctRecv, ctShift, ctIdx)); 9699566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_PartStratSF,dm,0,0,0)); 970a9f1d5b2SMichael Lange PetscFunctionReturn(0); 971a9f1d5b2SMichael Lange } 972a9f1d5b2SMichael Lange 97370034214SMatthew G. Knepley /*@ 97470034214SMatthew G. Knepley DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 97570034214SMatthew G. Knepley 976d083f849SBarry Smith Collective on dm 97770034214SMatthew G. Knepley 97870034214SMatthew G. Knepley Input Parameters: 97970034214SMatthew G. Knepley + dm - The DMPlex object 98070034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 98170034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 982cb15cd0eSMatthew G. Knepley - originalVec - The existing data in a local vector 98370034214SMatthew G. Knepley 98470034214SMatthew G. Knepley Output Parameters: 98570034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 986cb15cd0eSMatthew G. Knepley - newVec - The new data in a local vector 98770034214SMatthew G. Knepley 98870034214SMatthew G. Knepley Level: developer 98970034214SMatthew G. Knepley 990db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeFieldIS()`, `DMPlexDistributeData()` 99170034214SMatthew G. Knepley @*/ 99270034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec) 99370034214SMatthew G. Knepley { 99470034214SMatthew G. Knepley PetscSF fieldSF; 99570034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 99670034214SMatthew G. Knepley PetscScalar *originalValues, *newValues; 99770034214SMatthew G. Knepley 99870034214SMatthew G. Knepley PetscFunctionBegin; 9999566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0)); 10009566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 100170034214SMatthew G. Knepley 10029566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 10039566063dSJacob Faibussowitsch PetscCall(VecSetSizes(newVec, fieldSize, PETSC_DETERMINE)); 10049566063dSJacob Faibussowitsch PetscCall(VecSetType(newVec,dm->vectype)); 100570034214SMatthew G. Knepley 10069566063dSJacob Faibussowitsch PetscCall(VecGetArray(originalVec, &originalValues)); 10079566063dSJacob Faibussowitsch PetscCall(VecGetArray(newVec, &newValues)); 10089566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 10099566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 10109566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues,MPI_REPLACE)); 10119566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues,MPI_REPLACE)); 10129566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 10139566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(newVec, &newValues)); 10149566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(originalVec, &originalValues)); 10159566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0)); 101670034214SMatthew G. Knepley PetscFunctionReturn(0); 101770034214SMatthew G. Knepley } 101870034214SMatthew G. Knepley 101970034214SMatthew G. Knepley /*@ 102070034214SMatthew G. Knepley DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 102170034214SMatthew G. Knepley 1022d083f849SBarry Smith Collective on dm 102370034214SMatthew G. Knepley 102470034214SMatthew G. Knepley Input Parameters: 102570034214SMatthew G. Knepley + dm - The DMPlex object 102670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 102770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 102870034214SMatthew G. Knepley - originalIS - The existing data 102970034214SMatthew G. Knepley 103070034214SMatthew G. Knepley Output Parameters: 103170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 103270034214SMatthew G. Knepley - newIS - The new data 103370034214SMatthew G. Knepley 103470034214SMatthew G. Knepley Level: developer 103570034214SMatthew G. Knepley 1036db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()`, `DMPlexDistributeData()` 103770034214SMatthew G. Knepley @*/ 103870034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS) 103970034214SMatthew G. Knepley { 104070034214SMatthew G. Knepley PetscSF fieldSF; 104170034214SMatthew G. Knepley PetscInt *newValues, *remoteOffsets, fieldSize; 104270034214SMatthew G. Knepley const PetscInt *originalValues; 104370034214SMatthew G. Knepley 104470034214SMatthew G. Knepley PetscFunctionBegin; 10459566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0)); 10469566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 104770034214SMatthew G. Knepley 10489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 10499566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fieldSize, &newValues)); 105070034214SMatthew G. Knepley 10519566063dSJacob Faibussowitsch PetscCall(ISGetIndices(originalIS, &originalValues)); 10529566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 10539566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 10549566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues,MPI_REPLACE)); 10559566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues,MPI_REPLACE)); 10569566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 10579566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(originalIS, &originalValues)); 10589566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS)); 10599566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0)); 106070034214SMatthew G. Knepley PetscFunctionReturn(0); 106170034214SMatthew G. Knepley } 106270034214SMatthew G. Knepley 106370034214SMatthew G. Knepley /*@ 106470034214SMatthew G. Knepley DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 106570034214SMatthew G. Knepley 1066d083f849SBarry Smith Collective on dm 106770034214SMatthew G. Knepley 106870034214SMatthew G. Knepley Input Parameters: 106970034214SMatthew G. Knepley + dm - The DMPlex object 107070034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 107170034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 107270034214SMatthew G. Knepley . datatype - The type of data 107370034214SMatthew G. Knepley - originalData - The existing data 107470034214SMatthew G. Knepley 107570034214SMatthew G. Knepley Output Parameters: 107670034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout 107770034214SMatthew G. Knepley - newData - The new data 107870034214SMatthew G. Knepley 107970034214SMatthew G. Knepley Level: developer 108070034214SMatthew G. Knepley 1081db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeField()` 108270034214SMatthew G. Knepley @*/ 108370034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData) 108470034214SMatthew G. Knepley { 108570034214SMatthew G. Knepley PetscSF fieldSF; 108670034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 108770034214SMatthew G. Knepley PetscMPIInt dataSize; 108870034214SMatthew G. Knepley 108970034214SMatthew G. Knepley PetscFunctionBegin; 10909566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0)); 10919566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection)); 109270034214SMatthew G. Knepley 10939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newSection, &fieldSize)); 10949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_size(datatype, &dataSize)); 10959566063dSJacob Faibussowitsch PetscCall(PetscMalloc(fieldSize * dataSize, newData)); 109670034214SMatthew G. Knepley 10979566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF)); 10989566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 10999566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(fieldSF, datatype, originalData, *newData,MPI_REPLACE)); 11009566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(fieldSF, datatype, originalData, *newData,MPI_REPLACE)); 11019566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&fieldSF)); 11029566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0)); 110370034214SMatthew G. Knepley PetscFunctionReturn(0); 110470034214SMatthew G. Knepley } 110570034214SMatthew G. Knepley 110624cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 1107cc71bff1SMichael Lange { 1108cc71bff1SMichael Lange DM_Plex *pmesh = (DM_Plex*) (dmParallel)->data; 1109cc71bff1SMichael Lange MPI_Comm comm; 1110cc71bff1SMichael Lange PetscSF coneSF; 1111cc71bff1SMichael Lange PetscSection originalConeSection, newConeSection; 1112ac04eaf7SToby Isaac PetscInt *remoteOffsets, *cones, *globCones, *newCones, newConesSize; 1113cc71bff1SMichael Lange PetscBool flg; 1114cc71bff1SMichael Lange 1115cc71bff1SMichael Lange PetscFunctionBegin; 1116cc71bff1SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11170c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 11189566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0)); 1119cc71bff1SMichael Lange /* Distribute cone section */ 11209566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 11219566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSection(dm, &originalConeSection)); 11229566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSection(dmParallel, &newConeSection)); 11239566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection)); 11249566063dSJacob Faibussowitsch PetscCall(DMSetUp(dmParallel)); 1125cc71bff1SMichael Lange /* Communicate and renumber cones */ 11269566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF)); 11279566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 11289566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dm, &cones)); 1129ac04eaf7SToby Isaac if (original) { 1130ac04eaf7SToby Isaac PetscInt numCones; 1131ac04eaf7SToby Isaac 11329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(originalConeSection,&numCones)); 11339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCones,&globCones)); 11349566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones)); 1135367003a6SStefano Zampini } else { 1136ac04eaf7SToby Isaac globCones = cones; 1137ac04eaf7SToby Isaac } 11389566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dmParallel, &newCones)); 11399566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones,MPI_REPLACE)); 11409566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones,MPI_REPLACE)); 11411baa6e33SBarry Smith if (original) PetscCall(PetscFree(globCones)); 11429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newConeSection, &newConesSize)); 11439566063dSJacob Faibussowitsch PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones)); 114476bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 11453533c52bSMatthew G. Knepley PetscInt p; 11463533c52bSMatthew G. Knepley PetscBool valid = PETSC_TRUE; 11473533c52bSMatthew G. Knepley for (p = 0; p < newConesSize; ++p) { 114863a3b9bcSJacob Faibussowitsch if (newCones[p] < 0) {valid = PETSC_FALSE; PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Point %" PetscInt_FMT " not in overlap SF\n", PetscGlobalRank,p));} 11493533c52bSMatthew G. Knepley } 115028b400f6SJacob Faibussowitsch PetscCheck(valid,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 11513533c52bSMatthew G. Knepley } 11529566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg)); 1153cc71bff1SMichael Lange if (flg) { 11549566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Serial Cone Section:\n")); 11559566063dSJacob Faibussowitsch PetscCall(PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_(comm))); 11569566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Parallel Cone Section:\n")); 11579566063dSJacob Faibussowitsch PetscCall(PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_(comm))); 11589566063dSJacob Faibussowitsch PetscCall(PetscSFView(coneSF, NULL)); 1159cc71bff1SMichael Lange } 11609566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientations(dm, &cones)); 11619566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientations(dmParallel, &newCones)); 11629566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones,MPI_REPLACE)); 11639566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones,MPI_REPLACE)); 11649566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&coneSF)); 11659566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0)); 1166eaf898f9SPatrick Sanan /* Create supports and stratify DMPlex */ 1167cc71bff1SMichael Lange { 1168cc71bff1SMichael Lange PetscInt pStart, pEnd; 1169cc71bff1SMichael Lange 11709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd)); 11719566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pmesh->supportSection, pStart, pEnd)); 1172cc71bff1SMichael Lange } 11739566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dmParallel)); 11749566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dmParallel)); 11751cf84007SMatthew G. Knepley { 11761cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 11771cf84007SMatthew G. Knepley 11789566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 11799566063dSJacob Faibussowitsch PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure)); 11809566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 11819566063dSJacob Faibussowitsch PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors)); 11821cf84007SMatthew G. Knepley } 1183cc71bff1SMichael Lange PetscFunctionReturn(0); 1184cc71bff1SMichael Lange } 1185cc71bff1SMichael Lange 118624cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel) 11870df0e737SMichael Lange { 11880df0e737SMichael Lange MPI_Comm comm; 11899318fe57SMatthew G. Knepley DM cdm, cdmParallel; 11900df0e737SMichael Lange PetscSection originalCoordSection, newCoordSection; 11910df0e737SMichael Lange Vec originalCoordinates, newCoordinates; 11920df0e737SMichael Lange PetscInt bs; 11930df0e737SMichael Lange const char *name; 11944fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 11950df0e737SMichael Lange 11960df0e737SMichael Lange PetscFunctionBegin; 11970df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11980c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 11990df0e737SMichael Lange 12009566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12016858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 12026858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dmParallel, &cdmParallel)); 12036858538eSMatthew G. Knepley PetscCall(DMCopyDisc(cdm, cdmParallel)); 12049566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &originalCoordSection)); 12059566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dmParallel, &newCoordSection)); 12069566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &originalCoordinates)); 12070df0e737SMichael Lange if (originalCoordinates) { 12089566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates)); 12099566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject) originalCoordinates, &name)); 12109566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) newCoordinates, name)); 12110df0e737SMichael Lange 12129566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates)); 12139566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmParallel, newCoordinates)); 12149566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(originalCoordinates, &bs)); 12159566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(newCoordinates, bs)); 12169566063dSJacob Faibussowitsch PetscCall(VecDestroy(&newCoordinates)); 12170df0e737SMichael Lange } 12186858538eSMatthew G. Knepley 12196858538eSMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12204fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L)); 12214fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dmParallel, maxCell, Lstart, L)); 12226858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 12236858538eSMatthew G. Knepley if (cdm) { 12246858538eSMatthew G. Knepley PetscCall(DMClone(dmParallel, &cdmParallel)); 12256858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateDM(dmParallel, cdmParallel)); 12269566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(cdm, cdmParallel)); 12276858538eSMatthew G. Knepley PetscCall(DMDestroy(&cdmParallel)); 12286858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateSection(dm, &originalCoordSection)); 12296858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dm, &originalCoordinates)); 12306858538eSMatthew G. Knepley PetscCall(PetscSectionCreate(comm, &newCoordSection)); 12316858538eSMatthew G. Knepley if (originalCoordinates) { 12326858538eSMatthew G. Knepley PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates)); 12336858538eSMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject) originalCoordinates, &name)); 12346858538eSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject) newCoordinates, name)); 12356858538eSMatthew G. Knepley 12366858538eSMatthew G. Knepley PetscCall(DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates)); 12376858538eSMatthew G. Knepley PetscCall(VecGetBlockSize(originalCoordinates, &bs)); 12386858538eSMatthew G. Knepley PetscCall(VecSetBlockSize(newCoordinates, bs)); 12396858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateSection(dmParallel, bs, newCoordSection)); 12406858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dmParallel, newCoordinates)); 12416858538eSMatthew G. Knepley PetscCall(VecDestroy(&newCoordinates)); 12426858538eSMatthew G. Knepley } 12436858538eSMatthew G. Knepley PetscCall(PetscSectionDestroy(&newCoordSection)); 12446858538eSMatthew G. Knepley } 12450df0e737SMichael Lange PetscFunctionReturn(0); 12460df0e737SMichael Lange } 12470df0e737SMichael Lange 124824cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel) 12490df0e737SMichael Lange { 1250df0420ecSMatthew G. Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 12510df0e737SMichael Lange MPI_Comm comm; 12527980c9d4SMatthew G. Knepley DMLabel depthLabel; 12530df0e737SMichael Lange PetscMPIInt rank; 12547980c9d4SMatthew G. Knepley PetscInt depth, d, numLabels, numLocalLabels, l; 1255df0420ecSMatthew G. Knepley PetscBool hasLabels = PETSC_FALSE, lsendDepth, sendDepth; 1256df0420ecSMatthew G. Knepley PetscObjectState depthState = -1; 12570df0e737SMichael Lange 12580df0e737SMichael Lange PetscFunctionBegin; 12590df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 12600c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 12610c86c063SLisandro Dalcin 12629566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0)); 12639566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12649566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 12650df0e737SMichael Lange 1266df0420ecSMatthew G. Knepley /* If the user has changed the depth label, communicate it instead */ 12679566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 12689566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 12699566063dSJacob Faibussowitsch if (depthLabel) PetscCall(PetscObjectStateGet((PetscObject) depthLabel, &depthState)); 1270df0420ecSMatthew G. Knepley lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE; 12711c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm)); 1272df0420ecSMatthew G. Knepley if (sendDepth) { 12739566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dmParallel, &dmParallel->depthLabel)); 12749566063dSJacob Faibussowitsch PetscCall(DMRemoveLabelBySelf(dmParallel, &dmParallel->depthLabel, PETSC_FALSE)); 1275df0420ecSMatthew G. Knepley } 1276d995df53SMatthew G. Knepley /* Everyone must have either the same number of labels, or none */ 12779566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &numLocalLabels)); 1278d995df53SMatthew G. Knepley numLabels = numLocalLabels; 12799566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm)); 1280627847f0SMatthew G. Knepley if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE; 12815d80c0bfSVaclav Hapla for (l = 0; l < numLabels; ++l) { 1282bdd2d751SMichael Lange DMLabel label = NULL, labelNew = NULL; 128383e10cb3SLisandro Dalcin PetscBool isDepth, lisOutput = PETSC_TRUE, isOutput; 1284d67d17b1SMatthew G. Knepley const char *name = NULL; 12850df0e737SMichael Lange 1286d67d17b1SMatthew G. Knepley if (hasLabels) { 12879566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm, l, &label)); 12880df0e737SMichael Lange /* Skip "depth" because it is recreated */ 12899566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject) label, &name)); 12909566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "depth", &isDepth)); 1291d67d17b1SMatthew G. Knepley } 12929566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm)); 129383e10cb3SLisandro Dalcin if (isDepth && !sendDepth) continue; 12949566063dSJacob Faibussowitsch PetscCall(DMLabelDistribute(label, migrationSF, &labelNew)); 129583e10cb3SLisandro Dalcin if (isDepth) { 12967980c9d4SMatthew G. Knepley /* Put in any missing strata which can occur if users are managing the depth label themselves */ 12977980c9d4SMatthew G. Knepley PetscInt gdepth; 12987980c9d4SMatthew G. Knepley 12991c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm)); 130063a3b9bcSJacob Faibussowitsch PetscCheck(!(depth >= 0) || !(gdepth != depth),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %" PetscInt_FMT " != %" PetscInt_FMT, depth, gdepth); 13017980c9d4SMatthew G. Knepley for (d = 0; d <= gdepth; ++d) { 13027980c9d4SMatthew G. Knepley PetscBool has; 13037980c9d4SMatthew G. Knepley 13049566063dSJacob Faibussowitsch PetscCall(DMLabelHasStratum(labelNew, d, &has)); 13059566063dSJacob Faibussowitsch if (!has) PetscCall(DMLabelAddStratum(labelNew, d)); 13067980c9d4SMatthew G. Knepley } 13077980c9d4SMatthew G. Knepley } 13089566063dSJacob Faibussowitsch PetscCall(DMAddLabel(dmParallel, labelNew)); 130983e10cb3SLisandro Dalcin /* Put the output flag in the new label */ 13109566063dSJacob Faibussowitsch if (hasLabels) PetscCall(DMGetLabelOutput(dm, name, &lisOutput)); 13111c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm)); 13129566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject) labelNew, &name)); 13139566063dSJacob Faibussowitsch PetscCall(DMSetLabelOutput(dmParallel, name, isOutput)); 13149566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&labelNew)); 13150df0e737SMichael Lange } 1316695799ffSMatthew G. Knepley { 1317695799ffSMatthew G. Knepley DMLabel ctLabel; 1318695799ffSMatthew G. Knepley 1319695799ffSMatthew G. Knepley // Reset label for fast lookup 1320695799ffSMatthew G. Knepley PetscCall(DMPlexGetCellTypeLabel(dmParallel, &ctLabel)); 1321695799ffSMatthew G. Knepley PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel)); 1322695799ffSMatthew G. Knepley } 13239566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0)); 13240df0e737SMichael Lange PetscFunctionReturn(0); 13250df0e737SMichael Lange } 13260df0e737SMichael Lange 132724cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 1328a6f36705SMichael Lange { 132915078cd4SMichael Lange DM_Plex *mesh = (DM_Plex*) dm->data; 133015078cd4SMichael Lange DM_Plex *pmesh = (DM_Plex*) (dmParallel)->data; 1331a6f36705SMichael Lange MPI_Comm comm; 1332a6f36705SMichael Lange DM refTree; 1333a6f36705SMichael Lange PetscSection origParentSection, newParentSection; 1334a6f36705SMichael Lange PetscInt *origParents, *origChildIDs; 1335a6f36705SMichael Lange PetscBool flg; 1336a6f36705SMichael Lange 1337a6f36705SMichael Lange PetscFunctionBegin; 1338a6f36705SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 13390c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 13409566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 1341a6f36705SMichael Lange 1342a6f36705SMichael Lange /* Set up tree */ 13439566063dSJacob Faibussowitsch PetscCall(DMPlexGetReferenceTree(dm,&refTree)); 13449566063dSJacob Faibussowitsch PetscCall(DMPlexSetReferenceTree(dmParallel,refTree)); 13459566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL)); 1346a6f36705SMichael Lange if (origParentSection) { 1347a6f36705SMichael Lange PetscInt pStart, pEnd; 134808633170SToby Isaac PetscInt *newParents, *newChildIDs, *globParents; 1349a6f36705SMichael Lange PetscInt *remoteOffsetsParents, newParentSize; 1350a6f36705SMichael Lange PetscSF parentSF; 1351a6f36705SMichael Lange 13529566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd)); 13539566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection)); 13549566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(newParentSection,pStart,pEnd)); 13559566063dSJacob Faibussowitsch PetscCall(PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection)); 13569566063dSJacob Faibussowitsch PetscCall(PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF)); 13579566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsetsParents)); 13589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newParentSection,&newParentSize)); 13599566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs)); 136008633170SToby Isaac if (original) { 136108633170SToby Isaac PetscInt numParents; 136208633170SToby Isaac 13639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(origParentSection,&numParents)); 13649566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numParents,&globParents)); 13659566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents)); 136608633170SToby Isaac } 136708633170SToby Isaac else { 136808633170SToby Isaac globParents = origParents; 136908633170SToby Isaac } 13709566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents,MPI_REPLACE)); 13719566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents,MPI_REPLACE)); 13721baa6e33SBarry Smith if (original) PetscCall(PetscFree(globParents)); 13739566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs,MPI_REPLACE)); 13749566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs,MPI_REPLACE)); 13759566063dSJacob Faibussowitsch PetscCall(ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents)); 137676bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 13774a54e071SToby Isaac PetscInt p; 13784a54e071SToby Isaac PetscBool valid = PETSC_TRUE; 13794a54e071SToby Isaac for (p = 0; p < newParentSize; ++p) { 138063a3b9bcSJacob Faibussowitsch if (newParents[p] < 0) {valid = PETSC_FALSE; PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " not in overlap SF\n", p));} 13814a54e071SToby Isaac } 138228b400f6SJacob Faibussowitsch PetscCheck(valid,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 13834a54e071SToby Isaac } 13849566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg)); 1385a6f36705SMichael Lange if (flg) { 13869566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Serial Parent Section: \n")); 13879566063dSJacob Faibussowitsch PetscCall(PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_(comm))); 13889566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Parallel Parent Section: \n")); 13899566063dSJacob Faibussowitsch PetscCall(PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_(comm))); 13909566063dSJacob Faibussowitsch PetscCall(PetscSFView(parentSF, NULL)); 1391a6f36705SMichael Lange } 13929566063dSJacob Faibussowitsch PetscCall(DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs)); 13939566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&newParentSection)); 13949566063dSJacob Faibussowitsch PetscCall(PetscFree2(newParents,newChildIDs)); 13959566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&parentSF)); 1396a6f36705SMichael Lange } 139715078cd4SMichael Lange pmesh->useAnchors = mesh->useAnchors; 1398a6f36705SMichael Lange PetscFunctionReturn(0); 1399a6f36705SMichael Lange } 14000df0e737SMichael Lange 140124cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel) 14024eca1733SMichael Lange { 14039852e123SBarry Smith PetscMPIInt rank, size; 14044eca1733SMichael Lange MPI_Comm comm; 14054eca1733SMichael Lange 14064eca1733SMichael Lange PetscFunctionBegin; 14074eca1733SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14080c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 14094eca1733SMichael Lange 14104eca1733SMichael Lange /* Create point SF for parallel mesh */ 14119566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0)); 14129566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 14139566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 14149566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 14154eca1733SMichael Lange { 14164eca1733SMichael Lange const PetscInt *leaves; 14174eca1733SMichael Lange PetscSFNode *remotePoints, *rowners, *lowners; 14184eca1733SMichael Lange PetscInt numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints; 14194eca1733SMichael Lange PetscInt pStart, pEnd; 14204eca1733SMichael Lange 14219566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dmParallel, &pStart, &pEnd)); 14229566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL)); 14239566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(numRoots,&rowners,numLeaves,&lowners)); 14244eca1733SMichael Lange for (p=0; p<numRoots; p++) { 14254eca1733SMichael Lange rowners[p].rank = -1; 14264eca1733SMichael Lange rowners[p].index = -1; 14274eca1733SMichael Lange } 14289566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE)); 14299566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE)); 14304eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 14314eca1733SMichael Lange if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */ 14324eca1733SMichael Lange lowners[p].rank = rank; 14334eca1733SMichael Lange lowners[p].index = leaves ? leaves[p] : p; 14344eca1733SMichael Lange } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */ 14354eca1733SMichael Lange lowners[p].rank = -2; 14364eca1733SMichael Lange lowners[p].index = -2; 14374eca1733SMichael Lange } 14384eca1733SMichael Lange } 14394eca1733SMichael Lange for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */ 14404eca1733SMichael Lange rowners[p].rank = -3; 14414eca1733SMichael Lange rowners[p].index = -3; 14424eca1733SMichael Lange } 14439566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC)); 14449566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC)); 14459566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE)); 14469566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE)); 14474eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 14481dca8a05SBarry Smith PetscCheck(lowners[p].rank >= 0 && lowners[p].index >= 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed"); 14494eca1733SMichael Lange if (lowners[p].rank != rank) ++numGhostPoints; 14504eca1733SMichael Lange } 14519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGhostPoints, &ghostPoints)); 14529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGhostPoints, &remotePoints)); 14534eca1733SMichael Lange for (p = 0, gp = 0; p < numLeaves; ++p) { 14544eca1733SMichael Lange if (lowners[p].rank != rank) { 14554eca1733SMichael Lange ghostPoints[gp] = leaves ? leaves[p] : p; 14564eca1733SMichael Lange remotePoints[gp].rank = lowners[p].rank; 14574eca1733SMichael Lange remotePoints[gp].index = lowners[p].index; 14584eca1733SMichael Lange ++gp; 14594eca1733SMichael Lange } 14604eca1733SMichael Lange } 14619566063dSJacob Faibussowitsch PetscCall(PetscFree2(rowners,lowners)); 14629566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER)); 14639566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions((dmParallel)->sf)); 14644eca1733SMichael Lange } 14651cf84007SMatthew G. Knepley { 14661cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 14671cf84007SMatthew G. Knepley 14689566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 14699566063dSJacob Faibussowitsch PetscCall(DMSetBasicAdjacency(dmParallel, useCone, useClosure)); 14709566063dSJacob Faibussowitsch PetscCall(DMPlexGetAdjacencyUseAnchors(dm, &useAnchors)); 14719566063dSJacob Faibussowitsch PetscCall(DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors)); 14721cf84007SMatthew G. Knepley } 14739566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0)); 14744eca1733SMichael Lange PetscFunctionReturn(0); 14754eca1733SMichael Lange } 14764eca1733SMichael Lange 147798ba2d7fSLawrence Mitchell /*@ 147898ba2d7fSLawrence Mitchell DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition? 147998ba2d7fSLawrence Mitchell 148098ba2d7fSLawrence Mitchell Input Parameters: 148198ba2d7fSLawrence Mitchell + dm - The DMPlex object 148298ba2d7fSLawrence Mitchell - flg - Balance the partition? 148398ba2d7fSLawrence Mitchell 148498ba2d7fSLawrence Mitchell Level: intermediate 148598ba2d7fSLawrence Mitchell 1486db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetPartitionBalance()` 148798ba2d7fSLawrence Mitchell @*/ 148898ba2d7fSLawrence Mitchell PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg) 148998ba2d7fSLawrence Mitchell { 149098ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 149198ba2d7fSLawrence Mitchell 149298ba2d7fSLawrence Mitchell PetscFunctionBegin; 149398ba2d7fSLawrence Mitchell mesh->partitionBalance = flg; 149498ba2d7fSLawrence Mitchell PetscFunctionReturn(0); 149598ba2d7fSLawrence Mitchell } 149698ba2d7fSLawrence Mitchell 149798ba2d7fSLawrence Mitchell /*@ 149898ba2d7fSLawrence Mitchell DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition? 149998ba2d7fSLawrence Mitchell 150098ba2d7fSLawrence Mitchell Input Parameter: 1501a2b725a8SWilliam Gropp . dm - The DMPlex object 150298ba2d7fSLawrence Mitchell 150398ba2d7fSLawrence Mitchell Output Parameter: 1504a2b725a8SWilliam Gropp . flg - Balance the partition? 150598ba2d7fSLawrence Mitchell 150698ba2d7fSLawrence Mitchell Level: intermediate 150798ba2d7fSLawrence Mitchell 1508db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexSetPartitionBalance()` 150998ba2d7fSLawrence Mitchell @*/ 151098ba2d7fSLawrence Mitchell PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg) 151198ba2d7fSLawrence Mitchell { 151298ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 151398ba2d7fSLawrence Mitchell 151498ba2d7fSLawrence Mitchell PetscFunctionBegin; 151598ba2d7fSLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1516534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 151798ba2d7fSLawrence Mitchell *flg = mesh->partitionBalance; 151898ba2d7fSLawrence Mitchell PetscFunctionReturn(0); 151998ba2d7fSLawrence Mitchell } 152098ba2d7fSLawrence Mitchell 1521fc02256fSLawrence Mitchell typedef struct { 1522fc02256fSLawrence Mitchell PetscInt vote, rank, index; 1523fc02256fSLawrence Mitchell } Petsc3Int; 1524fc02256fSLawrence Mitchell 1525fc02256fSLawrence Mitchell /* MaxLoc, but carry a third piece of information around */ 15262eb0eadbSSatish Balay static void MPIAPI MaxLocCarry(void *in_, void *inout_, PetscMPIInt *len_, MPI_Datatype *dtype) 1527fc02256fSLawrence Mitchell { 1528fc02256fSLawrence Mitchell Petsc3Int *a = (Petsc3Int *)inout_; 1529fc02256fSLawrence Mitchell Petsc3Int *b = (Petsc3Int *)in_; 1530fc02256fSLawrence Mitchell PetscInt i, len = *len_; 1531fc02256fSLawrence Mitchell for (i = 0; i < len; i++) { 1532fc02256fSLawrence Mitchell if (a[i].vote < b[i].vote) { 1533fc02256fSLawrence Mitchell a[i].vote = b[i].vote; 1534fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1535fc02256fSLawrence Mitchell a[i].index = b[i].index; 1536fc02256fSLawrence Mitchell } else if (a[i].vote <= b[i].vote) { 1537fc02256fSLawrence Mitchell if (a[i].rank >= b[i].rank) { 1538fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1539fc02256fSLawrence Mitchell a[i].index = b[i].index; 1540fc02256fSLawrence Mitchell } 1541fc02256fSLawrence Mitchell } 1542fc02256fSLawrence Mitchell } 1543fc02256fSLawrence Mitchell } 1544fc02256fSLawrence Mitchell 1545f5bf2dbfSMichael Lange /*@C 1546a8c5bd36SStefano Zampini DMPlexCreatePointSF - Build a point SF from an SF describing a point migration 1547f5bf2dbfSMichael Lange 1548064ec1f3Sprj- Input Parameters: 1549f5bf2dbfSMichael Lange + dm - The source DMPlex object 1550f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping 1551d8d19677SJose E. Roman - ownership - Flag causing a vote to determine point ownership 1552f5bf2dbfSMichael Lange 1553f5bf2dbfSMichael Lange Output Parameter: 1554d8d19677SJose E. Roman . pointSF - The star forest describing the point overlap in the remapped DM 1555f5bf2dbfSMichael Lange 15563618482eSVaclav Hapla Notes: 15573618482eSVaclav Hapla Output pointSF is guaranteed to have the array of local indices (leaves) sorted. 15583618482eSVaclav Hapla 1559f5bf2dbfSMichael Lange Level: developer 1560f5bf2dbfSMichael Lange 1561db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 1562f5bf2dbfSMichael Lange @*/ 1563f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF) 1564f5bf2dbfSMichael Lange { 156523193802SMatthew G. Knepley PetscMPIInt rank, size; 15661627f6ccSMichael Lange PetscInt p, nroots, nleaves, idx, npointLeaves; 1567f5bf2dbfSMichael Lange PetscInt *pointLocal; 1568f5bf2dbfSMichael Lange const PetscInt *leaves; 1569f5bf2dbfSMichael Lange const PetscSFNode *roots; 1570f5bf2dbfSMichael Lange PetscSFNode *rootNodes, *leafNodes, *pointRemote; 157123193802SMatthew G. Knepley Vec shifts; 1572cae3e4f3SLawrence Mitchell const PetscInt numShifts = 13759; 157323193802SMatthew G. Knepley const PetscScalar *shift = NULL; 157423193802SMatthew G. Knepley const PetscBool shiftDebug = PETSC_FALSE; 157598ba2d7fSLawrence Mitchell PetscBool balance; 1576f5bf2dbfSMichael Lange 1577f5bf2dbfSMichael Lange PetscFunctionBegin; 1578f5bf2dbfSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 15799566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank)); 15809566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size)); 15819566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_CreatePointSF,dm,0,0,0)); 1582f5bf2dbfSMichael Lange 15839566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitionBalance(dm, &balance)); 15849566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots)); 15859566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes)); 1586f5bf2dbfSMichael Lange if (ownership) { 1587fc02256fSLawrence Mitchell MPI_Op op; 1588fc02256fSLawrence Mitchell MPI_Datatype datatype; 1589fc02256fSLawrence Mitchell Petsc3Int *rootVote = NULL, *leafVote = NULL; 159023193802SMatthew 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. */ 159198ba2d7fSLawrence Mitchell if (balance) { 159223193802SMatthew G. Knepley PetscRandom r; 159323193802SMatthew G. Knepley 15949566063dSJacob Faibussowitsch PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r)); 15959566063dSJacob Faibussowitsch PetscCall(PetscRandomSetInterval(r, 0, 2467*size)); 15969566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &shifts)); 15979566063dSJacob Faibussowitsch PetscCall(VecSetSizes(shifts, numShifts, numShifts)); 15989566063dSJacob Faibussowitsch PetscCall(VecSetType(shifts, VECSTANDARD)); 15999566063dSJacob Faibussowitsch PetscCall(VecSetRandom(shifts, r)); 16009566063dSJacob Faibussowitsch PetscCall(PetscRandomDestroy(&r)); 16019566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(shifts, &shift)); 160223193802SMatthew G. Knepley } 160323193802SMatthew G. Knepley 16049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nroots, &rootVote)); 16059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &leafVote)); 160623193802SMatthew G. Knepley /* Point ownership vote: Process with highest rank owns shared points */ 1607f5bf2dbfSMichael Lange for (p = 0; p < nleaves; ++p) { 160823193802SMatthew G. Knepley if (shiftDebug) { 160963a3b9bcSJacob Faibussowitsch 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, (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]), (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size)); 161023193802SMatthew G. Knepley } 1611f5bf2dbfSMichael Lange /* Either put in a bid or we know we own it */ 1612fc02256fSLawrence Mitchell leafVote[p].vote = (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size; 1613fc02256fSLawrence Mitchell leafVote[p].rank = rank; 1614fc02256fSLawrence Mitchell leafVote[p].index = p; 1615f5bf2dbfSMichael Lange } 1616f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 16171627f6ccSMichael Lange /* Root must not participate in the reduction, flag so that MAXLOC does not use */ 1618fc02256fSLawrence Mitchell rootVote[p].vote = -3; 1619fc02256fSLawrence Mitchell rootVote[p].rank = -3; 1620fc02256fSLawrence Mitchell rootVote[p].index = -3; 1621f5bf2dbfSMichael Lange } 16229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(3, MPIU_INT, &datatype)); 16239566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&datatype)); 16249566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_create(&MaxLocCarry, 1, &op)); 16259566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(migrationSF, datatype, leafVote, rootVote, op)); 16269566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(migrationSF, datatype, leafVote, rootVote, op)); 16279566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_free(&op)); 16289566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&datatype)); 1629c091126eSLawrence Mitchell for (p = 0; p < nroots; p++) { 1630fc02256fSLawrence Mitchell rootNodes[p].rank = rootVote[p].rank; 1631fc02256fSLawrence Mitchell rootNodes[p].index = rootVote[p].index; 1632c091126eSLawrence Mitchell } 16339566063dSJacob Faibussowitsch PetscCall(PetscFree(leafVote)); 16349566063dSJacob Faibussowitsch PetscCall(PetscFree(rootVote)); 1635f5bf2dbfSMichael Lange } else { 1636f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 1637f5bf2dbfSMichael Lange rootNodes[p].index = -1; 1638f5bf2dbfSMichael Lange rootNodes[p].rank = rank; 1639fc02256fSLawrence Mitchell } 1640f5bf2dbfSMichael Lange for (p = 0; p < nleaves; p++) { 1641f5bf2dbfSMichael Lange /* Write new local id into old location */ 1642f5bf2dbfSMichael Lange if (roots[p].rank == rank) { 16436462276dSToby Isaac rootNodes[roots[p].index].index = leaves ? leaves[p] : p; 1644f5bf2dbfSMichael Lange } 1645f5bf2dbfSMichael Lange } 1646f5bf2dbfSMichael Lange } 16479566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes,MPI_REPLACE)); 16489566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes,MPI_REPLACE)); 1649f5bf2dbfSMichael Lange 165023193802SMatthew G. Knepley for (npointLeaves = 0, p = 0; p < nleaves; p++) { 1651b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) npointLeaves++; 165223193802SMatthew G. Knepley } 16539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointLeaves, &pointLocal)); 16549566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointLeaves, &pointRemote)); 1655f5bf2dbfSMichael Lange for (idx = 0, p = 0; p < nleaves; p++) { 1656b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) { 16573618482eSVaclav Hapla /* Note that pointLocal is automatically sorted as it is sublist of 0, ..., nleaves-1 */ 1658f5bf2dbfSMichael Lange pointLocal[idx] = p; 1659f5bf2dbfSMichael Lange pointRemote[idx] = leafNodes[p]; 1660f5bf2dbfSMichael Lange idx++; 1661f5bf2dbfSMichael Lange } 1662f5bf2dbfSMichael Lange } 166323193802SMatthew G. Knepley if (shift) { 16649566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(shifts, &shift)); 16659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&shifts)); 166623193802SMatthew G. Knepley } 16679566063dSJacob Faibussowitsch if (shiftDebug) PetscCall(PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), PETSC_STDOUT)); 16689566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF)); 16699566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(*pointSF)); 16709566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER)); 16719566063dSJacob Faibussowitsch PetscCall(PetscFree2(rootNodes, leafNodes)); 16729566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_CreatePointSF,dm,0,0,0)); 16736c1ef331SVaclav Hapla if (PetscDefined(USE_DEBUG)) PetscCall(DMPlexCheckPointSF(dm, *pointSF)); 1674f5bf2dbfSMichael Lange PetscFunctionReturn(0); 1675f5bf2dbfSMichael Lange } 1676f5bf2dbfSMichael Lange 167715078cd4SMichael Lange /*@C 167815078cd4SMichael Lange DMPlexMigrate - Migrates internal DM data over the supplied star forest 167915078cd4SMichael Lange 1680d083f849SBarry Smith Collective on dm 168183655b49SVáclav Hapla 1682064ec1f3Sprj- Input Parameters: 168315078cd4SMichael Lange + dm - The source DMPlex object 1684d8d19677SJose E. Roman - sf - The star forest communication context describing the migration pattern 168515078cd4SMichael Lange 168615078cd4SMichael Lange Output Parameter: 1687d8d19677SJose E. Roman . targetDM - The target DMPlex object 168815078cd4SMichael Lange 1689b9f40539SMichael Lange Level: intermediate 169015078cd4SMichael Lange 1691db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexDistributeOverlap()` 169215078cd4SMichael Lange @*/ 1693b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM) 169415078cd4SMichael Lange { 1695b9f40539SMichael Lange MPI_Comm comm; 1696cc1750acSStefano Zampini PetscInt dim, cdim, nroots; 1697b9f40539SMichael Lange PetscSF sfPoint; 169815078cd4SMichael Lange ISLocalToGlobalMapping ltogMigration; 1699ac04eaf7SToby Isaac ISLocalToGlobalMapping ltogOriginal = NULL; 170015078cd4SMichael Lange 170115078cd4SMichael Lange PetscFunctionBegin; 170215078cd4SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17039566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0)); 17049566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 17059566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 17069566063dSJacob Faibussowitsch PetscCall(DMSetDimension(targetDM, dim)); 17079566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 17089566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(targetDM, cdim)); 170915078cd4SMichael Lange 1710bfb0467fSMichael Lange /* Check for a one-to-all distribution pattern */ 17119566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 17129566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL)); 1713bfb0467fSMichael Lange if (nroots >= 0) { 1714b9f40539SMichael Lange IS isOriginal; 1715ac04eaf7SToby Isaac PetscInt n, size, nleaves; 1716ac04eaf7SToby Isaac PetscInt *numbering_orig, *numbering_new; 1717367003a6SStefano Zampini 1718b9f40539SMichael Lange /* Get the original point numbering */ 17199566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointNumbering(dm, &isOriginal)); 17209566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(isOriginal, <ogOriginal)); 17219566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltogOriginal, &size)); 17229566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig)); 1723b9f40539SMichael Lange /* Convert to positive global numbers */ 1724b9f40539SMichael Lange for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);} 1725b9f40539SMichael Lange /* Derive the new local-to-global mapping from the old one */ 17269566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL)); 17279566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &numbering_new)); 17289566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sf, MPIU_INT, numbering_orig, numbering_new,MPI_REPLACE)); 17299566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_INT, numbering_orig, numbering_new,MPI_REPLACE)); 17309566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreate(comm, 1, nleaves, numbering_new, PETSC_OWN_POINTER, <ogMigration)); 17319566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig)); 17329566063dSJacob Faibussowitsch PetscCall(ISDestroy(&isOriginal)); 173315078cd4SMichael Lange } else { 1734bfb0467fSMichael Lange /* One-to-all distribution pattern: We can derive LToG from SF */ 17359566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateSF(sf, 0, <ogMigration)); 173615078cd4SMichael Lange } 1737a5a902f7SVaclav Hapla PetscCall(PetscObjectSetName((PetscObject)ltogMigration, "Point renumbering for DM migration")); 1738a5a902f7SVaclav Hapla PetscCall(ISLocalToGlobalMappingViewFromOptions(ltogMigration, (PetscObject)dm, "-partition_view")); 173915078cd4SMichael Lange /* Migrate DM data to target DM */ 17409566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM)); 17419566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeLabels(dm, sf, targetDM)); 17429566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeCoordinates(dm, sf, targetDM)); 17439566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM)); 17449566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<ogOriginal)); 17459566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<ogMigration)); 17469566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0)); 174715078cd4SMichael Lange PetscFunctionReturn(0); 174815078cd4SMichael Lange } 174915078cd4SMichael Lange 17503b8f15a2SMatthew G. Knepley /*@C 175170034214SMatthew G. Knepley DMPlexDistribute - Distributes the mesh and any associated sections. 175270034214SMatthew G. Knepley 1753d083f849SBarry Smith Collective on dm 175470034214SMatthew G. Knepley 1755064ec1f3Sprj- Input Parameters: 175670034214SMatthew G. Knepley + dm - The original DMPlex object 175770034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default 175870034214SMatthew G. Knepley 1759064ec1f3Sprj- Output Parameters: 1760f4ae5380SJed Brown + sf - The PetscSF used for point distribution, or NULL if not needed 1761f4ae5380SJed Brown - dmParallel - The distributed DMPlex object 176270034214SMatthew G. Knepley 1763f4ae5380SJed Brown Note: If the mesh was not distributed, the output dmParallel will be NULL. 176470034214SMatthew G. Knepley 1765b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 176670034214SMatthew G. Knepley representation on the mesh. 176770034214SMatthew G. Knepley 176870034214SMatthew G. Knepley Level: intermediate 176970034214SMatthew G. Knepley 1770db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexGetOverlap()` 177170034214SMatthew G. Knepley @*/ 177280cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel) 177370034214SMatthew G. Knepley { 177470034214SMatthew G. Knepley MPI_Comm comm; 177515078cd4SMichael Lange PetscPartitioner partitioner; 1776f8987ae8SMichael Lange IS cellPart; 1777f8987ae8SMichael Lange PetscSection cellPartSection; 1778cf86098cSMatthew G. Knepley DM dmCoord; 1779f8987ae8SMichael Lange DMLabel lblPartition, lblMigration; 1780874ddda9SLisandro Dalcin PetscSF sfMigration, sfStratified, sfPoint; 178198ba2d7fSLawrence Mitchell PetscBool flg, balance; 1782874ddda9SLisandro Dalcin PetscMPIInt rank, size; 178370034214SMatthew G. Knepley 178470034214SMatthew G. Knepley PetscFunctionBegin; 178570034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1786d5c515a1SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, overlap, 2); 1787d5c515a1SMatthew G. Knepley if (sf) PetscValidPointer(sf,3); 1788d5c515a1SMatthew G. Knepley PetscValidPointer(dmParallel,4); 178970034214SMatthew G. Knepley 17900c86c063SLisandro Dalcin if (sf) *sf = NULL; 17910c86c063SLisandro Dalcin *dmParallel = NULL; 17929566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm,&comm)); 17939566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 17949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 17950c86c063SLisandro Dalcin if (size == 1) PetscFunctionReturn(0); 179670034214SMatthew G. Knepley 17979566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0)); 179815078cd4SMichael Lange /* Create cell partition */ 17999566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0)); 18009566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &cellPartSection)); 18019566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm, &partitioner)); 18029566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDMPlexPartition(partitioner, dm, NULL, cellPartSection, &cellPart)); 18039566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_PartSelf,dm,0,0,0)); 1804f8987ae8SMichael Lange { 1805f8987ae8SMichael Lange /* Convert partition to DMLabel */ 1806825f8a23SLisandro Dalcin IS is; 1807825f8a23SLisandro Dalcin PetscHSetI ht; 1808f8987ae8SMichael Lange const PetscInt *points; 18098e330a33SStefano Zampini PetscInt *iranks; 18108e330a33SStefano Zampini PetscInt pStart, pEnd, proc, npoints, poff = 0, nranks; 1811825f8a23SLisandro Dalcin 18129566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition)); 1813825f8a23SLisandro Dalcin /* Preallocate strata */ 18149566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ht)); 18159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd)); 1816825f8a23SLisandro Dalcin for (proc = pStart; proc < pEnd; proc++) { 18179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints)); 18189566063dSJacob Faibussowitsch if (npoints) PetscCall(PetscHSetIAdd(ht, proc)); 1819825f8a23SLisandro Dalcin } 18209566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ht, &nranks)); 18219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nranks, &iranks)); 18229566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ht, &poff, iranks)); 18239566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ht)); 18249566063dSJacob Faibussowitsch PetscCall(DMLabelAddStrata(lblPartition, nranks, iranks)); 18259566063dSJacob Faibussowitsch PetscCall(PetscFree(iranks)); 1826825f8a23SLisandro Dalcin /* Inline DMPlexPartitionLabelClosure() */ 18279566063dSJacob Faibussowitsch PetscCall(ISGetIndices(cellPart, &points)); 18289566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellPartSection, &pStart, &pEnd)); 1829f8987ae8SMichael Lange for (proc = pStart; proc < pEnd; proc++) { 18309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellPartSection, proc, &npoints)); 1831825f8a23SLisandro Dalcin if (!npoints) continue; 18329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellPartSection, proc, &poff)); 18339566063dSJacob Faibussowitsch PetscCall(DMPlexClosurePoints_Private(dm, npoints, points+poff, &is)); 18349566063dSJacob Faibussowitsch PetscCall(DMLabelSetStratumIS(lblPartition, proc, is)); 18359566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 1836f8987ae8SMichael Lange } 18379566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(cellPart, &points)); 1838f8987ae8SMichael Lange } 18399566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_PartSelf,dm,0,0,0)); 18406e203dd9SStefano Zampini 18419566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration)); 18429566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration)); 18439566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration)); 18449566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified)); 18459566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfMigration)); 184643f7d02bSMichael Lange sfMigration = sfStratified; 18479566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sfMigration)); 18489566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0)); 18499566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg)); 185070034214SMatthew G. Knepley if (flg) { 18519566063dSJacob Faibussowitsch PetscCall(DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_(comm))); 18529566063dSJacob Faibussowitsch PetscCall(PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_(comm))); 185370034214SMatthew G. Knepley } 1854f8987ae8SMichael Lange 185515078cd4SMichael Lange /* Create non-overlapping parallel DM and migrate internal data */ 18569566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, dmParallel)); 18579566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh")); 18589566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(dm, sfMigration, *dmParallel)); 185970034214SMatthew G. Knepley 1860a157612eSMichael Lange /* Build the point SF without overlap */ 18619566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitionBalance(dm, &balance)); 18629566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitionBalance(*dmParallel, balance)); 18639566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint)); 18649566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*dmParallel, sfPoint)); 18659566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dmParallel, &dmCoord)); 18669566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 18679566063dSJacob Faibussowitsch if (flg) PetscCall(PetscSFView(sfPoint, NULL)); 186870034214SMatthew G. Knepley 1869a157612eSMichael Lange if (overlap > 0) { 187015078cd4SMichael Lange DM dmOverlap; 187157f290daSMatthew G. Knepley PetscInt nroots, nleaves, noldleaves, l; 187257f290daSMatthew G. Knepley const PetscInt *oldLeaves; 187357f290daSMatthew G. Knepley PetscSFNode *newRemote, *permRemote; 187415078cd4SMichael Lange const PetscSFNode *oldRemote; 187515078cd4SMichael Lange PetscSF sfOverlap, sfOverlapPoint; 1876524e35f8SStefano Zampini 1877a157612eSMichael Lange /* Add the partition overlap to the distributed DM */ 18789566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap)); 18799566063dSJacob Faibussowitsch PetscCall(DMDestroy(dmParallel)); 1880a157612eSMichael Lange *dmParallel = dmOverlap; 1881c389ea9fSToby Isaac if (flg) { 18829566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Overlap Migration SF:\n")); 18839566063dSJacob Faibussowitsch PetscCall(PetscSFView(sfOverlap, NULL)); 1884c389ea9fSToby Isaac } 188543331d4aSMichael Lange 1886f8987ae8SMichael Lange /* Re-map the migration SF to establish the full migration pattern */ 18879566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfMigration, &nroots, &noldleaves, &oldLeaves, &oldRemote)); 18889566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL)); 18899566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &newRemote)); 189057f290daSMatthew G. Knepley /* oldRemote: original root point mapping to original leaf point 189157f290daSMatthew G. Knepley newRemote: original leaf point mapping to overlapped leaf point */ 189257f290daSMatthew G. Knepley if (oldLeaves) { 189373e69a6aSMatthew G. Knepley /* After stratification, the migration remotes may not be in root (canonical) order, so we reorder using the leaf numbering */ 18949566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(noldleaves, &permRemote)); 189557f290daSMatthew G. Knepley for (l = 0; l < noldleaves; ++l) permRemote[oldLeaves[l]] = oldRemote[l]; 189657f290daSMatthew G. Knepley oldRemote = permRemote; 189757f290daSMatthew G. Knepley } 18989566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote,MPI_REPLACE)); 18999566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote,MPI_REPLACE)); 19009566063dSJacob Faibussowitsch if (oldLeaves) PetscCall(PetscFree(oldRemote)); 19019566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, &sfOverlapPoint)); 19029566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER)); 19039566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfOverlap)); 19049566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfMigration)); 190515078cd4SMichael Lange sfMigration = sfOverlapPoint; 1906c389ea9fSToby Isaac } 1907bf5244c7SMatthew G. Knepley /* Cleanup Partition */ 19089566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblPartition)); 19099566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblMigration)); 19109566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&cellPartSection)); 19119566063dSJacob Faibussowitsch PetscCall(ISDestroy(&cellPart)); 191245480ffeSMatthew G. Knepley /* Copy discretization */ 19139566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, *dmParallel)); 191466fe0bfeSMatthew G. Knepley /* Create sfNatural */ 191566fe0bfeSMatthew G. Knepley if (dm->useNatural) { 191666fe0bfeSMatthew G. Knepley PetscSection section; 191766fe0bfeSMatthew G. Knepley 19188aee0f92SAlexis Marboeuf /* First DM with useNatural = PETSC_TRUE is considered natural */ 19198aee0f92SAlexis Marboeuf /* sfMigration and sfNatural are respectively the point and dofs SFs mapping to this natural DM */ 19208aee0f92SAlexis Marboeuf /* Compose with a previous sfMigration if present */ 19218aee0f92SAlexis Marboeuf if (dm->sfMigration) { 19228aee0f92SAlexis Marboeuf PetscSF naturalPointSF; 19238aee0f92SAlexis Marboeuf 19248aee0f92SAlexis Marboeuf PetscCall(PetscSFCompose(dm->sfMigration, sfMigration, &naturalPointSF)); 19258aee0f92SAlexis Marboeuf PetscCall(PetscSFDestroy(&sfMigration)); 19268aee0f92SAlexis Marboeuf sfMigration = naturalPointSF; 19278aee0f92SAlexis Marboeuf } 19289566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 19299566063dSJacob Faibussowitsch PetscCall(DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural)); 19309566063dSJacob Faibussowitsch PetscCall(DMSetUseNatural(*dmParallel, PETSC_TRUE)); 193166fe0bfeSMatthew G. Knepley } 19325de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *dmParallel)); 1933721cbd76SMatthew G. Knepley /* Cleanup */ 1934f8987ae8SMichael Lange if (sf) {*sf = sfMigration;} 19359566063dSJacob Faibussowitsch else PetscCall(PetscSFDestroy(&sfMigration)); 19369566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 19379566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0)); 193870034214SMatthew G. Knepley PetscFunctionReturn(0); 193970034214SMatthew G. Knepley } 1940a157612eSMichael Lange 1941a157612eSMichael Lange /*@C 194224cc2ca5SMatthew G. Knepley DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM. 1943a157612eSMichael Lange 1944d083f849SBarry Smith Collective on dm 1945a157612eSMichael Lange 1946064ec1f3Sprj- Input Parameters: 1947064ec1f3Sprj- + dm - The non-overlapping distributed DMPlex object 194857fe9a49SVaclav Hapla - overlap - The overlap of partitions (the same on all ranks) 1949a157612eSMichael Lange 1950064ec1f3Sprj- Output Parameters: 1951a157612eSMichael Lange + sf - The PetscSF used for point distribution 1952a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL 1953a157612eSMichael Lange 1954dccdeccaSVaclav Hapla Notes: 1955dccdeccaSVaclav Hapla If the mesh was not distributed, the return value is NULL. 1956a157612eSMichael Lange 1957b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 1958a157612eSMichael Lange representation on the mesh. 1959a157612eSMichael Lange 1960c506a872SMatthew G. Knepley Options Database Keys: 1961c506a872SMatthew G. Knepley + -dm_plex_overlap_labels <name1,name2,...> - List of overlap label names 1962c506a872SMatthew G. Knepley . -dm_plex_overlap_values <int1,int2,...> - List of overlap label values 1963c506a872SMatthew G. Knepley . -dm_plex_overlap_exclude_label <name> - Label used to exclude points from overlap 1964c506a872SMatthew G. Knepley - -dm_plex_overlap_exclude_value <int> - Label value used to exclude points from overlap 1965c506a872SMatthew G. Knepley 1966dccdeccaSVaclav Hapla Level: advanced 1967a157612eSMichael Lange 1968db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMSetAdjacency()`, `DMPlexDistribute()`, `DMPlexCreateOverlapLabel()`, `DMPlexGetOverlap()` 1969a157612eSMichael Lange @*/ 1970b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap) 1971a157612eSMichael Lange { 1972c506a872SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1973a157612eSMichael Lange MPI_Comm comm; 19743567eaeeSMatthew G. Knepley PetscMPIInt size, rank; 1975a157612eSMichael Lange PetscSection rootSection, leafSection; 1976a157612eSMichael Lange IS rootrank, leafrank; 1977cf86098cSMatthew G. Knepley DM dmCoord; 1978a9f1d5b2SMichael Lange DMLabel lblOverlap; 1979f5bf2dbfSMichael Lange PetscSF sfOverlap, sfStratified, sfPoint; 1980a157612eSMichael Lange 1981a157612eSMichael Lange PetscFunctionBegin; 1982a157612eSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 198357fe9a49SVaclav Hapla PetscValidLogicalCollectiveInt(dm, overlap, 2); 1984a157612eSMichael Lange if (sf) PetscValidPointer(sf, 3); 1985a157612eSMichael Lange PetscValidPointer(dmOverlap, 4); 1986a157612eSMichael Lange 19870c86c063SLisandro Dalcin if (sf) *sf = NULL; 19880c86c063SLisandro Dalcin *dmOverlap = NULL; 19899566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm,&comm)); 19909566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 19919566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 19920c86c063SLisandro Dalcin if (size == 1) PetscFunctionReturn(0); 1993c506a872SMatthew G. Knepley { 1994c506a872SMatthew G. Knepley // We need to get options for the _already_distributed mesh, so it must be done here 1995c506a872SMatthew G. Knepley PetscInt overlap; 1996c506a872SMatthew G. Knepley const char *prefix; 1997c506a872SMatthew G. Knepley char oldPrefix[PETSC_MAX_PATH_LEN]; 1998a157612eSMichael Lange 1999c506a872SMatthew G. Knepley oldPrefix[0] = '\0'; 2000c506a872SMatthew G. Knepley PetscCall(PetscObjectGetOptionsPrefix((PetscObject) dm, &prefix)); 2001c506a872SMatthew G. Knepley PetscCall(PetscStrcpy(oldPrefix, prefix)); 2002c506a872SMatthew G. Knepley PetscCall(PetscObjectAppendOptionsPrefix((PetscObject) dm, "dist_")); 2003c506a872SMatthew G. Knepley PetscCall(DMPlexGetOverlap(dm, &overlap)); 2004c506a872SMatthew G. Knepley PetscObjectOptionsBegin((PetscObject) dm); 2005*dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap)); 2006c506a872SMatthew G. Knepley PetscOptionsEnd(); 2007c506a872SMatthew G. Knepley PetscCall(PetscObjectSetOptionsPrefix((PetscObject) dm, oldPrefix[0] == '\0' ? NULL : oldPrefix)); 2008c506a872SMatthew G. Knepley } 20099566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0)); 2010a157612eSMichael Lange /* Compute point overlap with neighbouring processes on the distributed DM */ 20119566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0)); 20129566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &rootSection)); 20139566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(comm, &leafSection)); 20149566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank)); 2015c506a872SMatthew 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)); 2016c506a872SMatthew G. Knepley else PetscCall(DMPlexCreateOverlapLabel(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap)); 2017a9f1d5b2SMichael Lange /* Convert overlap label to stratified migration SF */ 20189566063dSJacob Faibussowitsch PetscCall(DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap)); 20199566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified)); 20209566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfOverlap)); 2021a9f1d5b2SMichael Lange sfOverlap = sfStratified; 20229566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF")); 20239566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(sfOverlap)); 2024a9f1d5b2SMichael Lange 20259566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&rootSection)); 20269566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&leafSection)); 20279566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rootrank)); 20289566063dSJacob Faibussowitsch PetscCall(ISDestroy(&leafrank)); 20299566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0)); 2030a157612eSMichael Lange 2031a157612eSMichael Lange /* Build the overlapping DM */ 20329566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, dmOverlap)); 20339566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh")); 20349566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(dm, sfOverlap, *dmOverlap)); 2035cb54e036SVaclav Hapla /* Store the overlap in the new DM */ 203660667520SVaclav Hapla PetscCall(DMPlexSetOverlap_Plex(*dmOverlap, dm, overlap)); 2037f5bf2dbfSMichael Lange /* Build the new point SF */ 20389566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint)); 20399566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*dmOverlap, sfPoint)); 20409566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dmOverlap, &dmCoord)); 20419566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 20426858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(*dmOverlap, &dmCoord)); 20436858538eSMatthew G. Knepley if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 20449566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 2045a157612eSMichael Lange /* Cleanup overlap partition */ 20469566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblOverlap)); 2047a9f1d5b2SMichael Lange if (sf) *sf = sfOverlap; 20489566063dSJacob Faibussowitsch else PetscCall(PetscSFDestroy(&sfOverlap)); 20499566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0)); 2050a157612eSMichael Lange PetscFunctionReturn(0); 2051a157612eSMichael Lange } 20526462276dSToby Isaac 2053cb54e036SVaclav Hapla PetscErrorCode DMPlexGetOverlap_Plex(DM dm, PetscInt *overlap) 2054cb54e036SVaclav Hapla { 2055cb54e036SVaclav Hapla DM_Plex *mesh = (DM_Plex*) dm->data; 2056cb54e036SVaclav Hapla 2057cb54e036SVaclav Hapla PetscFunctionBegin; 2058cb54e036SVaclav Hapla *overlap = mesh->overlap; 2059cb54e036SVaclav Hapla PetscFunctionReturn(0); 2060cb54e036SVaclav Hapla } 2061cb54e036SVaclav Hapla 206260667520SVaclav Hapla PetscErrorCode DMPlexSetOverlap_Plex(DM dm, DM dmSrc, PetscInt overlap) 206360667520SVaclav Hapla { 206460667520SVaclav Hapla DM_Plex *mesh=NULL; 206560667520SVaclav Hapla DM_Plex *meshSrc=NULL; 206660667520SVaclav Hapla 206760667520SVaclav Hapla PetscFunctionBegin; 206860667520SVaclav Hapla PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMPLEX); 206960667520SVaclav Hapla mesh = (DM_Plex*) dm->data; 207060667520SVaclav Hapla mesh->overlap = overlap; 207160667520SVaclav Hapla if (dmSrc) { 207260667520SVaclav Hapla PetscValidHeaderSpecificType(dmSrc, DM_CLASSID, 2, DMPLEX); 207360667520SVaclav Hapla meshSrc = (DM_Plex*) dmSrc->data; 207460667520SVaclav Hapla mesh->overlap += meshSrc->overlap; 207560667520SVaclav Hapla } 207660667520SVaclav Hapla PetscFunctionReturn(0); 207760667520SVaclav Hapla } 207860667520SVaclav Hapla 2079cb54e036SVaclav Hapla /*@ 2080c506a872SMatthew G. Knepley DMPlexGetOverlap - Get the width of the cell overlap 2081cb54e036SVaclav Hapla 2082cb54e036SVaclav Hapla Not collective 2083cb54e036SVaclav Hapla 2084cb54e036SVaclav Hapla Input Parameter: 2085cb54e036SVaclav Hapla . dm - The DM 2086cb54e036SVaclav Hapla 2087064ec1f3Sprj- Output Parameter: 2088c506a872SMatthew G. Knepley . overlap - the width of the cell overlap 2089cb54e036SVaclav Hapla 2090cb54e036SVaclav Hapla Level: intermediate 2091cb54e036SVaclav Hapla 2092c506a872SMatthew G. Knepley .seealso: `DMPlexSetOverlap()`, `DMPlexDistribute()` 2093cb54e036SVaclav Hapla @*/ 2094cb54e036SVaclav Hapla PetscErrorCode DMPlexGetOverlap(DM dm, PetscInt *overlap) 2095cb54e036SVaclav Hapla { 2096cb54e036SVaclav Hapla PetscFunctionBegin; 2097cb54e036SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2098c506a872SMatthew G. Knepley PetscValidIntPointer(overlap, 2); 2099cac4c232SBarry Smith PetscUseMethod(dm, "DMPlexGetOverlap_C",(DM,PetscInt*),(dm,overlap)); 2100cb54e036SVaclav Hapla PetscFunctionReturn(0); 2101cb54e036SVaclav Hapla } 2102cb54e036SVaclav Hapla 2103c506a872SMatthew G. Knepley /*@ 2104c506a872SMatthew G. Knepley DMPlexSetOverlap - Set the width of the cell overlap 2105c506a872SMatthew G. Knepley 2106c506a872SMatthew G. Knepley Logically collective 2107c506a872SMatthew G. Knepley 2108c506a872SMatthew G. Knepley Input Parameters: 2109c506a872SMatthew G. Knepley + dm - The DM 2110c506a872SMatthew G. Knepley . dmSrc - The DM that produced this one, or NULL 2111c506a872SMatthew G. Knepley - overlap - the width of the cell overlap 2112c506a872SMatthew G. Knepley 2113c506a872SMatthew G. Knepley Note: 2114c506a872SMatthew G. Knepley The overlap from dmSrc is added to dm 2115c506a872SMatthew G. Knepley 2116c506a872SMatthew G. Knepley Level: intermediate 2117c506a872SMatthew G. Knepley 2118c506a872SMatthew G. Knepley .seealso: `DMPlexGetOverlap()`, `DMPlexDistribute()` 2119c506a872SMatthew G. Knepley @*/ 2120c506a872SMatthew G. Knepley PetscErrorCode DMPlexSetOverlap(DM dm, DM dmSrc, PetscInt overlap) 2121c506a872SMatthew G. Knepley { 2122c506a872SMatthew G. Knepley PetscFunctionBegin; 2123c506a872SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2124c506a872SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, overlap, 3); 2125c506a872SMatthew G. Knepley PetscTryMethod(dm, "DMPlexSetOverlap_C",(DM,DM,PetscInt),(dm,dmSrc,overlap)); 2126c506a872SMatthew G. Knepley PetscFunctionReturn(0); 2127c506a872SMatthew G. Knepley } 2128c506a872SMatthew G. Knepley 2129e600fa54SMatthew G. Knepley PetscErrorCode DMPlexDistributeSetDefault_Plex(DM dm, PetscBool dist) 2130e600fa54SMatthew G. Knepley { 2131e600fa54SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2132e600fa54SMatthew G. Knepley 2133e600fa54SMatthew G. Knepley PetscFunctionBegin; 2134e600fa54SMatthew G. Knepley mesh->distDefault = dist; 2135e600fa54SMatthew G. Knepley PetscFunctionReturn(0); 2136e600fa54SMatthew G. Knepley } 2137e600fa54SMatthew G. Knepley 2138e600fa54SMatthew G. Knepley /*@ 2139e600fa54SMatthew G. Knepley DMPlexDistributeSetDefault - Set flag indicating whether the DM should be distributed by default 2140e600fa54SMatthew G. Knepley 2141e600fa54SMatthew G. Knepley Logically collective 2142e600fa54SMatthew G. Knepley 2143e600fa54SMatthew G. Knepley Input Parameters: 2144e600fa54SMatthew G. Knepley + dm - The DM 2145e600fa54SMatthew G. Knepley - dist - Flag for distribution 2146e600fa54SMatthew G. Knepley 2147e600fa54SMatthew G. Knepley Level: intermediate 2148e600fa54SMatthew G. Knepley 21499e70506fSSatish Balay .seealso: `DMPlexDistributeGetDefault()`, `DMPlexDistribute()` 2150e600fa54SMatthew G. Knepley @*/ 2151e600fa54SMatthew G. Knepley PetscErrorCode DMPlexDistributeSetDefault(DM dm, PetscBool dist) 2152e600fa54SMatthew G. Knepley { 2153e600fa54SMatthew G. Knepley PetscFunctionBegin; 2154e600fa54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2155e600fa54SMatthew G. Knepley PetscValidLogicalCollectiveBool(dm, dist, 2); 2156cac4c232SBarry Smith PetscTryMethod(dm,"DMPlexDistributeSetDefault_C",(DM,PetscBool),(dm,dist)); 2157e600fa54SMatthew G. Knepley PetscFunctionReturn(0); 2158e600fa54SMatthew G. Knepley } 2159e600fa54SMatthew G. Knepley 2160e600fa54SMatthew G. Knepley PetscErrorCode DMPlexDistributeGetDefault_Plex(DM dm, PetscBool *dist) 2161e600fa54SMatthew G. Knepley { 2162e600fa54SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2163e600fa54SMatthew G. Knepley 2164e600fa54SMatthew G. Knepley PetscFunctionBegin; 2165e600fa54SMatthew G. Knepley *dist = mesh->distDefault; 2166e600fa54SMatthew G. Knepley PetscFunctionReturn(0); 2167e600fa54SMatthew G. Knepley } 2168e600fa54SMatthew G. Knepley 2169e600fa54SMatthew G. Knepley /*@ 2170e600fa54SMatthew G. Knepley DMPlexDistributeGetDefault - Get flag indicating whether the DM should be distributed by default 2171e600fa54SMatthew G. Knepley 2172e600fa54SMatthew G. Knepley Not collective 2173e600fa54SMatthew G. Knepley 2174e600fa54SMatthew G. Knepley Input Parameter: 2175e600fa54SMatthew G. Knepley . dm - The DM 2176e600fa54SMatthew G. Knepley 2177e600fa54SMatthew G. Knepley Output Parameter: 2178e600fa54SMatthew G. Knepley . dist - Flag for distribution 2179e600fa54SMatthew G. Knepley 2180e600fa54SMatthew G. Knepley Level: intermediate 2181e600fa54SMatthew G. Knepley 21829e70506fSSatish Balay .seealso: `DMPlexDistributeSetDefault()`, `DMPlexDistribute()` 2183e600fa54SMatthew G. Knepley @*/ 2184e600fa54SMatthew G. Knepley PetscErrorCode DMPlexDistributeGetDefault(DM dm, PetscBool *dist) 2185e600fa54SMatthew G. Knepley { 2186e600fa54SMatthew G. Knepley PetscFunctionBegin; 2187e600fa54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2188e600fa54SMatthew G. Knepley PetscValidBoolPointer(dist, 2); 2189cac4c232SBarry Smith PetscUseMethod(dm,"DMPlexDistributeGetDefault_C",(DM,PetscBool*),(dm,dist)); 2190e600fa54SMatthew G. Knepley PetscFunctionReturn(0); 2191e600fa54SMatthew G. Knepley } 2192e600fa54SMatthew G. Knepley 21936462276dSToby Isaac /*@C 21946462276dSToby Isaac DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the 21956462276dSToby Isaac root process of the original's communicator. 21966462276dSToby Isaac 2197d083f849SBarry Smith Collective on dm 219883655b49SVáclav Hapla 2199064ec1f3Sprj- Input Parameter: 22006462276dSToby Isaac . dm - the original DMPlex object 22016462276dSToby Isaac 22026462276dSToby Isaac Output Parameters: 2203a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 2204a13df41bSStefano Zampini - gatherMesh - the gathered DM object, or NULL 22056462276dSToby Isaac 22066462276dSToby Isaac Level: intermediate 22076462276dSToby Isaac 2208db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetRedundantDM()` 22096462276dSToby Isaac @*/ 2210a13df41bSStefano Zampini PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh) 22116462276dSToby Isaac { 22126462276dSToby Isaac MPI_Comm comm; 22136462276dSToby Isaac PetscMPIInt size; 22146462276dSToby Isaac PetscPartitioner oldPart, gatherPart; 22156462276dSToby Isaac 22166462276dSToby Isaac PetscFunctionBegin; 22176462276dSToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2218064a246eSJacob Faibussowitsch PetscValidPointer(gatherMesh,3); 22190c86c063SLisandro Dalcin *gatherMesh = NULL; 2220a13df41bSStefano Zampini if (sf) *sf = NULL; 22216462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 22229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 22236462276dSToby Isaac if (size == 1) PetscFunctionReturn(0); 22249566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm,&oldPart)); 22259566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)oldPart)); 22269566063dSJacob Faibussowitsch PetscCall(PetscPartitionerCreate(comm,&gatherPart)); 22279566063dSJacob Faibussowitsch PetscCall(PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER)); 22289566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitioner(dm,gatherPart)); 22299566063dSJacob Faibussowitsch PetscCall(DMPlexDistribute(dm,0,sf,gatherMesh)); 2230a13df41bSStefano Zampini 22319566063dSJacob Faibussowitsch PetscCall(DMPlexSetPartitioner(dm,oldPart)); 22329566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDestroy(&gatherPart)); 22339566063dSJacob Faibussowitsch PetscCall(PetscPartitionerDestroy(&oldPart)); 22346462276dSToby Isaac PetscFunctionReturn(0); 22356462276dSToby Isaac } 22366462276dSToby Isaac 22376462276dSToby Isaac /*@C 22386462276dSToby Isaac DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process. 22396462276dSToby Isaac 2240d083f849SBarry Smith Collective on dm 224183655b49SVáclav Hapla 2242064ec1f3Sprj- Input Parameter: 22436462276dSToby Isaac . dm - the original DMPlex object 22446462276dSToby Isaac 22456462276dSToby Isaac Output Parameters: 2246a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 2247a13df41bSStefano Zampini - redundantMesh - the redundant DM object, or NULL 22486462276dSToby Isaac 22496462276dSToby Isaac Level: intermediate 22506462276dSToby Isaac 2251db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetGatherDM()` 22526462276dSToby Isaac @*/ 2253a13df41bSStefano Zampini PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh) 22546462276dSToby Isaac { 22556462276dSToby Isaac MPI_Comm comm; 22566462276dSToby Isaac PetscMPIInt size, rank; 22576462276dSToby Isaac PetscInt pStart, pEnd, p; 22586462276dSToby Isaac PetscInt numPoints = -1; 2259a13df41bSStefano Zampini PetscSF migrationSF, sfPoint, gatherSF; 22606462276dSToby Isaac DM gatherDM, dmCoord; 22616462276dSToby Isaac PetscSFNode *points; 22626462276dSToby Isaac 22636462276dSToby Isaac PetscFunctionBegin; 22646462276dSToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2265064a246eSJacob Faibussowitsch PetscValidPointer(redundantMesh,3); 22660c86c063SLisandro Dalcin *redundantMesh = NULL; 22676462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 22689566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 226968dbc166SMatthew G. Knepley if (size == 1) { 22709566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) dm)); 227168dbc166SMatthew G. Knepley *redundantMesh = dm; 2272a13df41bSStefano Zampini if (sf) *sf = NULL; 227368dbc166SMatthew G. Knepley PetscFunctionReturn(0); 227468dbc166SMatthew G. Knepley } 22759566063dSJacob Faibussowitsch PetscCall(DMPlexGetGatherDM(dm,&gatherSF,&gatherDM)); 22766462276dSToby Isaac if (!gatherDM) PetscFunctionReturn(0); 22779566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 22789566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(gatherDM,&pStart,&pEnd)); 22796462276dSToby Isaac numPoints = pEnd - pStart; 22809566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&numPoints,1,MPIU_INT,0,comm)); 22819566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints,&points)); 22829566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm,&migrationSF)); 22836462276dSToby Isaac for (p = 0; p < numPoints; p++) { 22846462276dSToby Isaac points[p].index = p; 22856462276dSToby Isaac points[p].rank = 0; 22866462276dSToby Isaac } 22879566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER)); 22889566063dSJacob Faibussowitsch PetscCall(DMPlexCreate(comm, redundantMesh)); 22899566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh")); 22909566063dSJacob Faibussowitsch PetscCall(DMPlexMigrate(gatherDM, migrationSF, *redundantMesh)); 22912e28cf0cSVaclav Hapla /* This is to express that all point are in overlap */ 22922e28cf0cSVaclav Hapla PetscCall(DMPlexSetOverlap_Plex(*redundantMesh, NULL, PETSC_MAX_INT)); 22939566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint)); 22949566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*redundantMesh, sfPoint)); 22959566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*redundantMesh, &dmCoord)); 22969566063dSJacob Faibussowitsch if (dmCoord) PetscCall(DMSetPointSF(dmCoord, sfPoint)); 22979566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 2298a13df41bSStefano Zampini if (sf) { 2299a13df41bSStefano Zampini PetscSF tsf; 2300a13df41bSStefano Zampini 23019566063dSJacob Faibussowitsch PetscCall(PetscSFCompose(gatherSF,migrationSF,&tsf)); 23029566063dSJacob Faibussowitsch PetscCall(DMPlexStratifyMigrationSF(dm, tsf, sf)); 23039566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&tsf)); 2304a13df41bSStefano Zampini } 23059566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&migrationSF)); 23069566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&gatherSF)); 23079566063dSJacob Faibussowitsch PetscCall(DMDestroy(&gatherDM)); 23089566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, *redundantMesh)); 23095de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, *redundantMesh)); 23106462276dSToby Isaac PetscFunctionReturn(0); 23116462276dSToby Isaac } 23125fa78c88SVaclav Hapla 23135fa78c88SVaclav Hapla /*@ 23145fa78c88SVaclav Hapla DMPlexIsDistributed - Find out whether this DM is distributed, i.e. more than one rank owns some points. 23155fa78c88SVaclav Hapla 23165fa78c88SVaclav Hapla Collective 23175fa78c88SVaclav Hapla 23185fa78c88SVaclav Hapla Input Parameter: 23195fa78c88SVaclav Hapla . dm - The DM object 23205fa78c88SVaclav Hapla 23215fa78c88SVaclav Hapla Output Parameter: 23225fa78c88SVaclav Hapla . distributed - Flag whether the DM is distributed 23235fa78c88SVaclav Hapla 23245fa78c88SVaclav Hapla Level: intermediate 23255fa78c88SVaclav Hapla 23265fa78c88SVaclav Hapla Notes: 23275fa78c88SVaclav Hapla This currently finds out whether at least two ranks have any DAG points. 23285fa78c88SVaclav Hapla This involves MPI_Allreduce() with one integer. 23295fa78c88SVaclav Hapla The result is currently not stashed so every call to this routine involves this global communication. 23305fa78c88SVaclav Hapla 2331db781477SPatrick Sanan .seealso: `DMPlexDistribute()`, `DMPlexGetOverlap()`, `DMPlexIsInterpolated()` 23325fa78c88SVaclav Hapla @*/ 23335fa78c88SVaclav Hapla PetscErrorCode DMPlexIsDistributed(DM dm, PetscBool *distributed) 23345fa78c88SVaclav Hapla { 23355fa78c88SVaclav Hapla PetscInt pStart, pEnd, count; 23365fa78c88SVaclav Hapla MPI_Comm comm; 233735d70e31SStefano Zampini PetscMPIInt size; 23385fa78c88SVaclav Hapla 23395fa78c88SVaclav Hapla PetscFunctionBegin; 23405fa78c88SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2341dadcf809SJacob Faibussowitsch PetscValidBoolPointer(distributed,2); 23429566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm,&comm)); 23439566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm,&size)); 234435d70e31SStefano Zampini if (size == 1) { *distributed = PETSC_FALSE; PetscFunctionReturn(0); } 23459566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 234635d70e31SStefano Zampini count = (pEnd - pStart) > 0 ? 1 : 0; 23479566063dSJacob Faibussowitsch PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &count, 1, MPIU_INT, MPI_SUM, comm)); 23485fa78c88SVaclav Hapla *distributed = count > 1 ? PETSC_TRUE : PETSC_FALSE; 23495fa78c88SVaclav Hapla PetscFunctionReturn(0); 23505fa78c88SVaclav Hapla } 2351