1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2e8f14785SLisandro Dalcin #include <petsc/private/hashseti.h> 370034214SMatthew G. Knepley 477623264SMatthew G. Knepley PetscClassId PETSCPARTITIONER_CLASSID = 0; 577623264SMatthew G. Knepley 677623264SMatthew G. Knepley PetscFunctionList PetscPartitionerList = NULL; 777623264SMatthew G. Knepley PetscBool PetscPartitionerRegisterAllCalled = PETSC_FALSE; 877623264SMatthew G. Knepley 977623264SMatthew G. Knepley PetscBool ChacoPartitionercite = PETSC_FALSE; 1077623264SMatthew G. Knepley const char ChacoPartitionerCitation[] = "@inproceedings{Chaco95,\n" 1177623264SMatthew G. Knepley " author = {Bruce Hendrickson and Robert Leland},\n" 1277623264SMatthew G. Knepley " title = {A multilevel algorithm for partitioning graphs},\n" 1377623264SMatthew G. Knepley " booktitle = {Supercomputing '95: Proceedings of the 1995 ACM/IEEE Conference on Supercomputing (CDROM)}," 1477623264SMatthew G. Knepley " isbn = {0-89791-816-9},\n" 1577623264SMatthew G. Knepley " pages = {28},\n" 1677623264SMatthew G. Knepley " doi = {http://doi.acm.org/10.1145/224170.224228},\n" 1777623264SMatthew G. Knepley " publisher = {ACM Press},\n" 1877623264SMatthew G. Knepley " address = {New York},\n" 1977623264SMatthew G. Knepley " year = {1995}\n}\n"; 2077623264SMatthew G. Knepley 2177623264SMatthew G. Knepley PetscBool ParMetisPartitionercite = PETSC_FALSE; 2277623264SMatthew G. Knepley const char ParMetisPartitionerCitation[] = "@article{KarypisKumar98,\n" 2377623264SMatthew G. Knepley " author = {George Karypis and Vipin Kumar},\n" 2477623264SMatthew G. Knepley " title = {A Parallel Algorithm for Multilevel Graph Partitioning and Sparse Matrix Ordering},\n" 2577623264SMatthew G. Knepley " journal = {Journal of Parallel and Distributed Computing},\n" 2677623264SMatthew G. Knepley " volume = {48},\n" 2777623264SMatthew G. Knepley " pages = {71--85},\n" 2877623264SMatthew G. Knepley " year = {1998}\n}\n"; 2977623264SMatthew G. Knepley 300134a67bSLisandro Dalcin PETSC_STATIC_INLINE PetscInt DMPlex_GlobalID(PetscInt point) { return point >= 0 ? point : -(point+1); } 310134a67bSLisandro Dalcin 32532c4e7dSMichael Lange /*@C 33532c4e7dSMichael Lange DMPlexCreatePartitionerGraph - Create a CSR graph of point connections for the partitioner 34532c4e7dSMichael Lange 35532c4e7dSMichael Lange Input Parameters: 36532c4e7dSMichael Lange + dm - The mesh DM dm 37532c4e7dSMichael Lange - height - Height of the strata from which to construct the graph 38532c4e7dSMichael Lange 39532c4e7dSMichael Lange Output Parameter: 40532c4e7dSMichael Lange + numVertices - Number of vertices in the graph 413fa7752dSToby Isaac . offsets - Point offsets in the graph 423fa7752dSToby Isaac . adjacency - Point connectivity in the graph 433fa7752dSToby Isaac - globalNumbering - A map from the local cell numbering to the global numbering used in "adjacency". Negative indicates that the cell is a duplicate from another process. 44532c4e7dSMichael Lange 45b0441da4SMatthew G. Knepley The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function 46532c4e7dSMichael Lange representation on the mesh. 47532c4e7dSMichael Lange 48532c4e7dSMichael Lange Level: developer 49532c4e7dSMichael Lange 50b0441da4SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate(), DMSetAdjacency() 51532c4e7dSMichael Lange @*/ 523fa7752dSToby Isaac PetscErrorCode DMPlexCreatePartitionerGraph(DM dm, PetscInt height, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency, IS *globalNumbering) 53532c4e7dSMichael Lange { 54ffbd6163SMatthew G. Knepley PetscInt dim, depth, p, pStart, pEnd, a, adjSize, idx, size; 55389e55d8SMichael Lange PetscInt *adj = NULL, *vOffsets = NULL, *graph = NULL; 568cfe4c1fSMichael Lange IS cellNumbering; 578cfe4c1fSMichael Lange const PetscInt *cellNum; 58532c4e7dSMichael Lange PetscBool useCone, useClosure; 59532c4e7dSMichael Lange PetscSection section; 60532c4e7dSMichael Lange PetscSegBuffer adjBuffer; 618cfe4c1fSMichael Lange PetscSF sfPoint; 628f4e72b9SMatthew G. Knepley PetscInt *adjCells = NULL, *remoteCells = NULL; 638f4e72b9SMatthew G. Knepley const PetscInt *local; 648f4e72b9SMatthew G. Knepley PetscInt nroots, nleaves, l; 65532c4e7dSMichael Lange PetscMPIInt rank; 66532c4e7dSMichael Lange PetscErrorCode ierr; 67532c4e7dSMichael Lange 68532c4e7dSMichael Lange PetscFunctionBegin; 69532c4e7dSMichael Lange ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 70ffbd6163SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 71ffbd6163SMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 72ffbd6163SMatthew G. Knepley if (dim != depth) { 73ffbd6163SMatthew G. Knepley /* We do not handle the uninterpolated case here */ 74ffbd6163SMatthew G. Knepley ierr = DMPlexCreateNeighborCSR(dm, height, numVertices, offsets, adjacency);CHKERRQ(ierr); 75ffbd6163SMatthew G. Knepley /* DMPlexCreateNeighborCSR does not make a numbering */ 76ffbd6163SMatthew G. Knepley if (globalNumbering) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, globalNumbering);CHKERRQ(ierr);} 77ffbd6163SMatthew G. Knepley /* Different behavior for empty graphs */ 78ffbd6163SMatthew G. Knepley if (!*numVertices) { 79ffbd6163SMatthew G. Knepley ierr = PetscMalloc1(1, offsets);CHKERRQ(ierr); 80ffbd6163SMatthew G. Knepley (*offsets)[0] = 0; 81ffbd6163SMatthew G. Knepley } 82ffbd6163SMatthew G. Knepley /* Broken in parallel */ 83ffbd6163SMatthew G. Knepley if (rank && *numVertices) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Parallel partitioning of uninterpolated meshes not supported"); 84ffbd6163SMatthew G. Knepley PetscFunctionReturn(0); 85ffbd6163SMatthew G. Knepley } 868cfe4c1fSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 870134a67bSLisandro Dalcin ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr); 88532c4e7dSMichael Lange /* Build adjacency graph via a section/segbuffer */ 89532c4e7dSMichael Lange ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ion);CHKERRQ(ierr); 90532c4e7dSMichael Lange ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr); 91532c4e7dSMichael Lange ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr); 92532c4e7dSMichael Lange /* Always use FVM adjacency to create partitioner graph */ 93b0441da4SMatthew G. Knepley ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr); 94b0441da4SMatthew G. Knepley ierr = DMSetBasicAdjacency(dm, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr); 950134a67bSLisandro Dalcin ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, 0, NULL, sfPoint, &cellNumbering);CHKERRQ(ierr); 963fa7752dSToby Isaac if (globalNumbering) { 973fa7752dSToby Isaac ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr); 983fa7752dSToby Isaac *globalNumbering = cellNumbering; 993fa7752dSToby Isaac } 1008cfe4c1fSMichael Lange ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr); 1018f4e72b9SMatthew G. Knepley /* For all boundary faces (including faces adjacent to a ghost cell), record the local cell in adjCells 1028f4e72b9SMatthew G. Knepley Broadcast adjCells to remoteCells (to get cells from roots) and Reduce adjCells to remoteCells (to get cells from leaves) 1038f4e72b9SMatthew G. Knepley */ 1040134a67bSLisandro Dalcin ierr = PetscSFGetGraph(sfPoint, &nroots, &nleaves, &local, NULL);CHKERRQ(ierr); 1058f4e72b9SMatthew G. Knepley if (nroots >= 0) { 1068f4e72b9SMatthew G. Knepley PetscInt fStart, fEnd, f; 1078f4e72b9SMatthew G. Knepley 1088f4e72b9SMatthew G. Knepley ierr = PetscCalloc2(nroots, &adjCells, nroots, &remoteCells);CHKERRQ(ierr); 1090134a67bSLisandro Dalcin ierr = DMPlexGetHeightStratum(dm, height+1, &fStart, &fEnd);CHKERRQ(ierr); 1108f4e72b9SMatthew G. Knepley for (l = 0; l < nroots; ++l) adjCells[l] = -3; 1118f4e72b9SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 1128f4e72b9SMatthew G. Knepley const PetscInt *support; 1138f4e72b9SMatthew G. Knepley PetscInt supportSize; 1148f4e72b9SMatthew G. Knepley 1158f4e72b9SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr); 1168f4e72b9SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 1170134a67bSLisandro Dalcin if (supportSize == 1) adjCells[f] = DMPlex_GlobalID(cellNum[support[0]]); 1188f4e72b9SMatthew G. Knepley else if (supportSize == 2) { 1198f4e72b9SMatthew G. Knepley ierr = PetscFindInt(support[0], nleaves, local, &p);CHKERRQ(ierr); 1200134a67bSLisandro Dalcin if (p >= 0) adjCells[f] = DMPlex_GlobalID(cellNum[support[1]]); 1218f4e72b9SMatthew G. Knepley ierr = PetscFindInt(support[1], nleaves, local, &p);CHKERRQ(ierr); 1220134a67bSLisandro Dalcin if (p >= 0) adjCells[f] = DMPlex_GlobalID(cellNum[support[0]]); 1230134a67bSLisandro Dalcin } 1240134a67bSLisandro Dalcin /* Handle non-conforming meshes */ 1250134a67bSLisandro Dalcin if (supportSize > 2) { 1260134a67bSLisandro Dalcin PetscInt numChildren, i; 1270134a67bSLisandro Dalcin const PetscInt *children; 1280134a67bSLisandro Dalcin 1290134a67bSLisandro Dalcin ierr = DMPlexGetTreeChildren(dm, f, &numChildren, &children);CHKERRQ(ierr); 1300134a67bSLisandro Dalcin for (i = 0; i < numChildren; ++i) { 1310134a67bSLisandro Dalcin const PetscInt child = children[i]; 1320134a67bSLisandro Dalcin if (fStart <= child && child < fEnd) { 1330134a67bSLisandro Dalcin ierr = DMPlexGetSupport(dm, child, &support);CHKERRQ(ierr); 1340134a67bSLisandro Dalcin ierr = DMPlexGetSupportSize(dm, child, &supportSize);CHKERRQ(ierr); 1350134a67bSLisandro Dalcin if (supportSize == 1) adjCells[child] = DMPlex_GlobalID(cellNum[support[0]]); 1360134a67bSLisandro Dalcin else if (supportSize == 2) { 1370134a67bSLisandro Dalcin ierr = PetscFindInt(support[0], nleaves, local, &p);CHKERRQ(ierr); 1380134a67bSLisandro Dalcin if (p >= 0) adjCells[child] = DMPlex_GlobalID(cellNum[support[1]]); 1390134a67bSLisandro Dalcin ierr = PetscFindInt(support[1], nleaves, local, &p);CHKERRQ(ierr); 1400134a67bSLisandro Dalcin if (p >= 0) adjCells[child] = DMPlex_GlobalID(cellNum[support[0]]); 1410134a67bSLisandro Dalcin } 1420134a67bSLisandro Dalcin } 1430134a67bSLisandro Dalcin } 1448f4e72b9SMatthew G. Knepley } 1458f4e72b9SMatthew G. Knepley } 1468f4e72b9SMatthew G. Knepley for (l = 0; l < nroots; ++l) remoteCells[l] = -1; 1478f4e72b9SMatthew G. Knepley ierr = PetscSFBcastBegin(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr); 1488f4e72b9SMatthew G. Knepley ierr = PetscSFBcastEnd(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr); 1498f4e72b9SMatthew G. Knepley ierr = PetscSFReduceBegin(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr); 1508f4e72b9SMatthew G. Knepley ierr = PetscSFReduceEnd(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr); 1518f4e72b9SMatthew G. Knepley } 1528f4e72b9SMatthew G. Knepley /* Combine local and global adjacencies */ 1538cfe4c1fSMichael Lange for (*numVertices = 0, p = pStart; p < pEnd; p++) { 1548cfe4c1fSMichael Lange /* Skip non-owned cells in parallel (ParMetis expects no overlap) */ 1558cfe4c1fSMichael Lange if (nroots > 0) {if (cellNum[p] < 0) continue;} 1568f4e72b9SMatthew G. Knepley /* Add remote cells */ 1578f4e72b9SMatthew G. Knepley if (remoteCells) { 1580134a67bSLisandro Dalcin const PetscInt gp = DMPlex_GlobalID(cellNum[p]); 1590134a67bSLisandro Dalcin PetscInt coneSize, numChildren, c, i; 1600134a67bSLisandro Dalcin const PetscInt *cone, *children; 1610134a67bSLisandro Dalcin 1628f4e72b9SMatthew G. Knepley ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr); 1638f4e72b9SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr); 1648f4e72b9SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 1650134a67bSLisandro Dalcin const PetscInt point = cone[c]; 1660134a67bSLisandro Dalcin if (remoteCells[point] >= 0 && remoteCells[point] != gp) { 1678f4e72b9SMatthew G. Knepley PetscInt *PETSC_RESTRICT pBuf; 1688f4e72b9SMatthew G. Knepley ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr); 1698f4e72b9SMatthew G. Knepley ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr); 1700134a67bSLisandro Dalcin *pBuf = remoteCells[point]; 1710134a67bSLisandro Dalcin } 1720134a67bSLisandro Dalcin /* Handle non-conforming meshes */ 1730134a67bSLisandro Dalcin ierr = DMPlexGetTreeChildren(dm, point, &numChildren, &children);CHKERRQ(ierr); 1740134a67bSLisandro Dalcin for (i = 0; i < numChildren; ++i) { 1750134a67bSLisandro Dalcin const PetscInt child = children[i]; 1760134a67bSLisandro Dalcin if (remoteCells[child] >= 0 && remoteCells[child] != gp) { 1770134a67bSLisandro Dalcin PetscInt *PETSC_RESTRICT pBuf; 1780134a67bSLisandro Dalcin ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr); 1790134a67bSLisandro Dalcin ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr); 1800134a67bSLisandro Dalcin *pBuf = remoteCells[child]; 1810134a67bSLisandro Dalcin } 1828f4e72b9SMatthew G. Knepley } 1838f4e72b9SMatthew G. Knepley } 1848f4e72b9SMatthew G. Knepley } 1858f4e72b9SMatthew G. Knepley /* Add local cells */ 186532c4e7dSMichael Lange adjSize = PETSC_DETERMINE; 187532c4e7dSMichael Lange ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr); 188532c4e7dSMichael Lange for (a = 0; a < adjSize; ++a) { 189532c4e7dSMichael Lange const PetscInt point = adj[a]; 190532c4e7dSMichael Lange if (point != p && pStart <= point && point < pEnd) { 191532c4e7dSMichael Lange PetscInt *PETSC_RESTRICT pBuf; 192532c4e7dSMichael Lange ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr); 193532c4e7dSMichael Lange ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr); 1940134a67bSLisandro Dalcin *pBuf = DMPlex_GlobalID(cellNum[point]); 195532c4e7dSMichael Lange } 196532c4e7dSMichael Lange } 1978cfe4c1fSMichael Lange (*numVertices)++; 198532c4e7dSMichael Lange } 1994e468a93SLisandro Dalcin ierr = PetscFree(adj);CHKERRQ(ierr); 2008f4e72b9SMatthew G. Knepley ierr = PetscFree2(adjCells, remoteCells);CHKERRQ(ierr); 201b0441da4SMatthew G. Knepley ierr = DMSetBasicAdjacency(dm, useCone, useClosure);CHKERRQ(ierr); 2024e468a93SLisandro Dalcin 203532c4e7dSMichael Lange /* Derive CSR graph from section/segbuffer */ 204532c4e7dSMichael Lange ierr = PetscSectionSetUp(section);CHKERRQ(ierr); 205532c4e7dSMichael Lange ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 206389e55d8SMichael Lange ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr); 20743f7d02bSMichael Lange for (idx = 0, p = pStart; p < pEnd; p++) { 20843f7d02bSMichael Lange if (nroots > 0) {if (cellNum[p] < 0) continue;} 20943f7d02bSMichael Lange ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr); 21043f7d02bSMichael Lange } 211532c4e7dSMichael Lange vOffsets[*numVertices] = size; 212389e55d8SMichael Lange ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr); 2134e468a93SLisandro Dalcin 2144e468a93SLisandro Dalcin if (nroots >= 0) { 2154e468a93SLisandro Dalcin /* Filter out duplicate edges using section/segbuffer */ 2164e468a93SLisandro Dalcin ierr = PetscSectionReset(section);CHKERRQ(ierr); 2174e468a93SLisandro Dalcin ierr = PetscSectionSetChart(section, 0, *numVertices);CHKERRQ(ierr); 2184e468a93SLisandro Dalcin for (p = 0; p < *numVertices; p++) { 2194e468a93SLisandro Dalcin PetscInt start = vOffsets[p], end = vOffsets[p+1]; 2204e468a93SLisandro Dalcin PetscInt numEdges = end-start, *PETSC_RESTRICT edges; 2214e468a93SLisandro Dalcin ierr = PetscSortRemoveDupsInt(&numEdges, &graph[start]);CHKERRQ(ierr); 2224e468a93SLisandro Dalcin ierr = PetscSectionSetDof(section, p, numEdges);CHKERRQ(ierr); 2234e468a93SLisandro Dalcin ierr = PetscSegBufferGetInts(adjBuffer, numEdges, &edges);CHKERRQ(ierr); 2244e468a93SLisandro Dalcin ierr = PetscMemcpy(edges, &graph[start], numEdges*sizeof(*edges));CHKERRQ(ierr); 2254e468a93SLisandro Dalcin } 2264e468a93SLisandro Dalcin ierr = PetscFree(vOffsets);CHKERRQ(ierr); 2274e468a93SLisandro Dalcin ierr = PetscFree(graph);CHKERRQ(ierr); 2284e468a93SLisandro Dalcin /* Derive CSR graph from section/segbuffer */ 2294e468a93SLisandro Dalcin ierr = PetscSectionSetUp(section);CHKERRQ(ierr); 2304e468a93SLisandro Dalcin ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 2314e468a93SLisandro Dalcin ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr); 2324e468a93SLisandro Dalcin for (idx = 0, p = 0; p < *numVertices; p++) { 2334e468a93SLisandro Dalcin ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr); 2344e468a93SLisandro Dalcin } 2354e468a93SLisandro Dalcin vOffsets[*numVertices] = size; 2364e468a93SLisandro Dalcin ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr); 2374e468a93SLisandro Dalcin } else { 2384e468a93SLisandro Dalcin /* Sort adjacencies (not strictly necessary) */ 2394e468a93SLisandro Dalcin for (p = 0; p < *numVertices; p++) { 2404e468a93SLisandro Dalcin PetscInt start = vOffsets[p], end = vOffsets[p+1]; 2414e468a93SLisandro Dalcin ierr = PetscSortInt(end-start, &graph[start]);CHKERRQ(ierr); 2424e468a93SLisandro Dalcin } 2434e468a93SLisandro Dalcin } 2444e468a93SLisandro Dalcin 2454e468a93SLisandro Dalcin if (offsets) *offsets = vOffsets; 246389e55d8SMichael Lange if (adjacency) *adjacency = graph; 2474e468a93SLisandro Dalcin 248532c4e7dSMichael Lange /* Cleanup */ 249532c4e7dSMichael Lange ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr); 250532c4e7dSMichael Lange ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); 2514e468a93SLisandro Dalcin ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr); 2524e468a93SLisandro Dalcin ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr); 253532c4e7dSMichael Lange PetscFunctionReturn(0); 254532c4e7dSMichael Lange } 255532c4e7dSMichael Lange 256d5577e40SMatthew G. Knepley /*@C 257d5577e40SMatthew G. Knepley DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format. 258d5577e40SMatthew G. Knepley 259d5577e40SMatthew G. Knepley Collective 260d5577e40SMatthew G. Knepley 261d5577e40SMatthew G. Knepley Input Arguments: 262d5577e40SMatthew G. Knepley + dm - The DMPlex 263d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0) 264d5577e40SMatthew G. Knepley 265d5577e40SMatthew G. Knepley Output Arguments: 266d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh. 267d5577e40SMatthew G. Knepley . offsets - The offset to the adjacency list for each cell 268d5577e40SMatthew G. Knepley - adjacency - The adjacency list for all cells 269d5577e40SMatthew G. Knepley 270d5577e40SMatthew G. Knepley Note: This is suitable for input to a mesh partitioner like ParMetis. 271d5577e40SMatthew G. Knepley 272d5577e40SMatthew G. Knepley Level: advanced 273d5577e40SMatthew G. Knepley 274d5577e40SMatthew G. Knepley .seealso: DMPlexCreate() 275d5577e40SMatthew G. Knepley @*/ 27670034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency) 27770034214SMatthew G. Knepley { 27870034214SMatthew G. Knepley const PetscInt maxFaceCases = 30; 27970034214SMatthew G. Knepley PetscInt numFaceCases = 0; 28070034214SMatthew G. Knepley PetscInt numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */ 28170034214SMatthew G. Knepley PetscInt *off, *adj; 28270034214SMatthew G. Knepley PetscInt *neighborCells = NULL; 28370034214SMatthew G. Knepley PetscInt dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell; 28470034214SMatthew G. Knepley PetscErrorCode ierr; 28570034214SMatthew G. Knepley 28670034214SMatthew G. Knepley PetscFunctionBegin; 28770034214SMatthew G. Knepley /* For parallel partitioning, I think you have to communicate supports */ 288c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 28970034214SMatthew G. Knepley cellDim = dim - cellHeight; 29070034214SMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 29170034214SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 29270034214SMatthew G. Knepley if (cEnd - cStart == 0) { 29370034214SMatthew G. Knepley if (numVertices) *numVertices = 0; 29470034214SMatthew G. Knepley if (offsets) *offsets = NULL; 29570034214SMatthew G. Knepley if (adjacency) *adjacency = NULL; 29670034214SMatthew G. Knepley PetscFunctionReturn(0); 29770034214SMatthew G. Knepley } 29870034214SMatthew G. Knepley numCells = cEnd - cStart; 29970034214SMatthew G. Knepley faceDepth = depth - cellHeight; 30070034214SMatthew G. Knepley if (dim == depth) { 30170034214SMatthew G. Knepley PetscInt f, fStart, fEnd; 30270034214SMatthew G. Knepley 30370034214SMatthew G. Knepley ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr); 30470034214SMatthew G. Knepley /* Count neighboring cells */ 30570034214SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr); 30670034214SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 30770034214SMatthew G. Knepley const PetscInt *support; 30870034214SMatthew G. Knepley PetscInt supportSize; 30970034214SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 31070034214SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr); 31170034214SMatthew G. Knepley if (supportSize == 2) { 3129ffc88e4SToby Isaac PetscInt numChildren; 3139ffc88e4SToby Isaac 3149ffc88e4SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 3159ffc88e4SToby Isaac if (!numChildren) { 31670034214SMatthew G. Knepley ++off[support[0]-cStart+1]; 31770034214SMatthew G. Knepley ++off[support[1]-cStart+1]; 31870034214SMatthew G. Knepley } 31970034214SMatthew G. Knepley } 3209ffc88e4SToby Isaac } 32170034214SMatthew G. Knepley /* Prefix sum */ 32270034214SMatthew G. Knepley for (c = 1; c <= numCells; ++c) off[c] += off[c-1]; 32370034214SMatthew G. Knepley if (adjacency) { 32470034214SMatthew G. Knepley PetscInt *tmp; 32570034214SMatthew G. Knepley 32670034214SMatthew G. Knepley ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr); 327854ce69bSBarry Smith ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr); 32870034214SMatthew G. Knepley ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr); 32970034214SMatthew G. Knepley /* Get neighboring cells */ 33070034214SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 33170034214SMatthew G. Knepley const PetscInt *support; 33270034214SMatthew G. Knepley PetscInt supportSize; 33370034214SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 33470034214SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr); 33570034214SMatthew G. Knepley if (supportSize == 2) { 3369ffc88e4SToby Isaac PetscInt numChildren; 3379ffc88e4SToby Isaac 3389ffc88e4SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 3399ffc88e4SToby Isaac if (!numChildren) { 34070034214SMatthew G. Knepley adj[tmp[support[0]-cStart]++] = support[1]; 34170034214SMatthew G. Knepley adj[tmp[support[1]-cStart]++] = support[0]; 34270034214SMatthew G. Knepley } 34370034214SMatthew G. Knepley } 3449ffc88e4SToby Isaac } 34570034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 34670034214SMatthew G. Knepley for (c = 0; c < cEnd-cStart; ++c) if (tmp[c] != off[c+1]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Offset %d != %d for cell %d", tmp[c], off[c], c+cStart); 34770034214SMatthew G. Knepley #endif 34870034214SMatthew G. Knepley ierr = PetscFree(tmp);CHKERRQ(ierr); 34970034214SMatthew G. Knepley } 35070034214SMatthew G. Knepley if (numVertices) *numVertices = numCells; 35170034214SMatthew G. Knepley if (offsets) *offsets = off; 35270034214SMatthew G. Knepley if (adjacency) *adjacency = adj; 35370034214SMatthew G. Knepley PetscFunctionReturn(0); 35470034214SMatthew G. Knepley } 35570034214SMatthew G. Knepley /* Setup face recognition */ 35670034214SMatthew G. Knepley if (faceDepth == 1) { 35770034214SMatthew G. Knepley PetscInt cornersSeen[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* Could use PetscBT */ 35870034214SMatthew G. Knepley 35970034214SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 36070034214SMatthew G. Knepley PetscInt corners; 36170034214SMatthew G. Knepley 36270034214SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr); 36370034214SMatthew G. Knepley if (!cornersSeen[corners]) { 36470034214SMatthew G. Knepley PetscInt nFV; 36570034214SMatthew G. Knepley 36670034214SMatthew G. Knepley if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases"); 36770034214SMatthew G. Knepley cornersSeen[corners] = 1; 36870034214SMatthew G. Knepley 36970034214SMatthew G. Knepley ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr); 37070034214SMatthew G. Knepley 37170034214SMatthew G. Knepley numFaceVertices[numFaceCases++] = nFV; 37270034214SMatthew G. Knepley } 37370034214SMatthew G. Knepley } 37470034214SMatthew G. Knepley } 37570034214SMatthew G. Knepley ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr); 37670034214SMatthew G. Knepley /* Count neighboring cells */ 37770034214SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 37870034214SMatthew G. Knepley PetscInt numNeighbors = PETSC_DETERMINE, n; 37970034214SMatthew G. Knepley 3808b0b4c70SToby Isaac ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr); 38170034214SMatthew G. Knepley /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */ 38270034214SMatthew G. Knepley for (n = 0; n < numNeighbors; ++n) { 38370034214SMatthew G. Knepley PetscInt cellPair[2]; 38470034214SMatthew G. Knepley PetscBool found = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE; 38570034214SMatthew G. Knepley PetscInt meetSize = 0; 38670034214SMatthew G. Knepley const PetscInt *meet = NULL; 38770034214SMatthew G. Knepley 38870034214SMatthew G. Knepley cellPair[0] = cell; cellPair[1] = neighborCells[n]; 38970034214SMatthew G. Knepley if (cellPair[0] == cellPair[1]) continue; 39070034214SMatthew G. Knepley if (!found) { 39170034214SMatthew G. Knepley ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 39270034214SMatthew G. Knepley if (meetSize) { 39370034214SMatthew G. Knepley PetscInt f; 39470034214SMatthew G. Knepley 39570034214SMatthew G. Knepley for (f = 0; f < numFaceCases; ++f) { 39670034214SMatthew G. Knepley if (numFaceVertices[f] == meetSize) { 39770034214SMatthew G. Knepley found = PETSC_TRUE; 39870034214SMatthew G. Knepley break; 39970034214SMatthew G. Knepley } 40070034214SMatthew G. Knepley } 40170034214SMatthew G. Knepley } 40270034214SMatthew G. Knepley ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 40370034214SMatthew G. Knepley } 40470034214SMatthew G. Knepley if (found) ++off[cell-cStart+1]; 40570034214SMatthew G. Knepley } 40670034214SMatthew G. Knepley } 40770034214SMatthew G. Knepley /* Prefix sum */ 40870034214SMatthew G. Knepley for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1]; 40970034214SMatthew G. Knepley 41070034214SMatthew G. Knepley if (adjacency) { 41170034214SMatthew G. Knepley ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr); 41270034214SMatthew G. Knepley /* Get neighboring cells */ 41370034214SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 41470034214SMatthew G. Knepley PetscInt numNeighbors = PETSC_DETERMINE, n; 41570034214SMatthew G. Knepley PetscInt cellOffset = 0; 41670034214SMatthew G. Knepley 4178b0b4c70SToby Isaac ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr); 41870034214SMatthew G. Knepley /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */ 41970034214SMatthew G. Knepley for (n = 0; n < numNeighbors; ++n) { 42070034214SMatthew G. Knepley PetscInt cellPair[2]; 42170034214SMatthew G. Knepley PetscBool found = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE; 42270034214SMatthew G. Knepley PetscInt meetSize = 0; 42370034214SMatthew G. Knepley const PetscInt *meet = NULL; 42470034214SMatthew G. Knepley 42570034214SMatthew G. Knepley cellPair[0] = cell; cellPair[1] = neighborCells[n]; 42670034214SMatthew G. Knepley if (cellPair[0] == cellPair[1]) continue; 42770034214SMatthew G. Knepley if (!found) { 42870034214SMatthew G. Knepley ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 42970034214SMatthew G. Knepley if (meetSize) { 43070034214SMatthew G. Knepley PetscInt f; 43170034214SMatthew G. Knepley 43270034214SMatthew G. Knepley for (f = 0; f < numFaceCases; ++f) { 43370034214SMatthew G. Knepley if (numFaceVertices[f] == meetSize) { 43470034214SMatthew G. Knepley found = PETSC_TRUE; 43570034214SMatthew G. Knepley break; 43670034214SMatthew G. Knepley } 43770034214SMatthew G. Knepley } 43870034214SMatthew G. Knepley } 43970034214SMatthew G. Knepley ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 44070034214SMatthew G. Knepley } 44170034214SMatthew G. Knepley if (found) { 44270034214SMatthew G. Knepley adj[off[cell-cStart]+cellOffset] = neighborCells[n]; 44370034214SMatthew G. Knepley ++cellOffset; 44470034214SMatthew G. Knepley } 44570034214SMatthew G. Knepley } 44670034214SMatthew G. Knepley } 44770034214SMatthew G. Knepley } 44870034214SMatthew G. Knepley ierr = PetscFree(neighborCells);CHKERRQ(ierr); 44970034214SMatthew G. Knepley if (numVertices) *numVertices = numCells; 45070034214SMatthew G. Knepley if (offsets) *offsets = off; 45170034214SMatthew G. Knepley if (adjacency) *adjacency = adj; 45270034214SMatthew G. Knepley PetscFunctionReturn(0); 45370034214SMatthew G. Knepley } 45470034214SMatthew G. Knepley 45577623264SMatthew G. Knepley /*@C 45677623264SMatthew G. Knepley PetscPartitionerRegister - Adds a new PetscPartitioner implementation 45777623264SMatthew G. Knepley 45877623264SMatthew G. Knepley Not Collective 45977623264SMatthew G. Knepley 46077623264SMatthew G. Knepley Input Parameters: 46177623264SMatthew G. Knepley + name - The name of a new user-defined creation routine 46277623264SMatthew G. Knepley - create_func - The creation routine itself 46377623264SMatthew G. Knepley 46477623264SMatthew G. Knepley Notes: 46577623264SMatthew G. Knepley PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners 46677623264SMatthew G. Knepley 46777623264SMatthew G. Knepley Sample usage: 46877623264SMatthew G. Knepley .vb 46977623264SMatthew G. Knepley PetscPartitionerRegister("my_part", MyPetscPartitionerCreate); 47077623264SMatthew G. Knepley .ve 47177623264SMatthew G. Knepley 47277623264SMatthew G. Knepley Then, your PetscPartitioner type can be chosen with the procedural interface via 47377623264SMatthew G. Knepley .vb 47477623264SMatthew G. Knepley PetscPartitionerCreate(MPI_Comm, PetscPartitioner *); 47577623264SMatthew G. Knepley PetscPartitionerSetType(PetscPartitioner, "my_part"); 47677623264SMatthew G. Knepley .ve 47777623264SMatthew G. Knepley or at runtime via the option 47877623264SMatthew G. Knepley .vb 47977623264SMatthew G. Knepley -petscpartitioner_type my_part 48077623264SMatthew G. Knepley .ve 48177623264SMatthew G. Knepley 48277623264SMatthew G. Knepley Level: advanced 48377623264SMatthew G. Knepley 48477623264SMatthew G. Knepley .keywords: PetscPartitioner, register 48577623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy() 48677623264SMatthew G. Knepley 48777623264SMatthew G. Knepley @*/ 48877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner)) 48977623264SMatthew G. Knepley { 49077623264SMatthew G. Knepley PetscErrorCode ierr; 49177623264SMatthew G. Knepley 49277623264SMatthew G. Knepley PetscFunctionBegin; 49377623264SMatthew G. Knepley ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr); 49477623264SMatthew G. Knepley PetscFunctionReturn(0); 49577623264SMatthew G. Knepley } 49677623264SMatthew G. Knepley 49777623264SMatthew G. Knepley /*@C 49877623264SMatthew G. Knepley PetscPartitionerSetType - Builds a particular PetscPartitioner 49977623264SMatthew G. Knepley 50077623264SMatthew G. Knepley Collective on PetscPartitioner 50177623264SMatthew G. Knepley 50277623264SMatthew G. Knepley Input Parameters: 50377623264SMatthew G. Knepley + part - The PetscPartitioner object 50477623264SMatthew G. Knepley - name - The kind of partitioner 50577623264SMatthew G. Knepley 50677623264SMatthew G. Knepley Options Database Key: 50777623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types 50877623264SMatthew G. Knepley 50977623264SMatthew G. Knepley Level: intermediate 51077623264SMatthew G. Knepley 51177623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type 51277623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate() 51377623264SMatthew G. Knepley @*/ 51477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name) 51577623264SMatthew G. Knepley { 51677623264SMatthew G. Knepley PetscErrorCode (*r)(PetscPartitioner); 51777623264SMatthew G. Knepley PetscBool match; 51877623264SMatthew G. Knepley PetscErrorCode ierr; 51977623264SMatthew G. Knepley 52077623264SMatthew G. Knepley PetscFunctionBegin; 52177623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 52277623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr); 52377623264SMatthew G. Knepley if (match) PetscFunctionReturn(0); 52477623264SMatthew G. Knepley 5250f51fdf8SToby Isaac ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 52677623264SMatthew G. Knepley ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr); 52777623264SMatthew G. Knepley if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name); 52877623264SMatthew G. Knepley 52977623264SMatthew G. Knepley if (part->ops->destroy) { 53077623264SMatthew G. Knepley ierr = (*part->ops->destroy)(part);CHKERRQ(ierr); 53177623264SMatthew G. Knepley } 532*074d466cSStefano Zampini part->noGraph = PETSC_FALSE; 53309161815SVaclav Hapla ierr = PetscMemzero(part->ops, sizeof(struct _PetscPartitionerOps));CHKERRQ(ierr); 53477623264SMatthew G. Knepley ierr = (*r)(part);CHKERRQ(ierr); 53577623264SMatthew G. Knepley ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr); 53677623264SMatthew G. Knepley PetscFunctionReturn(0); 53777623264SMatthew G. Knepley } 53877623264SMatthew G. Knepley 53977623264SMatthew G. Knepley /*@C 54077623264SMatthew G. Knepley PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object. 54177623264SMatthew G. Knepley 54277623264SMatthew G. Knepley Not Collective 54377623264SMatthew G. Knepley 54477623264SMatthew G. Knepley Input Parameter: 54577623264SMatthew G. Knepley . part - The PetscPartitioner 54677623264SMatthew G. Knepley 54777623264SMatthew G. Knepley Output Parameter: 54877623264SMatthew G. Knepley . name - The PetscPartitioner type name 54977623264SMatthew G. Knepley 55077623264SMatthew G. Knepley Level: intermediate 55177623264SMatthew G. Knepley 55277623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name 55377623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate() 55477623264SMatthew G. Knepley @*/ 55577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name) 55677623264SMatthew G. Knepley { 55777623264SMatthew G. Knepley PetscErrorCode ierr; 55877623264SMatthew G. Knepley 55977623264SMatthew G. Knepley PetscFunctionBegin; 56077623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 56177623264SMatthew G. Knepley PetscValidPointer(name, 2); 5620f51fdf8SToby Isaac ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 56377623264SMatthew G. Knepley *name = ((PetscObject) part)->type_name; 56477623264SMatthew G. Knepley PetscFunctionReturn(0); 56577623264SMatthew G. Knepley } 56677623264SMatthew G. Knepley 56777623264SMatthew G. Knepley /*@C 56877623264SMatthew G. Knepley PetscPartitionerView - Views a PetscPartitioner 56977623264SMatthew G. Knepley 57077623264SMatthew G. Knepley Collective on PetscPartitioner 57177623264SMatthew G. Knepley 57277623264SMatthew G. Knepley Input Parameter: 57377623264SMatthew G. Knepley + part - the PetscPartitioner object to view 57477623264SMatthew G. Knepley - v - the viewer 57577623264SMatthew G. Knepley 57677623264SMatthew G. Knepley Level: developer 57777623264SMatthew G. Knepley 57877623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy() 57977623264SMatthew G. Knepley @*/ 58077623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v) 58177623264SMatthew G. Knepley { 582ffc59708SMatthew G. Knepley PetscMPIInt size; 5832abdaa70SMatthew G. Knepley PetscBool isascii; 58477623264SMatthew G. Knepley PetscErrorCode ierr; 58577623264SMatthew G. Knepley 58677623264SMatthew G. Knepley PetscFunctionBegin; 58777623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 58877623264SMatthew G. Knepley if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);} 5892abdaa70SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr); 5902abdaa70SMatthew G. Knepley if (isascii) { 5912abdaa70SMatthew G. Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr); 592ffc59708SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "Graph Partitioner: %d MPI Process%s\n", size, size > 1 ? "es" : "");CHKERRQ(ierr); 5932abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, " type: %s\n", part->hdr.type_name);CHKERRQ(ierr); 5942abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr); 5952abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "edge cut: %D\n", part->edgeCut);CHKERRQ(ierr); 5962abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "balance: %.2g\n", part->balance);CHKERRQ(ierr); 5972abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr); 5982abdaa70SMatthew G. Knepley } 59977623264SMatthew G. Knepley if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);} 60077623264SMatthew G. Knepley PetscFunctionReturn(0); 60177623264SMatthew G. Knepley } 60277623264SMatthew G. Knepley 603a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType) 604a0058e54SToby Isaac { 605a0058e54SToby Isaac PetscFunctionBegin; 606a0058e54SToby Isaac if (!currentType) { 607a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO) 608a0058e54SToby Isaac *defaultType = PETSCPARTITIONERCHACO; 609a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS) 610a0058e54SToby Isaac *defaultType = PETSCPARTITIONERPARMETIS; 611137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH) 612137cd93aSLisandro Dalcin *defaultType = PETSCPARTITIONERPTSCOTCH; 613a0058e54SToby Isaac #else 614a0058e54SToby Isaac *defaultType = PETSCPARTITIONERSIMPLE; 615a0058e54SToby Isaac #endif 616a0058e54SToby Isaac } else { 617a0058e54SToby Isaac *defaultType = currentType; 618a0058e54SToby Isaac } 619a0058e54SToby Isaac PetscFunctionReturn(0); 620a0058e54SToby Isaac } 621a0058e54SToby Isaac 62277623264SMatthew G. Knepley /*@ 62377623264SMatthew G. Knepley PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database 62477623264SMatthew G. Knepley 62577623264SMatthew G. Knepley Collective on PetscPartitioner 62677623264SMatthew G. Knepley 62777623264SMatthew G. Knepley Input Parameter: 62877623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for 62977623264SMatthew G. Knepley 63077623264SMatthew G. Knepley Level: developer 63177623264SMatthew G. Knepley 63277623264SMatthew G. Knepley .seealso: PetscPartitionerView() 63377623264SMatthew G. Knepley @*/ 63477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part) 63577623264SMatthew G. Knepley { 6366bb9daa8SLisandro Dalcin const char *defaultType; 6376bb9daa8SLisandro Dalcin char name[256]; 6386bb9daa8SLisandro Dalcin PetscBool flg; 63977623264SMatthew G. Knepley PetscErrorCode ierr; 64077623264SMatthew G. Knepley 64177623264SMatthew G. Knepley PetscFunctionBegin; 64277623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 6436bb9daa8SLisandro Dalcin ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 6446bb9daa8SLisandro Dalcin ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr); 64577623264SMatthew G. Knepley ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr); 6466bb9daa8SLisandro Dalcin ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, sizeof(name), &flg);CHKERRQ(ierr); 6476bb9daa8SLisandro Dalcin if (flg) { 6486bb9daa8SLisandro Dalcin ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr); 6496bb9daa8SLisandro Dalcin } else if (!((PetscObject) part)->type_name) { 6506bb9daa8SLisandro Dalcin ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr); 6516bb9daa8SLisandro Dalcin } 6526bb9daa8SLisandro Dalcin if (part->ops->setfromoptions) { 6536bb9daa8SLisandro Dalcin ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr); 6546bb9daa8SLisandro Dalcin } 655783e1fb6SStefano Zampini ierr = PetscViewerDestroy(&part->viewerGraph);CHKERRQ(ierr); 6560358368aSMatthew G. Knepley ierr = PetscOptionsGetViewer(((PetscObject) part)->comm, ((PetscObject) part)->options, ((PetscObject) part)->prefix, "-petscpartitioner_view_graph", &part->viewerGraph, &part->formatGraph, &part->viewGraph);CHKERRQ(ierr); 65777623264SMatthew G. Knepley /* process any options handlers added with PetscObjectAddOptionsHandler() */ 6580633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr); 65977623264SMatthew G. Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 66077623264SMatthew G. Knepley PetscFunctionReturn(0); 66177623264SMatthew G. Knepley } 66277623264SMatthew G. Knepley 66377623264SMatthew G. Knepley /*@C 66477623264SMatthew G. Knepley PetscPartitionerSetUp - Construct data structures for the PetscPartitioner 66577623264SMatthew G. Knepley 66677623264SMatthew G. Knepley Collective on PetscPartitioner 66777623264SMatthew G. Knepley 66877623264SMatthew G. Knepley Input Parameter: 66977623264SMatthew G. Knepley . part - the PetscPartitioner object to setup 67077623264SMatthew G. Knepley 67177623264SMatthew G. Knepley Level: developer 67277623264SMatthew G. Knepley 67377623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy() 67477623264SMatthew G. Knepley @*/ 67577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part) 67677623264SMatthew G. Knepley { 67777623264SMatthew G. Knepley PetscErrorCode ierr; 67877623264SMatthew G. Knepley 67977623264SMatthew G. Knepley PetscFunctionBegin; 68077623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 68177623264SMatthew G. Knepley if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);} 68277623264SMatthew G. Knepley PetscFunctionReturn(0); 68377623264SMatthew G. Knepley } 68477623264SMatthew G. Knepley 68577623264SMatthew G. Knepley /*@ 68677623264SMatthew G. Knepley PetscPartitionerDestroy - Destroys a PetscPartitioner object 68777623264SMatthew G. Knepley 68877623264SMatthew G. Knepley Collective on PetscPartitioner 68977623264SMatthew G. Knepley 69077623264SMatthew G. Knepley Input Parameter: 69177623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy 69277623264SMatthew G. Knepley 69377623264SMatthew G. Knepley Level: developer 69477623264SMatthew G. Knepley 69577623264SMatthew G. Knepley .seealso: PetscPartitionerView() 69677623264SMatthew G. Knepley @*/ 69777623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part) 69877623264SMatthew G. Knepley { 69977623264SMatthew G. Knepley PetscErrorCode ierr; 70077623264SMatthew G. Knepley 70177623264SMatthew G. Knepley PetscFunctionBegin; 70277623264SMatthew G. Knepley if (!*part) PetscFunctionReturn(0); 70377623264SMatthew G. Knepley PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1); 70477623264SMatthew G. Knepley 70577623264SMatthew G. Knepley if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);} 70677623264SMatthew G. Knepley ((PetscObject) (*part))->refct = 0; 70777623264SMatthew G. Knepley 7080358368aSMatthew G. Knepley ierr = PetscViewerDestroy(&(*part)->viewerGraph);CHKERRQ(ierr); 70977623264SMatthew G. Knepley if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);} 71077623264SMatthew G. Knepley ierr = PetscHeaderDestroy(part);CHKERRQ(ierr); 71177623264SMatthew G. Knepley PetscFunctionReturn(0); 71277623264SMatthew G. Knepley } 71377623264SMatthew G. Knepley 71477623264SMatthew G. Knepley /*@ 71577623264SMatthew G. Knepley PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType(). 71677623264SMatthew G. Knepley 71777623264SMatthew G. Knepley Collective on MPI_Comm 71877623264SMatthew G. Knepley 71977623264SMatthew G. Knepley Input Parameter: 72077623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object 72177623264SMatthew G. Knepley 72277623264SMatthew G. Knepley Output Parameter: 72377623264SMatthew G. Knepley . part - The PetscPartitioner object 72477623264SMatthew G. Knepley 72577623264SMatthew G. Knepley Level: beginner 72677623264SMatthew G. Knepley 727dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER 72877623264SMatthew G. Knepley @*/ 72977623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part) 73077623264SMatthew G. Knepley { 73177623264SMatthew G. Knepley PetscPartitioner p; 732a0058e54SToby Isaac const char *partitionerType = NULL; 73377623264SMatthew G. Knepley PetscErrorCode ierr; 73477623264SMatthew G. Knepley 73577623264SMatthew G. Knepley PetscFunctionBegin; 73677623264SMatthew G. Knepley PetscValidPointer(part, 2); 73777623264SMatthew G. Knepley *part = NULL; 73883cde681SMatthew G. Knepley ierr = DMInitializePackage();CHKERRQ(ierr); 73977623264SMatthew G. Knepley 74073107ff1SLisandro Dalcin ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr); 741a0058e54SToby Isaac ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr); 742a0058e54SToby Isaac ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr); 74377623264SMatthew G. Knepley 74472379da4SMatthew G. Knepley p->edgeCut = 0; 74572379da4SMatthew G. Knepley p->balance = 0.0; 74672379da4SMatthew G. Knepley 74777623264SMatthew G. Knepley *part = p; 74877623264SMatthew G. Knepley PetscFunctionReturn(0); 74977623264SMatthew G. Knepley } 75077623264SMatthew G. Knepley 75177623264SMatthew G. Knepley /*@ 75277623264SMatthew G. Knepley PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh 75377623264SMatthew G. Knepley 75477623264SMatthew G. Knepley Collective on DM 75577623264SMatthew G. Knepley 75677623264SMatthew G. Knepley Input Parameters: 75777623264SMatthew G. Knepley + part - The PetscPartitioner 758f8987ae8SMichael Lange - dm - The mesh DM 75977623264SMatthew G. Knepley 76077623264SMatthew G. Knepley Output Parameters: 76177623264SMatthew G. Knepley + partSection - The PetscSection giving the division of points by partition 762f8987ae8SMichael Lange - partition - The list of points by partition 76377623264SMatthew G. Knepley 7640358368aSMatthew G. Knepley Options Database: 7650358368aSMatthew G. Knepley . -petscpartitioner_view_graph - View the graph we are partitioning 7660358368aSMatthew G. Knepley 76777623264SMatthew G. Knepley Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight() 76877623264SMatthew G. Knepley 76977623264SMatthew G. Knepley Level: developer 77077623264SMatthew G. Knepley 77177623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate() 7724b15ede2SMatthew G. Knepley @*/ 773f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition) 77477623264SMatthew G. Knepley { 77577623264SMatthew G. Knepley PetscMPIInt size; 77677623264SMatthew G. Knepley PetscErrorCode ierr; 77777623264SMatthew G. Knepley 77877623264SMatthew G. Knepley PetscFunctionBegin; 77977623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 78077623264SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 78177623264SMatthew G. Knepley PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4); 78277623264SMatthew G. Knepley PetscValidPointer(partition, 5); 78377623264SMatthew G. Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr); 78477623264SMatthew G. Knepley if (size == 1) { 78577623264SMatthew G. Knepley PetscInt *points; 78677623264SMatthew G. Knepley PetscInt cStart, cEnd, c; 78777623264SMatthew G. Knepley 78877623264SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr); 78977623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr); 79077623264SMatthew G. Knepley ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr); 79177623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 79277623264SMatthew G. Knepley ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr); 79377623264SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) points[c] = c; 79477623264SMatthew G. Knepley ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 79577623264SMatthew G. Knepley PetscFunctionReturn(0); 79677623264SMatthew G. Knepley } 79777623264SMatthew G. Knepley if (part->height == 0) { 798*074d466cSStefano Zampini PetscInt numVertices = 0; 79977623264SMatthew G. Knepley PetscInt *start = NULL; 80077623264SMatthew G. Knepley PetscInt *adjacency = NULL; 8013fa7752dSToby Isaac IS globalNumbering; 80277623264SMatthew G. Knepley 803*074d466cSStefano Zampini if (!part->noGraph || part->viewGraph) { 804*074d466cSStefano Zampini ierr = DMPlexCreatePartitionerGraph(dm, part->height, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr); 805*074d466cSStefano Zampini } else { 806*074d466cSStefano Zampini const PetscInt *idxs; 807*074d466cSStefano Zampini PetscInt p, pStart, pEnd; 808*074d466cSStefano Zampini 809*074d466cSStefano Zampini ierr = DMPlexGetHeightStratum(dm, part->height, &pStart, &pEnd);CHKERRQ(ierr); 810*074d466cSStefano Zampini ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, 0, NULL, dm->sf, &globalNumbering);CHKERRQ(ierr); 811*074d466cSStefano Zampini ierr = ISGetIndices(globalNumbering, &idxs);CHKERRQ(ierr); 812*074d466cSStefano Zampini for (p = 0; p < pEnd - pStart; p++) numVertices += idxs[p] < 0 ? 0 : 1; 813*074d466cSStefano Zampini ierr = ISRestoreIndices(globalNumbering, &idxs);CHKERRQ(ierr); 814*074d466cSStefano Zampini } 8150358368aSMatthew G. Knepley if (part->viewGraph) { 8160358368aSMatthew G. Knepley PetscViewer viewer = part->viewerGraph; 8170358368aSMatthew G. Knepley PetscBool isascii; 8180358368aSMatthew G. Knepley PetscInt v, i; 8190358368aSMatthew G. Knepley PetscMPIInt rank; 8200358368aSMatthew G. Knepley 8210358368aSMatthew G. Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);CHKERRQ(ierr); 8220358368aSMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr); 8230358368aSMatthew G. Knepley if (isascii) { 8240358368aSMatthew G. Knepley ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 8250358368aSMatthew G. Knepley ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]Nv: %D\n", rank, numVertices);CHKERRQ(ierr); 8260358368aSMatthew G. Knepley for (v = 0; v < numVertices; ++v) { 8270358368aSMatthew G. Knepley const PetscInt s = start[v]; 8280358368aSMatthew G. Knepley const PetscInt e = start[v+1]; 8290358368aSMatthew G. Knepley 8300358368aSMatthew G. Knepley ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] ", rank);CHKERRQ(ierr); 8310358368aSMatthew G. Knepley for (i = s; i < e; ++i) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%D ", adjacency[i]);CHKERRQ(ierr);} 8320358368aSMatthew G. Knepley ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D-%D)\n", s, e);CHKERRQ(ierr); 8330358368aSMatthew G. Knepley } 8340358368aSMatthew G. Knepley ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 8350358368aSMatthew G. Knepley ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr); 8360358368aSMatthew G. Knepley } 8370358368aSMatthew G. Knepley } 83877623264SMatthew G. Knepley if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type"); 83977623264SMatthew G. Knepley ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr); 84077623264SMatthew G. Knepley ierr = PetscFree(start);CHKERRQ(ierr); 84177623264SMatthew G. Knepley ierr = PetscFree(adjacency);CHKERRQ(ierr); 8423fa7752dSToby Isaac if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */ 8433fa7752dSToby Isaac const PetscInt *globalNum; 8443fa7752dSToby Isaac const PetscInt *partIdx; 8453fa7752dSToby Isaac PetscInt *map, cStart, cEnd; 8463fa7752dSToby Isaac PetscInt *adjusted, i, localSize, offset; 8473fa7752dSToby Isaac IS newPartition; 8483fa7752dSToby Isaac 8493fa7752dSToby Isaac ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr); 8503fa7752dSToby Isaac ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr); 8513fa7752dSToby Isaac ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr); 8523fa7752dSToby Isaac ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr); 8533fa7752dSToby Isaac ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr); 8543fa7752dSToby Isaac ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr); 8553fa7752dSToby Isaac for (i = cStart, offset = 0; i < cEnd; i++) { 8563fa7752dSToby Isaac if (globalNum[i - cStart] >= 0) map[offset++] = i; 8573fa7752dSToby Isaac } 8583fa7752dSToby Isaac for (i = 0; i < localSize; i++) { 8593fa7752dSToby Isaac adjusted[i] = map[partIdx[i]]; 8603fa7752dSToby Isaac } 8613fa7752dSToby Isaac ierr = PetscFree(map);CHKERRQ(ierr); 8623fa7752dSToby Isaac ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr); 8633fa7752dSToby Isaac ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr); 8643fa7752dSToby Isaac ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr); 8653fa7752dSToby Isaac ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr); 8663fa7752dSToby Isaac ierr = ISDestroy(partition);CHKERRQ(ierr); 8673fa7752dSToby Isaac *partition = newPartition; 8683fa7752dSToby Isaac } 86977623264SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height); 8702abdaa70SMatthew G. Knepley ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr); 87177623264SMatthew G. Knepley PetscFunctionReturn(0); 87277623264SMatthew G. Knepley } 87377623264SMatthew G. Knepley 874d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part) 87577623264SMatthew G. Knepley { 87677623264SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 87777623264SMatthew G. Knepley PetscErrorCode ierr; 87877623264SMatthew G. Knepley 87977623264SMatthew G. Knepley PetscFunctionBegin; 88077623264SMatthew G. Knepley ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr); 88177623264SMatthew G. Knepley ierr = ISDestroy(&p->partition);CHKERRQ(ierr); 88277623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 88377623264SMatthew G. Knepley PetscFunctionReturn(0); 88477623264SMatthew G. Knepley } 88577623264SMatthew G. Knepley 886d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer) 88777623264SMatthew G. Knepley { 888077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 88977623264SMatthew G. Knepley PetscErrorCode ierr; 89077623264SMatthew G. Knepley 89177623264SMatthew G. Knepley PetscFunctionBegin; 892077101c0SMatthew G. Knepley if (p->random) { 893077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 894077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr); 895077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 896077101c0SMatthew G. Knepley } 89777623264SMatthew G. Knepley PetscFunctionReturn(0); 89877623264SMatthew G. Knepley } 89977623264SMatthew G. Knepley 900d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer) 90177623264SMatthew G. Knepley { 90277623264SMatthew G. Knepley PetscBool iascii; 90377623264SMatthew G. Knepley PetscErrorCode ierr; 90477623264SMatthew G. Knepley 90577623264SMatthew G. Knepley PetscFunctionBegin; 90677623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 90777623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 90877623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 90977623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);} 91077623264SMatthew G. Knepley PetscFunctionReturn(0); 91177623264SMatthew G. Knepley } 91277623264SMatthew G. Knepley 913d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 914077101c0SMatthew G. Knepley { 915077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 916077101c0SMatthew G. Knepley PetscErrorCode ierr; 917077101c0SMatthew G. Knepley 918077101c0SMatthew G. Knepley PetscFunctionBegin; 919077101c0SMatthew G. Knepley ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");CHKERRQ(ierr); 920077101c0SMatthew G. Knepley ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr); 921077101c0SMatthew G. Knepley ierr = PetscOptionsTail();CHKERRQ(ierr); 922077101c0SMatthew G. Knepley PetscFunctionReturn(0); 923077101c0SMatthew G. Knepley } 924077101c0SMatthew G. Knepley 925d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 92677623264SMatthew G. Knepley { 92777623264SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 92877623264SMatthew G. Knepley PetscInt np; 92977623264SMatthew G. Knepley PetscErrorCode ierr; 93077623264SMatthew G. Knepley 93177623264SMatthew G. Knepley PetscFunctionBegin; 932077101c0SMatthew G. Knepley if (p->random) { 933077101c0SMatthew G. Knepley PetscRandom r; 934aa1d5631SMatthew G. Knepley PetscInt *sizes, *points, v, p; 935aa1d5631SMatthew G. Knepley PetscMPIInt rank; 936077101c0SMatthew G. Knepley 937aa1d5631SMatthew G. Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 938077101c0SMatthew G. Knepley ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 939c717d290SMatthew G. Knepley ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr); 940077101c0SMatthew G. Knepley ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr); 941077101c0SMatthew G. Knepley ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr); 942aa1d5631SMatthew G. Knepley for (v = 0; v < numVertices; ++v) {points[v] = v;} 943ac9a96f1SMichael Lange for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);} 944aa1d5631SMatthew G. Knepley for (v = numVertices-1; v > 0; --v) { 945077101c0SMatthew G. Knepley PetscReal val; 946aa1d5631SMatthew G. Knepley PetscInt w, tmp; 947077101c0SMatthew G. Knepley 948aa1d5631SMatthew G. Knepley ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr); 949077101c0SMatthew G. Knepley ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr); 950aa1d5631SMatthew G. Knepley w = PetscFloorReal(val); 951aa1d5631SMatthew G. Knepley tmp = points[v]; 952aa1d5631SMatthew G. Knepley points[v] = points[w]; 953aa1d5631SMatthew G. Knepley points[w] = tmp; 954077101c0SMatthew G. Knepley } 955077101c0SMatthew G. Knepley ierr = PetscRandomDestroy(&r);CHKERRQ(ierr); 956077101c0SMatthew G. Knepley ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr); 957077101c0SMatthew G. Knepley ierr = PetscFree2(sizes, points);CHKERRQ(ierr); 958077101c0SMatthew G. Knepley } 959c717d290SMatthew G. Knepley if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()"); 96077623264SMatthew G. Knepley ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr); 96177623264SMatthew G. Knepley if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np); 96277623264SMatthew G. Knepley ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr); 96377623264SMatthew G. Knepley if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np); 9645680f57bSMatthew G. Knepley ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr); 96577623264SMatthew G. Knepley *partition = p->partition; 96677623264SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr); 96777623264SMatthew G. Knepley PetscFunctionReturn(0); 96877623264SMatthew G. Knepley } 96977623264SMatthew G. Knepley 970d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part) 97177623264SMatthew G. Knepley { 97277623264SMatthew G. Knepley PetscFunctionBegin; 973*074d466cSStefano Zampini part->noGraph = PETSC_TRUE; /* PetscPartitionerShell cannot overload the partition call, so it is safe for now */ 97477623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_Shell; 975077101c0SMatthew G. Knepley part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell; 97677623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Shell; 97777623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Shell; 97877623264SMatthew G. Knepley PetscFunctionReturn(0); 97977623264SMatthew G. Knepley } 98077623264SMatthew G. Knepley 98177623264SMatthew G. Knepley /*MC 98277623264SMatthew G. Knepley PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object 98377623264SMatthew G. Knepley 98477623264SMatthew G. Knepley Level: intermediate 98577623264SMatthew G. Knepley 98677623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 98777623264SMatthew G. Knepley M*/ 98877623264SMatthew G. Knepley 98977623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part) 99077623264SMatthew G. Knepley { 99177623264SMatthew G. Knepley PetscPartitioner_Shell *p; 99277623264SMatthew G. Knepley PetscErrorCode ierr; 99377623264SMatthew G. Knepley 99477623264SMatthew G. Knepley PetscFunctionBegin; 99577623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 99677623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 99777623264SMatthew G. Knepley part->data = p; 99877623264SMatthew G. Knepley 99977623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr); 1000077101c0SMatthew G. Knepley p->random = PETSC_FALSE; 100177623264SMatthew G. Knepley PetscFunctionReturn(0); 100277623264SMatthew G. Knepley } 100377623264SMatthew G. Knepley 10045680f57bSMatthew G. Knepley /*@C 10055680f57bSMatthew G. Knepley PetscPartitionerShellSetPartition - Set an artifical partition for a mesh 10065680f57bSMatthew G. Knepley 1007077101c0SMatthew G. Knepley Collective on Part 10085680f57bSMatthew G. Knepley 10095680f57bSMatthew G. Knepley Input Parameters: 10105680f57bSMatthew G. Knepley + part - The PetscPartitioner 10119852e123SBarry Smith . size - The number of partitions 10129852e123SBarry Smith . sizes - array of size size (or NULL) providing the number of points in each partition 10139758bf69SToby Isaac - points - array of size sum(sizes) (may be NULL iff sizes is NULL), a permutation of the points that groups those assigned to each partition in order (i.e., partition 0 first, partition 1 next, etc.) 10145680f57bSMatthew G. Knepley 10155680f57bSMatthew G. Knepley Level: developer 10165680f57bSMatthew G. Knepley 1017b7e49471SLawrence Mitchell Notes: 1018b7e49471SLawrence Mitchell 1019b7e49471SLawrence Mitchell It is safe to free the sizes and points arrays after use in this routine. 1020b7e49471SLawrence Mitchell 10215680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate() 10225680f57bSMatthew G. Knepley @*/ 10239852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[]) 10245680f57bSMatthew G. Knepley { 10255680f57bSMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 10265680f57bSMatthew G. Knepley PetscInt proc, numPoints; 10275680f57bSMatthew G. Knepley PetscErrorCode ierr; 10285680f57bSMatthew G. Knepley 10295680f57bSMatthew G. Knepley PetscFunctionBegin; 10305680f57bSMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 10315680f57bSMatthew G. Knepley if (sizes) {PetscValidPointer(sizes, 3);} 1032c717d290SMatthew G. Knepley if (points) {PetscValidPointer(points, 4);} 10335680f57bSMatthew G. Knepley ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr); 10345680f57bSMatthew G. Knepley ierr = ISDestroy(&p->partition);CHKERRQ(ierr); 10355680f57bSMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr); 10369852e123SBarry Smith ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr); 10375680f57bSMatthew G. Knepley if (sizes) { 10389852e123SBarry Smith for (proc = 0; proc < size; ++proc) { 10395680f57bSMatthew G. Knepley ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr); 10405680f57bSMatthew G. Knepley } 10415680f57bSMatthew G. Knepley } 10425680f57bSMatthew G. Knepley ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr); 10435680f57bSMatthew G. Knepley ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr); 10445680f57bSMatthew G. Knepley ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr); 10455680f57bSMatthew G. Knepley PetscFunctionReturn(0); 10465680f57bSMatthew G. Knepley } 10475680f57bSMatthew G. Knepley 1048077101c0SMatthew G. Knepley /*@ 1049077101c0SMatthew G. Knepley PetscPartitionerShellSetRandom - Set the flag to use a random partition 1050077101c0SMatthew G. Knepley 1051077101c0SMatthew G. Knepley Collective on Part 1052077101c0SMatthew G. Knepley 1053077101c0SMatthew G. Knepley Input Parameters: 1054077101c0SMatthew G. Knepley + part - The PetscPartitioner 1055077101c0SMatthew G. Knepley - random - The flag to use a random partition 1056077101c0SMatthew G. Knepley 1057077101c0SMatthew G. Knepley Level: intermediate 1058077101c0SMatthew G. Knepley 1059077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate() 1060077101c0SMatthew G. Knepley @*/ 1061077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random) 1062077101c0SMatthew G. Knepley { 1063077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 1064077101c0SMatthew G. Knepley 1065077101c0SMatthew G. Knepley PetscFunctionBegin; 1066077101c0SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1067077101c0SMatthew G. Knepley p->random = random; 1068077101c0SMatthew G. Knepley PetscFunctionReturn(0); 1069077101c0SMatthew G. Knepley } 1070077101c0SMatthew G. Knepley 1071077101c0SMatthew G. Knepley /*@ 1072077101c0SMatthew G. Knepley PetscPartitionerShellGetRandom - get the flag to use a random partition 1073077101c0SMatthew G. Knepley 1074077101c0SMatthew G. Knepley Collective on Part 1075077101c0SMatthew G. Knepley 1076077101c0SMatthew G. Knepley Input Parameter: 1077077101c0SMatthew G. Knepley . part - The PetscPartitioner 1078077101c0SMatthew G. Knepley 1079077101c0SMatthew G. Knepley Output Parameter 1080077101c0SMatthew G. Knepley . random - The flag to use a random partition 1081077101c0SMatthew G. Knepley 1082077101c0SMatthew G. Knepley Level: intermediate 1083077101c0SMatthew G. Knepley 1084077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate() 1085077101c0SMatthew G. Knepley @*/ 1086077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random) 1087077101c0SMatthew G. Knepley { 1088077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 1089077101c0SMatthew G. Knepley 1090077101c0SMatthew G. Knepley PetscFunctionBegin; 1091077101c0SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1092077101c0SMatthew G. Knepley PetscValidPointer(random, 2); 1093077101c0SMatthew G. Knepley *random = p->random; 1094077101c0SMatthew G. Knepley PetscFunctionReturn(0); 1095077101c0SMatthew G. Knepley } 1096077101c0SMatthew G. Knepley 1097d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part) 1098555a9cf8SMatthew G. Knepley { 1099555a9cf8SMatthew G. Knepley PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data; 1100555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 1101555a9cf8SMatthew G. Knepley 1102555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1103555a9cf8SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 1104555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1105555a9cf8SMatthew G. Knepley } 1106555a9cf8SMatthew G. Knepley 1107d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer) 1108555a9cf8SMatthew G. Knepley { 1109555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1110555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1111555a9cf8SMatthew G. Knepley } 1112555a9cf8SMatthew G. Knepley 1113d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer) 1114555a9cf8SMatthew G. Knepley { 1115555a9cf8SMatthew G. Knepley PetscBool iascii; 1116555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 1117555a9cf8SMatthew G. Knepley 1118555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1119555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1120555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1121555a9cf8SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 1122555a9cf8SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);} 1123555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1124555a9cf8SMatthew G. Knepley } 1125555a9cf8SMatthew G. Knepley 1126d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 1127555a9cf8SMatthew G. Knepley { 1128cead94edSToby Isaac MPI_Comm comm; 1129555a9cf8SMatthew G. Knepley PetscInt np; 1130cead94edSToby Isaac PetscMPIInt size; 1131555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 1132555a9cf8SMatthew G. Knepley 1133555a9cf8SMatthew G. Knepley PetscFunctionBegin; 113404ba2274SStefano Zampini comm = PetscObjectComm((PetscObject)part); 1135cead94edSToby Isaac ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1136555a9cf8SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 1137555a9cf8SMatthew G. Knepley ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr); 1138cead94edSToby Isaac if (size == 1) { 1139cead94edSToby Isaac for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);} 114004ba2274SStefano Zampini } else { 1141cead94edSToby Isaac PetscMPIInt rank; 1142cead94edSToby Isaac PetscInt nvGlobal, *offsets, myFirst, myLast; 1143cead94edSToby Isaac 1144a679a563SToby Isaac ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr); 1145cead94edSToby Isaac offsets[0] = 0; 1146cead94edSToby Isaac ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr); 1147cead94edSToby Isaac for (np = 2; np <= size; np++) { 1148cead94edSToby Isaac offsets[np] += offsets[np-1]; 1149cead94edSToby Isaac } 1150cead94edSToby Isaac nvGlobal = offsets[size]; 1151cead94edSToby Isaac ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1152cead94edSToby Isaac myFirst = offsets[rank]; 1153cead94edSToby Isaac myLast = offsets[rank + 1] - 1; 1154cead94edSToby Isaac ierr = PetscFree(offsets);CHKERRQ(ierr); 1155cead94edSToby Isaac if (numVertices) { 1156cead94edSToby Isaac PetscInt firstPart = 0, firstLargePart = 0; 1157cead94edSToby Isaac PetscInt lastPart = 0, lastLargePart = 0; 1158cead94edSToby Isaac PetscInt rem = nvGlobal % nparts; 1159cead94edSToby Isaac PetscInt pSmall = nvGlobal/nparts; 1160cead94edSToby Isaac PetscInt pBig = nvGlobal/nparts + 1; 1161cead94edSToby Isaac 1162cead94edSToby Isaac 1163cead94edSToby Isaac if (rem) { 1164cead94edSToby Isaac firstLargePart = myFirst / pBig; 1165cead94edSToby Isaac lastLargePart = myLast / pBig; 1166cead94edSToby Isaac 1167cead94edSToby Isaac if (firstLargePart < rem) { 1168cead94edSToby Isaac firstPart = firstLargePart; 116904ba2274SStefano Zampini } else { 1170cead94edSToby Isaac firstPart = rem + (myFirst - (rem * pBig)) / pSmall; 1171cead94edSToby Isaac } 1172cead94edSToby Isaac if (lastLargePart < rem) { 1173cead94edSToby Isaac lastPart = lastLargePart; 117404ba2274SStefano Zampini } else { 1175cead94edSToby Isaac lastPart = rem + (myLast - (rem * pBig)) / pSmall; 1176cead94edSToby Isaac } 117704ba2274SStefano Zampini } else { 1178cead94edSToby Isaac firstPart = myFirst / (nvGlobal/nparts); 1179cead94edSToby Isaac lastPart = myLast / (nvGlobal/nparts); 1180cead94edSToby Isaac } 1181cead94edSToby Isaac 1182cead94edSToby Isaac for (np = firstPart; np <= lastPart; np++) { 1183cead94edSToby Isaac PetscInt PartStart = np * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np); 1184cead94edSToby Isaac PetscInt PartEnd = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1); 1185cead94edSToby Isaac 1186cead94edSToby Isaac PartStart = PetscMax(PartStart,myFirst); 1187cead94edSToby Isaac PartEnd = PetscMin(PartEnd,myLast+1); 1188cead94edSToby Isaac ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr); 1189cead94edSToby Isaac } 1190cead94edSToby Isaac } 1191cead94edSToby Isaac } 1192cead94edSToby Isaac ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1193555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1194555a9cf8SMatthew G. Knepley } 1195555a9cf8SMatthew G. Knepley 1196d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part) 1197555a9cf8SMatthew G. Knepley { 1198555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1199*074d466cSStefano Zampini part->noGraph = PETSC_TRUE; 1200555a9cf8SMatthew G. Knepley part->ops->view = PetscPartitionerView_Simple; 1201555a9cf8SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Simple; 1202555a9cf8SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Simple; 1203555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1204555a9cf8SMatthew G. Knepley } 1205555a9cf8SMatthew G. Knepley 1206555a9cf8SMatthew G. Knepley /*MC 1207555a9cf8SMatthew G. Knepley PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object 1208555a9cf8SMatthew G. Knepley 1209555a9cf8SMatthew G. Knepley Level: intermediate 1210555a9cf8SMatthew G. Knepley 1211555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 1212555a9cf8SMatthew G. Knepley M*/ 1213555a9cf8SMatthew G. Knepley 1214555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part) 1215555a9cf8SMatthew G. Knepley { 1216555a9cf8SMatthew G. Knepley PetscPartitioner_Simple *p; 1217555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 1218555a9cf8SMatthew G. Knepley 1219555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1220555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1221555a9cf8SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 1222555a9cf8SMatthew G. Knepley part->data = p; 1223555a9cf8SMatthew G. Knepley 1224555a9cf8SMatthew G. Knepley ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr); 1225555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1226555a9cf8SMatthew G. Knepley } 1227555a9cf8SMatthew G. Knepley 1228d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part) 1229dae52e14SToby Isaac { 1230dae52e14SToby Isaac PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data; 1231dae52e14SToby Isaac PetscErrorCode ierr; 1232dae52e14SToby Isaac 1233dae52e14SToby Isaac PetscFunctionBegin; 1234dae52e14SToby Isaac ierr = PetscFree(p);CHKERRQ(ierr); 1235dae52e14SToby Isaac PetscFunctionReturn(0); 1236dae52e14SToby Isaac } 1237dae52e14SToby Isaac 1238d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer) 1239dae52e14SToby Isaac { 1240dae52e14SToby Isaac PetscFunctionBegin; 1241dae52e14SToby Isaac PetscFunctionReturn(0); 1242dae52e14SToby Isaac } 1243dae52e14SToby Isaac 1244d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer) 1245dae52e14SToby Isaac { 1246dae52e14SToby Isaac PetscBool iascii; 1247dae52e14SToby Isaac PetscErrorCode ierr; 1248dae52e14SToby Isaac 1249dae52e14SToby Isaac PetscFunctionBegin; 1250dae52e14SToby Isaac PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1251dae52e14SToby Isaac PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1252dae52e14SToby Isaac ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 1253dae52e14SToby Isaac if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);} 1254dae52e14SToby Isaac PetscFunctionReturn(0); 1255dae52e14SToby Isaac } 1256dae52e14SToby Isaac 1257d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 1258dae52e14SToby Isaac { 1259dae52e14SToby Isaac PetscInt np; 1260dae52e14SToby Isaac PetscErrorCode ierr; 1261dae52e14SToby Isaac 1262dae52e14SToby Isaac PetscFunctionBegin; 1263dae52e14SToby Isaac ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 1264dae52e14SToby Isaac ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr); 1265dae52e14SToby Isaac ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr); 1266dae52e14SToby Isaac for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);} 1267dae52e14SToby Isaac ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1268dae52e14SToby Isaac PetscFunctionReturn(0); 1269dae52e14SToby Isaac } 1270dae52e14SToby Isaac 1271d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part) 1272dae52e14SToby Isaac { 1273dae52e14SToby Isaac PetscFunctionBegin; 1274*074d466cSStefano Zampini part->noGraph = PETSC_TRUE; 1275dae52e14SToby Isaac part->ops->view = PetscPartitionerView_Gather; 1276dae52e14SToby Isaac part->ops->destroy = PetscPartitionerDestroy_Gather; 1277dae52e14SToby Isaac part->ops->partition = PetscPartitionerPartition_Gather; 1278dae52e14SToby Isaac PetscFunctionReturn(0); 1279dae52e14SToby Isaac } 1280dae52e14SToby Isaac 1281dae52e14SToby Isaac /*MC 1282dae52e14SToby Isaac PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object 1283dae52e14SToby Isaac 1284dae52e14SToby Isaac Level: intermediate 1285dae52e14SToby Isaac 1286dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 1287dae52e14SToby Isaac M*/ 1288dae52e14SToby Isaac 1289dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part) 1290dae52e14SToby Isaac { 1291dae52e14SToby Isaac PetscPartitioner_Gather *p; 1292dae52e14SToby Isaac PetscErrorCode ierr; 1293dae52e14SToby Isaac 1294dae52e14SToby Isaac PetscFunctionBegin; 1295dae52e14SToby Isaac PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1296dae52e14SToby Isaac ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 1297dae52e14SToby Isaac part->data = p; 1298dae52e14SToby Isaac 1299dae52e14SToby Isaac ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr); 1300dae52e14SToby Isaac PetscFunctionReturn(0); 1301dae52e14SToby Isaac } 1302dae52e14SToby Isaac 1303dae52e14SToby Isaac 1304d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part) 130577623264SMatthew G. Knepley { 130677623264SMatthew G. Knepley PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data; 130777623264SMatthew G. Knepley PetscErrorCode ierr; 130877623264SMatthew G. Knepley 130977623264SMatthew G. Knepley PetscFunctionBegin; 131077623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 131177623264SMatthew G. Knepley PetscFunctionReturn(0); 131277623264SMatthew G. Knepley } 131377623264SMatthew G. Knepley 1314d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer) 131577623264SMatthew G. Knepley { 131677623264SMatthew G. Knepley PetscFunctionBegin; 131777623264SMatthew G. Knepley PetscFunctionReturn(0); 131877623264SMatthew G. Knepley } 131977623264SMatthew G. Knepley 1320d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer) 132177623264SMatthew G. Knepley { 132277623264SMatthew G. Knepley PetscBool iascii; 132377623264SMatthew G. Knepley PetscErrorCode ierr; 132477623264SMatthew G. Knepley 132577623264SMatthew G. Knepley PetscFunctionBegin; 132677623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 132777623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 132877623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 132977623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);} 133077623264SMatthew G. Knepley PetscFunctionReturn(0); 133177623264SMatthew G. Knepley } 133277623264SMatthew G. Knepley 133370034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO) 133470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 133570034214SMatthew G. Knepley #include <unistd.h> 133670034214SMatthew G. Knepley #endif 133711d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT) 133811d1e910SBarry Smith #include <chaco.h> 133911d1e910SBarry Smith #else 134011d1e910SBarry Smith /* Older versions of Chaco do not have an include file */ 134170034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts, 134270034214SMatthew G. Knepley float *ewgts, float *x, float *y, float *z, char *outassignname, 134370034214SMatthew G. Knepley char *outfilename, short *assignment, int architecture, int ndims_tot, 134470034214SMatthew G. Knepley int mesh_dims[3], double *goal, int global_method, int local_method, 134570034214SMatthew G. Knepley int rqi_flag, int vmax, int ndims, double eigtol, long seed); 134611d1e910SBarry Smith #endif 134770034214SMatthew G. Knepley extern int FREE_GRAPH; 134877623264SMatthew G. Knepley #endif 134970034214SMatthew G. Knepley 1350d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 135170034214SMatthew G. Knepley { 135277623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO) 135370034214SMatthew G. Knepley enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3}; 135470034214SMatthew G. Knepley MPI_Comm comm; 135570034214SMatthew G. Knepley int nvtxs = numVertices; /* number of vertices in full graph */ 135670034214SMatthew G. Knepley int *vwgts = NULL; /* weights for all vertices */ 135770034214SMatthew G. Knepley float *ewgts = NULL; /* weights for all edges */ 135870034214SMatthew G. Knepley float *x = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */ 135970034214SMatthew G. Knepley char *outassignname = NULL; /* name of assignment output file */ 136070034214SMatthew G. Knepley char *outfilename = NULL; /* output file name */ 136170034214SMatthew G. Knepley int architecture = 1; /* 0 => hypercube, d => d-dimensional mesh */ 136270034214SMatthew G. Knepley int ndims_tot = 0; /* total number of cube dimensions to divide */ 136370034214SMatthew G. Knepley int mesh_dims[3]; /* dimensions of mesh of processors */ 136470034214SMatthew G. Knepley double *goal = NULL; /* desired set sizes for each set */ 136570034214SMatthew G. Knepley int global_method = 1; /* global partitioning algorithm */ 136670034214SMatthew G. Knepley int local_method = 1; /* local partitioning algorithm */ 136770034214SMatthew G. Knepley int rqi_flag = 0; /* should I use RQI/Symmlq eigensolver? */ 136870034214SMatthew G. Knepley int vmax = 200; /* how many vertices to coarsen down to? */ 136970034214SMatthew G. Knepley int ndims = 1; /* number of eigenvectors (2^d sets) */ 137070034214SMatthew G. Knepley double eigtol = 0.001; /* tolerance on eigenvectors */ 137170034214SMatthew G. Knepley long seed = 123636512; /* for random graph mutations */ 137211d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT) 137311d1e910SBarry Smith int *assignment; /* Output partition */ 137411d1e910SBarry Smith #else 137570034214SMatthew G. Knepley short int *assignment; /* Output partition */ 137611d1e910SBarry Smith #endif 137770034214SMatthew G. Knepley int fd_stdout, fd_pipe[2]; 137870034214SMatthew G. Knepley PetscInt *points; 137970034214SMatthew G. Knepley int i, v, p; 138070034214SMatthew G. Knepley PetscErrorCode ierr; 138170034214SMatthew G. Knepley 138270034214SMatthew G. Knepley PetscFunctionBegin; 138370034214SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 138407ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG) 138507ed3857SLisandro Dalcin { 138607ed3857SLisandro Dalcin int ival,isum; 138707ed3857SLisandro Dalcin PetscBool distributed; 138807ed3857SLisandro Dalcin 138907ed3857SLisandro Dalcin ival = (numVertices > 0); 139007ed3857SLisandro Dalcin ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr); 139107ed3857SLisandro Dalcin distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE; 139207ed3857SLisandro Dalcin if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph"); 139307ed3857SLisandro Dalcin } 139407ed3857SLisandro Dalcin #endif 139570034214SMatthew G. Knepley if (!numVertices) { 139677623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 139777623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 139870034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 139970034214SMatthew G. Knepley PetscFunctionReturn(0); 140070034214SMatthew G. Knepley } 140170034214SMatthew G. Knepley FREE_GRAPH = 0; /* Do not let Chaco free my memory */ 140270034214SMatthew G. Knepley for (i = 0; i < start[numVertices]; ++i) ++adjacency[i]; 140370034214SMatthew G. Knepley 140470034214SMatthew G. Knepley if (global_method == INERTIAL_METHOD) { 140570034214SMatthew G. Knepley /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */ 140670034214SMatthew G. Knepley SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported"); 140770034214SMatthew G. Knepley } 140877623264SMatthew G. Knepley mesh_dims[0] = nparts; 140970034214SMatthew G. Knepley mesh_dims[1] = 1; 141070034214SMatthew G. Knepley mesh_dims[2] = 1; 141170034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr); 141270034214SMatthew G. Knepley /* Chaco outputs to stdout. We redirect this to a buffer. */ 141370034214SMatthew G. Knepley /* TODO: check error codes for UNIX calls */ 141470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 141570034214SMatthew G. Knepley { 141670034214SMatthew G. Knepley int piperet; 141770034214SMatthew G. Knepley piperet = pipe(fd_pipe); 141870034214SMatthew G. Knepley if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe"); 141970034214SMatthew G. Knepley fd_stdout = dup(1); 142070034214SMatthew G. Knepley close(1); 142170034214SMatthew G. Knepley dup2(fd_pipe[1], 1); 142270034214SMatthew G. Knepley } 142370034214SMatthew G. Knepley #endif 142470034214SMatthew G. Knepley ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename, 142570034214SMatthew G. Knepley assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag, 142670034214SMatthew G. Knepley vmax, ndims, eigtol, seed); 142770034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 142870034214SMatthew G. Knepley { 142970034214SMatthew G. Knepley char msgLog[10000]; 143070034214SMatthew G. Knepley int count; 143170034214SMatthew G. Knepley 143270034214SMatthew G. Knepley fflush(stdout); 143370034214SMatthew G. Knepley count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char)); 143470034214SMatthew G. Knepley if (count < 0) count = 0; 143570034214SMatthew G. Knepley msgLog[count] = 0; 143670034214SMatthew G. Knepley close(1); 143770034214SMatthew G. Knepley dup2(fd_stdout, 1); 143870034214SMatthew G. Knepley close(fd_stdout); 143970034214SMatthew G. Knepley close(fd_pipe[0]); 144070034214SMatthew G. Knepley close(fd_pipe[1]); 144170034214SMatthew G. Knepley if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog); 144270034214SMatthew G. Knepley } 144307ed3857SLisandro Dalcin #else 144407ed3857SLisandro Dalcin if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout"); 144570034214SMatthew G. Knepley #endif 144670034214SMatthew G. Knepley /* Convert to PetscSection+IS */ 144777623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 144870034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 144977623264SMatthew G. Knepley ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr); 145070034214SMatthew G. Knepley } 145177623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 145270034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 145377623264SMatthew G. Knepley for (p = 0, i = 0; p < nparts; ++p) { 145470034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 145570034214SMatthew G. Knepley if (assignment[v] == p) points[i++] = v; 145670034214SMatthew G. Knepley } 145770034214SMatthew G. Knepley } 145870034214SMatthew G. Knepley if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 145970034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 146070034214SMatthew G. Knepley if (global_method == INERTIAL_METHOD) { 146170034214SMatthew G. Knepley /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */ 146270034214SMatthew G. Knepley } 146370034214SMatthew G. Knepley ierr = PetscFree(assignment);CHKERRQ(ierr); 146470034214SMatthew G. Knepley for (i = 0; i < start[numVertices]; ++i) --adjacency[i]; 146570034214SMatthew G. Knepley PetscFunctionReturn(0); 146677623264SMatthew G. Knepley #else 146777623264SMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco."); 146870034214SMatthew G. Knepley #endif 146977623264SMatthew G. Knepley } 147077623264SMatthew G. Knepley 1471d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part) 147277623264SMatthew G. Knepley { 147377623264SMatthew G. Knepley PetscFunctionBegin; 1474*074d466cSStefano Zampini part->noGraph = PETSC_FALSE; 147577623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_Chaco; 147677623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Chaco; 147777623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Chaco; 147877623264SMatthew G. Knepley PetscFunctionReturn(0); 147977623264SMatthew G. Knepley } 148077623264SMatthew G. Knepley 148177623264SMatthew G. Knepley /*MC 148277623264SMatthew G. Knepley PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library 148377623264SMatthew G. Knepley 148477623264SMatthew G. Knepley Level: intermediate 148577623264SMatthew G. Knepley 148677623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 148777623264SMatthew G. Knepley M*/ 148877623264SMatthew G. Knepley 148977623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part) 149077623264SMatthew G. Knepley { 149177623264SMatthew G. Knepley PetscPartitioner_Chaco *p; 149277623264SMatthew G. Knepley PetscErrorCode ierr; 149377623264SMatthew G. Knepley 149477623264SMatthew G. Knepley PetscFunctionBegin; 149577623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 149677623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 149777623264SMatthew G. Knepley part->data = p; 149877623264SMatthew G. Knepley 149977623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr); 150077623264SMatthew G. Knepley ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr); 150177623264SMatthew G. Knepley PetscFunctionReturn(0); 150277623264SMatthew G. Knepley } 150377623264SMatthew G. Knepley 15045b440754SMatthew G. Knepley static const char *ptypes[] = {"kway", "rb"}; 15055b440754SMatthew G. Knepley 1506d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part) 150777623264SMatthew G. Knepley { 150877623264SMatthew G. Knepley PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data; 150977623264SMatthew G. Knepley PetscErrorCode ierr; 151077623264SMatthew G. Knepley 151177623264SMatthew G. Knepley PetscFunctionBegin; 151277623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 151377623264SMatthew G. Knepley PetscFunctionReturn(0); 151477623264SMatthew G. Knepley } 151577623264SMatthew G. Knepley 1516d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer) 151777623264SMatthew G. Knepley { 15182abdaa70SMatthew G. Knepley PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data; 151977623264SMatthew G. Knepley PetscErrorCode ierr; 152077623264SMatthew G. Knepley 152177623264SMatthew G. Knepley PetscFunctionBegin; 15222abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 15232abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "ParMetis type: %s\n", ptypes[p->ptype]);CHKERRQ(ierr); 15242abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "load imbalance ratio %g\n", (double) p->imbalanceRatio);CHKERRQ(ierr); 15252abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "debug flag %D\n", p->debugFlag);CHKERRQ(ierr); 15262abdaa70SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 152777623264SMatthew G. Knepley PetscFunctionReturn(0); 152877623264SMatthew G. Knepley } 152977623264SMatthew G. Knepley 1530d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer) 153177623264SMatthew G. Knepley { 153277623264SMatthew G. Knepley PetscBool iascii; 153377623264SMatthew G. Knepley PetscErrorCode ierr; 153477623264SMatthew G. Knepley 153577623264SMatthew G. Knepley PetscFunctionBegin; 153677623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 153777623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 153877623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 153977623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);} 154077623264SMatthew G. Knepley PetscFunctionReturn(0); 154177623264SMatthew G. Knepley } 154270034214SMatthew G. Knepley 154344d8be81SLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_ParMetis(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 154444d8be81SLisandro Dalcin { 154544d8be81SLisandro Dalcin PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data; 154642678178SLisandro Dalcin PetscInt p_randomSeed = -1; /* TODO: Add this to PetscPartitioner_ParMetis */ 154744d8be81SLisandro Dalcin PetscErrorCode ierr; 154844d8be81SLisandro Dalcin 154944d8be81SLisandro Dalcin PetscFunctionBegin; 155044d8be81SLisandro Dalcin ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner ParMetis Options");CHKERRQ(ierr); 155144d8be81SLisandro Dalcin ierr = PetscOptionsEList("-petscpartitioner_parmetis_type", "Partitioning method", "", ptypes, 2, ptypes[p->ptype], &p->ptype, NULL);CHKERRQ(ierr); 15525b440754SMatthew G. Knepley ierr = PetscOptionsReal("-petscpartitioner_parmetis_imbalance_ratio", "Load imbalance ratio limit", "", p->imbalanceRatio, &p->imbalanceRatio, NULL);CHKERRQ(ierr); 15535b440754SMatthew G. Knepley ierr = PetscOptionsInt("-petscpartitioner_parmetis_debug", "Debugging flag", "", p->debugFlag, &p->debugFlag, NULL);CHKERRQ(ierr); 155442678178SLisandro Dalcin ierr = PetscOptionsInt("-petscpartitioner_parmetis_seed", "Random seed", "", p_randomSeed, &p_randomSeed, NULL);CHKERRQ(ierr); 155544d8be81SLisandro Dalcin ierr = PetscOptionsTail();CHKERRQ(ierr); 155644d8be81SLisandro Dalcin PetscFunctionReturn(0); 155744d8be81SLisandro Dalcin } 155844d8be81SLisandro Dalcin 155970034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS) 156070034214SMatthew G. Knepley #include <parmetis.h> 156177623264SMatthew G. Knepley #endif 156270034214SMatthew G. Knepley 1563d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 156470034214SMatthew G. Knepley { 156577623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS) 15665b440754SMatthew G. Knepley PetscPartitioner_ParMetis *pm = (PetscPartitioner_ParMetis *) part->data; 156770034214SMatthew G. Knepley MPI_Comm comm; 1568deea36a5SMatthew G. Knepley PetscSection section; 156970034214SMatthew G. Knepley PetscInt nvtxs = numVertices; /* The number of vertices in full graph */ 157070034214SMatthew G. Knepley PetscInt *vtxdist; /* Distribution of vertices across processes */ 157170034214SMatthew G. Knepley PetscInt *xadj = start; /* Start of edge list for each vertex */ 157270034214SMatthew G. Knepley PetscInt *adjncy = adjacency; /* Edge lists for all vertices */ 157370034214SMatthew G. Knepley PetscInt *vwgt = NULL; /* Vertex weights */ 157470034214SMatthew G. Knepley PetscInt *adjwgt = NULL; /* Edge weights */ 157570034214SMatthew G. Knepley PetscInt wgtflag = 0; /* Indicates which weights are present */ 157670034214SMatthew G. Knepley PetscInt numflag = 0; /* Indicates initial offset (0 or 1) */ 157770034214SMatthew G. Knepley PetscInt ncon = 1; /* The number of weights per vertex */ 15785b440754SMatthew G. Knepley PetscInt metis_ptype = pm->ptype; /* kway or recursive bisection */ 1579fb83b9f2SMichael Gegg real_t *tpwgts; /* The fraction of vertex weights assigned to each partition */ 1580fb83b9f2SMichael Gegg real_t *ubvec; /* The balance intolerance for vertex weights */ 1581b3ce585bSLisandro Dalcin PetscInt options[64]; /* Options */ 158270034214SMatthew G. Knepley /* Outputs */ 1583b3ce585bSLisandro Dalcin PetscInt v, i, *assignment, *points; 1584b3ce585bSLisandro Dalcin PetscMPIInt size, rank, p; 158542678178SLisandro Dalcin PetscInt pm_randomSeed = -1; 158670034214SMatthew G. Knepley PetscErrorCode ierr; 158770034214SMatthew G. Knepley 158870034214SMatthew G. Knepley PetscFunctionBegin; 158942678178SLisandro Dalcin ierr = PetscOptionsGetInt(((PetscObject)part)->options,((PetscObject)part)->prefix,"-petscpartitioner_parmetis_seed", &pm_randomSeed, NULL);CHKERRQ(ierr); 159077623264SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr); 1591b3ce585bSLisandro Dalcin ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 159270034214SMatthew G. Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 159370034214SMatthew G. Knepley /* Calculate vertex distribution */ 1594b3ce585bSLisandro Dalcin ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr); 159570034214SMatthew G. Knepley vtxdist[0] = 0; 159670034214SMatthew G. Knepley ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr); 1597b3ce585bSLisandro Dalcin for (p = 2; p <= size; ++p) { 159870034214SMatthew G. Knepley vtxdist[p] += vtxdist[p-1]; 159970034214SMatthew G. Knepley } 160044d8be81SLisandro Dalcin /* Calculate partition weights */ 160170034214SMatthew G. Knepley for (p = 0; p < nparts; ++p) { 160270034214SMatthew G. Knepley tpwgts[p] = 1.0/nparts; 160370034214SMatthew G. Knepley } 16045b440754SMatthew G. Knepley ubvec[0] = pm->imbalanceRatio; 1605deea36a5SMatthew G. Knepley /* Weight cells by dofs on cell by default */ 1606e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1607deea36a5SMatthew G. Knepley if (section) { 1608deea36a5SMatthew G. Knepley PetscInt cStart, cEnd, dof; 160970034214SMatthew G. Knepley 1610deea36a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1611deea36a5SMatthew G. Knepley for (v = cStart; v < cEnd; ++v) { 1612deea36a5SMatthew G. Knepley ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr); 1613925b1076SLisandro Dalcin /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */ 1614925b1076SLisandro Dalcin /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */ 1615925b1076SLisandro Dalcin if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1); 1616deea36a5SMatthew G. Knepley } 1617deea36a5SMatthew G. Knepley } else { 1618deea36a5SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) vwgt[v] = 1; 1619cd0de0f2SShri } 162044d8be81SLisandro Dalcin wgtflag |= 2; /* have weights on graph vertices */ 1621cd0de0f2SShri 162270034214SMatthew G. Knepley if (nparts == 1) { 16239fc93327SToby Isaac ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr); 162470034214SMatthew G. Knepley } else { 1625b3ce585bSLisandro Dalcin for (p = 0; !vtxdist[p+1] && p < size; ++p); 1626b3ce585bSLisandro Dalcin if (vtxdist[p+1] == vtxdist[size]) { 1627b3ce585bSLisandro Dalcin if (rank == p) { 162844d8be81SLisandro Dalcin ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */ 162942678178SLisandro Dalcin options[METIS_OPTION_DBGLVL] = pm->debugFlag; 163042678178SLisandro Dalcin options[METIS_OPTION_SEED] = pm_randomSeed; 163144d8be81SLisandro Dalcin if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()"); 163244d8be81SLisandro Dalcin if (metis_ptype == 1) { 163344d8be81SLisandro Dalcin PetscStackPush("METIS_PartGraphRecursive"); 163472379da4SMatthew G. Knepley ierr = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment); 163544d8be81SLisandro Dalcin PetscStackPop; 163644d8be81SLisandro Dalcin if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphRecursive()"); 163744d8be81SLisandro Dalcin } else { 163844d8be81SLisandro Dalcin /* 163944d8be81SLisandro Dalcin It would be nice to activate the two options below, but they would need some actual testing. 164044d8be81SLisandro Dalcin - Turning on these options may exercise path of the METIS code that have bugs and may break production runs. 164144d8be81SLisandro Dalcin - If CONTIG is set to 1, METIS will exit with error if the graph is disconnected, despite the manual saying the option is ignored in such case. 164244d8be81SLisandro Dalcin */ 164344d8be81SLisandro Dalcin /* options[METIS_OPTION_CONTIG] = 1; */ /* try to produce partitions that are contiguous */ 164444d8be81SLisandro Dalcin /* options[METIS_OPTION_MINCONN] = 1; */ /* minimize the maximum degree of the subdomain graph */ 164570034214SMatthew G. Knepley PetscStackPush("METIS_PartGraphKway"); 164672379da4SMatthew G. Knepley ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment); 164770034214SMatthew G. Knepley PetscStackPop; 164870034214SMatthew G. Knepley if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()"); 164970034214SMatthew G. Knepley } 165044d8be81SLisandro Dalcin } 165170034214SMatthew G. Knepley } else { 165242678178SLisandro Dalcin options[0] = 1; /*use options */ 16535b440754SMatthew G. Knepley options[1] = pm->debugFlag; 165442678178SLisandro Dalcin options[2] = (pm_randomSeed == -1) ? 15 : pm_randomSeed; /* default is GLOBAL_SEED=15 from `libparmetis/defs.h` */ 165570034214SMatthew G. Knepley PetscStackPush("ParMETIS_V3_PartKway"); 165672379da4SMatthew G. Knepley ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment, &comm); 165770034214SMatthew G. Knepley PetscStackPop; 1658c717d290SMatthew G. Knepley if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr); 165970034214SMatthew G. Knepley } 166070034214SMatthew G. Knepley } 166170034214SMatthew G. Knepley /* Convert to PetscSection+IS */ 166277623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 166377623264SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);} 166477623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 166570034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 166677623264SMatthew G. Knepley for (p = 0, i = 0; p < nparts; ++p) { 166770034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 166870034214SMatthew G. Knepley if (assignment[v] == p) points[i++] = v; 166970034214SMatthew G. Knepley } 167070034214SMatthew G. Knepley } 167170034214SMatthew G. Knepley if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 167270034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 1673cd0de0f2SShri ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr); 16749b80ac48SMatthew G. Knepley PetscFunctionReturn(0); 167570034214SMatthew G. Knepley #else 167677623264SMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis."); 167770034214SMatthew G. Knepley #endif 167870034214SMatthew G. Knepley } 167970034214SMatthew G. Knepley 1680d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part) 168177623264SMatthew G. Knepley { 168277623264SMatthew G. Knepley PetscFunctionBegin; 1683*074d466cSStefano Zampini part->noGraph = PETSC_FALSE; 168477623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_ParMetis; 168544d8be81SLisandro Dalcin part->ops->setfromoptions = PetscPartitionerSetFromOptions_ParMetis; 168677623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_ParMetis; 168777623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_ParMetis; 168877623264SMatthew G. Knepley PetscFunctionReturn(0); 168977623264SMatthew G. Knepley } 169077623264SMatthew G. Knepley 169177623264SMatthew G. Knepley /*MC 169277623264SMatthew G. Knepley PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library 169377623264SMatthew G. Knepley 169477623264SMatthew G. Knepley Level: intermediate 169577623264SMatthew G. Knepley 169677623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 169777623264SMatthew G. Knepley M*/ 169877623264SMatthew G. Knepley 169977623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part) 170077623264SMatthew G. Knepley { 170177623264SMatthew G. Knepley PetscPartitioner_ParMetis *p; 170277623264SMatthew G. Knepley PetscErrorCode ierr; 170377623264SMatthew G. Knepley 170477623264SMatthew G. Knepley PetscFunctionBegin; 170577623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 170677623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 170777623264SMatthew G. Knepley part->data = p; 170877623264SMatthew G. Knepley 17095b440754SMatthew G. Knepley p->ptype = 0; 17105b440754SMatthew G. Knepley p->imbalanceRatio = 1.05; 17115b440754SMatthew G. Knepley p->debugFlag = 0; 17125b440754SMatthew G. Knepley 171377623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr); 171477623264SMatthew G. Knepley ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr); 171570034214SMatthew G. Knepley PetscFunctionReturn(0); 171670034214SMatthew G. Knepley } 171770034214SMatthew G. Knepley 1718137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE; 1719137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] = 1720137cd93aSLisandro Dalcin "@article{PTSCOTCH,\n" 1721137cd93aSLisandro Dalcin " author = {C. Chevalier and F. Pellegrini},\n" 1722137cd93aSLisandro Dalcin " title = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n" 1723137cd93aSLisandro Dalcin " journal = {Parallel Computing},\n" 1724137cd93aSLisandro Dalcin " volume = {34},\n" 1725137cd93aSLisandro Dalcin " number = {6},\n" 1726137cd93aSLisandro Dalcin " pages = {318--331},\n" 1727137cd93aSLisandro Dalcin " year = {2008},\n" 1728137cd93aSLisandro Dalcin " doi = {https://doi.org/10.1016/j.parco.2007.12.001}\n" 1729137cd93aSLisandro Dalcin "}\n"; 1730137cd93aSLisandro Dalcin 1731137cd93aSLisandro Dalcin typedef struct { 1732137cd93aSLisandro Dalcin PetscInt strategy; 1733137cd93aSLisandro Dalcin PetscReal imbalance; 1734137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch; 1735137cd93aSLisandro Dalcin 1736137cd93aSLisandro Dalcin static const char *const 1737137cd93aSLisandro Dalcin PTScotchStrategyList[] = { 1738137cd93aSLisandro Dalcin "DEFAULT", 1739137cd93aSLisandro Dalcin "QUALITY", 1740137cd93aSLisandro Dalcin "SPEED", 1741137cd93aSLisandro Dalcin "BALANCE", 1742137cd93aSLisandro Dalcin "SAFETY", 1743137cd93aSLisandro Dalcin "SCALABILITY", 1744137cd93aSLisandro Dalcin "RECURSIVE", 1745137cd93aSLisandro Dalcin "REMAP" 1746137cd93aSLisandro Dalcin }; 1747137cd93aSLisandro Dalcin 1748137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH) 1749137cd93aSLisandro Dalcin 1750137cd93aSLisandro Dalcin EXTERN_C_BEGIN 1751137cd93aSLisandro Dalcin #include <ptscotch.h> 1752137cd93aSLisandro Dalcin EXTERN_C_END 1753137cd93aSLisandro Dalcin 1754137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0) 1755137cd93aSLisandro Dalcin 1756137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy) 1757137cd93aSLisandro Dalcin { 1758137cd93aSLisandro Dalcin switch (strategy) { 1759137cd93aSLisandro Dalcin case 0: return SCOTCH_STRATDEFAULT; 1760137cd93aSLisandro Dalcin case 1: return SCOTCH_STRATQUALITY; 1761137cd93aSLisandro Dalcin case 2: return SCOTCH_STRATSPEED; 1762137cd93aSLisandro Dalcin case 3: return SCOTCH_STRATBALANCE; 1763137cd93aSLisandro Dalcin case 4: return SCOTCH_STRATSAFETY; 1764137cd93aSLisandro Dalcin case 5: return SCOTCH_STRATSCALABILITY; 1765137cd93aSLisandro Dalcin case 6: return SCOTCH_STRATRECURSIVE; 1766137cd93aSLisandro Dalcin case 7: return SCOTCH_STRATREMAP; 1767137cd93aSLisandro Dalcin default: return SCOTCH_STRATDEFAULT; 1768137cd93aSLisandro Dalcin } 1769137cd93aSLisandro Dalcin } 1770137cd93aSLisandro Dalcin 1771137cd93aSLisandro Dalcin 1772137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[], 1773137cd93aSLisandro Dalcin SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[]) 1774137cd93aSLisandro Dalcin { 1775137cd93aSLisandro Dalcin SCOTCH_Graph grafdat; 1776137cd93aSLisandro Dalcin SCOTCH_Strat stradat; 1777137cd93aSLisandro Dalcin SCOTCH_Num vertnbr = n; 1778137cd93aSLisandro Dalcin SCOTCH_Num edgenbr = xadj[n]; 1779137cd93aSLisandro Dalcin SCOTCH_Num* velotab = vtxwgt; 1780137cd93aSLisandro Dalcin SCOTCH_Num* edlotab = adjwgt; 1781137cd93aSLisandro Dalcin SCOTCH_Num flagval = strategy; 1782137cd93aSLisandro Dalcin double kbalval = imbalance; 1783137cd93aSLisandro Dalcin PetscErrorCode ierr; 1784137cd93aSLisandro Dalcin 1785137cd93aSLisandro Dalcin PetscFunctionBegin; 1786d99a0000SVaclav Hapla { 1787d99a0000SVaclav Hapla PetscBool flg = PETSC_TRUE; 1788d99a0000SVaclav Hapla ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr); 1789d99a0000SVaclav Hapla if (!flg) velotab = NULL; 1790d99a0000SVaclav Hapla } 1791137cd93aSLisandro Dalcin ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr); 1792137cd93aSLisandro Dalcin ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr); 1793137cd93aSLisandro Dalcin ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr); 1794137cd93aSLisandro Dalcin ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr); 1795137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG) 1796137cd93aSLisandro Dalcin ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr); 1797137cd93aSLisandro Dalcin #endif 1798137cd93aSLisandro Dalcin ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr); 1799137cd93aSLisandro Dalcin SCOTCH_stratExit(&stradat); 1800137cd93aSLisandro Dalcin SCOTCH_graphExit(&grafdat); 1801137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1802137cd93aSLisandro Dalcin } 1803137cd93aSLisandro Dalcin 1804137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[], 1805137cd93aSLisandro Dalcin SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm) 1806137cd93aSLisandro Dalcin { 1807137cd93aSLisandro Dalcin PetscMPIInt procglbnbr; 1808137cd93aSLisandro Dalcin PetscMPIInt proclocnum; 1809137cd93aSLisandro Dalcin SCOTCH_Arch archdat; 1810137cd93aSLisandro Dalcin SCOTCH_Dgraph grafdat; 1811137cd93aSLisandro Dalcin SCOTCH_Dmapping mappdat; 1812137cd93aSLisandro Dalcin SCOTCH_Strat stradat; 1813137cd93aSLisandro Dalcin SCOTCH_Num vertlocnbr; 1814137cd93aSLisandro Dalcin SCOTCH_Num edgelocnbr; 1815137cd93aSLisandro Dalcin SCOTCH_Num* veloloctab = vtxwgt; 1816137cd93aSLisandro Dalcin SCOTCH_Num* edloloctab = adjwgt; 1817137cd93aSLisandro Dalcin SCOTCH_Num flagval = strategy; 1818137cd93aSLisandro Dalcin double kbalval = imbalance; 1819137cd93aSLisandro Dalcin PetscErrorCode ierr; 1820137cd93aSLisandro Dalcin 1821137cd93aSLisandro Dalcin PetscFunctionBegin; 1822d99a0000SVaclav Hapla { 1823d99a0000SVaclav Hapla PetscBool flg = PETSC_TRUE; 1824d99a0000SVaclav Hapla ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr); 1825d99a0000SVaclav Hapla if (!flg) veloloctab = NULL; 1826d99a0000SVaclav Hapla } 1827137cd93aSLisandro Dalcin ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr); 1828137cd93aSLisandro Dalcin ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr); 1829137cd93aSLisandro Dalcin vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum]; 1830137cd93aSLisandro Dalcin edgelocnbr = xadj[vertlocnbr]; 1831137cd93aSLisandro Dalcin 1832137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr); 1833137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr); 1834137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG) 1835137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr); 1836137cd93aSLisandro Dalcin #endif 1837137cd93aSLisandro Dalcin ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr); 1838137cd93aSLisandro Dalcin ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr); 1839137cd93aSLisandro Dalcin ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr); 1840137cd93aSLisandro Dalcin ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr); 1841137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr); 1842cb87ef4cSFlorian Wechsung 1843137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr); 1844137cd93aSLisandro Dalcin SCOTCH_dgraphMapExit(&grafdat, &mappdat); 1845137cd93aSLisandro Dalcin SCOTCH_archExit(&archdat); 1846137cd93aSLisandro Dalcin SCOTCH_stratExit(&stradat); 1847137cd93aSLisandro Dalcin SCOTCH_dgraphExit(&grafdat); 1848137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1849137cd93aSLisandro Dalcin } 1850137cd93aSLisandro Dalcin 1851137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */ 1852137cd93aSLisandro Dalcin 1853137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part) 1854137cd93aSLisandro Dalcin { 1855137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1856137cd93aSLisandro Dalcin PetscErrorCode ierr; 1857137cd93aSLisandro Dalcin 1858137cd93aSLisandro Dalcin PetscFunctionBegin; 1859137cd93aSLisandro Dalcin ierr = PetscFree(p);CHKERRQ(ierr); 1860137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1861137cd93aSLisandro Dalcin } 1862137cd93aSLisandro Dalcin 1863137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer) 1864137cd93aSLisandro Dalcin { 1865137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1866137cd93aSLisandro Dalcin PetscErrorCode ierr; 1867137cd93aSLisandro Dalcin 1868137cd93aSLisandro Dalcin PetscFunctionBegin; 1869137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1870137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr); 1871137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr); 1872137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1873137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1874137cd93aSLisandro Dalcin } 1875137cd93aSLisandro Dalcin 1876137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer) 1877137cd93aSLisandro Dalcin { 1878137cd93aSLisandro Dalcin PetscBool iascii; 1879137cd93aSLisandro Dalcin PetscErrorCode ierr; 1880137cd93aSLisandro Dalcin 1881137cd93aSLisandro Dalcin PetscFunctionBegin; 1882137cd93aSLisandro Dalcin PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1883137cd93aSLisandro Dalcin PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1884137cd93aSLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 1885137cd93aSLisandro Dalcin if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);} 1886137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1887137cd93aSLisandro Dalcin } 1888137cd93aSLisandro Dalcin 1889137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 1890137cd93aSLisandro Dalcin { 1891137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1892137cd93aSLisandro Dalcin const char *const *slist = PTScotchStrategyList; 1893137cd93aSLisandro Dalcin PetscInt nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0])); 1894137cd93aSLisandro Dalcin PetscBool flag; 1895137cd93aSLisandro Dalcin PetscErrorCode ierr; 1896137cd93aSLisandro Dalcin 1897137cd93aSLisandro Dalcin PetscFunctionBegin; 1898137cd93aSLisandro Dalcin ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner PTScotch Options");CHKERRQ(ierr); 1899137cd93aSLisandro Dalcin ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr); 1900137cd93aSLisandro Dalcin ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr); 1901137cd93aSLisandro Dalcin ierr = PetscOptionsTail();CHKERRQ(ierr); 1902137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1903137cd93aSLisandro Dalcin } 1904137cd93aSLisandro Dalcin 1905137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 1906137cd93aSLisandro Dalcin { 1907137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH) 1908137cd93aSLisandro Dalcin MPI_Comm comm = PetscObjectComm((PetscObject)part); 1909137cd93aSLisandro Dalcin PetscInt nvtxs = numVertices; /* The number of vertices in full graph */ 1910137cd93aSLisandro Dalcin PetscInt *vtxdist; /* Distribution of vertices across processes */ 1911137cd93aSLisandro Dalcin PetscInt *xadj = start; /* Start of edge list for each vertex */ 1912137cd93aSLisandro Dalcin PetscInt *adjncy = adjacency; /* Edge lists for all vertices */ 1913137cd93aSLisandro Dalcin PetscInt *vwgt = NULL; /* Vertex weights */ 1914137cd93aSLisandro Dalcin PetscInt *adjwgt = NULL; /* Edge weights */ 1915137cd93aSLisandro Dalcin PetscInt v, i, *assignment, *points; 1916137cd93aSLisandro Dalcin PetscMPIInt size, rank, p; 1917137cd93aSLisandro Dalcin PetscErrorCode ierr; 1918137cd93aSLisandro Dalcin 1919137cd93aSLisandro Dalcin PetscFunctionBegin; 1920137cd93aSLisandro Dalcin ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 1921137cd93aSLisandro Dalcin ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 192299b53901SStefano Zampini ierr = PetscMalloc2(size+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr); 1923137cd93aSLisandro Dalcin 1924137cd93aSLisandro Dalcin /* Calculate vertex distribution */ 1925137cd93aSLisandro Dalcin vtxdist[0] = 0; 1926137cd93aSLisandro Dalcin ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr); 1927137cd93aSLisandro Dalcin for (p = 2; p <= size; ++p) { 1928137cd93aSLisandro Dalcin vtxdist[p] += vtxdist[p-1]; 1929137cd93aSLisandro Dalcin } 1930137cd93aSLisandro Dalcin 1931137cd93aSLisandro Dalcin if (nparts == 1) { 1932137cd93aSLisandro Dalcin ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr); 1933137cd93aSLisandro Dalcin } else { 1934137cd93aSLisandro Dalcin PetscSection section; 1935137cd93aSLisandro Dalcin /* Weight cells by dofs on cell by default */ 1936137cd93aSLisandro Dalcin ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr); 1937e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1938137cd93aSLisandro Dalcin if (section) { 1939d5528e35SFlorian Wechsung PetscInt vStart, eEnd, dof; 1940d5528e35SFlorian Wechsung ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &eEnd);CHKERRQ(ierr); 1941d5528e35SFlorian Wechsung for (v = vStart; v < eEnd; ++v) { 1942137cd93aSLisandro Dalcin ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr); 1943137cd93aSLisandro Dalcin /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */ 1944137cd93aSLisandro Dalcin /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */ 1945137cd93aSLisandro Dalcin if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1); 1946137cd93aSLisandro Dalcin } 1947137cd93aSLisandro Dalcin } else { 1948137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) vwgt[v] = 1; 1949137cd93aSLisandro Dalcin } 1950137cd93aSLisandro Dalcin { 1951137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data; 1952137cd93aSLisandro Dalcin int strat = PTScotch_Strategy(pts->strategy); 1953137cd93aSLisandro Dalcin double imbal = (double)pts->imbalance; 1954137cd93aSLisandro Dalcin 1955137cd93aSLisandro Dalcin for (p = 0; !vtxdist[p+1] && p < size; ++p); 1956137cd93aSLisandro Dalcin if (vtxdist[p+1] == vtxdist[size]) { 1957137cd93aSLisandro Dalcin if (rank == p) { 1958137cd93aSLisandro Dalcin ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr); 1959137cd93aSLisandro Dalcin } 1960137cd93aSLisandro Dalcin } else { 1961137cd93aSLisandro Dalcin ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr); 1962137cd93aSLisandro Dalcin } 1963137cd93aSLisandro Dalcin } 1964137cd93aSLisandro Dalcin ierr = PetscFree(vwgt);CHKERRQ(ierr); 1965137cd93aSLisandro Dalcin } 1966137cd93aSLisandro Dalcin 1967137cd93aSLisandro Dalcin /* Convert to PetscSection+IS */ 1968137cd93aSLisandro Dalcin ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 1969137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);} 1970137cd93aSLisandro Dalcin ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1971137cd93aSLisandro Dalcin ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 1972137cd93aSLisandro Dalcin for (p = 0, i = 0; p < nparts; ++p) { 1973137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) { 1974137cd93aSLisandro Dalcin if (assignment[v] == p) points[i++] = v; 1975137cd93aSLisandro Dalcin } 1976137cd93aSLisandro Dalcin } 1977137cd93aSLisandro Dalcin if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 1978137cd93aSLisandro Dalcin ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 1979137cd93aSLisandro Dalcin 1980137cd93aSLisandro Dalcin ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr); 1981137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1982137cd93aSLisandro Dalcin #else 1983137cd93aSLisandro Dalcin SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch."); 1984137cd93aSLisandro Dalcin #endif 1985137cd93aSLisandro Dalcin } 1986137cd93aSLisandro Dalcin 1987137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part) 1988137cd93aSLisandro Dalcin { 1989137cd93aSLisandro Dalcin PetscFunctionBegin; 1990*074d466cSStefano Zampini part->noGraph = PETSC_FALSE; 1991137cd93aSLisandro Dalcin part->ops->view = PetscPartitionerView_PTScotch; 1992137cd93aSLisandro Dalcin part->ops->destroy = PetscPartitionerDestroy_PTScotch; 1993137cd93aSLisandro Dalcin part->ops->partition = PetscPartitionerPartition_PTScotch; 1994137cd93aSLisandro Dalcin part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch; 1995137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1996137cd93aSLisandro Dalcin } 1997137cd93aSLisandro Dalcin 1998137cd93aSLisandro Dalcin /*MC 1999137cd93aSLisandro Dalcin PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library 2000137cd93aSLisandro Dalcin 2001137cd93aSLisandro Dalcin Level: intermediate 2002137cd93aSLisandro Dalcin 2003137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 2004137cd93aSLisandro Dalcin M*/ 2005137cd93aSLisandro Dalcin 2006137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part) 2007137cd93aSLisandro Dalcin { 2008137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p; 2009137cd93aSLisandro Dalcin PetscErrorCode ierr; 2010137cd93aSLisandro Dalcin 2011137cd93aSLisandro Dalcin PetscFunctionBegin; 2012137cd93aSLisandro Dalcin PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 2013137cd93aSLisandro Dalcin ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 2014137cd93aSLisandro Dalcin part->data = p; 2015137cd93aSLisandro Dalcin 2016137cd93aSLisandro Dalcin p->strategy = 0; 2017137cd93aSLisandro Dalcin p->imbalance = 0.01; 2018137cd93aSLisandro Dalcin 2019137cd93aSLisandro Dalcin ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr); 2020137cd93aSLisandro Dalcin ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr); 2021137cd93aSLisandro Dalcin PetscFunctionReturn(0); 2022137cd93aSLisandro Dalcin } 2023137cd93aSLisandro Dalcin 2024137cd93aSLisandro Dalcin 20255680f57bSMatthew G. Knepley /*@ 20265680f57bSMatthew G. Knepley DMPlexGetPartitioner - Get the mesh partitioner 20275680f57bSMatthew G. Knepley 20285680f57bSMatthew G. Knepley Not collective 20295680f57bSMatthew G. Knepley 20305680f57bSMatthew G. Knepley Input Parameter: 20315680f57bSMatthew G. Knepley . dm - The DM 20325680f57bSMatthew G. Knepley 20335680f57bSMatthew G. Knepley Output Parameter: 20345680f57bSMatthew G. Knepley . part - The PetscPartitioner 20355680f57bSMatthew G. Knepley 20365680f57bSMatthew G. Knepley Level: developer 20375680f57bSMatthew G. Knepley 203898599a47SLawrence Mitchell Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner. 203998599a47SLawrence Mitchell 204098599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate() 20415680f57bSMatthew G. Knepley @*/ 20425680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part) 20435680f57bSMatthew G. Knepley { 20445680f57bSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 20455680f57bSMatthew G. Knepley 20465680f57bSMatthew G. Knepley PetscFunctionBegin; 20475680f57bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 20485680f57bSMatthew G. Knepley PetscValidPointer(part, 2); 20495680f57bSMatthew G. Knepley *part = mesh->partitioner; 20505680f57bSMatthew G. Knepley PetscFunctionReturn(0); 20515680f57bSMatthew G. Knepley } 20525680f57bSMatthew G. Knepley 205371bb2955SLawrence Mitchell /*@ 205471bb2955SLawrence Mitchell DMPlexSetPartitioner - Set the mesh partitioner 205571bb2955SLawrence Mitchell 205671bb2955SLawrence Mitchell logically collective on dm and part 205771bb2955SLawrence Mitchell 205871bb2955SLawrence Mitchell Input Parameters: 205971bb2955SLawrence Mitchell + dm - The DM 206071bb2955SLawrence Mitchell - part - The partitioner 206171bb2955SLawrence Mitchell 206271bb2955SLawrence Mitchell Level: developer 206371bb2955SLawrence Mitchell 206471bb2955SLawrence Mitchell Note: Any existing PetscPartitioner will be destroyed. 206571bb2955SLawrence Mitchell 206671bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate() 206771bb2955SLawrence Mitchell @*/ 206871bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part) 206971bb2955SLawrence Mitchell { 207071bb2955SLawrence Mitchell DM_Plex *mesh = (DM_Plex *) dm->data; 207171bb2955SLawrence Mitchell PetscErrorCode ierr; 207271bb2955SLawrence Mitchell 207371bb2955SLawrence Mitchell PetscFunctionBegin; 207471bb2955SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 207571bb2955SLawrence Mitchell PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2); 207671bb2955SLawrence Mitchell ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr); 207771bb2955SLawrence Mitchell ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr); 207871bb2955SLawrence Mitchell mesh->partitioner = part; 207971bb2955SLawrence Mitchell PetscFunctionReturn(0); 208071bb2955SLawrence Mitchell } 208171bb2955SLawrence Mitchell 2082e8f14785SLisandro Dalcin static PetscErrorCode DMPlexAddClosure_Tree(DM dm, PetscHSetI ht, PetscInt point, PetscBool up, PetscBool down) 2083270bba0cSToby Isaac { 2084270bba0cSToby Isaac PetscErrorCode ierr; 2085270bba0cSToby Isaac 2086270bba0cSToby Isaac PetscFunctionBegin; 20876a5a2ffdSToby Isaac if (up) { 20886a5a2ffdSToby Isaac PetscInt parent; 20896a5a2ffdSToby Isaac 2090270bba0cSToby Isaac ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr); 20916a5a2ffdSToby Isaac if (parent != point) { 20926a5a2ffdSToby Isaac PetscInt closureSize, *closure = NULL, i; 20936a5a2ffdSToby Isaac 2094270bba0cSToby Isaac ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 2095270bba0cSToby Isaac for (i = 0; i < closureSize; i++) { 2096270bba0cSToby Isaac PetscInt cpoint = closure[2*i]; 2097270bba0cSToby Isaac 2098e8f14785SLisandro Dalcin ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr); 20991b807c88SLisandro Dalcin ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr); 2100270bba0cSToby Isaac } 2101270bba0cSToby Isaac ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 21026a5a2ffdSToby Isaac } 21036a5a2ffdSToby Isaac } 21046a5a2ffdSToby Isaac if (down) { 21056a5a2ffdSToby Isaac PetscInt numChildren; 21066a5a2ffdSToby Isaac const PetscInt *children; 21076a5a2ffdSToby Isaac 21086a5a2ffdSToby Isaac ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr); 21096a5a2ffdSToby Isaac if (numChildren) { 21106a5a2ffdSToby Isaac PetscInt i; 21116a5a2ffdSToby Isaac 21126a5a2ffdSToby Isaac for (i = 0; i < numChildren; i++) { 21136a5a2ffdSToby Isaac PetscInt cpoint = children[i]; 21146a5a2ffdSToby Isaac 2115e8f14785SLisandro Dalcin ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr); 21161b807c88SLisandro Dalcin ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr); 21176a5a2ffdSToby Isaac } 21186a5a2ffdSToby Isaac } 21196a5a2ffdSToby Isaac } 2120270bba0cSToby Isaac PetscFunctionReturn(0); 2121270bba0cSToby Isaac } 2122270bba0cSToby Isaac 2123825f8a23SLisandro Dalcin PETSC_INTERN PetscErrorCode DMPlexPartitionLabelClosure_Private(DM,DMLabel,PetscInt,PetscInt,const PetscInt[],IS*); 2124825f8a23SLisandro Dalcin 2125825f8a23SLisandro Dalcin PetscErrorCode DMPlexPartitionLabelClosure_Private(DM dm, DMLabel label, PetscInt rank, PetscInt numPoints, const PetscInt points[], IS *closureIS) 2126825f8a23SLisandro Dalcin { 2127825f8a23SLisandro Dalcin DM_Plex *mesh = (DM_Plex *)dm->data; 2128825f8a23SLisandro Dalcin PetscBool hasTree = (mesh->parentSection || mesh->childSection) ? PETSC_TRUE : PETSC_FALSE; 2129825f8a23SLisandro Dalcin PetscInt *closure = NULL, closureSize, nelems, *elems, off = 0, p, c; 2130825f8a23SLisandro Dalcin PetscHSetI ht; 2131825f8a23SLisandro Dalcin PetscErrorCode ierr; 2132825f8a23SLisandro Dalcin 2133825f8a23SLisandro Dalcin PetscFunctionBegin; 2134825f8a23SLisandro Dalcin ierr = PetscHSetICreate(&ht);CHKERRQ(ierr); 2135825f8a23SLisandro Dalcin ierr = PetscHSetIResize(ht, numPoints*16);CHKERRQ(ierr); 2136825f8a23SLisandro Dalcin for (p = 0; p < numPoints; ++p) { 2137825f8a23SLisandro Dalcin ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 2138825f8a23SLisandro Dalcin for (c = 0; c < closureSize*2; c += 2) { 2139825f8a23SLisandro Dalcin ierr = PetscHSetIAdd(ht, closure[c]);CHKERRQ(ierr); 2140825f8a23SLisandro Dalcin if (hasTree) {ierr = DMPlexAddClosure_Tree(dm, ht, closure[c], PETSC_TRUE, PETSC_TRUE);CHKERRQ(ierr);} 2141825f8a23SLisandro Dalcin } 2142825f8a23SLisandro Dalcin } 2143825f8a23SLisandro Dalcin if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);} 2144825f8a23SLisandro Dalcin ierr = PetscHSetIGetSize(ht, &nelems);CHKERRQ(ierr); 2145825f8a23SLisandro Dalcin ierr = PetscMalloc1(nelems, &elems);CHKERRQ(ierr); 2146825f8a23SLisandro Dalcin ierr = PetscHSetIGetElems(ht, &off, elems);CHKERRQ(ierr); 2147825f8a23SLisandro Dalcin ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr); 2148825f8a23SLisandro Dalcin ierr = PetscSortInt(nelems, elems);CHKERRQ(ierr); 2149825f8a23SLisandro Dalcin ierr = ISCreateGeneral(PETSC_COMM_SELF, nelems, elems, PETSC_OWN_POINTER, closureIS);CHKERRQ(ierr); 2150825f8a23SLisandro Dalcin PetscFunctionReturn(0); 2151825f8a23SLisandro Dalcin } 2152825f8a23SLisandro Dalcin 21535abbe4feSMichael Lange /*@ 21545abbe4feSMichael Lange DMPlexPartitionLabelClosure - Add the closure of all points to the partition label 21555abbe4feSMichael Lange 21565abbe4feSMichael Lange Input Parameters: 21575abbe4feSMichael Lange + dm - The DM 21585abbe4feSMichael Lange - label - DMLabel assinging ranks to remote roots 21595abbe4feSMichael Lange 21605abbe4feSMichael Lange Level: developer 21615abbe4feSMichael Lange 21625abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 21635abbe4feSMichael Lange @*/ 21645abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label) 21659ffc88e4SToby Isaac { 2166825f8a23SLisandro Dalcin IS rankIS, pointIS, closureIS; 21675abbe4feSMichael Lange const PetscInt *ranks, *points; 2168825f8a23SLisandro Dalcin PetscInt numRanks, numPoints, r; 21699ffc88e4SToby Isaac PetscErrorCode ierr; 21709ffc88e4SToby Isaac 21719ffc88e4SToby Isaac PetscFunctionBegin; 21725abbe4feSMichael Lange ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr); 21735abbe4feSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 21745abbe4feSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 21755abbe4feSMichael Lange for (r = 0; r < numRanks; ++r) { 21765abbe4feSMichael Lange const PetscInt rank = ranks[r]; 21775abbe4feSMichael Lange ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr); 21785abbe4feSMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 21795abbe4feSMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 2180825f8a23SLisandro Dalcin ierr = DMPlexPartitionLabelClosure_Private(dm, label, rank, numPoints, points, &closureIS);CHKERRQ(ierr); 21815abbe4feSMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 21825abbe4feSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2183825f8a23SLisandro Dalcin ierr = DMLabelSetStratumIS(label, rank, closureIS);CHKERRQ(ierr); 2184825f8a23SLisandro Dalcin ierr = ISDestroy(&closureIS);CHKERRQ(ierr); 21859ffc88e4SToby Isaac } 21865abbe4feSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 21875abbe4feSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 21889ffc88e4SToby Isaac PetscFunctionReturn(0); 21899ffc88e4SToby Isaac } 21909ffc88e4SToby Isaac 219124d039d7SMichael Lange /*@ 219224d039d7SMichael Lange DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label 219324d039d7SMichael Lange 219424d039d7SMichael Lange Input Parameters: 219524d039d7SMichael Lange + dm - The DM 219624d039d7SMichael Lange - label - DMLabel assinging ranks to remote roots 219724d039d7SMichael Lange 219824d039d7SMichael Lange Level: developer 219924d039d7SMichael Lange 220024d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 220124d039d7SMichael Lange @*/ 220224d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label) 220370034214SMatthew G. Knepley { 220424d039d7SMichael Lange IS rankIS, pointIS; 220524d039d7SMichael Lange const PetscInt *ranks, *points; 220624d039d7SMichael Lange PetscInt numRanks, numPoints, r, p, a, adjSize; 220724d039d7SMichael Lange PetscInt *adj = NULL; 220870034214SMatthew G. Knepley PetscErrorCode ierr; 220970034214SMatthew G. Knepley 221070034214SMatthew G. Knepley PetscFunctionBegin; 221124d039d7SMichael Lange ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr); 221224d039d7SMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 221324d039d7SMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 221424d039d7SMichael Lange for (r = 0; r < numRanks; ++r) { 221524d039d7SMichael Lange const PetscInt rank = ranks[r]; 221670034214SMatthew G. Knepley 221724d039d7SMichael Lange ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr); 221824d039d7SMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 221924d039d7SMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 222070034214SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 222124d039d7SMichael Lange adjSize = PETSC_DETERMINE; 222224d039d7SMichael Lange ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr); 222324d039d7SMichael Lange for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);} 222470034214SMatthew G. Knepley } 222524d039d7SMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 222624d039d7SMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 222770034214SMatthew G. Knepley } 222824d039d7SMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 222924d039d7SMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 223024d039d7SMichael Lange ierr = PetscFree(adj);CHKERRQ(ierr); 223124d039d7SMichael Lange PetscFunctionReturn(0); 223270034214SMatthew G. Knepley } 223370034214SMatthew G. Knepley 2234be200f8dSMichael Lange /*@ 2235be200f8dSMichael Lange DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF 2236be200f8dSMichael Lange 2237be200f8dSMichael Lange Input Parameters: 2238be200f8dSMichael Lange + dm - The DM 2239be200f8dSMichael Lange - label - DMLabel assinging ranks to remote roots 2240be200f8dSMichael Lange 2241be200f8dSMichael Lange Level: developer 2242be200f8dSMichael Lange 2243be200f8dSMichael Lange Note: This is required when generating multi-level overlaps to capture 2244be200f8dSMichael Lange overlap points from non-neighbouring partitions. 2245be200f8dSMichael Lange 2246be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 2247be200f8dSMichael Lange @*/ 2248be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label) 2249be200f8dSMichael Lange { 2250be200f8dSMichael Lange MPI_Comm comm; 2251be200f8dSMichael Lange PetscMPIInt rank; 2252be200f8dSMichael Lange PetscSF sfPoint; 22535d04f6ebSMichael Lange DMLabel lblRoots, lblLeaves; 2254be200f8dSMichael Lange IS rankIS, pointIS; 2255be200f8dSMichael Lange const PetscInt *ranks; 2256be200f8dSMichael Lange PetscInt numRanks, r; 2257be200f8dSMichael Lange PetscErrorCode ierr; 2258be200f8dSMichael Lange 2259be200f8dSMichael Lange PetscFunctionBegin; 2260be200f8dSMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 2261be200f8dSMichael Lange ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 2262be200f8dSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 22635d04f6ebSMichael Lange /* Pull point contributions from remote leaves into local roots */ 22645d04f6ebSMichael Lange ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr); 22655d04f6ebSMichael Lange ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr); 22665d04f6ebSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 22675d04f6ebSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 22685d04f6ebSMichael Lange for (r = 0; r < numRanks; ++r) { 22695d04f6ebSMichael Lange const PetscInt remoteRank = ranks[r]; 22705d04f6ebSMichael Lange if (remoteRank == rank) continue; 22715d04f6ebSMichael Lange ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr); 22725d04f6ebSMichael Lange ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr); 22735d04f6ebSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 22745d04f6ebSMichael Lange } 22755d04f6ebSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 22765d04f6ebSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 22775d04f6ebSMichael Lange ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr); 2278be200f8dSMichael Lange /* Push point contributions from roots into remote leaves */ 2279be200f8dSMichael Lange ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr); 2280be200f8dSMichael Lange ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr); 2281be200f8dSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 2282be200f8dSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 2283be200f8dSMichael Lange for (r = 0; r < numRanks; ++r) { 2284be200f8dSMichael Lange const PetscInt remoteRank = ranks[r]; 2285be200f8dSMichael Lange if (remoteRank == rank) continue; 2286be200f8dSMichael Lange ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr); 2287be200f8dSMichael Lange ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr); 2288be200f8dSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2289be200f8dSMichael Lange } 2290be200f8dSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 2291be200f8dSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 2292be200f8dSMichael Lange ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr); 2293be200f8dSMichael Lange PetscFunctionReturn(0); 2294be200f8dSMichael Lange } 2295be200f8dSMichael Lange 22961fd9873aSMichael Lange /*@ 22971fd9873aSMichael Lange DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label 22981fd9873aSMichael Lange 22991fd9873aSMichael Lange Input Parameters: 23001fd9873aSMichael Lange + dm - The DM 23011fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots 23021fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank 23031fd9873aSMichael Lange 23041fd9873aSMichael Lange Output Parameter: 23051fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots 23061fd9873aSMichael Lange 23071fd9873aSMichael Lange Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The 23081fd9873aSMichael Lange resulting leafLabel is a receiver mapping of remote roots to their parent rank. 23091fd9873aSMichael Lange 23101fd9873aSMichael Lange Level: developer 23111fd9873aSMichael Lange 23121fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 23131fd9873aSMichael Lange @*/ 23141fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel) 23151fd9873aSMichael Lange { 23161fd9873aSMichael Lange MPI_Comm comm; 2317874ddda9SLisandro Dalcin PetscMPIInt rank, size, r; 2318874ddda9SLisandro Dalcin PetscInt p, n, numNeighbors, numPoints, dof, off, rootSize, l, nleaves, leafSize; 23191fd9873aSMichael Lange PetscSF sfPoint; 2320874ddda9SLisandro Dalcin PetscSection rootSection; 23211fd9873aSMichael Lange PetscSFNode *rootPoints, *leafPoints; 23221fd9873aSMichael Lange const PetscSFNode *remote; 23231fd9873aSMichael Lange const PetscInt *local, *neighbors; 23241fd9873aSMichael Lange IS valueIS; 2325874ddda9SLisandro Dalcin PetscBool mpiOverflow = PETSC_FALSE; 23261fd9873aSMichael Lange PetscErrorCode ierr; 23271fd9873aSMichael Lange 23281fd9873aSMichael Lange PetscFunctionBegin; 23291fd9873aSMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 23301fd9873aSMichael Lange ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 23319852e123SBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 23321fd9873aSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 23331fd9873aSMichael Lange 23341fd9873aSMichael Lange /* Convert to (point, rank) and use actual owners */ 23351fd9873aSMichael Lange ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr); 23369852e123SBarry Smith ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr); 23371fd9873aSMichael Lange ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr); 23381fd9873aSMichael Lange ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr); 23391fd9873aSMichael Lange ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr); 23401fd9873aSMichael Lange for (n = 0; n < numNeighbors; ++n) { 23411fd9873aSMichael Lange ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr); 23421fd9873aSMichael Lange ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr); 23431fd9873aSMichael Lange } 23441fd9873aSMichael Lange ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr); 2345874ddda9SLisandro Dalcin ierr = PetscSectionGetStorageSize(rootSection, &rootSize);CHKERRQ(ierr); 2346874ddda9SLisandro Dalcin ierr = PetscMalloc1(rootSize, &rootPoints);CHKERRQ(ierr); 23471fd9873aSMichael Lange ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr); 23481fd9873aSMichael Lange for (n = 0; n < numNeighbors; ++n) { 23491fd9873aSMichael Lange IS pointIS; 23501fd9873aSMichael Lange const PetscInt *points; 23511fd9873aSMichael Lange 23521fd9873aSMichael Lange ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr); 23531fd9873aSMichael Lange ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr); 23541fd9873aSMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 23551fd9873aSMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 23561fd9873aSMichael Lange for (p = 0; p < numPoints; ++p) { 2357f8987ae8SMichael Lange if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);} 2358f8987ae8SMichael Lange else {l = -1;} 23591fd9873aSMichael Lange if (l >= 0) {rootPoints[off+p] = remote[l];} 23601fd9873aSMichael Lange else {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;} 23611fd9873aSMichael Lange } 23621fd9873aSMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 23631fd9873aSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 23641fd9873aSMichael Lange } 2365874ddda9SLisandro Dalcin 2366874ddda9SLisandro Dalcin /* Try to communicate overlap using All-to-All */ 2367874ddda9SLisandro Dalcin if (!processSF) { 2368874ddda9SLisandro Dalcin PetscInt64 counter = 0; 2369874ddda9SLisandro Dalcin PetscBool locOverflow = PETSC_FALSE; 2370874ddda9SLisandro Dalcin PetscMPIInt *scounts, *sdispls, *rcounts, *rdispls; 2371874ddda9SLisandro Dalcin 2372874ddda9SLisandro Dalcin ierr = PetscCalloc4(size, &scounts, size, &sdispls, size, &rcounts, size, &rdispls);CHKERRQ(ierr); 2373874ddda9SLisandro Dalcin for (n = 0; n < numNeighbors; ++n) { 2374874ddda9SLisandro Dalcin ierr = PetscSectionGetDof(rootSection, neighbors[n], &dof);CHKERRQ(ierr); 2375874ddda9SLisandro Dalcin ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr); 2376874ddda9SLisandro Dalcin #if defined(PETSC_USE_64BIT_INDICES) 2377874ddda9SLisandro Dalcin if (dof > PETSC_MPI_INT_MAX) {locOverflow = PETSC_TRUE; break;} 2378874ddda9SLisandro Dalcin if (off > PETSC_MPI_INT_MAX) {locOverflow = PETSC_TRUE; break;} 2379874ddda9SLisandro Dalcin #endif 2380874ddda9SLisandro Dalcin scounts[neighbors[n]] = (PetscMPIInt) dof; 2381874ddda9SLisandro Dalcin sdispls[neighbors[n]] = (PetscMPIInt) off; 2382874ddda9SLisandro Dalcin } 2383874ddda9SLisandro Dalcin ierr = MPI_Alltoall(scounts, 1, MPI_INT, rcounts, 1, MPI_INT, comm);CHKERRQ(ierr); 2384874ddda9SLisandro Dalcin for (r = 0; r < size; ++r) { rdispls[r] = (int)counter; counter += rcounts[r]; } 2385874ddda9SLisandro Dalcin if (counter > PETSC_MPI_INT_MAX) locOverflow = PETSC_TRUE; 2386874ddda9SLisandro Dalcin ierr = MPI_Allreduce(&locOverflow, &mpiOverflow, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr); 2387874ddda9SLisandro Dalcin if (!mpiOverflow) { 2388874ddda9SLisandro Dalcin leafSize = (PetscInt) counter; 2389874ddda9SLisandro Dalcin ierr = PetscMalloc1(leafSize, &leafPoints);CHKERRQ(ierr); 2390874ddda9SLisandro Dalcin ierr = MPI_Alltoallv(rootPoints, scounts, sdispls, MPIU_2INT, leafPoints, rcounts, rdispls, MPIU_2INT, comm);CHKERRQ(ierr); 2391874ddda9SLisandro Dalcin } 2392874ddda9SLisandro Dalcin ierr = PetscFree4(scounts, sdispls, rcounts, rdispls);CHKERRQ(ierr); 2393874ddda9SLisandro Dalcin } 2394874ddda9SLisandro Dalcin 2395874ddda9SLisandro Dalcin /* Communicate overlap using process star forest */ 2396874ddda9SLisandro Dalcin if (processSF || mpiOverflow) { 2397874ddda9SLisandro Dalcin PetscSF procSF; 2398874ddda9SLisandro Dalcin PetscSFNode *remote; 2399874ddda9SLisandro Dalcin PetscSection leafSection; 2400874ddda9SLisandro Dalcin 2401874ddda9SLisandro Dalcin if (processSF) { 2402874ddda9SLisandro Dalcin ierr = PetscObjectReference((PetscObject)processSF);CHKERRQ(ierr); 2403874ddda9SLisandro Dalcin procSF = processSF; 2404874ddda9SLisandro Dalcin } else { 2405874ddda9SLisandro Dalcin ierr = PetscMalloc1(size, &remote);CHKERRQ(ierr); 2406874ddda9SLisandro Dalcin for (r = 0; r < size; ++r) { remote[r].rank = r; remote[r].index = rank; } 2407874ddda9SLisandro Dalcin ierr = PetscSFCreate(comm, &procSF);CHKERRQ(ierr); 2408874ddda9SLisandro Dalcin ierr = PetscSFSetGraph(procSF, size, size, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 2409874ddda9SLisandro Dalcin } 2410874ddda9SLisandro Dalcin 2411874ddda9SLisandro Dalcin ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &leafSection);CHKERRQ(ierr); 24121fd9873aSMichael Lange ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr); 2413874ddda9SLisandro Dalcin ierr = PetscSectionGetStorageSize(leafSection, &leafSize);CHKERRQ(ierr); 2414874ddda9SLisandro Dalcin ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr); 2415874ddda9SLisandro Dalcin ierr = PetscSFDestroy(&procSF);CHKERRQ(ierr); 2416874ddda9SLisandro Dalcin } 2417874ddda9SLisandro Dalcin 2418874ddda9SLisandro Dalcin for (p = 0; p < leafSize; p++) { 24191fd9873aSMichael Lange ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr); 24201fd9873aSMichael Lange } 2421874ddda9SLisandro Dalcin 2422874ddda9SLisandro Dalcin ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr); 2423874ddda9SLisandro Dalcin ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 24241fd9873aSMichael Lange ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr); 2425874ddda9SLisandro Dalcin ierr = PetscFree(rootPoints);CHKERRQ(ierr); 24261fd9873aSMichael Lange ierr = PetscFree(leafPoints);CHKERRQ(ierr); 24271fd9873aSMichael Lange PetscFunctionReturn(0); 24281fd9873aSMichael Lange } 24291fd9873aSMichael Lange 2430aa3148a8SMichael Lange /*@ 2431aa3148a8SMichael Lange DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points 2432aa3148a8SMichael Lange 2433aa3148a8SMichael Lange Input Parameters: 2434aa3148a8SMichael Lange + dm - The DM 2435aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots 2436aa3148a8SMichael Lange 2437aa3148a8SMichael Lange Output Parameter: 2438aa3148a8SMichael Lange - sf - The star forest communication context encapsulating the defined mapping 2439aa3148a8SMichael Lange 2440aa3148a8SMichael Lange Note: The incoming label is a receiver mapping of remote points to their parent rank. 2441aa3148a8SMichael Lange 2442aa3148a8SMichael Lange Level: developer 2443aa3148a8SMichael Lange 2444aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap 2445aa3148a8SMichael Lange @*/ 2446aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf) 2447aa3148a8SMichael Lange { 24489852e123SBarry Smith PetscMPIInt rank, size; 244943f7d02bSMichael Lange PetscInt n, numRemote, p, numPoints, pStart, pEnd, idx = 0; 2450aa3148a8SMichael Lange PetscSFNode *remotePoints; 245143f7d02bSMichael Lange IS remoteRootIS; 245243f7d02bSMichael Lange const PetscInt *remoteRoots; 2453aa3148a8SMichael Lange PetscErrorCode ierr; 2454aa3148a8SMichael Lange 2455aa3148a8SMichael Lange PetscFunctionBegin; 245643f7d02bSMichael Lange ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 24579852e123SBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr); 2458aa3148a8SMichael Lange 24599852e123SBarry Smith for (numRemote = 0, n = 0; n < size; ++n) { 2460aa3148a8SMichael Lange ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr); 2461aa3148a8SMichael Lange numRemote += numPoints; 2462aa3148a8SMichael Lange } 2463aa3148a8SMichael Lange ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr); 246443f7d02bSMichael Lange /* Put owned points first */ 246543f7d02bSMichael Lange ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr); 246643f7d02bSMichael Lange if (numPoints > 0) { 246743f7d02bSMichael Lange ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr); 246843f7d02bSMichael Lange ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 246943f7d02bSMichael Lange for (p = 0; p < numPoints; p++) { 247043f7d02bSMichael Lange remotePoints[idx].index = remoteRoots[p]; 247143f7d02bSMichael Lange remotePoints[idx].rank = rank; 247243f7d02bSMichael Lange idx++; 247343f7d02bSMichael Lange } 247443f7d02bSMichael Lange ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 247543f7d02bSMichael Lange ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr); 247643f7d02bSMichael Lange } 247743f7d02bSMichael Lange /* Now add remote points */ 24789852e123SBarry Smith for (n = 0; n < size; ++n) { 2479aa3148a8SMichael Lange ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr); 248043f7d02bSMichael Lange if (numPoints <= 0 || n == rank) continue; 2481aa3148a8SMichael Lange ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr); 2482aa3148a8SMichael Lange ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 2483aa3148a8SMichael Lange for (p = 0; p < numPoints; p++) { 2484aa3148a8SMichael Lange remotePoints[idx].index = remoteRoots[p]; 2485aa3148a8SMichael Lange remotePoints[idx].rank = n; 2486aa3148a8SMichael Lange idx++; 2487aa3148a8SMichael Lange } 2488aa3148a8SMichael Lange ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 2489aa3148a8SMichael Lange ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr); 2490aa3148a8SMichael Lange } 2491aa3148a8SMichael Lange ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr); 2492aa3148a8SMichael Lange ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2493aa3148a8SMichael Lange ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr); 249470034214SMatthew G. Knepley PetscFunctionReturn(0); 249570034214SMatthew G. Knepley } 2496cb87ef4cSFlorian Wechsung 24976a3739e5SFlorian Wechsung /* The two functions below are used by DMPlexRebalanceSharedPoints which errors 24986a3739e5SFlorian Wechsung * when PETSc is built without ParMETIS. To avoid -Wunused-function, we take 24996a3739e5SFlorian Wechsung * them out in that case. */ 25006a3739e5SFlorian Wechsung #if defined(PETSC_HAVE_PARMETIS) 25017a82c785SFlorian Wechsung /*@C 25027f57c1a4SFlorian Wechsung 25037f57c1a4SFlorian Wechsung DMPlexRewriteSF - Rewrites the ownership of the SF of a DM (in place). 25047f57c1a4SFlorian Wechsung 25057f57c1a4SFlorian Wechsung Input parameters: 25067f57c1a4SFlorian Wechsung + dm - The DMPlex object. 25077f57c1a4SFlorian Wechsung + n - The number of points. 25087f57c1a4SFlorian Wechsung + pointsToRewrite - The points in the SF whose ownership will change. 25097f57c1a4SFlorian Wechsung + targetOwners - New owner for each element in pointsToRewrite. 25107f57c1a4SFlorian Wechsung + degrees - Degrees of the points in the SF as obtained by PetscSFComputeDegreeBegin/PetscSFComputeDegreeEnd. 25117f57c1a4SFlorian Wechsung 25127f57c1a4SFlorian Wechsung Level: developer 25137f57c1a4SFlorian Wechsung 25147f57c1a4SFlorian Wechsung @*/ 25157f57c1a4SFlorian Wechsung static PetscErrorCode DMPlexRewriteSF(DM dm, PetscInt n, PetscInt *pointsToRewrite, PetscInt *targetOwners, const PetscInt *degrees) 25167f57c1a4SFlorian Wechsung { 25177f57c1a4SFlorian Wechsung PetscInt ierr, pStart, pEnd, i, j, counter, leafCounter, sumDegrees, nroots, nleafs; 25187f57c1a4SFlorian Wechsung PetscInt *cumSumDegrees, *newOwners, *newNumbers, *rankOnLeafs, *locationsOfLeafs, *remoteLocalPointOfLeafs, *points, *leafsNew; 25197f57c1a4SFlorian Wechsung PetscSFNode *leafLocationsNew; 25207f57c1a4SFlorian Wechsung const PetscSFNode *iremote; 25217f57c1a4SFlorian Wechsung const PetscInt *ilocal; 25227f57c1a4SFlorian Wechsung PetscBool *isLeaf; 25237f57c1a4SFlorian Wechsung PetscSF sf; 25247f57c1a4SFlorian Wechsung MPI_Comm comm; 25255a30b08bSFlorian Wechsung PetscMPIInt rank, size; 25267f57c1a4SFlorian Wechsung 25277f57c1a4SFlorian Wechsung PetscFunctionBegin; 25287f57c1a4SFlorian Wechsung ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 25297f57c1a4SFlorian Wechsung ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 25307f57c1a4SFlorian Wechsung ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 25317f57c1a4SFlorian Wechsung ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 25327f57c1a4SFlorian Wechsung 25337f57c1a4SFlorian Wechsung ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 25347f57c1a4SFlorian Wechsung ierr = PetscSFGetGraph(sf, &nroots, &nleafs, &ilocal, &iremote); CHKERRQ(ierr); 25357f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &isLeaf);CHKERRQ(ierr); 25367f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 25377f57c1a4SFlorian Wechsung isLeaf[i] = PETSC_FALSE; 25387f57c1a4SFlorian Wechsung } 25397f57c1a4SFlorian Wechsung for (i=0; i<nleafs; i++) { 25407f57c1a4SFlorian Wechsung isLeaf[ilocal[i]-pStart] = PETSC_TRUE; 25417f57c1a4SFlorian Wechsung } 25427f57c1a4SFlorian Wechsung 25437f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart+1, &cumSumDegrees);CHKERRQ(ierr); 25447f57c1a4SFlorian Wechsung cumSumDegrees[0] = 0; 25457f57c1a4SFlorian Wechsung for (i=1; i<=pEnd-pStart; i++) { 25467f57c1a4SFlorian Wechsung cumSumDegrees[i] = cumSumDegrees[i-1] + degrees[i-1]; 25477f57c1a4SFlorian Wechsung } 25487f57c1a4SFlorian Wechsung sumDegrees = cumSumDegrees[pEnd-pStart]; 25497f57c1a4SFlorian Wechsung /* get the location of my leafs (we have sumDegrees many leafs pointing at our roots) */ 25507f57c1a4SFlorian Wechsung 25517f57c1a4SFlorian Wechsung ierr = PetscMalloc1(sumDegrees, &locationsOfLeafs);CHKERRQ(ierr); 25527f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &rankOnLeafs);CHKERRQ(ierr); 25537f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 25547f57c1a4SFlorian Wechsung rankOnLeafs[i] = rank; 25557f57c1a4SFlorian Wechsung } 25567f57c1a4SFlorian Wechsung ierr = PetscSFGatherBegin(sf, MPIU_INT, rankOnLeafs, locationsOfLeafs);CHKERRQ(ierr); 25577f57c1a4SFlorian Wechsung ierr = PetscSFGatherEnd(sf, MPIU_INT, rankOnLeafs, locationsOfLeafs);CHKERRQ(ierr); 25587f57c1a4SFlorian Wechsung ierr = PetscFree(rankOnLeafs);CHKERRQ(ierr); 25597f57c1a4SFlorian Wechsung 25607f57c1a4SFlorian Wechsung /* get the remote local points of my leaves */ 25617f57c1a4SFlorian Wechsung ierr = PetscMalloc1(sumDegrees, &remoteLocalPointOfLeafs);CHKERRQ(ierr); 25627f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &points);CHKERRQ(ierr); 25637f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 25647f57c1a4SFlorian Wechsung points[i] = pStart+i; 25657f57c1a4SFlorian Wechsung } 25667f57c1a4SFlorian Wechsung ierr = PetscSFGatherBegin(sf, MPIU_INT, points, remoteLocalPointOfLeafs);CHKERRQ(ierr); 25677f57c1a4SFlorian Wechsung ierr = PetscSFGatherEnd(sf, MPIU_INT, points, remoteLocalPointOfLeafs);CHKERRQ(ierr); 25687f57c1a4SFlorian Wechsung ierr = PetscFree(points);CHKERRQ(ierr); 25697f57c1a4SFlorian Wechsung /* Figure out the new owners of the vertices that are up for grabs and their numbers on the new owners */ 25707f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &newOwners);CHKERRQ(ierr); 25717f57c1a4SFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &newNumbers);CHKERRQ(ierr); 25727f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 25737f57c1a4SFlorian Wechsung newOwners[i] = -1; 25747f57c1a4SFlorian Wechsung newNumbers[i] = -1; 25757f57c1a4SFlorian Wechsung } 25767f57c1a4SFlorian Wechsung { 25777f57c1a4SFlorian Wechsung PetscInt oldNumber, newNumber, oldOwner, newOwner; 25787f57c1a4SFlorian Wechsung for (i=0; i<n; i++) { 25797f57c1a4SFlorian Wechsung oldNumber = pointsToRewrite[i]; 25807f57c1a4SFlorian Wechsung newNumber = -1; 25817f57c1a4SFlorian Wechsung oldOwner = rank; 25827f57c1a4SFlorian Wechsung newOwner = targetOwners[i]; 25837f57c1a4SFlorian Wechsung if (oldOwner == newOwner) { 25847f57c1a4SFlorian Wechsung newNumber = oldNumber; 25857f57c1a4SFlorian Wechsung } else { 25867f57c1a4SFlorian Wechsung for (j=0; j<degrees[oldNumber]; j++) { 25877f57c1a4SFlorian Wechsung if (locationsOfLeafs[cumSumDegrees[oldNumber]+j] == newOwner) { 25887f57c1a4SFlorian Wechsung newNumber = remoteLocalPointOfLeafs[cumSumDegrees[oldNumber]+j]; 25897f57c1a4SFlorian Wechsung break; 25907f57c1a4SFlorian Wechsung } 25917f57c1a4SFlorian Wechsung } 25927f57c1a4SFlorian Wechsung } 25937f57c1a4SFlorian Wechsung if (newNumber == -1) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Couldn't find the new owner of vertex."); 25947f57c1a4SFlorian Wechsung 25957f57c1a4SFlorian Wechsung newOwners[oldNumber] = newOwner; 25967f57c1a4SFlorian Wechsung newNumbers[oldNumber] = newNumber; 25977f57c1a4SFlorian Wechsung } 25987f57c1a4SFlorian Wechsung } 25997f57c1a4SFlorian Wechsung ierr = PetscFree(cumSumDegrees);CHKERRQ(ierr); 26007f57c1a4SFlorian Wechsung ierr = PetscFree(locationsOfLeafs);CHKERRQ(ierr); 26017f57c1a4SFlorian Wechsung ierr = PetscFree(remoteLocalPointOfLeafs);CHKERRQ(ierr); 26027f57c1a4SFlorian Wechsung 26037f57c1a4SFlorian Wechsung ierr = PetscSFBcastBegin(sf, MPIU_INT, newOwners, newOwners);CHKERRQ(ierr); 26047f57c1a4SFlorian Wechsung ierr = PetscSFBcastEnd(sf, MPIU_INT, newOwners, newOwners);CHKERRQ(ierr); 26057f57c1a4SFlorian Wechsung ierr = PetscSFBcastBegin(sf, MPIU_INT, newNumbers, newNumbers);CHKERRQ(ierr); 26067f57c1a4SFlorian Wechsung ierr = PetscSFBcastEnd(sf, MPIU_INT, newNumbers, newNumbers);CHKERRQ(ierr); 26077f57c1a4SFlorian Wechsung 26087f57c1a4SFlorian Wechsung /* Now count how many leafs we have on each processor. */ 26097f57c1a4SFlorian Wechsung leafCounter=0; 26107f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 26117f57c1a4SFlorian Wechsung if (newOwners[i] >= 0) { 26127f57c1a4SFlorian Wechsung if (newOwners[i] != rank) { 26137f57c1a4SFlorian Wechsung leafCounter++; 26147f57c1a4SFlorian Wechsung } 26157f57c1a4SFlorian Wechsung } else { 26167f57c1a4SFlorian Wechsung if (isLeaf[i]) { 26177f57c1a4SFlorian Wechsung leafCounter++; 26187f57c1a4SFlorian Wechsung } 26197f57c1a4SFlorian Wechsung } 26207f57c1a4SFlorian Wechsung } 26217f57c1a4SFlorian Wechsung 26227f57c1a4SFlorian Wechsung /* Now set up the new sf by creating the leaf arrays */ 26237f57c1a4SFlorian Wechsung ierr = PetscMalloc1(leafCounter, &leafsNew);CHKERRQ(ierr); 26247f57c1a4SFlorian Wechsung ierr = PetscMalloc1(leafCounter, &leafLocationsNew);CHKERRQ(ierr); 26257f57c1a4SFlorian Wechsung 26267f57c1a4SFlorian Wechsung leafCounter = 0; 26277f57c1a4SFlorian Wechsung counter = 0; 26287f57c1a4SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 26297f57c1a4SFlorian Wechsung if (newOwners[i] >= 0) { 26307f57c1a4SFlorian Wechsung if (newOwners[i] != rank) { 26317f57c1a4SFlorian Wechsung leafsNew[leafCounter] = i; 26327f57c1a4SFlorian Wechsung leafLocationsNew[leafCounter].rank = newOwners[i]; 26337f57c1a4SFlorian Wechsung leafLocationsNew[leafCounter].index = newNumbers[i]; 26347f57c1a4SFlorian Wechsung leafCounter++; 26357f57c1a4SFlorian Wechsung } 26367f57c1a4SFlorian Wechsung } else { 26377f57c1a4SFlorian Wechsung if (isLeaf[i]) { 26387f57c1a4SFlorian Wechsung leafsNew[leafCounter] = i; 26397f57c1a4SFlorian Wechsung leafLocationsNew[leafCounter].rank = iremote[counter].rank; 26407f57c1a4SFlorian Wechsung leafLocationsNew[leafCounter].index = iremote[counter].index; 26417f57c1a4SFlorian Wechsung leafCounter++; 26427f57c1a4SFlorian Wechsung } 26437f57c1a4SFlorian Wechsung } 26447f57c1a4SFlorian Wechsung if (isLeaf[i]) { 26457f57c1a4SFlorian Wechsung counter++; 26467f57c1a4SFlorian Wechsung } 26477f57c1a4SFlorian Wechsung } 26487f57c1a4SFlorian Wechsung 26497f57c1a4SFlorian Wechsung ierr = PetscSFSetGraph(sf, nroots, leafCounter, leafsNew, PETSC_OWN_POINTER, leafLocationsNew, PETSC_OWN_POINTER);CHKERRQ(ierr); 26507f57c1a4SFlorian Wechsung ierr = PetscFree(newOwners);CHKERRQ(ierr); 26517f57c1a4SFlorian Wechsung ierr = PetscFree(newNumbers);CHKERRQ(ierr); 26527f57c1a4SFlorian Wechsung ierr = PetscFree(isLeaf);CHKERRQ(ierr); 26537f57c1a4SFlorian Wechsung PetscFunctionReturn(0); 26547f57c1a4SFlorian Wechsung } 26557f57c1a4SFlorian Wechsung 2656125d2a8fSFlorian Wechsung static PetscErrorCode DMPlexViewDistribution(MPI_Comm comm, PetscInt n, PetscInt skip, PetscInt *vtxwgt, PetscInt *part, PetscViewer viewer) 2657125d2a8fSFlorian Wechsung { 26585a30b08bSFlorian Wechsung PetscInt *distribution, min, max, sum, i, ierr; 26595a30b08bSFlorian Wechsung PetscMPIInt rank, size; 2660125d2a8fSFlorian Wechsung PetscFunctionBegin; 2661125d2a8fSFlorian Wechsung ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 2662125d2a8fSFlorian Wechsung ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 2663125d2a8fSFlorian Wechsung ierr = PetscCalloc1(size, &distribution);CHKERRQ(ierr); 2664125d2a8fSFlorian Wechsung for (i=0; i<n; i++) { 2665125d2a8fSFlorian Wechsung if (part) distribution[part[i]] += vtxwgt[skip*i]; 2666125d2a8fSFlorian Wechsung else distribution[rank] += vtxwgt[skip*i]; 2667125d2a8fSFlorian Wechsung } 2668125d2a8fSFlorian Wechsung ierr = MPI_Allreduce(MPI_IN_PLACE, distribution, size, MPIU_INT, MPI_SUM, comm);CHKERRQ(ierr); 2669125d2a8fSFlorian Wechsung min = distribution[0]; 2670125d2a8fSFlorian Wechsung max = distribution[0]; 2671125d2a8fSFlorian Wechsung sum = distribution[0]; 2672125d2a8fSFlorian Wechsung for (i=1; i<size; i++) { 2673125d2a8fSFlorian Wechsung if (distribution[i]<min) min=distribution[i]; 2674125d2a8fSFlorian Wechsung if (distribution[i]>max) max=distribution[i]; 2675125d2a8fSFlorian Wechsung sum += distribution[i]; 2676125d2a8fSFlorian Wechsung } 2677125d2a8fSFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Min: %D, Avg: %D, Max: %D, Balance: %f\n", min, sum/size, max, (max*1.*size)/sum);CHKERRQ(ierr); 2678125d2a8fSFlorian Wechsung ierr = PetscFree(distribution);CHKERRQ(ierr); 2679125d2a8fSFlorian Wechsung PetscFunctionReturn(0); 2680125d2a8fSFlorian Wechsung } 2681125d2a8fSFlorian Wechsung 26826a3739e5SFlorian Wechsung #endif 26836a3739e5SFlorian Wechsung 2684cb87ef4cSFlorian Wechsung /*@ 26855dc86ac1SFlorian Wechsung DMPlexRebalanceSharedPoints - Redistribute points in the plex that are shared in order to achieve better balancing. This routine updates the PointSF of the DM inplace. 2686cb87ef4cSFlorian Wechsung 2687cb87ef4cSFlorian Wechsung Input parameters: 2688ed44d270SFlorian Wechsung + dm - The DMPlex object. 26897f57c1a4SFlorian Wechsung + entityDepth - depth of the entity to balance (0 -> balance vertices). 26907f57c1a4SFlorian Wechsung + useInitialGuess - whether to use the current distribution as initial guess (only used by ParMETIS). 26917f57c1a4SFlorian Wechsung + parallel - whether to use ParMETIS and do the partition in parallel or whether to gather the graph onto a single process and use METIS. 2692cb87ef4cSFlorian Wechsung 26938b879b41SFlorian Wechsung Output parameters: 26948b879b41SFlorian Wechsung + success - whether the graph partitioning was successful or not. If not, try useInitialGuess=True and parallel=True. 26958b879b41SFlorian Wechsung 2696cb87ef4cSFlorian Wechsung Level: user 2697cb87ef4cSFlorian Wechsung 2698cb87ef4cSFlorian Wechsung @*/ 2699cb87ef4cSFlorian Wechsung 27008b879b41SFlorian Wechsung PetscErrorCode DMPlexRebalanceSharedPoints(DM dm, PetscInt entityDepth, PetscBool useInitialGuess, PetscBool parallel, PetscBool *success) 2701cb87ef4cSFlorian Wechsung { 270241525646SFlorian Wechsung #if defined(PETSC_HAVE_PARMETIS) 2703cb87ef4cSFlorian Wechsung PetscSF sf; 27040faf6628SFlorian Wechsung PetscInt ierr, i, j, idx, jdx; 27057f57c1a4SFlorian Wechsung PetscInt eBegin, eEnd, nroots, nleafs, pStart, pEnd; 27067f57c1a4SFlorian Wechsung const PetscInt *degrees, *ilocal; 27077f57c1a4SFlorian Wechsung const PetscSFNode *iremote; 2708cb87ef4cSFlorian Wechsung PetscBool *toBalance, *isLeaf, *isExclusivelyOwned, *isNonExclusivelyOwned; 2709cf818975SFlorian Wechsung PetscInt numExclusivelyOwned, numNonExclusivelyOwned; 2710cb87ef4cSFlorian Wechsung PetscMPIInt rank, size; 27117f57c1a4SFlorian Wechsung PetscInt *globalNumbersOfLocalOwnedVertices, *leafGlobalNumbers; 27125dc86ac1SFlorian Wechsung const PetscInt *cumSumVertices; 2713cb87ef4cSFlorian Wechsung PetscInt offset, counter; 27147f57c1a4SFlorian Wechsung PetscInt lenadjncy; 2715cb87ef4cSFlorian Wechsung PetscInt *xadj, *adjncy, *vtxwgt; 2716cb87ef4cSFlorian Wechsung PetscInt lenxadj; 2717cb87ef4cSFlorian Wechsung PetscInt *adjwgt = NULL; 2718cb87ef4cSFlorian Wechsung PetscInt *part, *options; 2719cf818975SFlorian Wechsung PetscInt nparts, wgtflag, numflag, ncon, edgecut; 2720cb87ef4cSFlorian Wechsung real_t *ubvec; 2721cb87ef4cSFlorian Wechsung PetscInt *firstVertices, *renumbering; 2722cb87ef4cSFlorian Wechsung PetscInt failed, failedGlobal; 2723cb87ef4cSFlorian Wechsung MPI_Comm comm; 27244baf1206SFlorian Wechsung Mat A, Apre; 272512617df9SFlorian Wechsung const char *prefix = NULL; 27267d197802SFlorian Wechsung PetscViewer viewer; 27277d197802SFlorian Wechsung PetscViewerFormat format; 27285a30b08bSFlorian Wechsung PetscLayout layout; 272912617df9SFlorian Wechsung 273012617df9SFlorian Wechsung PetscFunctionBegin; 27318b879b41SFlorian Wechsung if (success) *success = PETSC_FALSE; 27327f57c1a4SFlorian Wechsung ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 27337f57c1a4SFlorian Wechsung ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 27347f57c1a4SFlorian Wechsung ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 27357f57c1a4SFlorian Wechsung if (size==1) PetscFunctionReturn(0); 27367f57c1a4SFlorian Wechsung 273741525646SFlorian Wechsung ierr = PetscLogEventBegin(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr); 273841525646SFlorian Wechsung 27395a30b08bSFlorian Wechsung ierr = PetscOptionsGetViewer(comm,((PetscObject)dm)->options, prefix,"-dm_rebalance_partition_view",&viewer,&format,NULL);CHKERRQ(ierr); 27405dc86ac1SFlorian Wechsung if (viewer) { 27415a30b08bSFlorian Wechsung ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 27427d197802SFlorian Wechsung } 27437d197802SFlorian Wechsung 2744ed44d270SFlorian Wechsung /* Figure out all points in the plex that we are interested in balancing. */ 2745d5528e35SFlorian Wechsung ierr = DMPlexGetDepthStratum(dm, entityDepth, &eBegin, &eEnd);CHKERRQ(ierr); 2746cb87ef4cSFlorian Wechsung ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2747cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &toBalance);CHKERRQ(ierr); 2748cf818975SFlorian Wechsung 2749cb87ef4cSFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 27505a7e692eSFlorian Wechsung toBalance[i] = (PetscBool)(i-pStart>=eBegin && i-pStart<eEnd); 2751cb87ef4cSFlorian Wechsung } 2752cb87ef4cSFlorian Wechsung 2753cf818975SFlorian Wechsung /* There are three types of points: 2754cf818975SFlorian Wechsung * exclusivelyOwned: points that are owned by this process and only seen by this process 2755cf818975SFlorian Wechsung * nonExclusivelyOwned: points that are owned by this process but seen by at least another process 2756cf818975SFlorian Wechsung * leaf: a point that is seen by this process but owned by a different process 2757cf818975SFlorian Wechsung */ 2758cb87ef4cSFlorian Wechsung ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 2759cb87ef4cSFlorian Wechsung ierr = PetscSFGetGraph(sf, &nroots, &nleafs, &ilocal, &iremote); CHKERRQ(ierr); 2760cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &isLeaf);CHKERRQ(ierr); 2761cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &isNonExclusivelyOwned);CHKERRQ(ierr); 2762cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &isExclusivelyOwned);CHKERRQ(ierr); 2763cb87ef4cSFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 2764cb87ef4cSFlorian Wechsung isNonExclusivelyOwned[i] = PETSC_FALSE; 2765cb87ef4cSFlorian Wechsung isExclusivelyOwned[i] = PETSC_FALSE; 2766cf818975SFlorian Wechsung isLeaf[i] = PETSC_FALSE; 2767cb87ef4cSFlorian Wechsung } 2768cf818975SFlorian Wechsung 2769cf818975SFlorian Wechsung /* start by marking all the leafs */ 2770cb87ef4cSFlorian Wechsung for (i=0; i<nleafs; i++) { 2771cb87ef4cSFlorian Wechsung isLeaf[ilocal[i]-pStart] = PETSC_TRUE; 2772cb87ef4cSFlorian Wechsung } 2773cb87ef4cSFlorian Wechsung 2774cf818975SFlorian Wechsung /* for an owned point, we can figure out whether another processor sees it or 2775cf818975SFlorian Wechsung * not by calculating its degree */ 27767f57c1a4SFlorian Wechsung ierr = PetscSFComputeDegreeBegin(sf, °rees);CHKERRQ(ierr); 27777f57c1a4SFlorian Wechsung ierr = PetscSFComputeDegreeEnd(sf, °rees);CHKERRQ(ierr); 2778cf818975SFlorian Wechsung 2779cb87ef4cSFlorian Wechsung numExclusivelyOwned = 0; 2780cb87ef4cSFlorian Wechsung numNonExclusivelyOwned = 0; 2781cb87ef4cSFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 2782cb87ef4cSFlorian Wechsung if (toBalance[i]) { 27837f57c1a4SFlorian Wechsung if (degrees[i] > 0) { 2784cb87ef4cSFlorian Wechsung isNonExclusivelyOwned[i] = PETSC_TRUE; 2785cb87ef4cSFlorian Wechsung numNonExclusivelyOwned += 1; 2786cb87ef4cSFlorian Wechsung } else { 2787cb87ef4cSFlorian Wechsung if (!isLeaf[i]) { 2788cb87ef4cSFlorian Wechsung isExclusivelyOwned[i] = PETSC_TRUE; 2789cb87ef4cSFlorian Wechsung numExclusivelyOwned += 1; 2790cb87ef4cSFlorian Wechsung } 2791cb87ef4cSFlorian Wechsung } 2792cb87ef4cSFlorian Wechsung } 2793cb87ef4cSFlorian Wechsung } 2794cb87ef4cSFlorian Wechsung 2795cf818975SFlorian Wechsung /* We are going to build a graph with one vertex per core representing the 2796cf818975SFlorian Wechsung * exclusively owned points and then one vertex per nonExclusively owned 2797cf818975SFlorian Wechsung * point. */ 2798cb87ef4cSFlorian Wechsung 27995dc86ac1SFlorian Wechsung ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 28005dc86ac1SFlorian Wechsung ierr = PetscLayoutSetLocalSize(layout, 1 + numNonExclusivelyOwned);CHKERRQ(ierr); 28015dc86ac1SFlorian Wechsung ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 28025dc86ac1SFlorian Wechsung ierr = PetscLayoutGetRanges(layout, &cumSumVertices);CHKERRQ(ierr); 28035dc86ac1SFlorian Wechsung 2804cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &globalNumbersOfLocalOwnedVertices);CHKERRQ(ierr); 2805cb87ef4cSFlorian Wechsung offset = cumSumVertices[rank]; 2806cb87ef4cSFlorian Wechsung counter = 0; 2807cb87ef4cSFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 2808cb87ef4cSFlorian Wechsung if (toBalance[i]) { 28097f57c1a4SFlorian Wechsung if (degrees[i] > 0) { 2810cb87ef4cSFlorian Wechsung globalNumbersOfLocalOwnedVertices[i] = counter + 1 + offset; 2811cb87ef4cSFlorian Wechsung counter++; 2812cb87ef4cSFlorian Wechsung } 2813cb87ef4cSFlorian Wechsung } 2814cb87ef4cSFlorian Wechsung } 2815cb87ef4cSFlorian Wechsung 2816cb87ef4cSFlorian Wechsung /* send the global numbers of vertices I own to the leafs so that they know to connect to it */ 2817cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(pEnd-pStart, &leafGlobalNumbers);CHKERRQ(ierr); 2818cb87ef4cSFlorian Wechsung ierr = PetscSFBcastBegin(sf, MPIU_INT, globalNumbersOfLocalOwnedVertices, leafGlobalNumbers);CHKERRQ(ierr); 2819cb87ef4cSFlorian Wechsung ierr = PetscSFBcastEnd(sf, MPIU_INT, globalNumbersOfLocalOwnedVertices, leafGlobalNumbers);CHKERRQ(ierr); 2820cb87ef4cSFlorian Wechsung 28210faf6628SFlorian Wechsung /* Now start building the data structure for ParMETIS */ 2822cb87ef4cSFlorian Wechsung 28234baf1206SFlorian Wechsung ierr = MatCreate(comm, &Apre);CHKERRQ(ierr); 28244baf1206SFlorian Wechsung ierr = MatSetType(Apre, MATPREALLOCATOR);CHKERRQ(ierr); 28254baf1206SFlorian Wechsung ierr = MatSetSizes(Apre, 1+numNonExclusivelyOwned, 1+numNonExclusivelyOwned, cumSumVertices[size], cumSumVertices[size]);CHKERRQ(ierr); 28264baf1206SFlorian Wechsung ierr = MatSetUp(Apre);CHKERRQ(ierr); 28278c9a1619SFlorian Wechsung 28288c9a1619SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 28298c9a1619SFlorian Wechsung if (toBalance[i]) { 28300faf6628SFlorian Wechsung idx = cumSumVertices[rank]; 28310faf6628SFlorian Wechsung if (isNonExclusivelyOwned[i]) jdx = globalNumbersOfLocalOwnedVertices[i]; 28320faf6628SFlorian Wechsung else if (isLeaf[i]) jdx = leafGlobalNumbers[i]; 28330faf6628SFlorian Wechsung else continue; 28340faf6628SFlorian Wechsung ierr = MatSetValue(Apre, idx, jdx, 1, INSERT_VALUES);CHKERRQ(ierr); 28350faf6628SFlorian Wechsung ierr = MatSetValue(Apre, jdx, idx, 1, INSERT_VALUES);CHKERRQ(ierr); 28364baf1206SFlorian Wechsung } 28374baf1206SFlorian Wechsung } 28384baf1206SFlorian Wechsung 28394baf1206SFlorian Wechsung ierr = MatAssemblyBegin(Apre, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28404baf1206SFlorian Wechsung ierr = MatAssemblyEnd(Apre, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28414baf1206SFlorian Wechsung 28424baf1206SFlorian Wechsung ierr = MatCreate(comm, &A);CHKERRQ(ierr); 28434baf1206SFlorian Wechsung ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr); 28444baf1206SFlorian Wechsung ierr = MatSetSizes(A, 1+numNonExclusivelyOwned, 1+numNonExclusivelyOwned, cumSumVertices[size], cumSumVertices[size]);CHKERRQ(ierr); 28454baf1206SFlorian Wechsung ierr = MatPreallocatorPreallocate(Apre, PETSC_FALSE, A);CHKERRQ(ierr); 28464baf1206SFlorian Wechsung ierr = MatDestroy(&Apre);CHKERRQ(ierr); 28474baf1206SFlorian Wechsung 28484baf1206SFlorian Wechsung for (i=0; i<pEnd-pStart; i++) { 28494baf1206SFlorian Wechsung if (toBalance[i]) { 28500faf6628SFlorian Wechsung idx = cumSumVertices[rank]; 28510faf6628SFlorian Wechsung if (isNonExclusivelyOwned[i]) jdx = globalNumbersOfLocalOwnedVertices[i]; 28520faf6628SFlorian Wechsung else if (isLeaf[i]) jdx = leafGlobalNumbers[i]; 28530faf6628SFlorian Wechsung else continue; 28540faf6628SFlorian Wechsung ierr = MatSetValue(A, idx, jdx, 1, INSERT_VALUES);CHKERRQ(ierr); 28550faf6628SFlorian Wechsung ierr = MatSetValue(A, jdx, idx, 1, INSERT_VALUES);CHKERRQ(ierr); 28560941debbSFlorian Wechsung } 28570941debbSFlorian Wechsung } 28588c9a1619SFlorian Wechsung 28598c9a1619SFlorian Wechsung ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28608c9a1619SFlorian Wechsung ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28614baf1206SFlorian Wechsung ierr = PetscFree(leafGlobalNumbers);CHKERRQ(ierr); 28624baf1206SFlorian Wechsung ierr = PetscFree(globalNumbersOfLocalOwnedVertices);CHKERRQ(ierr); 28634baf1206SFlorian Wechsung 286441525646SFlorian Wechsung nparts = size; 286541525646SFlorian Wechsung wgtflag = 2; 286641525646SFlorian Wechsung numflag = 0; 286741525646SFlorian Wechsung ncon = 2; 286841525646SFlorian Wechsung real_t *tpwgts; 286941525646SFlorian Wechsung ierr = PetscMalloc1(ncon * nparts, &tpwgts);CHKERRQ(ierr); 287041525646SFlorian Wechsung for (i=0; i<ncon*nparts; i++) { 287141525646SFlorian Wechsung tpwgts[i] = 1./(nparts); 287241525646SFlorian Wechsung } 287341525646SFlorian Wechsung 287441525646SFlorian Wechsung ierr = PetscMalloc1(ncon, &ubvec);CHKERRQ(ierr); 287541525646SFlorian Wechsung ubvec[0] = 1.01; 28765a30b08bSFlorian Wechsung ubvec[1] = 1.01; 28778c9a1619SFlorian Wechsung lenadjncy = 0; 28788c9a1619SFlorian Wechsung for (i=0; i<1+numNonExclusivelyOwned; i++) { 28798c9a1619SFlorian Wechsung PetscInt temp=0; 28808c9a1619SFlorian Wechsung ierr = MatGetRow(A, cumSumVertices[rank] + i, &temp, NULL, NULL);CHKERRQ(ierr); 28818c9a1619SFlorian Wechsung lenadjncy += temp; 28828c9a1619SFlorian Wechsung ierr = MatRestoreRow(A, cumSumVertices[rank] + i, &temp, NULL, NULL);CHKERRQ(ierr); 28838c9a1619SFlorian Wechsung } 28848c9a1619SFlorian Wechsung ierr = PetscMalloc1(lenadjncy, &adjncy);CHKERRQ(ierr); 28857407ba93SFlorian Wechsung lenxadj = 2 + numNonExclusivelyOwned; 28860941debbSFlorian Wechsung ierr = PetscMalloc1(lenxadj, &xadj);CHKERRQ(ierr); 28870941debbSFlorian Wechsung xadj[0] = 0; 28888c9a1619SFlorian Wechsung counter = 0; 28898c9a1619SFlorian Wechsung for (i=0; i<1+numNonExclusivelyOwned; i++) { 28902953a68cSFlorian Wechsung PetscInt temp=0; 28912953a68cSFlorian Wechsung const PetscInt *cols; 28928c9a1619SFlorian Wechsung ierr = MatGetRow(A, cumSumVertices[rank] + i, &temp, &cols, NULL);CHKERRQ(ierr); 2893694ea26eSFlorian Wechsung ierr = PetscMemcpy(&adjncy[counter], cols, temp*sizeof(PetscInt));CHKERRQ(ierr); 28948c9a1619SFlorian Wechsung counter += temp; 28950941debbSFlorian Wechsung xadj[i+1] = counter; 28968c9a1619SFlorian Wechsung ierr = MatRestoreRow(A, cumSumVertices[rank] + i, &temp, &cols, NULL);CHKERRQ(ierr); 28978c9a1619SFlorian Wechsung } 28988c9a1619SFlorian Wechsung 2899cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(cumSumVertices[rank+1]-cumSumVertices[rank], &part);CHKERRQ(ierr); 290041525646SFlorian Wechsung ierr = PetscMalloc1(ncon*(1 + numNonExclusivelyOwned), &vtxwgt);CHKERRQ(ierr); 290141525646SFlorian Wechsung vtxwgt[0] = numExclusivelyOwned; 290241525646SFlorian Wechsung if (ncon>1) vtxwgt[1] = 1; 290341525646SFlorian Wechsung for (i=0; i<numNonExclusivelyOwned; i++) { 290441525646SFlorian Wechsung vtxwgt[ncon*(i+1)] = 1; 290541525646SFlorian Wechsung if (ncon>1) vtxwgt[ncon*(i+1)+1] = 0; 290641525646SFlorian Wechsung } 29078c9a1619SFlorian Wechsung 29085dc86ac1SFlorian Wechsung if (viewer) { 29097d197802SFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Attempt rebalancing of shared points of depth %D on interface of mesh distribution.\n", entityDepth);CHKERRQ(ierr); 29107d197802SFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Size of generated auxiliary graph: %D\n", cumSumVertices[size]);CHKERRQ(ierr); 291112617df9SFlorian Wechsung } 291241525646SFlorian Wechsung if (parallel) { 29135a30b08bSFlorian Wechsung ierr = PetscMalloc1(4, &options);CHKERRQ(ierr); 29145a30b08bSFlorian Wechsung options[0] = 1; 29155a30b08bSFlorian Wechsung options[1] = 0; /* Verbosity */ 29165a30b08bSFlorian Wechsung options[2] = 0; /* Seed */ 29175a30b08bSFlorian Wechsung options[3] = PARMETIS_PSR_COUPLED; /* Seed */ 29185dc86ac1SFlorian Wechsung if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using ParMETIS to partition graph.\n");CHKERRQ(ierr); } 29198c9a1619SFlorian Wechsung if (useInitialGuess) { 29205dc86ac1SFlorian Wechsung if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using current distribution of points as initial guess.\n");CHKERRQ(ierr); } 29218c9a1619SFlorian Wechsung PetscStackPush("ParMETIS_V3_RefineKway"); 29225dc86ac1SFlorian Wechsung ierr = ParMETIS_V3_RefineKway((PetscInt*)cumSumVertices, xadj, adjncy, vtxwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgecut, part, &comm); 292306b3913eSFlorian Wechsung if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in ParMETIS_V3_RefineKway()"); 29248c9a1619SFlorian Wechsung PetscStackPop; 29258c9a1619SFlorian Wechsung } else { 29268c9a1619SFlorian Wechsung PetscStackPush("ParMETIS_V3_PartKway"); 29275dc86ac1SFlorian Wechsung ierr = ParMETIS_V3_PartKway((PetscInt*)cumSumVertices, xadj, adjncy, vtxwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgecut, part, &comm); 29288c9a1619SFlorian Wechsung PetscStackPop; 292906b3913eSFlorian Wechsung if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in ParMETIS_V3_PartKway()"); 29308c9a1619SFlorian Wechsung } 2931ef74bcceSFlorian Wechsung ierr = PetscFree(options);CHKERRQ(ierr); 293241525646SFlorian Wechsung } else { 29335dc86ac1SFlorian Wechsung if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using METIS to partition graph.\n");CHKERRQ(ierr); } 293441525646SFlorian Wechsung Mat As; 293541525646SFlorian Wechsung PetscInt numRows; 293641525646SFlorian Wechsung PetscInt *partGlobal; 293741525646SFlorian Wechsung ierr = MatCreateRedundantMatrix(A, size, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &As);CHKERRQ(ierr); 2938cb87ef4cSFlorian Wechsung 293941525646SFlorian Wechsung PetscInt *numExclusivelyOwnedAll; 294041525646SFlorian Wechsung ierr = PetscMalloc1(size, &numExclusivelyOwnedAll);CHKERRQ(ierr); 294141525646SFlorian Wechsung numExclusivelyOwnedAll[rank] = numExclusivelyOwned; 29422953a68cSFlorian Wechsung ierr = MPI_Allgather(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,numExclusivelyOwnedAll,1,MPIU_INT,comm);CHKERRQ(ierr); 2943cb87ef4cSFlorian Wechsung 294441525646SFlorian Wechsung ierr = MatGetSize(As, &numRows, NULL);CHKERRQ(ierr); 294541525646SFlorian Wechsung ierr = PetscMalloc1(numRows, &partGlobal);CHKERRQ(ierr); 29465dc86ac1SFlorian Wechsung if (!rank) { 294741525646SFlorian Wechsung PetscInt *adjncy_g, *xadj_g, *vtxwgt_g; 294841525646SFlorian Wechsung lenadjncy = 0; 294941525646SFlorian Wechsung 295041525646SFlorian Wechsung for (i=0; i<numRows; i++) { 295141525646SFlorian Wechsung PetscInt temp=0; 295241525646SFlorian Wechsung ierr = MatGetRow(As, i, &temp, NULL, NULL);CHKERRQ(ierr); 295341525646SFlorian Wechsung lenadjncy += temp; 295441525646SFlorian Wechsung ierr = MatRestoreRow(As, i, &temp, NULL, NULL);CHKERRQ(ierr); 295541525646SFlorian Wechsung } 295641525646SFlorian Wechsung ierr = PetscMalloc1(lenadjncy, &adjncy_g);CHKERRQ(ierr); 295741525646SFlorian Wechsung lenxadj = 1 + numRows; 295841525646SFlorian Wechsung ierr = PetscMalloc1(lenxadj, &xadj_g);CHKERRQ(ierr); 295941525646SFlorian Wechsung xadj_g[0] = 0; 296041525646SFlorian Wechsung counter = 0; 296141525646SFlorian Wechsung for (i=0; i<numRows; i++) { 29622953a68cSFlorian Wechsung PetscInt temp=0; 29632953a68cSFlorian Wechsung const PetscInt *cols; 296441525646SFlorian Wechsung ierr = MatGetRow(As, i, &temp, &cols, NULL);CHKERRQ(ierr); 2965694ea26eSFlorian Wechsung ierr = PetscMemcpy(&adjncy_g[counter], cols, temp*sizeof(PetscInt));CHKERRQ(ierr); 296641525646SFlorian Wechsung counter += temp; 296741525646SFlorian Wechsung xadj_g[i+1] = counter; 296841525646SFlorian Wechsung ierr = MatRestoreRow(As, i, &temp, &cols, NULL);CHKERRQ(ierr); 296941525646SFlorian Wechsung } 297041525646SFlorian Wechsung ierr = PetscMalloc1(2*numRows, &vtxwgt_g);CHKERRQ(ierr); 297141525646SFlorian Wechsung for (i=0; i<size; i++){ 297241525646SFlorian Wechsung vtxwgt_g[ncon*cumSumVertices[i]] = numExclusivelyOwnedAll[i]; 297341525646SFlorian Wechsung if (ncon>1) vtxwgt_g[ncon*cumSumVertices[i]+1] = 1; 297441525646SFlorian Wechsung for (j=cumSumVertices[i]+1; j<cumSumVertices[i+1]; j++) { 297541525646SFlorian Wechsung vtxwgt_g[ncon*j] = 1; 297641525646SFlorian Wechsung if (ncon>1) vtxwgt_g[2*j+1] = 0; 297741525646SFlorian Wechsung } 297841525646SFlorian Wechsung } 297941525646SFlorian Wechsung ierr = PetscMalloc1(64, &options);CHKERRQ(ierr); 298041525646SFlorian Wechsung ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */ 298106b3913eSFlorian Wechsung if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()"); 298241525646SFlorian Wechsung options[METIS_OPTION_CONTIG] = 1; 298341525646SFlorian Wechsung PetscStackPush("METIS_PartGraphKway"); 298406b3913eSFlorian Wechsung ierr = METIS_PartGraphKway(&numRows, &ncon, xadj_g, adjncy_g, vtxwgt_g, NULL, NULL, &nparts, tpwgts, ubvec, options, &edgecut, partGlobal); 298541525646SFlorian Wechsung PetscStackPop; 298606b3913eSFlorian Wechsung if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()"); 2987ef74bcceSFlorian Wechsung ierr = PetscFree(options);CHKERRQ(ierr); 298841525646SFlorian Wechsung ierr = PetscFree(xadj_g);CHKERRQ(ierr); 298941525646SFlorian Wechsung ierr = PetscFree(adjncy_g);CHKERRQ(ierr); 299041525646SFlorian Wechsung ierr = PetscFree(vtxwgt_g);CHKERRQ(ierr); 299141525646SFlorian Wechsung } 299241525646SFlorian Wechsung ierr = PetscFree(numExclusivelyOwnedAll);CHKERRQ(ierr); 299341525646SFlorian Wechsung 29945dc86ac1SFlorian Wechsung /* Now scatter the parts array. */ 29955dc86ac1SFlorian Wechsung { 299681a13b52SFlorian Wechsung PetscMPIInt *counts, *mpiCumSumVertices; 29975dc86ac1SFlorian Wechsung ierr = PetscMalloc1(size, &counts);CHKERRQ(ierr); 299881a13b52SFlorian Wechsung ierr = PetscMalloc1(size+1, &mpiCumSumVertices);CHKERRQ(ierr); 29995dc86ac1SFlorian Wechsung for(i=0; i<size; i++) { 300081a13b52SFlorian Wechsung ierr = PetscMPIIntCast(cumSumVertices[i+1] - cumSumVertices[i], &(counts[i]));CHKERRQ(ierr); 300141525646SFlorian Wechsung } 300281a13b52SFlorian Wechsung for(i=0; i<=size; i++) { 300381a13b52SFlorian Wechsung ierr = PetscMPIIntCast(cumSumVertices[i], &(mpiCumSumVertices[i]));CHKERRQ(ierr); 300481a13b52SFlorian Wechsung } 300581a13b52SFlorian Wechsung ierr = MPI_Scatterv(partGlobal, counts, mpiCumSumVertices, MPIU_INT, part, counts[rank], MPIU_INT, 0, comm);CHKERRQ(ierr); 30065dc86ac1SFlorian Wechsung ierr = PetscFree(counts);CHKERRQ(ierr); 300781a13b52SFlorian Wechsung ierr = PetscFree(mpiCumSumVertices);CHKERRQ(ierr); 30085dc86ac1SFlorian Wechsung } 30095dc86ac1SFlorian Wechsung 301041525646SFlorian Wechsung ierr = PetscFree(partGlobal);CHKERRQ(ierr); 30112953a68cSFlorian Wechsung ierr = MatDestroy(&As);CHKERRQ(ierr); 301241525646SFlorian Wechsung } 3013cb87ef4cSFlorian Wechsung 30142953a68cSFlorian Wechsung ierr = MatDestroy(&A);CHKERRQ(ierr); 3015cb87ef4cSFlorian Wechsung ierr = PetscFree(ubvec);CHKERRQ(ierr); 3016cb87ef4cSFlorian Wechsung ierr = PetscFree(tpwgts);CHKERRQ(ierr); 3017cb87ef4cSFlorian Wechsung 3018cb87ef4cSFlorian Wechsung /* Now rename the result so that the vertex resembling the exclusively owned points stays on the same rank */ 3019cb87ef4cSFlorian Wechsung 3020cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(size, &firstVertices);CHKERRQ(ierr); 3021cb87ef4cSFlorian Wechsung ierr = PetscMalloc1(size, &renumbering);CHKERRQ(ierr); 3022cb87ef4cSFlorian Wechsung firstVertices[rank] = part[0]; 30232953a68cSFlorian Wechsung ierr = MPI_Allgather(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,firstVertices,1,MPIU_INT,comm);CHKERRQ(ierr); 3024cb87ef4cSFlorian Wechsung for (i=0; i<size; i++) { 3025cb87ef4cSFlorian Wechsung renumbering[firstVertices[i]] = i; 3026cb87ef4cSFlorian Wechsung } 3027cb87ef4cSFlorian Wechsung for (i=0; i<cumSumVertices[rank+1]-cumSumVertices[rank]; i++) { 3028cb87ef4cSFlorian Wechsung part[i] = renumbering[part[i]]; 3029cb87ef4cSFlorian Wechsung } 3030cb87ef4cSFlorian Wechsung /* Check if the renumbering worked (this can fail when ParMETIS gives fewer partitions than there are processes) */ 3031cb87ef4cSFlorian Wechsung failed = (PetscInt)(part[0] != rank); 30322953a68cSFlorian Wechsung ierr = MPI_Allreduce(&failed, &failedGlobal, 1, MPIU_INT, MPI_SUM, comm);CHKERRQ(ierr); 3033cb87ef4cSFlorian Wechsung 30347f57c1a4SFlorian Wechsung ierr = PetscFree(firstVertices);CHKERRQ(ierr); 30357f57c1a4SFlorian Wechsung ierr = PetscFree(renumbering);CHKERRQ(ierr); 30367f57c1a4SFlorian Wechsung 3037cb87ef4cSFlorian Wechsung if (failedGlobal > 0) { 30387f57c1a4SFlorian Wechsung ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 30397f57c1a4SFlorian Wechsung ierr = PetscFree(xadj);CHKERRQ(ierr); 30407f57c1a4SFlorian Wechsung ierr = PetscFree(adjncy);CHKERRQ(ierr); 30417f57c1a4SFlorian Wechsung ierr = PetscFree(vtxwgt);CHKERRQ(ierr); 30427f57c1a4SFlorian Wechsung ierr = PetscFree(toBalance);CHKERRQ(ierr); 30437f57c1a4SFlorian Wechsung ierr = PetscFree(isLeaf);CHKERRQ(ierr); 30447f57c1a4SFlorian Wechsung ierr = PetscFree(isNonExclusivelyOwned);CHKERRQ(ierr); 30457f57c1a4SFlorian Wechsung ierr = PetscFree(isExclusivelyOwned);CHKERRQ(ierr); 30467f57c1a4SFlorian Wechsung ierr = PetscFree(part);CHKERRQ(ierr); 30477f57c1a4SFlorian Wechsung if (viewer) { 304806b3913eSFlorian Wechsung ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 304906b3913eSFlorian Wechsung ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 30507f57c1a4SFlorian Wechsung } 30517f57c1a4SFlorian Wechsung ierr = PetscLogEventEnd(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr); 30528b879b41SFlorian Wechsung PetscFunctionReturn(0); 3053cb87ef4cSFlorian Wechsung } 3054cb87ef4cSFlorian Wechsung 30557407ba93SFlorian Wechsung /*Let's check how well we did distributing points*/ 30565dc86ac1SFlorian Wechsung if (viewer) { 30577d197802SFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Comparing number of owned entities of depth %D on each process before rebalancing, after rebalancing, and after consistency checks.\n", entityDepth);CHKERRQ(ierr); 3058125d2a8fSFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Initial. ");CHKERRQ(ierr); 3059125d2a8fSFlorian Wechsung ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, NULL, viewer);CHKERRQ(ierr); 3060125d2a8fSFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "Rebalanced. ");CHKERRQ(ierr); 3061125d2a8fSFlorian Wechsung ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, part, viewer);CHKERRQ(ierr); 30627407ba93SFlorian Wechsung } 30637407ba93SFlorian Wechsung 3064cb87ef4cSFlorian Wechsung /* Now check that every vertex is owned by a process that it is actually connected to. */ 306541525646SFlorian Wechsung for (i=1; i<=numNonExclusivelyOwned; i++) { 3066cb87ef4cSFlorian Wechsung PetscInt loc = 0; 306741525646SFlorian Wechsung ierr = PetscFindInt(cumSumVertices[part[i]], xadj[i+1]-xadj[i], &adjncy[xadj[i]], &loc);CHKERRQ(ierr); 30687407ba93SFlorian Wechsung /* If not, then just set the owner to the original owner (hopefully a rare event, it means that a vertex has been isolated) */ 3069cb87ef4cSFlorian Wechsung if (loc<0) { 307041525646SFlorian Wechsung part[i] = rank; 3071cb87ef4cSFlorian Wechsung } 3072cb87ef4cSFlorian Wechsung } 3073cb87ef4cSFlorian Wechsung 30747407ba93SFlorian Wechsung /* Let's see how significant the influences of the previous fixing up step was.*/ 30755dc86ac1SFlorian Wechsung if (viewer) { 3076125d2a8fSFlorian Wechsung ierr = PetscViewerASCIIPrintf(viewer, "After. ");CHKERRQ(ierr); 3077125d2a8fSFlorian Wechsung ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, part, viewer);CHKERRQ(ierr); 30787407ba93SFlorian Wechsung } 30797407ba93SFlorian Wechsung 30805dc86ac1SFlorian Wechsung ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 3081cb87ef4cSFlorian Wechsung ierr = PetscFree(xadj);CHKERRQ(ierr); 3082cb87ef4cSFlorian Wechsung ierr = PetscFree(adjncy);CHKERRQ(ierr); 308341525646SFlorian Wechsung ierr = PetscFree(vtxwgt);CHKERRQ(ierr); 3084cb87ef4cSFlorian Wechsung 30857f57c1a4SFlorian Wechsung /* Almost done, now rewrite the SF to reflect the new ownership. */ 3086cf818975SFlorian Wechsung { 30877f57c1a4SFlorian Wechsung PetscInt *pointsToRewrite; 308806b3913eSFlorian Wechsung ierr = PetscMalloc1(numNonExclusivelyOwned, &pointsToRewrite);CHKERRQ(ierr); 30897f57c1a4SFlorian Wechsung counter = 0; 3090cb87ef4cSFlorian Wechsung for(i=0; i<pEnd-pStart; i++) { 3091cb87ef4cSFlorian Wechsung if (toBalance[i]) { 3092cb87ef4cSFlorian Wechsung if (isNonExclusivelyOwned[i]) { 30937f57c1a4SFlorian Wechsung pointsToRewrite[counter] = i + pStart; 3094cb87ef4cSFlorian Wechsung counter++; 3095cb87ef4cSFlorian Wechsung } 3096cb87ef4cSFlorian Wechsung } 3097cb87ef4cSFlorian Wechsung } 30987f57c1a4SFlorian Wechsung ierr = DMPlexRewriteSF(dm, numNonExclusivelyOwned, pointsToRewrite, part+1, degrees);CHKERRQ(ierr); 30997f57c1a4SFlorian Wechsung ierr = PetscFree(pointsToRewrite);CHKERRQ(ierr); 3100cf818975SFlorian Wechsung } 3101cb87ef4cSFlorian Wechsung 3102cb87ef4cSFlorian Wechsung ierr = PetscFree(toBalance);CHKERRQ(ierr); 3103cb87ef4cSFlorian Wechsung ierr = PetscFree(isLeaf);CHKERRQ(ierr); 3104cf818975SFlorian Wechsung ierr = PetscFree(isNonExclusivelyOwned);CHKERRQ(ierr); 3105cf818975SFlorian Wechsung ierr = PetscFree(isExclusivelyOwned);CHKERRQ(ierr); 31067f57c1a4SFlorian Wechsung ierr = PetscFree(part);CHKERRQ(ierr); 31075dc86ac1SFlorian Wechsung if (viewer) { 310806b3913eSFlorian Wechsung ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 310906b3913eSFlorian Wechsung ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 31107d197802SFlorian Wechsung } 31118b879b41SFlorian Wechsung if (success) *success = PETSC_TRUE; 311241525646SFlorian Wechsung ierr = PetscLogEventEnd(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr); 3113240408d3SFlorian Wechsung #else 31145f441e9bSFlorian Wechsung SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis."); 311541525646SFlorian Wechsung #endif 3116cb87ef4cSFlorian Wechsung PetscFunctionReturn(0); 3117cb87ef4cSFlorian Wechsung } 3118