xref: /petsc/src/dm/impls/plex/plexpartition.c (revision ffbd61634109cd55a4e1889a4f100c5a1bdbfef1)
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 
43532c4e7dSMichael Lange   The user can control the definition of adjacency for the mesh using DMPlexGetAdjacencyUseCone() and
44532c4e7dSMichael Lange   DMPlexSetAdjacencyUseClosure(). They should choose the combination appropriate for the function
45532c4e7dSMichael Lange   representation on the mesh.
46532c4e7dSMichael Lange 
47532c4e7dSMichael Lange   Level: developer
48532c4e7dSMichael Lange 
49532c4e7dSMichael Lange .seealso: PetscPartitionerGetType(), PetscPartitionerCreate(), DMPlexSetAdjacencyUseCone(), DMPlexSetAdjacencyUseClosure()
50532c4e7dSMichael Lange @*/
513fa7752dSToby Isaac PetscErrorCode DMPlexCreatePartitionerGraph(DM dm, PetscInt height, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency, IS *globalNumbering)
52532c4e7dSMichael Lange {
53*ffbd6163SMatthew G. Knepley   PetscInt       dim, depth, p, pStart, pEnd, a, adjSize, idx, size;
54389e55d8SMichael Lange   PetscInt      *adj = NULL, *vOffsets = NULL, *graph = NULL;
558cfe4c1fSMichael Lange   IS             cellNumbering;
568cfe4c1fSMichael Lange   const PetscInt *cellNum;
57532c4e7dSMichael Lange   PetscBool      useCone, useClosure;
58532c4e7dSMichael Lange   PetscSection   section;
59532c4e7dSMichael Lange   PetscSegBuffer adjBuffer;
608cfe4c1fSMichael Lange   PetscSF        sfPoint;
618f4e72b9SMatthew G. Knepley   PetscInt       *adjCells = NULL, *remoteCells = NULL;
628f4e72b9SMatthew G. Knepley   const PetscInt *local;
638f4e72b9SMatthew G. Knepley   PetscInt       nroots, nleaves, l;
64532c4e7dSMichael Lange   PetscMPIInt    rank;
65532c4e7dSMichael Lange   PetscErrorCode ierr;
66532c4e7dSMichael Lange 
67532c4e7dSMichael Lange   PetscFunctionBegin;
68532c4e7dSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
69*ffbd6163SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
70*ffbd6163SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
71*ffbd6163SMatthew G. Knepley   if (dim != depth) {
72*ffbd6163SMatthew G. Knepley     /* We do not handle the uninterpolated case here */
73*ffbd6163SMatthew G. Knepley     ierr = DMPlexCreateNeighborCSR(dm, height, numVertices, offsets, adjacency);CHKERRQ(ierr);
74*ffbd6163SMatthew G. Knepley     /* DMPlexCreateNeighborCSR does not make a numbering */
75*ffbd6163SMatthew G. Knepley     if (globalNumbering) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, globalNumbering);CHKERRQ(ierr);}
76*ffbd6163SMatthew G. Knepley     /* Different behavior for empty graphs */
77*ffbd6163SMatthew G. Knepley     if (!*numVertices) {
78*ffbd6163SMatthew G. Knepley       ierr = PetscMalloc1(1, offsets);CHKERRQ(ierr);
79*ffbd6163SMatthew G. Knepley       (*offsets)[0] = 0;
80*ffbd6163SMatthew G. Knepley     }
81*ffbd6163SMatthew G. Knepley     /* Broken in parallel */
82*ffbd6163SMatthew G. Knepley     if (rank && *numVertices) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Parallel partitioning of uninterpolated meshes not supported");
83*ffbd6163SMatthew G. Knepley     PetscFunctionReturn(0);
84*ffbd6163SMatthew G. Knepley   }
85532c4e7dSMichael Lange   ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr);
868cfe4c1fSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
878cfe4c1fSMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
88532c4e7dSMichael Lange   /* Build adjacency graph via a section/segbuffer */
89532c4e7dSMichael Lange   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
90532c4e7dSMichael Lange   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
91532c4e7dSMichael Lange   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr);
92532c4e7dSMichael Lange   /* Always use FVM adjacency to create partitioner graph */
93532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
94532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
95532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, PETSC_TRUE);CHKERRQ(ierr);
96532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, PETSC_FALSE);CHKERRQ(ierr);
97f0927f4eSMatthew G. Knepley   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &cellNumbering);CHKERRQ(ierr);
983fa7752dSToby Isaac   if (globalNumbering) {
993fa7752dSToby Isaac     ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr);
1003fa7752dSToby Isaac     *globalNumbering = cellNumbering;
1013fa7752dSToby Isaac   }
1028cfe4c1fSMichael Lange   ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
1038f4e72b9SMatthew G. Knepley   /* For all boundary faces (including faces adjacent to a ghost cell), record the local cell in adjCells
1048f4e72b9SMatthew G. Knepley      Broadcast adjCells to remoteCells (to get cells from roots) and Reduce adjCells to remoteCells (to get cells from leaves)
1058f4e72b9SMatthew G. Knepley    */
1068f4e72b9SMatthew G. Knepley   ierr = PetscSFGetGraph(dm->sf, &nroots, &nleaves, &local, NULL);CHKERRQ(ierr);
1078f4e72b9SMatthew G. Knepley   if (nroots >= 0) {
1088f4e72b9SMatthew G. Knepley     PetscInt fStart, fEnd, f;
1098f4e72b9SMatthew G. Knepley 
1108f4e72b9SMatthew G. Knepley     ierr = PetscCalloc2(nroots, &adjCells, nroots, &remoteCells);CHKERRQ(ierr);
1118f4e72b9SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1128f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) adjCells[l] = -3;
1138f4e72b9SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
1148f4e72b9SMatthew G. Knepley       const PetscInt *support;
1158f4e72b9SMatthew G. Knepley       PetscInt        supportSize;
1168f4e72b9SMatthew G. Knepley 
1178f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
1188f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
1198f4e72b9SMatthew G. Knepley       if (supportSize == 1) adjCells[f] = cellNum[support[0]];
1208f4e72b9SMatthew G. Knepley       else if (supportSize == 2) {
1218f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[0], nleaves, local, &p);CHKERRQ(ierr);
1228f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[1]];
1238f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[1], nleaves, local, &p);CHKERRQ(ierr);
1248f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[0]];
1258f4e72b9SMatthew G. Knepley       }
1268f4e72b9SMatthew G. Knepley     }
1278f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) remoteCells[l] = -1;
1288f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastBegin(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
1298f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastEnd(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
1308f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceBegin(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
1318f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceEnd(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
1328f4e72b9SMatthew G. Knepley   }
1338f4e72b9SMatthew G. Knepley   /* Combine local and global adjacencies */
1348cfe4c1fSMichael Lange   for (*numVertices = 0, p = pStart; p < pEnd; p++) {
1358f4e72b9SMatthew G. Knepley     const PetscInt *cone;
1368f4e72b9SMatthew G. Knepley     PetscInt        coneSize, c;
1378f4e72b9SMatthew G. Knepley 
1388cfe4c1fSMichael Lange     /* Skip non-owned cells in parallel (ParMetis expects no overlap) */
1398cfe4c1fSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
1408f4e72b9SMatthew G. Knepley     /* Add remote cells */
1418f4e72b9SMatthew G. Knepley     if (remoteCells) {
1428f4e72b9SMatthew G. Knepley       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1438f4e72b9SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1448f4e72b9SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
1458f4e72b9SMatthew G. Knepley         if (remoteCells[cone[c]] != -1) {
1468f4e72b9SMatthew G. Knepley           PetscInt *PETSC_RESTRICT pBuf;
1478f4e72b9SMatthew G. Knepley 
1488f4e72b9SMatthew G. Knepley           ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
1498f4e72b9SMatthew G. Knepley           ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
1508f4e72b9SMatthew G. Knepley           *pBuf = remoteCells[cone[c]];
1518f4e72b9SMatthew G. Knepley         }
1528f4e72b9SMatthew G. Knepley       }
1538f4e72b9SMatthew G. Knepley     }
1548f4e72b9SMatthew G. Knepley     /* Add local cells */
155532c4e7dSMichael Lange     adjSize = PETSC_DETERMINE;
156532c4e7dSMichael Lange     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
157532c4e7dSMichael Lange     for (a = 0; a < adjSize; ++a) {
158532c4e7dSMichael Lange       const PetscInt point = adj[a];
159532c4e7dSMichael Lange       if (point != p && pStart <= point && point < pEnd) {
160532c4e7dSMichael Lange         PetscInt *PETSC_RESTRICT pBuf;
161532c4e7dSMichael Lange         ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
162532c4e7dSMichael Lange         ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
1638f4e72b9SMatthew G. Knepley         *pBuf = cellNum[point];
164532c4e7dSMichael Lange       }
165532c4e7dSMichael Lange     }
1668cfe4c1fSMichael Lange     (*numVertices)++;
167532c4e7dSMichael Lange   }
1688f4e72b9SMatthew G. Knepley   ierr = PetscFree2(adjCells, remoteCells);CHKERRQ(ierr);
169532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, useCone);CHKERRQ(ierr);
170532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, useClosure);CHKERRQ(ierr);
171532c4e7dSMichael Lange   /* Derive CSR graph from section/segbuffer */
172532c4e7dSMichael Lange   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
173532c4e7dSMichael Lange   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
174389e55d8SMichael Lange   ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr);
17543f7d02bSMichael Lange   for (idx = 0, p = pStart; p < pEnd; p++) {
17643f7d02bSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
17743f7d02bSMichael Lange     ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr);
17843f7d02bSMichael Lange   }
179532c4e7dSMichael Lange   vOffsets[*numVertices] = size;
180532c4e7dSMichael Lange   if (offsets) *offsets = vOffsets;
181389e55d8SMichael Lange   ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr);
1828cfe4c1fSMichael Lange   ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
183f0927f4eSMatthew G. Knepley   ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr);
184389e55d8SMichael Lange   if (adjacency) *adjacency = graph;
185532c4e7dSMichael Lange   /* Clean up */
186532c4e7dSMichael Lange   ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr);
187532c4e7dSMichael Lange   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
188532c4e7dSMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
189532c4e7dSMichael Lange   PetscFunctionReturn(0);
190532c4e7dSMichael Lange }
191532c4e7dSMichael Lange 
192d5577e40SMatthew G. Knepley /*@C
193d5577e40SMatthew G. Knepley   DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format.
194d5577e40SMatthew G. Knepley 
195d5577e40SMatthew G. Knepley   Collective
196d5577e40SMatthew G. Knepley 
197d5577e40SMatthew G. Knepley   Input Arguments:
198d5577e40SMatthew G. Knepley + dm - The DMPlex
199d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0)
200d5577e40SMatthew G. Knepley 
201d5577e40SMatthew G. Knepley   Output Arguments:
202d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh.
203d5577e40SMatthew G. Knepley . offsets     - The offset to the adjacency list for each cell
204d5577e40SMatthew G. Knepley - adjacency   - The adjacency list for all cells
205d5577e40SMatthew G. Knepley 
206d5577e40SMatthew G. Knepley   Note: This is suitable for input to a mesh partitioner like ParMetis.
207d5577e40SMatthew G. Knepley 
208d5577e40SMatthew G. Knepley   Level: advanced
209d5577e40SMatthew G. Knepley 
210d5577e40SMatthew G. Knepley .seealso: DMPlexCreate()
211d5577e40SMatthew G. Knepley @*/
21270034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency)
21370034214SMatthew G. Knepley {
21470034214SMatthew G. Knepley   const PetscInt maxFaceCases = 30;
21570034214SMatthew G. Knepley   PetscInt       numFaceCases = 0;
21670034214SMatthew G. Knepley   PetscInt       numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */
21770034214SMatthew G. Knepley   PetscInt      *off, *adj;
21870034214SMatthew G. Knepley   PetscInt      *neighborCells = NULL;
21970034214SMatthew G. Knepley   PetscInt       dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell;
22070034214SMatthew G. Knepley   PetscErrorCode ierr;
22170034214SMatthew G. Knepley 
22270034214SMatthew G. Knepley   PetscFunctionBegin;
22370034214SMatthew G. Knepley   /* For parallel partitioning, I think you have to communicate supports */
224c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
22570034214SMatthew G. Knepley   cellDim = dim - cellHeight;
22670034214SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
22770034214SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
22870034214SMatthew G. Knepley   if (cEnd - cStart == 0) {
22970034214SMatthew G. Knepley     if (numVertices) *numVertices = 0;
23070034214SMatthew G. Knepley     if (offsets)   *offsets   = NULL;
23170034214SMatthew G. Knepley     if (adjacency) *adjacency = NULL;
23270034214SMatthew G. Knepley     PetscFunctionReturn(0);
23370034214SMatthew G. Knepley   }
23470034214SMatthew G. Knepley   numCells  = cEnd - cStart;
23570034214SMatthew G. Knepley   faceDepth = depth - cellHeight;
23670034214SMatthew G. Knepley   if (dim == depth) {
23770034214SMatthew G. Knepley     PetscInt f, fStart, fEnd;
23870034214SMatthew G. Knepley 
23970034214SMatthew G. Knepley     ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
24070034214SMatthew G. Knepley     /* Count neighboring cells */
24170034214SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
24270034214SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
24370034214SMatthew G. Knepley       const PetscInt *support;
24470034214SMatthew G. Knepley       PetscInt        supportSize;
24570034214SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
24670034214SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
24770034214SMatthew G. Knepley       if (supportSize == 2) {
2489ffc88e4SToby Isaac         PetscInt numChildren;
2499ffc88e4SToby Isaac 
2509ffc88e4SToby Isaac         ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2519ffc88e4SToby Isaac         if (!numChildren) {
25270034214SMatthew G. Knepley           ++off[support[0]-cStart+1];
25370034214SMatthew G. Knepley           ++off[support[1]-cStart+1];
25470034214SMatthew G. Knepley         }
25570034214SMatthew G. Knepley       }
2569ffc88e4SToby Isaac     }
25770034214SMatthew G. Knepley     /* Prefix sum */
25870034214SMatthew G. Knepley     for (c = 1; c <= numCells; ++c) off[c] += off[c-1];
25970034214SMatthew G. Knepley     if (adjacency) {
26070034214SMatthew G. Knepley       PetscInt *tmp;
26170034214SMatthew G. Knepley 
26270034214SMatthew G. Knepley       ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
263854ce69bSBarry Smith       ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr);
26470034214SMatthew G. Knepley       ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr);
26570034214SMatthew G. Knepley       /* Get neighboring cells */
26670034214SMatthew G. Knepley       for (f = fStart; f < fEnd; ++f) {
26770034214SMatthew G. Knepley         const PetscInt *support;
26870034214SMatthew G. Knepley         PetscInt        supportSize;
26970034214SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
27070034214SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
27170034214SMatthew G. Knepley         if (supportSize == 2) {
2729ffc88e4SToby Isaac           PetscInt numChildren;
2739ffc88e4SToby Isaac 
2749ffc88e4SToby Isaac           ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2759ffc88e4SToby Isaac           if (!numChildren) {
27670034214SMatthew G. Knepley             adj[tmp[support[0]-cStart]++] = support[1];
27770034214SMatthew G. Knepley             adj[tmp[support[1]-cStart]++] = support[0];
27870034214SMatthew G. Knepley           }
27970034214SMatthew G. Knepley         }
2809ffc88e4SToby Isaac       }
28170034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
28270034214SMatthew 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);
28370034214SMatthew G. Knepley #endif
28470034214SMatthew G. Knepley       ierr = PetscFree(tmp);CHKERRQ(ierr);
28570034214SMatthew G. Knepley     }
28670034214SMatthew G. Knepley     if (numVertices) *numVertices = numCells;
28770034214SMatthew G. Knepley     if (offsets)   *offsets   = off;
28870034214SMatthew G. Knepley     if (adjacency) *adjacency = adj;
28970034214SMatthew G. Knepley     PetscFunctionReturn(0);
29070034214SMatthew G. Knepley   }
29170034214SMatthew G. Knepley   /* Setup face recognition */
29270034214SMatthew G. Knepley   if (faceDepth == 1) {
29370034214SMatthew 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 */
29470034214SMatthew G. Knepley 
29570034214SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
29670034214SMatthew G. Knepley       PetscInt corners;
29770034214SMatthew G. Knepley 
29870034214SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr);
29970034214SMatthew G. Knepley       if (!cornersSeen[corners]) {
30070034214SMatthew G. Knepley         PetscInt nFV;
30170034214SMatthew G. Knepley 
30270034214SMatthew G. Knepley         if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases");
30370034214SMatthew G. Knepley         cornersSeen[corners] = 1;
30470034214SMatthew G. Knepley 
30570034214SMatthew G. Knepley         ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr);
30670034214SMatthew G. Knepley 
30770034214SMatthew G. Knepley         numFaceVertices[numFaceCases++] = nFV;
30870034214SMatthew G. Knepley       }
30970034214SMatthew G. Knepley     }
31070034214SMatthew G. Knepley   }
31170034214SMatthew G. Knepley   ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
31270034214SMatthew G. Knepley   /* Count neighboring cells */
31370034214SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
31470034214SMatthew G. Knepley     PetscInt numNeighbors = PETSC_DETERMINE, n;
31570034214SMatthew G. Knepley 
3168b0b4c70SToby Isaac     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
31770034214SMatthew G. Knepley     /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
31870034214SMatthew G. Knepley     for (n = 0; n < numNeighbors; ++n) {
31970034214SMatthew G. Knepley       PetscInt        cellPair[2];
32070034214SMatthew G. Knepley       PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
32170034214SMatthew G. Knepley       PetscInt        meetSize = 0;
32270034214SMatthew G. Knepley       const PetscInt *meet    = NULL;
32370034214SMatthew G. Knepley 
32470034214SMatthew G. Knepley       cellPair[0] = cell; cellPair[1] = neighborCells[n];
32570034214SMatthew G. Knepley       if (cellPair[0] == cellPair[1]) continue;
32670034214SMatthew G. Knepley       if (!found) {
32770034214SMatthew G. Knepley         ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
32870034214SMatthew G. Knepley         if (meetSize) {
32970034214SMatthew G. Knepley           PetscInt f;
33070034214SMatthew G. Knepley 
33170034214SMatthew G. Knepley           for (f = 0; f < numFaceCases; ++f) {
33270034214SMatthew G. Knepley             if (numFaceVertices[f] == meetSize) {
33370034214SMatthew G. Knepley               found = PETSC_TRUE;
33470034214SMatthew G. Knepley               break;
33570034214SMatthew G. Knepley             }
33670034214SMatthew G. Knepley           }
33770034214SMatthew G. Knepley         }
33870034214SMatthew G. Knepley         ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
33970034214SMatthew G. Knepley       }
34070034214SMatthew G. Knepley       if (found) ++off[cell-cStart+1];
34170034214SMatthew G. Knepley     }
34270034214SMatthew G. Knepley   }
34370034214SMatthew G. Knepley   /* Prefix sum */
34470034214SMatthew G. Knepley   for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1];
34570034214SMatthew G. Knepley 
34670034214SMatthew G. Knepley   if (adjacency) {
34770034214SMatthew G. Knepley     ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
34870034214SMatthew G. Knepley     /* Get neighboring cells */
34970034214SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
35070034214SMatthew G. Knepley       PetscInt numNeighbors = PETSC_DETERMINE, n;
35170034214SMatthew G. Knepley       PetscInt cellOffset   = 0;
35270034214SMatthew G. Knepley 
3538b0b4c70SToby Isaac       ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
35470034214SMatthew G. Knepley       /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
35570034214SMatthew G. Knepley       for (n = 0; n < numNeighbors; ++n) {
35670034214SMatthew G. Knepley         PetscInt        cellPair[2];
35770034214SMatthew G. Knepley         PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
35870034214SMatthew G. Knepley         PetscInt        meetSize = 0;
35970034214SMatthew G. Knepley         const PetscInt *meet    = NULL;
36070034214SMatthew G. Knepley 
36170034214SMatthew G. Knepley         cellPair[0] = cell; cellPair[1] = neighborCells[n];
36270034214SMatthew G. Knepley         if (cellPair[0] == cellPair[1]) continue;
36370034214SMatthew G. Knepley         if (!found) {
36470034214SMatthew G. Knepley           ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
36570034214SMatthew G. Knepley           if (meetSize) {
36670034214SMatthew G. Knepley             PetscInt f;
36770034214SMatthew G. Knepley 
36870034214SMatthew G. Knepley             for (f = 0; f < numFaceCases; ++f) {
36970034214SMatthew G. Knepley               if (numFaceVertices[f] == meetSize) {
37070034214SMatthew G. Knepley                 found = PETSC_TRUE;
37170034214SMatthew G. Knepley                 break;
37270034214SMatthew G. Knepley               }
37370034214SMatthew G. Knepley             }
37470034214SMatthew G. Knepley           }
37570034214SMatthew G. Knepley           ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
37670034214SMatthew G. Knepley         }
37770034214SMatthew G. Knepley         if (found) {
37870034214SMatthew G. Knepley           adj[off[cell-cStart]+cellOffset] = neighborCells[n];
37970034214SMatthew G. Knepley           ++cellOffset;
38070034214SMatthew G. Knepley         }
38170034214SMatthew G. Knepley       }
38270034214SMatthew G. Knepley     }
38370034214SMatthew G. Knepley   }
38470034214SMatthew G. Knepley   ierr = PetscFree(neighborCells);CHKERRQ(ierr);
38570034214SMatthew G. Knepley   if (numVertices) *numVertices = numCells;
38670034214SMatthew G. Knepley   if (offsets)   *offsets   = off;
38770034214SMatthew G. Knepley   if (adjacency) *adjacency = adj;
38870034214SMatthew G. Knepley   PetscFunctionReturn(0);
38970034214SMatthew G. Knepley }
39070034214SMatthew G. Knepley 
39177623264SMatthew G. Knepley /*@C
39277623264SMatthew G. Knepley   PetscPartitionerRegister - Adds a new PetscPartitioner implementation
39377623264SMatthew G. Knepley 
39477623264SMatthew G. Knepley   Not Collective
39577623264SMatthew G. Knepley 
39677623264SMatthew G. Knepley   Input Parameters:
39777623264SMatthew G. Knepley + name        - The name of a new user-defined creation routine
39877623264SMatthew G. Knepley - create_func - The creation routine itself
39977623264SMatthew G. Knepley 
40077623264SMatthew G. Knepley   Notes:
40177623264SMatthew G. Knepley   PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners
40277623264SMatthew G. Knepley 
40377623264SMatthew G. Knepley   Sample usage:
40477623264SMatthew G. Knepley .vb
40577623264SMatthew G. Knepley     PetscPartitionerRegister("my_part", MyPetscPartitionerCreate);
40677623264SMatthew G. Knepley .ve
40777623264SMatthew G. Knepley 
40877623264SMatthew G. Knepley   Then, your PetscPartitioner type can be chosen with the procedural interface via
40977623264SMatthew G. Knepley .vb
41077623264SMatthew G. Knepley     PetscPartitionerCreate(MPI_Comm, PetscPartitioner *);
41177623264SMatthew G. Knepley     PetscPartitionerSetType(PetscPartitioner, "my_part");
41277623264SMatthew G. Knepley .ve
41377623264SMatthew G. Knepley    or at runtime via the option
41477623264SMatthew G. Knepley .vb
41577623264SMatthew G. Knepley     -petscpartitioner_type my_part
41677623264SMatthew G. Knepley .ve
41777623264SMatthew G. Knepley 
41877623264SMatthew G. Knepley   Level: advanced
41977623264SMatthew G. Knepley 
42077623264SMatthew G. Knepley .keywords: PetscPartitioner, register
42177623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy()
42277623264SMatthew G. Knepley 
42377623264SMatthew G. Knepley @*/
42477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner))
42577623264SMatthew G. Knepley {
42677623264SMatthew G. Knepley   PetscErrorCode ierr;
42777623264SMatthew G. Knepley 
42877623264SMatthew G. Knepley   PetscFunctionBegin;
42977623264SMatthew G. Knepley   ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr);
43077623264SMatthew G. Knepley   PetscFunctionReturn(0);
43177623264SMatthew G. Knepley }
43277623264SMatthew G. Knepley 
43377623264SMatthew G. Knepley /*@C
43477623264SMatthew G. Knepley   PetscPartitionerSetType - Builds a particular PetscPartitioner
43577623264SMatthew G. Knepley 
43677623264SMatthew G. Knepley   Collective on PetscPartitioner
43777623264SMatthew G. Knepley 
43877623264SMatthew G. Knepley   Input Parameters:
43977623264SMatthew G. Knepley + part - The PetscPartitioner object
44077623264SMatthew G. Knepley - name - The kind of partitioner
44177623264SMatthew G. Knepley 
44277623264SMatthew G. Knepley   Options Database Key:
44377623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types
44477623264SMatthew G. Knepley 
44577623264SMatthew G. Knepley   Level: intermediate
44677623264SMatthew G. Knepley 
44777623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type
44877623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate()
44977623264SMatthew G. Knepley @*/
45077623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name)
45177623264SMatthew G. Knepley {
45277623264SMatthew G. Knepley   PetscErrorCode (*r)(PetscPartitioner);
45377623264SMatthew G. Knepley   PetscBool      match;
45477623264SMatthew G. Knepley   PetscErrorCode ierr;
45577623264SMatthew G. Knepley 
45677623264SMatthew G. Knepley   PetscFunctionBegin;
45777623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
45877623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr);
45977623264SMatthew G. Knepley   if (match) PetscFunctionReturn(0);
46077623264SMatthew G. Knepley 
4610f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
46277623264SMatthew G. Knepley   ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr);
46377623264SMatthew G. Knepley   if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name);
46477623264SMatthew G. Knepley 
46577623264SMatthew G. Knepley   if (part->ops->destroy) {
46677623264SMatthew G. Knepley     ierr              = (*part->ops->destroy)(part);CHKERRQ(ierr);
46777623264SMatthew G. Knepley   }
46809161815SVaclav Hapla   ierr = PetscMemzero(part->ops, sizeof(struct _PetscPartitionerOps));CHKERRQ(ierr);
46977623264SMatthew G. Knepley   ierr = (*r)(part);CHKERRQ(ierr);
47077623264SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr);
47177623264SMatthew G. Knepley   PetscFunctionReturn(0);
47277623264SMatthew G. Knepley }
47377623264SMatthew G. Knepley 
47477623264SMatthew G. Knepley /*@C
47577623264SMatthew G. Knepley   PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object.
47677623264SMatthew G. Knepley 
47777623264SMatthew G. Knepley   Not Collective
47877623264SMatthew G. Knepley 
47977623264SMatthew G. Knepley   Input Parameter:
48077623264SMatthew G. Knepley . part - The PetscPartitioner
48177623264SMatthew G. Knepley 
48277623264SMatthew G. Knepley   Output Parameter:
48377623264SMatthew G. Knepley . name - The PetscPartitioner type name
48477623264SMatthew G. Knepley 
48577623264SMatthew G. Knepley   Level: intermediate
48677623264SMatthew G. Knepley 
48777623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name
48877623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate()
48977623264SMatthew G. Knepley @*/
49077623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name)
49177623264SMatthew G. Knepley {
49277623264SMatthew G. Knepley   PetscErrorCode ierr;
49377623264SMatthew G. Knepley 
49477623264SMatthew G. Knepley   PetscFunctionBegin;
49577623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
49677623264SMatthew G. Knepley   PetscValidPointer(name, 2);
4970f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
49877623264SMatthew G. Knepley   *name = ((PetscObject) part)->type_name;
49977623264SMatthew G. Knepley   PetscFunctionReturn(0);
50077623264SMatthew G. Knepley }
50177623264SMatthew G. Knepley 
50277623264SMatthew G. Knepley /*@C
50377623264SMatthew G. Knepley   PetscPartitionerView - Views a PetscPartitioner
50477623264SMatthew G. Knepley 
50577623264SMatthew G. Knepley   Collective on PetscPartitioner
50677623264SMatthew G. Knepley 
50777623264SMatthew G. Knepley   Input Parameter:
50877623264SMatthew G. Knepley + part - the PetscPartitioner object to view
50977623264SMatthew G. Knepley - v    - the viewer
51077623264SMatthew G. Knepley 
51177623264SMatthew G. Knepley   Level: developer
51277623264SMatthew G. Knepley 
51377623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy()
51477623264SMatthew G. Knepley @*/
51577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v)
51677623264SMatthew G. Knepley {
517ffc59708SMatthew G. Knepley   PetscMPIInt    size;
5182abdaa70SMatthew G. Knepley   PetscBool      isascii;
51977623264SMatthew G. Knepley   PetscErrorCode ierr;
52077623264SMatthew G. Knepley 
52177623264SMatthew G. Knepley   PetscFunctionBegin;
52277623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
52377623264SMatthew G. Knepley   if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);}
5242abdaa70SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
5252abdaa70SMatthew G. Knepley   if (isascii) {
5262abdaa70SMatthew G. Knepley     ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
527ffc59708SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "Graph Partitioner: %d MPI Process%s\n", size, size > 1 ? "es" : "");CHKERRQ(ierr);
5282abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "  type: %s\n", part->hdr.type_name);CHKERRQ(ierr);
5292abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
5302abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "edge cut: %D\n", part->edgeCut);CHKERRQ(ierr);
5312abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "balance:  %.2g\n", part->balance);CHKERRQ(ierr);
5322abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
5332abdaa70SMatthew G. Knepley   }
53477623264SMatthew G. Knepley   if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);}
53577623264SMatthew G. Knepley   PetscFunctionReturn(0);
53677623264SMatthew G. Knepley }
53777623264SMatthew G. Knepley 
538a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType)
539a0058e54SToby Isaac {
540a0058e54SToby Isaac   PetscFunctionBegin;
541a0058e54SToby Isaac   if (!currentType) {
542a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO)
543a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERCHACO;
544a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS)
545a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERPARMETIS;
546137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH)
547137cd93aSLisandro Dalcin     *defaultType = PETSCPARTITIONERPTSCOTCH;
548a0058e54SToby Isaac #else
549a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERSIMPLE;
550a0058e54SToby Isaac #endif
551a0058e54SToby Isaac   } else {
552a0058e54SToby Isaac     *defaultType = currentType;
553a0058e54SToby Isaac   }
554a0058e54SToby Isaac   PetscFunctionReturn(0);
555a0058e54SToby Isaac }
556a0058e54SToby Isaac 
55777623264SMatthew G. Knepley /*@
55877623264SMatthew G. Knepley   PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database
55977623264SMatthew G. Knepley 
56077623264SMatthew G. Knepley   Collective on PetscPartitioner
56177623264SMatthew G. Knepley 
56277623264SMatthew G. Knepley   Input Parameter:
56377623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for
56477623264SMatthew G. Knepley 
56577623264SMatthew G. Knepley   Level: developer
56677623264SMatthew G. Knepley 
56777623264SMatthew G. Knepley .seealso: PetscPartitionerView()
56877623264SMatthew G. Knepley @*/
56977623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part)
57077623264SMatthew G. Knepley {
5716bb9daa8SLisandro Dalcin   const char    *defaultType;
5726bb9daa8SLisandro Dalcin   char           name[256];
5736bb9daa8SLisandro Dalcin   PetscBool      flg;
57477623264SMatthew G. Knepley   PetscErrorCode ierr;
57577623264SMatthew G. Knepley 
57677623264SMatthew G. Knepley   PetscFunctionBegin;
57777623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
5786bb9daa8SLisandro Dalcin   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
5796bb9daa8SLisandro Dalcin   ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr);
58077623264SMatthew G. Knepley   ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr);
5816bb9daa8SLisandro Dalcin   ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, sizeof(name), &flg);CHKERRQ(ierr);
5826bb9daa8SLisandro Dalcin   if (flg) {
5836bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr);
5846bb9daa8SLisandro Dalcin   } else if (!((PetscObject) part)->type_name) {
5856bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr);
5866bb9daa8SLisandro Dalcin   }
5876bb9daa8SLisandro Dalcin   if (part->ops->setfromoptions) {
5886bb9daa8SLisandro Dalcin     ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr);
5896bb9daa8SLisandro Dalcin   }
59077623264SMatthew G. Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5910633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr);
59277623264SMatthew G. Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
59377623264SMatthew G. Knepley   PetscFunctionReturn(0);
59477623264SMatthew G. Knepley }
59577623264SMatthew G. Knepley 
59677623264SMatthew G. Knepley /*@C
59777623264SMatthew G. Knepley   PetscPartitionerSetUp - Construct data structures for the PetscPartitioner
59877623264SMatthew G. Knepley 
59977623264SMatthew G. Knepley   Collective on PetscPartitioner
60077623264SMatthew G. Knepley 
60177623264SMatthew G. Knepley   Input Parameter:
60277623264SMatthew G. Knepley . part - the PetscPartitioner object to setup
60377623264SMatthew G. Knepley 
60477623264SMatthew G. Knepley   Level: developer
60577623264SMatthew G. Knepley 
60677623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy()
60777623264SMatthew G. Knepley @*/
60877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part)
60977623264SMatthew G. Knepley {
61077623264SMatthew G. Knepley   PetscErrorCode ierr;
61177623264SMatthew G. Knepley 
61277623264SMatthew G. Knepley   PetscFunctionBegin;
61377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
61477623264SMatthew G. Knepley   if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);}
61577623264SMatthew G. Knepley   PetscFunctionReturn(0);
61677623264SMatthew G. Knepley }
61777623264SMatthew G. Knepley 
61877623264SMatthew G. Knepley /*@
61977623264SMatthew G. Knepley   PetscPartitionerDestroy - Destroys a PetscPartitioner object
62077623264SMatthew G. Knepley 
62177623264SMatthew G. Knepley   Collective on PetscPartitioner
62277623264SMatthew G. Knepley 
62377623264SMatthew G. Knepley   Input Parameter:
62477623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy
62577623264SMatthew G. Knepley 
62677623264SMatthew G. Knepley   Level: developer
62777623264SMatthew G. Knepley 
62877623264SMatthew G. Knepley .seealso: PetscPartitionerView()
62977623264SMatthew G. Knepley @*/
63077623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part)
63177623264SMatthew G. Knepley {
63277623264SMatthew G. Knepley   PetscErrorCode ierr;
63377623264SMatthew G. Knepley 
63477623264SMatthew G. Knepley   PetscFunctionBegin;
63577623264SMatthew G. Knepley   if (!*part) PetscFunctionReturn(0);
63677623264SMatthew G. Knepley   PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1);
63777623264SMatthew G. Knepley 
63877623264SMatthew G. Knepley   if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);}
63977623264SMatthew G. Knepley   ((PetscObject) (*part))->refct = 0;
64077623264SMatthew G. Knepley 
64177623264SMatthew G. Knepley   if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);}
64277623264SMatthew G. Knepley   ierr = PetscHeaderDestroy(part);CHKERRQ(ierr);
64377623264SMatthew G. Knepley   PetscFunctionReturn(0);
64477623264SMatthew G. Knepley }
64577623264SMatthew G. Knepley 
64677623264SMatthew G. Knepley /*@
64777623264SMatthew G. Knepley   PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType().
64877623264SMatthew G. Knepley 
64977623264SMatthew G. Knepley   Collective on MPI_Comm
65077623264SMatthew G. Knepley 
65177623264SMatthew G. Knepley   Input Parameter:
65277623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object
65377623264SMatthew G. Knepley 
65477623264SMatthew G. Knepley   Output Parameter:
65577623264SMatthew G. Knepley . part - The PetscPartitioner object
65677623264SMatthew G. Knepley 
65777623264SMatthew G. Knepley   Level: beginner
65877623264SMatthew G. Knepley 
659dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER
66077623264SMatthew G. Knepley @*/
66177623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part)
66277623264SMatthew G. Knepley {
66377623264SMatthew G. Knepley   PetscPartitioner p;
664a0058e54SToby Isaac   const char       *partitionerType = NULL;
66577623264SMatthew G. Knepley   PetscErrorCode   ierr;
66677623264SMatthew G. Knepley 
66777623264SMatthew G. Knepley   PetscFunctionBegin;
66877623264SMatthew G. Knepley   PetscValidPointer(part, 2);
66977623264SMatthew G. Knepley   *part = NULL;
67083cde681SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
67177623264SMatthew G. Knepley 
67273107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr);
673a0058e54SToby Isaac   ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr);
674a0058e54SToby Isaac   ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr);
67577623264SMatthew G. Knepley 
67672379da4SMatthew G. Knepley   p->edgeCut = 0;
67772379da4SMatthew G. Knepley   p->balance = 0.0;
67872379da4SMatthew G. Knepley 
67977623264SMatthew G. Knepley   *part = p;
68077623264SMatthew G. Knepley   PetscFunctionReturn(0);
68177623264SMatthew G. Knepley }
68277623264SMatthew G. Knepley 
68377623264SMatthew G. Knepley /*@
68477623264SMatthew G. Knepley   PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh
68577623264SMatthew G. Knepley 
68677623264SMatthew G. Knepley   Collective on DM
68777623264SMatthew G. Knepley 
68877623264SMatthew G. Knepley   Input Parameters:
68977623264SMatthew G. Knepley + part    - The PetscPartitioner
690f8987ae8SMichael Lange - dm      - The mesh DM
69177623264SMatthew G. Knepley 
69277623264SMatthew G. Knepley   Output Parameters:
69377623264SMatthew G. Knepley + partSection     - The PetscSection giving the division of points by partition
694f8987ae8SMichael Lange - partition       - The list of points by partition
69577623264SMatthew G. Knepley 
69677623264SMatthew G. Knepley   Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight()
69777623264SMatthew G. Knepley 
69877623264SMatthew G. Knepley   Level: developer
69977623264SMatthew G. Knepley 
70077623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate()
7014b15ede2SMatthew G. Knepley @*/
702f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition)
70377623264SMatthew G. Knepley {
70477623264SMatthew G. Knepley   PetscMPIInt    size;
70577623264SMatthew G. Knepley   PetscErrorCode ierr;
70677623264SMatthew G. Knepley 
70777623264SMatthew G. Knepley   PetscFunctionBegin;
70877623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
70977623264SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
71077623264SMatthew G. Knepley   PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4);
71177623264SMatthew G. Knepley   PetscValidPointer(partition, 5);
71277623264SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
71377623264SMatthew G. Knepley   if (size == 1) {
71477623264SMatthew G. Knepley     PetscInt *points;
71577623264SMatthew G. Knepley     PetscInt  cStart, cEnd, c;
71677623264SMatthew G. Knepley 
71777623264SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
71877623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr);
71977623264SMatthew G. Knepley     ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr);
72077623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
72177623264SMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr);
72277623264SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) points[c] = c;
72377623264SMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
72477623264SMatthew G. Knepley     PetscFunctionReturn(0);
72577623264SMatthew G. Knepley   }
72677623264SMatthew G. Knepley   if (part->height == 0) {
72777623264SMatthew G. Knepley     PetscInt numVertices;
72877623264SMatthew G. Knepley     PetscInt *start     = NULL;
72977623264SMatthew G. Knepley     PetscInt *adjacency = NULL;
7303fa7752dSToby Isaac     IS       globalNumbering;
73177623264SMatthew G. Knepley 
7323fa7752dSToby Isaac     ierr = DMPlexCreatePartitionerGraph(dm, 0, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr);
73377623264SMatthew G. Knepley     if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type");
73477623264SMatthew G. Knepley     ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
73577623264SMatthew G. Knepley     ierr = PetscFree(start);CHKERRQ(ierr);
73677623264SMatthew G. Knepley     ierr = PetscFree(adjacency);CHKERRQ(ierr);
7373fa7752dSToby Isaac     if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */
7383fa7752dSToby Isaac       const PetscInt *globalNum;
7393fa7752dSToby Isaac       const PetscInt *partIdx;
7403fa7752dSToby Isaac       PetscInt *map, cStart, cEnd;
7413fa7752dSToby Isaac       PetscInt *adjusted, i, localSize, offset;
7423fa7752dSToby Isaac       IS    newPartition;
7433fa7752dSToby Isaac 
7443fa7752dSToby Isaac       ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr);
7453fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr);
7463fa7752dSToby Isaac       ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7473fa7752dSToby Isaac       ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr);
7483fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr);
7493fa7752dSToby Isaac       ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
7503fa7752dSToby Isaac       for (i = cStart, offset = 0; i < cEnd; i++) {
7513fa7752dSToby Isaac         if (globalNum[i - cStart] >= 0) map[offset++] = i;
7523fa7752dSToby Isaac       }
7533fa7752dSToby Isaac       for (i = 0; i < localSize; i++) {
7543fa7752dSToby Isaac         adjusted[i] = map[partIdx[i]];
7553fa7752dSToby Isaac       }
7563fa7752dSToby Isaac       ierr = PetscFree(map);CHKERRQ(ierr);
7573fa7752dSToby Isaac       ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr);
7583fa7752dSToby Isaac       ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7593fa7752dSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr);
7603fa7752dSToby Isaac       ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr);
7613fa7752dSToby Isaac       ierr = ISDestroy(partition);CHKERRQ(ierr);
7623fa7752dSToby Isaac       *partition = newPartition;
7633fa7752dSToby Isaac     }
76477623264SMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height);
7652abdaa70SMatthew G. Knepley   ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr);
76677623264SMatthew G. Knepley   PetscFunctionReturn(0);
76777623264SMatthew G. Knepley }
76877623264SMatthew G. Knepley 
769d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part)
77077623264SMatthew G. Knepley {
77177623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
77277623264SMatthew G. Knepley   PetscErrorCode          ierr;
77377623264SMatthew G. Knepley 
77477623264SMatthew G. Knepley   PetscFunctionBegin;
77577623264SMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
77677623264SMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
77777623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
77877623264SMatthew G. Knepley   PetscFunctionReturn(0);
77977623264SMatthew G. Knepley }
78077623264SMatthew G. Knepley 
781d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer)
78277623264SMatthew G. Knepley {
783077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
78477623264SMatthew G. Knepley   PetscErrorCode          ierr;
78577623264SMatthew G. Knepley 
78677623264SMatthew G. Knepley   PetscFunctionBegin;
787077101c0SMatthew G. Knepley   if (p->random) {
788077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
789077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr);
790077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
791077101c0SMatthew G. Knepley   }
79277623264SMatthew G. Knepley   PetscFunctionReturn(0);
79377623264SMatthew G. Knepley }
79477623264SMatthew G. Knepley 
795d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer)
79677623264SMatthew G. Knepley {
79777623264SMatthew G. Knepley   PetscBool      iascii;
79877623264SMatthew G. Knepley   PetscErrorCode ierr;
79977623264SMatthew G. Knepley 
80077623264SMatthew G. Knepley   PetscFunctionBegin;
80177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
80277623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
80377623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
80477623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);}
80577623264SMatthew G. Knepley   PetscFunctionReturn(0);
80677623264SMatthew G. Knepley }
80777623264SMatthew G. Knepley 
808d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
809077101c0SMatthew G. Knepley {
810077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
811077101c0SMatthew G. Knepley   PetscErrorCode          ierr;
812077101c0SMatthew G. Knepley 
813077101c0SMatthew G. Knepley   PetscFunctionBegin;
814077101c0SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");CHKERRQ(ierr);
815077101c0SMatthew G. Knepley   ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr);
816077101c0SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
817077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
818077101c0SMatthew G. Knepley }
819077101c0SMatthew G. Knepley 
820d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
82177623264SMatthew G. Knepley {
82277623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
82377623264SMatthew G. Knepley   PetscInt                np;
82477623264SMatthew G. Knepley   PetscErrorCode          ierr;
82577623264SMatthew G. Knepley 
82677623264SMatthew G. Knepley   PetscFunctionBegin;
827077101c0SMatthew G. Knepley   if (p->random) {
828077101c0SMatthew G. Knepley     PetscRandom r;
829aa1d5631SMatthew G. Knepley     PetscInt   *sizes, *points, v, p;
830aa1d5631SMatthew G. Knepley     PetscMPIInt rank;
831077101c0SMatthew G. Knepley 
832aa1d5631SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
833077101c0SMatthew G. Knepley     ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
834c717d290SMatthew G. Knepley     ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr);
835077101c0SMatthew G. Knepley     ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr);
836077101c0SMatthew G. Knepley     ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr);
837aa1d5631SMatthew G. Knepley     for (v = 0; v < numVertices; ++v) {points[v] = v;}
838ac9a96f1SMichael Lange     for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);}
839aa1d5631SMatthew G. Knepley     for (v = numVertices-1; v > 0; --v) {
840077101c0SMatthew G. Knepley       PetscReal val;
841aa1d5631SMatthew G. Knepley       PetscInt  w, tmp;
842077101c0SMatthew G. Knepley 
843aa1d5631SMatthew G. Knepley       ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr);
844077101c0SMatthew G. Knepley       ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr);
845aa1d5631SMatthew G. Knepley       w    = PetscFloorReal(val);
846aa1d5631SMatthew G. Knepley       tmp       = points[v];
847aa1d5631SMatthew G. Knepley       points[v] = points[w];
848aa1d5631SMatthew G. Knepley       points[w] = tmp;
849077101c0SMatthew G. Knepley     }
850077101c0SMatthew G. Knepley     ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
851077101c0SMatthew G. Knepley     ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr);
852077101c0SMatthew G. Knepley     ierr = PetscFree2(sizes, points);CHKERRQ(ierr);
853077101c0SMatthew G. Knepley   }
854c717d290SMatthew G. Knepley   if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()");
85577623264SMatthew G. Knepley   ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr);
85677623264SMatthew G. Knepley   if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np);
85777623264SMatthew G. Knepley   ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr);
85877623264SMatthew G. Knepley   if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np);
8595680f57bSMatthew G. Knepley   ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr);
86077623264SMatthew G. Knepley   *partition = p->partition;
86177623264SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr);
86277623264SMatthew G. Knepley   PetscFunctionReturn(0);
86377623264SMatthew G. Knepley }
86477623264SMatthew G. Knepley 
865d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part)
86677623264SMatthew G. Knepley {
86777623264SMatthew G. Knepley   PetscFunctionBegin;
86877623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_Shell;
869077101c0SMatthew G. Knepley   part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell;
87077623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_Shell;
87177623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_Shell;
87277623264SMatthew G. Knepley   PetscFunctionReturn(0);
87377623264SMatthew G. Knepley }
87477623264SMatthew G. Knepley 
87577623264SMatthew G. Knepley /*MC
87677623264SMatthew G. Knepley   PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object
87777623264SMatthew G. Knepley 
87877623264SMatthew G. Knepley   Level: intermediate
87977623264SMatthew G. Knepley 
88077623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
88177623264SMatthew G. Knepley M*/
88277623264SMatthew G. Knepley 
88377623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part)
88477623264SMatthew G. Knepley {
88577623264SMatthew G. Knepley   PetscPartitioner_Shell *p;
88677623264SMatthew G. Knepley   PetscErrorCode          ierr;
88777623264SMatthew G. Knepley 
88877623264SMatthew G. Knepley   PetscFunctionBegin;
88977623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
89077623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
89177623264SMatthew G. Knepley   part->data = p;
89277623264SMatthew G. Knepley 
89377623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr);
894077101c0SMatthew G. Knepley   p->random = PETSC_FALSE;
89577623264SMatthew G. Knepley   PetscFunctionReturn(0);
89677623264SMatthew G. Knepley }
89777623264SMatthew G. Knepley 
8985680f57bSMatthew G. Knepley /*@C
8995680f57bSMatthew G. Knepley   PetscPartitionerShellSetPartition - Set an artifical partition for a mesh
9005680f57bSMatthew G. Knepley 
901077101c0SMatthew G. Knepley   Collective on Part
9025680f57bSMatthew G. Knepley 
9035680f57bSMatthew G. Knepley   Input Parameters:
9045680f57bSMatthew G. Knepley + part     - The PetscPartitioner
9059852e123SBarry Smith . size - The number of partitions
9069852e123SBarry Smith . sizes    - array of size size (or NULL) providing the number of points in each partition
9079758bf69SToby 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.)
9085680f57bSMatthew G. Knepley 
9095680f57bSMatthew G. Knepley   Level: developer
9105680f57bSMatthew G. Knepley 
911b7e49471SLawrence Mitchell   Notes:
912b7e49471SLawrence Mitchell 
913b7e49471SLawrence Mitchell     It is safe to free the sizes and points arrays after use in this routine.
914b7e49471SLawrence Mitchell 
9155680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate()
9165680f57bSMatthew G. Knepley @*/
9179852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[])
9185680f57bSMatthew G. Knepley {
9195680f57bSMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
9205680f57bSMatthew G. Knepley   PetscInt                proc, numPoints;
9215680f57bSMatthew G. Knepley   PetscErrorCode          ierr;
9225680f57bSMatthew G. Knepley 
9235680f57bSMatthew G. Knepley   PetscFunctionBegin;
9245680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
9255680f57bSMatthew G. Knepley   if (sizes)  {PetscValidPointer(sizes, 3);}
926c717d290SMatthew G. Knepley   if (points) {PetscValidPointer(points, 4);}
9275680f57bSMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
9285680f57bSMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
9295680f57bSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr);
9309852e123SBarry Smith   ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr);
9315680f57bSMatthew G. Knepley   if (sizes) {
9329852e123SBarry Smith     for (proc = 0; proc < size; ++proc) {
9335680f57bSMatthew G. Knepley       ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr);
9345680f57bSMatthew G. Knepley     }
9355680f57bSMatthew G. Knepley   }
9365680f57bSMatthew G. Knepley   ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr);
9375680f57bSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr);
9385680f57bSMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr);
9395680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
9405680f57bSMatthew G. Knepley }
9415680f57bSMatthew G. Knepley 
942077101c0SMatthew G. Knepley /*@
943077101c0SMatthew G. Knepley   PetscPartitionerShellSetRandom - Set the flag to use a random partition
944077101c0SMatthew G. Knepley 
945077101c0SMatthew G. Knepley   Collective on Part
946077101c0SMatthew G. Knepley 
947077101c0SMatthew G. Knepley   Input Parameters:
948077101c0SMatthew G. Knepley + part   - The PetscPartitioner
949077101c0SMatthew G. Knepley - random - The flag to use a random partition
950077101c0SMatthew G. Knepley 
951077101c0SMatthew G. Knepley   Level: intermediate
952077101c0SMatthew G. Knepley 
953077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate()
954077101c0SMatthew G. Knepley @*/
955077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random)
956077101c0SMatthew G. Knepley {
957077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
958077101c0SMatthew G. Knepley 
959077101c0SMatthew G. Knepley   PetscFunctionBegin;
960077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
961077101c0SMatthew G. Knepley   p->random = random;
962077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
963077101c0SMatthew G. Knepley }
964077101c0SMatthew G. Knepley 
965077101c0SMatthew G. Knepley /*@
966077101c0SMatthew G. Knepley   PetscPartitionerShellGetRandom - get the flag to use a random partition
967077101c0SMatthew G. Knepley 
968077101c0SMatthew G. Knepley   Collective on Part
969077101c0SMatthew G. Knepley 
970077101c0SMatthew G. Knepley   Input Parameter:
971077101c0SMatthew G. Knepley . part   - The PetscPartitioner
972077101c0SMatthew G. Knepley 
973077101c0SMatthew G. Knepley   Output Parameter
974077101c0SMatthew G. Knepley . random - The flag to use a random partition
975077101c0SMatthew G. Knepley 
976077101c0SMatthew G. Knepley   Level: intermediate
977077101c0SMatthew G. Knepley 
978077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate()
979077101c0SMatthew G. Knepley @*/
980077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random)
981077101c0SMatthew G. Knepley {
982077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
983077101c0SMatthew G. Knepley 
984077101c0SMatthew G. Knepley   PetscFunctionBegin;
985077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
986077101c0SMatthew G. Knepley   PetscValidPointer(random, 2);
987077101c0SMatthew G. Knepley   *random = p->random;
988077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
989077101c0SMatthew G. Knepley }
990077101c0SMatthew G. Knepley 
991d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part)
992555a9cf8SMatthew G. Knepley {
993555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data;
994555a9cf8SMatthew G. Knepley   PetscErrorCode          ierr;
995555a9cf8SMatthew G. Knepley 
996555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
997555a9cf8SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
998555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
999555a9cf8SMatthew G. Knepley }
1000555a9cf8SMatthew G. Knepley 
1001d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer)
1002555a9cf8SMatthew G. Knepley {
1003555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1004555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1005555a9cf8SMatthew G. Knepley }
1006555a9cf8SMatthew G. Knepley 
1007d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer)
1008555a9cf8SMatthew G. Knepley {
1009555a9cf8SMatthew G. Knepley   PetscBool      iascii;
1010555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
1011555a9cf8SMatthew G. Knepley 
1012555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1013555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1014555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1015555a9cf8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1016555a9cf8SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);}
1017555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1018555a9cf8SMatthew G. Knepley }
1019555a9cf8SMatthew G. Knepley 
1020d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1021555a9cf8SMatthew G. Knepley {
1022cead94edSToby Isaac   MPI_Comm       comm;
1023555a9cf8SMatthew G. Knepley   PetscInt       np;
1024cead94edSToby Isaac   PetscMPIInt    size;
1025555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
1026555a9cf8SMatthew G. Knepley 
1027555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1028cead94edSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
1029cead94edSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1030555a9cf8SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1031555a9cf8SMatthew G. Knepley   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1032cead94edSToby Isaac   if (size == 1) {
1033cead94edSToby Isaac     for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);}
1034cead94edSToby Isaac   }
1035cead94edSToby Isaac   else {
1036cead94edSToby Isaac     PetscMPIInt rank;
1037cead94edSToby Isaac     PetscInt nvGlobal, *offsets, myFirst, myLast;
1038cead94edSToby Isaac 
1039a679a563SToby Isaac     ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr);
1040cead94edSToby Isaac     offsets[0] = 0;
1041cead94edSToby Isaac     ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr);
1042cead94edSToby Isaac     for (np = 2; np <= size; np++) {
1043cead94edSToby Isaac       offsets[np] += offsets[np-1];
1044cead94edSToby Isaac     }
1045cead94edSToby Isaac     nvGlobal = offsets[size];
1046cead94edSToby Isaac     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1047cead94edSToby Isaac     myFirst = offsets[rank];
1048cead94edSToby Isaac     myLast  = offsets[rank + 1] - 1;
1049cead94edSToby Isaac     ierr = PetscFree(offsets);CHKERRQ(ierr);
1050cead94edSToby Isaac     if (numVertices) {
1051cead94edSToby Isaac       PetscInt firstPart = 0, firstLargePart = 0;
1052cead94edSToby Isaac       PetscInt lastPart = 0, lastLargePart = 0;
1053cead94edSToby Isaac       PetscInt rem = nvGlobal % nparts;
1054cead94edSToby Isaac       PetscInt pSmall = nvGlobal/nparts;
1055cead94edSToby Isaac       PetscInt pBig = nvGlobal/nparts + 1;
1056cead94edSToby Isaac 
1057cead94edSToby Isaac 
1058cead94edSToby Isaac       if (rem) {
1059cead94edSToby Isaac         firstLargePart = myFirst / pBig;
1060cead94edSToby Isaac         lastLargePart  = myLast  / pBig;
1061cead94edSToby Isaac 
1062cead94edSToby Isaac         if (firstLargePart < rem) {
1063cead94edSToby Isaac           firstPart = firstLargePart;
1064cead94edSToby Isaac         }
1065cead94edSToby Isaac         else {
1066cead94edSToby Isaac           firstPart = rem + (myFirst - (rem * pBig)) / pSmall;
1067cead94edSToby Isaac         }
1068cead94edSToby Isaac         if (lastLargePart < rem) {
1069cead94edSToby Isaac           lastPart = lastLargePart;
1070cead94edSToby Isaac         }
1071cead94edSToby Isaac         else {
1072cead94edSToby Isaac           lastPart = rem + (myLast - (rem * pBig)) / pSmall;
1073cead94edSToby Isaac         }
1074cead94edSToby Isaac       }
1075cead94edSToby Isaac       else {
1076cead94edSToby Isaac         firstPart = myFirst / (nvGlobal/nparts);
1077cead94edSToby Isaac         lastPart  = myLast  / (nvGlobal/nparts);
1078cead94edSToby Isaac       }
1079cead94edSToby Isaac 
1080cead94edSToby Isaac       for (np = firstPart; np <= lastPart; np++) {
1081cead94edSToby Isaac         PetscInt PartStart =  np    * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np);
1082cead94edSToby Isaac         PetscInt PartEnd   = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1);
1083cead94edSToby Isaac 
1084cead94edSToby Isaac         PartStart = PetscMax(PartStart,myFirst);
1085cead94edSToby Isaac         PartEnd   = PetscMin(PartEnd,myLast+1);
1086cead94edSToby Isaac         ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr);
1087cead94edSToby Isaac       }
1088cead94edSToby Isaac     }
1089cead94edSToby Isaac   }
1090cead94edSToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1091555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1092555a9cf8SMatthew G. Knepley }
1093555a9cf8SMatthew G. Knepley 
1094d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part)
1095555a9cf8SMatthew G. Knepley {
1096555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1097555a9cf8SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Simple;
1098555a9cf8SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Simple;
1099555a9cf8SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Simple;
1100555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1101555a9cf8SMatthew G. Knepley }
1102555a9cf8SMatthew G. Knepley 
1103555a9cf8SMatthew G. Knepley /*MC
1104555a9cf8SMatthew G. Knepley   PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object
1105555a9cf8SMatthew G. Knepley 
1106555a9cf8SMatthew G. Knepley   Level: intermediate
1107555a9cf8SMatthew G. Knepley 
1108555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1109555a9cf8SMatthew G. Knepley M*/
1110555a9cf8SMatthew G. Knepley 
1111555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part)
1112555a9cf8SMatthew G. Knepley {
1113555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p;
1114555a9cf8SMatthew G. Knepley   PetscErrorCode           ierr;
1115555a9cf8SMatthew G. Knepley 
1116555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1117555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1118555a9cf8SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1119555a9cf8SMatthew G. Knepley   part->data = p;
1120555a9cf8SMatthew G. Knepley 
1121555a9cf8SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr);
1122555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1123555a9cf8SMatthew G. Knepley }
1124555a9cf8SMatthew G. Knepley 
1125d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part)
1126dae52e14SToby Isaac {
1127dae52e14SToby Isaac   PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data;
1128dae52e14SToby Isaac   PetscErrorCode          ierr;
1129dae52e14SToby Isaac 
1130dae52e14SToby Isaac   PetscFunctionBegin;
1131dae52e14SToby Isaac   ierr = PetscFree(p);CHKERRQ(ierr);
1132dae52e14SToby Isaac   PetscFunctionReturn(0);
1133dae52e14SToby Isaac }
1134dae52e14SToby Isaac 
1135d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer)
1136dae52e14SToby Isaac {
1137dae52e14SToby Isaac   PetscFunctionBegin;
1138dae52e14SToby Isaac   PetscFunctionReturn(0);
1139dae52e14SToby Isaac }
1140dae52e14SToby Isaac 
1141d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer)
1142dae52e14SToby Isaac {
1143dae52e14SToby Isaac   PetscBool      iascii;
1144dae52e14SToby Isaac   PetscErrorCode ierr;
1145dae52e14SToby Isaac 
1146dae52e14SToby Isaac   PetscFunctionBegin;
1147dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1148dae52e14SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1149dae52e14SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1150dae52e14SToby Isaac   if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);}
1151dae52e14SToby Isaac   PetscFunctionReturn(0);
1152dae52e14SToby Isaac }
1153dae52e14SToby Isaac 
1154d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1155dae52e14SToby Isaac {
1156dae52e14SToby Isaac   PetscInt       np;
1157dae52e14SToby Isaac   PetscErrorCode ierr;
1158dae52e14SToby Isaac 
1159dae52e14SToby Isaac   PetscFunctionBegin;
1160dae52e14SToby Isaac   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1161dae52e14SToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1162dae52e14SToby Isaac   ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr);
1163dae52e14SToby Isaac   for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);}
1164dae52e14SToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1165dae52e14SToby Isaac   PetscFunctionReturn(0);
1166dae52e14SToby Isaac }
1167dae52e14SToby Isaac 
1168d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part)
1169dae52e14SToby Isaac {
1170dae52e14SToby Isaac   PetscFunctionBegin;
1171dae52e14SToby Isaac   part->ops->view      = PetscPartitionerView_Gather;
1172dae52e14SToby Isaac   part->ops->destroy   = PetscPartitionerDestroy_Gather;
1173dae52e14SToby Isaac   part->ops->partition = PetscPartitionerPartition_Gather;
1174dae52e14SToby Isaac   PetscFunctionReturn(0);
1175dae52e14SToby Isaac }
1176dae52e14SToby Isaac 
1177dae52e14SToby Isaac /*MC
1178dae52e14SToby Isaac   PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object
1179dae52e14SToby Isaac 
1180dae52e14SToby Isaac   Level: intermediate
1181dae52e14SToby Isaac 
1182dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1183dae52e14SToby Isaac M*/
1184dae52e14SToby Isaac 
1185dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part)
1186dae52e14SToby Isaac {
1187dae52e14SToby Isaac   PetscPartitioner_Gather *p;
1188dae52e14SToby Isaac   PetscErrorCode           ierr;
1189dae52e14SToby Isaac 
1190dae52e14SToby Isaac   PetscFunctionBegin;
1191dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1192dae52e14SToby Isaac   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1193dae52e14SToby Isaac   part->data = p;
1194dae52e14SToby Isaac 
1195dae52e14SToby Isaac   ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr);
1196dae52e14SToby Isaac   PetscFunctionReturn(0);
1197dae52e14SToby Isaac }
1198dae52e14SToby Isaac 
1199dae52e14SToby Isaac 
1200d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part)
120177623264SMatthew G. Knepley {
120277623264SMatthew G. Knepley   PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data;
120377623264SMatthew G. Knepley   PetscErrorCode          ierr;
120477623264SMatthew G. Knepley 
120577623264SMatthew G. Knepley   PetscFunctionBegin;
120677623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
120777623264SMatthew G. Knepley   PetscFunctionReturn(0);
120877623264SMatthew G. Knepley }
120977623264SMatthew G. Knepley 
1210d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer)
121177623264SMatthew G. Knepley {
121277623264SMatthew G. Knepley   PetscFunctionBegin;
121377623264SMatthew G. Knepley   PetscFunctionReturn(0);
121477623264SMatthew G. Knepley }
121577623264SMatthew G. Knepley 
1216d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer)
121777623264SMatthew G. Knepley {
121877623264SMatthew G. Knepley   PetscBool      iascii;
121977623264SMatthew G. Knepley   PetscErrorCode ierr;
122077623264SMatthew G. Knepley 
122177623264SMatthew G. Knepley   PetscFunctionBegin;
122277623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
122377623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
122477623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
122577623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);}
122677623264SMatthew G. Knepley   PetscFunctionReturn(0);
122777623264SMatthew G. Knepley }
122877623264SMatthew G. Knepley 
122970034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
123070034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
123170034214SMatthew G. Knepley #include <unistd.h>
123270034214SMatthew G. Knepley #endif
123311d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
123411d1e910SBarry Smith #include <chaco.h>
123511d1e910SBarry Smith #else
123611d1e910SBarry Smith /* Older versions of Chaco do not have an include file */
123770034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
123870034214SMatthew G. Knepley                        float *ewgts, float *x, float *y, float *z, char *outassignname,
123970034214SMatthew G. Knepley                        char *outfilename, short *assignment, int architecture, int ndims_tot,
124070034214SMatthew G. Knepley                        int mesh_dims[3], double *goal, int global_method, int local_method,
124170034214SMatthew G. Knepley                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
124211d1e910SBarry Smith #endif
124370034214SMatthew G. Knepley extern int FREE_GRAPH;
124477623264SMatthew G. Knepley #endif
124570034214SMatthew G. Knepley 
1246d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
124770034214SMatthew G. Knepley {
124877623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
124970034214SMatthew G. Knepley   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
125070034214SMatthew G. Knepley   MPI_Comm       comm;
125170034214SMatthew G. Knepley   int            nvtxs          = numVertices; /* number of vertices in full graph */
125270034214SMatthew G. Knepley   int           *vwgts          = NULL;   /* weights for all vertices */
125370034214SMatthew G. Knepley   float         *ewgts          = NULL;   /* weights for all edges */
125470034214SMatthew G. Knepley   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
125570034214SMatthew G. Knepley   char          *outassignname  = NULL;   /*  name of assignment output file */
125670034214SMatthew G. Knepley   char          *outfilename    = NULL;   /* output file name */
125770034214SMatthew G. Knepley   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
125870034214SMatthew G. Knepley   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
125970034214SMatthew G. Knepley   int            mesh_dims[3];            /* dimensions of mesh of processors */
126070034214SMatthew G. Knepley   double        *goal          = NULL;    /* desired set sizes for each set */
126170034214SMatthew G. Knepley   int            global_method = 1;       /* global partitioning algorithm */
126270034214SMatthew G. Knepley   int            local_method  = 1;       /* local partitioning algorithm */
126370034214SMatthew G. Knepley   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
126470034214SMatthew G. Knepley   int            vmax          = 200;     /* how many vertices to coarsen down to? */
126570034214SMatthew G. Knepley   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
126670034214SMatthew G. Knepley   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
126770034214SMatthew G. Knepley   long           seed          = 123636512; /* for random graph mutations */
126811d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
126911d1e910SBarry Smith   int           *assignment;              /* Output partition */
127011d1e910SBarry Smith #else
127170034214SMatthew G. Knepley   short int     *assignment;              /* Output partition */
127211d1e910SBarry Smith #endif
127370034214SMatthew G. Knepley   int            fd_stdout, fd_pipe[2];
127470034214SMatthew G. Knepley   PetscInt      *points;
127570034214SMatthew G. Knepley   int            i, v, p;
127670034214SMatthew G. Knepley   PetscErrorCode ierr;
127770034214SMatthew G. Knepley 
127870034214SMatthew G. Knepley   PetscFunctionBegin;
127970034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
128007ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG)
128107ed3857SLisandro Dalcin   {
128207ed3857SLisandro Dalcin     int ival,isum;
128307ed3857SLisandro Dalcin     PetscBool distributed;
128407ed3857SLisandro Dalcin 
128507ed3857SLisandro Dalcin     ival = (numVertices > 0);
128607ed3857SLisandro Dalcin     ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr);
128707ed3857SLisandro Dalcin     distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE;
128807ed3857SLisandro Dalcin     if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph");
128907ed3857SLisandro Dalcin   }
129007ed3857SLisandro Dalcin #endif
129170034214SMatthew G. Knepley   if (!numVertices) {
129277623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
129377623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
129470034214SMatthew G. Knepley     ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
129570034214SMatthew G. Knepley     PetscFunctionReturn(0);
129670034214SMatthew G. Knepley   }
129770034214SMatthew G. Knepley   FREE_GRAPH = 0;                         /* Do not let Chaco free my memory */
129870034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
129970034214SMatthew G. Knepley 
130070034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
130170034214SMatthew G. Knepley     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
130270034214SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
130370034214SMatthew G. Knepley   }
130477623264SMatthew G. Knepley   mesh_dims[0] = nparts;
130570034214SMatthew G. Knepley   mesh_dims[1] = 1;
130670034214SMatthew G. Knepley   mesh_dims[2] = 1;
130770034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr);
130870034214SMatthew G. Knepley   /* Chaco outputs to stdout. We redirect this to a buffer. */
130970034214SMatthew G. Knepley   /* TODO: check error codes for UNIX calls */
131070034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
131170034214SMatthew G. Knepley   {
131270034214SMatthew G. Knepley     int piperet;
131370034214SMatthew G. Knepley     piperet = pipe(fd_pipe);
131470034214SMatthew G. Knepley     if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe");
131570034214SMatthew G. Knepley     fd_stdout = dup(1);
131670034214SMatthew G. Knepley     close(1);
131770034214SMatthew G. Knepley     dup2(fd_pipe[1], 1);
131870034214SMatthew G. Knepley   }
131970034214SMatthew G. Knepley #endif
132070034214SMatthew G. Knepley   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
132170034214SMatthew G. Knepley                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
132270034214SMatthew G. Knepley                    vmax, ndims, eigtol, seed);
132370034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
132470034214SMatthew G. Knepley   {
132570034214SMatthew G. Knepley     char msgLog[10000];
132670034214SMatthew G. Knepley     int  count;
132770034214SMatthew G. Knepley 
132870034214SMatthew G. Knepley     fflush(stdout);
132970034214SMatthew G. Knepley     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
133070034214SMatthew G. Knepley     if (count < 0) count = 0;
133170034214SMatthew G. Knepley     msgLog[count] = 0;
133270034214SMatthew G. Knepley     close(1);
133370034214SMatthew G. Knepley     dup2(fd_stdout, 1);
133470034214SMatthew G. Knepley     close(fd_stdout);
133570034214SMatthew G. Knepley     close(fd_pipe[0]);
133670034214SMatthew G. Knepley     close(fd_pipe[1]);
133770034214SMatthew G. Knepley     if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
133870034214SMatthew G. Knepley   }
133907ed3857SLisandro Dalcin #else
134007ed3857SLisandro Dalcin   if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout");
134170034214SMatthew G. Knepley #endif
134270034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
134377623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
134470034214SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {
134577623264SMatthew G. Knepley     ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);
134670034214SMatthew G. Knepley   }
134777623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
134870034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
134977623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
135070034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
135170034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
135270034214SMatthew G. Knepley     }
135370034214SMatthew G. Knepley   }
135470034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
135570034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
135670034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
135770034214SMatthew G. Knepley     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
135870034214SMatthew G. Knepley   }
135970034214SMatthew G. Knepley   ierr = PetscFree(assignment);CHKERRQ(ierr);
136070034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
136170034214SMatthew G. Knepley   PetscFunctionReturn(0);
136277623264SMatthew G. Knepley #else
136377623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
136470034214SMatthew G. Knepley #endif
136577623264SMatthew G. Knepley }
136677623264SMatthew G. Knepley 
1367d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part)
136877623264SMatthew G. Knepley {
136977623264SMatthew G. Knepley   PetscFunctionBegin;
137077623264SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Chaco;
137177623264SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Chaco;
137277623264SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Chaco;
137377623264SMatthew G. Knepley   PetscFunctionReturn(0);
137477623264SMatthew G. Knepley }
137577623264SMatthew G. Knepley 
137677623264SMatthew G. Knepley /*MC
137777623264SMatthew G. Knepley   PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library
137877623264SMatthew G. Knepley 
137977623264SMatthew G. Knepley   Level: intermediate
138077623264SMatthew G. Knepley 
138177623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
138277623264SMatthew G. Knepley M*/
138377623264SMatthew G. Knepley 
138477623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part)
138577623264SMatthew G. Knepley {
138677623264SMatthew G. Knepley   PetscPartitioner_Chaco *p;
138777623264SMatthew G. Knepley   PetscErrorCode          ierr;
138877623264SMatthew G. Knepley 
138977623264SMatthew G. Knepley   PetscFunctionBegin;
139077623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
139177623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
139277623264SMatthew G. Knepley   part->data = p;
139377623264SMatthew G. Knepley 
139477623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr);
139577623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr);
139677623264SMatthew G. Knepley   PetscFunctionReturn(0);
139777623264SMatthew G. Knepley }
139877623264SMatthew G. Knepley 
13995b440754SMatthew G. Knepley static const char *ptypes[] = {"kway", "rb"};
14005b440754SMatthew G. Knepley 
1401d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part)
140277623264SMatthew G. Knepley {
140377623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
140477623264SMatthew G. Knepley   PetscErrorCode             ierr;
140577623264SMatthew G. Knepley 
140677623264SMatthew G. Knepley   PetscFunctionBegin;
140777623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
140877623264SMatthew G. Knepley   PetscFunctionReturn(0);
140977623264SMatthew G. Knepley }
141077623264SMatthew G. Knepley 
1411d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer)
141277623264SMatthew G. Knepley {
14132abdaa70SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
141477623264SMatthew G. Knepley   PetscErrorCode             ierr;
141577623264SMatthew G. Knepley 
141677623264SMatthew G. Knepley   PetscFunctionBegin;
14172abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
14182abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "ParMetis type: %s\n", ptypes[p->ptype]);CHKERRQ(ierr);
14192abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "load imbalance ratio %g\n", (double) p->imbalanceRatio);CHKERRQ(ierr);
14202abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "debug flag %D\n", p->debugFlag);CHKERRQ(ierr);
14212abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
142277623264SMatthew G. Knepley   PetscFunctionReturn(0);
142377623264SMatthew G. Knepley }
142477623264SMatthew G. Knepley 
1425d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer)
142677623264SMatthew G. Knepley {
142777623264SMatthew G. Knepley   PetscBool      iascii;
142877623264SMatthew G. Knepley   PetscErrorCode ierr;
142977623264SMatthew G. Knepley 
143077623264SMatthew G. Knepley   PetscFunctionBegin;
143177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
143277623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
143377623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
143477623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);}
143577623264SMatthew G. Knepley   PetscFunctionReturn(0);
143677623264SMatthew G. Knepley }
143770034214SMatthew G. Knepley 
143844d8be81SLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_ParMetis(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
143944d8be81SLisandro Dalcin {
144044d8be81SLisandro Dalcin   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
144144d8be81SLisandro Dalcin   PetscErrorCode            ierr;
144244d8be81SLisandro Dalcin 
144344d8be81SLisandro Dalcin   PetscFunctionBegin;
144444d8be81SLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner ParMetis Options");CHKERRQ(ierr);
144544d8be81SLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_parmetis_type", "Partitioning method", "", ptypes, 2, ptypes[p->ptype], &p->ptype, NULL);CHKERRQ(ierr);
14465b440754SMatthew G. Knepley   ierr = PetscOptionsReal("-petscpartitioner_parmetis_imbalance_ratio", "Load imbalance ratio limit", "", p->imbalanceRatio, &p->imbalanceRatio, NULL);CHKERRQ(ierr);
14475b440754SMatthew G. Knepley   ierr = PetscOptionsInt("-petscpartitioner_parmetis_debug", "Debugging flag", "", p->debugFlag, &p->debugFlag, NULL);CHKERRQ(ierr);
144844d8be81SLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
144944d8be81SLisandro Dalcin   PetscFunctionReturn(0);
145044d8be81SLisandro Dalcin }
145144d8be81SLisandro Dalcin 
145270034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
145370034214SMatthew G. Knepley #include <parmetis.h>
145477623264SMatthew G. Knepley #endif
145570034214SMatthew G. Knepley 
1456d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
145770034214SMatthew G. Knepley {
145877623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
14595b440754SMatthew G. Knepley   PetscPartitioner_ParMetis *pm = (PetscPartitioner_ParMetis *) part->data;
146070034214SMatthew G. Knepley   MPI_Comm       comm;
1461deea36a5SMatthew G. Knepley   PetscSection   section;
146270034214SMatthew G. Knepley   PetscInt       nvtxs       = numVertices; /* The number of vertices in full graph */
146370034214SMatthew G. Knepley   PetscInt      *vtxdist;                   /* Distribution of vertices across processes */
146470034214SMatthew G. Knepley   PetscInt      *xadj        = start;       /* Start of edge list for each vertex */
146570034214SMatthew G. Knepley   PetscInt      *adjncy      = adjacency;   /* Edge lists for all vertices */
146670034214SMatthew G. Knepley   PetscInt      *vwgt        = NULL;        /* Vertex weights */
146770034214SMatthew G. Knepley   PetscInt      *adjwgt      = NULL;        /* Edge weights */
146870034214SMatthew G. Knepley   PetscInt       wgtflag     = 0;           /* Indicates which weights are present */
146970034214SMatthew G. Knepley   PetscInt       numflag     = 0;           /* Indicates initial offset (0 or 1) */
147070034214SMatthew G. Knepley   PetscInt       ncon        = 1;           /* The number of weights per vertex */
14715b440754SMatthew G. Knepley   PetscInt       metis_ptype = pm->ptype;   /* kway or recursive bisection */
1472fb83b9f2SMichael Gegg   real_t        *tpwgts;                    /* The fraction of vertex weights assigned to each partition */
1473fb83b9f2SMichael Gegg   real_t        *ubvec;                     /* The balance intolerance for vertex weights */
1474b3ce585bSLisandro Dalcin   PetscInt       options[64];               /* Options */
147570034214SMatthew G. Knepley   /* Outputs */
1476b3ce585bSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1477b3ce585bSLisandro Dalcin   PetscMPIInt    size, rank, p;
147870034214SMatthew G. Knepley   PetscErrorCode ierr;
147970034214SMatthew G. Knepley 
148070034214SMatthew G. Knepley   PetscFunctionBegin;
148177623264SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr);
1482b3ce585bSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
148370034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
148470034214SMatthew G. Knepley   /* Calculate vertex distribution */
1485b3ce585bSLisandro Dalcin   ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr);
148670034214SMatthew G. Knepley   vtxdist[0] = 0;
148770034214SMatthew G. Knepley   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1488b3ce585bSLisandro Dalcin   for (p = 2; p <= size; ++p) {
148970034214SMatthew G. Knepley     vtxdist[p] += vtxdist[p-1];
149070034214SMatthew G. Knepley   }
149144d8be81SLisandro Dalcin   /* Calculate partition weights */
149270034214SMatthew G. Knepley   for (p = 0; p < nparts; ++p) {
149370034214SMatthew G. Knepley     tpwgts[p] = 1.0/nparts;
149470034214SMatthew G. Knepley   }
14955b440754SMatthew G. Knepley   ubvec[0] = pm->imbalanceRatio;
1496deea36a5SMatthew G. Knepley   /* Weight cells by dofs on cell by default */
1497e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1498deea36a5SMatthew G. Knepley   if (section) {
1499deea36a5SMatthew G. Knepley     PetscInt cStart, cEnd, dof;
150070034214SMatthew G. Knepley 
1501deea36a5SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1502deea36a5SMatthew G. Knepley     for (v = cStart; v < cEnd; ++v) {
1503deea36a5SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1504925b1076SLisandro Dalcin       /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1505925b1076SLisandro Dalcin       /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1506925b1076SLisandro Dalcin       if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1);
1507deea36a5SMatthew G. Knepley     }
1508deea36a5SMatthew G. Knepley   } else {
1509deea36a5SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1510cd0de0f2SShri   }
151144d8be81SLisandro Dalcin   wgtflag |= 2; /* have weights on graph vertices */
1512cd0de0f2SShri 
151370034214SMatthew G. Knepley   if (nparts == 1) {
15149fc93327SToby Isaac     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
151570034214SMatthew G. Knepley   } else {
1516b3ce585bSLisandro Dalcin     for (p = 0; !vtxdist[p+1] && p < size; ++p);
1517b3ce585bSLisandro Dalcin     if (vtxdist[p+1] == vtxdist[size]) {
1518b3ce585bSLisandro Dalcin       if (rank == p) {
151944d8be81SLisandro Dalcin         ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */
152044d8be81SLisandro Dalcin         if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()");
152144d8be81SLisandro Dalcin         if (metis_ptype == 1) {
152244d8be81SLisandro Dalcin           PetscStackPush("METIS_PartGraphRecursive");
152372379da4SMatthew G. Knepley           ierr = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
152444d8be81SLisandro Dalcin           PetscStackPop;
152544d8be81SLisandro Dalcin           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphRecursive()");
152644d8be81SLisandro Dalcin         } else {
152744d8be81SLisandro Dalcin           /*
152844d8be81SLisandro Dalcin            It would be nice to activate the two options below, but they would need some actual testing.
152944d8be81SLisandro Dalcin            - Turning on these options may exercise path of the METIS code that have bugs and may break production runs.
153044d8be81SLisandro 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.
153144d8be81SLisandro Dalcin           */
153244d8be81SLisandro Dalcin           /* options[METIS_OPTION_CONTIG]  = 1; */ /* try to produce partitions that are contiguous */
153344d8be81SLisandro Dalcin           /* options[METIS_OPTION_MINCONN] = 1; */ /* minimize the maximum degree of the subdomain graph */
153470034214SMatthew G. Knepley           PetscStackPush("METIS_PartGraphKway");
153572379da4SMatthew G. Knepley           ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
153670034214SMatthew G. Knepley           PetscStackPop;
153770034214SMatthew G. Knepley           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
153870034214SMatthew G. Knepley         }
153944d8be81SLisandro Dalcin       }
154070034214SMatthew G. Knepley     } else {
15415b440754SMatthew G. Knepley       options[0] = 1;
15425b440754SMatthew G. Knepley       options[1] = pm->debugFlag;
154370034214SMatthew G. Knepley       PetscStackPush("ParMETIS_V3_PartKway");
154472379da4SMatthew G. Knepley       ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment, &comm);
154570034214SMatthew G. Knepley       PetscStackPop;
1546c717d290SMatthew G. Knepley       if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr);
154770034214SMatthew G. Knepley     }
154870034214SMatthew G. Knepley   }
154970034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
155077623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
155177623264SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
155277623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
155370034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
155477623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
155570034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
155670034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
155770034214SMatthew G. Knepley     }
155870034214SMatthew G. Knepley   }
155970034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
156070034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1561cd0de0f2SShri   ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr);
15629b80ac48SMatthew G. Knepley   PetscFunctionReturn(0);
156370034214SMatthew G. Knepley #else
156477623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis.");
156570034214SMatthew G. Knepley #endif
156670034214SMatthew G. Knepley }
156770034214SMatthew G. Knepley 
1568d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part)
156977623264SMatthew G. Knepley {
157077623264SMatthew G. Knepley   PetscFunctionBegin;
157177623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_ParMetis;
157244d8be81SLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_ParMetis;
157377623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_ParMetis;
157477623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_ParMetis;
157577623264SMatthew G. Knepley   PetscFunctionReturn(0);
157677623264SMatthew G. Knepley }
157777623264SMatthew G. Knepley 
157877623264SMatthew G. Knepley /*MC
157977623264SMatthew G. Knepley   PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library
158077623264SMatthew G. Knepley 
158177623264SMatthew G. Knepley   Level: intermediate
158277623264SMatthew G. Knepley 
158377623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
158477623264SMatthew G. Knepley M*/
158577623264SMatthew G. Knepley 
158677623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part)
158777623264SMatthew G. Knepley {
158877623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p;
158977623264SMatthew G. Knepley   PetscErrorCode          ierr;
159077623264SMatthew G. Knepley 
159177623264SMatthew G. Knepley   PetscFunctionBegin;
159277623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
159377623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
159477623264SMatthew G. Knepley   part->data = p;
159577623264SMatthew G. Knepley 
15965b440754SMatthew G. Knepley   p->ptype          = 0;
15975b440754SMatthew G. Knepley   p->imbalanceRatio = 1.05;
15985b440754SMatthew G. Knepley   p->debugFlag      = 0;
15995b440754SMatthew G. Knepley 
160077623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr);
160177623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr);
160270034214SMatthew G. Knepley   PetscFunctionReturn(0);
160370034214SMatthew G. Knepley }
160470034214SMatthew G. Knepley 
1605137cd93aSLisandro Dalcin 
1606137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE;
1607137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] =
1608137cd93aSLisandro Dalcin   "@article{PTSCOTCH,\n"
1609137cd93aSLisandro Dalcin   "  author  = {C. Chevalier and F. Pellegrini},\n"
1610137cd93aSLisandro Dalcin   "  title   = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n"
1611137cd93aSLisandro Dalcin   "  journal = {Parallel Computing},\n"
1612137cd93aSLisandro Dalcin   "  volume  = {34},\n"
1613137cd93aSLisandro Dalcin   "  number  = {6},\n"
1614137cd93aSLisandro Dalcin   "  pages   = {318--331},\n"
1615137cd93aSLisandro Dalcin   "  year    = {2008},\n"
1616137cd93aSLisandro Dalcin   "  doi     = {https://doi.org/10.1016/j.parco.2007.12.001}\n"
1617137cd93aSLisandro Dalcin   "}\n";
1618137cd93aSLisandro Dalcin 
1619137cd93aSLisandro Dalcin typedef struct {
1620137cd93aSLisandro Dalcin   PetscInt  strategy;
1621137cd93aSLisandro Dalcin   PetscReal imbalance;
1622137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch;
1623137cd93aSLisandro Dalcin 
1624137cd93aSLisandro Dalcin static const char *const
1625137cd93aSLisandro Dalcin PTScotchStrategyList[] = {
1626137cd93aSLisandro Dalcin   "DEFAULT",
1627137cd93aSLisandro Dalcin   "QUALITY",
1628137cd93aSLisandro Dalcin   "SPEED",
1629137cd93aSLisandro Dalcin   "BALANCE",
1630137cd93aSLisandro Dalcin   "SAFETY",
1631137cd93aSLisandro Dalcin   "SCALABILITY",
1632137cd93aSLisandro Dalcin   "RECURSIVE",
1633137cd93aSLisandro Dalcin   "REMAP"
1634137cd93aSLisandro Dalcin };
1635137cd93aSLisandro Dalcin 
1636137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1637137cd93aSLisandro Dalcin 
1638137cd93aSLisandro Dalcin EXTERN_C_BEGIN
1639137cd93aSLisandro Dalcin #include <ptscotch.h>
1640137cd93aSLisandro Dalcin EXTERN_C_END
1641137cd93aSLisandro Dalcin 
1642137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0)
1643137cd93aSLisandro Dalcin 
1644137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy)
1645137cd93aSLisandro Dalcin {
1646137cd93aSLisandro Dalcin   switch (strategy) {
1647137cd93aSLisandro Dalcin   case  0: return SCOTCH_STRATDEFAULT;
1648137cd93aSLisandro Dalcin   case  1: return SCOTCH_STRATQUALITY;
1649137cd93aSLisandro Dalcin   case  2: return SCOTCH_STRATSPEED;
1650137cd93aSLisandro Dalcin   case  3: return SCOTCH_STRATBALANCE;
1651137cd93aSLisandro Dalcin   case  4: return SCOTCH_STRATSAFETY;
1652137cd93aSLisandro Dalcin   case  5: return SCOTCH_STRATSCALABILITY;
1653137cd93aSLisandro Dalcin   case  6: return SCOTCH_STRATRECURSIVE;
1654137cd93aSLisandro Dalcin   case  7: return SCOTCH_STRATREMAP;
1655137cd93aSLisandro Dalcin   default: return SCOTCH_STRATDEFAULT;
1656137cd93aSLisandro Dalcin   }
1657137cd93aSLisandro Dalcin }
1658137cd93aSLisandro Dalcin 
1659137cd93aSLisandro Dalcin 
1660137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1661137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[])
1662137cd93aSLisandro Dalcin {
1663137cd93aSLisandro Dalcin   SCOTCH_Graph   grafdat;
1664137cd93aSLisandro Dalcin   SCOTCH_Strat   stradat;
1665137cd93aSLisandro Dalcin   SCOTCH_Num     vertnbr = n;
1666137cd93aSLisandro Dalcin   SCOTCH_Num     edgenbr = xadj[n];
1667137cd93aSLisandro Dalcin   SCOTCH_Num*    velotab = vtxwgt;
1668137cd93aSLisandro Dalcin   SCOTCH_Num*    edlotab = adjwgt;
1669137cd93aSLisandro Dalcin   SCOTCH_Num     flagval = strategy;
1670137cd93aSLisandro Dalcin   double         kbalval = imbalance;
1671137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1672137cd93aSLisandro Dalcin 
1673137cd93aSLisandro Dalcin   PetscFunctionBegin;
1674d99a0000SVaclav Hapla   {
1675d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1676d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1677d99a0000SVaclav Hapla     if (!flg) velotab = NULL;
1678d99a0000SVaclav Hapla   }
1679137cd93aSLisandro Dalcin   ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr);
1680137cd93aSLisandro Dalcin   ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr);
1681137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1682137cd93aSLisandro Dalcin   ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr);
1683137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1684137cd93aSLisandro Dalcin   ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1685137cd93aSLisandro Dalcin #endif
1686137cd93aSLisandro Dalcin   ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr);
1687137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1688137cd93aSLisandro Dalcin   SCOTCH_graphExit(&grafdat);
1689137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1690137cd93aSLisandro Dalcin }
1691137cd93aSLisandro Dalcin 
1692137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1693137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm)
1694137cd93aSLisandro Dalcin {
1695137cd93aSLisandro Dalcin   PetscMPIInt     procglbnbr;
1696137cd93aSLisandro Dalcin   PetscMPIInt     proclocnum;
1697137cd93aSLisandro Dalcin   SCOTCH_Arch     archdat;
1698137cd93aSLisandro Dalcin   SCOTCH_Dgraph   grafdat;
1699137cd93aSLisandro Dalcin   SCOTCH_Dmapping mappdat;
1700137cd93aSLisandro Dalcin   SCOTCH_Strat    stradat;
1701137cd93aSLisandro Dalcin   SCOTCH_Num      vertlocnbr;
1702137cd93aSLisandro Dalcin   SCOTCH_Num      edgelocnbr;
1703137cd93aSLisandro Dalcin   SCOTCH_Num*     veloloctab = vtxwgt;
1704137cd93aSLisandro Dalcin   SCOTCH_Num*     edloloctab = adjwgt;
1705137cd93aSLisandro Dalcin   SCOTCH_Num      flagval = strategy;
1706137cd93aSLisandro Dalcin   double          kbalval = imbalance;
1707137cd93aSLisandro Dalcin   PetscErrorCode  ierr;
1708137cd93aSLisandro Dalcin 
1709137cd93aSLisandro Dalcin   PetscFunctionBegin;
1710d99a0000SVaclav Hapla   {
1711d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1712d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1713d99a0000SVaclav Hapla     if (!flg) veloloctab = NULL;
1714d99a0000SVaclav Hapla   }
1715137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr);
1716137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr);
1717137cd93aSLisandro Dalcin   vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum];
1718137cd93aSLisandro Dalcin   edgelocnbr = xadj[vertlocnbr];
1719137cd93aSLisandro Dalcin 
1720137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr);
1721137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr);
1722137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1723137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1724137cd93aSLisandro Dalcin #endif
1725137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1726137cd93aSLisandro Dalcin   ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr);
1727137cd93aSLisandro Dalcin   ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr);
1728137cd93aSLisandro Dalcin   ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr);
1729137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr);
1730137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr);
1731137cd93aSLisandro Dalcin   SCOTCH_dgraphMapExit(&grafdat, &mappdat);
1732137cd93aSLisandro Dalcin   SCOTCH_archExit(&archdat);
1733137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1734137cd93aSLisandro Dalcin   SCOTCH_dgraphExit(&grafdat);
1735137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1736137cd93aSLisandro Dalcin }
1737137cd93aSLisandro Dalcin 
1738137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */
1739137cd93aSLisandro Dalcin 
1740137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part)
1741137cd93aSLisandro Dalcin {
1742137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1743137cd93aSLisandro Dalcin   PetscErrorCode             ierr;
1744137cd93aSLisandro Dalcin 
1745137cd93aSLisandro Dalcin   PetscFunctionBegin;
1746137cd93aSLisandro Dalcin   ierr = PetscFree(p);CHKERRQ(ierr);
1747137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1748137cd93aSLisandro Dalcin }
1749137cd93aSLisandro Dalcin 
1750137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer)
1751137cd93aSLisandro Dalcin {
1752137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1753137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1754137cd93aSLisandro Dalcin 
1755137cd93aSLisandro Dalcin   PetscFunctionBegin;
1756137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1757137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr);
1758137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr);
1759137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1760137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1761137cd93aSLisandro Dalcin }
1762137cd93aSLisandro Dalcin 
1763137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer)
1764137cd93aSLisandro Dalcin {
1765137cd93aSLisandro Dalcin   PetscBool      iascii;
1766137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1767137cd93aSLisandro Dalcin 
1768137cd93aSLisandro Dalcin   PetscFunctionBegin;
1769137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1770137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1771137cd93aSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1772137cd93aSLisandro Dalcin   if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);}
1773137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1774137cd93aSLisandro Dalcin }
1775137cd93aSLisandro Dalcin 
1776137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
1777137cd93aSLisandro Dalcin {
1778137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1779137cd93aSLisandro Dalcin   const char *const         *slist = PTScotchStrategyList;
1780137cd93aSLisandro Dalcin   PetscInt                  nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0]));
1781137cd93aSLisandro Dalcin   PetscBool                 flag;
1782137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1783137cd93aSLisandro Dalcin 
1784137cd93aSLisandro Dalcin   PetscFunctionBegin;
1785137cd93aSLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner PTScotch Options");CHKERRQ(ierr);
1786137cd93aSLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr);
1787137cd93aSLisandro Dalcin   ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr);
1788137cd93aSLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
1789137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1790137cd93aSLisandro Dalcin }
1791137cd93aSLisandro Dalcin 
1792137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1793137cd93aSLisandro Dalcin {
1794137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1795137cd93aSLisandro Dalcin   MPI_Comm       comm       = PetscObjectComm((PetscObject)part);
1796137cd93aSLisandro Dalcin   PetscInt       nvtxs      = numVertices; /* The number of vertices in full graph */
1797137cd93aSLisandro Dalcin   PetscInt      *vtxdist;                  /* Distribution of vertices across processes */
1798137cd93aSLisandro Dalcin   PetscInt      *xadj       = start;       /* Start of edge list for each vertex */
1799137cd93aSLisandro Dalcin   PetscInt      *adjncy     = adjacency;   /* Edge lists for all vertices */
1800137cd93aSLisandro Dalcin   PetscInt      *vwgt       = NULL;        /* Vertex weights */
1801137cd93aSLisandro Dalcin   PetscInt      *adjwgt     = NULL;        /* Edge weights */
1802137cd93aSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1803137cd93aSLisandro Dalcin   PetscMPIInt    size, rank, p;
1804137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1805137cd93aSLisandro Dalcin 
1806137cd93aSLisandro Dalcin   PetscFunctionBegin;
1807137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1808137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1809137cd93aSLisandro Dalcin   ierr = PetscMalloc2(nparts+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr);
1810137cd93aSLisandro Dalcin 
1811137cd93aSLisandro Dalcin   /* Calculate vertex distribution */
1812137cd93aSLisandro Dalcin   vtxdist[0] = 0;
1813137cd93aSLisandro Dalcin   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1814137cd93aSLisandro Dalcin   for (p = 2; p <= size; ++p) {
1815137cd93aSLisandro Dalcin     vtxdist[p] += vtxdist[p-1];
1816137cd93aSLisandro Dalcin   }
1817137cd93aSLisandro Dalcin 
1818137cd93aSLisandro Dalcin   if (nparts == 1) {
1819137cd93aSLisandro Dalcin     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
1820137cd93aSLisandro Dalcin   } else {
1821137cd93aSLisandro Dalcin     PetscSection section;
1822137cd93aSLisandro Dalcin     /* Weight cells by dofs on cell by default */
1823137cd93aSLisandro Dalcin     ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr);
1824e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1825137cd93aSLisandro Dalcin     if (section) {
1826137cd93aSLisandro Dalcin       PetscInt vStart, vEnd, dof;
1827137cd93aSLisandro Dalcin       ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1828137cd93aSLisandro Dalcin       for (v = vStart; v < vEnd; ++v) {
1829137cd93aSLisandro Dalcin         ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1830137cd93aSLisandro Dalcin         /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1831137cd93aSLisandro Dalcin         /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1832137cd93aSLisandro Dalcin         if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1);
1833137cd93aSLisandro Dalcin       }
1834137cd93aSLisandro Dalcin     } else {
1835137cd93aSLisandro Dalcin       for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1836137cd93aSLisandro Dalcin     }
1837137cd93aSLisandro Dalcin     {
1838137cd93aSLisandro Dalcin       PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data;
1839137cd93aSLisandro Dalcin       int                       strat = PTScotch_Strategy(pts->strategy);
1840137cd93aSLisandro Dalcin       double                    imbal = (double)pts->imbalance;
1841137cd93aSLisandro Dalcin 
1842137cd93aSLisandro Dalcin       for (p = 0; !vtxdist[p+1] && p < size; ++p);
1843137cd93aSLisandro Dalcin       if (vtxdist[p+1] == vtxdist[size]) {
1844137cd93aSLisandro Dalcin         if (rank == p) {
1845137cd93aSLisandro Dalcin           ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr);
1846137cd93aSLisandro Dalcin         }
1847137cd93aSLisandro Dalcin       } else {
1848137cd93aSLisandro Dalcin         ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr);
1849137cd93aSLisandro Dalcin       }
1850137cd93aSLisandro Dalcin     }
1851137cd93aSLisandro Dalcin     ierr = PetscFree(vwgt);CHKERRQ(ierr);
1852137cd93aSLisandro Dalcin   }
1853137cd93aSLisandro Dalcin 
1854137cd93aSLisandro Dalcin   /* Convert to PetscSection+IS */
1855137cd93aSLisandro Dalcin   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1856137cd93aSLisandro Dalcin   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
1857137cd93aSLisandro Dalcin   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1858137cd93aSLisandro Dalcin   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
1859137cd93aSLisandro Dalcin   for (p = 0, i = 0; p < nparts; ++p) {
1860137cd93aSLisandro Dalcin     for (v = 0; v < nvtxs; ++v) {
1861137cd93aSLisandro Dalcin       if (assignment[v] == p) points[i++] = v;
1862137cd93aSLisandro Dalcin     }
1863137cd93aSLisandro Dalcin   }
1864137cd93aSLisandro Dalcin   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
1865137cd93aSLisandro Dalcin   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1866137cd93aSLisandro Dalcin 
1867137cd93aSLisandro Dalcin   ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr);
1868137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1869137cd93aSLisandro Dalcin #else
1870137cd93aSLisandro Dalcin   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch.");
1871137cd93aSLisandro Dalcin #endif
1872137cd93aSLisandro Dalcin }
1873137cd93aSLisandro Dalcin 
1874137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part)
1875137cd93aSLisandro Dalcin {
1876137cd93aSLisandro Dalcin   PetscFunctionBegin;
1877137cd93aSLisandro Dalcin   part->ops->view           = PetscPartitionerView_PTScotch;
1878137cd93aSLisandro Dalcin   part->ops->destroy        = PetscPartitionerDestroy_PTScotch;
1879137cd93aSLisandro Dalcin   part->ops->partition      = PetscPartitionerPartition_PTScotch;
1880137cd93aSLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch;
1881137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1882137cd93aSLisandro Dalcin }
1883137cd93aSLisandro Dalcin 
1884137cd93aSLisandro Dalcin /*MC
1885137cd93aSLisandro Dalcin   PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library
1886137cd93aSLisandro Dalcin 
1887137cd93aSLisandro Dalcin   Level: intermediate
1888137cd93aSLisandro Dalcin 
1889137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1890137cd93aSLisandro Dalcin M*/
1891137cd93aSLisandro Dalcin 
1892137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part)
1893137cd93aSLisandro Dalcin {
1894137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p;
1895137cd93aSLisandro Dalcin   PetscErrorCode          ierr;
1896137cd93aSLisandro Dalcin 
1897137cd93aSLisandro Dalcin   PetscFunctionBegin;
1898137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1899137cd93aSLisandro Dalcin   ierr = PetscNewLog(part, &p);CHKERRQ(ierr);
1900137cd93aSLisandro Dalcin   part->data = p;
1901137cd93aSLisandro Dalcin 
1902137cd93aSLisandro Dalcin   p->strategy  = 0;
1903137cd93aSLisandro Dalcin   p->imbalance = 0.01;
1904137cd93aSLisandro Dalcin 
1905137cd93aSLisandro Dalcin   ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr);
1906137cd93aSLisandro Dalcin   ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr);
1907137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1908137cd93aSLisandro Dalcin }
1909137cd93aSLisandro Dalcin 
1910137cd93aSLisandro Dalcin 
19115680f57bSMatthew G. Knepley /*@
19125680f57bSMatthew G. Knepley   DMPlexGetPartitioner - Get the mesh partitioner
19135680f57bSMatthew G. Knepley 
19145680f57bSMatthew G. Knepley   Not collective
19155680f57bSMatthew G. Knepley 
19165680f57bSMatthew G. Knepley   Input Parameter:
19175680f57bSMatthew G. Knepley . dm - The DM
19185680f57bSMatthew G. Knepley 
19195680f57bSMatthew G. Knepley   Output Parameter:
19205680f57bSMatthew G. Knepley . part - The PetscPartitioner
19215680f57bSMatthew G. Knepley 
19225680f57bSMatthew G. Knepley   Level: developer
19235680f57bSMatthew G. Knepley 
192498599a47SLawrence Mitchell   Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner.
192598599a47SLawrence Mitchell 
192698599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate()
19275680f57bSMatthew G. Knepley @*/
19285680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part)
19295680f57bSMatthew G. Knepley {
19305680f57bSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
19315680f57bSMatthew G. Knepley 
19325680f57bSMatthew G. Knepley   PetscFunctionBegin;
19335680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19345680f57bSMatthew G. Knepley   PetscValidPointer(part, 2);
19355680f57bSMatthew G. Knepley   *part = mesh->partitioner;
19365680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
19375680f57bSMatthew G. Knepley }
19385680f57bSMatthew G. Knepley 
193971bb2955SLawrence Mitchell /*@
194071bb2955SLawrence Mitchell   DMPlexSetPartitioner - Set the mesh partitioner
194171bb2955SLawrence Mitchell 
194271bb2955SLawrence Mitchell   logically collective on dm and part
194371bb2955SLawrence Mitchell 
194471bb2955SLawrence Mitchell   Input Parameters:
194571bb2955SLawrence Mitchell + dm - The DM
194671bb2955SLawrence Mitchell - part - The partitioner
194771bb2955SLawrence Mitchell 
194871bb2955SLawrence Mitchell   Level: developer
194971bb2955SLawrence Mitchell 
195071bb2955SLawrence Mitchell   Note: Any existing PetscPartitioner will be destroyed.
195171bb2955SLawrence Mitchell 
195271bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate()
195371bb2955SLawrence Mitchell @*/
195471bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part)
195571bb2955SLawrence Mitchell {
195671bb2955SLawrence Mitchell   DM_Plex       *mesh = (DM_Plex *) dm->data;
195771bb2955SLawrence Mitchell   PetscErrorCode ierr;
195871bb2955SLawrence Mitchell 
195971bb2955SLawrence Mitchell   PetscFunctionBegin;
196071bb2955SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
196171bb2955SLawrence Mitchell   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2);
196271bb2955SLawrence Mitchell   ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr);
196371bb2955SLawrence Mitchell   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
196471bb2955SLawrence Mitchell   mesh->partitioner = part;
196571bb2955SLawrence Mitchell   PetscFunctionReturn(0);
196671bb2955SLawrence Mitchell }
196771bb2955SLawrence Mitchell 
1968e8f14785SLisandro Dalcin static PetscErrorCode DMPlexAddClosure_Tree(DM dm, PetscHSetI ht, PetscInt point, PetscBool up, PetscBool down)
1969270bba0cSToby Isaac {
1970270bba0cSToby Isaac   PetscErrorCode ierr;
1971270bba0cSToby Isaac 
1972270bba0cSToby Isaac   PetscFunctionBegin;
19736a5a2ffdSToby Isaac   if (up) {
19746a5a2ffdSToby Isaac     PetscInt parent;
19756a5a2ffdSToby Isaac 
1976270bba0cSToby Isaac     ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr);
19776a5a2ffdSToby Isaac     if (parent != point) {
19786a5a2ffdSToby Isaac       PetscInt closureSize, *closure = NULL, i;
19796a5a2ffdSToby Isaac 
1980270bba0cSToby Isaac       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1981270bba0cSToby Isaac       for (i = 0; i < closureSize; i++) {
1982270bba0cSToby Isaac         PetscInt cpoint = closure[2*i];
1983270bba0cSToby Isaac 
1984e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
19851b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr);
1986270bba0cSToby Isaac       }
1987270bba0cSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
19886a5a2ffdSToby Isaac     }
19896a5a2ffdSToby Isaac   }
19906a5a2ffdSToby Isaac   if (down) {
19916a5a2ffdSToby Isaac     PetscInt numChildren;
19926a5a2ffdSToby Isaac     const PetscInt *children;
19936a5a2ffdSToby Isaac 
19946a5a2ffdSToby Isaac     ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr);
19956a5a2ffdSToby Isaac     if (numChildren) {
19966a5a2ffdSToby Isaac       PetscInt i;
19976a5a2ffdSToby Isaac 
19986a5a2ffdSToby Isaac       for (i = 0; i < numChildren; i++) {
19996a5a2ffdSToby Isaac         PetscInt cpoint = children[i];
20006a5a2ffdSToby Isaac 
2001e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
20021b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr);
20036a5a2ffdSToby Isaac       }
20046a5a2ffdSToby Isaac     }
20056a5a2ffdSToby Isaac   }
2006270bba0cSToby Isaac   PetscFunctionReturn(0);
2007270bba0cSToby Isaac }
2008270bba0cSToby Isaac 
20095abbe4feSMichael Lange /*@
20105abbe4feSMichael Lange   DMPlexPartitionLabelClosure - Add the closure of all points to the partition label
20115abbe4feSMichael Lange 
20125abbe4feSMichael Lange   Input Parameters:
20135abbe4feSMichael Lange + dm     - The DM
20145abbe4feSMichael Lange - label  - DMLabel assinging ranks to remote roots
20155abbe4feSMichael Lange 
20165abbe4feSMichael Lange   Level: developer
20175abbe4feSMichael Lange 
20185abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
20195abbe4feSMichael Lange @*/
20205abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label)
20219ffc88e4SToby Isaac {
20225abbe4feSMichael Lange   IS              rankIS,   pointIS;
20235abbe4feSMichael Lange   const PetscInt *ranks,   *points;
20245abbe4feSMichael Lange   PetscInt        numRanks, numPoints, r, p, c, closureSize;
20255abbe4feSMichael Lange   PetscInt       *closure = NULL;
20261b807c88SLisandro Dalcin   DM_Plex        *mesh    = (DM_Plex *)dm->data;
20271b807c88SLisandro Dalcin   PetscBool       hasTree = (mesh->parentSection || mesh->childSection) ? PETSC_TRUE : PETSC_FALSE;
20289ffc88e4SToby Isaac   PetscErrorCode  ierr;
20299ffc88e4SToby Isaac 
20309ffc88e4SToby Isaac   PetscFunctionBegin;
20315abbe4feSMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
20325abbe4feSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
20335abbe4feSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
20341b807c88SLisandro Dalcin 
20355abbe4feSMichael Lange   for (r = 0; r < numRanks; ++r) {
20365abbe4feSMichael Lange     const PetscInt rank = ranks[r];
2037e8f14785SLisandro Dalcin     PetscHSetI     ht;
2038e8f14785SLisandro Dalcin     PetscInt       nelems, *elems, off = 0;
20399ffc88e4SToby Isaac 
20405abbe4feSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
20415abbe4feSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
20425abbe4feSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2043e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
2044e8f14785SLisandro Dalcin     ierr = PetscHSetIResize(ht, numPoints*16);CHKERRQ(ierr);
20455abbe4feSMichael Lange     for (p = 0; p < numPoints; ++p) {
20465abbe4feSMichael Lange       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2047270bba0cSToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
2048e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, closure[c]);CHKERRQ(ierr);
20491b807c88SLisandro Dalcin         if (hasTree) {ierr = DMPlexAddClosure_Tree(dm, ht, closure[c], PETSC_TRUE, PETSC_TRUE);CHKERRQ(ierr);}
2050270bba0cSToby Isaac       }
20519ffc88e4SToby Isaac     }
20525abbe4feSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
20535abbe4feSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2054e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(ht, &nelems);CHKERRQ(ierr);
2055e8f14785SLisandro Dalcin     ierr = PetscMalloc1(nelems, &elems);CHKERRQ(ierr);
2056f5a7d1c1SBarry Smith     ierr = PetscHSetIGetElems(ht, &off, elems);CHKERRQ(ierr);
2057e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
2058e8f14785SLisandro Dalcin     ierr = PetscSortInt(nelems, elems);CHKERRQ(ierr);
2059e8f14785SLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF, nelems, elems, PETSC_OWN_POINTER, &pointIS);CHKERRQ(ierr);
20601b807c88SLisandro Dalcin     ierr = DMLabelSetStratumIS(label, rank, pointIS);CHKERRQ(ierr);
20611b807c88SLisandro Dalcin     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
20629ffc88e4SToby Isaac   }
20631b807c88SLisandro Dalcin 
20641b807c88SLisandro Dalcin   if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);}
20655abbe4feSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
20665abbe4feSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
20679ffc88e4SToby Isaac   PetscFunctionReturn(0);
20689ffc88e4SToby Isaac }
20699ffc88e4SToby Isaac 
207024d039d7SMichael Lange /*@
207124d039d7SMichael Lange   DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label
207224d039d7SMichael Lange 
207324d039d7SMichael Lange   Input Parameters:
207424d039d7SMichael Lange + dm     - The DM
207524d039d7SMichael Lange - label  - DMLabel assinging ranks to remote roots
207624d039d7SMichael Lange 
207724d039d7SMichael Lange   Level: developer
207824d039d7SMichael Lange 
207924d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
208024d039d7SMichael Lange @*/
208124d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label)
208270034214SMatthew G. Knepley {
208324d039d7SMichael Lange   IS              rankIS,   pointIS;
208424d039d7SMichael Lange   const PetscInt *ranks,   *points;
208524d039d7SMichael Lange   PetscInt        numRanks, numPoints, r, p, a, adjSize;
208624d039d7SMichael Lange   PetscInt       *adj = NULL;
208770034214SMatthew G. Knepley   PetscErrorCode  ierr;
208870034214SMatthew G. Knepley 
208970034214SMatthew G. Knepley   PetscFunctionBegin;
209024d039d7SMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
209124d039d7SMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
209224d039d7SMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
209324d039d7SMichael Lange   for (r = 0; r < numRanks; ++r) {
209424d039d7SMichael Lange     const PetscInt rank = ranks[r];
209570034214SMatthew G. Knepley 
209624d039d7SMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
209724d039d7SMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
209824d039d7SMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
209970034214SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
210024d039d7SMichael Lange       adjSize = PETSC_DETERMINE;
210124d039d7SMichael Lange       ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr);
210224d039d7SMichael Lange       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);}
210370034214SMatthew G. Knepley     }
210424d039d7SMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
210524d039d7SMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
210670034214SMatthew G. Knepley   }
210724d039d7SMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
210824d039d7SMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
210924d039d7SMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
211024d039d7SMichael Lange   PetscFunctionReturn(0);
211170034214SMatthew G. Knepley }
211270034214SMatthew G. Knepley 
2113be200f8dSMichael Lange /*@
2114be200f8dSMichael Lange   DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF
2115be200f8dSMichael Lange 
2116be200f8dSMichael Lange   Input Parameters:
2117be200f8dSMichael Lange + dm     - The DM
2118be200f8dSMichael Lange - label  - DMLabel assinging ranks to remote roots
2119be200f8dSMichael Lange 
2120be200f8dSMichael Lange   Level: developer
2121be200f8dSMichael Lange 
2122be200f8dSMichael Lange   Note: This is required when generating multi-level overlaps to capture
2123be200f8dSMichael Lange   overlap points from non-neighbouring partitions.
2124be200f8dSMichael Lange 
2125be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
2126be200f8dSMichael Lange @*/
2127be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label)
2128be200f8dSMichael Lange {
2129be200f8dSMichael Lange   MPI_Comm        comm;
2130be200f8dSMichael Lange   PetscMPIInt     rank;
2131be200f8dSMichael Lange   PetscSF         sfPoint;
21325d04f6ebSMichael Lange   DMLabel         lblRoots, lblLeaves;
2133be200f8dSMichael Lange   IS              rankIS, pointIS;
2134be200f8dSMichael Lange   const PetscInt *ranks;
2135be200f8dSMichael Lange   PetscInt        numRanks, r;
2136be200f8dSMichael Lange   PetscErrorCode  ierr;
2137be200f8dSMichael Lange 
2138be200f8dSMichael Lange   PetscFunctionBegin;
2139be200f8dSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
2140be200f8dSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2141be200f8dSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
21425d04f6ebSMichael Lange   /* Pull point contributions from remote leaves into local roots */
21435d04f6ebSMichael Lange   ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
21445d04f6ebSMichael Lange   ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr);
21455d04f6ebSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
21465d04f6ebSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
21475d04f6ebSMichael Lange   for (r = 0; r < numRanks; ++r) {
21485d04f6ebSMichael Lange     const PetscInt remoteRank = ranks[r];
21495d04f6ebSMichael Lange     if (remoteRank == rank) continue;
21505d04f6ebSMichael Lange     ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr);
21515d04f6ebSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
21525d04f6ebSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21535d04f6ebSMichael Lange   }
21545d04f6ebSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
21555d04f6ebSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
21565d04f6ebSMichael Lange   ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
2157be200f8dSMichael Lange   /* Push point contributions from roots into remote leaves */
2158be200f8dSMichael Lange   ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
2159be200f8dSMichael Lange   ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr);
2160be200f8dSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
2161be200f8dSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
2162be200f8dSMichael Lange   for (r = 0; r < numRanks; ++r) {
2163be200f8dSMichael Lange     const PetscInt remoteRank = ranks[r];
2164be200f8dSMichael Lange     if (remoteRank == rank) continue;
2165be200f8dSMichael Lange     ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr);
2166be200f8dSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
2167be200f8dSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2168be200f8dSMichael Lange   }
2169be200f8dSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
2170be200f8dSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
2171be200f8dSMichael Lange   ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
2172be200f8dSMichael Lange   PetscFunctionReturn(0);
2173be200f8dSMichael Lange }
2174be200f8dSMichael Lange 
21751fd9873aSMichael Lange /*@
21761fd9873aSMichael Lange   DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label
21771fd9873aSMichael Lange 
21781fd9873aSMichael Lange   Input Parameters:
21791fd9873aSMichael Lange + dm        - The DM
21801fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots
21811fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank
21821fd9873aSMichael Lange 
21831fd9873aSMichael Lange   Output Parameter:
21841fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots
21851fd9873aSMichael Lange 
21861fd9873aSMichael Lange   Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The
21871fd9873aSMichael Lange   resulting leafLabel is a receiver mapping of remote roots to their parent rank.
21881fd9873aSMichael Lange 
21891fd9873aSMichael Lange   Level: developer
21901fd9873aSMichael Lange 
21911fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
21921fd9873aSMichael Lange @*/
21931fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel)
21941fd9873aSMichael Lange {
21951fd9873aSMichael Lange   MPI_Comm           comm;
21969852e123SBarry Smith   PetscMPIInt        rank, size;
21979852e123SBarry Smith   PetscInt           p, n, numNeighbors, ssize, l, nleaves;
21981fd9873aSMichael Lange   PetscSF            sfPoint;
21991fd9873aSMichael Lange   PetscSFNode       *rootPoints, *leafPoints;
22001fd9873aSMichael Lange   PetscSection       rootSection, leafSection;
22011fd9873aSMichael Lange   const PetscSFNode *remote;
22021fd9873aSMichael Lange   const PetscInt    *local, *neighbors;
22031fd9873aSMichael Lange   IS                 valueIS;
22041fd9873aSMichael Lange   PetscErrorCode     ierr;
22051fd9873aSMichael Lange 
22061fd9873aSMichael Lange   PetscFunctionBegin;
22071fd9873aSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
22081fd9873aSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
22099852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
22101fd9873aSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
22111fd9873aSMichael Lange 
22121fd9873aSMichael Lange   /* Convert to (point, rank) and use actual owners */
22131fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
22149852e123SBarry Smith   ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr);
22151fd9873aSMichael Lange   ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr);
22161fd9873aSMichael Lange   ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr);
22171fd9873aSMichael Lange   ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr);
22181fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22191fd9873aSMichael Lange     PetscInt numPoints;
22201fd9873aSMichael Lange 
22211fd9873aSMichael Lange     ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr);
22221fd9873aSMichael Lange     ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr);
22231fd9873aSMichael Lange   }
22241fd9873aSMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
22259852e123SBarry Smith   ierr = PetscSectionGetStorageSize(rootSection, &ssize);CHKERRQ(ierr);
22269852e123SBarry Smith   ierr = PetscMalloc1(ssize, &rootPoints);CHKERRQ(ierr);
22271fd9873aSMichael Lange   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
22281fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22291fd9873aSMichael Lange     IS              pointIS;
22301fd9873aSMichael Lange     const PetscInt *points;
22311fd9873aSMichael Lange     PetscInt        off, numPoints, p;
22321fd9873aSMichael Lange 
22331fd9873aSMichael Lange     ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr);
22341fd9873aSMichael Lange     ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr);
22351fd9873aSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22361fd9873aSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
22371fd9873aSMichael Lange     for (p = 0; p < numPoints; ++p) {
2238f8987ae8SMichael Lange       if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);}
2239f8987ae8SMichael Lange       else       {l = -1;}
22401fd9873aSMichael Lange       if (l >= 0) {rootPoints[off+p] = remote[l];}
22411fd9873aSMichael Lange       else        {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;}
22421fd9873aSMichael Lange     }
22431fd9873aSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
22441fd9873aSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22451fd9873aSMichael Lange   }
22461fd9873aSMichael Lange   ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr);
22471fd9873aSMichael Lange   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
22481fd9873aSMichael Lange   /* Communicate overlap */
22491fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
22501fd9873aSMichael Lange   ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr);
22511fd9873aSMichael Lange   /* Filter remote contributions (ovLeafPoints) into the overlapSF */
22529852e123SBarry Smith   ierr = PetscSectionGetStorageSize(leafSection, &ssize);CHKERRQ(ierr);
22539852e123SBarry Smith   for (p = 0; p < ssize; p++) {
22541fd9873aSMichael Lange     ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr);
22551fd9873aSMichael Lange   }
22561fd9873aSMichael Lange   ierr = PetscFree(rootPoints);CHKERRQ(ierr);
22571fd9873aSMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
22581fd9873aSMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
22591fd9873aSMichael Lange   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
22601fd9873aSMichael Lange   PetscFunctionReturn(0);
22611fd9873aSMichael Lange }
22621fd9873aSMichael Lange 
2263aa3148a8SMichael Lange /*@
2264aa3148a8SMichael Lange   DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points
2265aa3148a8SMichael Lange 
2266aa3148a8SMichael Lange   Input Parameters:
2267aa3148a8SMichael Lange + dm    - The DM
2268aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots
2269aa3148a8SMichael Lange 
2270aa3148a8SMichael Lange   Output Parameter:
2271aa3148a8SMichael Lange - sf    - The star forest communication context encapsulating the defined mapping
2272aa3148a8SMichael Lange 
2273aa3148a8SMichael Lange   Note: The incoming label is a receiver mapping of remote points to their parent rank.
2274aa3148a8SMichael Lange 
2275aa3148a8SMichael Lange   Level: developer
2276aa3148a8SMichael Lange 
2277aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap
2278aa3148a8SMichael Lange @*/
2279aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf)
2280aa3148a8SMichael Lange {
22819852e123SBarry Smith   PetscMPIInt     rank, size;
228243f7d02bSMichael Lange   PetscInt        n, numRemote, p, numPoints, pStart, pEnd, idx = 0;
2283aa3148a8SMichael Lange   PetscSFNode    *remotePoints;
228443f7d02bSMichael Lange   IS              remoteRootIS;
228543f7d02bSMichael Lange   const PetscInt *remoteRoots;
2286aa3148a8SMichael Lange   PetscErrorCode ierr;
2287aa3148a8SMichael Lange 
2288aa3148a8SMichael Lange   PetscFunctionBegin;
228943f7d02bSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
22909852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
2291aa3148a8SMichael Lange 
22929852e123SBarry Smith   for (numRemote = 0, n = 0; n < size; ++n) {
2293aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
2294aa3148a8SMichael Lange     numRemote += numPoints;
2295aa3148a8SMichael Lange   }
2296aa3148a8SMichael Lange   ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr);
229743f7d02bSMichael Lange   /* Put owned points first */
229843f7d02bSMichael Lange   ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr);
229943f7d02bSMichael Lange   if (numPoints > 0) {
230043f7d02bSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr);
230143f7d02bSMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
230243f7d02bSMichael Lange     for (p = 0; p < numPoints; p++) {
230343f7d02bSMichael Lange       remotePoints[idx].index = remoteRoots[p];
230443f7d02bSMichael Lange       remotePoints[idx].rank = rank;
230543f7d02bSMichael Lange       idx++;
230643f7d02bSMichael Lange     }
230743f7d02bSMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
230843f7d02bSMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
230943f7d02bSMichael Lange   }
231043f7d02bSMichael Lange   /* Now add remote points */
23119852e123SBarry Smith   for (n = 0; n < size; ++n) {
2312aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
231343f7d02bSMichael Lange     if (numPoints <= 0 || n == rank) continue;
2314aa3148a8SMichael Lange     ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr);
2315aa3148a8SMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2316aa3148a8SMichael Lange     for (p = 0; p < numPoints; p++) {
2317aa3148a8SMichael Lange       remotePoints[idx].index = remoteRoots[p];
2318aa3148a8SMichael Lange       remotePoints[idx].rank = n;
2319aa3148a8SMichael Lange       idx++;
2320aa3148a8SMichael Lange     }
2321aa3148a8SMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2322aa3148a8SMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
2323aa3148a8SMichael Lange   }
2324aa3148a8SMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr);
2325aa3148a8SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2326aa3148a8SMichael Lange   ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
232770034214SMatthew G. Knepley   PetscFunctionReturn(0);
232870034214SMatthew G. Knepley }
2329