xref: /petsc/src/dm/impls/plex/plexpartition.c (revision 8b879b415c6f21ffb230bdd8eedaacafe37b4075)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2e8f14785SLisandro Dalcin #include <petsc/private/hashseti.h>
370034214SMatthew G. Knepley 
477623264SMatthew G. Knepley PetscClassId PETSCPARTITIONER_CLASSID = 0;
577623264SMatthew G. Knepley 
677623264SMatthew G. Knepley PetscFunctionList PetscPartitionerList              = NULL;
777623264SMatthew G. Knepley PetscBool         PetscPartitionerRegisterAllCalled = PETSC_FALSE;
877623264SMatthew G. Knepley 
977623264SMatthew G. Knepley PetscBool ChacoPartitionercite = PETSC_FALSE;
1077623264SMatthew G. Knepley const char ChacoPartitionerCitation[] = "@inproceedings{Chaco95,\n"
1177623264SMatthew G. Knepley                                "  author    = {Bruce Hendrickson and Robert Leland},\n"
1277623264SMatthew G. Knepley                                "  title     = {A multilevel algorithm for partitioning graphs},\n"
1377623264SMatthew G. Knepley                                "  booktitle = {Supercomputing '95: Proceedings of the 1995 ACM/IEEE Conference on Supercomputing (CDROM)},"
1477623264SMatthew G. Knepley                                "  isbn      = {0-89791-816-9},\n"
1577623264SMatthew G. Knepley                                "  pages     = {28},\n"
1677623264SMatthew G. Knepley                                "  doi       = {http://doi.acm.org/10.1145/224170.224228},\n"
1777623264SMatthew G. Knepley                                "  publisher = {ACM Press},\n"
1877623264SMatthew G. Knepley                                "  address   = {New York},\n"
1977623264SMatthew G. Knepley                                "  year      = {1995}\n}\n";
2077623264SMatthew G. Knepley 
2177623264SMatthew G. Knepley PetscBool ParMetisPartitionercite = PETSC_FALSE;
2277623264SMatthew G. Knepley const char ParMetisPartitionerCitation[] = "@article{KarypisKumar98,\n"
2377623264SMatthew G. Knepley                                "  author  = {George Karypis and Vipin Kumar},\n"
2477623264SMatthew G. Knepley                                "  title   = {A Parallel Algorithm for Multilevel Graph Partitioning and Sparse Matrix Ordering},\n"
2577623264SMatthew G. Knepley                                "  journal = {Journal of Parallel and Distributed Computing},\n"
2677623264SMatthew G. Knepley                                "  volume  = {48},\n"
2777623264SMatthew G. Knepley                                "  pages   = {71--85},\n"
2877623264SMatthew G. Knepley                                "  year    = {1998}\n}\n";
2977623264SMatthew G. Knepley 
30532c4e7dSMichael Lange /*@C
31532c4e7dSMichael Lange   DMPlexCreatePartitionerGraph - Create a CSR graph of point connections for the partitioner
32532c4e7dSMichael Lange 
33532c4e7dSMichael Lange   Input Parameters:
34532c4e7dSMichael Lange + dm      - The mesh DM dm
35532c4e7dSMichael Lange - height  - Height of the strata from which to construct the graph
36532c4e7dSMichael Lange 
37532c4e7dSMichael Lange   Output Parameter:
38532c4e7dSMichael Lange + numVertices     - Number of vertices in the graph
393fa7752dSToby Isaac . offsets         - Point offsets in the graph
403fa7752dSToby Isaac . adjacency       - Point connectivity in the graph
413fa7752dSToby 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.
42532c4e7dSMichael Lange 
43b0441da4SMatthew G. Knepley   The user can control the definition of adjacency for the mesh using DMSetAdjacency(). They should choose the combination appropriate for the function
44532c4e7dSMichael Lange   representation on the mesh.
45532c4e7dSMichael Lange 
46532c4e7dSMichael Lange   Level: developer
47532c4e7dSMichael Lange 
48b0441da4SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate(), DMSetAdjacency()
49532c4e7dSMichael Lange @*/
503fa7752dSToby Isaac PetscErrorCode DMPlexCreatePartitionerGraph(DM dm, PetscInt height, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency, IS *globalNumbering)
51532c4e7dSMichael Lange {
52ffbd6163SMatthew G. Knepley   PetscInt       dim, depth, p, pStart, pEnd, a, adjSize, idx, size;
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;
608f4e72b9SMatthew G. Knepley   PetscInt       *adjCells = NULL, *remoteCells = NULL;
618f4e72b9SMatthew G. Knepley   const PetscInt *local;
628f4e72b9SMatthew G. Knepley   PetscInt       nroots, nleaves, l;
63532c4e7dSMichael Lange   PetscMPIInt    rank;
64532c4e7dSMichael Lange   PetscErrorCode ierr;
65532c4e7dSMichael Lange 
66532c4e7dSMichael Lange   PetscFunctionBegin;
67532c4e7dSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
68ffbd6163SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
69ffbd6163SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
70ffbd6163SMatthew G. Knepley   if (dim != depth) {
71ffbd6163SMatthew G. Knepley     /* We do not handle the uninterpolated case here */
72ffbd6163SMatthew G. Knepley     ierr = DMPlexCreateNeighborCSR(dm, height, numVertices, offsets, adjacency);CHKERRQ(ierr);
73ffbd6163SMatthew G. Knepley     /* DMPlexCreateNeighborCSR does not make a numbering */
74ffbd6163SMatthew G. Knepley     if (globalNumbering) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, globalNumbering);CHKERRQ(ierr);}
75ffbd6163SMatthew G. Knepley     /* Different behavior for empty graphs */
76ffbd6163SMatthew G. Knepley     if (!*numVertices) {
77ffbd6163SMatthew G. Knepley       ierr = PetscMalloc1(1, offsets);CHKERRQ(ierr);
78ffbd6163SMatthew G. Knepley       (*offsets)[0] = 0;
79ffbd6163SMatthew G. Knepley     }
80ffbd6163SMatthew G. Knepley     /* Broken in parallel */
81ffbd6163SMatthew G. Knepley     if (rank && *numVertices) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Parallel partitioning of uninterpolated meshes not supported");
82ffbd6163SMatthew G. Knepley     PetscFunctionReturn(0);
83ffbd6163SMatthew G. Knepley   }
84532c4e7dSMichael Lange   ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr);
858cfe4c1fSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
868cfe4c1fSMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
87532c4e7dSMichael Lange   /* Build adjacency graph via a section/segbuffer */
88532c4e7dSMichael Lange   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
89532c4e7dSMichael Lange   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
90532c4e7dSMichael Lange   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr);
91532c4e7dSMichael Lange   /* Always use FVM adjacency to create partitioner graph */
92b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
93b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(dm, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
94f0927f4eSMatthew G. Knepley   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &cellNumbering);CHKERRQ(ierr);
953fa7752dSToby Isaac   if (globalNumbering) {
963fa7752dSToby Isaac     ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr);
973fa7752dSToby Isaac     *globalNumbering = cellNumbering;
983fa7752dSToby Isaac   }
998cfe4c1fSMichael Lange   ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
1008f4e72b9SMatthew G. Knepley   /* For all boundary faces (including faces adjacent to a ghost cell), record the local cell in adjCells
1018f4e72b9SMatthew G. Knepley      Broadcast adjCells to remoteCells (to get cells from roots) and Reduce adjCells to remoteCells (to get cells from leaves)
1028f4e72b9SMatthew G. Knepley    */
1038f4e72b9SMatthew G. Knepley   ierr = PetscSFGetGraph(dm->sf, &nroots, &nleaves, &local, NULL);CHKERRQ(ierr);
1048f4e72b9SMatthew G. Knepley   if (nroots >= 0) {
1058f4e72b9SMatthew G. Knepley     PetscInt fStart, fEnd, f;
1068f4e72b9SMatthew G. Knepley 
1078f4e72b9SMatthew G. Knepley     ierr = PetscCalloc2(nroots, &adjCells, nroots, &remoteCells);CHKERRQ(ierr);
1088f4e72b9SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1098f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) adjCells[l] = -3;
1108f4e72b9SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
1118f4e72b9SMatthew G. Knepley       const PetscInt *support;
1128f4e72b9SMatthew G. Knepley       PetscInt        supportSize;
1138f4e72b9SMatthew G. Knepley 
1148f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
1158f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
1168f4e72b9SMatthew G. Knepley       if (supportSize == 1) adjCells[f] = cellNum[support[0]];
1178f4e72b9SMatthew G. Knepley       else if (supportSize == 2) {
1188f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[0], nleaves, local, &p);CHKERRQ(ierr);
1198f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[1]];
1208f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[1], nleaves, local, &p);CHKERRQ(ierr);
1218f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[0]];
1228f4e72b9SMatthew G. Knepley       }
1238f4e72b9SMatthew G. Knepley     }
1248f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) remoteCells[l] = -1;
1258f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastBegin(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
1268f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastEnd(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
1278f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceBegin(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
1288f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceEnd(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
1298f4e72b9SMatthew G. Knepley   }
1308f4e72b9SMatthew G. Knepley   /* Combine local and global adjacencies */
1318cfe4c1fSMichael Lange   for (*numVertices = 0, p = pStart; p < pEnd; p++) {
1328f4e72b9SMatthew G. Knepley     const PetscInt *cone;
1338f4e72b9SMatthew G. Knepley     PetscInt        coneSize, c;
1348f4e72b9SMatthew G. Knepley 
1358cfe4c1fSMichael Lange     /* Skip non-owned cells in parallel (ParMetis expects no overlap) */
1368cfe4c1fSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
1378f4e72b9SMatthew G. Knepley     /* Add remote cells */
1388f4e72b9SMatthew G. Knepley     if (remoteCells) {
1398f4e72b9SMatthew G. Knepley       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1408f4e72b9SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1418f4e72b9SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
1428f4e72b9SMatthew G. Knepley         if (remoteCells[cone[c]] != -1) {
1438f4e72b9SMatthew G. Knepley           PetscInt *PETSC_RESTRICT pBuf;
1448f4e72b9SMatthew G. Knepley 
1458f4e72b9SMatthew G. Knepley           ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
1468f4e72b9SMatthew G. Knepley           ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
1478f4e72b9SMatthew G. Knepley           *pBuf = remoteCells[cone[c]];
1488f4e72b9SMatthew G. Knepley         }
1498f4e72b9SMatthew G. Knepley       }
1508f4e72b9SMatthew G. Knepley     }
1518f4e72b9SMatthew G. Knepley     /* Add local cells */
152532c4e7dSMichael Lange     adjSize = PETSC_DETERMINE;
153532c4e7dSMichael Lange     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
154532c4e7dSMichael Lange     for (a = 0; a < adjSize; ++a) {
155532c4e7dSMichael Lange       const PetscInt point = adj[a];
156532c4e7dSMichael Lange       if (point != p && pStart <= point && point < pEnd) {
157532c4e7dSMichael Lange         PetscInt *PETSC_RESTRICT pBuf;
158532c4e7dSMichael Lange         ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
159532c4e7dSMichael Lange         ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
1608f4e72b9SMatthew G. Knepley         *pBuf = cellNum[point];
161532c4e7dSMichael Lange       }
162532c4e7dSMichael Lange     }
1638cfe4c1fSMichael Lange     (*numVertices)++;
164532c4e7dSMichael Lange   }
1658f4e72b9SMatthew G. Knepley   ierr = PetscFree2(adjCells, remoteCells);CHKERRQ(ierr);
166b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(dm, useCone, useClosure);CHKERRQ(ierr);
167532c4e7dSMichael Lange   /* Derive CSR graph from section/segbuffer */
168532c4e7dSMichael Lange   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
169532c4e7dSMichael Lange   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
170389e55d8SMichael Lange   ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr);
17143f7d02bSMichael Lange   for (idx = 0, p = pStart; p < pEnd; p++) {
17243f7d02bSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
17343f7d02bSMichael Lange     ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr);
17443f7d02bSMichael Lange   }
175532c4e7dSMichael Lange   vOffsets[*numVertices] = size;
176532c4e7dSMichael Lange   if (offsets) *offsets = vOffsets;
177389e55d8SMichael Lange   ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr);
1788cfe4c1fSMichael Lange   ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
179f0927f4eSMatthew G. Knepley   ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr);
180389e55d8SMichael Lange   if (adjacency) *adjacency = graph;
181532c4e7dSMichael Lange   /* Clean up */
182532c4e7dSMichael Lange   ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr);
183532c4e7dSMichael Lange   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
184532c4e7dSMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
185532c4e7dSMichael Lange   PetscFunctionReturn(0);
186532c4e7dSMichael Lange }
187532c4e7dSMichael Lange 
188d5577e40SMatthew G. Knepley /*@C
189d5577e40SMatthew G. Knepley   DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format.
190d5577e40SMatthew G. Knepley 
191d5577e40SMatthew G. Knepley   Collective
192d5577e40SMatthew G. Knepley 
193d5577e40SMatthew G. Knepley   Input Arguments:
194d5577e40SMatthew G. Knepley + dm - The DMPlex
195d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0)
196d5577e40SMatthew G. Knepley 
197d5577e40SMatthew G. Knepley   Output Arguments:
198d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh.
199d5577e40SMatthew G. Knepley . offsets     - The offset to the adjacency list for each cell
200d5577e40SMatthew G. Knepley - adjacency   - The adjacency list for all cells
201d5577e40SMatthew G. Knepley 
202d5577e40SMatthew G. Knepley   Note: This is suitable for input to a mesh partitioner like ParMetis.
203d5577e40SMatthew G. Knepley 
204d5577e40SMatthew G. Knepley   Level: advanced
205d5577e40SMatthew G. Knepley 
206d5577e40SMatthew G. Knepley .seealso: DMPlexCreate()
207d5577e40SMatthew G. Knepley @*/
20870034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency)
20970034214SMatthew G. Knepley {
21070034214SMatthew G. Knepley   const PetscInt maxFaceCases = 30;
21170034214SMatthew G. Knepley   PetscInt       numFaceCases = 0;
21270034214SMatthew G. Knepley   PetscInt       numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */
21370034214SMatthew G. Knepley   PetscInt      *off, *adj;
21470034214SMatthew G. Knepley   PetscInt      *neighborCells = NULL;
21570034214SMatthew G. Knepley   PetscInt       dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell;
21670034214SMatthew G. Knepley   PetscErrorCode ierr;
21770034214SMatthew G. Knepley 
21870034214SMatthew G. Knepley   PetscFunctionBegin;
21970034214SMatthew G. Knepley   /* For parallel partitioning, I think you have to communicate supports */
220c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
22170034214SMatthew G. Knepley   cellDim = dim - cellHeight;
22270034214SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
22370034214SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
22470034214SMatthew G. Knepley   if (cEnd - cStart == 0) {
22570034214SMatthew G. Knepley     if (numVertices) *numVertices = 0;
22670034214SMatthew G. Knepley     if (offsets)   *offsets   = NULL;
22770034214SMatthew G. Knepley     if (adjacency) *adjacency = NULL;
22870034214SMatthew G. Knepley     PetscFunctionReturn(0);
22970034214SMatthew G. Knepley   }
23070034214SMatthew G. Knepley   numCells  = cEnd - cStart;
23170034214SMatthew G. Knepley   faceDepth = depth - cellHeight;
23270034214SMatthew G. Knepley   if (dim == depth) {
23370034214SMatthew G. Knepley     PetscInt f, fStart, fEnd;
23470034214SMatthew G. Knepley 
23570034214SMatthew G. Knepley     ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
23670034214SMatthew G. Knepley     /* Count neighboring cells */
23770034214SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
23870034214SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
23970034214SMatthew G. Knepley       const PetscInt *support;
24070034214SMatthew G. Knepley       PetscInt        supportSize;
24170034214SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
24270034214SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
24370034214SMatthew G. Knepley       if (supportSize == 2) {
2449ffc88e4SToby Isaac         PetscInt numChildren;
2459ffc88e4SToby Isaac 
2469ffc88e4SToby Isaac         ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2479ffc88e4SToby Isaac         if (!numChildren) {
24870034214SMatthew G. Knepley           ++off[support[0]-cStart+1];
24970034214SMatthew G. Knepley           ++off[support[1]-cStart+1];
25070034214SMatthew G. Knepley         }
25170034214SMatthew G. Knepley       }
2529ffc88e4SToby Isaac     }
25370034214SMatthew G. Knepley     /* Prefix sum */
25470034214SMatthew G. Knepley     for (c = 1; c <= numCells; ++c) off[c] += off[c-1];
25570034214SMatthew G. Knepley     if (adjacency) {
25670034214SMatthew G. Knepley       PetscInt *tmp;
25770034214SMatthew G. Knepley 
25870034214SMatthew G. Knepley       ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
259854ce69bSBarry Smith       ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr);
26070034214SMatthew G. Knepley       ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr);
26170034214SMatthew G. Knepley       /* Get neighboring cells */
26270034214SMatthew G. Knepley       for (f = fStart; f < fEnd; ++f) {
26370034214SMatthew G. Knepley         const PetscInt *support;
26470034214SMatthew G. Knepley         PetscInt        supportSize;
26570034214SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
26670034214SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
26770034214SMatthew G. Knepley         if (supportSize == 2) {
2689ffc88e4SToby Isaac           PetscInt numChildren;
2699ffc88e4SToby Isaac 
2709ffc88e4SToby Isaac           ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2719ffc88e4SToby Isaac           if (!numChildren) {
27270034214SMatthew G. Knepley             adj[tmp[support[0]-cStart]++] = support[1];
27370034214SMatthew G. Knepley             adj[tmp[support[1]-cStart]++] = support[0];
27470034214SMatthew G. Knepley           }
27570034214SMatthew G. Knepley         }
2769ffc88e4SToby Isaac       }
27770034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
27870034214SMatthew 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);
27970034214SMatthew G. Knepley #endif
28070034214SMatthew G. Knepley       ierr = PetscFree(tmp);CHKERRQ(ierr);
28170034214SMatthew G. Knepley     }
28270034214SMatthew G. Knepley     if (numVertices) *numVertices = numCells;
28370034214SMatthew G. Knepley     if (offsets)   *offsets   = off;
28470034214SMatthew G. Knepley     if (adjacency) *adjacency = adj;
28570034214SMatthew G. Knepley     PetscFunctionReturn(0);
28670034214SMatthew G. Knepley   }
28770034214SMatthew G. Knepley   /* Setup face recognition */
28870034214SMatthew G. Knepley   if (faceDepth == 1) {
28970034214SMatthew 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 */
29070034214SMatthew G. Knepley 
29170034214SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
29270034214SMatthew G. Knepley       PetscInt corners;
29370034214SMatthew G. Knepley 
29470034214SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr);
29570034214SMatthew G. Knepley       if (!cornersSeen[corners]) {
29670034214SMatthew G. Knepley         PetscInt nFV;
29770034214SMatthew G. Knepley 
29870034214SMatthew G. Knepley         if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases");
29970034214SMatthew G. Knepley         cornersSeen[corners] = 1;
30070034214SMatthew G. Knepley 
30170034214SMatthew G. Knepley         ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr);
30270034214SMatthew G. Knepley 
30370034214SMatthew G. Knepley         numFaceVertices[numFaceCases++] = nFV;
30470034214SMatthew G. Knepley       }
30570034214SMatthew G. Knepley     }
30670034214SMatthew G. Knepley   }
30770034214SMatthew G. Knepley   ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
30870034214SMatthew G. Knepley   /* Count neighboring cells */
30970034214SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
31070034214SMatthew G. Knepley     PetscInt numNeighbors = PETSC_DETERMINE, n;
31170034214SMatthew G. Knepley 
3128b0b4c70SToby Isaac     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
31370034214SMatthew G. Knepley     /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
31470034214SMatthew G. Knepley     for (n = 0; n < numNeighbors; ++n) {
31570034214SMatthew G. Knepley       PetscInt        cellPair[2];
31670034214SMatthew G. Knepley       PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
31770034214SMatthew G. Knepley       PetscInt        meetSize = 0;
31870034214SMatthew G. Knepley       const PetscInt *meet    = NULL;
31970034214SMatthew G. Knepley 
32070034214SMatthew G. Knepley       cellPair[0] = cell; cellPair[1] = neighborCells[n];
32170034214SMatthew G. Knepley       if (cellPair[0] == cellPair[1]) continue;
32270034214SMatthew G. Knepley       if (!found) {
32370034214SMatthew G. Knepley         ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
32470034214SMatthew G. Knepley         if (meetSize) {
32570034214SMatthew G. Knepley           PetscInt f;
32670034214SMatthew G. Knepley 
32770034214SMatthew G. Knepley           for (f = 0; f < numFaceCases; ++f) {
32870034214SMatthew G. Knepley             if (numFaceVertices[f] == meetSize) {
32970034214SMatthew G. Knepley               found = PETSC_TRUE;
33070034214SMatthew G. Knepley               break;
33170034214SMatthew G. Knepley             }
33270034214SMatthew G. Knepley           }
33370034214SMatthew G. Knepley         }
33470034214SMatthew G. Knepley         ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
33570034214SMatthew G. Knepley       }
33670034214SMatthew G. Knepley       if (found) ++off[cell-cStart+1];
33770034214SMatthew G. Knepley     }
33870034214SMatthew G. Knepley   }
33970034214SMatthew G. Knepley   /* Prefix sum */
34070034214SMatthew G. Knepley   for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1];
34170034214SMatthew G. Knepley 
34270034214SMatthew G. Knepley   if (adjacency) {
34370034214SMatthew G. Knepley     ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
34470034214SMatthew G. Knepley     /* Get neighboring cells */
34570034214SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
34670034214SMatthew G. Knepley       PetscInt numNeighbors = PETSC_DETERMINE, n;
34770034214SMatthew G. Knepley       PetscInt cellOffset   = 0;
34870034214SMatthew G. Knepley 
3498b0b4c70SToby Isaac       ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
35070034214SMatthew G. Knepley       /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
35170034214SMatthew G. Knepley       for (n = 0; n < numNeighbors; ++n) {
35270034214SMatthew G. Knepley         PetscInt        cellPair[2];
35370034214SMatthew G. Knepley         PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
35470034214SMatthew G. Knepley         PetscInt        meetSize = 0;
35570034214SMatthew G. Knepley         const PetscInt *meet    = NULL;
35670034214SMatthew G. Knepley 
35770034214SMatthew G. Knepley         cellPair[0] = cell; cellPair[1] = neighborCells[n];
35870034214SMatthew G. Knepley         if (cellPair[0] == cellPair[1]) continue;
35970034214SMatthew G. Knepley         if (!found) {
36070034214SMatthew G. Knepley           ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
36170034214SMatthew G. Knepley           if (meetSize) {
36270034214SMatthew G. Knepley             PetscInt f;
36370034214SMatthew G. Knepley 
36470034214SMatthew G. Knepley             for (f = 0; f < numFaceCases; ++f) {
36570034214SMatthew G. Knepley               if (numFaceVertices[f] == meetSize) {
36670034214SMatthew G. Knepley                 found = PETSC_TRUE;
36770034214SMatthew G. Knepley                 break;
36870034214SMatthew G. Knepley               }
36970034214SMatthew G. Knepley             }
37070034214SMatthew G. Knepley           }
37170034214SMatthew G. Knepley           ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
37270034214SMatthew G. Knepley         }
37370034214SMatthew G. Knepley         if (found) {
37470034214SMatthew G. Knepley           adj[off[cell-cStart]+cellOffset] = neighborCells[n];
37570034214SMatthew G. Knepley           ++cellOffset;
37670034214SMatthew G. Knepley         }
37770034214SMatthew G. Knepley       }
37870034214SMatthew G. Knepley     }
37970034214SMatthew G. Knepley   }
38070034214SMatthew G. Knepley   ierr = PetscFree(neighborCells);CHKERRQ(ierr);
38170034214SMatthew G. Knepley   if (numVertices) *numVertices = numCells;
38270034214SMatthew G. Knepley   if (offsets)   *offsets   = off;
38370034214SMatthew G. Knepley   if (adjacency) *adjacency = adj;
38470034214SMatthew G. Knepley   PetscFunctionReturn(0);
38570034214SMatthew G. Knepley }
38670034214SMatthew G. Knepley 
38777623264SMatthew G. Knepley /*@C
38877623264SMatthew G. Knepley   PetscPartitionerRegister - Adds a new PetscPartitioner implementation
38977623264SMatthew G. Knepley 
39077623264SMatthew G. Knepley   Not Collective
39177623264SMatthew G. Knepley 
39277623264SMatthew G. Knepley   Input Parameters:
39377623264SMatthew G. Knepley + name        - The name of a new user-defined creation routine
39477623264SMatthew G. Knepley - create_func - The creation routine itself
39577623264SMatthew G. Knepley 
39677623264SMatthew G. Knepley   Notes:
39777623264SMatthew G. Knepley   PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners
39877623264SMatthew G. Knepley 
39977623264SMatthew G. Knepley   Sample usage:
40077623264SMatthew G. Knepley .vb
40177623264SMatthew G. Knepley     PetscPartitionerRegister("my_part", MyPetscPartitionerCreate);
40277623264SMatthew G. Knepley .ve
40377623264SMatthew G. Knepley 
40477623264SMatthew G. Knepley   Then, your PetscPartitioner type can be chosen with the procedural interface via
40577623264SMatthew G. Knepley .vb
40677623264SMatthew G. Knepley     PetscPartitionerCreate(MPI_Comm, PetscPartitioner *);
40777623264SMatthew G. Knepley     PetscPartitionerSetType(PetscPartitioner, "my_part");
40877623264SMatthew G. Knepley .ve
40977623264SMatthew G. Knepley    or at runtime via the option
41077623264SMatthew G. Knepley .vb
41177623264SMatthew G. Knepley     -petscpartitioner_type my_part
41277623264SMatthew G. Knepley .ve
41377623264SMatthew G. Knepley 
41477623264SMatthew G. Knepley   Level: advanced
41577623264SMatthew G. Knepley 
41677623264SMatthew G. Knepley .keywords: PetscPartitioner, register
41777623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy()
41877623264SMatthew G. Knepley 
41977623264SMatthew G. Knepley @*/
42077623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner))
42177623264SMatthew G. Knepley {
42277623264SMatthew G. Knepley   PetscErrorCode ierr;
42377623264SMatthew G. Knepley 
42477623264SMatthew G. Knepley   PetscFunctionBegin;
42577623264SMatthew G. Knepley   ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr);
42677623264SMatthew G. Knepley   PetscFunctionReturn(0);
42777623264SMatthew G. Knepley }
42877623264SMatthew G. Knepley 
42977623264SMatthew G. Knepley /*@C
43077623264SMatthew G. Knepley   PetscPartitionerSetType - Builds a particular PetscPartitioner
43177623264SMatthew G. Knepley 
43277623264SMatthew G. Knepley   Collective on PetscPartitioner
43377623264SMatthew G. Knepley 
43477623264SMatthew G. Knepley   Input Parameters:
43577623264SMatthew G. Knepley + part - The PetscPartitioner object
43677623264SMatthew G. Knepley - name - The kind of partitioner
43777623264SMatthew G. Knepley 
43877623264SMatthew G. Knepley   Options Database Key:
43977623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types
44077623264SMatthew G. Knepley 
44177623264SMatthew G. Knepley   Level: intermediate
44277623264SMatthew G. Knepley 
44377623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type
44477623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate()
44577623264SMatthew G. Knepley @*/
44677623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name)
44777623264SMatthew G. Knepley {
44877623264SMatthew G. Knepley   PetscErrorCode (*r)(PetscPartitioner);
44977623264SMatthew G. Knepley   PetscBool      match;
45077623264SMatthew G. Knepley   PetscErrorCode ierr;
45177623264SMatthew G. Knepley 
45277623264SMatthew G. Knepley   PetscFunctionBegin;
45377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
45477623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr);
45577623264SMatthew G. Knepley   if (match) PetscFunctionReturn(0);
45677623264SMatthew G. Knepley 
4570f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
45877623264SMatthew G. Knepley   ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr);
45977623264SMatthew G. Knepley   if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name);
46077623264SMatthew G. Knepley 
46177623264SMatthew G. Knepley   if (part->ops->destroy) {
46277623264SMatthew G. Knepley     ierr              = (*part->ops->destroy)(part);CHKERRQ(ierr);
46377623264SMatthew G. Knepley   }
46409161815SVaclav Hapla   ierr = PetscMemzero(part->ops, sizeof(struct _PetscPartitionerOps));CHKERRQ(ierr);
46577623264SMatthew G. Knepley   ierr = (*r)(part);CHKERRQ(ierr);
46677623264SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr);
46777623264SMatthew G. Knepley   PetscFunctionReturn(0);
46877623264SMatthew G. Knepley }
46977623264SMatthew G. Knepley 
47077623264SMatthew G. Knepley /*@C
47177623264SMatthew G. Knepley   PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object.
47277623264SMatthew G. Knepley 
47377623264SMatthew G. Knepley   Not Collective
47477623264SMatthew G. Knepley 
47577623264SMatthew G. Knepley   Input Parameter:
47677623264SMatthew G. Knepley . part - The PetscPartitioner
47777623264SMatthew G. Knepley 
47877623264SMatthew G. Knepley   Output Parameter:
47977623264SMatthew G. Knepley . name - The PetscPartitioner type name
48077623264SMatthew G. Knepley 
48177623264SMatthew G. Knepley   Level: intermediate
48277623264SMatthew G. Knepley 
48377623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name
48477623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate()
48577623264SMatthew G. Knepley @*/
48677623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name)
48777623264SMatthew G. Knepley {
48877623264SMatthew G. Knepley   PetscErrorCode ierr;
48977623264SMatthew G. Knepley 
49077623264SMatthew G. Knepley   PetscFunctionBegin;
49177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
49277623264SMatthew G. Knepley   PetscValidPointer(name, 2);
4930f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
49477623264SMatthew G. Knepley   *name = ((PetscObject) part)->type_name;
49577623264SMatthew G. Knepley   PetscFunctionReturn(0);
49677623264SMatthew G. Knepley }
49777623264SMatthew G. Knepley 
49877623264SMatthew G. Knepley /*@C
49977623264SMatthew G. Knepley   PetscPartitionerView - Views a PetscPartitioner
50077623264SMatthew G. Knepley 
50177623264SMatthew G. Knepley   Collective on PetscPartitioner
50277623264SMatthew G. Knepley 
50377623264SMatthew G. Knepley   Input Parameter:
50477623264SMatthew G. Knepley + part - the PetscPartitioner object to view
50577623264SMatthew G. Knepley - v    - the viewer
50677623264SMatthew G. Knepley 
50777623264SMatthew G. Knepley   Level: developer
50877623264SMatthew G. Knepley 
50977623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy()
51077623264SMatthew G. Knepley @*/
51177623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v)
51277623264SMatthew G. Knepley {
513ffc59708SMatthew G. Knepley   PetscMPIInt    size;
5142abdaa70SMatthew G. Knepley   PetscBool      isascii;
51577623264SMatthew G. Knepley   PetscErrorCode ierr;
51677623264SMatthew G. Knepley 
51777623264SMatthew G. Knepley   PetscFunctionBegin;
51877623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
51977623264SMatthew G. Knepley   if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);}
5202abdaa70SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
5212abdaa70SMatthew G. Knepley   if (isascii) {
5222abdaa70SMatthew G. Knepley     ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
523ffc59708SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "Graph Partitioner: %d MPI Process%s\n", size, size > 1 ? "es" : "");CHKERRQ(ierr);
5242abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "  type: %s\n", part->hdr.type_name);CHKERRQ(ierr);
5252abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
5262abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "edge cut: %D\n", part->edgeCut);CHKERRQ(ierr);
5272abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "balance:  %.2g\n", part->balance);CHKERRQ(ierr);
5282abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
5292abdaa70SMatthew G. Knepley   }
53077623264SMatthew G. Knepley   if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);}
53177623264SMatthew G. Knepley   PetscFunctionReturn(0);
53277623264SMatthew G. Knepley }
53377623264SMatthew G. Knepley 
534a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType)
535a0058e54SToby Isaac {
536a0058e54SToby Isaac   PetscFunctionBegin;
537a0058e54SToby Isaac   if (!currentType) {
538a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO)
539a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERCHACO;
540a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS)
541a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERPARMETIS;
542137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH)
543137cd93aSLisandro Dalcin     *defaultType = PETSCPARTITIONERPTSCOTCH;
544a0058e54SToby Isaac #else
545a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERSIMPLE;
546a0058e54SToby Isaac #endif
547a0058e54SToby Isaac   } else {
548a0058e54SToby Isaac     *defaultType = currentType;
549a0058e54SToby Isaac   }
550a0058e54SToby Isaac   PetscFunctionReturn(0);
551a0058e54SToby Isaac }
552a0058e54SToby Isaac 
55377623264SMatthew G. Knepley /*@
55477623264SMatthew G. Knepley   PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database
55577623264SMatthew G. Knepley 
55677623264SMatthew G. Knepley   Collective on PetscPartitioner
55777623264SMatthew G. Knepley 
55877623264SMatthew G. Knepley   Input Parameter:
55977623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for
56077623264SMatthew G. Knepley 
56177623264SMatthew G. Knepley   Level: developer
56277623264SMatthew G. Knepley 
56377623264SMatthew G. Knepley .seealso: PetscPartitionerView()
56477623264SMatthew G. Knepley @*/
56577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part)
56677623264SMatthew G. Knepley {
5676bb9daa8SLisandro Dalcin   const char    *defaultType;
5686bb9daa8SLisandro Dalcin   char           name[256];
5696bb9daa8SLisandro Dalcin   PetscBool      flg;
57077623264SMatthew G. Knepley   PetscErrorCode ierr;
57177623264SMatthew G. Knepley 
57277623264SMatthew G. Knepley   PetscFunctionBegin;
57377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
5746bb9daa8SLisandro Dalcin   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
5756bb9daa8SLisandro Dalcin   ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr);
57677623264SMatthew G. Knepley   ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr);
5776bb9daa8SLisandro Dalcin   ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, sizeof(name), &flg);CHKERRQ(ierr);
5786bb9daa8SLisandro Dalcin   if (flg) {
5796bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr);
5806bb9daa8SLisandro Dalcin   } else if (!((PetscObject) part)->type_name) {
5816bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr);
5826bb9daa8SLisandro Dalcin   }
5836bb9daa8SLisandro Dalcin   if (part->ops->setfromoptions) {
5846bb9daa8SLisandro Dalcin     ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr);
5856bb9daa8SLisandro Dalcin   }
5860358368aSMatthew G. Knepley   ierr = PetscOptionsGetViewer(((PetscObject) part)->comm, ((PetscObject) part)->options, ((PetscObject) part)->prefix, "-petscpartitioner_view_graph", &part->viewerGraph, &part->formatGraph, &part->viewGraph);CHKERRQ(ierr);
58777623264SMatthew G. Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5880633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr);
58977623264SMatthew G. Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
59077623264SMatthew G. Knepley   PetscFunctionReturn(0);
59177623264SMatthew G. Knepley }
59277623264SMatthew G. Knepley 
59377623264SMatthew G. Knepley /*@C
59477623264SMatthew G. Knepley   PetscPartitionerSetUp - Construct data structures for the PetscPartitioner
59577623264SMatthew G. Knepley 
59677623264SMatthew G. Knepley   Collective on PetscPartitioner
59777623264SMatthew G. Knepley 
59877623264SMatthew G. Knepley   Input Parameter:
59977623264SMatthew G. Knepley . part - the PetscPartitioner object to setup
60077623264SMatthew G. Knepley 
60177623264SMatthew G. Knepley   Level: developer
60277623264SMatthew G. Knepley 
60377623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy()
60477623264SMatthew G. Knepley @*/
60577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part)
60677623264SMatthew G. Knepley {
60777623264SMatthew G. Knepley   PetscErrorCode ierr;
60877623264SMatthew G. Knepley 
60977623264SMatthew G. Knepley   PetscFunctionBegin;
61077623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
61177623264SMatthew G. Knepley   if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);}
61277623264SMatthew G. Knepley   PetscFunctionReturn(0);
61377623264SMatthew G. Knepley }
61477623264SMatthew G. Knepley 
61577623264SMatthew G. Knepley /*@
61677623264SMatthew G. Knepley   PetscPartitionerDestroy - Destroys a PetscPartitioner object
61777623264SMatthew G. Knepley 
61877623264SMatthew G. Knepley   Collective on PetscPartitioner
61977623264SMatthew G. Knepley 
62077623264SMatthew G. Knepley   Input Parameter:
62177623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy
62277623264SMatthew G. Knepley 
62377623264SMatthew G. Knepley   Level: developer
62477623264SMatthew G. Knepley 
62577623264SMatthew G. Knepley .seealso: PetscPartitionerView()
62677623264SMatthew G. Knepley @*/
62777623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part)
62877623264SMatthew G. Knepley {
62977623264SMatthew G. Knepley   PetscErrorCode ierr;
63077623264SMatthew G. Knepley 
63177623264SMatthew G. Knepley   PetscFunctionBegin;
63277623264SMatthew G. Knepley   if (!*part) PetscFunctionReturn(0);
63377623264SMatthew G. Knepley   PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1);
63477623264SMatthew G. Knepley 
63577623264SMatthew G. Knepley   if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);}
63677623264SMatthew G. Knepley   ((PetscObject) (*part))->refct = 0;
63777623264SMatthew G. Knepley 
6380358368aSMatthew G. Knepley   ierr = PetscViewerDestroy(&(*part)->viewerGraph);CHKERRQ(ierr);
63977623264SMatthew G. Knepley   if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);}
64077623264SMatthew G. Knepley   ierr = PetscHeaderDestroy(part);CHKERRQ(ierr);
64177623264SMatthew G. Knepley   PetscFunctionReturn(0);
64277623264SMatthew G. Knepley }
64377623264SMatthew G. Knepley 
64477623264SMatthew G. Knepley /*@
64577623264SMatthew G. Knepley   PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType().
64677623264SMatthew G. Knepley 
64777623264SMatthew G. Knepley   Collective on MPI_Comm
64877623264SMatthew G. Knepley 
64977623264SMatthew G. Knepley   Input Parameter:
65077623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object
65177623264SMatthew G. Knepley 
65277623264SMatthew G. Knepley   Output Parameter:
65377623264SMatthew G. Knepley . part - The PetscPartitioner object
65477623264SMatthew G. Knepley 
65577623264SMatthew G. Knepley   Level: beginner
65677623264SMatthew G. Knepley 
657dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER
65877623264SMatthew G. Knepley @*/
65977623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part)
66077623264SMatthew G. Knepley {
66177623264SMatthew G. Knepley   PetscPartitioner p;
662a0058e54SToby Isaac   const char       *partitionerType = NULL;
66377623264SMatthew G. Knepley   PetscErrorCode   ierr;
66477623264SMatthew G. Knepley 
66577623264SMatthew G. Knepley   PetscFunctionBegin;
66677623264SMatthew G. Knepley   PetscValidPointer(part, 2);
66777623264SMatthew G. Knepley   *part = NULL;
66883cde681SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
66977623264SMatthew G. Knepley 
67073107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr);
671a0058e54SToby Isaac   ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr);
672a0058e54SToby Isaac   ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr);
67377623264SMatthew G. Knepley 
67472379da4SMatthew G. Knepley   p->edgeCut = 0;
67572379da4SMatthew G. Knepley   p->balance = 0.0;
67672379da4SMatthew G. Knepley 
67777623264SMatthew G. Knepley   *part = p;
67877623264SMatthew G. Knepley   PetscFunctionReturn(0);
67977623264SMatthew G. Knepley }
68077623264SMatthew G. Knepley 
68177623264SMatthew G. Knepley /*@
68277623264SMatthew G. Knepley   PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh
68377623264SMatthew G. Knepley 
68477623264SMatthew G. Knepley   Collective on DM
68577623264SMatthew G. Knepley 
68677623264SMatthew G. Knepley   Input Parameters:
68777623264SMatthew G. Knepley + part    - The PetscPartitioner
688f8987ae8SMichael Lange - dm      - The mesh DM
68977623264SMatthew G. Knepley 
69077623264SMatthew G. Knepley   Output Parameters:
69177623264SMatthew G. Knepley + partSection     - The PetscSection giving the division of points by partition
692f8987ae8SMichael Lange - partition       - The list of points by partition
69377623264SMatthew G. Knepley 
6940358368aSMatthew G. Knepley   Options Database:
6950358368aSMatthew G. Knepley . -petscpartitioner_view_graph - View the graph we are partitioning
6960358368aSMatthew G. Knepley 
69777623264SMatthew G. Knepley   Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight()
69877623264SMatthew G. Knepley 
69977623264SMatthew G. Knepley   Level: developer
70077623264SMatthew G. Knepley 
70177623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate()
7024b15ede2SMatthew G. Knepley @*/
703f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition)
70477623264SMatthew G. Knepley {
70577623264SMatthew G. Knepley   PetscMPIInt    size;
70677623264SMatthew G. Knepley   PetscErrorCode ierr;
70777623264SMatthew G. Knepley 
70877623264SMatthew G. Knepley   PetscFunctionBegin;
70977623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
71077623264SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
71177623264SMatthew G. Knepley   PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4);
71277623264SMatthew G. Knepley   PetscValidPointer(partition, 5);
71377623264SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
71477623264SMatthew G. Knepley   if (size == 1) {
71577623264SMatthew G. Knepley     PetscInt *points;
71677623264SMatthew G. Knepley     PetscInt  cStart, cEnd, c;
71777623264SMatthew G. Knepley 
71877623264SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
71977623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr);
72077623264SMatthew G. Knepley     ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr);
72177623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
72277623264SMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr);
72377623264SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) points[c] = c;
72477623264SMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
72577623264SMatthew G. Knepley     PetscFunctionReturn(0);
72677623264SMatthew G. Knepley   }
72777623264SMatthew G. Knepley   if (part->height == 0) {
72877623264SMatthew G. Knepley     PetscInt numVertices;
72977623264SMatthew G. Knepley     PetscInt *start     = NULL;
73077623264SMatthew G. Knepley     PetscInt *adjacency = NULL;
7313fa7752dSToby Isaac     IS       globalNumbering;
73277623264SMatthew G. Knepley 
7333fa7752dSToby Isaac     ierr = DMPlexCreatePartitionerGraph(dm, 0, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr);
7340358368aSMatthew G. Knepley     if (part->viewGraph) {
7350358368aSMatthew G. Knepley       PetscViewer viewer = part->viewerGraph;
7360358368aSMatthew G. Knepley       PetscBool   isascii;
7370358368aSMatthew G. Knepley       PetscInt    v, i;
7380358368aSMatthew G. Knepley       PetscMPIInt rank;
7390358368aSMatthew G. Knepley 
7400358368aSMatthew G. Knepley       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);CHKERRQ(ierr);
7410358368aSMatthew G. Knepley       ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
7420358368aSMatthew G. Knepley       if (isascii) {
7430358368aSMatthew G. Knepley         ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
7440358368aSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]Nv: %D\n", rank, numVertices);CHKERRQ(ierr);
7450358368aSMatthew G. Knepley         for (v = 0; v < numVertices; ++v) {
7460358368aSMatthew G. Knepley           const PetscInt s = start[v];
7470358368aSMatthew G. Knepley           const PetscInt e = start[v+1];
7480358368aSMatthew G. Knepley 
7490358368aSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]  ", rank);CHKERRQ(ierr);
7500358368aSMatthew G. Knepley           for (i = s; i < e; ++i) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%D ", adjacency[i]);CHKERRQ(ierr);}
7510358368aSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D-%D)\n", s, e);CHKERRQ(ierr);
7520358368aSMatthew G. Knepley         }
7530358368aSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7540358368aSMatthew G. Knepley         ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7550358368aSMatthew G. Knepley       }
7560358368aSMatthew G. Knepley     }
75777623264SMatthew G. Knepley     if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type");
75877623264SMatthew G. Knepley     ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
75977623264SMatthew G. Knepley     ierr = PetscFree(start);CHKERRQ(ierr);
76077623264SMatthew G. Knepley     ierr = PetscFree(adjacency);CHKERRQ(ierr);
7613fa7752dSToby Isaac     if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */
7623fa7752dSToby Isaac       const PetscInt *globalNum;
7633fa7752dSToby Isaac       const PetscInt *partIdx;
7643fa7752dSToby Isaac       PetscInt *map, cStart, cEnd;
7653fa7752dSToby Isaac       PetscInt *adjusted, i, localSize, offset;
7663fa7752dSToby Isaac       IS    newPartition;
7673fa7752dSToby Isaac 
7683fa7752dSToby Isaac       ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr);
7693fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr);
7703fa7752dSToby Isaac       ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7713fa7752dSToby Isaac       ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr);
7723fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr);
7733fa7752dSToby Isaac       ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
7743fa7752dSToby Isaac       for (i = cStart, offset = 0; i < cEnd; i++) {
7753fa7752dSToby Isaac         if (globalNum[i - cStart] >= 0) map[offset++] = i;
7763fa7752dSToby Isaac       }
7773fa7752dSToby Isaac       for (i = 0; i < localSize; i++) {
7783fa7752dSToby Isaac         adjusted[i] = map[partIdx[i]];
7793fa7752dSToby Isaac       }
7803fa7752dSToby Isaac       ierr = PetscFree(map);CHKERRQ(ierr);
7813fa7752dSToby Isaac       ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr);
7823fa7752dSToby Isaac       ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7833fa7752dSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr);
7843fa7752dSToby Isaac       ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr);
7853fa7752dSToby Isaac       ierr = ISDestroy(partition);CHKERRQ(ierr);
7863fa7752dSToby Isaac       *partition = newPartition;
7873fa7752dSToby Isaac     }
78877623264SMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height);
7892abdaa70SMatthew G. Knepley   ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr);
79077623264SMatthew G. Knepley   PetscFunctionReturn(0);
79177623264SMatthew G. Knepley }
79277623264SMatthew G. Knepley 
793d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part)
79477623264SMatthew G. Knepley {
79577623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
79677623264SMatthew G. Knepley   PetscErrorCode          ierr;
79777623264SMatthew G. Knepley 
79877623264SMatthew G. Knepley   PetscFunctionBegin;
79977623264SMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
80077623264SMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
80177623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
80277623264SMatthew G. Knepley   PetscFunctionReturn(0);
80377623264SMatthew G. Knepley }
80477623264SMatthew G. Knepley 
805d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer)
80677623264SMatthew G. Knepley {
807077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
80877623264SMatthew G. Knepley   PetscErrorCode          ierr;
80977623264SMatthew G. Knepley 
81077623264SMatthew G. Knepley   PetscFunctionBegin;
811077101c0SMatthew G. Knepley   if (p->random) {
812077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
813077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr);
814077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
815077101c0SMatthew G. Knepley   }
81677623264SMatthew G. Knepley   PetscFunctionReturn(0);
81777623264SMatthew G. Knepley }
81877623264SMatthew G. Knepley 
819d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer)
82077623264SMatthew G. Knepley {
82177623264SMatthew G. Knepley   PetscBool      iascii;
82277623264SMatthew G. Knepley   PetscErrorCode ierr;
82377623264SMatthew G. Knepley 
82477623264SMatthew G. Knepley   PetscFunctionBegin;
82577623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
82677623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
82777623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
82877623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);}
82977623264SMatthew G. Knepley   PetscFunctionReturn(0);
83077623264SMatthew G. Knepley }
83177623264SMatthew G. Knepley 
832d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
833077101c0SMatthew G. Knepley {
834077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
835077101c0SMatthew G. Knepley   PetscErrorCode          ierr;
836077101c0SMatthew G. Knepley 
837077101c0SMatthew G. Knepley   PetscFunctionBegin;
838077101c0SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");CHKERRQ(ierr);
839077101c0SMatthew G. Knepley   ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr);
840077101c0SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
841077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
842077101c0SMatthew G. Knepley }
843077101c0SMatthew G. Knepley 
844d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
84577623264SMatthew G. Knepley {
84677623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
84777623264SMatthew G. Knepley   PetscInt                np;
84877623264SMatthew G. Knepley   PetscErrorCode          ierr;
84977623264SMatthew G. Knepley 
85077623264SMatthew G. Knepley   PetscFunctionBegin;
851077101c0SMatthew G. Knepley   if (p->random) {
852077101c0SMatthew G. Knepley     PetscRandom r;
853aa1d5631SMatthew G. Knepley     PetscInt   *sizes, *points, v, p;
854aa1d5631SMatthew G. Knepley     PetscMPIInt rank;
855077101c0SMatthew G. Knepley 
856aa1d5631SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
857077101c0SMatthew G. Knepley     ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
858c717d290SMatthew G. Knepley     ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr);
859077101c0SMatthew G. Knepley     ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr);
860077101c0SMatthew G. Knepley     ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr);
861aa1d5631SMatthew G. Knepley     for (v = 0; v < numVertices; ++v) {points[v] = v;}
862ac9a96f1SMichael Lange     for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);}
863aa1d5631SMatthew G. Knepley     for (v = numVertices-1; v > 0; --v) {
864077101c0SMatthew G. Knepley       PetscReal val;
865aa1d5631SMatthew G. Knepley       PetscInt  w, tmp;
866077101c0SMatthew G. Knepley 
867aa1d5631SMatthew G. Knepley       ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr);
868077101c0SMatthew G. Knepley       ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr);
869aa1d5631SMatthew G. Knepley       w    = PetscFloorReal(val);
870aa1d5631SMatthew G. Knepley       tmp       = points[v];
871aa1d5631SMatthew G. Knepley       points[v] = points[w];
872aa1d5631SMatthew G. Knepley       points[w] = tmp;
873077101c0SMatthew G. Knepley     }
874077101c0SMatthew G. Knepley     ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
875077101c0SMatthew G. Knepley     ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr);
876077101c0SMatthew G. Knepley     ierr = PetscFree2(sizes, points);CHKERRQ(ierr);
877077101c0SMatthew G. Knepley   }
878c717d290SMatthew G. Knepley   if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()");
87977623264SMatthew G. Knepley   ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr);
88077623264SMatthew G. Knepley   if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np);
88177623264SMatthew G. Knepley   ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr);
88277623264SMatthew G. Knepley   if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np);
8835680f57bSMatthew G. Knepley   ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr);
88477623264SMatthew G. Knepley   *partition = p->partition;
88577623264SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr);
88677623264SMatthew G. Knepley   PetscFunctionReturn(0);
88777623264SMatthew G. Knepley }
88877623264SMatthew G. Knepley 
889d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part)
89077623264SMatthew G. Knepley {
89177623264SMatthew G. Knepley   PetscFunctionBegin;
89277623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_Shell;
893077101c0SMatthew G. Knepley   part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell;
89477623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_Shell;
89577623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_Shell;
89677623264SMatthew G. Knepley   PetscFunctionReturn(0);
89777623264SMatthew G. Knepley }
89877623264SMatthew G. Knepley 
89977623264SMatthew G. Knepley /*MC
90077623264SMatthew G. Knepley   PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object
90177623264SMatthew G. Knepley 
90277623264SMatthew G. Knepley   Level: intermediate
90377623264SMatthew G. Knepley 
90477623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
90577623264SMatthew G. Knepley M*/
90677623264SMatthew G. Knepley 
90777623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part)
90877623264SMatthew G. Knepley {
90977623264SMatthew G. Knepley   PetscPartitioner_Shell *p;
91077623264SMatthew G. Knepley   PetscErrorCode          ierr;
91177623264SMatthew G. Knepley 
91277623264SMatthew G. Knepley   PetscFunctionBegin;
91377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
91477623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
91577623264SMatthew G. Knepley   part->data = p;
91677623264SMatthew G. Knepley 
91777623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr);
918077101c0SMatthew G. Knepley   p->random = PETSC_FALSE;
91977623264SMatthew G. Knepley   PetscFunctionReturn(0);
92077623264SMatthew G. Knepley }
92177623264SMatthew G. Knepley 
9225680f57bSMatthew G. Knepley /*@C
9235680f57bSMatthew G. Knepley   PetscPartitionerShellSetPartition - Set an artifical partition for a mesh
9245680f57bSMatthew G. Knepley 
925077101c0SMatthew G. Knepley   Collective on Part
9265680f57bSMatthew G. Knepley 
9275680f57bSMatthew G. Knepley   Input Parameters:
9285680f57bSMatthew G. Knepley + part     - The PetscPartitioner
9299852e123SBarry Smith . size - The number of partitions
9309852e123SBarry Smith . sizes    - array of size size (or NULL) providing the number of points in each partition
9319758bf69SToby 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.)
9325680f57bSMatthew G. Knepley 
9335680f57bSMatthew G. Knepley   Level: developer
9345680f57bSMatthew G. Knepley 
935b7e49471SLawrence Mitchell   Notes:
936b7e49471SLawrence Mitchell 
937b7e49471SLawrence Mitchell     It is safe to free the sizes and points arrays after use in this routine.
938b7e49471SLawrence Mitchell 
9395680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate()
9405680f57bSMatthew G. Knepley @*/
9419852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[])
9425680f57bSMatthew G. Knepley {
9435680f57bSMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
9445680f57bSMatthew G. Knepley   PetscInt                proc, numPoints;
9455680f57bSMatthew G. Knepley   PetscErrorCode          ierr;
9465680f57bSMatthew G. Knepley 
9475680f57bSMatthew G. Knepley   PetscFunctionBegin;
9485680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
9495680f57bSMatthew G. Knepley   if (sizes)  {PetscValidPointer(sizes, 3);}
950c717d290SMatthew G. Knepley   if (points) {PetscValidPointer(points, 4);}
9515680f57bSMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
9525680f57bSMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
9535680f57bSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr);
9549852e123SBarry Smith   ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr);
9555680f57bSMatthew G. Knepley   if (sizes) {
9569852e123SBarry Smith     for (proc = 0; proc < size; ++proc) {
9575680f57bSMatthew G. Knepley       ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr);
9585680f57bSMatthew G. Knepley     }
9595680f57bSMatthew G. Knepley   }
9605680f57bSMatthew G. Knepley   ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr);
9615680f57bSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr);
9625680f57bSMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr);
9635680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
9645680f57bSMatthew G. Knepley }
9655680f57bSMatthew G. Knepley 
966077101c0SMatthew G. Knepley /*@
967077101c0SMatthew G. Knepley   PetscPartitionerShellSetRandom - Set the flag to use a random partition
968077101c0SMatthew G. Knepley 
969077101c0SMatthew G. Knepley   Collective on Part
970077101c0SMatthew G. Knepley 
971077101c0SMatthew G. Knepley   Input Parameters:
972077101c0SMatthew G. Knepley + part   - The PetscPartitioner
973077101c0SMatthew G. Knepley - random - The flag to use a random partition
974077101c0SMatthew G. Knepley 
975077101c0SMatthew G. Knepley   Level: intermediate
976077101c0SMatthew G. Knepley 
977077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate()
978077101c0SMatthew G. Knepley @*/
979077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random)
980077101c0SMatthew G. Knepley {
981077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
982077101c0SMatthew G. Knepley 
983077101c0SMatthew G. Knepley   PetscFunctionBegin;
984077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
985077101c0SMatthew G. Knepley   p->random = random;
986077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
987077101c0SMatthew G. Knepley }
988077101c0SMatthew G. Knepley 
989077101c0SMatthew G. Knepley /*@
990077101c0SMatthew G. Knepley   PetscPartitionerShellGetRandom - get the flag to use a random partition
991077101c0SMatthew G. Knepley 
992077101c0SMatthew G. Knepley   Collective on Part
993077101c0SMatthew G. Knepley 
994077101c0SMatthew G. Knepley   Input Parameter:
995077101c0SMatthew G. Knepley . part   - The PetscPartitioner
996077101c0SMatthew G. Knepley 
997077101c0SMatthew G. Knepley   Output Parameter
998077101c0SMatthew G. Knepley . random - The flag to use a random partition
999077101c0SMatthew G. Knepley 
1000077101c0SMatthew G. Knepley   Level: intermediate
1001077101c0SMatthew G. Knepley 
1002077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate()
1003077101c0SMatthew G. Knepley @*/
1004077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random)
1005077101c0SMatthew G. Knepley {
1006077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
1007077101c0SMatthew G. Knepley 
1008077101c0SMatthew G. Knepley   PetscFunctionBegin;
1009077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1010077101c0SMatthew G. Knepley   PetscValidPointer(random, 2);
1011077101c0SMatthew G. Knepley   *random = p->random;
1012077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
1013077101c0SMatthew G. Knepley }
1014077101c0SMatthew G. Knepley 
1015d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part)
1016555a9cf8SMatthew G. Knepley {
1017555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data;
1018555a9cf8SMatthew G. Knepley   PetscErrorCode          ierr;
1019555a9cf8SMatthew G. Knepley 
1020555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1021555a9cf8SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
1022555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1023555a9cf8SMatthew G. Knepley }
1024555a9cf8SMatthew G. Knepley 
1025d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer)
1026555a9cf8SMatthew G. Knepley {
1027555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1028555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1029555a9cf8SMatthew G. Knepley }
1030555a9cf8SMatthew G. Knepley 
1031d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer)
1032555a9cf8SMatthew G. Knepley {
1033555a9cf8SMatthew G. Knepley   PetscBool      iascii;
1034555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
1035555a9cf8SMatthew G. Knepley 
1036555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1037555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1038555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1039555a9cf8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1040555a9cf8SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);}
1041555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1042555a9cf8SMatthew G. Knepley }
1043555a9cf8SMatthew G. Knepley 
1044d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1045555a9cf8SMatthew G. Knepley {
1046cead94edSToby Isaac   MPI_Comm       comm;
1047555a9cf8SMatthew G. Knepley   PetscInt       np;
1048cead94edSToby Isaac   PetscMPIInt    size;
1049555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
1050555a9cf8SMatthew G. Knepley 
1051555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1052cead94edSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
1053cead94edSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1054555a9cf8SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1055555a9cf8SMatthew G. Knepley   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1056cead94edSToby Isaac   if (size == 1) {
1057cead94edSToby Isaac     for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);}
1058cead94edSToby Isaac   }
1059cead94edSToby Isaac   else {
1060cead94edSToby Isaac     PetscMPIInt rank;
1061cead94edSToby Isaac     PetscInt nvGlobal, *offsets, myFirst, myLast;
1062cead94edSToby Isaac 
1063a679a563SToby Isaac     ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr);
1064cead94edSToby Isaac     offsets[0] = 0;
1065cead94edSToby Isaac     ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr);
1066cead94edSToby Isaac     for (np = 2; np <= size; np++) {
1067cead94edSToby Isaac       offsets[np] += offsets[np-1];
1068cead94edSToby Isaac     }
1069cead94edSToby Isaac     nvGlobal = offsets[size];
1070cead94edSToby Isaac     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1071cead94edSToby Isaac     myFirst = offsets[rank];
1072cead94edSToby Isaac     myLast  = offsets[rank + 1] - 1;
1073cead94edSToby Isaac     ierr = PetscFree(offsets);CHKERRQ(ierr);
1074cead94edSToby Isaac     if (numVertices) {
1075cead94edSToby Isaac       PetscInt firstPart = 0, firstLargePart = 0;
1076cead94edSToby Isaac       PetscInt lastPart = 0, lastLargePart = 0;
1077cead94edSToby Isaac       PetscInt rem = nvGlobal % nparts;
1078cead94edSToby Isaac       PetscInt pSmall = nvGlobal/nparts;
1079cead94edSToby Isaac       PetscInt pBig = nvGlobal/nparts + 1;
1080cead94edSToby Isaac 
1081cead94edSToby Isaac 
1082cead94edSToby Isaac       if (rem) {
1083cead94edSToby Isaac         firstLargePart = myFirst / pBig;
1084cead94edSToby Isaac         lastLargePart  = myLast  / pBig;
1085cead94edSToby Isaac 
1086cead94edSToby Isaac         if (firstLargePart < rem) {
1087cead94edSToby Isaac           firstPart = firstLargePart;
1088cead94edSToby Isaac         }
1089cead94edSToby Isaac         else {
1090cead94edSToby Isaac           firstPart = rem + (myFirst - (rem * pBig)) / pSmall;
1091cead94edSToby Isaac         }
1092cead94edSToby Isaac         if (lastLargePart < rem) {
1093cead94edSToby Isaac           lastPart = lastLargePart;
1094cead94edSToby Isaac         }
1095cead94edSToby Isaac         else {
1096cead94edSToby Isaac           lastPart = rem + (myLast - (rem * pBig)) / pSmall;
1097cead94edSToby Isaac         }
1098cead94edSToby Isaac       }
1099cead94edSToby Isaac       else {
1100cead94edSToby Isaac         firstPart = myFirst / (nvGlobal/nparts);
1101cead94edSToby Isaac         lastPart  = myLast  / (nvGlobal/nparts);
1102cead94edSToby Isaac       }
1103cead94edSToby Isaac 
1104cead94edSToby Isaac       for (np = firstPart; np <= lastPart; np++) {
1105cead94edSToby Isaac         PetscInt PartStart =  np    * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np);
1106cead94edSToby Isaac         PetscInt PartEnd   = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1);
1107cead94edSToby Isaac 
1108cead94edSToby Isaac         PartStart = PetscMax(PartStart,myFirst);
1109cead94edSToby Isaac         PartEnd   = PetscMin(PartEnd,myLast+1);
1110cead94edSToby Isaac         ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr);
1111cead94edSToby Isaac       }
1112cead94edSToby Isaac     }
1113cead94edSToby Isaac   }
1114cead94edSToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1115555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1116555a9cf8SMatthew G. Knepley }
1117555a9cf8SMatthew G. Knepley 
1118d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part)
1119555a9cf8SMatthew G. Knepley {
1120555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1121555a9cf8SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Simple;
1122555a9cf8SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Simple;
1123555a9cf8SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Simple;
1124555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1125555a9cf8SMatthew G. Knepley }
1126555a9cf8SMatthew G. Knepley 
1127555a9cf8SMatthew G. Knepley /*MC
1128555a9cf8SMatthew G. Knepley   PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object
1129555a9cf8SMatthew G. Knepley 
1130555a9cf8SMatthew G. Knepley   Level: intermediate
1131555a9cf8SMatthew G. Knepley 
1132555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1133555a9cf8SMatthew G. Knepley M*/
1134555a9cf8SMatthew G. Knepley 
1135555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part)
1136555a9cf8SMatthew G. Knepley {
1137555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p;
1138555a9cf8SMatthew G. Knepley   PetscErrorCode           ierr;
1139555a9cf8SMatthew G. Knepley 
1140555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1141555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1142555a9cf8SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1143555a9cf8SMatthew G. Knepley   part->data = p;
1144555a9cf8SMatthew G. Knepley 
1145555a9cf8SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr);
1146555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1147555a9cf8SMatthew G. Knepley }
1148555a9cf8SMatthew G. Knepley 
1149d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part)
1150dae52e14SToby Isaac {
1151dae52e14SToby Isaac   PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data;
1152dae52e14SToby Isaac   PetscErrorCode          ierr;
1153dae52e14SToby Isaac 
1154dae52e14SToby Isaac   PetscFunctionBegin;
1155dae52e14SToby Isaac   ierr = PetscFree(p);CHKERRQ(ierr);
1156dae52e14SToby Isaac   PetscFunctionReturn(0);
1157dae52e14SToby Isaac }
1158dae52e14SToby Isaac 
1159d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer)
1160dae52e14SToby Isaac {
1161dae52e14SToby Isaac   PetscFunctionBegin;
1162dae52e14SToby Isaac   PetscFunctionReturn(0);
1163dae52e14SToby Isaac }
1164dae52e14SToby Isaac 
1165d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer)
1166dae52e14SToby Isaac {
1167dae52e14SToby Isaac   PetscBool      iascii;
1168dae52e14SToby Isaac   PetscErrorCode ierr;
1169dae52e14SToby Isaac 
1170dae52e14SToby Isaac   PetscFunctionBegin;
1171dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1172dae52e14SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1173dae52e14SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1174dae52e14SToby Isaac   if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);}
1175dae52e14SToby Isaac   PetscFunctionReturn(0);
1176dae52e14SToby Isaac }
1177dae52e14SToby Isaac 
1178d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1179dae52e14SToby Isaac {
1180dae52e14SToby Isaac   PetscInt       np;
1181dae52e14SToby Isaac   PetscErrorCode ierr;
1182dae52e14SToby Isaac 
1183dae52e14SToby Isaac   PetscFunctionBegin;
1184dae52e14SToby Isaac   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1185dae52e14SToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1186dae52e14SToby Isaac   ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr);
1187dae52e14SToby Isaac   for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);}
1188dae52e14SToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1189dae52e14SToby Isaac   PetscFunctionReturn(0);
1190dae52e14SToby Isaac }
1191dae52e14SToby Isaac 
1192d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part)
1193dae52e14SToby Isaac {
1194dae52e14SToby Isaac   PetscFunctionBegin;
1195dae52e14SToby Isaac   part->ops->view      = PetscPartitionerView_Gather;
1196dae52e14SToby Isaac   part->ops->destroy   = PetscPartitionerDestroy_Gather;
1197dae52e14SToby Isaac   part->ops->partition = PetscPartitionerPartition_Gather;
1198dae52e14SToby Isaac   PetscFunctionReturn(0);
1199dae52e14SToby Isaac }
1200dae52e14SToby Isaac 
1201dae52e14SToby Isaac /*MC
1202dae52e14SToby Isaac   PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object
1203dae52e14SToby Isaac 
1204dae52e14SToby Isaac   Level: intermediate
1205dae52e14SToby Isaac 
1206dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1207dae52e14SToby Isaac M*/
1208dae52e14SToby Isaac 
1209dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part)
1210dae52e14SToby Isaac {
1211dae52e14SToby Isaac   PetscPartitioner_Gather *p;
1212dae52e14SToby Isaac   PetscErrorCode           ierr;
1213dae52e14SToby Isaac 
1214dae52e14SToby Isaac   PetscFunctionBegin;
1215dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1216dae52e14SToby Isaac   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1217dae52e14SToby Isaac   part->data = p;
1218dae52e14SToby Isaac 
1219dae52e14SToby Isaac   ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr);
1220dae52e14SToby Isaac   PetscFunctionReturn(0);
1221dae52e14SToby Isaac }
1222dae52e14SToby Isaac 
1223dae52e14SToby Isaac 
1224d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part)
122577623264SMatthew G. Knepley {
122677623264SMatthew G. Knepley   PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data;
122777623264SMatthew G. Knepley   PetscErrorCode          ierr;
122877623264SMatthew G. Knepley 
122977623264SMatthew G. Knepley   PetscFunctionBegin;
123077623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
123177623264SMatthew G. Knepley   PetscFunctionReturn(0);
123277623264SMatthew G. Knepley }
123377623264SMatthew G. Knepley 
1234d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer)
123577623264SMatthew G. Knepley {
123677623264SMatthew G. Knepley   PetscFunctionBegin;
123777623264SMatthew G. Knepley   PetscFunctionReturn(0);
123877623264SMatthew G. Knepley }
123977623264SMatthew G. Knepley 
1240d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer)
124177623264SMatthew G. Knepley {
124277623264SMatthew G. Knepley   PetscBool      iascii;
124377623264SMatthew G. Knepley   PetscErrorCode ierr;
124477623264SMatthew G. Knepley 
124577623264SMatthew G. Knepley   PetscFunctionBegin;
124677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
124777623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
124877623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
124977623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);}
125077623264SMatthew G. Knepley   PetscFunctionReturn(0);
125177623264SMatthew G. Knepley }
125277623264SMatthew G. Knepley 
125370034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
125470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
125570034214SMatthew G. Knepley #include <unistd.h>
125670034214SMatthew G. Knepley #endif
125711d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
125811d1e910SBarry Smith #include <chaco.h>
125911d1e910SBarry Smith #else
126011d1e910SBarry Smith /* Older versions of Chaco do not have an include file */
126170034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
126270034214SMatthew G. Knepley                        float *ewgts, float *x, float *y, float *z, char *outassignname,
126370034214SMatthew G. Knepley                        char *outfilename, short *assignment, int architecture, int ndims_tot,
126470034214SMatthew G. Knepley                        int mesh_dims[3], double *goal, int global_method, int local_method,
126570034214SMatthew G. Knepley                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
126611d1e910SBarry Smith #endif
126770034214SMatthew G. Knepley extern int FREE_GRAPH;
126877623264SMatthew G. Knepley #endif
126970034214SMatthew G. Knepley 
1270d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
127170034214SMatthew G. Knepley {
127277623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
127370034214SMatthew G. Knepley   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
127470034214SMatthew G. Knepley   MPI_Comm       comm;
127570034214SMatthew G. Knepley   int            nvtxs          = numVertices; /* number of vertices in full graph */
127670034214SMatthew G. Knepley   int           *vwgts          = NULL;   /* weights for all vertices */
127770034214SMatthew G. Knepley   float         *ewgts          = NULL;   /* weights for all edges */
127870034214SMatthew G. Knepley   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
127970034214SMatthew G. Knepley   char          *outassignname  = NULL;   /*  name of assignment output file */
128070034214SMatthew G. Knepley   char          *outfilename    = NULL;   /* output file name */
128170034214SMatthew G. Knepley   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
128270034214SMatthew G. Knepley   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
128370034214SMatthew G. Knepley   int            mesh_dims[3];            /* dimensions of mesh of processors */
128470034214SMatthew G. Knepley   double        *goal          = NULL;    /* desired set sizes for each set */
128570034214SMatthew G. Knepley   int            global_method = 1;       /* global partitioning algorithm */
128670034214SMatthew G. Knepley   int            local_method  = 1;       /* local partitioning algorithm */
128770034214SMatthew G. Knepley   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
128870034214SMatthew G. Knepley   int            vmax          = 200;     /* how many vertices to coarsen down to? */
128970034214SMatthew G. Knepley   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
129070034214SMatthew G. Knepley   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
129170034214SMatthew G. Knepley   long           seed          = 123636512; /* for random graph mutations */
129211d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
129311d1e910SBarry Smith   int           *assignment;              /* Output partition */
129411d1e910SBarry Smith #else
129570034214SMatthew G. Knepley   short int     *assignment;              /* Output partition */
129611d1e910SBarry Smith #endif
129770034214SMatthew G. Knepley   int            fd_stdout, fd_pipe[2];
129870034214SMatthew G. Knepley   PetscInt      *points;
129970034214SMatthew G. Knepley   int            i, v, p;
130070034214SMatthew G. Knepley   PetscErrorCode ierr;
130170034214SMatthew G. Knepley 
130270034214SMatthew G. Knepley   PetscFunctionBegin;
130370034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
130407ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG)
130507ed3857SLisandro Dalcin   {
130607ed3857SLisandro Dalcin     int ival,isum;
130707ed3857SLisandro Dalcin     PetscBool distributed;
130807ed3857SLisandro Dalcin 
130907ed3857SLisandro Dalcin     ival = (numVertices > 0);
131007ed3857SLisandro Dalcin     ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr);
131107ed3857SLisandro Dalcin     distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE;
131207ed3857SLisandro Dalcin     if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph");
131307ed3857SLisandro Dalcin   }
131407ed3857SLisandro Dalcin #endif
131570034214SMatthew G. Knepley   if (!numVertices) {
131677623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
131777623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
131870034214SMatthew G. Knepley     ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
131970034214SMatthew G. Knepley     PetscFunctionReturn(0);
132070034214SMatthew G. Knepley   }
132170034214SMatthew G. Knepley   FREE_GRAPH = 0;                         /* Do not let Chaco free my memory */
132270034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
132370034214SMatthew G. Knepley 
132470034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
132570034214SMatthew G. Knepley     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
132670034214SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
132770034214SMatthew G. Knepley   }
132877623264SMatthew G. Knepley   mesh_dims[0] = nparts;
132970034214SMatthew G. Knepley   mesh_dims[1] = 1;
133070034214SMatthew G. Knepley   mesh_dims[2] = 1;
133170034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr);
133270034214SMatthew G. Knepley   /* Chaco outputs to stdout. We redirect this to a buffer. */
133370034214SMatthew G. Knepley   /* TODO: check error codes for UNIX calls */
133470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
133570034214SMatthew G. Knepley   {
133670034214SMatthew G. Knepley     int piperet;
133770034214SMatthew G. Knepley     piperet = pipe(fd_pipe);
133870034214SMatthew G. Knepley     if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe");
133970034214SMatthew G. Knepley     fd_stdout = dup(1);
134070034214SMatthew G. Knepley     close(1);
134170034214SMatthew G. Knepley     dup2(fd_pipe[1], 1);
134270034214SMatthew G. Knepley   }
134370034214SMatthew G. Knepley #endif
134470034214SMatthew G. Knepley   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
134570034214SMatthew G. Knepley                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
134670034214SMatthew G. Knepley                    vmax, ndims, eigtol, seed);
134770034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
134870034214SMatthew G. Knepley   {
134970034214SMatthew G. Knepley     char msgLog[10000];
135070034214SMatthew G. Knepley     int  count;
135170034214SMatthew G. Knepley 
135270034214SMatthew G. Knepley     fflush(stdout);
135370034214SMatthew G. Knepley     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
135470034214SMatthew G. Knepley     if (count < 0) count = 0;
135570034214SMatthew G. Knepley     msgLog[count] = 0;
135670034214SMatthew G. Knepley     close(1);
135770034214SMatthew G. Knepley     dup2(fd_stdout, 1);
135870034214SMatthew G. Knepley     close(fd_stdout);
135970034214SMatthew G. Knepley     close(fd_pipe[0]);
136070034214SMatthew G. Knepley     close(fd_pipe[1]);
136170034214SMatthew G. Knepley     if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
136270034214SMatthew G. Knepley   }
136307ed3857SLisandro Dalcin #else
136407ed3857SLisandro Dalcin   if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout");
136570034214SMatthew G. Knepley #endif
136670034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
136777623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
136870034214SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {
136977623264SMatthew G. Knepley     ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);
137070034214SMatthew G. Knepley   }
137177623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
137270034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
137377623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
137470034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
137570034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
137670034214SMatthew G. Knepley     }
137770034214SMatthew G. Knepley   }
137870034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
137970034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
138070034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
138170034214SMatthew G. Knepley     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
138270034214SMatthew G. Knepley   }
138370034214SMatthew G. Knepley   ierr = PetscFree(assignment);CHKERRQ(ierr);
138470034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
138570034214SMatthew G. Knepley   PetscFunctionReturn(0);
138677623264SMatthew G. Knepley #else
138777623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
138870034214SMatthew G. Knepley #endif
138977623264SMatthew G. Knepley }
139077623264SMatthew G. Knepley 
1391d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part)
139277623264SMatthew G. Knepley {
139377623264SMatthew G. Knepley   PetscFunctionBegin;
139477623264SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Chaco;
139577623264SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Chaco;
139677623264SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Chaco;
139777623264SMatthew G. Knepley   PetscFunctionReturn(0);
139877623264SMatthew G. Knepley }
139977623264SMatthew G. Knepley 
140077623264SMatthew G. Knepley /*MC
140177623264SMatthew G. Knepley   PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library
140277623264SMatthew G. Knepley 
140377623264SMatthew G. Knepley   Level: intermediate
140477623264SMatthew G. Knepley 
140577623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
140677623264SMatthew G. Knepley M*/
140777623264SMatthew G. Knepley 
140877623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part)
140977623264SMatthew G. Knepley {
141077623264SMatthew G. Knepley   PetscPartitioner_Chaco *p;
141177623264SMatthew G. Knepley   PetscErrorCode          ierr;
141277623264SMatthew G. Knepley 
141377623264SMatthew G. Knepley   PetscFunctionBegin;
141477623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
141577623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
141677623264SMatthew G. Knepley   part->data = p;
141777623264SMatthew G. Knepley 
141877623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr);
141977623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr);
142077623264SMatthew G. Knepley   PetscFunctionReturn(0);
142177623264SMatthew G. Knepley }
142277623264SMatthew G. Knepley 
14235b440754SMatthew G. Knepley static const char *ptypes[] = {"kway", "rb"};
14245b440754SMatthew G. Knepley 
1425d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part)
142677623264SMatthew G. Knepley {
142777623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
142877623264SMatthew G. Knepley   PetscErrorCode             ierr;
142977623264SMatthew G. Knepley 
143077623264SMatthew G. Knepley   PetscFunctionBegin;
143177623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
143277623264SMatthew G. Knepley   PetscFunctionReturn(0);
143377623264SMatthew G. Knepley }
143477623264SMatthew G. Knepley 
1435d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer)
143677623264SMatthew G. Knepley {
14372abdaa70SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
143877623264SMatthew G. Knepley   PetscErrorCode             ierr;
143977623264SMatthew G. Knepley 
144077623264SMatthew G. Knepley   PetscFunctionBegin;
14412abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
14422abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "ParMetis type: %s\n", ptypes[p->ptype]);CHKERRQ(ierr);
14432abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "load imbalance ratio %g\n", (double) p->imbalanceRatio);CHKERRQ(ierr);
14442abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "debug flag %D\n", p->debugFlag);CHKERRQ(ierr);
14452abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
144677623264SMatthew G. Knepley   PetscFunctionReturn(0);
144777623264SMatthew G. Knepley }
144877623264SMatthew G. Knepley 
1449d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer)
145077623264SMatthew G. Knepley {
145177623264SMatthew G. Knepley   PetscBool      iascii;
145277623264SMatthew G. Knepley   PetscErrorCode ierr;
145377623264SMatthew G. Knepley 
145477623264SMatthew G. Knepley   PetscFunctionBegin;
145577623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
145677623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
145777623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
145877623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);}
145977623264SMatthew G. Knepley   PetscFunctionReturn(0);
146077623264SMatthew G. Knepley }
146170034214SMatthew G. Knepley 
146244d8be81SLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_ParMetis(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
146344d8be81SLisandro Dalcin {
146444d8be81SLisandro Dalcin   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
146544d8be81SLisandro Dalcin   PetscErrorCode            ierr;
146644d8be81SLisandro Dalcin 
146744d8be81SLisandro Dalcin   PetscFunctionBegin;
146844d8be81SLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner ParMetis Options");CHKERRQ(ierr);
146944d8be81SLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_parmetis_type", "Partitioning method", "", ptypes, 2, ptypes[p->ptype], &p->ptype, NULL);CHKERRQ(ierr);
14705b440754SMatthew G. Knepley   ierr = PetscOptionsReal("-petscpartitioner_parmetis_imbalance_ratio", "Load imbalance ratio limit", "", p->imbalanceRatio, &p->imbalanceRatio, NULL);CHKERRQ(ierr);
14715b440754SMatthew G. Knepley   ierr = PetscOptionsInt("-petscpartitioner_parmetis_debug", "Debugging flag", "", p->debugFlag, &p->debugFlag, NULL);CHKERRQ(ierr);
147244d8be81SLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
147344d8be81SLisandro Dalcin   PetscFunctionReturn(0);
147444d8be81SLisandro Dalcin }
147544d8be81SLisandro Dalcin 
147670034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
147770034214SMatthew G. Knepley #include <parmetis.h>
147877623264SMatthew G. Knepley #endif
147970034214SMatthew G. Knepley 
1480d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
148170034214SMatthew G. Knepley {
148277623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
14835b440754SMatthew G. Knepley   PetscPartitioner_ParMetis *pm = (PetscPartitioner_ParMetis *) part->data;
148470034214SMatthew G. Knepley   MPI_Comm       comm;
1485deea36a5SMatthew G. Knepley   PetscSection   section;
148670034214SMatthew G. Knepley   PetscInt       nvtxs       = numVertices; /* The number of vertices in full graph */
148770034214SMatthew G. Knepley   PetscInt      *vtxdist;                   /* Distribution of vertices across processes */
148870034214SMatthew G. Knepley   PetscInt      *xadj        = start;       /* Start of edge list for each vertex */
148970034214SMatthew G. Knepley   PetscInt      *adjncy      = adjacency;   /* Edge lists for all vertices */
149070034214SMatthew G. Knepley   PetscInt      *vwgt        = NULL;        /* Vertex weights */
149170034214SMatthew G. Knepley   PetscInt      *adjwgt      = NULL;        /* Edge weights */
149270034214SMatthew G. Knepley   PetscInt       wgtflag     = 0;           /* Indicates which weights are present */
149370034214SMatthew G. Knepley   PetscInt       numflag     = 0;           /* Indicates initial offset (0 or 1) */
149470034214SMatthew G. Knepley   PetscInt       ncon        = 1;           /* The number of weights per vertex */
14955b440754SMatthew G. Knepley   PetscInt       metis_ptype = pm->ptype;   /* kway or recursive bisection */
1496fb83b9f2SMichael Gegg   real_t        *tpwgts;                    /* The fraction of vertex weights assigned to each partition */
1497fb83b9f2SMichael Gegg   real_t        *ubvec;                     /* The balance intolerance for vertex weights */
1498b3ce585bSLisandro Dalcin   PetscInt       options[64];               /* Options */
149970034214SMatthew G. Knepley   /* Outputs */
1500b3ce585bSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1501b3ce585bSLisandro Dalcin   PetscMPIInt    size, rank, p;
150270034214SMatthew G. Knepley   PetscErrorCode ierr;
150370034214SMatthew G. Knepley 
150470034214SMatthew G. Knepley   PetscFunctionBegin;
150577623264SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr);
1506b3ce585bSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
150770034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
150870034214SMatthew G. Knepley   /* Calculate vertex distribution */
1509b3ce585bSLisandro Dalcin   ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr);
151070034214SMatthew G. Knepley   vtxdist[0] = 0;
151170034214SMatthew G. Knepley   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1512b3ce585bSLisandro Dalcin   for (p = 2; p <= size; ++p) {
151370034214SMatthew G. Knepley     vtxdist[p] += vtxdist[p-1];
151470034214SMatthew G. Knepley   }
151544d8be81SLisandro Dalcin   /* Calculate partition weights */
151670034214SMatthew G. Knepley   for (p = 0; p < nparts; ++p) {
151770034214SMatthew G. Knepley     tpwgts[p] = 1.0/nparts;
151870034214SMatthew G. Knepley   }
15195b440754SMatthew G. Knepley   ubvec[0] = pm->imbalanceRatio;
1520deea36a5SMatthew G. Knepley   /* Weight cells by dofs on cell by default */
1521e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1522deea36a5SMatthew G. Knepley   if (section) {
1523deea36a5SMatthew G. Knepley     PetscInt cStart, cEnd, dof;
152470034214SMatthew G. Knepley 
1525deea36a5SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1526deea36a5SMatthew G. Knepley     for (v = cStart; v < cEnd; ++v) {
1527deea36a5SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1528925b1076SLisandro Dalcin       /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1529925b1076SLisandro Dalcin       /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1530925b1076SLisandro Dalcin       if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1);
1531deea36a5SMatthew G. Knepley     }
1532deea36a5SMatthew G. Knepley   } else {
1533deea36a5SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1534cd0de0f2SShri   }
153544d8be81SLisandro Dalcin   wgtflag |= 2; /* have weights on graph vertices */
1536cd0de0f2SShri 
153770034214SMatthew G. Knepley   if (nparts == 1) {
15389fc93327SToby Isaac     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
153970034214SMatthew G. Knepley   } else {
1540b3ce585bSLisandro Dalcin     for (p = 0; !vtxdist[p+1] && p < size; ++p);
1541b3ce585bSLisandro Dalcin     if (vtxdist[p+1] == vtxdist[size]) {
1542b3ce585bSLisandro Dalcin       if (rank == p) {
154344d8be81SLisandro Dalcin         ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */
154444d8be81SLisandro Dalcin         if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()");
154544d8be81SLisandro Dalcin         if (metis_ptype == 1) {
154644d8be81SLisandro Dalcin           PetscStackPush("METIS_PartGraphRecursive");
154772379da4SMatthew G. Knepley           ierr = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
154844d8be81SLisandro Dalcin           PetscStackPop;
154944d8be81SLisandro Dalcin           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphRecursive()");
155044d8be81SLisandro Dalcin         } else {
155144d8be81SLisandro Dalcin           /*
155244d8be81SLisandro Dalcin            It would be nice to activate the two options below, but they would need some actual testing.
155344d8be81SLisandro Dalcin            - Turning on these options may exercise path of the METIS code that have bugs and may break production runs.
155444d8be81SLisandro Dalcin            - If CONTIG is set to 1, METIS will exit with error if the graph is disconnected, despite the manual saying the option is ignored in such case.
155544d8be81SLisandro Dalcin           */
155644d8be81SLisandro Dalcin           /* options[METIS_OPTION_CONTIG]  = 1; */ /* try to produce partitions that are contiguous */
155744d8be81SLisandro Dalcin           /* options[METIS_OPTION_MINCONN] = 1; */ /* minimize the maximum degree of the subdomain graph */
155870034214SMatthew G. Knepley           PetscStackPush("METIS_PartGraphKway");
155972379da4SMatthew G. Knepley           ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
156070034214SMatthew G. Knepley           PetscStackPop;
156170034214SMatthew G. Knepley           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
156270034214SMatthew G. Knepley         }
156344d8be81SLisandro Dalcin       }
156470034214SMatthew G. Knepley     } else {
15655b440754SMatthew G. Knepley       options[0] = 1;
15665b440754SMatthew G. Knepley       options[1] = pm->debugFlag;
156770034214SMatthew G. Knepley       PetscStackPush("ParMETIS_V3_PartKway");
156872379da4SMatthew G. Knepley       ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment, &comm);
156970034214SMatthew G. Knepley       PetscStackPop;
1570c717d290SMatthew G. Knepley       if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr);
157170034214SMatthew G. Knepley     }
157270034214SMatthew G. Knepley   }
157370034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
157477623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
157577623264SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
157677623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
157770034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
157877623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
157970034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
158070034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
158170034214SMatthew G. Knepley     }
158270034214SMatthew G. Knepley   }
158370034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
158470034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1585cd0de0f2SShri   ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr);
15869b80ac48SMatthew G. Knepley   PetscFunctionReturn(0);
158770034214SMatthew G. Knepley #else
158877623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis.");
158970034214SMatthew G. Knepley #endif
159070034214SMatthew G. Knepley }
159170034214SMatthew G. Knepley 
1592d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part)
159377623264SMatthew G. Knepley {
159477623264SMatthew G. Knepley   PetscFunctionBegin;
159577623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_ParMetis;
159644d8be81SLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_ParMetis;
159777623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_ParMetis;
159877623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_ParMetis;
159977623264SMatthew G. Knepley   PetscFunctionReturn(0);
160077623264SMatthew G. Knepley }
160177623264SMatthew G. Knepley 
160277623264SMatthew G. Knepley /*MC
160377623264SMatthew G. Knepley   PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library
160477623264SMatthew G. Knepley 
160577623264SMatthew G. Knepley   Level: intermediate
160677623264SMatthew G. Knepley 
160777623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
160877623264SMatthew G. Knepley M*/
160977623264SMatthew G. Knepley 
161077623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part)
161177623264SMatthew G. Knepley {
161277623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p;
161377623264SMatthew G. Knepley   PetscErrorCode          ierr;
161477623264SMatthew G. Knepley 
161577623264SMatthew G. Knepley   PetscFunctionBegin;
161677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
161777623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
161877623264SMatthew G. Knepley   part->data = p;
161977623264SMatthew G. Knepley 
16205b440754SMatthew G. Knepley   p->ptype          = 0;
16215b440754SMatthew G. Knepley   p->imbalanceRatio = 1.05;
16225b440754SMatthew G. Knepley   p->debugFlag      = 0;
16235b440754SMatthew G. Knepley 
162477623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr);
162577623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr);
162670034214SMatthew G. Knepley   PetscFunctionReturn(0);
162770034214SMatthew G. Knepley }
162870034214SMatthew G. Knepley 
1629137cd93aSLisandro Dalcin 
1630137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE;
1631137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] =
1632137cd93aSLisandro Dalcin   "@article{PTSCOTCH,\n"
1633137cd93aSLisandro Dalcin   "  author  = {C. Chevalier and F. Pellegrini},\n"
1634137cd93aSLisandro Dalcin   "  title   = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n"
1635137cd93aSLisandro Dalcin   "  journal = {Parallel Computing},\n"
1636137cd93aSLisandro Dalcin   "  volume  = {34},\n"
1637137cd93aSLisandro Dalcin   "  number  = {6},\n"
1638137cd93aSLisandro Dalcin   "  pages   = {318--331},\n"
1639137cd93aSLisandro Dalcin   "  year    = {2008},\n"
1640137cd93aSLisandro Dalcin   "  doi     = {https://doi.org/10.1016/j.parco.2007.12.001}\n"
1641137cd93aSLisandro Dalcin   "}\n";
1642137cd93aSLisandro Dalcin 
1643137cd93aSLisandro Dalcin typedef struct {
1644137cd93aSLisandro Dalcin   PetscInt  strategy;
1645137cd93aSLisandro Dalcin   PetscReal imbalance;
1646137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch;
1647137cd93aSLisandro Dalcin 
1648137cd93aSLisandro Dalcin static const char *const
1649137cd93aSLisandro Dalcin PTScotchStrategyList[] = {
1650137cd93aSLisandro Dalcin   "DEFAULT",
1651137cd93aSLisandro Dalcin   "QUALITY",
1652137cd93aSLisandro Dalcin   "SPEED",
1653137cd93aSLisandro Dalcin   "BALANCE",
1654137cd93aSLisandro Dalcin   "SAFETY",
1655137cd93aSLisandro Dalcin   "SCALABILITY",
1656137cd93aSLisandro Dalcin   "RECURSIVE",
1657137cd93aSLisandro Dalcin   "REMAP"
1658137cd93aSLisandro Dalcin };
1659137cd93aSLisandro Dalcin 
1660137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1661137cd93aSLisandro Dalcin 
1662137cd93aSLisandro Dalcin EXTERN_C_BEGIN
1663137cd93aSLisandro Dalcin #include <ptscotch.h>
1664137cd93aSLisandro Dalcin EXTERN_C_END
1665137cd93aSLisandro Dalcin 
1666137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0)
1667137cd93aSLisandro Dalcin 
1668137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy)
1669137cd93aSLisandro Dalcin {
1670137cd93aSLisandro Dalcin   switch (strategy) {
1671137cd93aSLisandro Dalcin   case  0: return SCOTCH_STRATDEFAULT;
1672137cd93aSLisandro Dalcin   case  1: return SCOTCH_STRATQUALITY;
1673137cd93aSLisandro Dalcin   case  2: return SCOTCH_STRATSPEED;
1674137cd93aSLisandro Dalcin   case  3: return SCOTCH_STRATBALANCE;
1675137cd93aSLisandro Dalcin   case  4: return SCOTCH_STRATSAFETY;
1676137cd93aSLisandro Dalcin   case  5: return SCOTCH_STRATSCALABILITY;
1677137cd93aSLisandro Dalcin   case  6: return SCOTCH_STRATRECURSIVE;
1678137cd93aSLisandro Dalcin   case  7: return SCOTCH_STRATREMAP;
1679137cd93aSLisandro Dalcin   default: return SCOTCH_STRATDEFAULT;
1680137cd93aSLisandro Dalcin   }
1681137cd93aSLisandro Dalcin }
1682137cd93aSLisandro Dalcin 
1683137cd93aSLisandro Dalcin 
1684137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1685137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[])
1686137cd93aSLisandro Dalcin {
1687137cd93aSLisandro Dalcin   SCOTCH_Graph   grafdat;
1688137cd93aSLisandro Dalcin   SCOTCH_Strat   stradat;
1689137cd93aSLisandro Dalcin   SCOTCH_Num     vertnbr = n;
1690137cd93aSLisandro Dalcin   SCOTCH_Num     edgenbr = xadj[n];
1691137cd93aSLisandro Dalcin   SCOTCH_Num*    velotab = vtxwgt;
1692137cd93aSLisandro Dalcin   SCOTCH_Num*    edlotab = adjwgt;
1693137cd93aSLisandro Dalcin   SCOTCH_Num     flagval = strategy;
1694137cd93aSLisandro Dalcin   double         kbalval = imbalance;
1695137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1696137cd93aSLisandro Dalcin 
1697137cd93aSLisandro Dalcin   PetscFunctionBegin;
1698d99a0000SVaclav Hapla   {
1699d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1700d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1701d99a0000SVaclav Hapla     if (!flg) velotab = NULL;
1702d99a0000SVaclav Hapla   }
1703137cd93aSLisandro Dalcin   ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr);
1704137cd93aSLisandro Dalcin   ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr);
1705137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1706137cd93aSLisandro Dalcin   ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr);
1707137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1708137cd93aSLisandro Dalcin   ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1709137cd93aSLisandro Dalcin #endif
1710137cd93aSLisandro Dalcin   ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr);
1711137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1712137cd93aSLisandro Dalcin   SCOTCH_graphExit(&grafdat);
1713137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1714137cd93aSLisandro Dalcin }
1715137cd93aSLisandro Dalcin 
1716137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1717137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm)
1718137cd93aSLisandro Dalcin {
1719137cd93aSLisandro Dalcin   PetscMPIInt     procglbnbr;
1720137cd93aSLisandro Dalcin   PetscMPIInt     proclocnum;
1721137cd93aSLisandro Dalcin   SCOTCH_Arch     archdat;
1722137cd93aSLisandro Dalcin   SCOTCH_Dgraph   grafdat;
1723137cd93aSLisandro Dalcin   SCOTCH_Dmapping mappdat;
1724137cd93aSLisandro Dalcin   SCOTCH_Strat    stradat;
1725137cd93aSLisandro Dalcin   SCOTCH_Num      vertlocnbr;
1726137cd93aSLisandro Dalcin   SCOTCH_Num      edgelocnbr;
1727137cd93aSLisandro Dalcin   SCOTCH_Num*     veloloctab = vtxwgt;
1728137cd93aSLisandro Dalcin   SCOTCH_Num*     edloloctab = adjwgt;
1729137cd93aSLisandro Dalcin   SCOTCH_Num      flagval = strategy;
1730137cd93aSLisandro Dalcin   double          kbalval = imbalance;
1731137cd93aSLisandro Dalcin   PetscErrorCode  ierr;
1732137cd93aSLisandro Dalcin 
1733137cd93aSLisandro Dalcin   PetscFunctionBegin;
1734d99a0000SVaclav Hapla   {
1735d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1736d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1737d99a0000SVaclav Hapla     if (!flg) veloloctab = NULL;
1738d99a0000SVaclav Hapla   }
1739137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr);
1740137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr);
1741137cd93aSLisandro Dalcin   vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum];
1742137cd93aSLisandro Dalcin   edgelocnbr = xadj[vertlocnbr];
1743137cd93aSLisandro Dalcin 
1744137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr);
1745137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr);
1746137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1747137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1748137cd93aSLisandro Dalcin #endif
1749137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1750137cd93aSLisandro Dalcin   ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr);
1751137cd93aSLisandro Dalcin   ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr);
1752137cd93aSLisandro Dalcin   ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr);
1753137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr);
1754cb87ef4cSFlorian Wechsung 
1755137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr);
1756137cd93aSLisandro Dalcin   SCOTCH_dgraphMapExit(&grafdat, &mappdat);
1757137cd93aSLisandro Dalcin   SCOTCH_archExit(&archdat);
1758137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1759137cd93aSLisandro Dalcin   SCOTCH_dgraphExit(&grafdat);
1760137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1761137cd93aSLisandro Dalcin }
1762137cd93aSLisandro Dalcin 
1763137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */
1764137cd93aSLisandro Dalcin 
1765137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part)
1766137cd93aSLisandro Dalcin {
1767137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1768137cd93aSLisandro Dalcin   PetscErrorCode             ierr;
1769137cd93aSLisandro Dalcin 
1770137cd93aSLisandro Dalcin   PetscFunctionBegin;
1771137cd93aSLisandro Dalcin   ierr = PetscFree(p);CHKERRQ(ierr);
1772137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1773137cd93aSLisandro Dalcin }
1774137cd93aSLisandro Dalcin 
1775137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer)
1776137cd93aSLisandro Dalcin {
1777137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1778137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1779137cd93aSLisandro Dalcin 
1780137cd93aSLisandro Dalcin   PetscFunctionBegin;
1781137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1782137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr);
1783137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr);
1784137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1785137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1786137cd93aSLisandro Dalcin }
1787137cd93aSLisandro Dalcin 
1788137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer)
1789137cd93aSLisandro Dalcin {
1790137cd93aSLisandro Dalcin   PetscBool      iascii;
1791137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1792137cd93aSLisandro Dalcin 
1793137cd93aSLisandro Dalcin   PetscFunctionBegin;
1794137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1795137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1796137cd93aSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1797137cd93aSLisandro Dalcin   if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);}
1798137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1799137cd93aSLisandro Dalcin }
1800137cd93aSLisandro Dalcin 
1801137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
1802137cd93aSLisandro Dalcin {
1803137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1804137cd93aSLisandro Dalcin   const char *const         *slist = PTScotchStrategyList;
1805137cd93aSLisandro Dalcin   PetscInt                  nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0]));
1806137cd93aSLisandro Dalcin   PetscBool                 flag;
1807137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1808137cd93aSLisandro Dalcin 
1809137cd93aSLisandro Dalcin   PetscFunctionBegin;
1810137cd93aSLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner PTScotch Options");CHKERRQ(ierr);
1811137cd93aSLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr);
1812137cd93aSLisandro Dalcin   ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr);
1813137cd93aSLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
1814137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1815137cd93aSLisandro Dalcin }
1816137cd93aSLisandro Dalcin 
1817137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1818137cd93aSLisandro Dalcin {
1819137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1820137cd93aSLisandro Dalcin   MPI_Comm       comm       = PetscObjectComm((PetscObject)part);
1821137cd93aSLisandro Dalcin   PetscInt       nvtxs      = numVertices; /* The number of vertices in full graph */
1822137cd93aSLisandro Dalcin   PetscInt      *vtxdist;                  /* Distribution of vertices across processes */
1823137cd93aSLisandro Dalcin   PetscInt      *xadj       = start;       /* Start of edge list for each vertex */
1824137cd93aSLisandro Dalcin   PetscInt      *adjncy     = adjacency;   /* Edge lists for all vertices */
1825137cd93aSLisandro Dalcin   PetscInt      *vwgt       = NULL;        /* Vertex weights */
1826137cd93aSLisandro Dalcin   PetscInt      *adjwgt     = NULL;        /* Edge weights */
1827137cd93aSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1828137cd93aSLisandro Dalcin   PetscMPIInt    size, rank, p;
1829137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1830137cd93aSLisandro Dalcin 
1831137cd93aSLisandro Dalcin   PetscFunctionBegin;
1832137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1833137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1834137cd93aSLisandro Dalcin   ierr = PetscMalloc2(nparts+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr);
1835137cd93aSLisandro Dalcin 
1836137cd93aSLisandro Dalcin   /* Calculate vertex distribution */
1837137cd93aSLisandro Dalcin   vtxdist[0] = 0;
1838137cd93aSLisandro Dalcin   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1839137cd93aSLisandro Dalcin   for (p = 2; p <= size; ++p) {
1840137cd93aSLisandro Dalcin     vtxdist[p] += vtxdist[p-1];
1841137cd93aSLisandro Dalcin   }
1842137cd93aSLisandro Dalcin 
1843137cd93aSLisandro Dalcin   if (nparts == 1) {
1844137cd93aSLisandro Dalcin     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
1845137cd93aSLisandro Dalcin   } else {
1846137cd93aSLisandro Dalcin     PetscSection section;
1847137cd93aSLisandro Dalcin     /* Weight cells by dofs on cell by default */
1848137cd93aSLisandro Dalcin     ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr);
1849e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1850137cd93aSLisandro Dalcin     if (section) {
1851d5528e35SFlorian Wechsung       PetscInt vStart, eEnd, dof;
1852d5528e35SFlorian Wechsung       ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &eEnd);CHKERRQ(ierr);
1853d5528e35SFlorian Wechsung       for (v = vStart; v < eEnd; ++v) {
1854137cd93aSLisandro Dalcin         ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1855137cd93aSLisandro Dalcin         /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1856137cd93aSLisandro Dalcin         /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1857137cd93aSLisandro Dalcin         if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1);
1858137cd93aSLisandro Dalcin       }
1859137cd93aSLisandro Dalcin     } else {
1860137cd93aSLisandro Dalcin       for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1861137cd93aSLisandro Dalcin     }
1862137cd93aSLisandro Dalcin     {
1863137cd93aSLisandro Dalcin       PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data;
1864137cd93aSLisandro Dalcin       int                       strat = PTScotch_Strategy(pts->strategy);
1865137cd93aSLisandro Dalcin       double                    imbal = (double)pts->imbalance;
1866137cd93aSLisandro Dalcin 
1867137cd93aSLisandro Dalcin       for (p = 0; !vtxdist[p+1] && p < size; ++p);
1868137cd93aSLisandro Dalcin       if (vtxdist[p+1] == vtxdist[size]) {
1869137cd93aSLisandro Dalcin         if (rank == p) {
1870137cd93aSLisandro Dalcin           ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr);
1871137cd93aSLisandro Dalcin         }
1872137cd93aSLisandro Dalcin       } else {
1873137cd93aSLisandro Dalcin         ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr);
1874137cd93aSLisandro Dalcin       }
1875137cd93aSLisandro Dalcin     }
1876137cd93aSLisandro Dalcin     ierr = PetscFree(vwgt);CHKERRQ(ierr);
1877137cd93aSLisandro Dalcin   }
1878137cd93aSLisandro Dalcin 
1879137cd93aSLisandro Dalcin   /* Convert to PetscSection+IS */
1880137cd93aSLisandro Dalcin   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1881137cd93aSLisandro Dalcin   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
1882137cd93aSLisandro Dalcin   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1883137cd93aSLisandro Dalcin   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
1884137cd93aSLisandro Dalcin   for (p = 0, i = 0; p < nparts; ++p) {
1885137cd93aSLisandro Dalcin     for (v = 0; v < nvtxs; ++v) {
1886137cd93aSLisandro Dalcin       if (assignment[v] == p) points[i++] = v;
1887137cd93aSLisandro Dalcin     }
1888137cd93aSLisandro Dalcin   }
1889137cd93aSLisandro Dalcin   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
1890137cd93aSLisandro Dalcin   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1891137cd93aSLisandro Dalcin 
1892137cd93aSLisandro Dalcin   ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr);
1893137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1894137cd93aSLisandro Dalcin #else
1895137cd93aSLisandro Dalcin   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch.");
1896137cd93aSLisandro Dalcin #endif
1897137cd93aSLisandro Dalcin }
1898137cd93aSLisandro Dalcin 
1899137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part)
1900137cd93aSLisandro Dalcin {
1901137cd93aSLisandro Dalcin   PetscFunctionBegin;
1902137cd93aSLisandro Dalcin   part->ops->view           = PetscPartitionerView_PTScotch;
1903137cd93aSLisandro Dalcin   part->ops->destroy        = PetscPartitionerDestroy_PTScotch;
1904137cd93aSLisandro Dalcin   part->ops->partition      = PetscPartitionerPartition_PTScotch;
1905137cd93aSLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch;
1906137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1907137cd93aSLisandro Dalcin }
1908137cd93aSLisandro Dalcin 
1909137cd93aSLisandro Dalcin /*MC
1910137cd93aSLisandro Dalcin   PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library
1911137cd93aSLisandro Dalcin 
1912137cd93aSLisandro Dalcin   Level: intermediate
1913137cd93aSLisandro Dalcin 
1914137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1915137cd93aSLisandro Dalcin M*/
1916137cd93aSLisandro Dalcin 
1917137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part)
1918137cd93aSLisandro Dalcin {
1919137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p;
1920137cd93aSLisandro Dalcin   PetscErrorCode          ierr;
1921137cd93aSLisandro Dalcin 
1922137cd93aSLisandro Dalcin   PetscFunctionBegin;
1923137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1924137cd93aSLisandro Dalcin   ierr = PetscNewLog(part, &p);CHKERRQ(ierr);
1925137cd93aSLisandro Dalcin   part->data = p;
1926137cd93aSLisandro Dalcin 
1927137cd93aSLisandro Dalcin   p->strategy  = 0;
1928137cd93aSLisandro Dalcin   p->imbalance = 0.01;
1929137cd93aSLisandro Dalcin 
1930137cd93aSLisandro Dalcin   ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr);
1931137cd93aSLisandro Dalcin   ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr);
1932137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1933137cd93aSLisandro Dalcin }
1934137cd93aSLisandro Dalcin 
1935137cd93aSLisandro Dalcin 
19365680f57bSMatthew G. Knepley /*@
19375680f57bSMatthew G. Knepley   DMPlexGetPartitioner - Get the mesh partitioner
19385680f57bSMatthew G. Knepley 
19395680f57bSMatthew G. Knepley   Not collective
19405680f57bSMatthew G. Knepley 
19415680f57bSMatthew G. Knepley   Input Parameter:
19425680f57bSMatthew G. Knepley . dm - The DM
19435680f57bSMatthew G. Knepley 
19445680f57bSMatthew G. Knepley   Output Parameter:
19455680f57bSMatthew G. Knepley . part - The PetscPartitioner
19465680f57bSMatthew G. Knepley 
19475680f57bSMatthew G. Knepley   Level: developer
19485680f57bSMatthew G. Knepley 
194998599a47SLawrence Mitchell   Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner.
195098599a47SLawrence Mitchell 
195198599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate()
19525680f57bSMatthew G. Knepley @*/
19535680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part)
19545680f57bSMatthew G. Knepley {
19555680f57bSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
19565680f57bSMatthew G. Knepley 
19575680f57bSMatthew G. Knepley   PetscFunctionBegin;
19585680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19595680f57bSMatthew G. Knepley   PetscValidPointer(part, 2);
19605680f57bSMatthew G. Knepley   *part = mesh->partitioner;
19615680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
19625680f57bSMatthew G. Knepley }
19635680f57bSMatthew G. Knepley 
196471bb2955SLawrence Mitchell /*@
196571bb2955SLawrence Mitchell   DMPlexSetPartitioner - Set the mesh partitioner
196671bb2955SLawrence Mitchell 
196771bb2955SLawrence Mitchell   logically collective on dm and part
196871bb2955SLawrence Mitchell 
196971bb2955SLawrence Mitchell   Input Parameters:
197071bb2955SLawrence Mitchell + dm - The DM
197171bb2955SLawrence Mitchell - part - The partitioner
197271bb2955SLawrence Mitchell 
197371bb2955SLawrence Mitchell   Level: developer
197471bb2955SLawrence Mitchell 
197571bb2955SLawrence Mitchell   Note: Any existing PetscPartitioner will be destroyed.
197671bb2955SLawrence Mitchell 
197771bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate()
197871bb2955SLawrence Mitchell @*/
197971bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part)
198071bb2955SLawrence Mitchell {
198171bb2955SLawrence Mitchell   DM_Plex       *mesh = (DM_Plex *) dm->data;
198271bb2955SLawrence Mitchell   PetscErrorCode ierr;
198371bb2955SLawrence Mitchell 
198471bb2955SLawrence Mitchell   PetscFunctionBegin;
198571bb2955SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
198671bb2955SLawrence Mitchell   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2);
198771bb2955SLawrence Mitchell   ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr);
198871bb2955SLawrence Mitchell   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
198971bb2955SLawrence Mitchell   mesh->partitioner = part;
199071bb2955SLawrence Mitchell   PetscFunctionReturn(0);
199171bb2955SLawrence Mitchell }
199271bb2955SLawrence Mitchell 
1993e8f14785SLisandro Dalcin static PetscErrorCode DMPlexAddClosure_Tree(DM dm, PetscHSetI ht, PetscInt point, PetscBool up, PetscBool down)
1994270bba0cSToby Isaac {
1995270bba0cSToby Isaac   PetscErrorCode ierr;
1996270bba0cSToby Isaac 
1997270bba0cSToby Isaac   PetscFunctionBegin;
19986a5a2ffdSToby Isaac   if (up) {
19996a5a2ffdSToby Isaac     PetscInt parent;
20006a5a2ffdSToby Isaac 
2001270bba0cSToby Isaac     ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr);
20026a5a2ffdSToby Isaac     if (parent != point) {
20036a5a2ffdSToby Isaac       PetscInt closureSize, *closure = NULL, i;
20046a5a2ffdSToby Isaac 
2005270bba0cSToby Isaac       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
2006270bba0cSToby Isaac       for (i = 0; i < closureSize; i++) {
2007270bba0cSToby Isaac         PetscInt cpoint = closure[2*i];
2008270bba0cSToby Isaac 
2009e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
20101b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr);
2011270bba0cSToby Isaac       }
2012270bba0cSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
20136a5a2ffdSToby Isaac     }
20146a5a2ffdSToby Isaac   }
20156a5a2ffdSToby Isaac   if (down) {
20166a5a2ffdSToby Isaac     PetscInt numChildren;
20176a5a2ffdSToby Isaac     const PetscInt *children;
20186a5a2ffdSToby Isaac 
20196a5a2ffdSToby Isaac     ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr);
20206a5a2ffdSToby Isaac     if (numChildren) {
20216a5a2ffdSToby Isaac       PetscInt i;
20226a5a2ffdSToby Isaac 
20236a5a2ffdSToby Isaac       for (i = 0; i < numChildren; i++) {
20246a5a2ffdSToby Isaac         PetscInt cpoint = children[i];
20256a5a2ffdSToby Isaac 
2026e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
20271b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr);
20286a5a2ffdSToby Isaac       }
20296a5a2ffdSToby Isaac     }
20306a5a2ffdSToby Isaac   }
2031270bba0cSToby Isaac   PetscFunctionReturn(0);
2032270bba0cSToby Isaac }
2033270bba0cSToby Isaac 
2034825f8a23SLisandro Dalcin PETSC_INTERN PetscErrorCode DMPlexPartitionLabelClosure_Private(DM,DMLabel,PetscInt,PetscInt,const PetscInt[],IS*);
2035825f8a23SLisandro Dalcin 
2036825f8a23SLisandro Dalcin PetscErrorCode DMPlexPartitionLabelClosure_Private(DM dm, DMLabel label, PetscInt rank, PetscInt numPoints, const PetscInt points[], IS *closureIS)
2037825f8a23SLisandro Dalcin {
2038825f8a23SLisandro Dalcin   DM_Plex        *mesh = (DM_Plex *)dm->data;
2039825f8a23SLisandro Dalcin   PetscBool      hasTree = (mesh->parentSection || mesh->childSection) ? PETSC_TRUE : PETSC_FALSE;
2040825f8a23SLisandro Dalcin   PetscInt       *closure = NULL, closureSize, nelems, *elems, off = 0, p, c;
2041825f8a23SLisandro Dalcin   PetscHSetI     ht;
2042825f8a23SLisandro Dalcin   PetscErrorCode ierr;
2043825f8a23SLisandro Dalcin 
2044825f8a23SLisandro Dalcin   PetscFunctionBegin;
2045825f8a23SLisandro Dalcin   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
2046825f8a23SLisandro Dalcin   ierr = PetscHSetIResize(ht, numPoints*16);CHKERRQ(ierr);
2047825f8a23SLisandro Dalcin   for (p = 0; p < numPoints; ++p) {
2048825f8a23SLisandro Dalcin     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2049825f8a23SLisandro Dalcin     for (c = 0; c < closureSize*2; c += 2) {
2050825f8a23SLisandro Dalcin       ierr = PetscHSetIAdd(ht, closure[c]);CHKERRQ(ierr);
2051825f8a23SLisandro Dalcin       if (hasTree) {ierr = DMPlexAddClosure_Tree(dm, ht, closure[c], PETSC_TRUE, PETSC_TRUE);CHKERRQ(ierr);}
2052825f8a23SLisandro Dalcin     }
2053825f8a23SLisandro Dalcin   }
2054825f8a23SLisandro Dalcin   if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);}
2055825f8a23SLisandro Dalcin   ierr = PetscHSetIGetSize(ht, &nelems);CHKERRQ(ierr);
2056825f8a23SLisandro Dalcin   ierr = PetscMalloc1(nelems, &elems);CHKERRQ(ierr);
2057825f8a23SLisandro Dalcin   ierr = PetscHSetIGetElems(ht, &off, elems);CHKERRQ(ierr);
2058825f8a23SLisandro Dalcin   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
2059825f8a23SLisandro Dalcin   ierr = PetscSortInt(nelems, elems);CHKERRQ(ierr);
2060825f8a23SLisandro Dalcin   ierr = ISCreateGeneral(PETSC_COMM_SELF, nelems, elems, PETSC_OWN_POINTER, closureIS);CHKERRQ(ierr);
2061825f8a23SLisandro Dalcin   PetscFunctionReturn(0);
2062825f8a23SLisandro Dalcin }
2063825f8a23SLisandro Dalcin 
20645abbe4feSMichael Lange /*@
20655abbe4feSMichael Lange   DMPlexPartitionLabelClosure - Add the closure of all points to the partition label
20665abbe4feSMichael Lange 
20675abbe4feSMichael Lange   Input Parameters:
20685abbe4feSMichael Lange + dm     - The DM
20695abbe4feSMichael Lange - label  - DMLabel assinging ranks to remote roots
20705abbe4feSMichael Lange 
20715abbe4feSMichael Lange   Level: developer
20725abbe4feSMichael Lange 
20735abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
20745abbe4feSMichael Lange @*/
20755abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label)
20769ffc88e4SToby Isaac {
2077825f8a23SLisandro Dalcin   IS              rankIS,   pointIS, closureIS;
20785abbe4feSMichael Lange   const PetscInt *ranks,   *points;
2079825f8a23SLisandro Dalcin   PetscInt        numRanks, numPoints, r;
20809ffc88e4SToby Isaac   PetscErrorCode  ierr;
20819ffc88e4SToby Isaac 
20829ffc88e4SToby Isaac   PetscFunctionBegin;
20835abbe4feSMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
20845abbe4feSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
20855abbe4feSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
20865abbe4feSMichael Lange   for (r = 0; r < numRanks; ++r) {
20875abbe4feSMichael Lange     const PetscInt rank = ranks[r];
20885abbe4feSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
20895abbe4feSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
20905abbe4feSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2091825f8a23SLisandro Dalcin     ierr = DMPlexPartitionLabelClosure_Private(dm, label, rank, numPoints, points, &closureIS);CHKERRQ(ierr);
20925abbe4feSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
20935abbe4feSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2094825f8a23SLisandro Dalcin     ierr = DMLabelSetStratumIS(label, rank, closureIS);CHKERRQ(ierr);
2095825f8a23SLisandro Dalcin     ierr = ISDestroy(&closureIS);CHKERRQ(ierr);
20969ffc88e4SToby Isaac   }
20975abbe4feSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
20985abbe4feSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
20999ffc88e4SToby Isaac   PetscFunctionReturn(0);
21009ffc88e4SToby Isaac }
21019ffc88e4SToby Isaac 
210224d039d7SMichael Lange /*@
210324d039d7SMichael Lange   DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label
210424d039d7SMichael Lange 
210524d039d7SMichael Lange   Input Parameters:
210624d039d7SMichael Lange + dm     - The DM
210724d039d7SMichael Lange - label  - DMLabel assinging ranks to remote roots
210824d039d7SMichael Lange 
210924d039d7SMichael Lange   Level: developer
211024d039d7SMichael Lange 
211124d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
211224d039d7SMichael Lange @*/
211324d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label)
211470034214SMatthew G. Knepley {
211524d039d7SMichael Lange   IS              rankIS,   pointIS;
211624d039d7SMichael Lange   const PetscInt *ranks,   *points;
211724d039d7SMichael Lange   PetscInt        numRanks, numPoints, r, p, a, adjSize;
211824d039d7SMichael Lange   PetscInt       *adj = NULL;
211970034214SMatthew G. Knepley   PetscErrorCode  ierr;
212070034214SMatthew G. Knepley 
212170034214SMatthew G. Knepley   PetscFunctionBegin;
212224d039d7SMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
212324d039d7SMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
212424d039d7SMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
212524d039d7SMichael Lange   for (r = 0; r < numRanks; ++r) {
212624d039d7SMichael Lange     const PetscInt rank = ranks[r];
212770034214SMatthew G. Knepley 
212824d039d7SMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
212924d039d7SMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
213024d039d7SMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
213170034214SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
213224d039d7SMichael Lange       adjSize = PETSC_DETERMINE;
213324d039d7SMichael Lange       ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr);
213424d039d7SMichael Lange       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);}
213570034214SMatthew G. Knepley     }
213624d039d7SMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
213724d039d7SMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
213870034214SMatthew G. Knepley   }
213924d039d7SMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
214024d039d7SMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
214124d039d7SMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
214224d039d7SMichael Lange   PetscFunctionReturn(0);
214370034214SMatthew G. Knepley }
214470034214SMatthew G. Knepley 
2145be200f8dSMichael Lange /*@
2146be200f8dSMichael Lange   DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF
2147be200f8dSMichael Lange 
2148be200f8dSMichael Lange   Input Parameters:
2149be200f8dSMichael Lange + dm     - The DM
2150be200f8dSMichael Lange - label  - DMLabel assinging ranks to remote roots
2151be200f8dSMichael Lange 
2152be200f8dSMichael Lange   Level: developer
2153be200f8dSMichael Lange 
2154be200f8dSMichael Lange   Note: This is required when generating multi-level overlaps to capture
2155be200f8dSMichael Lange   overlap points from non-neighbouring partitions.
2156be200f8dSMichael Lange 
2157be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
2158be200f8dSMichael Lange @*/
2159be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label)
2160be200f8dSMichael Lange {
2161be200f8dSMichael Lange   MPI_Comm        comm;
2162be200f8dSMichael Lange   PetscMPIInt     rank;
2163be200f8dSMichael Lange   PetscSF         sfPoint;
21645d04f6ebSMichael Lange   DMLabel         lblRoots, lblLeaves;
2165be200f8dSMichael Lange   IS              rankIS, pointIS;
2166be200f8dSMichael Lange   const PetscInt *ranks;
2167be200f8dSMichael Lange   PetscInt        numRanks, r;
2168be200f8dSMichael Lange   PetscErrorCode  ierr;
2169be200f8dSMichael Lange 
2170be200f8dSMichael Lange   PetscFunctionBegin;
2171be200f8dSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
2172be200f8dSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2173be200f8dSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
21745d04f6ebSMichael Lange   /* Pull point contributions from remote leaves into local roots */
21755d04f6ebSMichael Lange   ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
21765d04f6ebSMichael Lange   ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr);
21775d04f6ebSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
21785d04f6ebSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
21795d04f6ebSMichael Lange   for (r = 0; r < numRanks; ++r) {
21805d04f6ebSMichael Lange     const PetscInt remoteRank = ranks[r];
21815d04f6ebSMichael Lange     if (remoteRank == rank) continue;
21825d04f6ebSMichael Lange     ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr);
21835d04f6ebSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
21845d04f6ebSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21855d04f6ebSMichael Lange   }
21865d04f6ebSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
21875d04f6ebSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
21885d04f6ebSMichael Lange   ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
2189be200f8dSMichael Lange   /* Push point contributions from roots into remote leaves */
2190be200f8dSMichael Lange   ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
2191be200f8dSMichael Lange   ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr);
2192be200f8dSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
2193be200f8dSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
2194be200f8dSMichael Lange   for (r = 0; r < numRanks; ++r) {
2195be200f8dSMichael Lange     const PetscInt remoteRank = ranks[r];
2196be200f8dSMichael Lange     if (remoteRank == rank) continue;
2197be200f8dSMichael Lange     ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr);
2198be200f8dSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
2199be200f8dSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2200be200f8dSMichael Lange   }
2201be200f8dSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
2202be200f8dSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
2203be200f8dSMichael Lange   ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
2204be200f8dSMichael Lange   PetscFunctionReturn(0);
2205be200f8dSMichael Lange }
2206be200f8dSMichael Lange 
22071fd9873aSMichael Lange /*@
22081fd9873aSMichael Lange   DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label
22091fd9873aSMichael Lange 
22101fd9873aSMichael Lange   Input Parameters:
22111fd9873aSMichael Lange + dm        - The DM
22121fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots
22131fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank
22141fd9873aSMichael Lange 
22151fd9873aSMichael Lange   Output Parameter:
22161fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots
22171fd9873aSMichael Lange 
22181fd9873aSMichael Lange   Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The
22191fd9873aSMichael Lange   resulting leafLabel is a receiver mapping of remote roots to their parent rank.
22201fd9873aSMichael Lange 
22211fd9873aSMichael Lange   Level: developer
22221fd9873aSMichael Lange 
22231fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
22241fd9873aSMichael Lange @*/
22251fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel)
22261fd9873aSMichael Lange {
22271fd9873aSMichael Lange   MPI_Comm           comm;
2228874ddda9SLisandro Dalcin   PetscMPIInt        rank, size, r;
2229874ddda9SLisandro Dalcin   PetscInt           p, n, numNeighbors, numPoints, dof, off, rootSize, l, nleaves, leafSize;
22301fd9873aSMichael Lange   PetscSF            sfPoint;
2231874ddda9SLisandro Dalcin   PetscSection       rootSection;
22321fd9873aSMichael Lange   PetscSFNode       *rootPoints, *leafPoints;
22331fd9873aSMichael Lange   const PetscSFNode *remote;
22341fd9873aSMichael Lange   const PetscInt    *local, *neighbors;
22351fd9873aSMichael Lange   IS                 valueIS;
2236874ddda9SLisandro Dalcin   PetscBool          mpiOverflow = PETSC_FALSE;
22371fd9873aSMichael Lange   PetscErrorCode     ierr;
22381fd9873aSMichael Lange 
22391fd9873aSMichael Lange   PetscFunctionBegin;
22401fd9873aSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
22411fd9873aSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
22429852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
22431fd9873aSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
22441fd9873aSMichael Lange 
22451fd9873aSMichael Lange   /* Convert to (point, rank) and use actual owners */
22461fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
22479852e123SBarry Smith   ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr);
22481fd9873aSMichael Lange   ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr);
22491fd9873aSMichael Lange   ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr);
22501fd9873aSMichael Lange   ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr);
22511fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22521fd9873aSMichael Lange     ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr);
22531fd9873aSMichael Lange     ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr);
22541fd9873aSMichael Lange   }
22551fd9873aSMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
2256874ddda9SLisandro Dalcin   ierr = PetscSectionGetStorageSize(rootSection, &rootSize);CHKERRQ(ierr);
2257874ddda9SLisandro Dalcin   ierr = PetscMalloc1(rootSize, &rootPoints);CHKERRQ(ierr);
22581fd9873aSMichael Lange   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
22591fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22601fd9873aSMichael Lange     IS              pointIS;
22611fd9873aSMichael Lange     const PetscInt *points;
22621fd9873aSMichael Lange 
22631fd9873aSMichael Lange     ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr);
22641fd9873aSMichael Lange     ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr);
22651fd9873aSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22661fd9873aSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
22671fd9873aSMichael Lange     for (p = 0; p < numPoints; ++p) {
2268f8987ae8SMichael Lange       if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);}
2269f8987ae8SMichael Lange       else       {l = -1;}
22701fd9873aSMichael Lange       if (l >= 0) {rootPoints[off+p] = remote[l];}
22711fd9873aSMichael Lange       else        {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;}
22721fd9873aSMichael Lange     }
22731fd9873aSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
22741fd9873aSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22751fd9873aSMichael Lange   }
2276874ddda9SLisandro Dalcin 
2277874ddda9SLisandro Dalcin   /* Try to communicate overlap using All-to-All */
2278874ddda9SLisandro Dalcin   if (!processSF) {
2279874ddda9SLisandro Dalcin     PetscInt64  counter = 0;
2280874ddda9SLisandro Dalcin     PetscBool   locOverflow = PETSC_FALSE;
2281874ddda9SLisandro Dalcin     PetscMPIInt *scounts, *sdispls, *rcounts, *rdispls;
2282874ddda9SLisandro Dalcin 
2283874ddda9SLisandro Dalcin     ierr = PetscCalloc4(size, &scounts, size, &sdispls, size, &rcounts, size, &rdispls);CHKERRQ(ierr);
2284874ddda9SLisandro Dalcin     for (n = 0; n < numNeighbors; ++n) {
2285874ddda9SLisandro Dalcin       ierr = PetscSectionGetDof(rootSection, neighbors[n], &dof);CHKERRQ(ierr);
2286874ddda9SLisandro Dalcin       ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr);
2287874ddda9SLisandro Dalcin #if defined(PETSC_USE_64BIT_INDICES)
2288874ddda9SLisandro Dalcin       if (dof > PETSC_MPI_INT_MAX) {locOverflow = PETSC_TRUE; break;}
2289874ddda9SLisandro Dalcin       if (off > PETSC_MPI_INT_MAX) {locOverflow = PETSC_TRUE; break;}
2290874ddda9SLisandro Dalcin #endif
2291874ddda9SLisandro Dalcin       scounts[neighbors[n]] = (PetscMPIInt) dof;
2292874ddda9SLisandro Dalcin       sdispls[neighbors[n]] = (PetscMPIInt) off;
2293874ddda9SLisandro Dalcin     }
2294874ddda9SLisandro Dalcin     ierr = MPI_Alltoall(scounts, 1, MPI_INT, rcounts, 1, MPI_INT, comm);CHKERRQ(ierr);
2295874ddda9SLisandro Dalcin     for (r = 0; r < size; ++r) { rdispls[r] = (int)counter; counter += rcounts[r]; }
2296874ddda9SLisandro Dalcin     if (counter > PETSC_MPI_INT_MAX) locOverflow = PETSC_TRUE;
2297874ddda9SLisandro Dalcin     ierr = MPI_Allreduce(&locOverflow, &mpiOverflow, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
2298874ddda9SLisandro Dalcin     if (!mpiOverflow) {
2299874ddda9SLisandro Dalcin       leafSize = (PetscInt) counter;
2300874ddda9SLisandro Dalcin       ierr = PetscMalloc1(leafSize, &leafPoints);CHKERRQ(ierr);
2301874ddda9SLisandro Dalcin       ierr = MPI_Alltoallv(rootPoints, scounts, sdispls, MPIU_2INT, leafPoints, rcounts, rdispls, MPIU_2INT, comm);CHKERRQ(ierr);
2302874ddda9SLisandro Dalcin     }
2303874ddda9SLisandro Dalcin     ierr = PetscFree4(scounts, sdispls, rcounts, rdispls);CHKERRQ(ierr);
2304874ddda9SLisandro Dalcin   }
2305874ddda9SLisandro Dalcin 
2306874ddda9SLisandro Dalcin   /* Communicate overlap using process star forest */
2307874ddda9SLisandro Dalcin   if (processSF || mpiOverflow) {
2308874ddda9SLisandro Dalcin     PetscSF      procSF;
2309874ddda9SLisandro Dalcin     PetscSFNode  *remote;
2310874ddda9SLisandro Dalcin     PetscSection leafSection;
2311874ddda9SLisandro Dalcin 
2312874ddda9SLisandro Dalcin     if (processSF) {
2313874ddda9SLisandro Dalcin       ierr = PetscObjectReference((PetscObject)processSF);CHKERRQ(ierr);
2314874ddda9SLisandro Dalcin       procSF = processSF;
2315874ddda9SLisandro Dalcin     } else {
2316874ddda9SLisandro Dalcin       ierr = PetscMalloc1(size, &remote);CHKERRQ(ierr);
2317874ddda9SLisandro Dalcin       for (r = 0; r < size; ++r) { remote[r].rank  = r; remote[r].index = rank; }
2318874ddda9SLisandro Dalcin       ierr = PetscSFCreate(comm, &procSF);CHKERRQ(ierr);
2319874ddda9SLisandro Dalcin       ierr = PetscSFSetGraph(procSF, size, size, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2320874ddda9SLisandro Dalcin     }
2321874ddda9SLisandro Dalcin 
2322874ddda9SLisandro Dalcin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &leafSection);CHKERRQ(ierr);
23231fd9873aSMichael Lange     ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr);
2324874ddda9SLisandro Dalcin     ierr = PetscSectionGetStorageSize(leafSection, &leafSize);CHKERRQ(ierr);
2325874ddda9SLisandro Dalcin     ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
2326874ddda9SLisandro Dalcin     ierr = PetscSFDestroy(&procSF);CHKERRQ(ierr);
2327874ddda9SLisandro Dalcin   }
2328874ddda9SLisandro Dalcin 
2329874ddda9SLisandro Dalcin   for (p = 0; p < leafSize; p++) {
23301fd9873aSMichael Lange     ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr);
23311fd9873aSMichael Lange   }
2332874ddda9SLisandro Dalcin 
2333874ddda9SLisandro Dalcin   ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr);
2334874ddda9SLisandro Dalcin   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
23351fd9873aSMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
2336874ddda9SLisandro Dalcin   ierr = PetscFree(rootPoints);CHKERRQ(ierr);
23371fd9873aSMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
23381fd9873aSMichael Lange   PetscFunctionReturn(0);
23391fd9873aSMichael Lange }
23401fd9873aSMichael Lange 
2341aa3148a8SMichael Lange /*@
2342aa3148a8SMichael Lange   DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points
2343aa3148a8SMichael Lange 
2344aa3148a8SMichael Lange   Input Parameters:
2345aa3148a8SMichael Lange + dm    - The DM
2346aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots
2347aa3148a8SMichael Lange 
2348aa3148a8SMichael Lange   Output Parameter:
2349aa3148a8SMichael Lange - sf    - The star forest communication context encapsulating the defined mapping
2350aa3148a8SMichael Lange 
2351aa3148a8SMichael Lange   Note: The incoming label is a receiver mapping of remote points to their parent rank.
2352aa3148a8SMichael Lange 
2353aa3148a8SMichael Lange   Level: developer
2354aa3148a8SMichael Lange 
2355aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap
2356aa3148a8SMichael Lange @*/
2357aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf)
2358aa3148a8SMichael Lange {
23599852e123SBarry Smith   PetscMPIInt     rank, size;
236043f7d02bSMichael Lange   PetscInt        n, numRemote, p, numPoints, pStart, pEnd, idx = 0;
2361aa3148a8SMichael Lange   PetscSFNode    *remotePoints;
236243f7d02bSMichael Lange   IS              remoteRootIS;
236343f7d02bSMichael Lange   const PetscInt *remoteRoots;
2364aa3148a8SMichael Lange   PetscErrorCode ierr;
2365aa3148a8SMichael Lange 
2366aa3148a8SMichael Lange   PetscFunctionBegin;
236743f7d02bSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
23689852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
2369aa3148a8SMichael Lange 
23709852e123SBarry Smith   for (numRemote = 0, n = 0; n < size; ++n) {
2371aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
2372aa3148a8SMichael Lange     numRemote += numPoints;
2373aa3148a8SMichael Lange   }
2374aa3148a8SMichael Lange   ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr);
237543f7d02bSMichael Lange   /* Put owned points first */
237643f7d02bSMichael Lange   ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr);
237743f7d02bSMichael Lange   if (numPoints > 0) {
237843f7d02bSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr);
237943f7d02bSMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
238043f7d02bSMichael Lange     for (p = 0; p < numPoints; p++) {
238143f7d02bSMichael Lange       remotePoints[idx].index = remoteRoots[p];
238243f7d02bSMichael Lange       remotePoints[idx].rank = rank;
238343f7d02bSMichael Lange       idx++;
238443f7d02bSMichael Lange     }
238543f7d02bSMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
238643f7d02bSMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
238743f7d02bSMichael Lange   }
238843f7d02bSMichael Lange   /* Now add remote points */
23899852e123SBarry Smith   for (n = 0; n < size; ++n) {
2390aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
239143f7d02bSMichael Lange     if (numPoints <= 0 || n == rank) continue;
2392aa3148a8SMichael Lange     ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr);
2393aa3148a8SMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2394aa3148a8SMichael Lange     for (p = 0; p < numPoints; p++) {
2395aa3148a8SMichael Lange       remotePoints[idx].index = remoteRoots[p];
2396aa3148a8SMichael Lange       remotePoints[idx].rank = n;
2397aa3148a8SMichael Lange       idx++;
2398aa3148a8SMichael Lange     }
2399aa3148a8SMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2400aa3148a8SMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
2401aa3148a8SMichael Lange   }
2402aa3148a8SMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr);
2403aa3148a8SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2404aa3148a8SMichael Lange   ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
240570034214SMatthew G. Knepley   PetscFunctionReturn(0);
240670034214SMatthew G. Knepley }
2407cb87ef4cSFlorian Wechsung 
24086a3739e5SFlorian Wechsung /* The two functions below are used by DMPlexRebalanceSharedPoints which errors
24096a3739e5SFlorian Wechsung  * when PETSc is built without ParMETIS. To avoid -Wunused-function, we take
24106a3739e5SFlorian Wechsung  * them out in that case. */
24116a3739e5SFlorian Wechsung #if defined(PETSC_HAVE_PARMETIS)
24127a82c785SFlorian Wechsung /*@C
24137f57c1a4SFlorian Wechsung 
24147f57c1a4SFlorian Wechsung   DMPlexRewriteSF - Rewrites the ownership of the SF of a DM (in place).
24157f57c1a4SFlorian Wechsung 
24167f57c1a4SFlorian Wechsung   Input parameters:
24177f57c1a4SFlorian Wechsung   + dm                - The DMPlex object.
24187f57c1a4SFlorian Wechsung   + n                 - The number of points.
24197f57c1a4SFlorian Wechsung   + pointsToRewrite   - The points in the SF whose ownership will change.
24207f57c1a4SFlorian Wechsung   + targetOwners      - New owner for each element in pointsToRewrite.
24217f57c1a4SFlorian Wechsung   + degrees           - Degrees of the points in the SF as obtained by PetscSFComputeDegreeBegin/PetscSFComputeDegreeEnd.
24227f57c1a4SFlorian Wechsung 
24237f57c1a4SFlorian Wechsung   Level: developer
24247f57c1a4SFlorian Wechsung 
24257f57c1a4SFlorian Wechsung @*/
24267f57c1a4SFlorian Wechsung static PetscErrorCode DMPlexRewriteSF(DM dm, PetscInt n, PetscInt *pointsToRewrite, PetscInt *targetOwners, const PetscInt *degrees)
24277f57c1a4SFlorian Wechsung {
24287f57c1a4SFlorian Wechsung   PetscInt      ierr, pStart, pEnd, i, j, counter, leafCounter, sumDegrees, nroots, nleafs;
24297f57c1a4SFlorian Wechsung   PetscInt     *cumSumDegrees, *newOwners, *newNumbers, *rankOnLeafs, *locationsOfLeafs, *remoteLocalPointOfLeafs, *points, *leafsNew;
24307f57c1a4SFlorian Wechsung   PetscSFNode  *leafLocationsNew;
24317f57c1a4SFlorian Wechsung   const         PetscSFNode *iremote;
24327f57c1a4SFlorian Wechsung   const         PetscInt *ilocal;
24337f57c1a4SFlorian Wechsung   PetscBool    *isLeaf;
24347f57c1a4SFlorian Wechsung   PetscSF       sf;
24357f57c1a4SFlorian Wechsung   MPI_Comm      comm;
24365a30b08bSFlorian Wechsung   PetscMPIInt   rank, size;
24377f57c1a4SFlorian Wechsung 
24387f57c1a4SFlorian Wechsung   PetscFunctionBegin;
24397f57c1a4SFlorian Wechsung   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
24407f57c1a4SFlorian Wechsung   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
24417f57c1a4SFlorian Wechsung   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
24427f57c1a4SFlorian Wechsung   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
24437f57c1a4SFlorian Wechsung 
24447f57c1a4SFlorian Wechsung   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
24457f57c1a4SFlorian Wechsung   ierr = PetscSFGetGraph(sf, &nroots, &nleafs, &ilocal, &iremote); CHKERRQ(ierr);
24467f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &isLeaf);CHKERRQ(ierr);
24477f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
24487f57c1a4SFlorian Wechsung     isLeaf[i] = PETSC_FALSE;
24497f57c1a4SFlorian Wechsung   }
24507f57c1a4SFlorian Wechsung   for (i=0; i<nleafs; i++) {
24517f57c1a4SFlorian Wechsung     isLeaf[ilocal[i]-pStart] = PETSC_TRUE;
24527f57c1a4SFlorian Wechsung   }
24537f57c1a4SFlorian Wechsung 
24547f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart+1, &cumSumDegrees);CHKERRQ(ierr);
24557f57c1a4SFlorian Wechsung   cumSumDegrees[0] = 0;
24567f57c1a4SFlorian Wechsung   for (i=1; i<=pEnd-pStart; i++) {
24577f57c1a4SFlorian Wechsung     cumSumDegrees[i] = cumSumDegrees[i-1] + degrees[i-1];
24587f57c1a4SFlorian Wechsung   }
24597f57c1a4SFlorian Wechsung   sumDegrees = cumSumDegrees[pEnd-pStart];
24607f57c1a4SFlorian Wechsung   /* get the location of my leafs (we have sumDegrees many leafs pointing at our roots) */
24617f57c1a4SFlorian Wechsung 
24627f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(sumDegrees, &locationsOfLeafs);CHKERRQ(ierr);
24637f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &rankOnLeafs);CHKERRQ(ierr);
24647f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
24657f57c1a4SFlorian Wechsung     rankOnLeafs[i] = rank;
24667f57c1a4SFlorian Wechsung   }
24677f57c1a4SFlorian Wechsung   ierr = PetscSFGatherBegin(sf, MPIU_INT, rankOnLeafs, locationsOfLeafs);CHKERRQ(ierr);
24687f57c1a4SFlorian Wechsung   ierr = PetscSFGatherEnd(sf, MPIU_INT, rankOnLeafs, locationsOfLeafs);CHKERRQ(ierr);
24697f57c1a4SFlorian Wechsung   ierr = PetscFree(rankOnLeafs);CHKERRQ(ierr);
24707f57c1a4SFlorian Wechsung 
24717f57c1a4SFlorian Wechsung   /* get the remote local points of my leaves */
24727f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(sumDegrees, &remoteLocalPointOfLeafs);CHKERRQ(ierr);
24737f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &points);CHKERRQ(ierr);
24747f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
24757f57c1a4SFlorian Wechsung     points[i] = pStart+i;
24767f57c1a4SFlorian Wechsung   }
24777f57c1a4SFlorian Wechsung   ierr = PetscSFGatherBegin(sf, MPIU_INT, points, remoteLocalPointOfLeafs);CHKERRQ(ierr);
24787f57c1a4SFlorian Wechsung   ierr = PetscSFGatherEnd(sf, MPIU_INT, points, remoteLocalPointOfLeafs);CHKERRQ(ierr);
24797f57c1a4SFlorian Wechsung   ierr = PetscFree(points);CHKERRQ(ierr);
24807f57c1a4SFlorian Wechsung   /* Figure out the new owners of the vertices that are up for grabs and their numbers on the new owners */
24817f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &newOwners);CHKERRQ(ierr);
24827f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &newNumbers);CHKERRQ(ierr);
24837f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
24847f57c1a4SFlorian Wechsung     newOwners[i] = -1;
24857f57c1a4SFlorian Wechsung     newNumbers[i] = -1;
24867f57c1a4SFlorian Wechsung   }
24877f57c1a4SFlorian Wechsung   {
24887f57c1a4SFlorian Wechsung     PetscInt oldNumber, newNumber, oldOwner, newOwner;
24897f57c1a4SFlorian Wechsung     for (i=0; i<n; i++) {
24907f57c1a4SFlorian Wechsung       oldNumber = pointsToRewrite[i];
24917f57c1a4SFlorian Wechsung       newNumber = -1;
24927f57c1a4SFlorian Wechsung       oldOwner = rank;
24937f57c1a4SFlorian Wechsung       newOwner = targetOwners[i];
24947f57c1a4SFlorian Wechsung       if (oldOwner == newOwner) {
24957f57c1a4SFlorian Wechsung         newNumber = oldNumber;
24967f57c1a4SFlorian Wechsung       } else {
24977f57c1a4SFlorian Wechsung         for (j=0; j<degrees[oldNumber]; j++) {
24987f57c1a4SFlorian Wechsung           if (locationsOfLeafs[cumSumDegrees[oldNumber]+j] == newOwner) {
24997f57c1a4SFlorian Wechsung             newNumber = remoteLocalPointOfLeafs[cumSumDegrees[oldNumber]+j];
25007f57c1a4SFlorian Wechsung             break;
25017f57c1a4SFlorian Wechsung           }
25027f57c1a4SFlorian Wechsung         }
25037f57c1a4SFlorian Wechsung       }
25047f57c1a4SFlorian Wechsung       if (newNumber == -1) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Couldn't find the new owner of vertex.");
25057f57c1a4SFlorian Wechsung 
25067f57c1a4SFlorian Wechsung       newOwners[oldNumber] = newOwner;
25077f57c1a4SFlorian Wechsung       newNumbers[oldNumber] = newNumber;
25087f57c1a4SFlorian Wechsung     }
25097f57c1a4SFlorian Wechsung   }
25107f57c1a4SFlorian Wechsung   ierr = PetscFree(cumSumDegrees);CHKERRQ(ierr);
25117f57c1a4SFlorian Wechsung   ierr = PetscFree(locationsOfLeafs);CHKERRQ(ierr);
25127f57c1a4SFlorian Wechsung   ierr = PetscFree(remoteLocalPointOfLeafs);CHKERRQ(ierr);
25137f57c1a4SFlorian Wechsung 
25147f57c1a4SFlorian Wechsung   ierr = PetscSFBcastBegin(sf, MPIU_INT, newOwners, newOwners);CHKERRQ(ierr);
25157f57c1a4SFlorian Wechsung   ierr = PetscSFBcastEnd(sf, MPIU_INT, newOwners, newOwners);CHKERRQ(ierr);
25167f57c1a4SFlorian Wechsung   ierr = PetscSFBcastBegin(sf, MPIU_INT, newNumbers, newNumbers);CHKERRQ(ierr);
25177f57c1a4SFlorian Wechsung   ierr = PetscSFBcastEnd(sf, MPIU_INT, newNumbers, newNumbers);CHKERRQ(ierr);
25187f57c1a4SFlorian Wechsung 
25197f57c1a4SFlorian Wechsung   /* Now count how many leafs we have on each processor. */
25207f57c1a4SFlorian Wechsung   leafCounter=0;
25217f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
25227f57c1a4SFlorian Wechsung     if (newOwners[i] >= 0) {
25237f57c1a4SFlorian Wechsung       if (newOwners[i] != rank) {
25247f57c1a4SFlorian Wechsung         leafCounter++;
25257f57c1a4SFlorian Wechsung       }
25267f57c1a4SFlorian Wechsung     } else {
25277f57c1a4SFlorian Wechsung       if (isLeaf[i]) {
25287f57c1a4SFlorian Wechsung         leafCounter++;
25297f57c1a4SFlorian Wechsung       }
25307f57c1a4SFlorian Wechsung     }
25317f57c1a4SFlorian Wechsung   }
25327f57c1a4SFlorian Wechsung 
25337f57c1a4SFlorian Wechsung   /* Now set up the new sf by creating the leaf arrays */
25347f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(leafCounter, &leafsNew);CHKERRQ(ierr);
25357f57c1a4SFlorian Wechsung   ierr = PetscMalloc1(leafCounter, &leafLocationsNew);CHKERRQ(ierr);
25367f57c1a4SFlorian Wechsung 
25377f57c1a4SFlorian Wechsung   leafCounter = 0;
25387f57c1a4SFlorian Wechsung   counter = 0;
25397f57c1a4SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
25407f57c1a4SFlorian Wechsung     if (newOwners[i] >= 0) {
25417f57c1a4SFlorian Wechsung       if (newOwners[i] != rank) {
25427f57c1a4SFlorian Wechsung         leafsNew[leafCounter] = i;
25437f57c1a4SFlorian Wechsung         leafLocationsNew[leafCounter].rank = newOwners[i];
25447f57c1a4SFlorian Wechsung         leafLocationsNew[leafCounter].index = newNumbers[i];
25457f57c1a4SFlorian Wechsung         leafCounter++;
25467f57c1a4SFlorian Wechsung       }
25477f57c1a4SFlorian Wechsung     } else {
25487f57c1a4SFlorian Wechsung       if (isLeaf[i]) {
25497f57c1a4SFlorian Wechsung         leafsNew[leafCounter] = i;
25507f57c1a4SFlorian Wechsung         leafLocationsNew[leafCounter].rank = iremote[counter].rank;
25517f57c1a4SFlorian Wechsung         leafLocationsNew[leafCounter].index = iremote[counter].index;
25527f57c1a4SFlorian Wechsung         leafCounter++;
25537f57c1a4SFlorian Wechsung       }
25547f57c1a4SFlorian Wechsung     }
25557f57c1a4SFlorian Wechsung     if (isLeaf[i]) {
25567f57c1a4SFlorian Wechsung       counter++;
25577f57c1a4SFlorian Wechsung     }
25587f57c1a4SFlorian Wechsung   }
25597f57c1a4SFlorian Wechsung 
25607f57c1a4SFlorian Wechsung   ierr = PetscSFSetGraph(sf, nroots, leafCounter, leafsNew, PETSC_OWN_POINTER, leafLocationsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
25617f57c1a4SFlorian Wechsung   ierr = PetscFree(newOwners);CHKERRQ(ierr);
25627f57c1a4SFlorian Wechsung   ierr = PetscFree(newNumbers);CHKERRQ(ierr);
25637f57c1a4SFlorian Wechsung   ierr = PetscFree(isLeaf);CHKERRQ(ierr);
25647f57c1a4SFlorian Wechsung   PetscFunctionReturn(0);
25657f57c1a4SFlorian Wechsung }
25667f57c1a4SFlorian Wechsung 
2567125d2a8fSFlorian Wechsung static PetscErrorCode DMPlexViewDistribution(MPI_Comm comm, PetscInt n, PetscInt skip, PetscInt *vtxwgt, PetscInt *part, PetscViewer viewer)
2568125d2a8fSFlorian Wechsung {
25695a30b08bSFlorian Wechsung   PetscInt *distribution, min, max, sum, i, ierr;
25705a30b08bSFlorian Wechsung   PetscMPIInt rank, size;
2571125d2a8fSFlorian Wechsung   PetscFunctionBegin;
2572125d2a8fSFlorian Wechsung   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
2573125d2a8fSFlorian Wechsung   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2574125d2a8fSFlorian Wechsung   ierr = PetscCalloc1(size, &distribution);CHKERRQ(ierr);
2575125d2a8fSFlorian Wechsung   for (i=0; i<n; i++) {
2576125d2a8fSFlorian Wechsung     if (part) distribution[part[i]] += vtxwgt[skip*i];
2577125d2a8fSFlorian Wechsung     else distribution[rank] += vtxwgt[skip*i];
2578125d2a8fSFlorian Wechsung   }
2579125d2a8fSFlorian Wechsung   ierr = MPI_Allreduce(MPI_IN_PLACE, distribution, size, MPIU_INT, MPI_SUM, comm);CHKERRQ(ierr);
2580125d2a8fSFlorian Wechsung   min = distribution[0];
2581125d2a8fSFlorian Wechsung   max = distribution[0];
2582125d2a8fSFlorian Wechsung   sum = distribution[0];
2583125d2a8fSFlorian Wechsung   for (i=1; i<size; i++) {
2584125d2a8fSFlorian Wechsung     if (distribution[i]<min) min=distribution[i];
2585125d2a8fSFlorian Wechsung     if (distribution[i]>max) max=distribution[i];
2586125d2a8fSFlorian Wechsung     sum += distribution[i];
2587125d2a8fSFlorian Wechsung   }
2588125d2a8fSFlorian Wechsung   ierr = PetscViewerASCIIPrintf(viewer, "Min: %D, Avg: %D, Max: %D, Balance: %f\n", min, sum/size, max, (max*1.*size)/sum);CHKERRQ(ierr);
2589125d2a8fSFlorian Wechsung   ierr = PetscFree(distribution);CHKERRQ(ierr);
2590125d2a8fSFlorian Wechsung   PetscFunctionReturn(0);
2591125d2a8fSFlorian Wechsung }
2592125d2a8fSFlorian Wechsung 
25936a3739e5SFlorian Wechsung #endif
25946a3739e5SFlorian Wechsung 
2595cb87ef4cSFlorian Wechsung /*@
25965dc86ac1SFlorian Wechsung   DMPlexRebalanceSharedPoints - Redistribute points in the plex that are shared in order to achieve better balancing. This routine updates the PointSF of the DM inplace.
2597cb87ef4cSFlorian Wechsung 
2598cb87ef4cSFlorian Wechsung   Input parameters:
2599ed44d270SFlorian Wechsung   + dm               - The DMPlex object.
26007f57c1a4SFlorian Wechsung   + entityDepth      - depth of the entity to balance (0 -> balance vertices).
26017f57c1a4SFlorian Wechsung   + useInitialGuess  - whether to use the current distribution as initial guess (only used by ParMETIS).
26027f57c1a4SFlorian Wechsung   + parallel         - whether to use ParMETIS and do the partition in parallel or whether to gather the graph onto a single process and use METIS.
2603cb87ef4cSFlorian Wechsung 
2604*8b879b41SFlorian Wechsung   Output parameters:
2605*8b879b41SFlorian Wechsung   + success          - whether the graph partitioning was successful or not. If not, try useInitialGuess=True and parallel=True.
2606*8b879b41SFlorian Wechsung 
2607cb87ef4cSFlorian Wechsung   Level: user
2608cb87ef4cSFlorian Wechsung 
2609cb87ef4cSFlorian Wechsung @*/
2610cb87ef4cSFlorian Wechsung 
2611*8b879b41SFlorian Wechsung PetscErrorCode DMPlexRebalanceSharedPoints(DM dm, PetscInt entityDepth, PetscBool useInitialGuess, PetscBool parallel, PetscBool *success)
2612cb87ef4cSFlorian Wechsung {
261341525646SFlorian Wechsung #if defined(PETSC_HAVE_PARMETIS)
2614cb87ef4cSFlorian Wechsung   PetscSF     sf;
26150faf6628SFlorian Wechsung   PetscInt    ierr, i, j, idx, jdx;
26167f57c1a4SFlorian Wechsung   PetscInt    eBegin, eEnd, nroots, nleafs, pStart, pEnd;
26177f57c1a4SFlorian Wechsung   const       PetscInt *degrees, *ilocal;
26187f57c1a4SFlorian Wechsung   const       PetscSFNode *iremote;
2619cb87ef4cSFlorian Wechsung   PetscBool   *toBalance, *isLeaf, *isExclusivelyOwned, *isNonExclusivelyOwned;
2620cf818975SFlorian Wechsung   PetscInt    numExclusivelyOwned, numNonExclusivelyOwned;
2621cb87ef4cSFlorian Wechsung   PetscMPIInt rank, size;
26227f57c1a4SFlorian Wechsung   PetscInt    *globalNumbersOfLocalOwnedVertices, *leafGlobalNumbers;
26235dc86ac1SFlorian Wechsung   const       PetscInt *cumSumVertices;
2624cb87ef4cSFlorian Wechsung   PetscInt    offset, counter;
26257f57c1a4SFlorian Wechsung   PetscInt    lenadjncy;
2626cb87ef4cSFlorian Wechsung   PetscInt    *xadj, *adjncy, *vtxwgt;
2627cb87ef4cSFlorian Wechsung   PetscInt    lenxadj;
2628cb87ef4cSFlorian Wechsung   PetscInt    *adjwgt = NULL;
2629cb87ef4cSFlorian Wechsung   PetscInt    *part, *options;
2630cf818975SFlorian Wechsung   PetscInt    nparts, wgtflag, numflag, ncon, edgecut;
2631cb87ef4cSFlorian Wechsung   real_t      *ubvec;
2632cb87ef4cSFlorian Wechsung   PetscInt    *firstVertices, *renumbering;
2633cb87ef4cSFlorian Wechsung   PetscInt    failed, failedGlobal;
2634cb87ef4cSFlorian Wechsung   MPI_Comm    comm;
26354baf1206SFlorian Wechsung   Mat         A, Apre;
263612617df9SFlorian Wechsung   const char *prefix = NULL;
26377d197802SFlorian Wechsung   PetscViewer       viewer;
26387d197802SFlorian Wechsung   PetscViewerFormat format;
26395a30b08bSFlorian Wechsung   PetscLayout layout;
264012617df9SFlorian Wechsung 
264112617df9SFlorian Wechsung   PetscFunctionBegin;
2642*8b879b41SFlorian Wechsung   if (success) *success = PETSC_FALSE;
26437f57c1a4SFlorian Wechsung   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
26447f57c1a4SFlorian Wechsung   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
26457f57c1a4SFlorian Wechsung   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
26467f57c1a4SFlorian Wechsung   if (size==1) PetscFunctionReturn(0);
26477f57c1a4SFlorian Wechsung 
264841525646SFlorian Wechsung   ierr = PetscLogEventBegin(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr);
264941525646SFlorian Wechsung 
26505a30b08bSFlorian Wechsung   ierr = PetscOptionsGetViewer(comm,((PetscObject)dm)->options, prefix,"-dm_rebalance_partition_view",&viewer,&format,NULL);CHKERRQ(ierr);
26515dc86ac1SFlorian Wechsung   if (viewer) {
26525a30b08bSFlorian Wechsung     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
26537d197802SFlorian Wechsung   }
26547d197802SFlorian Wechsung 
2655ed44d270SFlorian Wechsung   /* Figure out all points in the plex that we are interested in balancing. */
2656d5528e35SFlorian Wechsung   ierr = DMPlexGetDepthStratum(dm, entityDepth, &eBegin, &eEnd);CHKERRQ(ierr);
2657cb87ef4cSFlorian Wechsung   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2658cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &toBalance);CHKERRQ(ierr);
2659cf818975SFlorian Wechsung 
2660cb87ef4cSFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
26615a7e692eSFlorian Wechsung     toBalance[i] = (PetscBool)(i-pStart>=eBegin && i-pStart<eEnd);
2662cb87ef4cSFlorian Wechsung   }
2663cb87ef4cSFlorian Wechsung 
2664cf818975SFlorian Wechsung   /* There are three types of points:
2665cf818975SFlorian Wechsung    * exclusivelyOwned: points that are owned by this process and only seen by this process
2666cf818975SFlorian Wechsung    * nonExclusivelyOwned: points that are owned by this process but seen by at least another process
2667cf818975SFlorian Wechsung    * leaf: a point that is seen by this process but owned by a different process
2668cf818975SFlorian Wechsung    */
2669cb87ef4cSFlorian Wechsung   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
2670cb87ef4cSFlorian Wechsung   ierr = PetscSFGetGraph(sf, &nroots, &nleafs, &ilocal, &iremote); CHKERRQ(ierr);
2671cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &isLeaf);CHKERRQ(ierr);
2672cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &isNonExclusivelyOwned);CHKERRQ(ierr);
2673cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &isExclusivelyOwned);CHKERRQ(ierr);
2674cb87ef4cSFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
2675cb87ef4cSFlorian Wechsung     isNonExclusivelyOwned[i] = PETSC_FALSE;
2676cb87ef4cSFlorian Wechsung     isExclusivelyOwned[i] = PETSC_FALSE;
2677cf818975SFlorian Wechsung     isLeaf[i] = PETSC_FALSE;
2678cb87ef4cSFlorian Wechsung   }
2679cf818975SFlorian Wechsung 
2680cf818975SFlorian Wechsung   /* start by marking all the leafs */
2681cb87ef4cSFlorian Wechsung   for (i=0; i<nleafs; i++) {
2682cb87ef4cSFlorian Wechsung     isLeaf[ilocal[i]-pStart] = PETSC_TRUE;
2683cb87ef4cSFlorian Wechsung   }
2684cb87ef4cSFlorian Wechsung 
2685cf818975SFlorian Wechsung   /* for an owned point, we can figure out whether another processor sees it or
2686cf818975SFlorian Wechsung    * not by calculating its degree */
26877f57c1a4SFlorian Wechsung   ierr = PetscSFComputeDegreeBegin(sf, &degrees);CHKERRQ(ierr);
26887f57c1a4SFlorian Wechsung   ierr = PetscSFComputeDegreeEnd(sf, &degrees);CHKERRQ(ierr);
2689cf818975SFlorian Wechsung 
2690cb87ef4cSFlorian Wechsung   numExclusivelyOwned = 0;
2691cb87ef4cSFlorian Wechsung   numNonExclusivelyOwned = 0;
2692cb87ef4cSFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
2693cb87ef4cSFlorian Wechsung     if (toBalance[i]) {
26947f57c1a4SFlorian Wechsung       if (degrees[i] > 0) {
2695cb87ef4cSFlorian Wechsung         isNonExclusivelyOwned[i] = PETSC_TRUE;
2696cb87ef4cSFlorian Wechsung         numNonExclusivelyOwned += 1;
2697cb87ef4cSFlorian Wechsung       } else {
2698cb87ef4cSFlorian Wechsung         if (!isLeaf[i]) {
2699cb87ef4cSFlorian Wechsung           isExclusivelyOwned[i] = PETSC_TRUE;
2700cb87ef4cSFlorian Wechsung           numExclusivelyOwned += 1;
2701cb87ef4cSFlorian Wechsung         }
2702cb87ef4cSFlorian Wechsung       }
2703cb87ef4cSFlorian Wechsung     }
2704cb87ef4cSFlorian Wechsung   }
2705cb87ef4cSFlorian Wechsung 
2706cf818975SFlorian Wechsung   /* We are going to build a graph with one vertex per core representing the
2707cf818975SFlorian Wechsung    * exclusively owned points and then one vertex per nonExclusively owned
2708cf818975SFlorian Wechsung    * point. */
2709cb87ef4cSFlorian Wechsung 
27105dc86ac1SFlorian Wechsung   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
27115dc86ac1SFlorian Wechsung   ierr = PetscLayoutSetLocalSize(layout, 1 + numNonExclusivelyOwned);CHKERRQ(ierr);
27125dc86ac1SFlorian Wechsung   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
27135dc86ac1SFlorian Wechsung   ierr = PetscLayoutGetRanges(layout, &cumSumVertices);CHKERRQ(ierr);
27145dc86ac1SFlorian Wechsung 
2715cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &globalNumbersOfLocalOwnedVertices);CHKERRQ(ierr);
2716cb87ef4cSFlorian Wechsung   offset = cumSumVertices[rank];
2717cb87ef4cSFlorian Wechsung   counter = 0;
2718cb87ef4cSFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
2719cb87ef4cSFlorian Wechsung     if (toBalance[i]) {
27207f57c1a4SFlorian Wechsung       if (degrees[i] > 0) {
2721cb87ef4cSFlorian Wechsung         globalNumbersOfLocalOwnedVertices[i] = counter + 1 + offset;
2722cb87ef4cSFlorian Wechsung         counter++;
2723cb87ef4cSFlorian Wechsung       }
2724cb87ef4cSFlorian Wechsung     }
2725cb87ef4cSFlorian Wechsung   }
2726cb87ef4cSFlorian Wechsung 
2727cb87ef4cSFlorian Wechsung   /* send the global numbers of vertices I own to the leafs so that they know to connect to it */
2728cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(pEnd-pStart, &leafGlobalNumbers);CHKERRQ(ierr);
2729cb87ef4cSFlorian Wechsung   ierr = PetscSFBcastBegin(sf, MPIU_INT, globalNumbersOfLocalOwnedVertices, leafGlobalNumbers);CHKERRQ(ierr);
2730cb87ef4cSFlorian Wechsung   ierr = PetscSFBcastEnd(sf, MPIU_INT, globalNumbersOfLocalOwnedVertices, leafGlobalNumbers);CHKERRQ(ierr);
2731cb87ef4cSFlorian Wechsung 
27320faf6628SFlorian Wechsung   /* Now start building the data structure for ParMETIS */
2733cb87ef4cSFlorian Wechsung 
27344baf1206SFlorian Wechsung   ierr = MatCreate(comm, &Apre);CHKERRQ(ierr);
27354baf1206SFlorian Wechsung   ierr = MatSetType(Apre, MATPREALLOCATOR);CHKERRQ(ierr);
27364baf1206SFlorian Wechsung   ierr = MatSetSizes(Apre, 1+numNonExclusivelyOwned, 1+numNonExclusivelyOwned, cumSumVertices[size], cumSumVertices[size]);CHKERRQ(ierr);
27374baf1206SFlorian Wechsung   ierr = MatSetUp(Apre);CHKERRQ(ierr);
27388c9a1619SFlorian Wechsung 
27398c9a1619SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
27408c9a1619SFlorian Wechsung     if (toBalance[i]) {
27410faf6628SFlorian Wechsung       idx = cumSumVertices[rank];
27420faf6628SFlorian Wechsung       if (isNonExclusivelyOwned[i]) jdx = globalNumbersOfLocalOwnedVertices[i];
27430faf6628SFlorian Wechsung       else if (isLeaf[i]) jdx = leafGlobalNumbers[i];
27440faf6628SFlorian Wechsung       else continue;
27450faf6628SFlorian Wechsung       ierr = MatSetValue(Apre, idx, jdx, 1, INSERT_VALUES);CHKERRQ(ierr);
27460faf6628SFlorian Wechsung       ierr = MatSetValue(Apre, jdx, idx, 1, INSERT_VALUES);CHKERRQ(ierr);
27474baf1206SFlorian Wechsung     }
27484baf1206SFlorian Wechsung   }
27494baf1206SFlorian Wechsung 
27504baf1206SFlorian Wechsung   ierr = MatAssemblyBegin(Apre, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27514baf1206SFlorian Wechsung   ierr = MatAssemblyEnd(Apre, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27524baf1206SFlorian Wechsung 
27534baf1206SFlorian Wechsung   ierr = MatCreate(comm, &A);CHKERRQ(ierr);
27544baf1206SFlorian Wechsung   ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr);
27554baf1206SFlorian Wechsung   ierr = MatSetSizes(A, 1+numNonExclusivelyOwned, 1+numNonExclusivelyOwned, cumSumVertices[size], cumSumVertices[size]);CHKERRQ(ierr);
27564baf1206SFlorian Wechsung   ierr = MatPreallocatorPreallocate(Apre, PETSC_FALSE, A);CHKERRQ(ierr);
27574baf1206SFlorian Wechsung   ierr = MatDestroy(&Apre);CHKERRQ(ierr);
27584baf1206SFlorian Wechsung 
27594baf1206SFlorian Wechsung   for (i=0; i<pEnd-pStart; i++) {
27604baf1206SFlorian Wechsung     if (toBalance[i]) {
27610faf6628SFlorian Wechsung       idx = cumSumVertices[rank];
27620faf6628SFlorian Wechsung       if (isNonExclusivelyOwned[i]) jdx = globalNumbersOfLocalOwnedVertices[i];
27630faf6628SFlorian Wechsung       else if (isLeaf[i]) jdx = leafGlobalNumbers[i];
27640faf6628SFlorian Wechsung       else continue;
27650faf6628SFlorian Wechsung       ierr = MatSetValue(A, idx, jdx, 1, INSERT_VALUES);CHKERRQ(ierr);
27660faf6628SFlorian Wechsung       ierr = MatSetValue(A, jdx, idx, 1, INSERT_VALUES);CHKERRQ(ierr);
27670941debbSFlorian Wechsung     }
27680941debbSFlorian Wechsung   }
27698c9a1619SFlorian Wechsung 
27708c9a1619SFlorian Wechsung   ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27718c9a1619SFlorian Wechsung   ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27724baf1206SFlorian Wechsung   ierr = PetscFree(leafGlobalNumbers);CHKERRQ(ierr);
27734baf1206SFlorian Wechsung   ierr = PetscFree(globalNumbersOfLocalOwnedVertices);CHKERRQ(ierr);
27744baf1206SFlorian Wechsung 
277541525646SFlorian Wechsung   nparts = size;
277641525646SFlorian Wechsung   wgtflag = 2;
277741525646SFlorian Wechsung   numflag = 0;
277841525646SFlorian Wechsung   ncon = 2;
277941525646SFlorian Wechsung   real_t *tpwgts;
278041525646SFlorian Wechsung   ierr = PetscMalloc1(ncon * nparts, &tpwgts);CHKERRQ(ierr);
278141525646SFlorian Wechsung   for (i=0; i<ncon*nparts; i++) {
278241525646SFlorian Wechsung     tpwgts[i] = 1./(nparts);
278341525646SFlorian Wechsung   }
278441525646SFlorian Wechsung 
278541525646SFlorian Wechsung   ierr = PetscMalloc1(ncon, &ubvec);CHKERRQ(ierr);
278641525646SFlorian Wechsung   ubvec[0] = 1.01;
27875a30b08bSFlorian Wechsung   ubvec[1] = 1.01;
27888c9a1619SFlorian Wechsung   lenadjncy = 0;
27898c9a1619SFlorian Wechsung   for (i=0; i<1+numNonExclusivelyOwned; i++) {
27908c9a1619SFlorian Wechsung     PetscInt temp=0;
27918c9a1619SFlorian Wechsung     ierr = MatGetRow(A, cumSumVertices[rank] + i, &temp, NULL, NULL);CHKERRQ(ierr);
27928c9a1619SFlorian Wechsung     lenadjncy += temp;
27938c9a1619SFlorian Wechsung     ierr = MatRestoreRow(A, cumSumVertices[rank] + i, &temp, NULL, NULL);CHKERRQ(ierr);
27948c9a1619SFlorian Wechsung   }
27958c9a1619SFlorian Wechsung   ierr = PetscMalloc1(lenadjncy, &adjncy);CHKERRQ(ierr);
27967407ba93SFlorian Wechsung   lenxadj = 2 + numNonExclusivelyOwned;
27970941debbSFlorian Wechsung   ierr = PetscMalloc1(lenxadj, &xadj);CHKERRQ(ierr);
27980941debbSFlorian Wechsung   xadj[0] = 0;
27998c9a1619SFlorian Wechsung   counter = 0;
28008c9a1619SFlorian Wechsung   for (i=0; i<1+numNonExclusivelyOwned; i++) {
28012953a68cSFlorian Wechsung     PetscInt        temp=0;
28022953a68cSFlorian Wechsung     const PetscInt *cols;
28038c9a1619SFlorian Wechsung     ierr = MatGetRow(A, cumSumVertices[rank] + i, &temp, &cols, NULL);CHKERRQ(ierr);
2804694ea26eSFlorian Wechsung     ierr = PetscMemcpy(&adjncy[counter], cols, temp*sizeof(PetscInt));CHKERRQ(ierr);
28058c9a1619SFlorian Wechsung     counter += temp;
28060941debbSFlorian Wechsung     xadj[i+1] = counter;
28078c9a1619SFlorian Wechsung     ierr = MatRestoreRow(A, cumSumVertices[rank] + i, &temp, &cols, NULL);CHKERRQ(ierr);
28088c9a1619SFlorian Wechsung   }
28098c9a1619SFlorian Wechsung 
2810cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(cumSumVertices[rank+1]-cumSumVertices[rank], &part);CHKERRQ(ierr);
281141525646SFlorian Wechsung   ierr = PetscMalloc1(ncon*(1 + numNonExclusivelyOwned), &vtxwgt);CHKERRQ(ierr);
281241525646SFlorian Wechsung   vtxwgt[0] = numExclusivelyOwned;
281341525646SFlorian Wechsung   if (ncon>1) vtxwgt[1] = 1;
281441525646SFlorian Wechsung   for (i=0; i<numNonExclusivelyOwned; i++) {
281541525646SFlorian Wechsung     vtxwgt[ncon*(i+1)] = 1;
281641525646SFlorian Wechsung     if (ncon>1) vtxwgt[ncon*(i+1)+1] = 0;
281741525646SFlorian Wechsung   }
28188c9a1619SFlorian Wechsung 
28195dc86ac1SFlorian Wechsung   if (viewer) {
28207d197802SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Attempt rebalancing of shared points of depth %D on interface of mesh distribution.\n", entityDepth);CHKERRQ(ierr);
28217d197802SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Size of generated auxiliary graph: %D\n", cumSumVertices[size]);CHKERRQ(ierr);
282212617df9SFlorian Wechsung   }
282341525646SFlorian Wechsung   if (parallel) {
28245a30b08bSFlorian Wechsung     ierr = PetscMalloc1(4, &options);CHKERRQ(ierr);
28255a30b08bSFlorian Wechsung     options[0] = 1;
28265a30b08bSFlorian Wechsung     options[1] = 0; /* Verbosity */
28275a30b08bSFlorian Wechsung     options[2] = 0; /* Seed */
28285a30b08bSFlorian Wechsung     options[3] = PARMETIS_PSR_COUPLED; /* Seed */
28295dc86ac1SFlorian Wechsung     if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using ParMETIS to partition graph.\n");CHKERRQ(ierr); }
28308c9a1619SFlorian Wechsung     if (useInitialGuess) {
28315dc86ac1SFlorian Wechsung       if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using current distribution of points as initial guess.\n");CHKERRQ(ierr); }
28328c9a1619SFlorian Wechsung       PetscStackPush("ParMETIS_V3_RefineKway");
28335dc86ac1SFlorian Wechsung       ierr = ParMETIS_V3_RefineKway((PetscInt*)cumSumVertices, xadj, adjncy, vtxwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgecut, part, &comm);
283406b3913eSFlorian Wechsung       if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in ParMETIS_V3_RefineKway()");
28358c9a1619SFlorian Wechsung       PetscStackPop;
28368c9a1619SFlorian Wechsung     } else {
28378c9a1619SFlorian Wechsung       PetscStackPush("ParMETIS_V3_PartKway");
28385dc86ac1SFlorian Wechsung       ierr = ParMETIS_V3_PartKway((PetscInt*)cumSumVertices, xadj, adjncy, vtxwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgecut, part, &comm);
28398c9a1619SFlorian Wechsung       PetscStackPop;
284006b3913eSFlorian Wechsung       if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in ParMETIS_V3_PartKway()");
28418c9a1619SFlorian Wechsung     }
2842ef74bcceSFlorian Wechsung     ierr = PetscFree(options);CHKERRQ(ierr);
284341525646SFlorian Wechsung   } else {
28445dc86ac1SFlorian Wechsung     if (viewer) { ierr = PetscViewerASCIIPrintf(viewer, "Using METIS to partition graph.\n");CHKERRQ(ierr); }
284541525646SFlorian Wechsung     Mat As;
284641525646SFlorian Wechsung     PetscInt numRows;
284741525646SFlorian Wechsung     PetscInt *partGlobal;
284841525646SFlorian Wechsung     ierr = MatCreateRedundantMatrix(A, size, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &As);CHKERRQ(ierr);
2849cb87ef4cSFlorian Wechsung 
285041525646SFlorian Wechsung     PetscInt *numExclusivelyOwnedAll;
285141525646SFlorian Wechsung     ierr = PetscMalloc1(size, &numExclusivelyOwnedAll);CHKERRQ(ierr);
285241525646SFlorian Wechsung     numExclusivelyOwnedAll[rank] = numExclusivelyOwned;
28532953a68cSFlorian Wechsung     ierr = MPI_Allgather(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,numExclusivelyOwnedAll,1,MPIU_INT,comm);CHKERRQ(ierr);
2854cb87ef4cSFlorian Wechsung 
285541525646SFlorian Wechsung     ierr = MatGetSize(As, &numRows, NULL);CHKERRQ(ierr);
285641525646SFlorian Wechsung     ierr = PetscMalloc1(numRows, &partGlobal);CHKERRQ(ierr);
28575dc86ac1SFlorian Wechsung     if (!rank) {
285841525646SFlorian Wechsung       PetscInt *adjncy_g, *xadj_g, *vtxwgt_g;
285941525646SFlorian Wechsung       lenadjncy = 0;
286041525646SFlorian Wechsung 
286141525646SFlorian Wechsung       for (i=0; i<numRows; i++) {
286241525646SFlorian Wechsung         PetscInt temp=0;
286341525646SFlorian Wechsung         ierr = MatGetRow(As, i, &temp, NULL, NULL);CHKERRQ(ierr);
286441525646SFlorian Wechsung         lenadjncy += temp;
286541525646SFlorian Wechsung         ierr = MatRestoreRow(As, i, &temp, NULL, NULL);CHKERRQ(ierr);
286641525646SFlorian Wechsung       }
286741525646SFlorian Wechsung       ierr = PetscMalloc1(lenadjncy, &adjncy_g);CHKERRQ(ierr);
286841525646SFlorian Wechsung       lenxadj = 1 + numRows;
286941525646SFlorian Wechsung       ierr = PetscMalloc1(lenxadj, &xadj_g);CHKERRQ(ierr);
287041525646SFlorian Wechsung       xadj_g[0] = 0;
287141525646SFlorian Wechsung       counter = 0;
287241525646SFlorian Wechsung       for (i=0; i<numRows; i++) {
28732953a68cSFlorian Wechsung         PetscInt        temp=0;
28742953a68cSFlorian Wechsung         const PetscInt *cols;
287541525646SFlorian Wechsung         ierr = MatGetRow(As, i, &temp, &cols, NULL);CHKERRQ(ierr);
2876694ea26eSFlorian Wechsung         ierr = PetscMemcpy(&adjncy_g[counter], cols, temp*sizeof(PetscInt));CHKERRQ(ierr);
287741525646SFlorian Wechsung         counter += temp;
287841525646SFlorian Wechsung         xadj_g[i+1] = counter;
287941525646SFlorian Wechsung         ierr = MatRestoreRow(As, i, &temp, &cols, NULL);CHKERRQ(ierr);
288041525646SFlorian Wechsung       }
288141525646SFlorian Wechsung       ierr = PetscMalloc1(2*numRows, &vtxwgt_g);CHKERRQ(ierr);
288241525646SFlorian Wechsung       for (i=0; i<size; i++){
288341525646SFlorian Wechsung         vtxwgt_g[ncon*cumSumVertices[i]] = numExclusivelyOwnedAll[i];
288441525646SFlorian Wechsung         if (ncon>1) vtxwgt_g[ncon*cumSumVertices[i]+1] = 1;
288541525646SFlorian Wechsung         for (j=cumSumVertices[i]+1; j<cumSumVertices[i+1]; j++) {
288641525646SFlorian Wechsung           vtxwgt_g[ncon*j] = 1;
288741525646SFlorian Wechsung           if (ncon>1) vtxwgt_g[2*j+1] = 0;
288841525646SFlorian Wechsung         }
288941525646SFlorian Wechsung       }
289041525646SFlorian Wechsung       ierr = PetscMalloc1(64, &options);CHKERRQ(ierr);
289141525646SFlorian Wechsung       ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */
289206b3913eSFlorian Wechsung       if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()");
289341525646SFlorian Wechsung       options[METIS_OPTION_CONTIG] = 1;
289441525646SFlorian Wechsung       PetscStackPush("METIS_PartGraphKway");
289506b3913eSFlorian Wechsung       ierr = METIS_PartGraphKway(&numRows, &ncon, xadj_g, adjncy_g, vtxwgt_g, NULL, NULL, &nparts, tpwgts, ubvec, options, &edgecut, partGlobal);
289641525646SFlorian Wechsung       PetscStackPop;
289706b3913eSFlorian Wechsung       if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
2898ef74bcceSFlorian Wechsung       ierr = PetscFree(options);CHKERRQ(ierr);
289941525646SFlorian Wechsung       ierr = PetscFree(xadj_g);CHKERRQ(ierr);
290041525646SFlorian Wechsung       ierr = PetscFree(adjncy_g);CHKERRQ(ierr);
290141525646SFlorian Wechsung       ierr = PetscFree(vtxwgt_g);CHKERRQ(ierr);
290241525646SFlorian Wechsung     }
290341525646SFlorian Wechsung     ierr = PetscFree(numExclusivelyOwnedAll);CHKERRQ(ierr);
290441525646SFlorian Wechsung 
29055dc86ac1SFlorian Wechsung     /* Now scatter the parts array. */
29065dc86ac1SFlorian Wechsung     {
290781a13b52SFlorian Wechsung       PetscMPIInt *counts, *mpiCumSumVertices;
29085dc86ac1SFlorian Wechsung       ierr = PetscMalloc1(size, &counts);CHKERRQ(ierr);
290981a13b52SFlorian Wechsung       ierr = PetscMalloc1(size+1, &mpiCumSumVertices);CHKERRQ(ierr);
29105dc86ac1SFlorian Wechsung       for(i=0; i<size; i++) {
291181a13b52SFlorian Wechsung         ierr = PetscMPIIntCast(cumSumVertices[i+1] - cumSumVertices[i], &(counts[i]));CHKERRQ(ierr);
291241525646SFlorian Wechsung       }
291381a13b52SFlorian Wechsung       for(i=0; i<=size; i++) {
291481a13b52SFlorian Wechsung         ierr = PetscMPIIntCast(cumSumVertices[i], &(mpiCumSumVertices[i]));CHKERRQ(ierr);
291581a13b52SFlorian Wechsung       }
291681a13b52SFlorian Wechsung       ierr = MPI_Scatterv(partGlobal, counts, mpiCumSumVertices, MPIU_INT, part, counts[rank], MPIU_INT, 0, comm);CHKERRQ(ierr);
29175dc86ac1SFlorian Wechsung       ierr = PetscFree(counts);CHKERRQ(ierr);
291881a13b52SFlorian Wechsung       ierr = PetscFree(mpiCumSumVertices);CHKERRQ(ierr);
29195dc86ac1SFlorian Wechsung     }
29205dc86ac1SFlorian Wechsung 
292141525646SFlorian Wechsung     ierr = PetscFree(partGlobal);CHKERRQ(ierr);
29222953a68cSFlorian Wechsung     ierr = MatDestroy(&As);CHKERRQ(ierr);
292341525646SFlorian Wechsung   }
2924cb87ef4cSFlorian Wechsung 
29252953a68cSFlorian Wechsung   ierr = MatDestroy(&A);CHKERRQ(ierr);
2926cb87ef4cSFlorian Wechsung   ierr = PetscFree(ubvec);CHKERRQ(ierr);
2927cb87ef4cSFlorian Wechsung   ierr = PetscFree(tpwgts);CHKERRQ(ierr);
2928cb87ef4cSFlorian Wechsung 
2929cb87ef4cSFlorian Wechsung   /* Now rename the result so that the vertex resembling the exclusively owned points stays on the same rank */
2930cb87ef4cSFlorian Wechsung 
2931cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(size, &firstVertices);CHKERRQ(ierr);
2932cb87ef4cSFlorian Wechsung   ierr = PetscMalloc1(size, &renumbering);CHKERRQ(ierr);
2933cb87ef4cSFlorian Wechsung   firstVertices[rank] = part[0];
29342953a68cSFlorian Wechsung   ierr = MPI_Allgather(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,firstVertices,1,MPIU_INT,comm);CHKERRQ(ierr);
2935cb87ef4cSFlorian Wechsung   for (i=0; i<size; i++) {
2936cb87ef4cSFlorian Wechsung     renumbering[firstVertices[i]] = i;
2937cb87ef4cSFlorian Wechsung   }
2938cb87ef4cSFlorian Wechsung   for (i=0; i<cumSumVertices[rank+1]-cumSumVertices[rank]; i++) {
2939cb87ef4cSFlorian Wechsung     part[i] = renumbering[part[i]];
2940cb87ef4cSFlorian Wechsung   }
2941cb87ef4cSFlorian Wechsung   /* Check if the renumbering worked (this can fail when ParMETIS gives fewer partitions than there are processes) */
2942cb87ef4cSFlorian Wechsung   failed = (PetscInt)(part[0] != rank);
29432953a68cSFlorian Wechsung   ierr = MPI_Allreduce(&failed, &failedGlobal, 1, MPIU_INT, MPI_SUM, comm);CHKERRQ(ierr);
2944cb87ef4cSFlorian Wechsung 
29457f57c1a4SFlorian Wechsung   ierr = PetscFree(firstVertices);CHKERRQ(ierr);
29467f57c1a4SFlorian Wechsung   ierr = PetscFree(renumbering);CHKERRQ(ierr);
29477f57c1a4SFlorian Wechsung 
2948cb87ef4cSFlorian Wechsung   if (failedGlobal > 0) {
29497f57c1a4SFlorian Wechsung     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
29507f57c1a4SFlorian Wechsung     ierr = PetscFree(xadj);CHKERRQ(ierr);
29517f57c1a4SFlorian Wechsung     ierr = PetscFree(adjncy);CHKERRQ(ierr);
29527f57c1a4SFlorian Wechsung     ierr = PetscFree(vtxwgt);CHKERRQ(ierr);
29537f57c1a4SFlorian Wechsung     ierr = PetscFree(toBalance);CHKERRQ(ierr);
29547f57c1a4SFlorian Wechsung     ierr = PetscFree(isLeaf);CHKERRQ(ierr);
29557f57c1a4SFlorian Wechsung     ierr = PetscFree(isNonExclusivelyOwned);CHKERRQ(ierr);
29567f57c1a4SFlorian Wechsung     ierr = PetscFree(isExclusivelyOwned);CHKERRQ(ierr);
29577f57c1a4SFlorian Wechsung     ierr = PetscFree(part);CHKERRQ(ierr);
29587f57c1a4SFlorian Wechsung     if (viewer) {
295906b3913eSFlorian Wechsung       ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
296006b3913eSFlorian Wechsung       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
29617f57c1a4SFlorian Wechsung     }
29627f57c1a4SFlorian Wechsung     ierr = PetscLogEventEnd(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr);
2963*8b879b41SFlorian Wechsung     PetscFunctionReturn(0);
2964cb87ef4cSFlorian Wechsung   }
2965cb87ef4cSFlorian Wechsung 
29667407ba93SFlorian Wechsung   /*Let's check how well we did distributing points*/
29675dc86ac1SFlorian Wechsung   if (viewer) {
29687d197802SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Comparing number of owned entities of depth %D on each process before rebalancing, after rebalancing, and after consistency checks.\n", entityDepth);CHKERRQ(ierr);
2969125d2a8fSFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Initial.     ");CHKERRQ(ierr);
2970125d2a8fSFlorian Wechsung     ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, NULL, viewer);CHKERRQ(ierr);
2971125d2a8fSFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Rebalanced.  ");CHKERRQ(ierr);
2972125d2a8fSFlorian Wechsung     ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, part, viewer);CHKERRQ(ierr);
29737407ba93SFlorian Wechsung   }
29747407ba93SFlorian Wechsung 
2975cb87ef4cSFlorian Wechsung   /* Now check that every vertex is owned by a process that it is actually connected to. */
297641525646SFlorian Wechsung   for (i=1; i<=numNonExclusivelyOwned; i++) {
2977cb87ef4cSFlorian Wechsung     PetscInt loc = 0;
297841525646SFlorian Wechsung     ierr = PetscFindInt(cumSumVertices[part[i]], xadj[i+1]-xadj[i], &adjncy[xadj[i]], &loc);CHKERRQ(ierr);
29797407ba93SFlorian Wechsung     /* If not, then just set the owner to the original owner (hopefully a rare event, it means that a vertex has been isolated) */
2980cb87ef4cSFlorian Wechsung     if (loc<0) {
298141525646SFlorian Wechsung       part[i] = rank;
2982cb87ef4cSFlorian Wechsung     }
2983cb87ef4cSFlorian Wechsung   }
2984cb87ef4cSFlorian Wechsung 
29857407ba93SFlorian Wechsung   /* Let's see how significant the influences of the previous fixing up step was.*/
29865dc86ac1SFlorian Wechsung   if (viewer) {
2987125d2a8fSFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "After.       ");CHKERRQ(ierr);
2988125d2a8fSFlorian Wechsung     ierr = DMPlexViewDistribution(comm, cumSumVertices[rank+1]-cumSumVertices[rank], ncon, vtxwgt, part, viewer);CHKERRQ(ierr);
29897407ba93SFlorian Wechsung   }
29907407ba93SFlorian Wechsung 
29915dc86ac1SFlorian Wechsung   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
2992cb87ef4cSFlorian Wechsung   ierr = PetscFree(xadj);CHKERRQ(ierr);
2993cb87ef4cSFlorian Wechsung   ierr = PetscFree(adjncy);CHKERRQ(ierr);
299441525646SFlorian Wechsung   ierr = PetscFree(vtxwgt);CHKERRQ(ierr);
2995cb87ef4cSFlorian Wechsung 
29967f57c1a4SFlorian Wechsung   /* Almost done, now rewrite the SF to reflect the new ownership. */
2997cf818975SFlorian Wechsung   {
29987f57c1a4SFlorian Wechsung     PetscInt *pointsToRewrite;
299906b3913eSFlorian Wechsung     ierr = PetscMalloc1(numNonExclusivelyOwned, &pointsToRewrite);CHKERRQ(ierr);
30007f57c1a4SFlorian Wechsung     counter = 0;
3001cb87ef4cSFlorian Wechsung     for(i=0; i<pEnd-pStart; i++) {
3002cb87ef4cSFlorian Wechsung       if (toBalance[i]) {
3003cb87ef4cSFlorian Wechsung         if (isNonExclusivelyOwned[i]) {
30047f57c1a4SFlorian Wechsung           pointsToRewrite[counter] = i + pStart;
3005cb87ef4cSFlorian Wechsung           counter++;
3006cb87ef4cSFlorian Wechsung         }
3007cb87ef4cSFlorian Wechsung       }
3008cb87ef4cSFlorian Wechsung     }
30097f57c1a4SFlorian Wechsung     ierr = DMPlexRewriteSF(dm, numNonExclusivelyOwned, pointsToRewrite, part+1, degrees);CHKERRQ(ierr);
30107f57c1a4SFlorian Wechsung     ierr = PetscFree(pointsToRewrite);CHKERRQ(ierr);
3011cf818975SFlorian Wechsung   }
3012cb87ef4cSFlorian Wechsung 
3013cb87ef4cSFlorian Wechsung   ierr = PetscFree(toBalance);CHKERRQ(ierr);
3014cb87ef4cSFlorian Wechsung   ierr = PetscFree(isLeaf);CHKERRQ(ierr);
3015cf818975SFlorian Wechsung   ierr = PetscFree(isNonExclusivelyOwned);CHKERRQ(ierr);
3016cf818975SFlorian Wechsung   ierr = PetscFree(isExclusivelyOwned);CHKERRQ(ierr);
30177f57c1a4SFlorian Wechsung   ierr = PetscFree(part);CHKERRQ(ierr);
30185dc86ac1SFlorian Wechsung   if (viewer) {
301906b3913eSFlorian Wechsung     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
302006b3913eSFlorian Wechsung     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
30217d197802SFlorian Wechsung   }
3022*8b879b41SFlorian Wechsung   if (success) *success = PETSC_TRUE;
302341525646SFlorian Wechsung   ierr = PetscLogEventEnd(DMPLEX_RebalanceSharedPoints, dm, 0, 0, 0);CHKERRQ(ierr);
3024240408d3SFlorian Wechsung #else
30255f441e9bSFlorian Wechsung   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis.");
302641525646SFlorian Wechsung #endif
3027cb87ef4cSFlorian Wechsung   PetscFunctionReturn(0);
3028cb87ef4cSFlorian Wechsung }
3029