1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 270034214SMatthew G. Knepley 377623264SMatthew G. Knepley PetscClassId PETSCPARTITIONER_CLASSID = 0; 477623264SMatthew G. Knepley 577623264SMatthew G. Knepley PetscFunctionList PetscPartitionerList = NULL; 677623264SMatthew G. Knepley PetscBool PetscPartitionerRegisterAllCalled = PETSC_FALSE; 777623264SMatthew G. Knepley 877623264SMatthew G. Knepley PetscBool ChacoPartitionercite = PETSC_FALSE; 977623264SMatthew G. Knepley const char ChacoPartitionerCitation[] = "@inproceedings{Chaco95,\n" 1077623264SMatthew G. Knepley " author = {Bruce Hendrickson and Robert Leland},\n" 1177623264SMatthew G. Knepley " title = {A multilevel algorithm for partitioning graphs},\n" 1277623264SMatthew G. Knepley " booktitle = {Supercomputing '95: Proceedings of the 1995 ACM/IEEE Conference on Supercomputing (CDROM)}," 1377623264SMatthew G. Knepley " isbn = {0-89791-816-9},\n" 1477623264SMatthew G. Knepley " pages = {28},\n" 1577623264SMatthew G. Knepley " doi = {http://doi.acm.org/10.1145/224170.224228},\n" 1677623264SMatthew G. Knepley " publisher = {ACM Press},\n" 1777623264SMatthew G. Knepley " address = {New York},\n" 1877623264SMatthew G. Knepley " year = {1995}\n}\n"; 1977623264SMatthew G. Knepley 2077623264SMatthew G. Knepley PetscBool ParMetisPartitionercite = PETSC_FALSE; 2177623264SMatthew G. Knepley const char ParMetisPartitionerCitation[] = "@article{KarypisKumar98,\n" 2277623264SMatthew G. Knepley " author = {George Karypis and Vipin Kumar},\n" 2377623264SMatthew G. Knepley " title = {A Parallel Algorithm for Multilevel Graph Partitioning and Sparse Matrix Ordering},\n" 2477623264SMatthew G. Knepley " journal = {Journal of Parallel and Distributed Computing},\n" 2577623264SMatthew G. Knepley " volume = {48},\n" 2677623264SMatthew G. Knepley " pages = {71--85},\n" 2777623264SMatthew G. Knepley " year = {1998}\n}\n"; 2877623264SMatthew G. Knepley 29532c4e7dSMichael Lange /*@C 30532c4e7dSMichael Lange DMPlexCreatePartitionerGraph - Create a CSR graph of point connections for the partitioner 31532c4e7dSMichael Lange 32532c4e7dSMichael Lange Input Parameters: 33532c4e7dSMichael Lange + dm - The mesh DM dm 34532c4e7dSMichael Lange - height - Height of the strata from which to construct the graph 35532c4e7dSMichael Lange 36532c4e7dSMichael Lange Output Parameter: 37532c4e7dSMichael Lange + numVertices - Number of vertices in the graph 383fa7752dSToby Isaac . offsets - Point offsets in the graph 393fa7752dSToby Isaac . adjacency - Point connectivity in the graph 403fa7752dSToby 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. 41532c4e7dSMichael Lange 42532c4e7dSMichael Lange The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and 43532c4e7dSMichael Lange DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function 44532c4e7dSMichael Lange representation on the mesh. 45532c4e7dSMichael Lange 46532c4e7dSMichael Lange Level: developer 47532c4e7dSMichael Lange 48532c4e7dSMichael Lange .seealso: PetscPartitionerGetType(), PetscPartitionerCreate(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure() 49532c4e7dSMichael Lange @*/ 503fa7752dSToby Isaac PetscErrorCode DMPlexCreatePartitionerGraph(DM dm, PetscInt height, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency, IS *globalNumbering) 51532c4e7dSMichael Lange { 5243f7d02bSMichael Lange PetscInt p, pStart, pEnd, a, adjSize, idx, size, nroots; 53389e55d8SMichael Lange PetscInt *adj = NULL, *vOffsets = NULL, *graph = NULL; 548cfe4c1fSMichael Lange IS cellNumbering; 558cfe4c1fSMichael Lange const PetscInt *cellNum; 56532c4e7dSMichael Lange PetscBool useCone, useClosure; 57532c4e7dSMichael Lange PetscSection section; 58532c4e7dSMichael Lange PetscSegBuffer adjBuffer; 598cfe4c1fSMichael Lange PetscSF sfPoint; 60532c4e7dSMichael Lange PetscMPIInt rank; 61532c4e7dSMichael Lange PetscErrorCode ierr; 62532c4e7dSMichael Lange 63532c4e7dSMichael Lange PetscFunctionBegin; 64532c4e7dSMichael Lange ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 65532c4e7dSMichael Lange ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr); 668cfe4c1fSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 678cfe4c1fSMichael Lange ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr); 68532c4e7dSMichael Lange /* Build adjacency graph via a section/segbuffer */ 69532c4e7dSMichael Lange ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ion);CHKERRQ(ierr); 70532c4e7dSMichael Lange ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr); 71532c4e7dSMichael Lange ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr); 72532c4e7dSMichael Lange /* Always use FVM adjacency to create partitioner graph */ 73532c4e7dSMichael Lange ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr); 74532c4e7dSMichael Lange ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr); 75532c4e7dSMichael Lange ierr = DMPlexSetAdjacencyUseCone(dm, PETSC_TRUE);CHKERRQ(ierr); 76532c4e7dSMichael Lange ierr = DMPlexSetAdjacencyUseClosure(dm, PETSC_FALSE);CHKERRQ(ierr); 77f0927f4eSMatthew G. Knepley ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &cellNumbering);CHKERRQ(ierr); 783fa7752dSToby Isaac if (globalNumbering) { 793fa7752dSToby Isaac ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr); 803fa7752dSToby Isaac *globalNumbering = cellNumbering; 813fa7752dSToby Isaac } 828cfe4c1fSMichael Lange ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr); 838cfe4c1fSMichael Lange for (*numVertices = 0, p = pStart; p < pEnd; p++) { 848cfe4c1fSMichael Lange /* Skip non-owned cells in parallel (ParMetis expects no overlap) */ 858cfe4c1fSMichael Lange if (nroots > 0) {if (cellNum[p] < 0) continue;} 86532c4e7dSMichael Lange adjSize = PETSC_DETERMINE; 87532c4e7dSMichael Lange ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr); 88532c4e7dSMichael Lange for (a = 0; a < adjSize; ++a) { 89532c4e7dSMichael Lange const PetscInt point = adj[a]; 90532c4e7dSMichael Lange if (point != p && pStart <= point && point < pEnd) { 91532c4e7dSMichael Lange PetscInt *PETSC_RESTRICT pBuf; 92532c4e7dSMichael Lange ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr); 93532c4e7dSMichael Lange ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr); 94532c4e7dSMichael Lange *pBuf = point; 95532c4e7dSMichael Lange } 96532c4e7dSMichael Lange } 978cfe4c1fSMichael Lange (*numVertices)++; 98532c4e7dSMichael Lange } 99532c4e7dSMichael Lange ierr = DMPlexSetAdjacencyUseCone(dm, useCone);CHKERRQ(ierr); 100532c4e7dSMichael Lange ierr = DMPlexSetAdjacencyUseClosure(dm, useClosure);CHKERRQ(ierr); 101532c4e7dSMichael Lange /* Derive CSR graph from section/segbuffer */ 102532c4e7dSMichael Lange ierr = PetscSectionSetUp(section);CHKERRQ(ierr); 103532c4e7dSMichael Lange ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 104389e55d8SMichael Lange ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr); 10543f7d02bSMichael Lange for (idx = 0, p = pStart; p < pEnd; p++) { 10643f7d02bSMichael Lange if (nroots > 0) {if (cellNum[p] < 0) continue;} 10743f7d02bSMichael Lange ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr); 10843f7d02bSMichael Lange } 109532c4e7dSMichael Lange vOffsets[*numVertices] = size; 110532c4e7dSMichael Lange if (offsets) *offsets = vOffsets; 111389e55d8SMichael Lange ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr); 112bf4602e4SToby Isaac { 1138cfe4c1fSMichael Lange ISLocalToGlobalMapping ltogCells; 1148cfe4c1fSMichael Lange PetscInt n, size, *cells_arr; 1158cfe4c1fSMichael Lange /* In parallel, apply a global cell numbering to the graph */ 1168cfe4c1fSMichael Lange ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr); 1178cfe4c1fSMichael Lange ierr = ISLocalToGlobalMappingCreateIS(cellNumbering, <ogCells);CHKERRQ(ierr); 1188cfe4c1fSMichael Lange ierr = ISLocalToGlobalMappingGetSize(ltogCells, &size);CHKERRQ(ierr); 1198cfe4c1fSMichael Lange ierr = ISLocalToGlobalMappingGetIndices(ltogCells, (const PetscInt**)&cells_arr);CHKERRQ(ierr); 1208cfe4c1fSMichael Lange /* Convert to positive global cell numbers */ 1218cfe4c1fSMichael Lange for (n=0; n<size; n++) {if (cells_arr[n] < 0) cells_arr[n] = -(cells_arr[n]+1);} 1228cfe4c1fSMichael Lange ierr = ISLocalToGlobalMappingRestoreIndices(ltogCells, (const PetscInt**)&cells_arr);CHKERRQ(ierr); 123389e55d8SMichael Lange ierr = ISLocalToGlobalMappingApplyBlock(ltogCells, vOffsets[*numVertices], graph, graph);CHKERRQ(ierr); 1248cfe4c1fSMichael Lange ierr = ISLocalToGlobalMappingDestroy(<ogCells);CHKERRQ(ierr); 125f0927f4eSMatthew G. Knepley ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr); 1268cfe4c1fSMichael Lange } 127389e55d8SMichael Lange if (adjacency) *adjacency = graph; 128532c4e7dSMichael Lange /* Clean up */ 129532c4e7dSMichael Lange ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr); 130532c4e7dSMichael Lange ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); 131532c4e7dSMichael Lange ierr = PetscFree(adj);CHKERRQ(ierr); 132532c4e7dSMichael Lange PetscFunctionReturn(0); 133532c4e7dSMichael Lange } 134532c4e7dSMichael Lange 135d5577e40SMatthew G. Knepley /*@C 136d5577e40SMatthew G. Knepley DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format. 137d5577e40SMatthew G. Knepley 138d5577e40SMatthew G. Knepley Collective 139d5577e40SMatthew G. Knepley 140d5577e40SMatthew G. Knepley Input Arguments: 141d5577e40SMatthew G. Knepley + dm - The DMPlex 142d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0) 143d5577e40SMatthew G. Knepley 144d5577e40SMatthew G. Knepley Output Arguments: 145d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh. 146d5577e40SMatthew G. Knepley . offsets - The offset to the adjacency list for each cell 147d5577e40SMatthew G. Knepley - adjacency - The adjacency list for all cells 148d5577e40SMatthew G. Knepley 149d5577e40SMatthew G. Knepley Note: This is suitable for input to a mesh partitioner like ParMetis. 150d5577e40SMatthew G. Knepley 151d5577e40SMatthew G. Knepley Level: advanced 152d5577e40SMatthew G. Knepley 153d5577e40SMatthew G. Knepley .seealso: DMPlexCreate() 154d5577e40SMatthew G. Knepley @*/ 15570034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency) 15670034214SMatthew G. Knepley { 15770034214SMatthew G. Knepley const PetscInt maxFaceCases = 30; 15870034214SMatthew G. Knepley PetscInt numFaceCases = 0; 15970034214SMatthew G. Knepley PetscInt numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */ 16070034214SMatthew G. Knepley PetscInt *off, *adj; 16170034214SMatthew G. Knepley PetscInt *neighborCells = NULL; 16270034214SMatthew G. Knepley PetscInt dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell; 16370034214SMatthew G. Knepley PetscErrorCode ierr; 16470034214SMatthew G. Knepley 16570034214SMatthew G. Knepley PetscFunctionBegin; 16670034214SMatthew G. Knepley /* For parallel partitioning, I think you have to communicate supports */ 167c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 16870034214SMatthew G. Knepley cellDim = dim - cellHeight; 16970034214SMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 17070034214SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 17170034214SMatthew G. Knepley if (cEnd - cStart == 0) { 17270034214SMatthew G. Knepley if (numVertices) *numVertices = 0; 17370034214SMatthew G. Knepley if (offsets) *offsets = NULL; 17470034214SMatthew G. Knepley if (adjacency) *adjacency = NULL; 17570034214SMatthew G. Knepley PetscFunctionReturn(0); 17670034214SMatthew G. Knepley } 17770034214SMatthew G. Knepley numCells = cEnd - cStart; 17870034214SMatthew G. Knepley faceDepth = depth - cellHeight; 17970034214SMatthew G. Knepley if (dim == depth) { 18070034214SMatthew G. Knepley PetscInt f, fStart, fEnd; 18170034214SMatthew G. Knepley 18270034214SMatthew G. Knepley ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr); 18370034214SMatthew G. Knepley /* Count neighboring cells */ 18470034214SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr); 18570034214SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 18670034214SMatthew G. Knepley const PetscInt *support; 18770034214SMatthew G. Knepley PetscInt supportSize; 18870034214SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 18970034214SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr); 19070034214SMatthew G. Knepley if (supportSize == 2) { 1919ffc88e4SToby Isaac PetscInt numChildren; 1929ffc88e4SToby Isaac 1939ffc88e4SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 1949ffc88e4SToby Isaac if (!numChildren) { 19570034214SMatthew G. Knepley ++off[support[0]-cStart+1]; 19670034214SMatthew G. Knepley ++off[support[1]-cStart+1]; 19770034214SMatthew G. Knepley } 19870034214SMatthew G. Knepley } 1999ffc88e4SToby Isaac } 20070034214SMatthew G. Knepley /* Prefix sum */ 20170034214SMatthew G. Knepley for (c = 1; c <= numCells; ++c) off[c] += off[c-1]; 20270034214SMatthew G. Knepley if (adjacency) { 20370034214SMatthew G. Knepley PetscInt *tmp; 20470034214SMatthew G. Knepley 20570034214SMatthew G. Knepley ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr); 206854ce69bSBarry Smith ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr); 20770034214SMatthew G. Knepley ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr); 20870034214SMatthew G. Knepley /* Get neighboring cells */ 20970034214SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 21070034214SMatthew G. Knepley const PetscInt *support; 21170034214SMatthew G. Knepley PetscInt supportSize; 21270034214SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 21370034214SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr); 21470034214SMatthew G. Knepley if (supportSize == 2) { 2159ffc88e4SToby Isaac PetscInt numChildren; 2169ffc88e4SToby Isaac 2179ffc88e4SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 2189ffc88e4SToby Isaac if (!numChildren) { 21970034214SMatthew G. Knepley adj[tmp[support[0]-cStart]++] = support[1]; 22070034214SMatthew G. Knepley adj[tmp[support[1]-cStart]++] = support[0]; 22170034214SMatthew G. Knepley } 22270034214SMatthew G. Knepley } 2239ffc88e4SToby Isaac } 22470034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 22570034214SMatthew 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); 22670034214SMatthew G. Knepley #endif 22770034214SMatthew G. Knepley ierr = PetscFree(tmp);CHKERRQ(ierr); 22870034214SMatthew G. Knepley } 22970034214SMatthew G. Knepley if (numVertices) *numVertices = numCells; 23070034214SMatthew G. Knepley if (offsets) *offsets = off; 23170034214SMatthew G. Knepley if (adjacency) *adjacency = adj; 23270034214SMatthew G. Knepley PetscFunctionReturn(0); 23370034214SMatthew G. Knepley } 23470034214SMatthew G. Knepley /* Setup face recognition */ 23570034214SMatthew G. Knepley if (faceDepth == 1) { 23670034214SMatthew 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 */ 23770034214SMatthew G. Knepley 23870034214SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 23970034214SMatthew G. Knepley PetscInt corners; 24070034214SMatthew G. Knepley 24170034214SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr); 24270034214SMatthew G. Knepley if (!cornersSeen[corners]) { 24370034214SMatthew G. Knepley PetscInt nFV; 24470034214SMatthew G. Knepley 24570034214SMatthew G. Knepley if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases"); 24670034214SMatthew G. Knepley cornersSeen[corners] = 1; 24770034214SMatthew G. Knepley 24870034214SMatthew G. Knepley ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr); 24970034214SMatthew G. Knepley 25070034214SMatthew G. Knepley numFaceVertices[numFaceCases++] = nFV; 25170034214SMatthew G. Knepley } 25270034214SMatthew G. Knepley } 25370034214SMatthew G. Knepley } 25470034214SMatthew G. Knepley ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr); 25570034214SMatthew G. Knepley /* Count neighboring cells */ 25670034214SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 25770034214SMatthew G. Knepley PetscInt numNeighbors = PETSC_DETERMINE, n; 25870034214SMatthew G. Knepley 2598b0b4c70SToby Isaac ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr); 26070034214SMatthew G. Knepley /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */ 26170034214SMatthew G. Knepley for (n = 0; n < numNeighbors; ++n) { 26270034214SMatthew G. Knepley PetscInt cellPair[2]; 26370034214SMatthew G. Knepley PetscBool found = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE; 26470034214SMatthew G. Knepley PetscInt meetSize = 0; 26570034214SMatthew G. Knepley const PetscInt *meet = NULL; 26670034214SMatthew G. Knepley 26770034214SMatthew G. Knepley cellPair[0] = cell; cellPair[1] = neighborCells[n]; 26870034214SMatthew G. Knepley if (cellPair[0] == cellPair[1]) continue; 26970034214SMatthew G. Knepley if (!found) { 27070034214SMatthew G. Knepley ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 27170034214SMatthew G. Knepley if (meetSize) { 27270034214SMatthew G. Knepley PetscInt f; 27370034214SMatthew G. Knepley 27470034214SMatthew G. Knepley for (f = 0; f < numFaceCases; ++f) { 27570034214SMatthew G. Knepley if (numFaceVertices[f] == meetSize) { 27670034214SMatthew G. Knepley found = PETSC_TRUE; 27770034214SMatthew G. Knepley break; 27870034214SMatthew G. Knepley } 27970034214SMatthew G. Knepley } 28070034214SMatthew G. Knepley } 28170034214SMatthew G. Knepley ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 28270034214SMatthew G. Knepley } 28370034214SMatthew G. Knepley if (found) ++off[cell-cStart+1]; 28470034214SMatthew G. Knepley } 28570034214SMatthew G. Knepley } 28670034214SMatthew G. Knepley /* Prefix sum */ 28770034214SMatthew G. Knepley for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1]; 28870034214SMatthew G. Knepley 28970034214SMatthew G. Knepley if (adjacency) { 29070034214SMatthew G. Knepley ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr); 29170034214SMatthew G. Knepley /* Get neighboring cells */ 29270034214SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 29370034214SMatthew G. Knepley PetscInt numNeighbors = PETSC_DETERMINE, n; 29470034214SMatthew G. Knepley PetscInt cellOffset = 0; 29570034214SMatthew G. Knepley 2968b0b4c70SToby Isaac ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr); 29770034214SMatthew G. Knepley /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */ 29870034214SMatthew G. Knepley for (n = 0; n < numNeighbors; ++n) { 29970034214SMatthew G. Knepley PetscInt cellPair[2]; 30070034214SMatthew G. Knepley PetscBool found = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE; 30170034214SMatthew G. Knepley PetscInt meetSize = 0; 30270034214SMatthew G. Knepley const PetscInt *meet = NULL; 30370034214SMatthew G. Knepley 30470034214SMatthew G. Knepley cellPair[0] = cell; cellPair[1] = neighborCells[n]; 30570034214SMatthew G. Knepley if (cellPair[0] == cellPair[1]) continue; 30670034214SMatthew G. Knepley if (!found) { 30770034214SMatthew G. Knepley ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 30870034214SMatthew G. Knepley if (meetSize) { 30970034214SMatthew G. Knepley PetscInt f; 31070034214SMatthew G. Knepley 31170034214SMatthew G. Knepley for (f = 0; f < numFaceCases; ++f) { 31270034214SMatthew G. Knepley if (numFaceVertices[f] == meetSize) { 31370034214SMatthew G. Knepley found = PETSC_TRUE; 31470034214SMatthew G. Knepley break; 31570034214SMatthew G. Knepley } 31670034214SMatthew G. Knepley } 31770034214SMatthew G. Knepley } 31870034214SMatthew G. Knepley ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr); 31970034214SMatthew G. Knepley } 32070034214SMatthew G. Knepley if (found) { 32170034214SMatthew G. Knepley adj[off[cell-cStart]+cellOffset] = neighborCells[n]; 32270034214SMatthew G. Knepley ++cellOffset; 32370034214SMatthew G. Knepley } 32470034214SMatthew G. Knepley } 32570034214SMatthew G. Knepley } 32670034214SMatthew G. Knepley } 32770034214SMatthew G. Knepley ierr = PetscFree(neighborCells);CHKERRQ(ierr); 32870034214SMatthew G. Knepley if (numVertices) *numVertices = numCells; 32970034214SMatthew G. Knepley if (offsets) *offsets = off; 33070034214SMatthew G. Knepley if (adjacency) *adjacency = adj; 33170034214SMatthew G. Knepley PetscFunctionReturn(0); 33270034214SMatthew G. Knepley } 33370034214SMatthew G. Knepley 33477623264SMatthew G. Knepley /*@C 33577623264SMatthew G. Knepley PetscPartitionerRegister - Adds a new PetscPartitioner implementation 33677623264SMatthew G. Knepley 33777623264SMatthew G. Knepley Not Collective 33877623264SMatthew G. Knepley 33977623264SMatthew G. Knepley Input Parameters: 34077623264SMatthew G. Knepley + name - The name of a new user-defined creation routine 34177623264SMatthew G. Knepley - create_func - The creation routine itself 34277623264SMatthew G. Knepley 34377623264SMatthew G. Knepley Notes: 34477623264SMatthew G. Knepley PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners 34577623264SMatthew G. Knepley 34677623264SMatthew G. Knepley Sample usage: 34777623264SMatthew G. Knepley .vb 34877623264SMatthew G. Knepley PetscPartitionerRegister("my_part", MyPetscPartitionerCreate); 34977623264SMatthew G. Knepley .ve 35077623264SMatthew G. Knepley 35177623264SMatthew G. Knepley Then, your PetscPartitioner type can be chosen with the procedural interface via 35277623264SMatthew G. Knepley .vb 35377623264SMatthew G. Knepley PetscPartitionerCreate(MPI_Comm, PetscPartitioner *); 35477623264SMatthew G. Knepley PetscPartitionerSetType(PetscPartitioner, "my_part"); 35577623264SMatthew G. Knepley .ve 35677623264SMatthew G. Knepley or at runtime via the option 35777623264SMatthew G. Knepley .vb 35877623264SMatthew G. Knepley -petscpartitioner_type my_part 35977623264SMatthew G. Knepley .ve 36077623264SMatthew G. Knepley 36177623264SMatthew G. Knepley Level: advanced 36277623264SMatthew G. Knepley 36377623264SMatthew G. Knepley .keywords: PetscPartitioner, register 36477623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy() 36577623264SMatthew G. Knepley 36677623264SMatthew G. Knepley @*/ 36777623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner)) 36877623264SMatthew G. Knepley { 36977623264SMatthew G. Knepley PetscErrorCode ierr; 37077623264SMatthew G. Knepley 37177623264SMatthew G. Knepley PetscFunctionBegin; 37277623264SMatthew G. Knepley ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr); 37377623264SMatthew G. Knepley PetscFunctionReturn(0); 37477623264SMatthew G. Knepley } 37577623264SMatthew G. Knepley 37677623264SMatthew G. Knepley /*@C 37777623264SMatthew G. Knepley PetscPartitionerSetType - Builds a particular PetscPartitioner 37877623264SMatthew G. Knepley 37977623264SMatthew G. Knepley Collective on PetscPartitioner 38077623264SMatthew G. Knepley 38177623264SMatthew G. Knepley Input Parameters: 38277623264SMatthew G. Knepley + part - The PetscPartitioner object 38377623264SMatthew G. Knepley - name - The kind of partitioner 38477623264SMatthew G. Knepley 38577623264SMatthew G. Knepley Options Database Key: 38677623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types 38777623264SMatthew G. Knepley 38877623264SMatthew G. Knepley Level: intermediate 38977623264SMatthew G. Knepley 39077623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type 39177623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate() 39277623264SMatthew G. Knepley @*/ 39377623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name) 39477623264SMatthew G. Knepley { 39577623264SMatthew G. Knepley PetscErrorCode (*r)(PetscPartitioner); 39677623264SMatthew G. Knepley PetscBool match; 39777623264SMatthew G. Knepley PetscErrorCode ierr; 39877623264SMatthew G. Knepley 39977623264SMatthew G. Knepley PetscFunctionBegin; 40077623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 40177623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr); 40277623264SMatthew G. Knepley if (match) PetscFunctionReturn(0); 40377623264SMatthew G. Knepley 4040f51fdf8SToby Isaac ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 40577623264SMatthew G. Knepley ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr); 40677623264SMatthew G. Knepley if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name); 40777623264SMatthew G. Knepley 40877623264SMatthew G. Knepley if (part->ops->destroy) { 40977623264SMatthew G. Knepley ierr = (*part->ops->destroy)(part);CHKERRQ(ierr); 41077623264SMatthew G. Knepley part->ops->destroy = NULL; 41177623264SMatthew G. Knepley } 41277623264SMatthew G. Knepley ierr = (*r)(part);CHKERRQ(ierr); 41377623264SMatthew G. Knepley ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr); 41477623264SMatthew G. Knepley PetscFunctionReturn(0); 41577623264SMatthew G. Knepley } 41677623264SMatthew G. Knepley 41777623264SMatthew G. Knepley /*@C 41877623264SMatthew G. Knepley PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object. 41977623264SMatthew G. Knepley 42077623264SMatthew G. Knepley Not Collective 42177623264SMatthew G. Knepley 42277623264SMatthew G. Knepley Input Parameter: 42377623264SMatthew G. Knepley . part - The PetscPartitioner 42477623264SMatthew G. Knepley 42577623264SMatthew G. Knepley Output Parameter: 42677623264SMatthew G. Knepley . name - The PetscPartitioner type name 42777623264SMatthew G. Knepley 42877623264SMatthew G. Knepley Level: intermediate 42977623264SMatthew G. Knepley 43077623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name 43177623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate() 43277623264SMatthew G. Knepley @*/ 43377623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name) 43477623264SMatthew G. Knepley { 43577623264SMatthew G. Knepley PetscErrorCode ierr; 43677623264SMatthew G. Knepley 43777623264SMatthew G. Knepley PetscFunctionBegin; 43877623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 43977623264SMatthew G. Knepley PetscValidPointer(name, 2); 4400f51fdf8SToby Isaac ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 44177623264SMatthew G. Knepley *name = ((PetscObject) part)->type_name; 44277623264SMatthew G. Knepley PetscFunctionReturn(0); 44377623264SMatthew G. Knepley } 44477623264SMatthew G. Knepley 44577623264SMatthew G. Knepley /*@C 44677623264SMatthew G. Knepley PetscPartitionerView - Views a PetscPartitioner 44777623264SMatthew G. Knepley 44877623264SMatthew G. Knepley Collective on PetscPartitioner 44977623264SMatthew G. Knepley 45077623264SMatthew G. Knepley Input Parameter: 45177623264SMatthew G. Knepley + part - the PetscPartitioner object to view 45277623264SMatthew G. Knepley - v - the viewer 45377623264SMatthew G. Knepley 45477623264SMatthew G. Knepley Level: developer 45577623264SMatthew G. Knepley 45677623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy() 45777623264SMatthew G. Knepley @*/ 45877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v) 45977623264SMatthew G. Knepley { 46077623264SMatthew G. Knepley PetscErrorCode ierr; 46177623264SMatthew G. Knepley 46277623264SMatthew G. Knepley PetscFunctionBegin; 46377623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 46477623264SMatthew G. Knepley if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);} 46577623264SMatthew G. Knepley if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);} 46677623264SMatthew G. Knepley PetscFunctionReturn(0); 46777623264SMatthew G. Knepley } 46877623264SMatthew G. Knepley 469a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType) 470a0058e54SToby Isaac { 471a0058e54SToby Isaac PetscFunctionBegin; 472a0058e54SToby Isaac if (!currentType) { 473a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO) 474a0058e54SToby Isaac *defaultType = PETSCPARTITIONERCHACO; 475a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS) 476a0058e54SToby Isaac *defaultType = PETSCPARTITIONERPARMETIS; 477*137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH) 478*137cd93aSLisandro Dalcin *defaultType = PETSCPARTITIONERPTSCOTCH; 479a0058e54SToby Isaac #else 480a0058e54SToby Isaac *defaultType = PETSCPARTITIONERSIMPLE; 481a0058e54SToby Isaac #endif 482a0058e54SToby Isaac } else { 483a0058e54SToby Isaac *defaultType = currentType; 484a0058e54SToby Isaac } 485a0058e54SToby Isaac PetscFunctionReturn(0); 486a0058e54SToby Isaac } 487a0058e54SToby Isaac 48877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetTypeFromOptions_Internal(PetscPartitioner part) 48977623264SMatthew G. Knepley { 49077623264SMatthew G. Knepley const char *defaultType; 49177623264SMatthew G. Knepley char name[256]; 49277623264SMatthew G. Knepley PetscBool flg; 49377623264SMatthew G. Knepley PetscErrorCode ierr; 49477623264SMatthew G. Knepley 49577623264SMatthew G. Knepley PetscFunctionBegin; 49677623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 497a0058e54SToby Isaac ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr); 4980f51fdf8SToby Isaac ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr); 49977623264SMatthew G. Knepley 50077623264SMatthew G. Knepley ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr); 50177623264SMatthew G. Knepley ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, 256, &flg);CHKERRQ(ierr); 50277623264SMatthew G. Knepley if (flg) { 50377623264SMatthew G. Knepley ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr); 50477623264SMatthew G. Knepley } else if (!((PetscObject) part)->type_name) { 50577623264SMatthew G. Knepley ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr); 50677623264SMatthew G. Knepley } 50777623264SMatthew G. Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 50877623264SMatthew G. Knepley PetscFunctionReturn(0); 50977623264SMatthew G. Knepley } 51077623264SMatthew G. Knepley 51177623264SMatthew G. Knepley /*@ 51277623264SMatthew G. Knepley PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database 51377623264SMatthew G. Knepley 51477623264SMatthew G. Knepley Collective on PetscPartitioner 51577623264SMatthew G. Knepley 51677623264SMatthew G. Knepley Input Parameter: 51777623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for 51877623264SMatthew G. Knepley 51977623264SMatthew G. Knepley Level: developer 52077623264SMatthew G. Knepley 52177623264SMatthew G. Knepley .seealso: PetscPartitionerView() 52277623264SMatthew G. Knepley @*/ 52377623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part) 52477623264SMatthew G. Knepley { 52577623264SMatthew G. Knepley PetscErrorCode ierr; 52677623264SMatthew G. Knepley 52777623264SMatthew G. Knepley PetscFunctionBegin; 52877623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 52977623264SMatthew G. Knepley ierr = PetscPartitionerSetTypeFromOptions_Internal(part);CHKERRQ(ierr); 53077623264SMatthew G. Knepley 53177623264SMatthew G. Knepley ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr); 532077101c0SMatthew G. Knepley if (part->ops->setfromoptions) {ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr);} 53377623264SMatthew G. Knepley /* process any options handlers added with PetscObjectAddOptionsHandler() */ 5340633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr); 53577623264SMatthew G. Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 53677623264SMatthew G. Knepley ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr); 53777623264SMatthew G. Knepley PetscFunctionReturn(0); 53877623264SMatthew G. Knepley } 53977623264SMatthew G. Knepley 54077623264SMatthew G. Knepley /*@C 54177623264SMatthew G. Knepley PetscPartitionerSetUp - Construct data structures for the PetscPartitioner 54277623264SMatthew G. Knepley 54377623264SMatthew G. Knepley Collective on PetscPartitioner 54477623264SMatthew G. Knepley 54577623264SMatthew G. Knepley Input Parameter: 54677623264SMatthew G. Knepley . part - the PetscPartitioner object to setup 54777623264SMatthew G. Knepley 54877623264SMatthew G. Knepley Level: developer 54977623264SMatthew G. Knepley 55077623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy() 55177623264SMatthew G. Knepley @*/ 55277623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part) 55377623264SMatthew G. Knepley { 55477623264SMatthew G. Knepley PetscErrorCode ierr; 55577623264SMatthew G. Knepley 55677623264SMatthew G. Knepley PetscFunctionBegin; 55777623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 55877623264SMatthew G. Knepley if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);} 55977623264SMatthew G. Knepley PetscFunctionReturn(0); 56077623264SMatthew G. Knepley } 56177623264SMatthew G. Knepley 56277623264SMatthew G. Knepley /*@ 56377623264SMatthew G. Knepley PetscPartitionerDestroy - Destroys a PetscPartitioner object 56477623264SMatthew G. Knepley 56577623264SMatthew G. Knepley Collective on PetscPartitioner 56677623264SMatthew G. Knepley 56777623264SMatthew G. Knepley Input Parameter: 56877623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy 56977623264SMatthew G. Knepley 57077623264SMatthew G. Knepley Level: developer 57177623264SMatthew G. Knepley 57277623264SMatthew G. Knepley .seealso: PetscPartitionerView() 57377623264SMatthew G. Knepley @*/ 57477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part) 57577623264SMatthew G. Knepley { 57677623264SMatthew G. Knepley PetscErrorCode ierr; 57777623264SMatthew G. Knepley 57877623264SMatthew G. Knepley PetscFunctionBegin; 57977623264SMatthew G. Knepley if (!*part) PetscFunctionReturn(0); 58077623264SMatthew G. Knepley PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1); 58177623264SMatthew G. Knepley 58277623264SMatthew G. Knepley if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);} 58377623264SMatthew G. Knepley ((PetscObject) (*part))->refct = 0; 58477623264SMatthew G. Knepley 58577623264SMatthew G. Knepley if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);} 58677623264SMatthew G. Knepley ierr = PetscHeaderDestroy(part);CHKERRQ(ierr); 58777623264SMatthew G. Knepley PetscFunctionReturn(0); 58877623264SMatthew G. Knepley } 58977623264SMatthew G. Knepley 59077623264SMatthew G. Knepley /*@ 59177623264SMatthew G. Knepley PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType(). 59277623264SMatthew G. Knepley 59377623264SMatthew G. Knepley Collective on MPI_Comm 59477623264SMatthew G. Knepley 59577623264SMatthew G. Knepley Input Parameter: 59677623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object 59777623264SMatthew G. Knepley 59877623264SMatthew G. Knepley Output Parameter: 59977623264SMatthew G. Knepley . part - The PetscPartitioner object 60077623264SMatthew G. Knepley 60177623264SMatthew G. Knepley Level: beginner 60277623264SMatthew G. Knepley 603dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER 60477623264SMatthew G. Knepley @*/ 60577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part) 60677623264SMatthew G. Knepley { 60777623264SMatthew G. Knepley PetscPartitioner p; 608a0058e54SToby Isaac const char *partitionerType = NULL; 60977623264SMatthew G. Knepley PetscErrorCode ierr; 61077623264SMatthew G. Knepley 61177623264SMatthew G. Knepley PetscFunctionBegin; 61277623264SMatthew G. Knepley PetscValidPointer(part, 2); 61377623264SMatthew G. Knepley *part = NULL; 61483cde681SMatthew G. Knepley ierr = DMInitializePackage();CHKERRQ(ierr); 61577623264SMatthew G. Knepley 61673107ff1SLisandro Dalcin ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr); 617a0058e54SToby Isaac ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr); 618a0058e54SToby Isaac ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr); 61977623264SMatthew G. Knepley 62077623264SMatthew G. Knepley *part = p; 62177623264SMatthew G. Knepley PetscFunctionReturn(0); 62277623264SMatthew G. Knepley } 62377623264SMatthew G. Knepley 62477623264SMatthew G. Knepley /*@ 62577623264SMatthew G. Knepley PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh 62677623264SMatthew G. Knepley 62777623264SMatthew G. Knepley Collective on DM 62877623264SMatthew G. Knepley 62977623264SMatthew G. Knepley Input Parameters: 63077623264SMatthew G. Knepley + part - The PetscPartitioner 631f8987ae8SMichael Lange - dm - The mesh DM 63277623264SMatthew G. Knepley 63377623264SMatthew G. Knepley Output Parameters: 63477623264SMatthew G. Knepley + partSection - The PetscSection giving the division of points by partition 635f8987ae8SMichael Lange - partition - The list of points by partition 63677623264SMatthew G. Knepley 63777623264SMatthew G. Knepley Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight() 63877623264SMatthew G. Knepley 63977623264SMatthew G. Knepley Level: developer 64077623264SMatthew G. Knepley 64177623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate() 6424b15ede2SMatthew G. Knepley @*/ 643f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition) 64477623264SMatthew G. Knepley { 64577623264SMatthew G. Knepley PetscMPIInt size; 64677623264SMatthew G. Knepley PetscErrorCode ierr; 64777623264SMatthew G. Knepley 64877623264SMatthew G. Knepley PetscFunctionBegin; 64977623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 65077623264SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 65177623264SMatthew G. Knepley PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4); 65277623264SMatthew G. Knepley PetscValidPointer(partition, 5); 65377623264SMatthew G. Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr); 65477623264SMatthew G. Knepley if (size == 1) { 65577623264SMatthew G. Knepley PetscInt *points; 65677623264SMatthew G. Knepley PetscInt cStart, cEnd, c; 65777623264SMatthew G. Knepley 65877623264SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr); 65977623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr); 66077623264SMatthew G. Knepley ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr); 66177623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 66277623264SMatthew G. Knepley ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr); 66377623264SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) points[c] = c; 66477623264SMatthew G. Knepley ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 66577623264SMatthew G. Knepley PetscFunctionReturn(0); 66677623264SMatthew G. Knepley } 66777623264SMatthew G. Knepley if (part->height == 0) { 66877623264SMatthew G. Knepley PetscInt numVertices; 66977623264SMatthew G. Knepley PetscInt *start = NULL; 67077623264SMatthew G. Knepley PetscInt *adjacency = NULL; 6713fa7752dSToby Isaac IS globalNumbering; 67277623264SMatthew G. Knepley 6733fa7752dSToby Isaac ierr = DMPlexCreatePartitionerGraph(dm, 0, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr); 67477623264SMatthew G. Knepley if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type"); 67577623264SMatthew G. Knepley ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr); 67677623264SMatthew G. Knepley ierr = PetscFree(start);CHKERRQ(ierr); 67777623264SMatthew G. Knepley ierr = PetscFree(adjacency);CHKERRQ(ierr); 6783fa7752dSToby Isaac if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */ 6793fa7752dSToby Isaac const PetscInt *globalNum; 6803fa7752dSToby Isaac const PetscInt *partIdx; 6813fa7752dSToby Isaac PetscInt *map, cStart, cEnd; 6823fa7752dSToby Isaac PetscInt *adjusted, i, localSize, offset; 6833fa7752dSToby Isaac IS newPartition; 6843fa7752dSToby Isaac 6853fa7752dSToby Isaac ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr); 6863fa7752dSToby Isaac ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr); 6873fa7752dSToby Isaac ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr); 6883fa7752dSToby Isaac ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr); 6893fa7752dSToby Isaac ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr); 6903fa7752dSToby Isaac ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr); 6913fa7752dSToby Isaac for (i = cStart, offset = 0; i < cEnd; i++) { 6923fa7752dSToby Isaac if (globalNum[i - cStart] >= 0) map[offset++] = i; 6933fa7752dSToby Isaac } 6943fa7752dSToby Isaac for (i = 0; i < localSize; i++) { 6953fa7752dSToby Isaac adjusted[i] = map[partIdx[i]]; 6963fa7752dSToby Isaac } 6973fa7752dSToby Isaac ierr = PetscFree(map);CHKERRQ(ierr); 6983fa7752dSToby Isaac ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr); 6993fa7752dSToby Isaac ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr); 7003fa7752dSToby Isaac ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr); 7013fa7752dSToby Isaac ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr); 7023fa7752dSToby Isaac ierr = ISDestroy(partition);CHKERRQ(ierr); 7033fa7752dSToby Isaac *partition = newPartition; 7043fa7752dSToby Isaac } 70577623264SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height); 70677623264SMatthew G. Knepley PetscFunctionReturn(0); 7073fa7752dSToby Isaac 70877623264SMatthew G. Knepley } 70977623264SMatthew G. Knepley 710d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part) 71177623264SMatthew G. Knepley { 71277623264SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 71377623264SMatthew G. Knepley PetscErrorCode ierr; 71477623264SMatthew G. Knepley 71577623264SMatthew G. Knepley PetscFunctionBegin; 71677623264SMatthew G. Knepley ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr); 71777623264SMatthew G. Knepley ierr = ISDestroy(&p->partition);CHKERRQ(ierr); 71877623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 71977623264SMatthew G. Knepley PetscFunctionReturn(0); 72077623264SMatthew G. Knepley } 72177623264SMatthew G. Knepley 722d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer) 72377623264SMatthew G. Knepley { 724077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 72577623264SMatthew G. Knepley PetscViewerFormat format; 72677623264SMatthew G. Knepley PetscErrorCode ierr; 72777623264SMatthew G. Knepley 72877623264SMatthew G. Knepley PetscFunctionBegin; 72977623264SMatthew G. Knepley ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 73077623264SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "Shell Graph Partitioner:\n");CHKERRQ(ierr); 731077101c0SMatthew G. Knepley if (p->random) { 732077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 733077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr); 734077101c0SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 735077101c0SMatthew G. Knepley } 73677623264SMatthew G. Knepley PetscFunctionReturn(0); 73777623264SMatthew G. Knepley } 73877623264SMatthew G. Knepley 739d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer) 74077623264SMatthew G. Knepley { 74177623264SMatthew G. Knepley PetscBool iascii; 74277623264SMatthew G. Knepley PetscErrorCode ierr; 74377623264SMatthew G. Knepley 74477623264SMatthew G. Knepley PetscFunctionBegin; 74577623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 74677623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 74777623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 74877623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);} 74977623264SMatthew G. Knepley PetscFunctionReturn(0); 75077623264SMatthew G. Knepley } 75177623264SMatthew G. Knepley 752d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 753077101c0SMatthew G. Knepley { 754077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 755077101c0SMatthew G. Knepley PetscErrorCode ierr; 756077101c0SMatthew G. Knepley 757077101c0SMatthew G. Knepley PetscFunctionBegin; 758077101c0SMatthew G. Knepley ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitionerShell Options");CHKERRQ(ierr); 759077101c0SMatthew G. Knepley ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr); 760077101c0SMatthew G. Knepley ierr = PetscOptionsTail();CHKERRQ(ierr); 761077101c0SMatthew G. Knepley PetscFunctionReturn(0); 762077101c0SMatthew G. Knepley } 763077101c0SMatthew G. Knepley 764d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 76577623264SMatthew G. Knepley { 76677623264SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 76777623264SMatthew G. Knepley PetscInt np; 76877623264SMatthew G. Knepley PetscErrorCode ierr; 76977623264SMatthew G. Knepley 77077623264SMatthew G. Knepley PetscFunctionBegin; 771077101c0SMatthew G. Knepley if (p->random) { 772077101c0SMatthew G. Knepley PetscRandom r; 773aa1d5631SMatthew G. Knepley PetscInt *sizes, *points, v, p; 774aa1d5631SMatthew G. Knepley PetscMPIInt rank; 775077101c0SMatthew G. Knepley 776aa1d5631SMatthew G. Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 777077101c0SMatthew G. Knepley ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 778c717d290SMatthew G. Knepley ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr); 779077101c0SMatthew G. Knepley ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr); 780077101c0SMatthew G. Knepley ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr); 781aa1d5631SMatthew G. Knepley for (v = 0; v < numVertices; ++v) {points[v] = v;} 782ac9a96f1SMichael Lange for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);} 783aa1d5631SMatthew G. Knepley for (v = numVertices-1; v > 0; --v) { 784077101c0SMatthew G. Knepley PetscReal val; 785aa1d5631SMatthew G. Knepley PetscInt w, tmp; 786077101c0SMatthew G. Knepley 787aa1d5631SMatthew G. Knepley ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr); 788077101c0SMatthew G. Knepley ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr); 789aa1d5631SMatthew G. Knepley w = PetscFloorReal(val); 790aa1d5631SMatthew G. Knepley tmp = points[v]; 791aa1d5631SMatthew G. Knepley points[v] = points[w]; 792aa1d5631SMatthew G. Knepley points[w] = tmp; 793077101c0SMatthew G. Knepley } 794077101c0SMatthew G. Knepley ierr = PetscRandomDestroy(&r);CHKERRQ(ierr); 795077101c0SMatthew G. Knepley ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr); 796077101c0SMatthew G. Knepley ierr = PetscFree2(sizes, points);CHKERRQ(ierr); 797077101c0SMatthew G. Knepley } 798c717d290SMatthew G. Knepley if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()"); 79977623264SMatthew G. Knepley ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr); 80077623264SMatthew G. Knepley if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np); 80177623264SMatthew G. Knepley ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr); 80277623264SMatthew G. Knepley if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np); 8035680f57bSMatthew G. Knepley ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr); 80477623264SMatthew G. Knepley *partition = p->partition; 80577623264SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr); 80677623264SMatthew G. Knepley PetscFunctionReturn(0); 80777623264SMatthew G. Knepley } 80877623264SMatthew G. Knepley 809d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part) 81077623264SMatthew G. Knepley { 81177623264SMatthew G. Knepley PetscFunctionBegin; 81277623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_Shell; 813077101c0SMatthew G. Knepley part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell; 81477623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Shell; 81577623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Shell; 81677623264SMatthew G. Knepley PetscFunctionReturn(0); 81777623264SMatthew G. Knepley } 81877623264SMatthew G. Knepley 81977623264SMatthew G. Knepley /*MC 82077623264SMatthew G. Knepley PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object 82177623264SMatthew G. Knepley 82277623264SMatthew G. Knepley Level: intermediate 82377623264SMatthew G. Knepley 82477623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 82577623264SMatthew G. Knepley M*/ 82677623264SMatthew G. Knepley 82777623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part) 82877623264SMatthew G. Knepley { 82977623264SMatthew G. Knepley PetscPartitioner_Shell *p; 83077623264SMatthew G. Knepley PetscErrorCode ierr; 83177623264SMatthew G. Knepley 83277623264SMatthew G. Knepley PetscFunctionBegin; 83377623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 83477623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 83577623264SMatthew G. Knepley part->data = p; 83677623264SMatthew G. Knepley 83777623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr); 838077101c0SMatthew G. Knepley p->random = PETSC_FALSE; 83977623264SMatthew G. Knepley PetscFunctionReturn(0); 84077623264SMatthew G. Knepley } 84177623264SMatthew G. Knepley 8425680f57bSMatthew G. Knepley /*@C 8435680f57bSMatthew G. Knepley PetscPartitionerShellSetPartition - Set an artifical partition for a mesh 8445680f57bSMatthew G. Knepley 845077101c0SMatthew G. Knepley Collective on Part 8465680f57bSMatthew G. Knepley 8475680f57bSMatthew G. Knepley Input Parameters: 8485680f57bSMatthew G. Knepley + part - The PetscPartitioner 8499852e123SBarry Smith . size - The number of partitions 8509852e123SBarry Smith . sizes - array of size size (or NULL) providing the number of points in each partition 8519758bf69SToby 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.) 8525680f57bSMatthew G. Knepley 8535680f57bSMatthew G. Knepley Level: developer 8545680f57bSMatthew G. Knepley 855b7e49471SLawrence Mitchell Notes: 856b7e49471SLawrence Mitchell 857b7e49471SLawrence Mitchell It is safe to free the sizes and points arrays after use in this routine. 858b7e49471SLawrence Mitchell 8595680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate() 8605680f57bSMatthew G. Knepley @*/ 8619852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[]) 8625680f57bSMatthew G. Knepley { 8635680f57bSMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 8645680f57bSMatthew G. Knepley PetscInt proc, numPoints; 8655680f57bSMatthew G. Knepley PetscErrorCode ierr; 8665680f57bSMatthew G. Knepley 8675680f57bSMatthew G. Knepley PetscFunctionBegin; 8685680f57bSMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 8695680f57bSMatthew G. Knepley if (sizes) {PetscValidPointer(sizes, 3);} 870c717d290SMatthew G. Knepley if (points) {PetscValidPointer(points, 4);} 8715680f57bSMatthew G. Knepley ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr); 8725680f57bSMatthew G. Knepley ierr = ISDestroy(&p->partition);CHKERRQ(ierr); 8735680f57bSMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr); 8749852e123SBarry Smith ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr); 8755680f57bSMatthew G. Knepley if (sizes) { 8769852e123SBarry Smith for (proc = 0; proc < size; ++proc) { 8775680f57bSMatthew G. Knepley ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr); 8785680f57bSMatthew G. Knepley } 8795680f57bSMatthew G. Knepley } 8805680f57bSMatthew G. Knepley ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr); 8815680f57bSMatthew G. Knepley ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr); 8825680f57bSMatthew G. Knepley ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr); 8835680f57bSMatthew G. Knepley PetscFunctionReturn(0); 8845680f57bSMatthew G. Knepley } 8855680f57bSMatthew G. Knepley 886077101c0SMatthew G. Knepley /*@ 887077101c0SMatthew G. Knepley PetscPartitionerShellSetRandom - Set the flag to use a random partition 888077101c0SMatthew G. Knepley 889077101c0SMatthew G. Knepley Collective on Part 890077101c0SMatthew G. Knepley 891077101c0SMatthew G. Knepley Input Parameters: 892077101c0SMatthew G. Knepley + part - The PetscPartitioner 893077101c0SMatthew G. Knepley - random - The flag to use a random partition 894077101c0SMatthew G. Knepley 895077101c0SMatthew G. Knepley Level: intermediate 896077101c0SMatthew G. Knepley 897077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate() 898077101c0SMatthew G. Knepley @*/ 899077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random) 900077101c0SMatthew G. Knepley { 901077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 902077101c0SMatthew G. Knepley 903077101c0SMatthew G. Knepley PetscFunctionBegin; 904077101c0SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 905077101c0SMatthew G. Knepley p->random = random; 906077101c0SMatthew G. Knepley PetscFunctionReturn(0); 907077101c0SMatthew G. Knepley } 908077101c0SMatthew G. Knepley 909077101c0SMatthew G. Knepley /*@ 910077101c0SMatthew G. Knepley PetscPartitionerShellGetRandom - get the flag to use a random partition 911077101c0SMatthew G. Knepley 912077101c0SMatthew G. Knepley Collective on Part 913077101c0SMatthew G. Knepley 914077101c0SMatthew G. Knepley Input Parameter: 915077101c0SMatthew G. Knepley . part - The PetscPartitioner 916077101c0SMatthew G. Knepley 917077101c0SMatthew G. Knepley Output Parameter 918077101c0SMatthew G. Knepley . random - The flag to use a random partition 919077101c0SMatthew G. Knepley 920077101c0SMatthew G. Knepley Level: intermediate 921077101c0SMatthew G. Knepley 922077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate() 923077101c0SMatthew G. Knepley @*/ 924077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random) 925077101c0SMatthew G. Knepley { 926077101c0SMatthew G. Knepley PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data; 927077101c0SMatthew G. Knepley 928077101c0SMatthew G. Knepley PetscFunctionBegin; 929077101c0SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 930077101c0SMatthew G. Knepley PetscValidPointer(random, 2); 931077101c0SMatthew G. Knepley *random = p->random; 932077101c0SMatthew G. Knepley PetscFunctionReturn(0); 933077101c0SMatthew G. Knepley } 934077101c0SMatthew G. Knepley 935d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part) 936555a9cf8SMatthew G. Knepley { 937555a9cf8SMatthew G. Knepley PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data; 938555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 939555a9cf8SMatthew G. Knepley 940555a9cf8SMatthew G. Knepley PetscFunctionBegin; 941555a9cf8SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 942555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 943555a9cf8SMatthew G. Knepley } 944555a9cf8SMatthew G. Knepley 945d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer) 946555a9cf8SMatthew G. Knepley { 947555a9cf8SMatthew G. Knepley PetscViewerFormat format; 948555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 949555a9cf8SMatthew G. Knepley 950555a9cf8SMatthew G. Knepley PetscFunctionBegin; 951555a9cf8SMatthew G. Knepley ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 952555a9cf8SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "Simple Graph Partitioner:\n");CHKERRQ(ierr); 953555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 954555a9cf8SMatthew G. Knepley } 955555a9cf8SMatthew G. Knepley 956d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer) 957555a9cf8SMatthew G. Knepley { 958555a9cf8SMatthew G. Knepley PetscBool iascii; 959555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 960555a9cf8SMatthew G. Knepley 961555a9cf8SMatthew G. Knepley PetscFunctionBegin; 962555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 963555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 964555a9cf8SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 965555a9cf8SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);} 966555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 967555a9cf8SMatthew G. Knepley } 968555a9cf8SMatthew G. Knepley 969d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 970555a9cf8SMatthew G. Knepley { 971cead94edSToby Isaac MPI_Comm comm; 972555a9cf8SMatthew G. Knepley PetscInt np; 973cead94edSToby Isaac PetscMPIInt size; 974555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 975555a9cf8SMatthew G. Knepley 976555a9cf8SMatthew G. Knepley PetscFunctionBegin; 977cead94edSToby Isaac comm = PetscObjectComm((PetscObject)dm); 978cead94edSToby Isaac ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 979555a9cf8SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 980555a9cf8SMatthew G. Knepley ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr); 981cead94edSToby Isaac if (size == 1) { 982cead94edSToby Isaac for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);} 983cead94edSToby Isaac } 984cead94edSToby Isaac else { 985cead94edSToby Isaac PetscMPIInt rank; 986cead94edSToby Isaac PetscInt nvGlobal, *offsets, myFirst, myLast; 987cead94edSToby Isaac 988a679a563SToby Isaac ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr); 989cead94edSToby Isaac offsets[0] = 0; 990cead94edSToby Isaac ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr); 991cead94edSToby Isaac for (np = 2; np <= size; np++) { 992cead94edSToby Isaac offsets[np] += offsets[np-1]; 993cead94edSToby Isaac } 994cead94edSToby Isaac nvGlobal = offsets[size]; 995cead94edSToby Isaac ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 996cead94edSToby Isaac myFirst = offsets[rank]; 997cead94edSToby Isaac myLast = offsets[rank + 1] - 1; 998cead94edSToby Isaac ierr = PetscFree(offsets);CHKERRQ(ierr); 999cead94edSToby Isaac if (numVertices) { 1000cead94edSToby Isaac PetscInt firstPart = 0, firstLargePart = 0; 1001cead94edSToby Isaac PetscInt lastPart = 0, lastLargePart = 0; 1002cead94edSToby Isaac PetscInt rem = nvGlobal % nparts; 1003cead94edSToby Isaac PetscInt pSmall = nvGlobal/nparts; 1004cead94edSToby Isaac PetscInt pBig = nvGlobal/nparts + 1; 1005cead94edSToby Isaac 1006cead94edSToby Isaac 1007cead94edSToby Isaac if (rem) { 1008cead94edSToby Isaac firstLargePart = myFirst / pBig; 1009cead94edSToby Isaac lastLargePart = myLast / pBig; 1010cead94edSToby Isaac 1011cead94edSToby Isaac if (firstLargePart < rem) { 1012cead94edSToby Isaac firstPart = firstLargePart; 1013cead94edSToby Isaac } 1014cead94edSToby Isaac else { 1015cead94edSToby Isaac firstPart = rem + (myFirst - (rem * pBig)) / pSmall; 1016cead94edSToby Isaac } 1017cead94edSToby Isaac if (lastLargePart < rem) { 1018cead94edSToby Isaac lastPart = lastLargePart; 1019cead94edSToby Isaac } 1020cead94edSToby Isaac else { 1021cead94edSToby Isaac lastPart = rem + (myLast - (rem * pBig)) / pSmall; 1022cead94edSToby Isaac } 1023cead94edSToby Isaac } 1024cead94edSToby Isaac else { 1025cead94edSToby Isaac firstPart = myFirst / (nvGlobal/nparts); 1026cead94edSToby Isaac lastPart = myLast / (nvGlobal/nparts); 1027cead94edSToby Isaac } 1028cead94edSToby Isaac 1029cead94edSToby Isaac for (np = firstPart; np <= lastPart; np++) { 1030cead94edSToby Isaac PetscInt PartStart = np * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np); 1031cead94edSToby Isaac PetscInt PartEnd = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1); 1032cead94edSToby Isaac 1033cead94edSToby Isaac PartStart = PetscMax(PartStart,myFirst); 1034cead94edSToby Isaac PartEnd = PetscMin(PartEnd,myLast+1); 1035cead94edSToby Isaac ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr); 1036cead94edSToby Isaac } 1037cead94edSToby Isaac } 1038cead94edSToby Isaac } 1039cead94edSToby Isaac ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1040555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1041555a9cf8SMatthew G. Knepley } 1042555a9cf8SMatthew G. Knepley 1043d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part) 1044555a9cf8SMatthew G. Knepley { 1045555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1046555a9cf8SMatthew G. Knepley part->ops->view = PetscPartitionerView_Simple; 1047555a9cf8SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Simple; 1048555a9cf8SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Simple; 1049555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1050555a9cf8SMatthew G. Knepley } 1051555a9cf8SMatthew G. Knepley 1052555a9cf8SMatthew G. Knepley /*MC 1053555a9cf8SMatthew G. Knepley PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object 1054555a9cf8SMatthew G. Knepley 1055555a9cf8SMatthew G. Knepley Level: intermediate 1056555a9cf8SMatthew G. Knepley 1057555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 1058555a9cf8SMatthew G. Knepley M*/ 1059555a9cf8SMatthew G. Knepley 1060555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part) 1061555a9cf8SMatthew G. Knepley { 1062555a9cf8SMatthew G. Knepley PetscPartitioner_Simple *p; 1063555a9cf8SMatthew G. Knepley PetscErrorCode ierr; 1064555a9cf8SMatthew G. Knepley 1065555a9cf8SMatthew G. Knepley PetscFunctionBegin; 1066555a9cf8SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1067555a9cf8SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 1068555a9cf8SMatthew G. Knepley part->data = p; 1069555a9cf8SMatthew G. Knepley 1070555a9cf8SMatthew G. Knepley ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr); 1071555a9cf8SMatthew G. Knepley PetscFunctionReturn(0); 1072555a9cf8SMatthew G. Knepley } 1073555a9cf8SMatthew G. Knepley 1074d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part) 1075dae52e14SToby Isaac { 1076dae52e14SToby Isaac PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data; 1077dae52e14SToby Isaac PetscErrorCode ierr; 1078dae52e14SToby Isaac 1079dae52e14SToby Isaac PetscFunctionBegin; 1080dae52e14SToby Isaac ierr = PetscFree(p);CHKERRQ(ierr); 1081dae52e14SToby Isaac PetscFunctionReturn(0); 1082dae52e14SToby Isaac } 1083dae52e14SToby Isaac 1084d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer) 1085dae52e14SToby Isaac { 1086dae52e14SToby Isaac PetscViewerFormat format; 1087dae52e14SToby Isaac PetscErrorCode ierr; 1088dae52e14SToby Isaac 1089dae52e14SToby Isaac PetscFunctionBegin; 1090dae52e14SToby Isaac ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 1091dae52e14SToby Isaac ierr = PetscViewerASCIIPrintf(viewer, "Gather Graph Partitioner:\n");CHKERRQ(ierr); 1092dae52e14SToby Isaac PetscFunctionReturn(0); 1093dae52e14SToby Isaac } 1094dae52e14SToby Isaac 1095d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer) 1096dae52e14SToby Isaac { 1097dae52e14SToby Isaac PetscBool iascii; 1098dae52e14SToby Isaac PetscErrorCode ierr; 1099dae52e14SToby Isaac 1100dae52e14SToby Isaac PetscFunctionBegin; 1101dae52e14SToby Isaac PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1102dae52e14SToby Isaac PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1103dae52e14SToby Isaac ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 1104dae52e14SToby Isaac if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);} 1105dae52e14SToby Isaac PetscFunctionReturn(0); 1106dae52e14SToby Isaac } 1107dae52e14SToby Isaac 1108d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 1109dae52e14SToby Isaac { 1110dae52e14SToby Isaac PetscInt np; 1111dae52e14SToby Isaac PetscErrorCode ierr; 1112dae52e14SToby Isaac 1113dae52e14SToby Isaac PetscFunctionBegin; 1114dae52e14SToby Isaac ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 1115dae52e14SToby Isaac ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr); 1116dae52e14SToby Isaac ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr); 1117dae52e14SToby Isaac for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);} 1118dae52e14SToby Isaac ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1119dae52e14SToby Isaac PetscFunctionReturn(0); 1120dae52e14SToby Isaac } 1121dae52e14SToby Isaac 1122d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part) 1123dae52e14SToby Isaac { 1124dae52e14SToby Isaac PetscFunctionBegin; 1125dae52e14SToby Isaac part->ops->view = PetscPartitionerView_Gather; 1126dae52e14SToby Isaac part->ops->destroy = PetscPartitionerDestroy_Gather; 1127dae52e14SToby Isaac part->ops->partition = PetscPartitionerPartition_Gather; 1128dae52e14SToby Isaac PetscFunctionReturn(0); 1129dae52e14SToby Isaac } 1130dae52e14SToby Isaac 1131dae52e14SToby Isaac /*MC 1132dae52e14SToby Isaac PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object 1133dae52e14SToby Isaac 1134dae52e14SToby Isaac Level: intermediate 1135dae52e14SToby Isaac 1136dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 1137dae52e14SToby Isaac M*/ 1138dae52e14SToby Isaac 1139dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part) 1140dae52e14SToby Isaac { 1141dae52e14SToby Isaac PetscPartitioner_Gather *p; 1142dae52e14SToby Isaac PetscErrorCode ierr; 1143dae52e14SToby Isaac 1144dae52e14SToby Isaac PetscFunctionBegin; 1145dae52e14SToby Isaac PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1146dae52e14SToby Isaac ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 1147dae52e14SToby Isaac part->data = p; 1148dae52e14SToby Isaac 1149dae52e14SToby Isaac ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr); 1150dae52e14SToby Isaac PetscFunctionReturn(0); 1151dae52e14SToby Isaac } 1152dae52e14SToby Isaac 1153dae52e14SToby Isaac 1154d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part) 115577623264SMatthew G. Knepley { 115677623264SMatthew G. Knepley PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data; 115777623264SMatthew G. Knepley PetscErrorCode ierr; 115877623264SMatthew G. Knepley 115977623264SMatthew G. Knepley PetscFunctionBegin; 116077623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 116177623264SMatthew G. Knepley PetscFunctionReturn(0); 116277623264SMatthew G. Knepley } 116377623264SMatthew G. Knepley 1164d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer) 116577623264SMatthew G. Knepley { 116677623264SMatthew G. Knepley PetscViewerFormat format; 116777623264SMatthew G. Knepley PetscErrorCode ierr; 116877623264SMatthew G. Knepley 116977623264SMatthew G. Knepley PetscFunctionBegin; 117077623264SMatthew G. Knepley ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 117177623264SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "Chaco Graph Partitioner:\n");CHKERRQ(ierr); 117277623264SMatthew G. Knepley PetscFunctionReturn(0); 117377623264SMatthew G. Knepley } 117477623264SMatthew G. Knepley 1175d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer) 117677623264SMatthew G. Knepley { 117777623264SMatthew G. Knepley PetscBool iascii; 117877623264SMatthew G. Knepley PetscErrorCode ierr; 117977623264SMatthew G. Knepley 118077623264SMatthew G. Knepley PetscFunctionBegin; 118177623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 118277623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 118377623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 118477623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);} 118577623264SMatthew G. Knepley PetscFunctionReturn(0); 118677623264SMatthew G. Knepley } 118777623264SMatthew G. Knepley 118870034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO) 118970034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 119070034214SMatthew G. Knepley #include <unistd.h> 119170034214SMatthew G. Knepley #endif 119211d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT) 119311d1e910SBarry Smith #include <chaco.h> 119411d1e910SBarry Smith #else 119511d1e910SBarry Smith /* Older versions of Chaco do not have an include file */ 119670034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts, 119770034214SMatthew G. Knepley float *ewgts, float *x, float *y, float *z, char *outassignname, 119870034214SMatthew G. Knepley char *outfilename, short *assignment, int architecture, int ndims_tot, 119970034214SMatthew G. Knepley int mesh_dims[3], double *goal, int global_method, int local_method, 120070034214SMatthew G. Knepley int rqi_flag, int vmax, int ndims, double eigtol, long seed); 120111d1e910SBarry Smith #endif 120270034214SMatthew G. Knepley extern int FREE_GRAPH; 120377623264SMatthew G. Knepley #endif 120470034214SMatthew G. Knepley 1205d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 120670034214SMatthew G. Knepley { 120777623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO) 120870034214SMatthew G. Knepley enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3}; 120970034214SMatthew G. Knepley MPI_Comm comm; 121070034214SMatthew G. Knepley int nvtxs = numVertices; /* number of vertices in full graph */ 121170034214SMatthew G. Knepley int *vwgts = NULL; /* weights for all vertices */ 121270034214SMatthew G. Knepley float *ewgts = NULL; /* weights for all edges */ 121370034214SMatthew G. Knepley float *x = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */ 121470034214SMatthew G. Knepley char *outassignname = NULL; /* name of assignment output file */ 121570034214SMatthew G. Knepley char *outfilename = NULL; /* output file name */ 121670034214SMatthew G. Knepley int architecture = 1; /* 0 => hypercube, d => d-dimensional mesh */ 121770034214SMatthew G. Knepley int ndims_tot = 0; /* total number of cube dimensions to divide */ 121870034214SMatthew G. Knepley int mesh_dims[3]; /* dimensions of mesh of processors */ 121970034214SMatthew G. Knepley double *goal = NULL; /* desired set sizes for each set */ 122070034214SMatthew G. Knepley int global_method = 1; /* global partitioning algorithm */ 122170034214SMatthew G. Knepley int local_method = 1; /* local partitioning algorithm */ 122270034214SMatthew G. Knepley int rqi_flag = 0; /* should I use RQI/Symmlq eigensolver? */ 122370034214SMatthew G. Knepley int vmax = 200; /* how many vertices to coarsen down to? */ 122470034214SMatthew G. Knepley int ndims = 1; /* number of eigenvectors (2^d sets) */ 122570034214SMatthew G. Knepley double eigtol = 0.001; /* tolerance on eigenvectors */ 122670034214SMatthew G. Knepley long seed = 123636512; /* for random graph mutations */ 122711d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT) 122811d1e910SBarry Smith int *assignment; /* Output partition */ 122911d1e910SBarry Smith #else 123070034214SMatthew G. Knepley short int *assignment; /* Output partition */ 123111d1e910SBarry Smith #endif 123270034214SMatthew G. Knepley int fd_stdout, fd_pipe[2]; 123370034214SMatthew G. Knepley PetscInt *points; 123470034214SMatthew G. Knepley int i, v, p; 123570034214SMatthew G. Knepley PetscErrorCode ierr; 123670034214SMatthew G. Knepley 123770034214SMatthew G. Knepley PetscFunctionBegin; 123870034214SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 123907ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG) 124007ed3857SLisandro Dalcin { 124107ed3857SLisandro Dalcin int ival,isum; 124207ed3857SLisandro Dalcin PetscBool distributed; 124307ed3857SLisandro Dalcin 124407ed3857SLisandro Dalcin ival = (numVertices > 0); 124507ed3857SLisandro Dalcin ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr); 124607ed3857SLisandro Dalcin distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE; 124707ed3857SLisandro Dalcin if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph"); 124807ed3857SLisandro Dalcin } 124907ed3857SLisandro Dalcin #endif 125070034214SMatthew G. Knepley if (!numVertices) { 125177623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 125277623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 125370034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 125470034214SMatthew G. Knepley PetscFunctionReturn(0); 125570034214SMatthew G. Knepley } 125670034214SMatthew G. Knepley FREE_GRAPH = 0; /* Do not let Chaco free my memory */ 125770034214SMatthew G. Knepley for (i = 0; i < start[numVertices]; ++i) ++adjacency[i]; 125870034214SMatthew G. Knepley 125970034214SMatthew G. Knepley if (global_method == INERTIAL_METHOD) { 126070034214SMatthew G. Knepley /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */ 126170034214SMatthew G. Knepley SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported"); 126270034214SMatthew G. Knepley } 126377623264SMatthew G. Knepley mesh_dims[0] = nparts; 126470034214SMatthew G. Knepley mesh_dims[1] = 1; 126570034214SMatthew G. Knepley mesh_dims[2] = 1; 126670034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr); 126770034214SMatthew G. Knepley /* Chaco outputs to stdout. We redirect this to a buffer. */ 126870034214SMatthew G. Knepley /* TODO: check error codes for UNIX calls */ 126970034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 127070034214SMatthew G. Knepley { 127170034214SMatthew G. Knepley int piperet; 127270034214SMatthew G. Knepley piperet = pipe(fd_pipe); 127370034214SMatthew G. Knepley if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe"); 127470034214SMatthew G. Knepley fd_stdout = dup(1); 127570034214SMatthew G. Knepley close(1); 127670034214SMatthew G. Knepley dup2(fd_pipe[1], 1); 127770034214SMatthew G. Knepley } 127870034214SMatthew G. Knepley #endif 127970034214SMatthew G. Knepley ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename, 128070034214SMatthew G. Knepley assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag, 128170034214SMatthew G. Knepley vmax, ndims, eigtol, seed); 128270034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H) 128370034214SMatthew G. Knepley { 128470034214SMatthew G. Knepley char msgLog[10000]; 128570034214SMatthew G. Knepley int count; 128670034214SMatthew G. Knepley 128770034214SMatthew G. Knepley fflush(stdout); 128870034214SMatthew G. Knepley count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char)); 128970034214SMatthew G. Knepley if (count < 0) count = 0; 129070034214SMatthew G. Knepley msgLog[count] = 0; 129170034214SMatthew G. Knepley close(1); 129270034214SMatthew G. Knepley dup2(fd_stdout, 1); 129370034214SMatthew G. Knepley close(fd_stdout); 129470034214SMatthew G. Knepley close(fd_pipe[0]); 129570034214SMatthew G. Knepley close(fd_pipe[1]); 129670034214SMatthew G. Knepley if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog); 129770034214SMatthew G. Knepley } 129807ed3857SLisandro Dalcin #else 129907ed3857SLisandro Dalcin if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout"); 130070034214SMatthew G. Knepley #endif 130170034214SMatthew G. Knepley /* Convert to PetscSection+IS */ 130277623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 130370034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 130477623264SMatthew G. Knepley ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr); 130570034214SMatthew G. Knepley } 130677623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 130770034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 130877623264SMatthew G. Knepley for (p = 0, i = 0; p < nparts; ++p) { 130970034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 131070034214SMatthew G. Knepley if (assignment[v] == p) points[i++] = v; 131170034214SMatthew G. Knepley } 131270034214SMatthew G. Knepley } 131370034214SMatthew G. Knepley if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 131470034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 131570034214SMatthew G. Knepley if (global_method == INERTIAL_METHOD) { 131670034214SMatthew G. Knepley /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */ 131770034214SMatthew G. Knepley } 131870034214SMatthew G. Knepley ierr = PetscFree(assignment);CHKERRQ(ierr); 131970034214SMatthew G. Knepley for (i = 0; i < start[numVertices]; ++i) --adjacency[i]; 132070034214SMatthew G. Knepley PetscFunctionReturn(0); 132177623264SMatthew G. Knepley #else 132277623264SMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco."); 132370034214SMatthew G. Knepley #endif 132477623264SMatthew G. Knepley } 132577623264SMatthew G. Knepley 1326d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part) 132777623264SMatthew G. Knepley { 132877623264SMatthew G. Knepley PetscFunctionBegin; 132977623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_Chaco; 133077623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_Chaco; 133177623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_Chaco; 133277623264SMatthew G. Knepley PetscFunctionReturn(0); 133377623264SMatthew G. Knepley } 133477623264SMatthew G. Knepley 133577623264SMatthew G. Knepley /*MC 133677623264SMatthew G. Knepley PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library 133777623264SMatthew G. Knepley 133877623264SMatthew G. Knepley Level: intermediate 133977623264SMatthew G. Knepley 134077623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 134177623264SMatthew G. Knepley M*/ 134277623264SMatthew G. Knepley 134377623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part) 134477623264SMatthew G. Knepley { 134577623264SMatthew G. Knepley PetscPartitioner_Chaco *p; 134677623264SMatthew G. Knepley PetscErrorCode ierr; 134777623264SMatthew G. Knepley 134877623264SMatthew G. Knepley PetscFunctionBegin; 134977623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 135077623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 135177623264SMatthew G. Knepley part->data = p; 135277623264SMatthew G. Knepley 135377623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr); 135477623264SMatthew G. Knepley ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr); 135577623264SMatthew G. Knepley PetscFunctionReturn(0); 135677623264SMatthew G. Knepley } 135777623264SMatthew G. Knepley 1358d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part) 135977623264SMatthew G. Knepley { 136077623264SMatthew G. Knepley PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data; 136177623264SMatthew G. Knepley PetscErrorCode ierr; 136277623264SMatthew G. Knepley 136377623264SMatthew G. Knepley PetscFunctionBegin; 136477623264SMatthew G. Knepley ierr = PetscFree(p);CHKERRQ(ierr); 136577623264SMatthew G. Knepley PetscFunctionReturn(0); 136677623264SMatthew G. Knepley } 136777623264SMatthew G. Knepley 1368d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer) 136977623264SMatthew G. Knepley { 137077623264SMatthew G. Knepley PetscViewerFormat format; 137177623264SMatthew G. Knepley PetscErrorCode ierr; 137277623264SMatthew G. Knepley 137377623264SMatthew G. Knepley PetscFunctionBegin; 137477623264SMatthew G. Knepley ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 137577623264SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "ParMetis Graph Partitioner:\n");CHKERRQ(ierr); 137677623264SMatthew G. Knepley PetscFunctionReturn(0); 137777623264SMatthew G. Knepley } 137877623264SMatthew G. Knepley 1379d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer) 138077623264SMatthew G. Knepley { 138177623264SMatthew G. Knepley PetscBool iascii; 138277623264SMatthew G. Knepley PetscErrorCode ierr; 138377623264SMatthew G. Knepley 138477623264SMatthew G. Knepley PetscFunctionBegin; 138577623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 138677623264SMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 138777623264SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 138877623264SMatthew G. Knepley if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);} 138977623264SMatthew G. Knepley PetscFunctionReturn(0); 139077623264SMatthew G. Knepley } 139170034214SMatthew G. Knepley 139270034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS) 139370034214SMatthew G. Knepley #include <parmetis.h> 139477623264SMatthew G. Knepley #endif 139570034214SMatthew G. Knepley 1396d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 139770034214SMatthew G. Knepley { 139877623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS) 139970034214SMatthew G. Knepley MPI_Comm comm; 1400deea36a5SMatthew G. Knepley PetscSection section; 140170034214SMatthew G. Knepley PetscInt nvtxs = numVertices; /* The number of vertices in full graph */ 140270034214SMatthew G. Knepley PetscInt *vtxdist; /* Distribution of vertices across processes */ 140370034214SMatthew G. Knepley PetscInt *xadj = start; /* Start of edge list for each vertex */ 140470034214SMatthew G. Knepley PetscInt *adjncy = adjacency; /* Edge lists for all vertices */ 140570034214SMatthew G. Knepley PetscInt *vwgt = NULL; /* Vertex weights */ 140670034214SMatthew G. Knepley PetscInt *adjwgt = NULL; /* Edge weights */ 140770034214SMatthew G. Knepley PetscInt wgtflag = 0; /* Indicates which weights are present */ 140870034214SMatthew G. Knepley PetscInt numflag = 0; /* Indicates initial offset (0 or 1) */ 140970034214SMatthew G. Knepley PetscInt ncon = 1; /* The number of weights per vertex */ 1410fb83b9f2SMichael Gegg real_t *tpwgts; /* The fraction of vertex weights assigned to each partition */ 1411fb83b9f2SMichael Gegg real_t *ubvec; /* The balance intolerance for vertex weights */ 1412b3ce585bSLisandro Dalcin PetscInt options[64]; /* Options */ 141370034214SMatthew G. Knepley /* Outputs */ 141470034214SMatthew G. Knepley PetscInt edgeCut; /* The number of edges cut by the partition */ 1415b3ce585bSLisandro Dalcin PetscInt v, i, *assignment, *points; 1416b3ce585bSLisandro Dalcin PetscMPIInt size, rank, p; 141770034214SMatthew G. Knepley PetscErrorCode ierr; 141870034214SMatthew G. Knepley 141970034214SMatthew G. Knepley PetscFunctionBegin; 142077623264SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr); 1421b3ce585bSLisandro Dalcin ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 142270034214SMatthew G. Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 142370034214SMatthew G. Knepley options[0] = 0; /* Use all defaults */ 142470034214SMatthew G. Knepley /* Calculate vertex distribution */ 1425b3ce585bSLisandro Dalcin ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr); 142670034214SMatthew G. Knepley vtxdist[0] = 0; 142770034214SMatthew G. Knepley ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr); 1428b3ce585bSLisandro Dalcin for (p = 2; p <= size; ++p) { 142970034214SMatthew G. Knepley vtxdist[p] += vtxdist[p-1]; 143070034214SMatthew G. Knepley } 143170034214SMatthew G. Knepley /* Calculate weights */ 143270034214SMatthew G. Knepley for (p = 0; p < nparts; ++p) { 143370034214SMatthew G. Knepley tpwgts[p] = 1.0/nparts; 143470034214SMatthew G. Knepley } 143570034214SMatthew G. Knepley ubvec[0] = 1.05; 1436deea36a5SMatthew G. Knepley /* Weight cells by dofs on cell by default */ 1437deea36a5SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1438deea36a5SMatthew G. Knepley if (section) { 1439deea36a5SMatthew G. Knepley PetscInt cStart, cEnd, dof; 144070034214SMatthew G. Knepley 1441deea36a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1442deea36a5SMatthew G. Knepley for (v = cStart; v < cEnd; ++v) { 1443deea36a5SMatthew G. Knepley ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr); 1444925b1076SLisandro Dalcin /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */ 1445925b1076SLisandro Dalcin /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */ 1446925b1076SLisandro Dalcin if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1); 1447deea36a5SMatthew G. Knepley } 1448deea36a5SMatthew G. Knepley } else { 1449deea36a5SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) vwgt[v] = 1; 1450cd0de0f2SShri } 1451cd0de0f2SShri 145270034214SMatthew G. Knepley if (nparts == 1) { 14539fc93327SToby Isaac ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr); 145470034214SMatthew G. Knepley } else { 1455b3ce585bSLisandro Dalcin for (p = 0; !vtxdist[p+1] && p < size; ++p); 1456b3ce585bSLisandro Dalcin if (vtxdist[p+1] == vtxdist[size]) { 1457b3ce585bSLisandro Dalcin if (rank == p) { 145870034214SMatthew G. Knepley PetscStackPush("METIS_PartGraphKway"); 145970034214SMatthew G. Knepley ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, NULL, &edgeCut, assignment); 146070034214SMatthew G. Knepley PetscStackPop; 146170034214SMatthew G. Knepley if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()"); 146270034214SMatthew G. Knepley } 146370034214SMatthew G. Knepley } else { 146470034214SMatthew G. Knepley PetscStackPush("ParMETIS_V3_PartKway"); 146570034214SMatthew G. Knepley ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgeCut, assignment, &comm); 146670034214SMatthew G. Knepley PetscStackPop; 1467c717d290SMatthew G. Knepley if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr); 146870034214SMatthew G. Knepley } 146970034214SMatthew G. Knepley } 147070034214SMatthew G. Knepley /* Convert to PetscSection+IS */ 147177623264SMatthew G. Knepley ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 147277623264SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);} 147377623264SMatthew G. Knepley ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 147470034214SMatthew G. Knepley ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 147577623264SMatthew G. Knepley for (p = 0, i = 0; p < nparts; ++p) { 147670034214SMatthew G. Knepley for (v = 0; v < nvtxs; ++v) { 147770034214SMatthew G. Knepley if (assignment[v] == p) points[i++] = v; 147870034214SMatthew G. Knepley } 147970034214SMatthew G. Knepley } 148070034214SMatthew G. Knepley if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 148170034214SMatthew G. Knepley ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 1482cd0de0f2SShri ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr); 14839b80ac48SMatthew G. Knepley PetscFunctionReturn(0); 148470034214SMatthew G. Knepley #else 148577623264SMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis."); 148670034214SMatthew G. Knepley #endif 148770034214SMatthew G. Knepley } 148870034214SMatthew G. Knepley 1489d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part) 149077623264SMatthew G. Knepley { 149177623264SMatthew G. Knepley PetscFunctionBegin; 149277623264SMatthew G. Knepley part->ops->view = PetscPartitionerView_ParMetis; 149377623264SMatthew G. Knepley part->ops->destroy = PetscPartitionerDestroy_ParMetis; 149477623264SMatthew G. Knepley part->ops->partition = PetscPartitionerPartition_ParMetis; 149577623264SMatthew G. Knepley PetscFunctionReturn(0); 149677623264SMatthew G. Knepley } 149777623264SMatthew G. Knepley 149877623264SMatthew G. Knepley /*MC 149977623264SMatthew G. Knepley PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library 150077623264SMatthew G. Knepley 150177623264SMatthew G. Knepley Level: intermediate 150277623264SMatthew G. Knepley 150377623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 150477623264SMatthew G. Knepley M*/ 150577623264SMatthew G. Knepley 150677623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part) 150777623264SMatthew G. Knepley { 150877623264SMatthew G. Knepley PetscPartitioner_ParMetis *p; 150977623264SMatthew G. Knepley PetscErrorCode ierr; 151077623264SMatthew G. Knepley 151177623264SMatthew G. Knepley PetscFunctionBegin; 151277623264SMatthew G. Knepley PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 151377623264SMatthew G. Knepley ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 151477623264SMatthew G. Knepley part->data = p; 151577623264SMatthew G. Knepley 151677623264SMatthew G. Knepley ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr); 151777623264SMatthew G. Knepley ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr); 151870034214SMatthew G. Knepley PetscFunctionReturn(0); 151970034214SMatthew G. Knepley } 152070034214SMatthew G. Knepley 1521*137cd93aSLisandro Dalcin 1522*137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE; 1523*137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] = 1524*137cd93aSLisandro Dalcin "@article{PTSCOTCH,\n" 1525*137cd93aSLisandro Dalcin " author = {C. Chevalier and F. Pellegrini},\n" 1526*137cd93aSLisandro Dalcin " title = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n" 1527*137cd93aSLisandro Dalcin " journal = {Parallel Computing},\n" 1528*137cd93aSLisandro Dalcin " volume = {34},\n" 1529*137cd93aSLisandro Dalcin " number = {6},\n" 1530*137cd93aSLisandro Dalcin " pages = {318--331},\n" 1531*137cd93aSLisandro Dalcin " year = {2008},\n" 1532*137cd93aSLisandro Dalcin " doi = {https://doi.org/10.1016/j.parco.2007.12.001}\n" 1533*137cd93aSLisandro Dalcin "}\n"; 1534*137cd93aSLisandro Dalcin 1535*137cd93aSLisandro Dalcin typedef struct { 1536*137cd93aSLisandro Dalcin PetscInt strategy; 1537*137cd93aSLisandro Dalcin PetscReal imbalance; 1538*137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch; 1539*137cd93aSLisandro Dalcin 1540*137cd93aSLisandro Dalcin static const char *const 1541*137cd93aSLisandro Dalcin PTScotchStrategyList[] = { 1542*137cd93aSLisandro Dalcin "DEFAULT", 1543*137cd93aSLisandro Dalcin "QUALITY", 1544*137cd93aSLisandro Dalcin "SPEED", 1545*137cd93aSLisandro Dalcin "BALANCE", 1546*137cd93aSLisandro Dalcin "SAFETY", 1547*137cd93aSLisandro Dalcin "SCALABILITY", 1548*137cd93aSLisandro Dalcin "RECURSIVE", 1549*137cd93aSLisandro Dalcin "REMAP" 1550*137cd93aSLisandro Dalcin }; 1551*137cd93aSLisandro Dalcin 1552*137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH) 1553*137cd93aSLisandro Dalcin 1554*137cd93aSLisandro Dalcin EXTERN_C_BEGIN 1555*137cd93aSLisandro Dalcin #include <ptscotch.h> 1556*137cd93aSLisandro Dalcin EXTERN_C_END 1557*137cd93aSLisandro Dalcin 1558*137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0) 1559*137cd93aSLisandro Dalcin 1560*137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy) 1561*137cd93aSLisandro Dalcin { 1562*137cd93aSLisandro Dalcin switch (strategy) { 1563*137cd93aSLisandro Dalcin case 0: return SCOTCH_STRATDEFAULT; 1564*137cd93aSLisandro Dalcin case 1: return SCOTCH_STRATQUALITY; 1565*137cd93aSLisandro Dalcin case 2: return SCOTCH_STRATSPEED; 1566*137cd93aSLisandro Dalcin case 3: return SCOTCH_STRATBALANCE; 1567*137cd93aSLisandro Dalcin case 4: return SCOTCH_STRATSAFETY; 1568*137cd93aSLisandro Dalcin case 5: return SCOTCH_STRATSCALABILITY; 1569*137cd93aSLisandro Dalcin case 6: return SCOTCH_STRATRECURSIVE; 1570*137cd93aSLisandro Dalcin case 7: return SCOTCH_STRATREMAP; 1571*137cd93aSLisandro Dalcin default: return SCOTCH_STRATDEFAULT; 1572*137cd93aSLisandro Dalcin } 1573*137cd93aSLisandro Dalcin } 1574*137cd93aSLisandro Dalcin 1575*137cd93aSLisandro Dalcin 1576*137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[], 1577*137cd93aSLisandro Dalcin SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[]) 1578*137cd93aSLisandro Dalcin { 1579*137cd93aSLisandro Dalcin SCOTCH_Graph grafdat; 1580*137cd93aSLisandro Dalcin SCOTCH_Strat stradat; 1581*137cd93aSLisandro Dalcin SCOTCH_Num vertnbr = n; 1582*137cd93aSLisandro Dalcin SCOTCH_Num edgenbr = xadj[n]; 1583*137cd93aSLisandro Dalcin SCOTCH_Num* velotab = vtxwgt; 1584*137cd93aSLisandro Dalcin SCOTCH_Num* edlotab = adjwgt; 1585*137cd93aSLisandro Dalcin SCOTCH_Num flagval = strategy; 1586*137cd93aSLisandro Dalcin double kbalval = imbalance; 1587*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1588*137cd93aSLisandro Dalcin 1589*137cd93aSLisandro Dalcin PetscFunctionBegin; 1590*137cd93aSLisandro Dalcin ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr); 1591*137cd93aSLisandro Dalcin ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr); 1592*137cd93aSLisandro Dalcin ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr); 1593*137cd93aSLisandro Dalcin ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr); 1594*137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG) 1595*137cd93aSLisandro Dalcin ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr); 1596*137cd93aSLisandro Dalcin #endif 1597*137cd93aSLisandro Dalcin ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr); 1598*137cd93aSLisandro Dalcin SCOTCH_stratExit(&stradat); 1599*137cd93aSLisandro Dalcin SCOTCH_graphExit(&grafdat); 1600*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1601*137cd93aSLisandro Dalcin } 1602*137cd93aSLisandro Dalcin 1603*137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[], 1604*137cd93aSLisandro Dalcin SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm) 1605*137cd93aSLisandro Dalcin { 1606*137cd93aSLisandro Dalcin PetscMPIInt procglbnbr; 1607*137cd93aSLisandro Dalcin PetscMPIInt proclocnum; 1608*137cd93aSLisandro Dalcin SCOTCH_Arch archdat; 1609*137cd93aSLisandro Dalcin SCOTCH_Dgraph grafdat; 1610*137cd93aSLisandro Dalcin SCOTCH_Dmapping mappdat; 1611*137cd93aSLisandro Dalcin SCOTCH_Strat stradat; 1612*137cd93aSLisandro Dalcin SCOTCH_Num vertlocnbr; 1613*137cd93aSLisandro Dalcin SCOTCH_Num edgelocnbr; 1614*137cd93aSLisandro Dalcin SCOTCH_Num* veloloctab = vtxwgt; 1615*137cd93aSLisandro Dalcin SCOTCH_Num* edloloctab = adjwgt; 1616*137cd93aSLisandro Dalcin SCOTCH_Num flagval = strategy; 1617*137cd93aSLisandro Dalcin double kbalval = imbalance; 1618*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1619*137cd93aSLisandro Dalcin 1620*137cd93aSLisandro Dalcin PetscFunctionBegin; 1621*137cd93aSLisandro Dalcin ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr); 1622*137cd93aSLisandro Dalcin ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr); 1623*137cd93aSLisandro Dalcin vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum]; 1624*137cd93aSLisandro Dalcin edgelocnbr = xadj[vertlocnbr]; 1625*137cd93aSLisandro Dalcin 1626*137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr); 1627*137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr); 1628*137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG) 1629*137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr); 1630*137cd93aSLisandro Dalcin #endif 1631*137cd93aSLisandro Dalcin ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr); 1632*137cd93aSLisandro Dalcin ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr); 1633*137cd93aSLisandro Dalcin ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr); 1634*137cd93aSLisandro Dalcin ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr); 1635*137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr); 1636*137cd93aSLisandro Dalcin ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr); 1637*137cd93aSLisandro Dalcin SCOTCH_dgraphMapExit(&grafdat, &mappdat); 1638*137cd93aSLisandro Dalcin SCOTCH_archExit(&archdat); 1639*137cd93aSLisandro Dalcin SCOTCH_stratExit(&stradat); 1640*137cd93aSLisandro Dalcin SCOTCH_dgraphExit(&grafdat); 1641*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1642*137cd93aSLisandro Dalcin } 1643*137cd93aSLisandro Dalcin 1644*137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */ 1645*137cd93aSLisandro Dalcin 1646*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part) 1647*137cd93aSLisandro Dalcin { 1648*137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1649*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1650*137cd93aSLisandro Dalcin 1651*137cd93aSLisandro Dalcin PetscFunctionBegin; 1652*137cd93aSLisandro Dalcin ierr = PetscFree(p);CHKERRQ(ierr); 1653*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1654*137cd93aSLisandro Dalcin } 1655*137cd93aSLisandro Dalcin 1656*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer) 1657*137cd93aSLisandro Dalcin { 1658*137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1659*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1660*137cd93aSLisandro Dalcin 1661*137cd93aSLisandro Dalcin PetscFunctionBegin; 1662*137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer, "PTScotch Graph Partitioner:\n");CHKERRQ(ierr); 1663*137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1664*137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr); 1665*137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr); 1666*137cd93aSLisandro Dalcin ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1667*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1668*137cd93aSLisandro Dalcin } 1669*137cd93aSLisandro Dalcin 1670*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer) 1671*137cd93aSLisandro Dalcin { 1672*137cd93aSLisandro Dalcin PetscBool iascii; 1673*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1674*137cd93aSLisandro Dalcin 1675*137cd93aSLisandro Dalcin PetscFunctionBegin; 1676*137cd93aSLisandro Dalcin PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1677*137cd93aSLisandro Dalcin PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1678*137cd93aSLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 1679*137cd93aSLisandro Dalcin if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);} 1680*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1681*137cd93aSLisandro Dalcin } 1682*137cd93aSLisandro Dalcin 1683*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 1684*137cd93aSLisandro Dalcin { 1685*137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data; 1686*137cd93aSLisandro Dalcin const char *const *slist = PTScotchStrategyList; 1687*137cd93aSLisandro Dalcin PetscInt nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0])); 1688*137cd93aSLisandro Dalcin PetscBool flag; 1689*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1690*137cd93aSLisandro Dalcin 1691*137cd93aSLisandro Dalcin PetscFunctionBegin; 1692*137cd93aSLisandro Dalcin ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitionerPTScotch Options");CHKERRQ(ierr); 1693*137cd93aSLisandro Dalcin ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr); 1694*137cd93aSLisandro Dalcin ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr); 1695*137cd93aSLisandro Dalcin ierr = PetscOptionsTail();CHKERRQ(ierr); 1696*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1697*137cd93aSLisandro Dalcin } 1698*137cd93aSLisandro Dalcin 1699*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition) 1700*137cd93aSLisandro Dalcin { 1701*137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH) 1702*137cd93aSLisandro Dalcin MPI_Comm comm = PetscObjectComm((PetscObject)part); 1703*137cd93aSLisandro Dalcin PetscInt nvtxs = numVertices; /* The number of vertices in full graph */ 1704*137cd93aSLisandro Dalcin PetscInt *vtxdist; /* Distribution of vertices across processes */ 1705*137cd93aSLisandro Dalcin PetscInt *xadj = start; /* Start of edge list for each vertex */ 1706*137cd93aSLisandro Dalcin PetscInt *adjncy = adjacency; /* Edge lists for all vertices */ 1707*137cd93aSLisandro Dalcin PetscInt *vwgt = NULL; /* Vertex weights */ 1708*137cd93aSLisandro Dalcin PetscInt *adjwgt = NULL; /* Edge weights */ 1709*137cd93aSLisandro Dalcin PetscInt v, i, *assignment, *points; 1710*137cd93aSLisandro Dalcin PetscMPIInt size, rank, p; 1711*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1712*137cd93aSLisandro Dalcin 1713*137cd93aSLisandro Dalcin PetscFunctionBegin; 1714*137cd93aSLisandro Dalcin ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 1715*137cd93aSLisandro Dalcin ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 1716*137cd93aSLisandro Dalcin ierr = PetscMalloc2(nparts+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr); 1717*137cd93aSLisandro Dalcin 1718*137cd93aSLisandro Dalcin /* Calculate vertex distribution */ 1719*137cd93aSLisandro Dalcin vtxdist[0] = 0; 1720*137cd93aSLisandro Dalcin ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr); 1721*137cd93aSLisandro Dalcin for (p = 2; p <= size; ++p) { 1722*137cd93aSLisandro Dalcin vtxdist[p] += vtxdist[p-1]; 1723*137cd93aSLisandro Dalcin } 1724*137cd93aSLisandro Dalcin 1725*137cd93aSLisandro Dalcin if (nparts == 1) { 1726*137cd93aSLisandro Dalcin ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr); 1727*137cd93aSLisandro Dalcin } else { 1728*137cd93aSLisandro Dalcin PetscSection section; 1729*137cd93aSLisandro Dalcin /* Weight cells by dofs on cell by default */ 1730*137cd93aSLisandro Dalcin ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr); 1731*137cd93aSLisandro Dalcin ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1732*137cd93aSLisandro Dalcin if (section) { 1733*137cd93aSLisandro Dalcin PetscInt vStart, vEnd, dof; 1734*137cd93aSLisandro Dalcin ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 1735*137cd93aSLisandro Dalcin for (v = vStart; v < vEnd; ++v) { 1736*137cd93aSLisandro Dalcin ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr); 1737*137cd93aSLisandro Dalcin /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */ 1738*137cd93aSLisandro Dalcin /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */ 1739*137cd93aSLisandro Dalcin if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1); 1740*137cd93aSLisandro Dalcin } 1741*137cd93aSLisandro Dalcin } else { 1742*137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) vwgt[v] = 1; 1743*137cd93aSLisandro Dalcin } 1744*137cd93aSLisandro Dalcin { 1745*137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data; 1746*137cd93aSLisandro Dalcin int strat = PTScotch_Strategy(pts->strategy); 1747*137cd93aSLisandro Dalcin double imbal = (double)pts->imbalance; 1748*137cd93aSLisandro Dalcin 1749*137cd93aSLisandro Dalcin for (p = 0; !vtxdist[p+1] && p < size; ++p); 1750*137cd93aSLisandro Dalcin if (vtxdist[p+1] == vtxdist[size]) { 1751*137cd93aSLisandro Dalcin if (rank == p) { 1752*137cd93aSLisandro Dalcin ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr); 1753*137cd93aSLisandro Dalcin } 1754*137cd93aSLisandro Dalcin } else { 1755*137cd93aSLisandro Dalcin ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr); 1756*137cd93aSLisandro Dalcin } 1757*137cd93aSLisandro Dalcin } 1758*137cd93aSLisandro Dalcin ierr = PetscFree(vwgt);CHKERRQ(ierr); 1759*137cd93aSLisandro Dalcin } 1760*137cd93aSLisandro Dalcin 1761*137cd93aSLisandro Dalcin /* Convert to PetscSection+IS */ 1762*137cd93aSLisandro Dalcin ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr); 1763*137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);} 1764*137cd93aSLisandro Dalcin ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr); 1765*137cd93aSLisandro Dalcin ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr); 1766*137cd93aSLisandro Dalcin for (p = 0, i = 0; p < nparts; ++p) { 1767*137cd93aSLisandro Dalcin for (v = 0; v < nvtxs; ++v) { 1768*137cd93aSLisandro Dalcin if (assignment[v] == p) points[i++] = v; 1769*137cd93aSLisandro Dalcin } 1770*137cd93aSLisandro Dalcin } 1771*137cd93aSLisandro Dalcin if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs); 1772*137cd93aSLisandro Dalcin ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr); 1773*137cd93aSLisandro Dalcin 1774*137cd93aSLisandro Dalcin ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr); 1775*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1776*137cd93aSLisandro Dalcin #else 1777*137cd93aSLisandro Dalcin SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch."); 1778*137cd93aSLisandro Dalcin #endif 1779*137cd93aSLisandro Dalcin } 1780*137cd93aSLisandro Dalcin 1781*137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part) 1782*137cd93aSLisandro Dalcin { 1783*137cd93aSLisandro Dalcin PetscFunctionBegin; 1784*137cd93aSLisandro Dalcin part->ops->view = PetscPartitionerView_PTScotch; 1785*137cd93aSLisandro Dalcin part->ops->destroy = PetscPartitionerDestroy_PTScotch; 1786*137cd93aSLisandro Dalcin part->ops->partition = PetscPartitionerPartition_PTScotch; 1787*137cd93aSLisandro Dalcin part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch; 1788*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1789*137cd93aSLisandro Dalcin } 1790*137cd93aSLisandro Dalcin 1791*137cd93aSLisandro Dalcin /*MC 1792*137cd93aSLisandro Dalcin PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library 1793*137cd93aSLisandro Dalcin 1794*137cd93aSLisandro Dalcin Level: intermediate 1795*137cd93aSLisandro Dalcin 1796*137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 1797*137cd93aSLisandro Dalcin M*/ 1798*137cd93aSLisandro Dalcin 1799*137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part) 1800*137cd93aSLisandro Dalcin { 1801*137cd93aSLisandro Dalcin PetscPartitioner_PTScotch *p; 1802*137cd93aSLisandro Dalcin PetscErrorCode ierr; 1803*137cd93aSLisandro Dalcin 1804*137cd93aSLisandro Dalcin PetscFunctionBegin; 1805*137cd93aSLisandro Dalcin PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 1806*137cd93aSLisandro Dalcin ierr = PetscNewLog(part, &p);CHKERRQ(ierr); 1807*137cd93aSLisandro Dalcin part->data = p; 1808*137cd93aSLisandro Dalcin 1809*137cd93aSLisandro Dalcin p->strategy = 0; 1810*137cd93aSLisandro Dalcin p->imbalance = 0.01; 1811*137cd93aSLisandro Dalcin 1812*137cd93aSLisandro Dalcin ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr); 1813*137cd93aSLisandro Dalcin ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr); 1814*137cd93aSLisandro Dalcin PetscFunctionReturn(0); 1815*137cd93aSLisandro Dalcin } 1816*137cd93aSLisandro Dalcin 1817*137cd93aSLisandro Dalcin 18185680f57bSMatthew G. Knepley /*@ 18195680f57bSMatthew G. Knepley DMPlexGetPartitioner - Get the mesh partitioner 18205680f57bSMatthew G. Knepley 18215680f57bSMatthew G. Knepley Not collective 18225680f57bSMatthew G. Knepley 18235680f57bSMatthew G. Knepley Input Parameter: 18245680f57bSMatthew G. Knepley . dm - The DM 18255680f57bSMatthew G. Knepley 18265680f57bSMatthew G. Knepley Output Parameter: 18275680f57bSMatthew G. Knepley . part - The PetscPartitioner 18285680f57bSMatthew G. Knepley 18295680f57bSMatthew G. Knepley Level: developer 18305680f57bSMatthew G. Knepley 183198599a47SLawrence Mitchell Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner. 183298599a47SLawrence Mitchell 183398599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate() 18345680f57bSMatthew G. Knepley @*/ 18355680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part) 18365680f57bSMatthew G. Knepley { 18375680f57bSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 18385680f57bSMatthew G. Knepley 18395680f57bSMatthew G. Knepley PetscFunctionBegin; 18405680f57bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 18415680f57bSMatthew G. Knepley PetscValidPointer(part, 2); 18425680f57bSMatthew G. Knepley *part = mesh->partitioner; 18435680f57bSMatthew G. Knepley PetscFunctionReturn(0); 18445680f57bSMatthew G. Knepley } 18455680f57bSMatthew G. Knepley 184671bb2955SLawrence Mitchell /*@ 184771bb2955SLawrence Mitchell DMPlexSetPartitioner - Set the mesh partitioner 184871bb2955SLawrence Mitchell 184971bb2955SLawrence Mitchell logically collective on dm and part 185071bb2955SLawrence Mitchell 185171bb2955SLawrence Mitchell Input Parameters: 185271bb2955SLawrence Mitchell + dm - The DM 185371bb2955SLawrence Mitchell - part - The partitioner 185471bb2955SLawrence Mitchell 185571bb2955SLawrence Mitchell Level: developer 185671bb2955SLawrence Mitchell 185771bb2955SLawrence Mitchell Note: Any existing PetscPartitioner will be destroyed. 185871bb2955SLawrence Mitchell 185971bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate() 186071bb2955SLawrence Mitchell @*/ 186171bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part) 186271bb2955SLawrence Mitchell { 186371bb2955SLawrence Mitchell DM_Plex *mesh = (DM_Plex *) dm->data; 186471bb2955SLawrence Mitchell PetscErrorCode ierr; 186571bb2955SLawrence Mitchell 186671bb2955SLawrence Mitchell PetscFunctionBegin; 186771bb2955SLawrence Mitchell PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 186871bb2955SLawrence Mitchell PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2); 186971bb2955SLawrence Mitchell ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr); 187071bb2955SLawrence Mitchell ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr); 187171bb2955SLawrence Mitchell mesh->partitioner = part; 187271bb2955SLawrence Mitchell PetscFunctionReturn(0); 187371bb2955SLawrence Mitchell } 187471bb2955SLawrence Mitchell 18756a5a2ffdSToby Isaac static PetscErrorCode DMPlexPartitionLabelClosure_Tree(DM dm, DMLabel label, PetscInt rank, PetscInt point, PetscBool up, PetscBool down) 1876270bba0cSToby Isaac { 1877270bba0cSToby Isaac PetscErrorCode ierr; 1878270bba0cSToby Isaac 1879270bba0cSToby Isaac PetscFunctionBegin; 18806a5a2ffdSToby Isaac if (up) { 18816a5a2ffdSToby Isaac PetscInt parent; 18826a5a2ffdSToby Isaac 1883270bba0cSToby Isaac ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr); 18846a5a2ffdSToby Isaac if (parent != point) { 18856a5a2ffdSToby Isaac PetscInt closureSize, *closure = NULL, i; 18866a5a2ffdSToby Isaac 1887270bba0cSToby Isaac ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 1888270bba0cSToby Isaac for (i = 0; i < closureSize; i++) { 1889270bba0cSToby Isaac PetscInt cpoint = closure[2*i]; 1890270bba0cSToby Isaac 1891270bba0cSToby Isaac ierr = DMLabelSetValue(label,cpoint,rank);CHKERRQ(ierr); 18926a5a2ffdSToby Isaac ierr = DMPlexPartitionLabelClosure_Tree(dm,label,rank,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr); 1893270bba0cSToby Isaac } 1894270bba0cSToby Isaac ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 18956a5a2ffdSToby Isaac } 18966a5a2ffdSToby Isaac } 18976a5a2ffdSToby Isaac if (down) { 18986a5a2ffdSToby Isaac PetscInt numChildren; 18996a5a2ffdSToby Isaac const PetscInt *children; 19006a5a2ffdSToby Isaac 19016a5a2ffdSToby Isaac ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr); 19026a5a2ffdSToby Isaac if (numChildren) { 19036a5a2ffdSToby Isaac PetscInt i; 19046a5a2ffdSToby Isaac 19056a5a2ffdSToby Isaac for (i = 0; i < numChildren; i++) { 19066a5a2ffdSToby Isaac PetscInt cpoint = children[i]; 19076a5a2ffdSToby Isaac 19086a5a2ffdSToby Isaac ierr = DMLabelSetValue(label,cpoint,rank);CHKERRQ(ierr); 19096a5a2ffdSToby Isaac ierr = DMPlexPartitionLabelClosure_Tree(dm,label,rank,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr); 19106a5a2ffdSToby Isaac } 19116a5a2ffdSToby Isaac } 19126a5a2ffdSToby Isaac } 1913270bba0cSToby Isaac PetscFunctionReturn(0); 1914270bba0cSToby Isaac } 1915270bba0cSToby Isaac 19165abbe4feSMichael Lange /*@ 19175abbe4feSMichael Lange DMPlexPartitionLabelClosure - Add the closure of all points to the partition label 19185abbe4feSMichael Lange 19195abbe4feSMichael Lange Input Parameters: 19205abbe4feSMichael Lange + dm - The DM 19215abbe4feSMichael Lange - label - DMLabel assinging ranks to remote roots 19225abbe4feSMichael Lange 19235abbe4feSMichael Lange Level: developer 19245abbe4feSMichael Lange 19255abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 19265abbe4feSMichael Lange @*/ 19275abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label) 19289ffc88e4SToby Isaac { 19295abbe4feSMichael Lange IS rankIS, pointIS; 19305abbe4feSMichael Lange const PetscInt *ranks, *points; 19315abbe4feSMichael Lange PetscInt numRanks, numPoints, r, p, c, closureSize; 19325abbe4feSMichael Lange PetscInt *closure = NULL; 19339ffc88e4SToby Isaac PetscErrorCode ierr; 19349ffc88e4SToby Isaac 19359ffc88e4SToby Isaac PetscFunctionBegin; 19365abbe4feSMichael Lange ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr); 19375abbe4feSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 19385abbe4feSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 19395abbe4feSMichael Lange for (r = 0; r < numRanks; ++r) { 19405abbe4feSMichael Lange const PetscInt rank = ranks[r]; 19419ffc88e4SToby Isaac 19425abbe4feSMichael Lange ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr); 19435abbe4feSMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 19445abbe4feSMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 19455abbe4feSMichael Lange for (p = 0; p < numPoints; ++p) { 19465abbe4feSMichael Lange ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1947270bba0cSToby Isaac for (c = 0; c < closureSize*2; c += 2) { 1948270bba0cSToby Isaac ierr = DMLabelSetValue(label, closure[c], rank);CHKERRQ(ierr); 19496a5a2ffdSToby Isaac ierr = DMPlexPartitionLabelClosure_Tree(dm,label,rank,closure[c],PETSC_TRUE,PETSC_TRUE);CHKERRQ(ierr); 1950270bba0cSToby Isaac } 19519ffc88e4SToby Isaac } 19525abbe4feSMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 19535abbe4feSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 19549ffc88e4SToby Isaac } 19557de78196SMichael Lange if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);} 19565abbe4feSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 19575abbe4feSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 19589ffc88e4SToby Isaac PetscFunctionReturn(0); 19599ffc88e4SToby Isaac } 19609ffc88e4SToby Isaac 196124d039d7SMichael Lange /*@ 196224d039d7SMichael Lange DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label 196324d039d7SMichael Lange 196424d039d7SMichael Lange Input Parameters: 196524d039d7SMichael Lange + dm - The DM 196624d039d7SMichael Lange - label - DMLabel assinging ranks to remote roots 196724d039d7SMichael Lange 196824d039d7SMichael Lange Level: developer 196924d039d7SMichael Lange 197024d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 197124d039d7SMichael Lange @*/ 197224d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label) 197370034214SMatthew G. Knepley { 197424d039d7SMichael Lange IS rankIS, pointIS; 197524d039d7SMichael Lange const PetscInt *ranks, *points; 197624d039d7SMichael Lange PetscInt numRanks, numPoints, r, p, a, adjSize; 197724d039d7SMichael Lange PetscInt *adj = NULL; 197870034214SMatthew G. Knepley PetscErrorCode ierr; 197970034214SMatthew G. Knepley 198070034214SMatthew G. Knepley PetscFunctionBegin; 198124d039d7SMichael Lange ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr); 198224d039d7SMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 198324d039d7SMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 198424d039d7SMichael Lange for (r = 0; r < numRanks; ++r) { 198524d039d7SMichael Lange const PetscInt rank = ranks[r]; 198670034214SMatthew G. Knepley 198724d039d7SMichael Lange ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr); 198824d039d7SMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 198924d039d7SMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 199070034214SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 199124d039d7SMichael Lange adjSize = PETSC_DETERMINE; 199224d039d7SMichael Lange ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr); 199324d039d7SMichael Lange for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);} 199470034214SMatthew G. Knepley } 199524d039d7SMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 199624d039d7SMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 199770034214SMatthew G. Knepley } 199824d039d7SMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 199924d039d7SMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 200024d039d7SMichael Lange ierr = PetscFree(adj);CHKERRQ(ierr); 200124d039d7SMichael Lange PetscFunctionReturn(0); 200270034214SMatthew G. Knepley } 200370034214SMatthew G. Knepley 2004be200f8dSMichael Lange /*@ 2005be200f8dSMichael Lange DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF 2006be200f8dSMichael Lange 2007be200f8dSMichael Lange Input Parameters: 2008be200f8dSMichael Lange + dm - The DM 2009be200f8dSMichael Lange - label - DMLabel assinging ranks to remote roots 2010be200f8dSMichael Lange 2011be200f8dSMichael Lange Level: developer 2012be200f8dSMichael Lange 2013be200f8dSMichael Lange Note: This is required when generating multi-level overlaps to capture 2014be200f8dSMichael Lange overlap points from non-neighbouring partitions. 2015be200f8dSMichael Lange 2016be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 2017be200f8dSMichael Lange @*/ 2018be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label) 2019be200f8dSMichael Lange { 2020be200f8dSMichael Lange MPI_Comm comm; 2021be200f8dSMichael Lange PetscMPIInt rank; 2022be200f8dSMichael Lange PetscSF sfPoint; 20235d04f6ebSMichael Lange DMLabel lblRoots, lblLeaves; 2024be200f8dSMichael Lange IS rankIS, pointIS; 2025be200f8dSMichael Lange const PetscInt *ranks; 2026be200f8dSMichael Lange PetscInt numRanks, r; 2027be200f8dSMichael Lange PetscErrorCode ierr; 2028be200f8dSMichael Lange 2029be200f8dSMichael Lange PetscFunctionBegin; 2030be200f8dSMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 2031be200f8dSMichael Lange ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 2032be200f8dSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 20335d04f6ebSMichael Lange /* Pull point contributions from remote leaves into local roots */ 20345d04f6ebSMichael Lange ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr); 20355d04f6ebSMichael Lange ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr); 20365d04f6ebSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 20375d04f6ebSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 20385d04f6ebSMichael Lange for (r = 0; r < numRanks; ++r) { 20395d04f6ebSMichael Lange const PetscInt remoteRank = ranks[r]; 20405d04f6ebSMichael Lange if (remoteRank == rank) continue; 20415d04f6ebSMichael Lange ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr); 20425d04f6ebSMichael Lange ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr); 20435d04f6ebSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 20445d04f6ebSMichael Lange } 20455d04f6ebSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 20465d04f6ebSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 20475d04f6ebSMichael Lange ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr); 2048be200f8dSMichael Lange /* Push point contributions from roots into remote leaves */ 2049be200f8dSMichael Lange ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr); 2050be200f8dSMichael Lange ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr); 2051be200f8dSMichael Lange ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr); 2052be200f8dSMichael Lange ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr); 2053be200f8dSMichael Lange for (r = 0; r < numRanks; ++r) { 2054be200f8dSMichael Lange const PetscInt remoteRank = ranks[r]; 2055be200f8dSMichael Lange if (remoteRank == rank) continue; 2056be200f8dSMichael Lange ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr); 2057be200f8dSMichael Lange ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr); 2058be200f8dSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2059be200f8dSMichael Lange } 2060be200f8dSMichael Lange ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr); 2061be200f8dSMichael Lange ierr = ISDestroy(&rankIS);CHKERRQ(ierr); 2062be200f8dSMichael Lange ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr); 2063be200f8dSMichael Lange PetscFunctionReturn(0); 2064be200f8dSMichael Lange } 2065be200f8dSMichael Lange 20661fd9873aSMichael Lange /*@ 20671fd9873aSMichael Lange DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label 20681fd9873aSMichael Lange 20691fd9873aSMichael Lange Input Parameters: 20701fd9873aSMichael Lange + dm - The DM 20711fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots 20721fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank 20731fd9873aSMichael Lange 20741fd9873aSMichael Lange Output Parameter: 20751fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots 20761fd9873aSMichael Lange 20771fd9873aSMichael Lange Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The 20781fd9873aSMichael Lange resulting leafLabel is a receiver mapping of remote roots to their parent rank. 20791fd9873aSMichael Lange 20801fd9873aSMichael Lange Level: developer 20811fd9873aSMichael Lange 20821fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap 20831fd9873aSMichael Lange @*/ 20841fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel) 20851fd9873aSMichael Lange { 20861fd9873aSMichael Lange MPI_Comm comm; 20879852e123SBarry Smith PetscMPIInt rank, size; 20889852e123SBarry Smith PetscInt p, n, numNeighbors, ssize, l, nleaves; 20891fd9873aSMichael Lange PetscSF sfPoint; 20901fd9873aSMichael Lange PetscSFNode *rootPoints, *leafPoints; 20911fd9873aSMichael Lange PetscSection rootSection, leafSection; 20921fd9873aSMichael Lange const PetscSFNode *remote; 20931fd9873aSMichael Lange const PetscInt *local, *neighbors; 20941fd9873aSMichael Lange IS valueIS; 20951fd9873aSMichael Lange PetscErrorCode ierr; 20961fd9873aSMichael Lange 20971fd9873aSMichael Lange PetscFunctionBegin; 20981fd9873aSMichael Lange ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 20991fd9873aSMichael Lange ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 21009852e123SBarry Smith ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 21011fd9873aSMichael Lange ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 21021fd9873aSMichael Lange 21031fd9873aSMichael Lange /* Convert to (point, rank) and use actual owners */ 21041fd9873aSMichael Lange ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr); 21059852e123SBarry Smith ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr); 21061fd9873aSMichael Lange ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr); 21071fd9873aSMichael Lange ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr); 21081fd9873aSMichael Lange ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr); 21091fd9873aSMichael Lange for (n = 0; n < numNeighbors; ++n) { 21101fd9873aSMichael Lange PetscInt numPoints; 21111fd9873aSMichael Lange 21121fd9873aSMichael Lange ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr); 21131fd9873aSMichael Lange ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr); 21141fd9873aSMichael Lange } 21151fd9873aSMichael Lange ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr); 21169852e123SBarry Smith ierr = PetscSectionGetStorageSize(rootSection, &ssize);CHKERRQ(ierr); 21179852e123SBarry Smith ierr = PetscMalloc1(ssize, &rootPoints);CHKERRQ(ierr); 21181fd9873aSMichael Lange ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr); 21191fd9873aSMichael Lange for (n = 0; n < numNeighbors; ++n) { 21201fd9873aSMichael Lange IS pointIS; 21211fd9873aSMichael Lange const PetscInt *points; 21221fd9873aSMichael Lange PetscInt off, numPoints, p; 21231fd9873aSMichael Lange 21241fd9873aSMichael Lange ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr); 21251fd9873aSMichael Lange ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr); 21261fd9873aSMichael Lange ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 21271fd9873aSMichael Lange ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 21281fd9873aSMichael Lange for (p = 0; p < numPoints; ++p) { 2129f8987ae8SMichael Lange if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);} 2130f8987ae8SMichael Lange else {l = -1;} 21311fd9873aSMichael Lange if (l >= 0) {rootPoints[off+p] = remote[l];} 21321fd9873aSMichael Lange else {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;} 21331fd9873aSMichael Lange } 21341fd9873aSMichael Lange ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 21351fd9873aSMichael Lange ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 21361fd9873aSMichael Lange } 21371fd9873aSMichael Lange ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr); 21381fd9873aSMichael Lange ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 21391fd9873aSMichael Lange /* Communicate overlap */ 21401fd9873aSMichael Lange ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr); 21411fd9873aSMichael Lange ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr); 21421fd9873aSMichael Lange /* Filter remote contributions (ovLeafPoints) into the overlapSF */ 21439852e123SBarry Smith ierr = PetscSectionGetStorageSize(leafSection, &ssize);CHKERRQ(ierr); 21449852e123SBarry Smith for (p = 0; p < ssize; p++) { 21451fd9873aSMichael Lange ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr); 21461fd9873aSMichael Lange } 21471fd9873aSMichael Lange ierr = PetscFree(rootPoints);CHKERRQ(ierr); 21481fd9873aSMichael Lange ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr); 21491fd9873aSMichael Lange ierr = PetscFree(leafPoints);CHKERRQ(ierr); 21501fd9873aSMichael Lange ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr); 21511fd9873aSMichael Lange PetscFunctionReturn(0); 21521fd9873aSMichael Lange } 21531fd9873aSMichael Lange 2154aa3148a8SMichael Lange /*@ 2155aa3148a8SMichael Lange DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points 2156aa3148a8SMichael Lange 2157aa3148a8SMichael Lange Input Parameters: 2158aa3148a8SMichael Lange + dm - The DM 2159aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots 2160aa3148a8SMichael Lange 2161aa3148a8SMichael Lange Output Parameter: 2162aa3148a8SMichael Lange - sf - The star forest communication context encapsulating the defined mapping 2163aa3148a8SMichael Lange 2164aa3148a8SMichael Lange Note: The incoming label is a receiver mapping of remote points to their parent rank. 2165aa3148a8SMichael Lange 2166aa3148a8SMichael Lange Level: developer 2167aa3148a8SMichael Lange 2168aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap 2169aa3148a8SMichael Lange @*/ 2170aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf) 2171aa3148a8SMichael Lange { 21729852e123SBarry Smith PetscMPIInt rank, size; 217343f7d02bSMichael Lange PetscInt n, numRemote, p, numPoints, pStart, pEnd, idx = 0; 2174aa3148a8SMichael Lange PetscSFNode *remotePoints; 217543f7d02bSMichael Lange IS remoteRootIS; 217643f7d02bSMichael Lange const PetscInt *remoteRoots; 2177aa3148a8SMichael Lange PetscErrorCode ierr; 2178aa3148a8SMichael Lange 2179aa3148a8SMichael Lange PetscFunctionBegin; 218043f7d02bSMichael Lange ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 21819852e123SBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr); 2182aa3148a8SMichael Lange 21839852e123SBarry Smith for (numRemote = 0, n = 0; n < size; ++n) { 2184aa3148a8SMichael Lange ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr); 2185aa3148a8SMichael Lange numRemote += numPoints; 2186aa3148a8SMichael Lange } 2187aa3148a8SMichael Lange ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr); 218843f7d02bSMichael Lange /* Put owned points first */ 218943f7d02bSMichael Lange ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr); 219043f7d02bSMichael Lange if (numPoints > 0) { 219143f7d02bSMichael Lange ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr); 219243f7d02bSMichael Lange ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 219343f7d02bSMichael Lange for (p = 0; p < numPoints; p++) { 219443f7d02bSMichael Lange remotePoints[idx].index = remoteRoots[p]; 219543f7d02bSMichael Lange remotePoints[idx].rank = rank; 219643f7d02bSMichael Lange idx++; 219743f7d02bSMichael Lange } 219843f7d02bSMichael Lange ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 219943f7d02bSMichael Lange ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr); 220043f7d02bSMichael Lange } 220143f7d02bSMichael Lange /* Now add remote points */ 22029852e123SBarry Smith for (n = 0; n < size; ++n) { 2203aa3148a8SMichael Lange ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr); 220443f7d02bSMichael Lange if (numPoints <= 0 || n == rank) continue; 2205aa3148a8SMichael Lange ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr); 2206aa3148a8SMichael Lange ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 2207aa3148a8SMichael Lange for (p = 0; p < numPoints; p++) { 2208aa3148a8SMichael Lange remotePoints[idx].index = remoteRoots[p]; 2209aa3148a8SMichael Lange remotePoints[idx].rank = n; 2210aa3148a8SMichael Lange idx++; 2211aa3148a8SMichael Lange } 2212aa3148a8SMichael Lange ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr); 2213aa3148a8SMichael Lange ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr); 2214aa3148a8SMichael Lange } 2215aa3148a8SMichael Lange ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr); 2216aa3148a8SMichael Lange ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2217aa3148a8SMichael Lange ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr); 221870034214SMatthew G. Knepley PetscFunctionReturn(0); 221970034214SMatthew G. Knepley } 2220