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 19b0441da4SMatthew G. Knepley .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: 393c1f0c11SLawrence Mitchell - user - The user callback 403c1f0c11SLawrence Mitchell - ctx - context for callback evaluation 413c1f0c11SLawrence Mitchell 423c1f0c11SLawrence Mitchell Level: advanced 433c1f0c11SLawrence Mitchell 44b0441da4SMatthew G. Knepley .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 66b0441da4SMatthew G. Knepley .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 89b0441da4SMatthew G. Knepley .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 PetscErrorCode ierr; 10770034214SMatthew G. Knepley 10870034214SMatthew G. Knepley PetscFunctionBeginHot; 10970034214SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr); 11070034214SMatthew G. Knepley ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr); 1114b6b44bdSMatthew G. Knepley for (c = 0; c <= coneSize; ++c) { 1124b6b44bdSMatthew G. Knepley const PetscInt point = !c ? p : cone[c-1]; 11370034214SMatthew G. Knepley const PetscInt *support = NULL; 11470034214SMatthew G. Knepley PetscInt supportSize, s, q; 11570034214SMatthew G. Knepley 1164b6b44bdSMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr); 1174b6b44bdSMatthew G. Knepley ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 11870034214SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 119527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = support[s]),0); ++q) { 12070034214SMatthew G. Knepley if (support[s] == adj[q]) break; 12170034214SMatthew G. Knepley } 12270034214SMatthew G. Knepley if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize); 12370034214SMatthew G. Knepley } 12470034214SMatthew G. Knepley } 12570034214SMatthew G. Knepley *adjSize = numAdj; 12670034214SMatthew G. Knepley PetscFunctionReturn(0); 12770034214SMatthew G. Knepley } 12870034214SMatthew G. Knepley 12970034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Support_Internal(DM dm, PetscInt p, PetscInt *adjSize, PetscInt adj[]) 13070034214SMatthew G. Knepley { 13170034214SMatthew G. Knepley const PetscInt *support = NULL; 13270034214SMatthew G. Knepley PetscInt numAdj = 0, maxAdjSize = *adjSize, supportSize, s; 13370034214SMatthew G. Knepley PetscErrorCode ierr; 13470034214SMatthew G. Knepley 13570034214SMatthew G. Knepley PetscFunctionBeginHot; 13670034214SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr); 13770034214SMatthew G. Knepley ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr); 1384b6b44bdSMatthew G. Knepley for (s = 0; s <= supportSize; ++s) { 1394b6b44bdSMatthew G. Knepley const PetscInt point = !s ? p : support[s-1]; 14070034214SMatthew G. Knepley const PetscInt *cone = NULL; 14170034214SMatthew G. Knepley PetscInt coneSize, c, q; 14270034214SMatthew G. Knepley 1434b6b44bdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 1444b6b44bdSMatthew G. Knepley ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 14570034214SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 146527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = cone[c]),0); ++q) { 14770034214SMatthew G. Knepley if (cone[c] == adj[q]) break; 14870034214SMatthew G. Knepley } 14970034214SMatthew G. Knepley if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize); 15070034214SMatthew G. Knepley } 15170034214SMatthew G. Knepley } 15270034214SMatthew G. Knepley *adjSize = numAdj; 15370034214SMatthew G. Knepley PetscFunctionReturn(0); 15470034214SMatthew G. Knepley } 15570034214SMatthew G. Knepley 15670034214SMatthew G. Knepley static PetscErrorCode DMPlexGetAdjacency_Transitive_Internal(DM dm, PetscInt p, PetscBool useClosure, PetscInt *adjSize, PetscInt adj[]) 15770034214SMatthew G. Knepley { 15870034214SMatthew G. Knepley PetscInt *star = NULL; 15970034214SMatthew G. Knepley PetscInt numAdj = 0, maxAdjSize = *adjSize, starSize, s; 16070034214SMatthew G. Knepley PetscErrorCode ierr; 16170034214SMatthew G. Knepley 16270034214SMatthew G. Knepley PetscFunctionBeginHot; 16370034214SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr); 16470034214SMatthew G. Knepley for (s = 0; s < starSize*2; s += 2) { 16570034214SMatthew G. Knepley const PetscInt *closure = NULL; 16670034214SMatthew G. Knepley PetscInt closureSize, c, q; 16770034214SMatthew G. Knepley 16870034214SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr); 16970034214SMatthew G. Knepley for (c = 0; c < closureSize*2; c += 2) { 170527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(adj[numAdj++] = closure[c]),0); ++q) { 17170034214SMatthew G. Knepley if (closure[c] == adj[q]) break; 17270034214SMatthew G. Knepley } 17370034214SMatthew G. Knepley if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize); 17470034214SMatthew G. Knepley } 17570034214SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, star[s], (PetscBool)!useClosure, &closureSize, (PetscInt**) &closure);CHKERRQ(ierr); 17670034214SMatthew G. Knepley } 17770034214SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, p, useClosure, &starSize, &star);CHKERRQ(ierr); 17870034214SMatthew G. Knepley *adjSize = numAdj; 17970034214SMatthew G. Knepley PetscFunctionReturn(0); 18070034214SMatthew G. Knepley } 18170034214SMatthew G. Knepley 1825b317d89SToby Isaac PetscErrorCode DMPlexGetAdjacency_Internal(DM dm, PetscInt p, PetscBool useCone, PetscBool useTransitiveClosure, PetscBool useAnchors, PetscInt *adjSize, PetscInt *adj[]) 18370034214SMatthew G. Knepley { 18479bad331SMatthew G. Knepley static PetscInt asiz = 0; 1858b0b4c70SToby Isaac PetscInt maxAnchors = 1; 1868b0b4c70SToby Isaac PetscInt aStart = -1, aEnd = -1; 1878b0b4c70SToby Isaac PetscInt maxAdjSize; 1888b0b4c70SToby Isaac PetscSection aSec = NULL; 1898b0b4c70SToby Isaac IS aIS = NULL; 1908b0b4c70SToby Isaac const PetscInt *anchors; 1913c1f0c11SLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 19270034214SMatthew G. Knepley PetscErrorCode ierr; 19370034214SMatthew G. Knepley 19470034214SMatthew G. Knepley PetscFunctionBeginHot; 1955b317d89SToby Isaac if (useAnchors) { 196a17985deSToby Isaac ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr); 1978b0b4c70SToby Isaac if (aSec) { 1988b0b4c70SToby Isaac ierr = PetscSectionGetMaxDof(aSec,&maxAnchors);CHKERRQ(ierr); 19924c766afSToby Isaac maxAnchors = PetscMax(1,maxAnchors); 2008b0b4c70SToby Isaac ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr); 2018b0b4c70SToby Isaac ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr); 2028b0b4c70SToby Isaac } 2038b0b4c70SToby Isaac } 20479bad331SMatthew G. Knepley if (!*adj) { 20524c766afSToby Isaac PetscInt depth, coneSeries, supportSeries, maxC, maxS, pStart, pEnd; 20679bad331SMatthew G. Knepley 20724c766afSToby Isaac ierr = DMPlexGetChart(dm, &pStart,&pEnd);CHKERRQ(ierr); 20879bad331SMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 209412e9a14SMatthew G. Knepley depth = PetscMax(depth, -depth); 21024c766afSToby Isaac ierr = DMPlexGetMaxSizes(dm, &maxC, &maxS);CHKERRQ(ierr); 21124c766afSToby Isaac coneSeries = (maxC > 1) ? ((PetscPowInt(maxC,depth+1)-1)/(maxC-1)) : depth+1; 21224c766afSToby Isaac supportSeries = (maxS > 1) ? ((PetscPowInt(maxS,depth+1)-1)/(maxS-1)) : depth+1; 21324c766afSToby Isaac asiz = PetscMax(PetscPowInt(maxS,depth)*coneSeries,PetscPowInt(maxC,depth)*supportSeries); 2148b0b4c70SToby Isaac asiz *= maxAnchors; 21524c766afSToby Isaac asiz = PetscMin(asiz,pEnd-pStart); 21679bad331SMatthew G. Knepley ierr = PetscMalloc1(asiz,adj);CHKERRQ(ierr); 21779bad331SMatthew G. Knepley } 21879bad331SMatthew G. Knepley if (*adjSize < 0) *adjSize = asiz; 2198b0b4c70SToby Isaac maxAdjSize = *adjSize; 2203c1f0c11SLawrence Mitchell if (mesh->useradjacency) { 22162f17a49SStefano Zampini ierr = (*mesh->useradjacency)(dm, p, adjSize, *adj, mesh->useradjacencyctx);CHKERRQ(ierr); 2223c1f0c11SLawrence Mitchell } else if (useTransitiveClosure) { 22379bad331SMatthew G. Knepley ierr = DMPlexGetAdjacency_Transitive_Internal(dm, p, useCone, adjSize, *adj);CHKERRQ(ierr); 22470034214SMatthew G. Knepley } else if (useCone) { 22579bad331SMatthew G. Knepley ierr = DMPlexGetAdjacency_Cone_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr); 22670034214SMatthew G. Knepley } else { 22779bad331SMatthew G. Knepley ierr = DMPlexGetAdjacency_Support_Internal(dm, p, adjSize, *adj);CHKERRQ(ierr); 22870034214SMatthew G. Knepley } 2295b317d89SToby Isaac if (useAnchors && aSec) { 2308b0b4c70SToby Isaac PetscInt origSize = *adjSize; 2318b0b4c70SToby Isaac PetscInt numAdj = origSize; 2328b0b4c70SToby Isaac PetscInt i = 0, j; 2338b0b4c70SToby Isaac PetscInt *orig = *adj; 2348b0b4c70SToby Isaac 2358b0b4c70SToby Isaac while (i < origSize) { 2368b0b4c70SToby Isaac PetscInt p = orig[i]; 2378b0b4c70SToby Isaac PetscInt aDof = 0; 2388b0b4c70SToby Isaac 2398b0b4c70SToby Isaac if (p >= aStart && p < aEnd) { 2408b0b4c70SToby Isaac ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr); 2418b0b4c70SToby Isaac } 2428b0b4c70SToby Isaac if (aDof) { 2438b0b4c70SToby Isaac PetscInt aOff; 2448b0b4c70SToby Isaac PetscInt s, q; 2458b0b4c70SToby Isaac 2468b0b4c70SToby Isaac for (j = i + 1; j < numAdj; j++) { 2478b0b4c70SToby Isaac orig[j - 1] = orig[j]; 2488b0b4c70SToby Isaac } 2498b0b4c70SToby Isaac origSize--; 2508b0b4c70SToby Isaac numAdj--; 2518b0b4c70SToby Isaac ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr); 2528b0b4c70SToby Isaac for (s = 0; s < aDof; ++s) { 253527e7439SSatish Balay for (q = 0; q < numAdj || ((void)(orig[numAdj++] = anchors[aOff+s]),0); ++q) { 2548b0b4c70SToby Isaac if (anchors[aOff+s] == orig[q]) break; 2558b0b4c70SToby Isaac } 2568b0b4c70SToby Isaac if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize); 2578b0b4c70SToby Isaac } 2588b0b4c70SToby Isaac } 2598b0b4c70SToby Isaac else { 2608b0b4c70SToby Isaac i++; 2618b0b4c70SToby Isaac } 2628b0b4c70SToby Isaac } 2638b0b4c70SToby Isaac *adjSize = numAdj; 2648b0b4c70SToby Isaac ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr); 2658b0b4c70SToby Isaac } 26670034214SMatthew G. Knepley PetscFunctionReturn(0); 26770034214SMatthew G. Knepley } 26870034214SMatthew G. Knepley 26970034214SMatthew G. Knepley /*@ 27070034214SMatthew G. Knepley DMPlexGetAdjacency - Return all points adjacent to the given point 27170034214SMatthew G. Knepley 27270034214SMatthew G. Knepley Input Parameters: 27370034214SMatthew G. Knepley + dm - The DM object 27470034214SMatthew G. Knepley . p - The point 27570034214SMatthew G. Knepley . adjSize - The maximum size of adj if it is non-NULL, or PETSC_DETERMINE 27670034214SMatthew G. Knepley - adj - Either NULL so that the array is allocated, or an existing array with size adjSize 27770034214SMatthew G. Knepley 27870034214SMatthew G. Knepley Output Parameters: 27970034214SMatthew G. Knepley + adjSize - The number of adjacent points 28070034214SMatthew G. Knepley - adj - The adjacent points 28170034214SMatthew G. Knepley 28270034214SMatthew G. Knepley Level: advanced 28370034214SMatthew G. Knepley 28495452b02SPatrick Sanan Notes: 28595452b02SPatrick Sanan The user must PetscFree the adj array if it was not passed in. 28670034214SMatthew G. Knepley 287b0441da4SMatthew G. Knepley .seealso: DMSetAdjacency(), DMPlexDistribute(), DMCreateMatrix(), DMPlexPreallocateOperator() 28870034214SMatthew G. Knepley @*/ 28970034214SMatthew G. Knepley PetscErrorCode DMPlexGetAdjacency(DM dm, PetscInt p, PetscInt *adjSize, PetscInt *adj[]) 29070034214SMatthew G. Knepley { 2911cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 29270034214SMatthew G. Knepley PetscErrorCode ierr; 29370034214SMatthew G. Knepley 29470034214SMatthew G. Knepley PetscFunctionBeginHot; 29570034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 29670034214SMatthew G. Knepley PetscValidPointer(adjSize,3); 29770034214SMatthew G. Knepley PetscValidPointer(adj,4); 298b0441da4SMatthew G. Knepley ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr); 2991cf84007SMatthew G. Knepley ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr); 3001cf84007SMatthew G. Knepley ierr = DMPlexGetAdjacency_Internal(dm, p, useCone, useClosure, useAnchors, adjSize, adj);CHKERRQ(ierr); 30170034214SMatthew G. Knepley PetscFunctionReturn(0); 30270034214SMatthew G. Knepley } 30308633170SToby Isaac 304b0a623aaSMatthew G. Knepley /*@ 305b0a623aaSMatthew G. Knepley DMPlexCreateTwoSidedProcessSF - Create an SF which just has process connectivity 306b0a623aaSMatthew G. Knepley 307d083f849SBarry Smith Collective on dm 308b0a623aaSMatthew G. Knepley 309b0a623aaSMatthew G. Knepley Input Parameters: 310b0a623aaSMatthew G. Knepley + dm - The DM 311b0a623aaSMatthew G. Knepley - sfPoint - The PetscSF which encodes point connectivity 312b0a623aaSMatthew G. Knepley 313b0a623aaSMatthew G. Knepley Output Parameters: 314b0a623aaSMatthew G. Knepley + processRanks - A list of process neighbors, or NULL 315b0a623aaSMatthew G. Knepley - sfProcess - An SF encoding the two-sided process connectivity, or NULL 316b0a623aaSMatthew G. Knepley 317b0a623aaSMatthew G. Knepley Level: developer 318b0a623aaSMatthew G. Knepley 319b0a623aaSMatthew G. Knepley .seealso: PetscSFCreate(), DMPlexCreateProcessSF() 320b0a623aaSMatthew G. Knepley @*/ 321b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexCreateTwoSidedProcessSF(DM dm, PetscSF sfPoint, PetscSection rootRankSection, IS rootRanks, PetscSection leafRankSection, IS leafRanks, IS *processRanks, PetscSF *sfProcess) 322b0a623aaSMatthew G. Knepley { 323b0a623aaSMatthew G. Knepley const PetscSFNode *remotePoints; 324b0a623aaSMatthew G. Knepley PetscInt *localPointsNew; 325b0a623aaSMatthew G. Knepley PetscSFNode *remotePointsNew; 326b0a623aaSMatthew G. Knepley const PetscInt *nranks; 327b0a623aaSMatthew G. Knepley PetscInt *ranksNew; 328b0a623aaSMatthew G. Knepley PetscBT neighbors; 329b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, numLeaves, l, numNeighbors, n; 3309852e123SBarry Smith PetscMPIInt size, proc, rank; 331b0a623aaSMatthew G. Knepley PetscErrorCode ierr; 332b0a623aaSMatthew G. Knepley 333b0a623aaSMatthew G. Knepley PetscFunctionBegin; 334b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 335b0a623aaSMatthew G. Knepley PetscValidHeaderSpecific(sfPoint, PETSCSF_CLASSID, 2); 336064a246eSJacob Faibussowitsch if (processRanks) {PetscValidPointer(processRanks, 7);} 337064a246eSJacob Faibussowitsch if (sfProcess) {PetscValidPointer(sfProcess, 8);} 338ffc4695bSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRMPI(ierr); 339ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr); 340b0a623aaSMatthew G. Knepley ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, NULL, &remotePoints);CHKERRQ(ierr); 3419852e123SBarry Smith ierr = PetscBTCreate(size, &neighbors);CHKERRQ(ierr); 3429852e123SBarry Smith ierr = PetscBTMemzero(size, neighbors);CHKERRQ(ierr); 343b0a623aaSMatthew G. Knepley /* Compute root-to-leaf process connectivity */ 344b0a623aaSMatthew G. Knepley ierr = PetscSectionGetChart(rootRankSection, &pStart, &pEnd);CHKERRQ(ierr); 345b0a623aaSMatthew G. Knepley ierr = ISGetIndices(rootRanks, &nranks);CHKERRQ(ierr); 346b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 347b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 348b0a623aaSMatthew G. Knepley 349b0a623aaSMatthew G. Knepley ierr = PetscSectionGetDof(rootRankSection, p, &ndof);CHKERRQ(ierr); 350b0a623aaSMatthew G. Knepley ierr = PetscSectionGetOffset(rootRankSection, p, &noff);CHKERRQ(ierr); 351302440fdSBarry Smith for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);} 352b0a623aaSMatthew G. Knepley } 353b0a623aaSMatthew G. Knepley ierr = ISRestoreIndices(rootRanks, &nranks);CHKERRQ(ierr); 354b0a623aaSMatthew G. Knepley /* Compute leaf-to-neighbor process connectivity */ 355b0a623aaSMatthew G. Knepley ierr = PetscSectionGetChart(leafRankSection, &pStart, &pEnd);CHKERRQ(ierr); 356b0a623aaSMatthew G. Knepley ierr = ISGetIndices(leafRanks, &nranks);CHKERRQ(ierr); 357b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 358b0a623aaSMatthew G. Knepley PetscInt ndof, noff, n; 359b0a623aaSMatthew G. Knepley 360b0a623aaSMatthew G. Knepley ierr = PetscSectionGetDof(leafRankSection, p, &ndof);CHKERRQ(ierr); 361b0a623aaSMatthew G. Knepley ierr = PetscSectionGetOffset(leafRankSection, p, &noff);CHKERRQ(ierr); 362302440fdSBarry Smith for (n = 0; n < ndof; ++n) {ierr = PetscBTSet(neighbors, nranks[noff+n]);CHKERRQ(ierr);} 363b0a623aaSMatthew G. Knepley } 364b0a623aaSMatthew G. Knepley ierr = ISRestoreIndices(leafRanks, &nranks);CHKERRQ(ierr); 365b0a623aaSMatthew G. Knepley /* Compute leaf-to-root process connectivity */ 366b0a623aaSMatthew G. Knepley for (l = 0; l < numLeaves; ++l) {PetscBTSet(neighbors, remotePoints[l].rank);} 367b0a623aaSMatthew G. Knepley /* Calculate edges */ 368b0a623aaSMatthew G. Knepley PetscBTClear(neighbors, rank); 3699852e123SBarry Smith for (proc = 0, numNeighbors = 0; proc < size; ++proc) {if (PetscBTLookup(neighbors, proc)) ++numNeighbors;} 370b0a623aaSMatthew G. Knepley ierr = PetscMalloc1(numNeighbors, &ranksNew);CHKERRQ(ierr); 371b0a623aaSMatthew G. Knepley ierr = PetscMalloc1(numNeighbors, &localPointsNew);CHKERRQ(ierr); 372b0a623aaSMatthew G. Knepley ierr = PetscMalloc1(numNeighbors, &remotePointsNew);CHKERRQ(ierr); 3739852e123SBarry Smith for (proc = 0, n = 0; proc < size; ++proc) { 374b0a623aaSMatthew G. Knepley if (PetscBTLookup(neighbors, proc)) { 375b0a623aaSMatthew G. Knepley ranksNew[n] = proc; 376b0a623aaSMatthew G. Knepley localPointsNew[n] = proc; 377b0a623aaSMatthew G. Knepley remotePointsNew[n].index = rank; 378b0a623aaSMatthew G. Knepley remotePointsNew[n].rank = proc; 379b0a623aaSMatthew G. Knepley ++n; 380b0a623aaSMatthew G. Knepley } 381b0a623aaSMatthew G. Knepley } 382b0a623aaSMatthew G. Knepley ierr = PetscBTDestroy(&neighbors);CHKERRQ(ierr); 383b0a623aaSMatthew G. Knepley if (processRanks) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), numNeighbors, ranksNew, PETSC_OWN_POINTER, processRanks);CHKERRQ(ierr);} 384b0a623aaSMatthew G. Knepley else {ierr = PetscFree(ranksNew);CHKERRQ(ierr);} 385b0a623aaSMatthew G. Knepley if (sfProcess) { 386b0a623aaSMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfProcess);CHKERRQ(ierr); 387b0a623aaSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) *sfProcess, "Two-Sided Process SF");CHKERRQ(ierr); 388b0a623aaSMatthew G. Knepley ierr = PetscSFSetFromOptions(*sfProcess);CHKERRQ(ierr); 3899852e123SBarry Smith ierr = PetscSFSetGraph(*sfProcess, size, numNeighbors, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr); 390b0a623aaSMatthew G. Knepley } 391b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 392b0a623aaSMatthew G. Knepley } 393b0a623aaSMatthew G. Knepley 394b0a623aaSMatthew G. Knepley /*@ 395b0a623aaSMatthew G. Knepley DMPlexDistributeOwnership - Compute owner information for shared points. This basically gets two-sided for an SF. 396b0a623aaSMatthew G. Knepley 397d083f849SBarry Smith Collective on dm 398b0a623aaSMatthew G. Knepley 399b0a623aaSMatthew G. Knepley Input Parameter: 400b0a623aaSMatthew G. Knepley . dm - The DM 401b0a623aaSMatthew G. Knepley 402b0a623aaSMatthew G. Knepley Output Parameters: 403b0a623aaSMatthew G. Knepley + rootSection - The number of leaves for a given root point 404b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 405b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 406b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 407b0a623aaSMatthew G. Knepley 408b0a623aaSMatthew G. Knepley Level: developer 409b0a623aaSMatthew G. Knepley 410dccdeccaSVaclav Hapla .seealso: DMPlexCreateOverlapLabel(), DMPlexDistribute(), DMPlexDistributeOverlap() 411b0a623aaSMatthew G. Knepley @*/ 412b0a623aaSMatthew G. Knepley PetscErrorCode DMPlexDistributeOwnership(DM dm, PetscSection rootSection, IS *rootrank, PetscSection leafSection, IS *leafrank) 413b0a623aaSMatthew G. Knepley { 414b0a623aaSMatthew G. Knepley MPI_Comm comm; 415b0a623aaSMatthew G. Knepley PetscSF sfPoint; 416b0a623aaSMatthew G. Knepley const PetscInt *rootdegree; 417b0a623aaSMatthew G. Knepley PetscInt *myrank, *remoterank; 418b0a623aaSMatthew G. Knepley PetscInt pStart, pEnd, p, nedges; 419b0a623aaSMatthew G. Knepley PetscMPIInt rank; 420b0a623aaSMatthew G. Knepley PetscErrorCode ierr; 421b0a623aaSMatthew G. Knepley 422b0a623aaSMatthew G. Knepley PetscFunctionBegin; 423b0a623aaSMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 424ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 425b0a623aaSMatthew G. Knepley ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 426b0a623aaSMatthew G. Knepley ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 427b0a623aaSMatthew G. Knepley /* Compute number of leaves for each root */ 428b0a623aaSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) rootSection, "Root Section");CHKERRQ(ierr); 429b0a623aaSMatthew G. Knepley ierr = PetscSectionSetChart(rootSection, pStart, pEnd);CHKERRQ(ierr); 430b0a623aaSMatthew G. Knepley ierr = PetscSFComputeDegreeBegin(sfPoint, &rootdegree);CHKERRQ(ierr); 431b0a623aaSMatthew G. Knepley ierr = PetscSFComputeDegreeEnd(sfPoint, &rootdegree);CHKERRQ(ierr); 432b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionSetDof(rootSection, p, rootdegree[p-pStart]);CHKERRQ(ierr);} 433b0a623aaSMatthew G. Knepley ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr); 434b0a623aaSMatthew G. Knepley /* Gather rank of each leaf to root */ 435b0a623aaSMatthew G. Knepley ierr = PetscSectionGetStorageSize(rootSection, &nedges);CHKERRQ(ierr); 436b0a623aaSMatthew G. Knepley ierr = PetscMalloc1(pEnd-pStart, &myrank);CHKERRQ(ierr); 437b0a623aaSMatthew G. Knepley ierr = PetscMalloc1(nedges, &remoterank);CHKERRQ(ierr); 438b0a623aaSMatthew G. Knepley for (p = 0; p < pEnd-pStart; ++p) myrank[p] = rank; 439b0a623aaSMatthew G. Knepley ierr = PetscSFGatherBegin(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr); 440b0a623aaSMatthew G. Knepley ierr = PetscSFGatherEnd(sfPoint, MPIU_INT, myrank, remoterank);CHKERRQ(ierr); 441b0a623aaSMatthew G. Knepley ierr = PetscFree(myrank);CHKERRQ(ierr); 442b0a623aaSMatthew G. Knepley ierr = ISCreateGeneral(comm, nedges, remoterank, PETSC_OWN_POINTER, rootrank);CHKERRQ(ierr); 443b0a623aaSMatthew G. Knepley /* Distribute remote ranks to leaves */ 444b0a623aaSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) leafSection, "Leaf Section");CHKERRQ(ierr); 445b0a623aaSMatthew G. Knepley ierr = DMPlexDistributeFieldIS(dm, sfPoint, rootSection, *rootrank, leafSection, leafrank);CHKERRQ(ierr); 446b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 447b0a623aaSMatthew G. Knepley } 448b0a623aaSMatthew G. Knepley 449278397a0SMatthew G. Knepley /*@C 450dccdeccaSVaclav Hapla DMPlexCreateOverlapLabel - Compute owner information for shared points. This basically gets two-sided for an SF. 451b0a623aaSMatthew G. Knepley 452d083f849SBarry Smith Collective on dm 453b0a623aaSMatthew G. Knepley 454b0a623aaSMatthew G. Knepley Input Parameters: 455b0a623aaSMatthew G. Knepley + dm - The DM 45624d039d7SMichael Lange . levels - Number of overlap levels 457b0a623aaSMatthew G. Knepley . rootSection - The number of leaves for a given root point 458b0a623aaSMatthew G. Knepley . rootrank - The rank of each edge into the root point 459b0a623aaSMatthew G. Knepley . leafSection - The number of processes sharing a given leaf point 460b0a623aaSMatthew G. Knepley - leafrank - The rank of each process sharing a leaf point 461b0a623aaSMatthew G. Knepley 462064ec1f3Sprj- Output Parameter: 463b4ec6ac8SBarry Smith . ovLabel - DMLabel containing remote overlap contributions as point/rank pairings 464b0a623aaSMatthew G. Knepley 465b0a623aaSMatthew G. Knepley Level: developer 466b0a623aaSMatthew G. Knepley 4671fd9873aSMichael Lange .seealso: DMPlexDistributeOwnership(), DMPlexDistribute() 468b0a623aaSMatthew G. Knepley @*/ 469dccdeccaSVaclav Hapla PetscErrorCode DMPlexCreateOverlapLabel(DM dm, PetscInt levels, PetscSection rootSection, IS rootrank, PetscSection leafSection, IS leafrank, DMLabel *ovLabel) 470b0a623aaSMatthew G. Knepley { 471e540f424SMichael Lange MPI_Comm comm; 472b0a623aaSMatthew G. Knepley DMLabel ovAdjByRank; /* A DMLabel containing all points adjacent to shared points, separated by rank (value in label) */ 473874ddda9SLisandro Dalcin PetscSF sfPoint; 474b0a623aaSMatthew G. Knepley const PetscSFNode *remote; 475b0a623aaSMatthew G. Knepley const PetscInt *local; 4761fd9873aSMichael Lange const PetscInt *nrank, *rrank; 477b0a623aaSMatthew G. Knepley PetscInt *adj = NULL; 4781fd9873aSMichael Lange PetscInt pStart, pEnd, p, sStart, sEnd, nleaves, l; 4799852e123SBarry Smith PetscMPIInt rank, size; 48031bc6364SLisandro Dalcin PetscBool flg; 481b0a623aaSMatthew G. Knepley PetscErrorCode ierr; 482b0a623aaSMatthew G. Knepley 483b0a623aaSMatthew G. Knepley PetscFunctionBegin; 4846ba1a4c7SVaclav Hapla *ovLabel = NULL; 485e540f424SMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 486ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 487ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 4886ba1a4c7SVaclav Hapla if (size == 1) PetscFunctionReturn(0); 489b0a623aaSMatthew G. Knepley ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 490b0a623aaSMatthew G. Knepley ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 491d1674c6dSMatthew Knepley if (!levels) { 492d1674c6dSMatthew Knepley /* Add owned points */ 493d1674c6dSMatthew Knepley ierr = DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel);CHKERRQ(ierr); 494d1674c6dSMatthew Knepley for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);} 495d1674c6dSMatthew Knepley PetscFunctionReturn(0); 496d1674c6dSMatthew Knepley } 497b0a623aaSMatthew G. Knepley ierr = PetscSectionGetChart(leafSection, &sStart, &sEnd);CHKERRQ(ierr); 498b0a623aaSMatthew G. Knepley ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr); 499d67d17b1SMatthew G. Knepley ierr = DMLabelCreate(PETSC_COMM_SELF, "Overlap adjacency", &ovAdjByRank);CHKERRQ(ierr); 500b0a623aaSMatthew G. Knepley /* Handle leaves: shared with the root point */ 501b0a623aaSMatthew G. Knepley for (l = 0; l < nleaves; ++l) { 502b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, a; 503b0a623aaSMatthew G. Knepley 504df20756eSMatthew G. Knepley ierr = DMPlexGetAdjacency(dm, local ? local[l] : l, &adjSize, &adj);CHKERRQ(ierr); 505b0a623aaSMatthew G. Knepley for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remote[l].rank);CHKERRQ(ierr);} 506b0a623aaSMatthew G. Knepley } 507b0a623aaSMatthew G. Knepley ierr = ISGetIndices(rootrank, &rrank);CHKERRQ(ierr); 508b0a623aaSMatthew G. Knepley ierr = ISGetIndices(leafrank, &nrank);CHKERRQ(ierr); 509b0a623aaSMatthew G. Knepley /* Handle roots */ 510b0a623aaSMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 511b0a623aaSMatthew G. Knepley PetscInt adjSize = PETSC_DETERMINE, neighbors = 0, noff, n, a; 512b0a623aaSMatthew G. Knepley 513b0a623aaSMatthew G. Knepley if ((p >= sStart) && (p < sEnd)) { 514b0a623aaSMatthew G. Knepley /* Some leaves share a root with other leaves on different processes */ 515b0a623aaSMatthew G. Knepley ierr = PetscSectionGetDof(leafSection, p, &neighbors);CHKERRQ(ierr); 516b0a623aaSMatthew G. Knepley if (neighbors) { 517b0a623aaSMatthew G. Knepley ierr = PetscSectionGetOffset(leafSection, p, &noff);CHKERRQ(ierr); 518b0a623aaSMatthew G. Knepley ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr); 519b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 520b0a623aaSMatthew G. Knepley const PetscInt remoteRank = nrank[noff+n]; 521b0a623aaSMatthew G. Knepley 522b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 523b0a623aaSMatthew G. Knepley for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);} 524b0a623aaSMatthew G. Knepley } 525b0a623aaSMatthew G. Knepley } 526b0a623aaSMatthew G. Knepley } 527b0a623aaSMatthew G. Knepley /* Roots are shared with leaves */ 528b0a623aaSMatthew G. Knepley ierr = PetscSectionGetDof(rootSection, p, &neighbors);CHKERRQ(ierr); 529b0a623aaSMatthew G. Knepley if (!neighbors) continue; 530b0a623aaSMatthew G. Knepley ierr = PetscSectionGetOffset(rootSection, p, &noff);CHKERRQ(ierr); 531b0a623aaSMatthew G. Knepley ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr); 532b0a623aaSMatthew G. Knepley for (n = 0; n < neighbors; ++n) { 533b0a623aaSMatthew G. Knepley const PetscInt remoteRank = rrank[noff+n]; 534b0a623aaSMatthew G. Knepley 535b0a623aaSMatthew G. Knepley if (remoteRank == rank) continue; 536b0a623aaSMatthew G. Knepley for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(ovAdjByRank, adj[a], remoteRank);CHKERRQ(ierr);} 537b0a623aaSMatthew G. Knepley } 538b0a623aaSMatthew G. Knepley } 539b0a623aaSMatthew G. Knepley ierr = PetscFree(adj);CHKERRQ(ierr); 540b0a623aaSMatthew G. Knepley ierr = ISRestoreIndices(rootrank, &rrank);CHKERRQ(ierr); 541b0a623aaSMatthew G. Knepley ierr = ISRestoreIndices(leafrank, &nrank);CHKERRQ(ierr); 54224d039d7SMichael Lange /* Add additional overlap levels */ 543be200f8dSMichael Lange for (l = 1; l < levels; l++) { 544be200f8dSMichael Lange /* Propagate point donations over SF to capture remote connections */ 545be200f8dSMichael Lange ierr = DMPlexPartitionLabelPropagate(dm, ovAdjByRank);CHKERRQ(ierr); 546be200f8dSMichael Lange /* Add next level of point donations to the label */ 547be200f8dSMichael Lange ierr = DMPlexPartitionLabelAdjacency(dm, ovAdjByRank);CHKERRQ(ierr); 548be200f8dSMichael Lange } 54926a7d390SMatthew G. Knepley /* We require the closure in the overlap */ 5505abbe4feSMichael Lange ierr = DMPlexPartitionLabelClosure(dm, ovAdjByRank);CHKERRQ(ierr); 551c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-overlap_view", &flg);CHKERRQ(ierr); 552e540f424SMichael Lange if (flg) { 553825f8a23SLisandro Dalcin PetscViewer viewer; 554825f8a23SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &viewer);CHKERRQ(ierr); 555825f8a23SLisandro Dalcin ierr = DMLabelView(ovAdjByRank, viewer);CHKERRQ(ierr); 556b0a623aaSMatthew G. Knepley } 557874ddda9SLisandro Dalcin /* Invert sender to receiver label */ 558d67d17b1SMatthew G. Knepley ierr = DMLabelCreate(PETSC_COMM_SELF, "Overlap label", ovLabel);CHKERRQ(ierr); 559874ddda9SLisandro Dalcin ierr = DMPlexPartitionLabelInvert(dm, ovAdjByRank, NULL, *ovLabel);CHKERRQ(ierr); 560a9f1d5b2SMichael Lange /* Add owned points, except for shared local points */ 561a9f1d5b2SMichael Lange for (p = pStart; p < pEnd; ++p) {ierr = DMLabelSetValue(*ovLabel, p, rank);CHKERRQ(ierr);} 562e540f424SMichael Lange for (l = 0; l < nleaves; ++l) { 563a9f1d5b2SMichael Lange ierr = DMLabelClearValue(*ovLabel, local[l], rank);CHKERRQ(ierr); 564a9f1d5b2SMichael Lange ierr = DMLabelSetValue(*ovLabel, remote[l].index, remote[l].rank);CHKERRQ(ierr); 565e540f424SMichael Lange } 566e540f424SMichael Lange /* Clean up */ 5671fd9873aSMichael Lange ierr = DMLabelDestroy(&ovAdjByRank);CHKERRQ(ierr); 568b0a623aaSMatthew G. Knepley PetscFunctionReturn(0); 569b0a623aaSMatthew G. Knepley } 57070034214SMatthew G. Knepley 57124cc2ca5SMatthew G. Knepley /*@C 57224cc2ca5SMatthew G. Knepley DMPlexCreateOverlapMigrationSF - Create an SF describing the new mesh distribution to make the overlap described by the input SF 57324cc2ca5SMatthew G. Knepley 574d083f849SBarry Smith Collective on dm 57524cc2ca5SMatthew G. Knepley 57624cc2ca5SMatthew G. Knepley Input Parameters: 57724cc2ca5SMatthew G. Knepley + dm - The DM 57824cc2ca5SMatthew G. Knepley - overlapSF - The SF mapping ghost points in overlap to owner points on other processes 57924cc2ca5SMatthew G. Knepley 580064ec1f3Sprj- Output Parameter: 581a2b725a8SWilliam Gropp . migrationSF - An SF that maps original points in old locations to points in new locations 58224cc2ca5SMatthew G. Knepley 58324cc2ca5SMatthew G. Knepley Level: developer 58424cc2ca5SMatthew G. Knepley 585dccdeccaSVaclav Hapla .seealso: DMPlexCreateOverlapLabel(), DMPlexDistributeOverlap(), DMPlexDistribute() 58624cc2ca5SMatthew G. Knepley @*/ 58746f9b1c3SMichael Lange PetscErrorCode DMPlexCreateOverlapMigrationSF(DM dm, PetscSF overlapSF, PetscSF *migrationSF) 58846f9b1c3SMichael Lange { 58946f9b1c3SMichael Lange MPI_Comm comm; 5909852e123SBarry Smith PetscMPIInt rank, size; 59146f9b1c3SMichael Lange PetscInt d, dim, p, pStart, pEnd, nroots, nleaves, newLeaves, point, numSharedPoints; 59246f9b1c3SMichael Lange PetscInt *pointDepths, *remoteDepths, *ilocal; 59346f9b1c3SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 59446f9b1c3SMichael Lange PetscSFNode *iremote; 59546f9b1c3SMichael Lange PetscSF pointSF; 59646f9b1c3SMichael Lange const PetscInt *sharedLocal; 59746f9b1c3SMichael Lange const PetscSFNode *overlapRemote, *sharedRemote; 59846f9b1c3SMichael Lange PetscErrorCode ierr; 59946f9b1c3SMichael Lange 60046f9b1c3SMichael Lange PetscFunctionBegin; 60146f9b1c3SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 60246f9b1c3SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 603ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 604ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 60546f9b1c3SMichael Lange ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 60646f9b1c3SMichael Lange 60746f9b1c3SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 60846f9b1c3SMichael Lange ierr = PetscSFGetGraph(overlapSF, &nroots, &nleaves, NULL, &overlapRemote);CHKERRQ(ierr); 60946f9b1c3SMichael Lange ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr); 61046f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 61146f9b1c3SMichael Lange ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 61246f9b1c3SMichael Lange for (p=pStart; p<pEnd; p++) pointDepths[p] = d; 61346f9b1c3SMichael Lange } 61446f9b1c3SMichael Lange for (p=0; p<nleaves; p++) remoteDepths[p] = -1; 615ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(overlapSF, MPIU_INT, pointDepths, remoteDepths,MPI_REPLACE);CHKERRQ(ierr); 616ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(overlapSF, MPIU_INT, pointDepths, remoteDepths,MPI_REPLACE);CHKERRQ(ierr); 61746f9b1c3SMichael Lange 6182d4ee042Sprj- /* Count received points in each stratum and compute the internal strata shift */ 61946f9b1c3SMichael Lange ierr = PetscMalloc3(dim+1, &depthRecv, dim+1, &depthShift, dim+1, &depthIdx);CHKERRQ(ierr); 62046f9b1c3SMichael Lange for (d=0; d<dim+1; d++) depthRecv[d]=0; 62146f9b1c3SMichael Lange for (p=0; p<nleaves; p++) depthRecv[remoteDepths[p]]++; 62246f9b1c3SMichael Lange depthShift[dim] = 0; 62346f9b1c3SMichael Lange for (d=0; d<dim; d++) depthShift[d] = depthRecv[dim]; 62446f9b1c3SMichael Lange for (d=1; d<dim; d++) depthShift[d] += depthRecv[0]; 62546f9b1c3SMichael Lange for (d=dim-2; d>0; d--) depthShift[d] += depthRecv[d+1]; 62646f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 62746f9b1c3SMichael Lange ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 62846f9b1c3SMichael Lange depthIdx[d] = pStart + depthShift[d]; 62946f9b1c3SMichael Lange } 63046f9b1c3SMichael Lange 63146f9b1c3SMichael Lange /* Form the overlap SF build an SF that describes the full overlap migration SF */ 63246f9b1c3SMichael Lange ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 63346f9b1c3SMichael Lange newLeaves = pEnd - pStart + nleaves; 63409b7985cSMatthew G. Knepley ierr = PetscMalloc1(newLeaves, &ilocal);CHKERRQ(ierr); 63509b7985cSMatthew G. Knepley ierr = PetscMalloc1(newLeaves, &iremote);CHKERRQ(ierr); 63646f9b1c3SMichael Lange /* First map local points to themselves */ 63746f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 63846f9b1c3SMichael Lange ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 63946f9b1c3SMichael Lange for (p=pStart; p<pEnd; p++) { 64046f9b1c3SMichael Lange point = p + depthShift[d]; 64146f9b1c3SMichael Lange ilocal[point] = point; 64246f9b1c3SMichael Lange iremote[point].index = p; 64346f9b1c3SMichael Lange iremote[point].rank = rank; 64446f9b1c3SMichael Lange depthIdx[d]++; 64546f9b1c3SMichael Lange } 64646f9b1c3SMichael Lange } 64746f9b1c3SMichael Lange 64846f9b1c3SMichael Lange /* Add in the remote roots for currently shared points */ 64946f9b1c3SMichael Lange ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr); 65046f9b1c3SMichael Lange ierr = PetscSFGetGraph(pointSF, NULL, &numSharedPoints, &sharedLocal, &sharedRemote);CHKERRQ(ierr); 65146f9b1c3SMichael Lange for (d=0; d<dim+1; d++) { 65246f9b1c3SMichael Lange ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 65346f9b1c3SMichael Lange for (p=0; p<numSharedPoints; p++) { 65446f9b1c3SMichael Lange if (pStart <= sharedLocal[p] && sharedLocal[p] < pEnd) { 65546f9b1c3SMichael Lange point = sharedLocal[p] + depthShift[d]; 65646f9b1c3SMichael Lange iremote[point].index = sharedRemote[p].index; 65746f9b1c3SMichael Lange iremote[point].rank = sharedRemote[p].rank; 65846f9b1c3SMichael Lange } 65946f9b1c3SMichael Lange } 66046f9b1c3SMichael Lange } 66146f9b1c3SMichael Lange 66246f9b1c3SMichael Lange /* Now add the incoming overlap points */ 66346f9b1c3SMichael Lange for (p=0; p<nleaves; p++) { 66446f9b1c3SMichael Lange point = depthIdx[remoteDepths[p]]; 66546f9b1c3SMichael Lange ilocal[point] = point; 66646f9b1c3SMichael Lange iremote[point].index = overlapRemote[p].index; 66746f9b1c3SMichael Lange iremote[point].rank = overlapRemote[p].rank; 66846f9b1c3SMichael Lange depthIdx[remoteDepths[p]]++; 66946f9b1c3SMichael Lange } 67015fff7beSMatthew G. Knepley ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr); 67146f9b1c3SMichael Lange 67246f9b1c3SMichael Lange ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr); 67346f9b1c3SMichael Lange ierr = PetscObjectSetName((PetscObject) *migrationSF, "Overlap Migration SF");CHKERRQ(ierr); 67446f9b1c3SMichael Lange ierr = PetscSFSetFromOptions(*migrationSF);CHKERRQ(ierr); 67546f9b1c3SMichael Lange ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 67646f9b1c3SMichael Lange ierr = PetscSFSetGraph(*migrationSF, pEnd-pStart, newLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr); 67746f9b1c3SMichael Lange 67846f9b1c3SMichael Lange ierr = PetscFree3(depthRecv, depthShift, depthIdx);CHKERRQ(ierr); 67970034214SMatthew G. Knepley PetscFunctionReturn(0); 68070034214SMatthew G. Knepley } 68170034214SMatthew G. Knepley 682a9f1d5b2SMichael Lange /*@ 683f0e73a3dSToby Isaac DMPlexStratifyMigrationSF - Rearrange the leaves of a migration sf for stratification. 684a9f1d5b2SMichael Lange 685064ec1f3Sprj- Input Parameters: 686a9f1d5b2SMichael Lange + dm - The DM 687a9f1d5b2SMichael Lange - sf - A star forest with non-ordered leaves, usually defining a DM point migration 688a9f1d5b2SMichael Lange 689a9f1d5b2SMichael Lange Output Parameter: 690a9f1d5b2SMichael Lange . migrationSF - A star forest with added leaf indirection that ensures the resulting DM is stratified 691a9f1d5b2SMichael Lange 692412e9a14SMatthew G. Knepley Note: 693412e9a14SMatthew G. Knepley This lexicographically sorts by (depth, cellType) 694412e9a14SMatthew G. Knepley 695a9f1d5b2SMichael Lange Level: developer 696a9f1d5b2SMichael Lange 697a9f1d5b2SMichael Lange .seealso: DMPlexPartitionLabelCreateSF(), DMPlexDistribute(), DMPlexDistributeOverlap() 698a9f1d5b2SMichael Lange @*/ 699a9f1d5b2SMichael Lange PetscErrorCode DMPlexStratifyMigrationSF(DM dm, PetscSF sf, PetscSF *migrationSF) 700a9f1d5b2SMichael Lange { 701a9f1d5b2SMichael Lange MPI_Comm comm; 7029852e123SBarry Smith PetscMPIInt rank, size; 703412e9a14SMatthew G. Knepley PetscInt d, ldepth, depth, dim, p, pStart, pEnd, nroots, nleaves; 704412e9a14SMatthew G. Knepley PetscSFNode *pointDepths, *remoteDepths; 705412e9a14SMatthew G. Knepley PetscInt *ilocal; 706a9f1d5b2SMichael Lange PetscInt *depthRecv, *depthShift, *depthIdx; 707412e9a14SMatthew G. Knepley PetscInt *ctRecv, *ctShift, *ctIdx; 708a9f1d5b2SMichael Lange const PetscSFNode *iremote; 709a9f1d5b2SMichael Lange PetscErrorCode ierr; 710a9f1d5b2SMichael Lange 711a9f1d5b2SMichael Lange PetscFunctionBegin; 712a9f1d5b2SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 713a9f1d5b2SMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 714ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 715ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 7167fab53ddSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &ldepth);CHKERRQ(ierr); 717412e9a14SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 718820f2d46SBarry Smith ierr = MPIU_Allreduce(&ldepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRMPI(ierr); 7197fab53ddSMatthew G. Knepley if ((ldepth >= 0) && (depth != ldepth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", ldepth, depth); 72030b0ce1bSStefano Zampini ierr = PetscLogEventBegin(DMPLEX_PartStratSF,dm,0,0,0);CHKERRQ(ierr); 721a9f1d5b2SMichael Lange 722a9f1d5b2SMichael Lange /* Before building the migration SF we need to know the new stratum offsets */ 723a9f1d5b2SMichael Lange ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, &iremote);CHKERRQ(ierr); 724a9f1d5b2SMichael Lange ierr = PetscMalloc2(nroots, &pointDepths, nleaves, &remoteDepths);CHKERRQ(ierr); 7257fab53ddSMatthew G. Knepley for (d = 0; d < depth+1; ++d) { 726a9f1d5b2SMichael Lange ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 727f0e73a3dSToby Isaac for (p = pStart; p < pEnd; ++p) { 728412e9a14SMatthew G. Knepley DMPolytopeType ct; 729f0e73a3dSToby Isaac 730412e9a14SMatthew G. Knepley ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr); 731412e9a14SMatthew G. Knepley pointDepths[p].index = d; 732412e9a14SMatthew G. Knepley pointDepths[p].rank = ct; 733f0e73a3dSToby Isaac } 734412e9a14SMatthew G. Knepley } 735412e9a14SMatthew G. Knepley for (p = 0; p < nleaves; ++p) {remoteDepths[p].index = -1; remoteDepths[p].rank = -1;} 736ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(sf, MPIU_2INT, pointDepths, remoteDepths,MPI_REPLACE);CHKERRQ(ierr); 737ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(sf, MPIU_2INT, pointDepths, remoteDepths,MPI_REPLACE);CHKERRQ(ierr); 738412e9a14SMatthew G. Knepley /* Count received points in each stratum and compute the internal strata shift */ 739412e9a14SMatthew G. Knepley ierr = PetscCalloc6(depth+1, &depthRecv, depth+1, &depthShift, depth+1, &depthIdx, DM_NUM_POLYTOPES, &ctRecv, DM_NUM_POLYTOPES, &ctShift, DM_NUM_POLYTOPES, &ctIdx);CHKERRQ(ierr); 740412e9a14SMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 741412e9a14SMatthew G. Knepley if (remoteDepths[p].rank < 0) { 742412e9a14SMatthew G. Knepley ++depthRecv[remoteDepths[p].index]; 743412e9a14SMatthew G. Knepley } else { 744412e9a14SMatthew G. Knepley ++ctRecv[remoteDepths[p].rank]; 745412e9a14SMatthew G. Knepley } 746412e9a14SMatthew G. Knepley } 747412e9a14SMatthew G. Knepley { 748412e9a14SMatthew G. Knepley PetscInt depths[4], dims[4], shift = 0, i, c; 749412e9a14SMatthew G. Knepley 7508238f61eSMatthew G. Knepley /* Cells (depth), Vertices (0), Faces (depth-1), Edges (1) 7518238f61eSMatthew G. Knepley Consider DM_POLYTOPE_FV_GHOST and DM_POLYTOPE_INTERIOR_GHOST as cells 7528238f61eSMatthew G. Knepley */ 753412e9a14SMatthew G. Knepley depths[0] = depth; depths[1] = 0; depths[2] = depth-1; depths[3] = 1; 754412e9a14SMatthew G. Knepley dims[0] = dim; dims[1] = 0; dims[2] = dim-1; dims[3] = 1; 755412e9a14SMatthew G. Knepley for (i = 0; i <= depth; ++i) { 756412e9a14SMatthew G. Knepley const PetscInt dep = depths[i]; 757412e9a14SMatthew G. Knepley const PetscInt dim = dims[i]; 758412e9a14SMatthew G. Knepley 759412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 7608238f61eSMatthew G. Knepley if (DMPolytopeTypeGetDim((DMPolytopeType) c) != dim && !(i == 0 && (c == DM_POLYTOPE_FV_GHOST || c == DM_POLYTOPE_INTERIOR_GHOST))) continue; 761412e9a14SMatthew G. Knepley ctShift[c] = shift; 762412e9a14SMatthew G. Knepley shift += ctRecv[c]; 763412e9a14SMatthew G. Knepley } 764412e9a14SMatthew G. Knepley depthShift[dep] = shift; 765412e9a14SMatthew G. Knepley shift += depthRecv[dep]; 766412e9a14SMatthew G. Knepley } 767412e9a14SMatthew G. Knepley for (c = 0; c < DM_NUM_POLYTOPES; ++c) { 768412e9a14SMatthew G. Knepley const PetscInt ctDim = DMPolytopeTypeGetDim((DMPolytopeType) c); 769412e9a14SMatthew G. Knepley 7708238f61eSMatthew G. Knepley if ((ctDim < 0 || ctDim > dim) && (c != DM_POLYTOPE_FV_GHOST && c != DM_POLYTOPE_INTERIOR_GHOST)) { 771412e9a14SMatthew G. Knepley ctShift[c] = shift; 772412e9a14SMatthew G. Knepley shift += ctRecv[c]; 773412e9a14SMatthew G. Knepley } 774412e9a14SMatthew G. Knepley } 775412e9a14SMatthew G. Knepley } 776a9f1d5b2SMichael Lange /* Derive a new local permutation based on stratified indices */ 777a9f1d5b2SMichael Lange ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr); 7787fab53ddSMatthew G. Knepley for (p = 0; p < nleaves; ++p) { 779412e9a14SMatthew G. Knepley const PetscInt dep = remoteDepths[p].index; 780412e9a14SMatthew G. Knepley const DMPolytopeType ct = (DMPolytopeType) remoteDepths[p].rank; 7817fab53ddSMatthew G. Knepley 782412e9a14SMatthew G. Knepley if ((PetscInt) ct < 0) { 7837fab53ddSMatthew G. Knepley ilocal[p] = depthShift[dep] + depthIdx[dep]; 784412e9a14SMatthew G. Knepley ++depthIdx[dep]; 785412e9a14SMatthew G. Knepley } else { 786412e9a14SMatthew G. Knepley ilocal[p] = ctShift[ct] + ctIdx[ct]; 787412e9a14SMatthew G. Knepley ++ctIdx[ct]; 788412e9a14SMatthew G. Knepley } 789a9f1d5b2SMichael Lange } 790a9f1d5b2SMichael Lange ierr = PetscSFCreate(comm, migrationSF);CHKERRQ(ierr); 791a9f1d5b2SMichael Lange ierr = PetscObjectSetName((PetscObject) *migrationSF, "Migration SF");CHKERRQ(ierr); 792a9f1d5b2SMichael Lange ierr = PetscSFSetGraph(*migrationSF, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_COPY_VALUES);CHKERRQ(ierr); 793a9f1d5b2SMichael Lange ierr = PetscFree2(pointDepths,remoteDepths);CHKERRQ(ierr); 794412e9a14SMatthew G. Knepley ierr = PetscFree6(depthRecv, depthShift, depthIdx, ctRecv, ctShift, ctIdx);CHKERRQ(ierr); 79530b0ce1bSStefano Zampini ierr = PetscLogEventEnd(DMPLEX_PartStratSF,dm,0,0,0);CHKERRQ(ierr); 796a9f1d5b2SMichael Lange PetscFunctionReturn(0); 797a9f1d5b2SMichael Lange } 798a9f1d5b2SMichael Lange 79970034214SMatthew G. Knepley /*@ 80070034214SMatthew G. Knepley DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 80170034214SMatthew G. Knepley 802d083f849SBarry Smith Collective on dm 80370034214SMatthew G. Knepley 80470034214SMatthew G. Knepley Input Parameters: 80570034214SMatthew G. Knepley + dm - The DMPlex object 80670034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 80770034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 808cb15cd0eSMatthew G. Knepley - originalVec - The existing data in a local vector 80970034214SMatthew G. Knepley 81070034214SMatthew G. Knepley Output Parameters: 81170034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 812cb15cd0eSMatthew G. Knepley - newVec - The new data in a local vector 81370034214SMatthew G. Knepley 81470034214SMatthew G. Knepley Level: developer 81570034214SMatthew G. Knepley 81670034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeFieldIS(), DMPlexDistributeData() 81770034214SMatthew G. Knepley @*/ 81870034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec) 81970034214SMatthew G. Knepley { 82070034214SMatthew G. Knepley PetscSF fieldSF; 82170034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 82270034214SMatthew G. Knepley PetscScalar *originalValues, *newValues; 82370034214SMatthew G. Knepley PetscErrorCode ierr; 82470034214SMatthew G. Knepley 82570034214SMatthew G. Knepley PetscFunctionBegin; 82670034214SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr); 82770034214SMatthew G. Knepley ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr); 82870034214SMatthew G. Knepley 82970034214SMatthew G. Knepley ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr); 83070034214SMatthew G. Knepley ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr); 83170034214SMatthew G. Knepley ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr); 83270034214SMatthew G. Knepley 83370034214SMatthew G. Knepley ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr); 83470034214SMatthew G. Knepley ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr); 83570034214SMatthew G. Knepley ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr); 8368a713f3bSLawrence Mitchell ierr = PetscFree(remoteOffsets);CHKERRQ(ierr); 837ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues,MPI_REPLACE);CHKERRQ(ierr); 838ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues,MPI_REPLACE);CHKERRQ(ierr); 83970034214SMatthew G. Knepley ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr); 84070034214SMatthew G. Knepley ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr); 84170034214SMatthew G. Knepley ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr); 84270034214SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr); 84370034214SMatthew G. Knepley PetscFunctionReturn(0); 84470034214SMatthew G. Knepley } 84570034214SMatthew G. Knepley 84670034214SMatthew G. Knepley /*@ 84770034214SMatthew G. Knepley DMPlexDistributeFieldIS - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 84870034214SMatthew G. Knepley 849d083f849SBarry Smith Collective on dm 85070034214SMatthew G. Knepley 85170034214SMatthew G. Knepley Input Parameters: 85270034214SMatthew G. Knepley + dm - The DMPlex object 85370034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 85470034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 85570034214SMatthew G. Knepley - originalIS - The existing data 85670034214SMatthew G. Knepley 85770034214SMatthew G. Knepley Output Parameters: 85870034214SMatthew G. Knepley + newSection - The PetscSF describing the new data layout 85970034214SMatthew G. Knepley - newIS - The new data 86070034214SMatthew G. Knepley 86170034214SMatthew G. Knepley Level: developer 86270034214SMatthew G. Knepley 86370034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexDistributeData() 86470034214SMatthew G. Knepley @*/ 86570034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeFieldIS(DM dm, PetscSF pointSF, PetscSection originalSection, IS originalIS, PetscSection newSection, IS *newIS) 86670034214SMatthew G. Knepley { 86770034214SMatthew G. Knepley PetscSF fieldSF; 86870034214SMatthew G. Knepley PetscInt *newValues, *remoteOffsets, fieldSize; 86970034214SMatthew G. Knepley const PetscInt *originalValues; 87070034214SMatthew G. Knepley PetscErrorCode ierr; 87170034214SMatthew G. Knepley 87270034214SMatthew G. Knepley PetscFunctionBegin; 87370034214SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr); 87470034214SMatthew G. Knepley ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr); 87570034214SMatthew G. Knepley 87670034214SMatthew G. Knepley ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr); 877854ce69bSBarry Smith ierr = PetscMalloc1(fieldSize, &newValues);CHKERRQ(ierr); 87870034214SMatthew G. Knepley 87970034214SMatthew G. Knepley ierr = ISGetIndices(originalIS, &originalValues);CHKERRQ(ierr); 88070034214SMatthew G. Knepley ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr); 8818a713f3bSLawrence Mitchell ierr = PetscFree(remoteOffsets);CHKERRQ(ierr); 882ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues,MPI_REPLACE);CHKERRQ(ierr); 883ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(fieldSF, MPIU_INT, (PetscInt *) originalValues, newValues,MPI_REPLACE);CHKERRQ(ierr); 88470034214SMatthew G. Knepley ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr); 88570034214SMatthew G. Knepley ierr = ISRestoreIndices(originalIS, &originalValues);CHKERRQ(ierr); 88670034214SMatthew G. Knepley ierr = ISCreateGeneral(PetscObjectComm((PetscObject) pointSF), fieldSize, newValues, PETSC_OWN_POINTER, newIS);CHKERRQ(ierr); 88770034214SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr); 88870034214SMatthew G. Knepley PetscFunctionReturn(0); 88970034214SMatthew G. Knepley } 89070034214SMatthew G. Knepley 89170034214SMatthew G. Knepley /*@ 89270034214SMatthew G. Knepley DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution 89370034214SMatthew G. Knepley 894d083f849SBarry Smith Collective on dm 89570034214SMatthew G. Knepley 89670034214SMatthew G. Knepley Input Parameters: 89770034214SMatthew G. Knepley + dm - The DMPlex object 89870034214SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern 89970034214SMatthew G. Knepley . originalSection - The PetscSection for existing data layout 90070034214SMatthew G. Knepley . datatype - The type of data 90170034214SMatthew G. Knepley - originalData - The existing data 90270034214SMatthew G. Knepley 90370034214SMatthew G. Knepley Output Parameters: 90470034214SMatthew G. Knepley + newSection - The PetscSection describing the new data layout 90570034214SMatthew G. Knepley - newData - The new data 90670034214SMatthew G. Knepley 90770034214SMatthew G. Knepley Level: developer 90870034214SMatthew G. Knepley 90970034214SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField() 91070034214SMatthew G. Knepley @*/ 91170034214SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData) 91270034214SMatthew G. Knepley { 91370034214SMatthew G. Knepley PetscSF fieldSF; 91470034214SMatthew G. Knepley PetscInt *remoteOffsets, fieldSize; 91570034214SMatthew G. Knepley PetscMPIInt dataSize; 91670034214SMatthew G. Knepley PetscErrorCode ierr; 91770034214SMatthew G. Knepley 91870034214SMatthew G. Knepley PetscFunctionBegin; 91970034214SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr); 92070034214SMatthew G. Knepley ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr); 92170034214SMatthew G. Knepley 92270034214SMatthew G. Knepley ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr); 923ffc4695bSBarry Smith ierr = MPI_Type_size(datatype, &dataSize);CHKERRMPI(ierr); 92470034214SMatthew G. Knepley ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr); 92570034214SMatthew G. Knepley 92670034214SMatthew G. Knepley ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr); 9278a713f3bSLawrence Mitchell ierr = PetscFree(remoteOffsets);CHKERRQ(ierr); 928ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData,MPI_REPLACE);CHKERRQ(ierr); 929ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData,MPI_REPLACE);CHKERRQ(ierr); 93070034214SMatthew G. Knepley ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr); 93170034214SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr); 93270034214SMatthew G. Knepley PetscFunctionReturn(0); 93370034214SMatthew G. Knepley } 93470034214SMatthew G. Knepley 93524cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCones(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 936cc71bff1SMichael Lange { 937cc71bff1SMichael Lange DM_Plex *pmesh = (DM_Plex*) (dmParallel)->data; 938cc71bff1SMichael Lange MPI_Comm comm; 939cc71bff1SMichael Lange PetscSF coneSF; 940cc71bff1SMichael Lange PetscSection originalConeSection, newConeSection; 941ac04eaf7SToby Isaac PetscInt *remoteOffsets, *cones, *globCones, *newCones, newConesSize; 942cc71bff1SMichael Lange PetscBool flg; 943cc71bff1SMichael Lange PetscErrorCode ierr; 944cc71bff1SMichael Lange 945cc71bff1SMichael Lange PetscFunctionBegin; 946cc71bff1SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 9470c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 9480c86c063SLisandro Dalcin ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr); 949cc71bff1SMichael Lange /* Distribute cone section */ 950cc71bff1SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 951cc71bff1SMichael Lange ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr); 952cc71bff1SMichael Lange ierr = DMPlexGetConeSection(dmParallel, &newConeSection);CHKERRQ(ierr); 953cc71bff1SMichael Lange ierr = PetscSFDistributeSection(migrationSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr); 954cc71bff1SMichael Lange ierr = DMSetUp(dmParallel);CHKERRQ(ierr); 955cc71bff1SMichael Lange { 956cc71bff1SMichael Lange PetscInt pStart, pEnd, p; 957cc71bff1SMichael Lange 958cc71bff1SMichael Lange ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr); 959cc71bff1SMichael Lange for (p = pStart; p < pEnd; ++p) { 960cc71bff1SMichael Lange PetscInt coneSize; 961cc71bff1SMichael Lange ierr = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr); 962cc71bff1SMichael Lange pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize); 963cc71bff1SMichael Lange } 964cc71bff1SMichael Lange } 965cc71bff1SMichael Lange /* Communicate and renumber cones */ 966cc71bff1SMichael Lange ierr = PetscSFCreateSectionSF(migrationSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr); 9678a713f3bSLawrence Mitchell ierr = PetscFree(remoteOffsets);CHKERRQ(ierr); 968cc71bff1SMichael Lange ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr); 969ac04eaf7SToby Isaac if (original) { 970ac04eaf7SToby Isaac PetscInt numCones; 971ac04eaf7SToby Isaac 972367003a6SStefano Zampini ierr = PetscSectionGetStorageSize(originalConeSection,&numCones);CHKERRQ(ierr); 973367003a6SStefano Zampini ierr = PetscMalloc1(numCones,&globCones);CHKERRQ(ierr); 974ac04eaf7SToby Isaac ierr = ISLocalToGlobalMappingApplyBlock(original, numCones, cones, globCones);CHKERRQ(ierr); 975367003a6SStefano Zampini } else { 976ac04eaf7SToby Isaac globCones = cones; 977ac04eaf7SToby Isaac } 978cc71bff1SMichael Lange ierr = DMPlexGetCones(dmParallel, &newCones);CHKERRQ(ierr); 979ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(coneSF, MPIU_INT, globCones, newCones,MPI_REPLACE);CHKERRQ(ierr); 980ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(coneSF, MPIU_INT, globCones, newCones,MPI_REPLACE);CHKERRQ(ierr); 981ac04eaf7SToby Isaac if (original) { 982ac04eaf7SToby Isaac ierr = PetscFree(globCones);CHKERRQ(ierr); 983ac04eaf7SToby Isaac } 984cc71bff1SMichael Lange ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr); 985cc71bff1SMichael Lange ierr = ISGlobalToLocalMappingApplyBlock(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr); 98676bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 9873533c52bSMatthew G. Knepley PetscInt p; 9883533c52bSMatthew G. Knepley PetscBool valid = PETSC_TRUE; 9893533c52bSMatthew G. Knepley for (p = 0; p < newConesSize; ++p) { 990367003a6SStefano Zampini if (newCones[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] Point %D not in overlap SF\n", PetscGlobalRank,p);CHKERRQ(ierr);} 9913533c52bSMatthew G. Knepley } 9923533c52bSMatthew G. Knepley if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 9933533c52bSMatthew G. Knepley } 994c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr); 995cc71bff1SMichael Lange if (flg) { 996cc71bff1SMichael Lange ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr); 997a8c5bd36SStefano Zampini ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 998cc71bff1SMichael Lange ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr); 999a8c5bd36SStefano Zampini ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 1000cc71bff1SMichael Lange ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr); 1001cc71bff1SMichael Lange } 1002cc71bff1SMichael Lange ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr); 1003cc71bff1SMichael Lange ierr = DMPlexGetConeOrientations(dmParallel, &newCones);CHKERRQ(ierr); 1004ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones,MPI_REPLACE);CHKERRQ(ierr); 1005ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones,MPI_REPLACE);CHKERRQ(ierr); 1006cc71bff1SMichael Lange ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr); 1007cc71bff1SMichael Lange ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr); 1008eaf898f9SPatrick Sanan /* Create supports and stratify DMPlex */ 1009cc71bff1SMichael Lange { 1010cc71bff1SMichael Lange PetscInt pStart, pEnd; 1011cc71bff1SMichael Lange 1012cc71bff1SMichael Lange ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr); 1013cc71bff1SMichael Lange ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr); 1014cc71bff1SMichael Lange } 1015cc71bff1SMichael Lange ierr = DMPlexSymmetrize(dmParallel);CHKERRQ(ierr); 1016cc71bff1SMichael Lange ierr = DMPlexStratify(dmParallel);CHKERRQ(ierr); 10171cf84007SMatthew G. Knepley { 10181cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 10191cf84007SMatthew G. Knepley 1020b0441da4SMatthew G. Knepley ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr); 1021b0441da4SMatthew G. Knepley ierr = DMSetBasicAdjacency(dmParallel, useCone, useClosure);CHKERRQ(ierr); 10221cf84007SMatthew G. Knepley ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr); 10231cf84007SMatthew G. Knepley ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr); 10241cf84007SMatthew G. Knepley } 1025cc71bff1SMichael Lange PetscFunctionReturn(0); 1026cc71bff1SMichael Lange } 1027cc71bff1SMichael Lange 102824cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeCoordinates(DM dm, PetscSF migrationSF, DM dmParallel) 10290df0e737SMichael Lange { 10300df0e737SMichael Lange MPI_Comm comm; 10319318fe57SMatthew G. Knepley DM cdm, cdmParallel; 10320df0e737SMichael Lange PetscSection originalCoordSection, newCoordSection; 10330df0e737SMichael Lange Vec originalCoordinates, newCoordinates; 10340df0e737SMichael Lange PetscInt bs; 103590b157c4SStefano Zampini PetscBool isper; 10360df0e737SMichael Lange const char *name; 10370df0e737SMichael Lange const PetscReal *maxCell, *L; 10385dc8c3f7SMatthew G. Knepley const DMBoundaryType *bd; 10390df0e737SMichael Lange PetscErrorCode ierr; 10400df0e737SMichael Lange 10410df0e737SMichael Lange PetscFunctionBegin; 10420df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10430c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 10440df0e737SMichael Lange 10450df0e737SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 10460df0e737SMichael Lange ierr = DMGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr); 10470df0e737SMichael Lange ierr = DMGetCoordinateSection(dmParallel, &newCoordSection);CHKERRQ(ierr); 10480df0e737SMichael Lange ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr); 10490df0e737SMichael Lange if (originalCoordinates) { 10508b9ced59SLisandro Dalcin ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr); 10510df0e737SMichael Lange ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr); 10520df0e737SMichael Lange ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr); 10530df0e737SMichael Lange 10540df0e737SMichael Lange ierr = DMPlexDistributeField(dm, migrationSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr); 10550df0e737SMichael Lange ierr = DMSetCoordinatesLocal(dmParallel, newCoordinates);CHKERRQ(ierr); 10560df0e737SMichael Lange ierr = VecGetBlockSize(originalCoordinates, &bs);CHKERRQ(ierr); 10570df0e737SMichael Lange ierr = VecSetBlockSize(newCoordinates, bs);CHKERRQ(ierr); 10580df0e737SMichael Lange ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr); 10590df0e737SMichael Lange } 106090b157c4SStefano Zampini ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr); 106190b157c4SStefano Zampini ierr = DMSetPeriodicity(dmParallel, isper, maxCell, L, bd);CHKERRQ(ierr); 10629318fe57SMatthew G. Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 10639318fe57SMatthew G. Knepley ierr = DMGetCoordinateDM(dmParallel, &cdmParallel);CHKERRQ(ierr); 10649318fe57SMatthew G. Knepley ierr = DMCopyDisc(cdm, cdmParallel);CHKERRQ(ierr); 10650df0e737SMichael Lange PetscFunctionReturn(0); 10660df0e737SMichael Lange } 10670df0e737SMichael Lange 106824cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeLabels(DM dm, PetscSF migrationSF, DM dmParallel) 10690df0e737SMichael Lange { 1070df0420ecSMatthew G. Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 10710df0e737SMichael Lange MPI_Comm comm; 10727980c9d4SMatthew G. Knepley DMLabel depthLabel; 10730df0e737SMichael Lange PetscMPIInt rank; 10747980c9d4SMatthew G. Knepley PetscInt depth, d, numLabels, numLocalLabels, l; 1075df0420ecSMatthew G. Knepley PetscBool hasLabels = PETSC_FALSE, lsendDepth, sendDepth; 1076df0420ecSMatthew G. Knepley PetscObjectState depthState = -1; 10770df0e737SMichael Lange PetscErrorCode ierr; 10780df0e737SMichael Lange 10790df0e737SMichael Lange PetscFunctionBegin; 10800df0e737SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10810c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 10820c86c063SLisandro Dalcin 10830df0e737SMichael Lange ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr); 10840df0e737SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 1085ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 10860df0e737SMichael Lange 1087df0420ecSMatthew G. Knepley /* If the user has changed the depth label, communicate it instead */ 10887980c9d4SMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 10897980c9d4SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1090d67d17b1SMatthew G. Knepley if (depthLabel) {ierr = PetscObjectStateGet((PetscObject) depthLabel, &depthState);CHKERRQ(ierr);} 1091df0420ecSMatthew G. Knepley lsendDepth = mesh->depthState != depthState ? PETSC_TRUE : PETSC_FALSE; 1092820f2d46SBarry Smith ierr = MPIU_Allreduce(&lsendDepth, &sendDepth, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr); 1093df0420ecSMatthew G. Knepley if (sendDepth) { 1094a7998027SVaclav Hapla ierr = DMPlexGetDepthLabel(dmParallel, &dmParallel->depthLabel);CHKERRQ(ierr); 1095a7998027SVaclav Hapla ierr = DMRemoveLabelBySelf(dmParallel, &dmParallel->depthLabel, PETSC_FALSE);CHKERRQ(ierr); 1096df0420ecSMatthew G. Knepley } 1097d995df53SMatthew G. Knepley /* Everyone must have either the same number of labels, or none */ 1098c58f1c22SToby Isaac ierr = DMGetNumLabels(dm, &numLocalLabels);CHKERRQ(ierr); 1099d995df53SMatthew G. Knepley numLabels = numLocalLabels; 1100ffc4695bSBarry Smith ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr); 1101627847f0SMatthew G. Knepley if (numLabels == numLocalLabels) hasLabels = PETSC_TRUE; 11025d80c0bfSVaclav Hapla for (l = 0; l < numLabels; ++l) { 1103bdd2d751SMichael Lange DMLabel label = NULL, labelNew = NULL; 110483e10cb3SLisandro Dalcin PetscBool isDepth, lisOutput = PETSC_TRUE, isOutput; 1105d67d17b1SMatthew G. Knepley const char *name = NULL; 11060df0e737SMichael Lange 1107d67d17b1SMatthew G. Knepley if (hasLabels) { 1108d67d17b1SMatthew G. Knepley ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr); 11090df0e737SMichael Lange /* Skip "depth" because it is recreated */ 1110d67d17b1SMatthew G. Knepley ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr); 1111d67d17b1SMatthew G. Knepley ierr = PetscStrcmp(name, "depth", &isDepth);CHKERRQ(ierr); 1112d67d17b1SMatthew G. Knepley } 1113ffc4695bSBarry Smith ierr = MPI_Bcast(&isDepth, 1, MPIU_BOOL, 0, comm);CHKERRMPI(ierr); 111483e10cb3SLisandro Dalcin if (isDepth && !sendDepth) continue; 1115bdd2d751SMichael Lange ierr = DMLabelDistribute(label, migrationSF, &labelNew);CHKERRQ(ierr); 111683e10cb3SLisandro Dalcin if (isDepth) { 11177980c9d4SMatthew G. Knepley /* Put in any missing strata which can occur if users are managing the depth label themselves */ 11187980c9d4SMatthew G. Knepley PetscInt gdepth; 11197980c9d4SMatthew G. Knepley 1120820f2d46SBarry Smith ierr = MPIU_Allreduce(&depth, &gdepth, 1, MPIU_INT, MPI_MAX, comm);CHKERRMPI(ierr); 11217980c9d4SMatthew G. Knepley if ((depth >= 0) && (gdepth != depth)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent Plex depth %d != %d", depth, gdepth); 11227980c9d4SMatthew G. Knepley for (d = 0; d <= gdepth; ++d) { 11237980c9d4SMatthew G. Knepley PetscBool has; 11247980c9d4SMatthew G. Knepley 11257980c9d4SMatthew G. Knepley ierr = DMLabelHasStratum(labelNew, d, &has);CHKERRQ(ierr); 112683e10cb3SLisandro Dalcin if (!has) {ierr = DMLabelAddStratum(labelNew, d);CHKERRQ(ierr);} 11277980c9d4SMatthew G. Knepley } 11287980c9d4SMatthew G. Knepley } 1129c58f1c22SToby Isaac ierr = DMAddLabel(dmParallel, labelNew);CHKERRQ(ierr); 113083e10cb3SLisandro Dalcin /* Put the output flag in the new label */ 1131d67d17b1SMatthew G. Knepley if (hasLabels) {ierr = DMGetLabelOutput(dm, name, &lisOutput);CHKERRQ(ierr);} 1132820f2d46SBarry Smith ierr = MPIU_Allreduce(&lisOutput, &isOutput, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr); 1133d67d17b1SMatthew G. Knepley ierr = PetscObjectGetName((PetscObject) labelNew, &name);CHKERRQ(ierr); 1134d67d17b1SMatthew G. Knepley ierr = DMSetLabelOutput(dmParallel, name, isOutput);CHKERRQ(ierr); 113508f633c4SVaclav Hapla ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr); 11360df0e737SMichael Lange } 11370df0e737SMichael Lange ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr); 11380df0e737SMichael Lange PetscFunctionReturn(0); 11390df0e737SMichael Lange } 11400df0e737SMichael Lange 114124cc2ca5SMatthew G. Knepley static PetscErrorCode DMPlexDistributeSetupTree(DM dm, PetscSF migrationSF, ISLocalToGlobalMapping original, ISLocalToGlobalMapping renumbering, DM dmParallel) 1142a6f36705SMichael Lange { 114315078cd4SMichael Lange DM_Plex *mesh = (DM_Plex*) dm->data; 114415078cd4SMichael Lange DM_Plex *pmesh = (DM_Plex*) (dmParallel)->data; 1145a6f36705SMichael Lange MPI_Comm comm; 1146a6f36705SMichael Lange DM refTree; 1147a6f36705SMichael Lange PetscSection origParentSection, newParentSection; 1148a6f36705SMichael Lange PetscInt *origParents, *origChildIDs; 1149a6f36705SMichael Lange PetscBool flg; 1150a6f36705SMichael Lange PetscErrorCode ierr; 1151a6f36705SMichael Lange 1152a6f36705SMichael Lange PetscFunctionBegin; 1153a6f36705SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11540c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 5); 1155a6f36705SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 1156a6f36705SMichael Lange 1157a6f36705SMichael Lange /* Set up tree */ 1158a6f36705SMichael Lange ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr); 1159a6f36705SMichael Lange ierr = DMPlexSetReferenceTree(dmParallel,refTree);CHKERRQ(ierr); 1160a6f36705SMichael Lange ierr = DMPlexGetTree(dm,&origParentSection,&origParents,&origChildIDs,NULL,NULL);CHKERRQ(ierr); 1161a6f36705SMichael Lange if (origParentSection) { 1162a6f36705SMichael Lange PetscInt pStart, pEnd; 116308633170SToby Isaac PetscInt *newParents, *newChildIDs, *globParents; 1164a6f36705SMichael Lange PetscInt *remoteOffsetsParents, newParentSize; 1165a6f36705SMichael Lange PetscSF parentSF; 1166a6f36705SMichael Lange 1167a6f36705SMichael Lange ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr); 1168a6f36705SMichael Lange ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmParallel),&newParentSection);CHKERRQ(ierr); 1169a6f36705SMichael Lange ierr = PetscSectionSetChart(newParentSection,pStart,pEnd);CHKERRQ(ierr); 1170a6f36705SMichael Lange ierr = PetscSFDistributeSection(migrationSF, origParentSection, &remoteOffsetsParents, newParentSection);CHKERRQ(ierr); 1171a6f36705SMichael Lange ierr = PetscSFCreateSectionSF(migrationSF, origParentSection, remoteOffsetsParents, newParentSection, &parentSF);CHKERRQ(ierr); 11728a713f3bSLawrence Mitchell ierr = PetscFree(remoteOffsetsParents);CHKERRQ(ierr); 1173a6f36705SMichael Lange ierr = PetscSectionGetStorageSize(newParentSection,&newParentSize);CHKERRQ(ierr); 1174a6f36705SMichael Lange ierr = PetscMalloc2(newParentSize,&newParents,newParentSize,&newChildIDs);CHKERRQ(ierr); 117508633170SToby Isaac if (original) { 117608633170SToby Isaac PetscInt numParents; 117708633170SToby Isaac 117808633170SToby Isaac ierr = PetscSectionGetStorageSize(origParentSection,&numParents);CHKERRQ(ierr); 117908633170SToby Isaac ierr = PetscMalloc1(numParents,&globParents);CHKERRQ(ierr); 118008633170SToby Isaac ierr = ISLocalToGlobalMappingApplyBlock(original, numParents, origParents, globParents);CHKERRQ(ierr); 118108633170SToby Isaac } 118208633170SToby Isaac else { 118308633170SToby Isaac globParents = origParents; 118408633170SToby Isaac } 1185ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(parentSF, MPIU_INT, globParents, newParents,MPI_REPLACE);CHKERRQ(ierr); 1186ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(parentSF, MPIU_INT, globParents, newParents,MPI_REPLACE);CHKERRQ(ierr); 118708633170SToby Isaac if (original) { 118808633170SToby Isaac ierr = PetscFree(globParents);CHKERRQ(ierr); 118908633170SToby Isaac } 1190ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(parentSF, MPIU_INT, origChildIDs, newChildIDs,MPI_REPLACE);CHKERRQ(ierr); 1191ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(parentSF, MPIU_INT, origChildIDs, newChildIDs,MPI_REPLACE);CHKERRQ(ierr); 1192a6f36705SMichael Lange ierr = ISGlobalToLocalMappingApplyBlock(renumbering,IS_GTOLM_MASK, newParentSize, newParents, NULL, newParents);CHKERRQ(ierr); 119376bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 11944a54e071SToby Isaac PetscInt p; 11954a54e071SToby Isaac PetscBool valid = PETSC_TRUE; 11964a54e071SToby Isaac for (p = 0; p < newParentSize; ++p) { 11974a54e071SToby Isaac if (newParents[p] < 0) {valid = PETSC_FALSE; ierr = PetscPrintf(PETSC_COMM_SELF, "Point %d not in overlap SF\n", p);CHKERRQ(ierr);} 11984a54e071SToby Isaac } 11994a54e071SToby Isaac if (!valid) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid global to local map"); 12004a54e071SToby Isaac } 1201c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-parents_view", &flg);CHKERRQ(ierr); 1202a6f36705SMichael Lange if (flg) { 1203a6f36705SMichael Lange ierr = PetscPrintf(comm, "Serial Parent Section: \n");CHKERRQ(ierr); 1204a8c5bd36SStefano Zampini ierr = PetscSectionView(origParentSection, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 1205a6f36705SMichael Lange ierr = PetscPrintf(comm, "Parallel Parent Section: \n");CHKERRQ(ierr); 1206a8c5bd36SStefano Zampini ierr = PetscSectionView(newParentSection, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 1207a6f36705SMichael Lange ierr = PetscSFView(parentSF, NULL);CHKERRQ(ierr); 1208a6f36705SMichael Lange } 1209a6f36705SMichael Lange ierr = DMPlexSetTree(dmParallel,newParentSection,newParents,newChildIDs);CHKERRQ(ierr); 1210a6f36705SMichael Lange ierr = PetscSectionDestroy(&newParentSection);CHKERRQ(ierr); 1211a6f36705SMichael Lange ierr = PetscFree2(newParents,newChildIDs);CHKERRQ(ierr); 1212a6f36705SMichael Lange ierr = PetscSFDestroy(&parentSF);CHKERRQ(ierr); 1213a6f36705SMichael Lange } 121415078cd4SMichael Lange pmesh->useAnchors = mesh->useAnchors; 1215a6f36705SMichael Lange PetscFunctionReturn(0); 1216a6f36705SMichael Lange } 12170df0e737SMichael Lange 121824cc2ca5SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexDistributeSF(DM dm, PetscSF migrationSF, DM dmParallel) 12194eca1733SMichael Lange { 12209852e123SBarry Smith PetscMPIInt rank, size; 12214eca1733SMichael Lange MPI_Comm comm; 12224eca1733SMichael Lange PetscErrorCode ierr; 12234eca1733SMichael Lange 12244eca1733SMichael Lange PetscFunctionBegin; 12254eca1733SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 12260c86c063SLisandro Dalcin PetscValidHeaderSpecific(dmParallel, DM_CLASSID, 3); 12274eca1733SMichael Lange 12284eca1733SMichael Lange /* Create point SF for parallel mesh */ 12294eca1733SMichael Lange ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr); 12304eca1733SMichael Lange ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 1231ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 1232ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 12334eca1733SMichael Lange { 12344eca1733SMichael Lange const PetscInt *leaves; 12354eca1733SMichael Lange PetscSFNode *remotePoints, *rowners, *lowners; 12364eca1733SMichael Lange PetscInt numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints; 12374eca1733SMichael Lange PetscInt pStart, pEnd; 12384eca1733SMichael Lange 12394eca1733SMichael Lange ierr = DMPlexGetChart(dmParallel, &pStart, &pEnd);CHKERRQ(ierr); 12404eca1733SMichael Lange ierr = PetscSFGetGraph(migrationSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr); 12414eca1733SMichael Lange ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr); 12424eca1733SMichael Lange for (p=0; p<numRoots; p++) { 12434eca1733SMichael Lange rowners[p].rank = -1; 12444eca1733SMichael Lange rowners[p].index = -1; 12454eca1733SMichael Lange } 1246ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE);CHKERRQ(ierr); 1247ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE);CHKERRQ(ierr); 12484eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 12494eca1733SMichael Lange if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */ 12504eca1733SMichael Lange lowners[p].rank = rank; 12514eca1733SMichael Lange lowners[p].index = leaves ? leaves[p] : p; 12524eca1733SMichael Lange } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */ 12534eca1733SMichael Lange lowners[p].rank = -2; 12544eca1733SMichael Lange lowners[p].index = -2; 12554eca1733SMichael Lange } 12564eca1733SMichael Lange } 12574eca1733SMichael Lange for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */ 12584eca1733SMichael Lange rowners[p].rank = -3; 12594eca1733SMichael Lange rowners[p].index = -3; 12604eca1733SMichael Lange } 12614eca1733SMichael Lange ierr = PetscSFReduceBegin(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr); 12624eca1733SMichael Lange ierr = PetscSFReduceEnd(migrationSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr); 1263ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE);CHKERRQ(ierr); 1264ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rowners, lowners,MPI_REPLACE);CHKERRQ(ierr); 12654eca1733SMichael Lange for (p = 0; p < numLeaves; ++p) { 12664eca1733SMichael Lange if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed"); 12674eca1733SMichael Lange if (lowners[p].rank != rank) ++numGhostPoints; 12684eca1733SMichael Lange } 12694eca1733SMichael Lange ierr = PetscMalloc1(numGhostPoints, &ghostPoints);CHKERRQ(ierr); 12704eca1733SMichael Lange ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr); 12714eca1733SMichael Lange for (p = 0, gp = 0; p < numLeaves; ++p) { 12724eca1733SMichael Lange if (lowners[p].rank != rank) { 12734eca1733SMichael Lange ghostPoints[gp] = leaves ? leaves[p] : p; 12744eca1733SMichael Lange remotePoints[gp].rank = lowners[p].rank; 12754eca1733SMichael Lange remotePoints[gp].index = lowners[p].index; 12764eca1733SMichael Lange ++gp; 12774eca1733SMichael Lange } 12784eca1733SMichael Lange } 12794eca1733SMichael Lange ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr); 12804eca1733SMichael Lange ierr = PetscSFSetGraph((dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr); 12814eca1733SMichael Lange ierr = PetscSFSetFromOptions((dmParallel)->sf);CHKERRQ(ierr); 12824eca1733SMichael Lange } 12831cf84007SMatthew G. Knepley { 12841cf84007SMatthew G. Knepley PetscBool useCone, useClosure, useAnchors; 12851cf84007SMatthew G. Knepley 1286b0441da4SMatthew G. Knepley ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr); 1287b0441da4SMatthew G. Knepley ierr = DMSetBasicAdjacency(dmParallel, useCone, useClosure);CHKERRQ(ierr); 12881cf84007SMatthew G. Knepley ierr = DMPlexGetAdjacencyUseAnchors(dm, &useAnchors);CHKERRQ(ierr); 12891cf84007SMatthew G. Knepley ierr = DMPlexSetAdjacencyUseAnchors(dmParallel, useAnchors);CHKERRQ(ierr); 12901cf84007SMatthew G. Knepley } 12914eca1733SMichael Lange ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr); 12924eca1733SMichael Lange PetscFunctionReturn(0); 12934eca1733SMichael Lange } 12944eca1733SMichael Lange 129598ba2d7fSLawrence Mitchell /*@ 129698ba2d7fSLawrence Mitchell DMPlexSetPartitionBalance - Should distribution of the DM attempt to balance the shared point partition? 129798ba2d7fSLawrence Mitchell 129898ba2d7fSLawrence Mitchell Input Parameters: 129998ba2d7fSLawrence Mitchell + dm - The DMPlex object 130098ba2d7fSLawrence Mitchell - flg - Balance the partition? 130198ba2d7fSLawrence Mitchell 130298ba2d7fSLawrence Mitchell Level: intermediate 130398ba2d7fSLawrence Mitchell 130498ba2d7fSLawrence Mitchell .seealso: DMPlexDistribute(), DMPlexGetPartitionBalance() 130598ba2d7fSLawrence Mitchell @*/ 130698ba2d7fSLawrence Mitchell PetscErrorCode DMPlexSetPartitionBalance(DM dm, PetscBool flg) 130798ba2d7fSLawrence Mitchell { 130898ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 130998ba2d7fSLawrence Mitchell 131098ba2d7fSLawrence Mitchell PetscFunctionBegin; 131198ba2d7fSLawrence Mitchell mesh->partitionBalance = flg; 131298ba2d7fSLawrence Mitchell PetscFunctionReturn(0); 131398ba2d7fSLawrence Mitchell } 131498ba2d7fSLawrence Mitchell 131598ba2d7fSLawrence Mitchell /*@ 131698ba2d7fSLawrence Mitchell DMPlexGetPartitionBalance - Does distribution of the DM attempt to balance the shared point partition? 131798ba2d7fSLawrence Mitchell 131898ba2d7fSLawrence Mitchell Input Parameter: 1319a2b725a8SWilliam Gropp . dm - The DMPlex object 132098ba2d7fSLawrence Mitchell 132198ba2d7fSLawrence Mitchell Output Parameter: 1322a2b725a8SWilliam Gropp . flg - Balance the partition? 132398ba2d7fSLawrence Mitchell 132498ba2d7fSLawrence Mitchell Level: intermediate 132598ba2d7fSLawrence Mitchell 132698ba2d7fSLawrence Mitchell .seealso: DMPlexDistribute(), DMPlexSetPartitionBalance() 132798ba2d7fSLawrence Mitchell @*/ 132898ba2d7fSLawrence Mitchell PetscErrorCode DMPlexGetPartitionBalance(DM dm, PetscBool *flg) 132998ba2d7fSLawrence Mitchell { 133098ba2d7fSLawrence Mitchell DM_Plex *mesh = (DM_Plex *)dm->data; 133198ba2d7fSLawrence Mitchell 133298ba2d7fSLawrence Mitchell PetscFunctionBegin; 133398ba2d7fSLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1334534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 133598ba2d7fSLawrence Mitchell *flg = mesh->partitionBalance; 133698ba2d7fSLawrence Mitchell PetscFunctionReturn(0); 133798ba2d7fSLawrence Mitchell } 133898ba2d7fSLawrence Mitchell 1339fc02256fSLawrence Mitchell typedef struct { 1340fc02256fSLawrence Mitchell PetscInt vote, rank, index; 1341fc02256fSLawrence Mitchell } Petsc3Int; 1342fc02256fSLawrence Mitchell 1343fc02256fSLawrence Mitchell /* MaxLoc, but carry a third piece of information around */ 13442eb0eadbSSatish Balay static void MPIAPI MaxLocCarry(void *in_, void *inout_, PetscMPIInt *len_, MPI_Datatype *dtype) 1345fc02256fSLawrence Mitchell { 1346fc02256fSLawrence Mitchell Petsc3Int *a = (Petsc3Int *)inout_; 1347fc02256fSLawrence Mitchell Petsc3Int *b = (Petsc3Int *)in_; 1348fc02256fSLawrence Mitchell PetscInt i, len = *len_; 1349fc02256fSLawrence Mitchell for (i = 0; i < len; i++) { 1350fc02256fSLawrence Mitchell if (a[i].vote < b[i].vote) { 1351fc02256fSLawrence Mitchell a[i].vote = b[i].vote; 1352fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1353fc02256fSLawrence Mitchell a[i].index = b[i].index; 1354fc02256fSLawrence Mitchell } else if (a[i].vote <= b[i].vote) { 1355fc02256fSLawrence Mitchell if (a[i].rank >= b[i].rank) { 1356fc02256fSLawrence Mitchell a[i].rank = b[i].rank; 1357fc02256fSLawrence Mitchell a[i].index = b[i].index; 1358fc02256fSLawrence Mitchell } 1359fc02256fSLawrence Mitchell } 1360fc02256fSLawrence Mitchell } 1361fc02256fSLawrence Mitchell } 1362fc02256fSLawrence Mitchell 1363f5bf2dbfSMichael Lange /*@C 1364a8c5bd36SStefano Zampini DMPlexCreatePointSF - Build a point SF from an SF describing a point migration 1365f5bf2dbfSMichael Lange 1366064ec1f3Sprj- Input Parameters: 1367f5bf2dbfSMichael Lange + dm - The source DMPlex object 1368f5bf2dbfSMichael Lange . migrationSF - The star forest that describes the parallel point remapping 1369*d8d19677SJose E. Roman - ownership - Flag causing a vote to determine point ownership 1370f5bf2dbfSMichael Lange 1371f5bf2dbfSMichael Lange Output Parameter: 1372*d8d19677SJose E. Roman . pointSF - The star forest describing the point overlap in the remapped DM 1373f5bf2dbfSMichael Lange 13743618482eSVaclav Hapla Notes: 13753618482eSVaclav Hapla Output pointSF is guaranteed to have the array of local indices (leaves) sorted. 13763618482eSVaclav Hapla 1377f5bf2dbfSMichael Lange Level: developer 1378f5bf2dbfSMichael Lange 1379f5bf2dbfSMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap() 1380f5bf2dbfSMichael Lange @*/ 1381f5bf2dbfSMichael Lange PetscErrorCode DMPlexCreatePointSF(DM dm, PetscSF migrationSF, PetscBool ownership, PetscSF *pointSF) 1382f5bf2dbfSMichael Lange { 138323193802SMatthew G. Knepley PetscMPIInt rank, size; 13841627f6ccSMichael Lange PetscInt p, nroots, nleaves, idx, npointLeaves; 1385f5bf2dbfSMichael Lange PetscInt *pointLocal; 1386f5bf2dbfSMichael Lange const PetscInt *leaves; 1387f5bf2dbfSMichael Lange const PetscSFNode *roots; 1388f5bf2dbfSMichael Lange PetscSFNode *rootNodes, *leafNodes, *pointRemote; 138923193802SMatthew G. Knepley Vec shifts; 1390cae3e4f3SLawrence Mitchell const PetscInt numShifts = 13759; 139123193802SMatthew G. Knepley const PetscScalar *shift = NULL; 139223193802SMatthew G. Knepley const PetscBool shiftDebug = PETSC_FALSE; 139398ba2d7fSLawrence Mitchell PetscBool balance; 1394f5bf2dbfSMichael Lange PetscErrorCode ierr; 1395f5bf2dbfSMichael Lange 1396f5bf2dbfSMichael Lange PetscFunctionBegin; 1397f5bf2dbfSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1398ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr); 1399ffc4695bSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRMPI(ierr); 140030b0ce1bSStefano Zampini ierr = PetscLogEventBegin(DMPLEX_CreatePointSF,dm,0,0,0);CHKERRQ(ierr); 1401f5bf2dbfSMichael Lange 140298ba2d7fSLawrence Mitchell ierr = DMPlexGetPartitionBalance(dm, &balance);CHKERRQ(ierr); 1403f5bf2dbfSMichael Lange ierr = PetscSFGetGraph(migrationSF, &nroots, &nleaves, &leaves, &roots);CHKERRQ(ierr); 1404f5bf2dbfSMichael Lange ierr = PetscMalloc2(nroots, &rootNodes, nleaves, &leafNodes);CHKERRQ(ierr); 1405f5bf2dbfSMichael Lange if (ownership) { 1406fc02256fSLawrence Mitchell MPI_Op op; 1407fc02256fSLawrence Mitchell MPI_Datatype datatype; 1408fc02256fSLawrence Mitchell Petsc3Int *rootVote = NULL, *leafVote = NULL; 140923193802SMatthew 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. */ 141098ba2d7fSLawrence Mitchell if (balance) { 141123193802SMatthew G. Knepley PetscRandom r; 141223193802SMatthew G. Knepley 141323193802SMatthew G. Knepley ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 1414cae3e4f3SLawrence Mitchell ierr = PetscRandomSetInterval(r, 0, 2467*size);CHKERRQ(ierr); 141523193802SMatthew G. Knepley ierr = VecCreate(PETSC_COMM_SELF, &shifts);CHKERRQ(ierr); 141623193802SMatthew G. Knepley ierr = VecSetSizes(shifts, numShifts, numShifts);CHKERRQ(ierr); 141723193802SMatthew G. Knepley ierr = VecSetType(shifts, VECSTANDARD);CHKERRQ(ierr); 141823193802SMatthew G. Knepley ierr = VecSetRandom(shifts, r);CHKERRQ(ierr); 141923193802SMatthew G. Knepley ierr = PetscRandomDestroy(&r);CHKERRQ(ierr); 142023193802SMatthew G. Knepley ierr = VecGetArrayRead(shifts, &shift);CHKERRQ(ierr); 142123193802SMatthew G. Knepley } 142223193802SMatthew G. Knepley 1423fc02256fSLawrence Mitchell ierr = PetscMalloc1(nroots, &rootVote);CHKERRQ(ierr); 1424fc02256fSLawrence Mitchell ierr = PetscMalloc1(nleaves, &leafVote);CHKERRQ(ierr); 142523193802SMatthew G. Knepley /* Point ownership vote: Process with highest rank owns shared points */ 1426f5bf2dbfSMichael Lange for (p = 0; p < nleaves; ++p) { 142723193802SMatthew G. Knepley if (shiftDebug) { 1428e7bcd635SMatthew G. Knepley ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d] Point %D RemotePoint %D Shift %D MyRank %D\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);CHKERRQ(ierr); 142923193802SMatthew G. Knepley } 1430f5bf2dbfSMichael Lange /* Either put in a bid or we know we own it */ 1431fc02256fSLawrence Mitchell leafVote[p].vote = (rank + (shift ? (PetscInt) PetscRealPart(shift[roots[p].index%numShifts]) : 0))%size; 1432fc02256fSLawrence Mitchell leafVote[p].rank = rank; 1433fc02256fSLawrence Mitchell leafVote[p].index = p; 1434f5bf2dbfSMichael Lange } 1435f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 14361627f6ccSMichael Lange /* Root must not participate in the reduction, flag so that MAXLOC does not use */ 1437fc02256fSLawrence Mitchell rootVote[p].vote = -3; 1438fc02256fSLawrence Mitchell rootVote[p].rank = -3; 1439fc02256fSLawrence Mitchell rootVote[p].index = -3; 1440f5bf2dbfSMichael Lange } 1441ffc4695bSBarry Smith ierr = MPI_Type_contiguous(3, MPIU_INT, &datatype);CHKERRMPI(ierr); 1442ffc4695bSBarry Smith ierr = MPI_Type_commit(&datatype);CHKERRMPI(ierr); 1443ffc4695bSBarry Smith ierr = MPI_Op_create(&MaxLocCarry, 1, &op);CHKERRMPI(ierr); 1444fc02256fSLawrence Mitchell ierr = PetscSFReduceBegin(migrationSF, datatype, leafVote, rootVote, op);CHKERRQ(ierr); 1445fc02256fSLawrence Mitchell ierr = PetscSFReduceEnd(migrationSF, datatype, leafVote, rootVote, op);CHKERRQ(ierr); 1446ffc4695bSBarry Smith ierr = MPI_Op_free(&op);CHKERRMPI(ierr); 1447ffc4695bSBarry Smith ierr = MPI_Type_free(&datatype);CHKERRMPI(ierr); 1448c091126eSLawrence Mitchell for (p = 0; p < nroots; p++) { 1449fc02256fSLawrence Mitchell rootNodes[p].rank = rootVote[p].rank; 1450fc02256fSLawrence Mitchell rootNodes[p].index = rootVote[p].index; 1451c091126eSLawrence Mitchell } 1452fc02256fSLawrence Mitchell ierr = PetscFree(leafVote);CHKERRQ(ierr); 1453fc02256fSLawrence Mitchell ierr = PetscFree(rootVote);CHKERRQ(ierr); 1454f5bf2dbfSMichael Lange } else { 1455f5bf2dbfSMichael Lange for (p = 0; p < nroots; p++) { 1456f5bf2dbfSMichael Lange rootNodes[p].index = -1; 1457f5bf2dbfSMichael Lange rootNodes[p].rank = rank; 1458fc02256fSLawrence Mitchell } 1459f5bf2dbfSMichael Lange for (p = 0; p < nleaves; p++) { 1460f5bf2dbfSMichael Lange /* Write new local id into old location */ 1461f5bf2dbfSMichael Lange if (roots[p].rank == rank) { 14626462276dSToby Isaac rootNodes[roots[p].index].index = leaves ? leaves[p] : p; 1463f5bf2dbfSMichael Lange } 1464f5bf2dbfSMichael Lange } 1465f5bf2dbfSMichael Lange } 1466ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(migrationSF, MPIU_2INT, rootNodes, leafNodes,MPI_REPLACE);CHKERRQ(ierr); 1467ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(migrationSF, MPIU_2INT, rootNodes, leafNodes,MPI_REPLACE);CHKERRQ(ierr); 1468f5bf2dbfSMichael Lange 146923193802SMatthew G. Knepley for (npointLeaves = 0, p = 0; p < nleaves; p++) { 1470b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) npointLeaves++; 147123193802SMatthew G. Knepley } 14721627f6ccSMichael Lange ierr = PetscMalloc1(npointLeaves, &pointLocal);CHKERRQ(ierr); 14731627f6ccSMichael Lange ierr = PetscMalloc1(npointLeaves, &pointRemote);CHKERRQ(ierr); 1474f5bf2dbfSMichael Lange for (idx = 0, p = 0; p < nleaves; p++) { 1475b0e1264bSMatthew G. Knepley if (leafNodes[p].rank != rank) { 14763618482eSVaclav Hapla /* Note that pointLocal is automatically sorted as it is sublist of 0, ..., nleaves-1 */ 1477f5bf2dbfSMichael Lange pointLocal[idx] = p; 1478f5bf2dbfSMichael Lange pointRemote[idx] = leafNodes[p]; 1479f5bf2dbfSMichael Lange idx++; 1480f5bf2dbfSMichael Lange } 1481f5bf2dbfSMichael Lange } 148223193802SMatthew G. Knepley if (shift) { 148323193802SMatthew G. Knepley ierr = VecRestoreArrayRead(shifts, &shift);CHKERRQ(ierr); 148423193802SMatthew G. Knepley ierr = VecDestroy(&shifts);CHKERRQ(ierr); 148523193802SMatthew G. Knepley } 148623193802SMatthew G. Knepley if (shiftDebug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), PETSC_STDOUT);CHKERRQ(ierr);} 1487f5bf2dbfSMichael Lange ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), pointSF);CHKERRQ(ierr); 14881627f6ccSMichael Lange ierr = PetscSFSetFromOptions(*pointSF);CHKERRQ(ierr); 1489f5bf2dbfSMichael Lange ierr = PetscSFSetGraph(*pointSF, nleaves, npointLeaves, pointLocal, PETSC_OWN_POINTER, pointRemote, PETSC_OWN_POINTER);CHKERRQ(ierr); 1490f5bf2dbfSMichael Lange ierr = PetscFree2(rootNodes, leafNodes);CHKERRQ(ierr); 149130b0ce1bSStefano Zampini ierr = PetscLogEventEnd(DMPLEX_CreatePointSF,dm,0,0,0);CHKERRQ(ierr); 1492f5bf2dbfSMichael Lange PetscFunctionReturn(0); 1493f5bf2dbfSMichael Lange } 1494f5bf2dbfSMichael Lange 149515078cd4SMichael Lange /*@C 149615078cd4SMichael Lange DMPlexMigrate - Migrates internal DM data over the supplied star forest 149715078cd4SMichael Lange 1498d083f849SBarry Smith Collective on dm 149983655b49SVáclav Hapla 1500064ec1f3Sprj- Input Parameters: 150115078cd4SMichael Lange + dm - The source DMPlex object 1502*d8d19677SJose E. Roman - sf - The star forest communication context describing the migration pattern 150315078cd4SMichael Lange 150415078cd4SMichael Lange Output Parameter: 1505*d8d19677SJose E. Roman . targetDM - The target DMPlex object 150615078cd4SMichael Lange 1507b9f40539SMichael Lange Level: intermediate 150815078cd4SMichael Lange 150915078cd4SMichael Lange .seealso: DMPlexDistribute(), DMPlexDistributeOverlap() 151015078cd4SMichael Lange @*/ 1511b9f40539SMichael Lange PetscErrorCode DMPlexMigrate(DM dm, PetscSF sf, DM targetDM) 151215078cd4SMichael Lange { 1513b9f40539SMichael Lange MPI_Comm comm; 1514cc1750acSStefano Zampini PetscInt dim, cdim, nroots; 1515b9f40539SMichael Lange PetscSF sfPoint; 151615078cd4SMichael Lange ISLocalToGlobalMapping ltogMigration; 1517ac04eaf7SToby Isaac ISLocalToGlobalMapping ltogOriginal = NULL; 1518b9f40539SMichael Lange PetscBool flg; 151915078cd4SMichael Lange PetscErrorCode ierr; 152015078cd4SMichael Lange 152115078cd4SMichael Lange PetscFunctionBegin; 152215078cd4SMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 15231b858b30SMichael Lange ierr = PetscLogEventBegin(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr); 1524b9f40539SMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 152515078cd4SMichael Lange ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 152615078cd4SMichael Lange ierr = DMSetDimension(targetDM, dim);CHKERRQ(ierr); 1527cc1750acSStefano Zampini ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 1528cc1750acSStefano Zampini ierr = DMSetCoordinateDim(targetDM, cdim);CHKERRQ(ierr); 152915078cd4SMichael Lange 1530bfb0467fSMichael Lange /* Check for a one-to-all distribution pattern */ 1531b9f40539SMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 1532b9f40539SMichael Lange ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr); 1533bfb0467fSMichael Lange if (nroots >= 0) { 1534b9f40539SMichael Lange IS isOriginal; 1535ac04eaf7SToby Isaac PetscInt n, size, nleaves; 1536ac04eaf7SToby Isaac PetscInt *numbering_orig, *numbering_new; 1537367003a6SStefano Zampini 1538b9f40539SMichael Lange /* Get the original point numbering */ 1539b9f40539SMichael Lange ierr = DMPlexCreatePointNumbering(dm, &isOriginal);CHKERRQ(ierr); 1540b9f40539SMichael Lange ierr = ISLocalToGlobalMappingCreateIS(isOriginal, <ogOriginal);CHKERRQ(ierr); 1541b9f40539SMichael Lange ierr = ISLocalToGlobalMappingGetSize(ltogOriginal, &size);CHKERRQ(ierr); 1542b9f40539SMichael Lange ierr = ISLocalToGlobalMappingGetBlockIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr); 1543b9f40539SMichael Lange /* Convert to positive global numbers */ 1544b9f40539SMichael Lange for (n=0; n<size; n++) {if (numbering_orig[n] < 0) numbering_orig[n] = -(numbering_orig[n]+1);} 1545b9f40539SMichael Lange /* Derive the new local-to-global mapping from the old one */ 1546b9f40539SMichael Lange ierr = PetscSFGetGraph(sf, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr); 1547b9f40539SMichael Lange ierr = PetscMalloc1(nleaves, &numbering_new);CHKERRQ(ierr); 1548ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(sf, MPIU_INT, numbering_orig, numbering_new,MPI_REPLACE);CHKERRQ(ierr); 1549ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(sf, MPIU_INT, numbering_orig, numbering_new,MPI_REPLACE);CHKERRQ(ierr); 15506e0288c8SStefano Zampini ierr = ISLocalToGlobalMappingCreate(comm, 1, nleaves, numbering_new, PETSC_OWN_POINTER, <ogMigration);CHKERRQ(ierr); 1551b9f40539SMichael Lange ierr = ISLocalToGlobalMappingRestoreIndices(ltogOriginal, (const PetscInt**)&numbering_orig);CHKERRQ(ierr); 1552b9f40539SMichael Lange ierr = ISDestroy(&isOriginal);CHKERRQ(ierr); 155315078cd4SMichael Lange } else { 1554bfb0467fSMichael Lange /* One-to-all distribution pattern: We can derive LToG from SF */ 155515078cd4SMichael Lange ierr = ISLocalToGlobalMappingCreateSF(sf, 0, <ogMigration);CHKERRQ(ierr); 155615078cd4SMichael Lange } 1557c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr); 1558b9f40539SMichael Lange if (flg) { 1559b9f40539SMichael Lange ierr = PetscPrintf(comm, "Point renumbering for DM migration:\n");CHKERRQ(ierr); 1560b9f40539SMichael Lange ierr = ISLocalToGlobalMappingView(ltogMigration, NULL);CHKERRQ(ierr); 1561b9f40539SMichael Lange } 156215078cd4SMichael Lange /* Migrate DM data to target DM */ 1563ac04eaf7SToby Isaac ierr = DMPlexDistributeCones(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr); 156415078cd4SMichael Lange ierr = DMPlexDistributeLabels(dm, sf, targetDM);CHKERRQ(ierr); 15650dc1530dSSander Arens ierr = DMPlexDistributeCoordinates(dm, sf, targetDM);CHKERRQ(ierr); 156608633170SToby Isaac ierr = DMPlexDistributeSetupTree(dm, sf, ltogOriginal, ltogMigration, targetDM);CHKERRQ(ierr); 1567ac04eaf7SToby Isaac ierr = ISLocalToGlobalMappingDestroy(<ogOriginal);CHKERRQ(ierr); 1568302440fdSBarry Smith ierr = ISLocalToGlobalMappingDestroy(<ogMigration);CHKERRQ(ierr); 15691b858b30SMichael Lange ierr = PetscLogEventEnd(DMPLEX_Migrate, dm, 0, 0, 0);CHKERRQ(ierr); 157015078cd4SMichael Lange PetscFunctionReturn(0); 157115078cd4SMichael Lange } 157215078cd4SMichael Lange 15733b8f15a2SMatthew G. Knepley /*@C 157470034214SMatthew G. Knepley DMPlexDistribute - Distributes the mesh and any associated sections. 157570034214SMatthew G. Knepley 1576d083f849SBarry Smith Collective on dm 157770034214SMatthew G. Knepley 1578064ec1f3Sprj- Input Parameters: 157970034214SMatthew G. Knepley + dm - The original DMPlex object 158070034214SMatthew G. Knepley - overlap - The overlap of partitions, 0 is the default 158170034214SMatthew G. Knepley 1582064ec1f3Sprj- Output Parameters: 1583f4ae5380SJed Brown + sf - The PetscSF used for point distribution, or NULL if not needed 1584f4ae5380SJed Brown - dmParallel - The distributed DMPlex object 158570034214SMatthew G. Knepley 1586f4ae5380SJed Brown Note: If the mesh was not distributed, the output dmParallel will be NULL. 158770034214SMatthew G. Knepley 1588b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 158970034214SMatthew G. Knepley representation on the mesh. 159070034214SMatthew G. Knepley 159170034214SMatthew G. Knepley Level: intermediate 159270034214SMatthew G. Knepley 1593cb54e036SVaclav Hapla .seealso: DMPlexCreate(), DMSetAdjacency(), DMPlexGetOverlap() 159470034214SMatthew G. Knepley @*/ 159580cf41d5SMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, PetscInt overlap, PetscSF *sf, DM *dmParallel) 159670034214SMatthew G. Knepley { 159770034214SMatthew G. Knepley MPI_Comm comm; 159815078cd4SMichael Lange PetscPartitioner partitioner; 1599f8987ae8SMichael Lange IS cellPart; 1600f8987ae8SMichael Lange PetscSection cellPartSection; 1601cf86098cSMatthew G. Knepley DM dmCoord; 1602f8987ae8SMichael Lange DMLabel lblPartition, lblMigration; 1603874ddda9SLisandro Dalcin PetscSF sfMigration, sfStratified, sfPoint; 160498ba2d7fSLawrence Mitchell PetscBool flg, balance; 1605874ddda9SLisandro Dalcin PetscMPIInt rank, size; 160670034214SMatthew G. Knepley PetscErrorCode ierr; 160770034214SMatthew G. Knepley 160870034214SMatthew G. Knepley PetscFunctionBegin; 160970034214SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1610d5c515a1SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, overlap, 2); 1611d5c515a1SMatthew G. Knepley if (sf) PetscValidPointer(sf,3); 1612d5c515a1SMatthew G. Knepley PetscValidPointer(dmParallel,4); 161370034214SMatthew G. Knepley 16140c86c063SLisandro Dalcin if (sf) *sf = NULL; 16150c86c063SLisandro Dalcin *dmParallel = NULL; 161670034214SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 1617ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 1618ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 16190c86c063SLisandro Dalcin if (size == 1) PetscFunctionReturn(0); 162070034214SMatthew G. Knepley 16210c86c063SLisandro Dalcin ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr); 162215078cd4SMichael Lange /* Create cell partition */ 162367eb269bSLisandro Dalcin ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr); 162477623264SMatthew G. Knepley ierr = PetscSectionCreate(comm, &cellPartSection);CHKERRQ(ierr); 162515078cd4SMichael Lange ierr = DMPlexGetPartitioner(dm, &partitioner);CHKERRQ(ierr); 16263c41b853SStefano Zampini ierr = PetscPartitionerDMPlexPartition(partitioner, dm, NULL, cellPartSection, &cellPart);CHKERRQ(ierr); 162730b0ce1bSStefano Zampini ierr = PetscLogEventBegin(DMPLEX_PartSelf,dm,0,0,0);CHKERRQ(ierr); 1628f8987ae8SMichael Lange { 1629f8987ae8SMichael Lange /* Convert partition to DMLabel */ 1630825f8a23SLisandro Dalcin IS is; 1631825f8a23SLisandro Dalcin PetscHSetI ht; 1632f8987ae8SMichael Lange const PetscInt *points; 16338e330a33SStefano Zampini PetscInt *iranks; 16348e330a33SStefano Zampini PetscInt pStart, pEnd, proc, npoints, poff = 0, nranks; 1635825f8a23SLisandro Dalcin 1636d67d17b1SMatthew G. Knepley ierr = DMLabelCreate(PETSC_COMM_SELF, "Point Partition", &lblPartition);CHKERRQ(ierr); 1637825f8a23SLisandro Dalcin /* Preallocate strata */ 1638825f8a23SLisandro Dalcin ierr = PetscHSetICreate(&ht);CHKERRQ(ierr); 1639825f8a23SLisandro Dalcin ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr); 1640825f8a23SLisandro Dalcin for (proc = pStart; proc < pEnd; proc++) { 1641825f8a23SLisandro Dalcin ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr); 1642825f8a23SLisandro Dalcin if (npoints) {ierr = PetscHSetIAdd(ht, proc);CHKERRQ(ierr);} 1643825f8a23SLisandro Dalcin } 1644825f8a23SLisandro Dalcin ierr = PetscHSetIGetSize(ht, &nranks);CHKERRQ(ierr); 1645825f8a23SLisandro Dalcin ierr = PetscMalloc1(nranks, &iranks);CHKERRQ(ierr); 1646825f8a23SLisandro Dalcin ierr = PetscHSetIGetElems(ht, &poff, iranks);CHKERRQ(ierr); 1647825f8a23SLisandro Dalcin ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr); 1648825f8a23SLisandro Dalcin ierr = DMLabelAddStrata(lblPartition, nranks, iranks);CHKERRQ(ierr); 1649825f8a23SLisandro Dalcin ierr = PetscFree(iranks);CHKERRQ(ierr); 1650825f8a23SLisandro Dalcin /* Inline DMPlexPartitionLabelClosure() */ 1651f8987ae8SMichael Lange ierr = ISGetIndices(cellPart, &points);CHKERRQ(ierr); 1652f8987ae8SMichael Lange ierr = PetscSectionGetChart(cellPartSection, &pStart, &pEnd);CHKERRQ(ierr); 1653f8987ae8SMichael Lange for (proc = pStart; proc < pEnd; proc++) { 1654f8987ae8SMichael Lange ierr = PetscSectionGetDof(cellPartSection, proc, &npoints);CHKERRQ(ierr); 1655825f8a23SLisandro Dalcin if (!npoints) continue; 1656825f8a23SLisandro Dalcin ierr = PetscSectionGetOffset(cellPartSection, proc, &poff);CHKERRQ(ierr); 16578e330a33SStefano Zampini ierr = DMPlexClosurePoints_Private(dm, npoints, points+poff, &is);CHKERRQ(ierr); 1658825f8a23SLisandro Dalcin ierr = DMLabelSetStratumIS(lblPartition, proc, is);CHKERRQ(ierr); 1659825f8a23SLisandro Dalcin ierr = ISDestroy(&is);CHKERRQ(ierr); 1660f8987ae8SMichael Lange } 1661f8987ae8SMichael Lange ierr = ISRestoreIndices(cellPart, &points);CHKERRQ(ierr); 1662f8987ae8SMichael Lange } 166330b0ce1bSStefano Zampini ierr = PetscLogEventEnd(DMPLEX_PartSelf,dm,0,0,0);CHKERRQ(ierr); 16646e203dd9SStefano Zampini 1665d67d17b1SMatthew G. Knepley ierr = DMLabelCreate(PETSC_COMM_SELF, "Point migration", &lblMigration);CHKERRQ(ierr); 1666874ddda9SLisandro Dalcin ierr = DMPlexPartitionLabelInvert(dm, lblPartition, NULL, lblMigration);CHKERRQ(ierr); 1667302440fdSBarry Smith ierr = DMPlexPartitionLabelCreateSF(dm, lblMigration, &sfMigration);CHKERRQ(ierr); 166843f7d02bSMichael Lange ierr = DMPlexStratifyMigrationSF(dm, sfMigration, &sfStratified);CHKERRQ(ierr); 166943f7d02bSMichael Lange ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr); 167043f7d02bSMichael Lange sfMigration = sfStratified; 167130b0ce1bSStefano Zampini ierr = PetscSFSetUp(sfMigration);CHKERRQ(ierr); 167267eb269bSLisandro Dalcin ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr); 1673c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr); 167470034214SMatthew G. Knepley if (flg) { 1675a8c5bd36SStefano Zampini ierr = DMLabelView(lblPartition, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 1676a8c5bd36SStefano Zampini ierr = PetscSFView(sfMigration, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 167770034214SMatthew G. Knepley } 1678f8987ae8SMichael Lange 167915078cd4SMichael Lange /* Create non-overlapping parallel DM and migrate internal data */ 168070034214SMatthew G. Knepley ierr = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr); 168170034214SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr); 1682b9f40539SMichael Lange ierr = DMPlexMigrate(dm, sfMigration, *dmParallel);CHKERRQ(ierr); 168370034214SMatthew G. Knepley 1684a157612eSMichael Lange /* Build the point SF without overlap */ 168598ba2d7fSLawrence Mitchell ierr = DMPlexGetPartitionBalance(dm, &balance);CHKERRQ(ierr); 168698ba2d7fSLawrence Mitchell ierr = DMPlexSetPartitionBalance(*dmParallel, balance);CHKERRQ(ierr); 16871627f6ccSMichael Lange ierr = DMPlexCreatePointSF(*dmParallel, sfMigration, PETSC_TRUE, &sfPoint);CHKERRQ(ierr); 1688f5bf2dbfSMichael Lange ierr = DMSetPointSF(*dmParallel, sfPoint);CHKERRQ(ierr); 1689cf86098cSMatthew G. Knepley ierr = DMGetCoordinateDM(*dmParallel, &dmCoord);CHKERRQ(ierr); 1690cf86098cSMatthew G. Knepley if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);} 1691a8c5bd36SStefano Zampini if (flg) {ierr = PetscSFView(sfPoint, NULL);CHKERRQ(ierr);} 169270034214SMatthew G. Knepley 1693a157612eSMichael Lange if (overlap > 0) { 169415078cd4SMichael Lange DM dmOverlap; 169557f290daSMatthew G. Knepley PetscInt nroots, nleaves, noldleaves, l; 169657f290daSMatthew G. Knepley const PetscInt *oldLeaves; 169757f290daSMatthew G. Knepley PetscSFNode *newRemote, *permRemote; 169815078cd4SMichael Lange const PetscSFNode *oldRemote; 169915078cd4SMichael Lange PetscSF sfOverlap, sfOverlapPoint; 1700524e35f8SStefano Zampini 1701a157612eSMichael Lange /* Add the partition overlap to the distributed DM */ 1702b9f40539SMichael Lange ierr = DMPlexDistributeOverlap(*dmParallel, overlap, &sfOverlap, &dmOverlap);CHKERRQ(ierr); 1703a157612eSMichael Lange ierr = DMDestroy(dmParallel);CHKERRQ(ierr); 1704a157612eSMichael Lange *dmParallel = dmOverlap; 1705c389ea9fSToby Isaac if (flg) { 17063d822a50SMichael Lange ierr = PetscPrintf(comm, "Overlap Migration SF:\n");CHKERRQ(ierr); 170715078cd4SMichael Lange ierr = PetscSFView(sfOverlap, NULL);CHKERRQ(ierr); 1708c389ea9fSToby Isaac } 170943331d4aSMichael Lange 1710f8987ae8SMichael Lange /* Re-map the migration SF to establish the full migration pattern */ 171157f290daSMatthew G. Knepley ierr = PetscSFGetGraph(sfMigration, &nroots, &noldleaves, &oldLeaves, &oldRemote);CHKERRQ(ierr); 171215078cd4SMichael Lange ierr = PetscSFGetGraph(sfOverlap, NULL, &nleaves, NULL, NULL);CHKERRQ(ierr); 171343331d4aSMichael Lange ierr = PetscMalloc1(nleaves, &newRemote);CHKERRQ(ierr); 171457f290daSMatthew G. Knepley /* oldRemote: original root point mapping to original leaf point 171557f290daSMatthew G. Knepley newRemote: original leaf point mapping to overlapped leaf point */ 171657f290daSMatthew G. Knepley if (oldLeaves) { 171773e69a6aSMatthew G. Knepley /* After stratification, the migration remotes may not be in root (canonical) order, so we reorder using the leaf numbering */ 171873e69a6aSMatthew G. Knepley ierr = PetscMalloc1(noldleaves, &permRemote);CHKERRQ(ierr); 171957f290daSMatthew G. Knepley for (l = 0; l < noldleaves; ++l) permRemote[oldLeaves[l]] = oldRemote[l]; 172057f290daSMatthew G. Knepley oldRemote = permRemote; 172157f290daSMatthew G. Knepley } 1722ad227feaSJunchao Zhang ierr = PetscSFBcastBegin(sfOverlap, MPIU_2INT, oldRemote, newRemote,MPI_REPLACE);CHKERRQ(ierr); 1723ad227feaSJunchao Zhang ierr = PetscSFBcastEnd(sfOverlap, MPIU_2INT, oldRemote, newRemote,MPI_REPLACE);CHKERRQ(ierr); 172457f290daSMatthew G. Knepley if (oldLeaves) {ierr = PetscFree(oldRemote);CHKERRQ(ierr);} 172515078cd4SMichael Lange ierr = PetscSFCreate(comm, &sfOverlapPoint);CHKERRQ(ierr); 172615078cd4SMichael Lange ierr = PetscSFSetGraph(sfOverlapPoint, nroots, nleaves, NULL, PETSC_OWN_POINTER, newRemote, PETSC_OWN_POINTER);CHKERRQ(ierr); 172715078cd4SMichael Lange ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr); 1728f8987ae8SMichael Lange ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr); 172915078cd4SMichael Lange sfMigration = sfOverlapPoint; 1730c389ea9fSToby Isaac } 1731bf5244c7SMatthew G. Knepley /* Cleanup Partition */ 1732f8987ae8SMichael Lange ierr = DMLabelDestroy(&lblPartition);CHKERRQ(ierr); 1733f8987ae8SMichael Lange ierr = DMLabelDestroy(&lblMigration);CHKERRQ(ierr); 17344eca1733SMichael Lange ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr); 17354eca1733SMichael Lange ierr = ISDestroy(&cellPart);CHKERRQ(ierr); 173645480ffeSMatthew G. Knepley /* Copy discretization */ 173745480ffeSMatthew G. Knepley ierr = DMCopyDisc(dm, *dmParallel);CHKERRQ(ierr); 173866fe0bfeSMatthew G. Knepley /* Create sfNatural */ 173966fe0bfeSMatthew G. Knepley if (dm->useNatural) { 174066fe0bfeSMatthew G. Knepley PetscSection section; 174166fe0bfeSMatthew G. Knepley 174292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 17430c1f158bSMatthew G. Knepley ierr = DMPlexCreateGlobalToNaturalSF(*dmParallel, section, sfMigration, &(*dmParallel)->sfNatural);CHKERRQ(ierr); 17440e663481SBlaise Bourdin ierr = DMSetUseNatural(*dmParallel, PETSC_TRUE);CHKERRQ(ierr); 174566fe0bfeSMatthew G. Knepley } 1746721cbd76SMatthew G. Knepley /* Cleanup */ 1747f8987ae8SMichael Lange if (sf) {*sf = sfMigration;} 1748f8987ae8SMichael Lange else {ierr = PetscSFDestroy(&sfMigration);CHKERRQ(ierr);} 1749f5bf2dbfSMichael Lange ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr); 175070034214SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr); 175170034214SMatthew G. Knepley PetscFunctionReturn(0); 175270034214SMatthew G. Knepley } 1753a157612eSMichael Lange 1754a157612eSMichael Lange /*@C 175524cc2ca5SMatthew G. Knepley DMPlexDistributeOverlap - Add partition overlap to a distributed non-overlapping DM. 1756a157612eSMichael Lange 1757d083f849SBarry Smith Collective on dm 1758a157612eSMichael Lange 1759064ec1f3Sprj- Input Parameters: 1760064ec1f3Sprj- + dm - The non-overlapping distributed DMPlex object 176157fe9a49SVaclav Hapla - overlap - The overlap of partitions (the same on all ranks) 1762a157612eSMichael Lange 1763064ec1f3Sprj- Output Parameters: 1764a157612eSMichael Lange + sf - The PetscSF used for point distribution 1765a157612eSMichael Lange - dmOverlap - The overlapping distributed DMPlex object, or NULL 1766a157612eSMichael Lange 1767dccdeccaSVaclav Hapla Notes: 1768dccdeccaSVaclav Hapla If the mesh was not distributed, the return value is NULL. 1769a157612eSMichael Lange 1770b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 1771a157612eSMichael Lange representation on the mesh. 1772a157612eSMichael Lange 1773dccdeccaSVaclav Hapla Level: advanced 1774a157612eSMichael Lange 1775cb54e036SVaclav Hapla .seealso: DMPlexCreate(), DMSetAdjacency(), DMPlexDistribute(), DMPlexCreateOverlapLabel(), DMPlexGetOverlap() 1776a157612eSMichael Lange @*/ 1777b9f40539SMichael Lange PetscErrorCode DMPlexDistributeOverlap(DM dm, PetscInt overlap, PetscSF *sf, DM *dmOverlap) 1778a157612eSMichael Lange { 1779a157612eSMichael Lange MPI_Comm comm; 17803567eaeeSMatthew G. Knepley PetscMPIInt size, rank; 1781a157612eSMichael Lange PetscSection rootSection, leafSection; 1782a157612eSMichael Lange IS rootrank, leafrank; 1783cf86098cSMatthew G. Knepley DM dmCoord; 1784a9f1d5b2SMichael Lange DMLabel lblOverlap; 1785f5bf2dbfSMichael Lange PetscSF sfOverlap, sfStratified, sfPoint; 1786a157612eSMichael Lange PetscErrorCode ierr; 1787a157612eSMichael Lange 1788a157612eSMichael Lange PetscFunctionBegin; 1789a157612eSMichael Lange PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 179057fe9a49SVaclav Hapla PetscValidLogicalCollectiveInt(dm, overlap, 2); 1791a157612eSMichael Lange if (sf) PetscValidPointer(sf, 3); 1792a157612eSMichael Lange PetscValidPointer(dmOverlap, 4); 1793a157612eSMichael Lange 17940c86c063SLisandro Dalcin if (sf) *sf = NULL; 17950c86c063SLisandro Dalcin *dmOverlap = NULL; 1796a157612eSMichael Lange ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 1797ffc4695bSBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr); 1798ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr); 17990c86c063SLisandro Dalcin if (size == 1) PetscFunctionReturn(0); 1800a157612eSMichael Lange 18010c86c063SLisandro Dalcin ierr = PetscLogEventBegin(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr); 1802a157612eSMichael Lange /* Compute point overlap with neighbouring processes on the distributed DM */ 180367eb269bSLisandro Dalcin ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr); 1804a157612eSMichael Lange ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr); 1805a157612eSMichael Lange ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr); 1806a157612eSMichael Lange ierr = DMPlexDistributeOwnership(dm, rootSection, &rootrank, leafSection, &leafrank);CHKERRQ(ierr); 1807dccdeccaSVaclav Hapla ierr = DMPlexCreateOverlapLabel(dm, overlap, rootSection, rootrank, leafSection, leafrank, &lblOverlap);CHKERRQ(ierr); 1808a9f1d5b2SMichael Lange /* Convert overlap label to stratified migration SF */ 1809a9f1d5b2SMichael Lange ierr = DMPlexPartitionLabelCreateSF(dm, lblOverlap, &sfOverlap);CHKERRQ(ierr); 1810a9f1d5b2SMichael Lange ierr = DMPlexStratifyMigrationSF(dm, sfOverlap, &sfStratified);CHKERRQ(ierr); 1811a9f1d5b2SMichael Lange ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr); 1812a9f1d5b2SMichael Lange sfOverlap = sfStratified; 1813a9f1d5b2SMichael Lange ierr = PetscObjectSetName((PetscObject) sfOverlap, "Overlap SF");CHKERRQ(ierr); 1814a9f1d5b2SMichael Lange ierr = PetscSFSetFromOptions(sfOverlap);CHKERRQ(ierr); 1815a9f1d5b2SMichael Lange 181615fff7beSMatthew G. Knepley ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr); 181715fff7beSMatthew G. Knepley ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr); 181815fff7beSMatthew G. Knepley ierr = ISDestroy(&rootrank);CHKERRQ(ierr); 181915fff7beSMatthew G. Knepley ierr = ISDestroy(&leafrank);CHKERRQ(ierr); 182067eb269bSLisandro Dalcin ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr); 1821a157612eSMichael Lange 1822a157612eSMichael Lange /* Build the overlapping DM */ 1823a157612eSMichael Lange ierr = DMPlexCreate(comm, dmOverlap);CHKERRQ(ierr); 1824a157612eSMichael Lange ierr = PetscObjectSetName((PetscObject) *dmOverlap, "Parallel Mesh");CHKERRQ(ierr); 1825a9f1d5b2SMichael Lange ierr = DMPlexMigrate(dm, sfOverlap, *dmOverlap);CHKERRQ(ierr); 1826cb54e036SVaclav Hapla /* Store the overlap in the new DM */ 1827cb54e036SVaclav Hapla ((DM_Plex*)(*dmOverlap)->data)->overlap = overlap + ((DM_Plex*)dm->data)->overlap; 1828f5bf2dbfSMichael Lange /* Build the new point SF */ 18291627f6ccSMichael Lange ierr = DMPlexCreatePointSF(*dmOverlap, sfOverlap, PETSC_FALSE, &sfPoint);CHKERRQ(ierr); 1830f5bf2dbfSMichael Lange ierr = DMSetPointSF(*dmOverlap, sfPoint);CHKERRQ(ierr); 1831cf86098cSMatthew G. Knepley ierr = DMGetCoordinateDM(*dmOverlap, &dmCoord);CHKERRQ(ierr); 1832cf86098cSMatthew G. Knepley if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);} 1833f5bf2dbfSMichael Lange ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr); 1834a157612eSMichael Lange /* Cleanup overlap partition */ 1835a9f1d5b2SMichael Lange ierr = DMLabelDestroy(&lblOverlap);CHKERRQ(ierr); 1836a9f1d5b2SMichael Lange if (sf) *sf = sfOverlap; 1837a9f1d5b2SMichael Lange else {ierr = PetscSFDestroy(&sfOverlap);CHKERRQ(ierr);} 18381b858b30SMichael Lange ierr = PetscLogEventEnd(DMPLEX_DistributeOverlap, dm, 0, 0, 0);CHKERRQ(ierr); 1839a157612eSMichael Lange PetscFunctionReturn(0); 1840a157612eSMichael Lange } 18416462276dSToby Isaac 1842cb54e036SVaclav Hapla PetscErrorCode DMPlexGetOverlap_Plex(DM dm, PetscInt *overlap) 1843cb54e036SVaclav Hapla { 1844cb54e036SVaclav Hapla DM_Plex *mesh = (DM_Plex*) dm->data; 1845cb54e036SVaclav Hapla 1846cb54e036SVaclav Hapla PetscFunctionBegin; 1847cb54e036SVaclav Hapla *overlap = mesh->overlap; 1848cb54e036SVaclav Hapla PetscFunctionReturn(0); 1849cb54e036SVaclav Hapla } 1850cb54e036SVaclav Hapla 1851cb54e036SVaclav Hapla /*@ 1852cb54e036SVaclav Hapla DMPlexGetOverlap - Get the DMPlex partition overlap. 1853cb54e036SVaclav Hapla 1854cb54e036SVaclav Hapla Not collective 1855cb54e036SVaclav Hapla 1856cb54e036SVaclav Hapla Input Parameter: 1857cb54e036SVaclav Hapla . dm - The DM 1858cb54e036SVaclav Hapla 1859064ec1f3Sprj- Output Parameter: 1860cb54e036SVaclav Hapla . overlap - The overlap of this DM 1861cb54e036SVaclav Hapla 1862cb54e036SVaclav Hapla Level: intermediate 1863cb54e036SVaclav Hapla 1864cb54e036SVaclav Hapla .seealso: DMPlexDistribute(), DMPlexDistributeOverlap(), DMPlexCreateOverlapLabel() 1865cb54e036SVaclav Hapla @*/ 1866cb54e036SVaclav Hapla PetscErrorCode DMPlexGetOverlap(DM dm, PetscInt *overlap) 1867cb54e036SVaclav Hapla { 1868cb54e036SVaclav Hapla PetscErrorCode ierr; 1869cb54e036SVaclav Hapla 1870cb54e036SVaclav Hapla PetscFunctionBegin; 1871cb54e036SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1872cb54e036SVaclav Hapla ierr = PetscUseMethod(dm,"DMPlexGetOverlap_C",(DM,PetscInt*),(dm,overlap));CHKERRQ(ierr); 1873cb54e036SVaclav Hapla PetscFunctionReturn(0); 1874cb54e036SVaclav Hapla } 1875cb54e036SVaclav Hapla 18766462276dSToby Isaac /*@C 18776462276dSToby Isaac DMPlexGetGatherDM - Get a copy of the DMPlex that gathers all points on the 18786462276dSToby Isaac root process of the original's communicator. 18796462276dSToby Isaac 1880d083f849SBarry Smith Collective on dm 188183655b49SVáclav Hapla 1882064ec1f3Sprj- Input Parameter: 18836462276dSToby Isaac . dm - the original DMPlex object 18846462276dSToby Isaac 18856462276dSToby Isaac Output Parameters: 1886a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 1887a13df41bSStefano Zampini - gatherMesh - the gathered DM object, or NULL 18886462276dSToby Isaac 18896462276dSToby Isaac Level: intermediate 18906462276dSToby Isaac 18916462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetRedundantDM() 18926462276dSToby Isaac @*/ 1893a13df41bSStefano Zampini PetscErrorCode DMPlexGetGatherDM(DM dm, PetscSF *sf, DM *gatherMesh) 18946462276dSToby Isaac { 18956462276dSToby Isaac MPI_Comm comm; 18966462276dSToby Isaac PetscMPIInt size; 18976462276dSToby Isaac PetscPartitioner oldPart, gatherPart; 18986462276dSToby Isaac PetscErrorCode ierr; 18996462276dSToby Isaac 19006462276dSToby Isaac PetscFunctionBegin; 19016462276dSToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1902064a246eSJacob Faibussowitsch PetscValidPointer(gatherMesh,3); 19030c86c063SLisandro Dalcin *gatherMesh = NULL; 1904a13df41bSStefano Zampini if (sf) *sf = NULL; 19056462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 1906ffc4695bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 19076462276dSToby Isaac if (size == 1) PetscFunctionReturn(0); 19086462276dSToby Isaac ierr = DMPlexGetPartitioner(dm,&oldPart);CHKERRQ(ierr); 19096462276dSToby Isaac ierr = PetscObjectReference((PetscObject)oldPart);CHKERRQ(ierr); 19106462276dSToby Isaac ierr = PetscPartitionerCreate(comm,&gatherPart);CHKERRQ(ierr); 19116462276dSToby Isaac ierr = PetscPartitionerSetType(gatherPart,PETSCPARTITIONERGATHER);CHKERRQ(ierr); 19126462276dSToby Isaac ierr = DMPlexSetPartitioner(dm,gatherPart);CHKERRQ(ierr); 1913a13df41bSStefano Zampini ierr = DMPlexDistribute(dm,0,sf,gatherMesh);CHKERRQ(ierr); 1914a13df41bSStefano Zampini 19156462276dSToby Isaac ierr = DMPlexSetPartitioner(dm,oldPart);CHKERRQ(ierr); 19166462276dSToby Isaac ierr = PetscPartitionerDestroy(&gatherPart);CHKERRQ(ierr); 19176462276dSToby Isaac ierr = PetscPartitionerDestroy(&oldPart);CHKERRQ(ierr); 19186462276dSToby Isaac PetscFunctionReturn(0); 19196462276dSToby Isaac } 19206462276dSToby Isaac 19216462276dSToby Isaac /*@C 19226462276dSToby Isaac DMPlexGetRedundantDM - Get a copy of the DMPlex that is completely copied on each process. 19236462276dSToby Isaac 1924d083f849SBarry Smith Collective on dm 192583655b49SVáclav Hapla 1926064ec1f3Sprj- Input Parameter: 19276462276dSToby Isaac . dm - the original DMPlex object 19286462276dSToby Isaac 19296462276dSToby Isaac Output Parameters: 1930a13df41bSStefano Zampini + sf - the PetscSF used for point distribution (optional) 1931a13df41bSStefano Zampini - redundantMesh - the redundant DM object, or NULL 19326462276dSToby Isaac 19336462276dSToby Isaac Level: intermediate 19346462276dSToby Isaac 19356462276dSToby Isaac .seealso: DMPlexDistribute(), DMPlexGetGatherDM() 19366462276dSToby Isaac @*/ 1937a13df41bSStefano Zampini PetscErrorCode DMPlexGetRedundantDM(DM dm, PetscSF *sf, DM *redundantMesh) 19386462276dSToby Isaac { 19396462276dSToby Isaac MPI_Comm comm; 19406462276dSToby Isaac PetscMPIInt size, rank; 19416462276dSToby Isaac PetscInt pStart, pEnd, p; 19426462276dSToby Isaac PetscInt numPoints = -1; 1943a13df41bSStefano Zampini PetscSF migrationSF, sfPoint, gatherSF; 19446462276dSToby Isaac DM gatherDM, dmCoord; 19456462276dSToby Isaac PetscSFNode *points; 19466462276dSToby Isaac PetscErrorCode ierr; 19476462276dSToby Isaac 19486462276dSToby Isaac PetscFunctionBegin; 19496462276dSToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1950064a246eSJacob Faibussowitsch PetscValidPointer(redundantMesh,3); 19510c86c063SLisandro Dalcin *redundantMesh = NULL; 19526462276dSToby Isaac comm = PetscObjectComm((PetscObject)dm); 1953ffc4695bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 195468dbc166SMatthew G. Knepley if (size == 1) { 195568dbc166SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 195668dbc166SMatthew G. Knepley *redundantMesh = dm; 1957a13df41bSStefano Zampini if (sf) *sf = NULL; 195868dbc166SMatthew G. Knepley PetscFunctionReturn(0); 195968dbc166SMatthew G. Knepley } 1960a13df41bSStefano Zampini ierr = DMPlexGetGatherDM(dm,&gatherSF,&gatherDM);CHKERRQ(ierr); 19616462276dSToby Isaac if (!gatherDM) PetscFunctionReturn(0); 1962ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 19636462276dSToby Isaac ierr = DMPlexGetChart(gatherDM,&pStart,&pEnd);CHKERRQ(ierr); 19646462276dSToby Isaac numPoints = pEnd - pStart; 1965ffc4695bSBarry Smith ierr = MPI_Bcast(&numPoints,1,MPIU_INT,0,comm);CHKERRMPI(ierr); 19666462276dSToby Isaac ierr = PetscMalloc1(numPoints,&points);CHKERRQ(ierr); 19676462276dSToby Isaac ierr = PetscSFCreate(comm,&migrationSF);CHKERRQ(ierr); 19686462276dSToby Isaac for (p = 0; p < numPoints; p++) { 19696462276dSToby Isaac points[p].index = p; 19706462276dSToby Isaac points[p].rank = 0; 19716462276dSToby Isaac } 19726462276dSToby Isaac ierr = PetscSFSetGraph(migrationSF,pEnd-pStart,numPoints,NULL,PETSC_OWN_POINTER,points,PETSC_OWN_POINTER);CHKERRQ(ierr); 19736462276dSToby Isaac ierr = DMPlexCreate(comm, redundantMesh);CHKERRQ(ierr); 19746462276dSToby Isaac ierr = PetscObjectSetName((PetscObject) *redundantMesh, "Redundant Mesh");CHKERRQ(ierr); 19756462276dSToby Isaac ierr = DMPlexMigrate(gatherDM, migrationSF, *redundantMesh);CHKERRQ(ierr); 19766462276dSToby Isaac ierr = DMPlexCreatePointSF(*redundantMesh, migrationSF, PETSC_FALSE, &sfPoint);CHKERRQ(ierr); 19776462276dSToby Isaac ierr = DMSetPointSF(*redundantMesh, sfPoint);CHKERRQ(ierr); 19786462276dSToby Isaac ierr = DMGetCoordinateDM(*redundantMesh, &dmCoord);CHKERRQ(ierr); 19796462276dSToby Isaac if (dmCoord) {ierr = DMSetPointSF(dmCoord, sfPoint);CHKERRQ(ierr);} 19806462276dSToby Isaac ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr); 1981a13df41bSStefano Zampini if (sf) { 1982a13df41bSStefano Zampini PetscSF tsf; 1983a13df41bSStefano Zampini 1984a13df41bSStefano Zampini ierr = PetscSFCompose(gatherSF,migrationSF,&tsf);CHKERRQ(ierr); 1985a13df41bSStefano Zampini ierr = DMPlexStratifyMigrationSF(dm, tsf, sf);CHKERRQ(ierr); 1986a13df41bSStefano Zampini ierr = PetscSFDestroy(&tsf);CHKERRQ(ierr); 1987a13df41bSStefano Zampini } 19886462276dSToby Isaac ierr = PetscSFDestroy(&migrationSF);CHKERRQ(ierr); 1989a13df41bSStefano Zampini ierr = PetscSFDestroy(&gatherSF);CHKERRQ(ierr); 19906462276dSToby Isaac ierr = DMDestroy(&gatherDM);CHKERRQ(ierr); 19916462276dSToby Isaac PetscFunctionReturn(0); 19926462276dSToby Isaac } 19935fa78c88SVaclav Hapla 19945fa78c88SVaclav Hapla /*@ 19955fa78c88SVaclav Hapla DMPlexIsDistributed - Find out whether this DM is distributed, i.e. more than one rank owns some points. 19965fa78c88SVaclav Hapla 19975fa78c88SVaclav Hapla Collective 19985fa78c88SVaclav Hapla 19995fa78c88SVaclav Hapla Input Parameter: 20005fa78c88SVaclav Hapla . dm - The DM object 20015fa78c88SVaclav Hapla 20025fa78c88SVaclav Hapla Output Parameter: 20035fa78c88SVaclav Hapla . distributed - Flag whether the DM is distributed 20045fa78c88SVaclav Hapla 20055fa78c88SVaclav Hapla Level: intermediate 20065fa78c88SVaclav Hapla 20075fa78c88SVaclav Hapla Notes: 20085fa78c88SVaclav Hapla This currently finds out whether at least two ranks have any DAG points. 20095fa78c88SVaclav Hapla This involves MPI_Allreduce() with one integer. 20105fa78c88SVaclav Hapla The result is currently not stashed so every call to this routine involves this global communication. 20115fa78c88SVaclav Hapla 20125fa78c88SVaclav Hapla .seealso: DMPlexDistribute(), DMPlexGetOverlap(), DMPlexIsInterpolated() 20135fa78c88SVaclav Hapla @*/ 20145fa78c88SVaclav Hapla PetscErrorCode DMPlexIsDistributed(DM dm, PetscBool *distributed) 20155fa78c88SVaclav Hapla { 20165fa78c88SVaclav Hapla PetscInt pStart, pEnd, count; 20175fa78c88SVaclav Hapla MPI_Comm comm; 20185fa78c88SVaclav Hapla PetscErrorCode ierr; 20195fa78c88SVaclav Hapla 20205fa78c88SVaclav Hapla PetscFunctionBegin; 20215fa78c88SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 20225fa78c88SVaclav Hapla PetscValidPointer(distributed,2); 20235fa78c88SVaclav Hapla ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 20245fa78c88SVaclav Hapla ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 20255fa78c88SVaclav Hapla count = !!(pEnd - pStart); 2026ffc4695bSBarry Smith ierr = MPI_Allreduce(MPI_IN_PLACE, &count, 1, MPIU_INT, MPI_SUM, comm);CHKERRMPI(ierr); 20275fa78c88SVaclav Hapla *distributed = count > 1 ? PETSC_TRUE : PETSC_FALSE; 20285fa78c88SVaclav Hapla PetscFunctionReturn(0); 20295fa78c88SVaclav Hapla } 2030