xref: /petsc/src/dm/impls/plex/plexpartition.c (revision 8f4e72b916ad56bd9e75e6c76185494e86ebfcd0)
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*8f4e72b9SMatthew G. Knepley   PetscInt       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;
61*8f4e72b9SMatthew G. Knepley   PetscInt       *adjCells = NULL, *remoteCells = NULL;
62*8f4e72b9SMatthew G. Knepley   const PetscInt *local;
63*8f4e72b9SMatthew 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);
69532c4e7dSMichael Lange   ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr);
708cfe4c1fSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
718cfe4c1fSMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
72532c4e7dSMichael Lange   /* Build adjacency graph via a section/segbuffer */
73532c4e7dSMichael Lange   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
74532c4e7dSMichael Lange   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
75532c4e7dSMichael Lange   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr);
76532c4e7dSMichael Lange   /* Always use FVM adjacency to create partitioner graph */
77532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
78532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
79532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, PETSC_TRUE);CHKERRQ(ierr);
80532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, PETSC_FALSE);CHKERRQ(ierr);
81f0927f4eSMatthew G. Knepley   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &cellNumbering);CHKERRQ(ierr);
823fa7752dSToby Isaac   if (globalNumbering) {
833fa7752dSToby Isaac     ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr);
843fa7752dSToby Isaac     *globalNumbering = cellNumbering;
853fa7752dSToby Isaac   }
868cfe4c1fSMichael Lange   ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
87*8f4e72b9SMatthew G. Knepley   /* For all boundary faces (including faces adjacent to a ghost cell), record the local cell in adjCells
88*8f4e72b9SMatthew G. Knepley      Broadcast adjCells to remoteCells (to get cells from roots) and Reduce adjCells to remoteCells (to get cells from leaves)
89*8f4e72b9SMatthew G. Knepley    */
90*8f4e72b9SMatthew G. Knepley   ierr = PetscSFGetGraph(dm->sf, &nroots, &nleaves, &local, NULL);CHKERRQ(ierr);
91*8f4e72b9SMatthew G. Knepley   if (nroots >= 0) {
92*8f4e72b9SMatthew G. Knepley     PetscInt fStart, fEnd, f;
93*8f4e72b9SMatthew G. Knepley 
94*8f4e72b9SMatthew G. Knepley     ierr = PetscCalloc2(nroots, &adjCells, nroots, &remoteCells);CHKERRQ(ierr);
95*8f4e72b9SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
96*8f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) adjCells[l] = -3;
97*8f4e72b9SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
98*8f4e72b9SMatthew G. Knepley       const PetscInt *support;
99*8f4e72b9SMatthew G. Knepley       PetscInt        supportSize;
100*8f4e72b9SMatthew G. Knepley 
101*8f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
102*8f4e72b9SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
103*8f4e72b9SMatthew G. Knepley       if (supportSize == 1) adjCells[f] = cellNum[support[0]];
104*8f4e72b9SMatthew G. Knepley       else if (supportSize == 2) {
105*8f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[0], nleaves, local, &p);CHKERRQ(ierr);
106*8f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[1]];
107*8f4e72b9SMatthew G. Knepley         ierr = PetscFindInt(support[1], nleaves, local, &p);CHKERRQ(ierr);
108*8f4e72b9SMatthew G. Knepley         if (p >= 0) adjCells[f] = cellNum[support[0]];
109*8f4e72b9SMatthew G. Knepley       }
110*8f4e72b9SMatthew G. Knepley     }
111*8f4e72b9SMatthew G. Knepley     for (l = 0; l < nroots; ++l) remoteCells[l] = -1;
112*8f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastBegin(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
113*8f4e72b9SMatthew G. Knepley     ierr = PetscSFBcastEnd(dm->sf, MPIU_INT, adjCells, remoteCells);CHKERRQ(ierr);
114*8f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceBegin(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
115*8f4e72b9SMatthew G. Knepley     ierr = PetscSFReduceEnd(dm->sf, MPIU_INT, adjCells, remoteCells, MPI_MAX);CHKERRQ(ierr);
116*8f4e72b9SMatthew G. Knepley   }
117*8f4e72b9SMatthew G. Knepley   /* Combine local and global adjacencies */
1188cfe4c1fSMichael Lange   for (*numVertices = 0, p = pStart; p < pEnd; p++) {
119*8f4e72b9SMatthew G. Knepley     const PetscInt *cone;
120*8f4e72b9SMatthew G. Knepley     PetscInt        coneSize, c;
121*8f4e72b9SMatthew G. Knepley 
1228cfe4c1fSMichael Lange     /* Skip non-owned cells in parallel (ParMetis expects no overlap) */
1238cfe4c1fSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
124*8f4e72b9SMatthew G. Knepley     /* Add remote cells */
125*8f4e72b9SMatthew G. Knepley     if (remoteCells) {
126*8f4e72b9SMatthew G. Knepley       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
127*8f4e72b9SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
128*8f4e72b9SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
129*8f4e72b9SMatthew G. Knepley         if (remoteCells[cone[c]] != -1) {
130*8f4e72b9SMatthew G. Knepley           PetscInt *PETSC_RESTRICT pBuf;
131*8f4e72b9SMatthew G. Knepley 
132*8f4e72b9SMatthew G. Knepley           ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
133*8f4e72b9SMatthew G. Knepley           ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
134*8f4e72b9SMatthew G. Knepley           *pBuf = remoteCells[cone[c]];
135*8f4e72b9SMatthew G. Knepley         }
136*8f4e72b9SMatthew G. Knepley       }
137*8f4e72b9SMatthew G. Knepley     }
138*8f4e72b9SMatthew G. Knepley     /* Add local cells */
139532c4e7dSMichael Lange     adjSize = PETSC_DETERMINE;
140532c4e7dSMichael Lange     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
141532c4e7dSMichael Lange     for (a = 0; a < adjSize; ++a) {
142532c4e7dSMichael Lange       const PetscInt point = adj[a];
143532c4e7dSMichael Lange       if (point != p && pStart <= point && point < pEnd) {
144532c4e7dSMichael Lange         PetscInt *PETSC_RESTRICT pBuf;
145532c4e7dSMichael Lange         ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
146532c4e7dSMichael Lange         ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
147*8f4e72b9SMatthew G. Knepley         *pBuf = cellNum[point];
148532c4e7dSMichael Lange       }
149532c4e7dSMichael Lange     }
1508cfe4c1fSMichael Lange     (*numVertices)++;
151532c4e7dSMichael Lange   }
152*8f4e72b9SMatthew G. Knepley   ierr = PetscFree2(adjCells, remoteCells);CHKERRQ(ierr);
153532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, useCone);CHKERRQ(ierr);
154532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, useClosure);CHKERRQ(ierr);
155532c4e7dSMichael Lange   /* Derive CSR graph from section/segbuffer */
156532c4e7dSMichael Lange   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
157532c4e7dSMichael Lange   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
158389e55d8SMichael Lange   ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr);
15943f7d02bSMichael Lange   for (idx = 0, p = pStart; p < pEnd; p++) {
16043f7d02bSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
16143f7d02bSMichael Lange     ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr);
16243f7d02bSMichael Lange   }
163532c4e7dSMichael Lange   vOffsets[*numVertices] = size;
164532c4e7dSMichael Lange   if (offsets) *offsets = vOffsets;
165389e55d8SMichael Lange   ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr);
1668cfe4c1fSMichael Lange   ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
167f0927f4eSMatthew G. Knepley   ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr);
168389e55d8SMichael Lange   if (adjacency) *adjacency = graph;
169532c4e7dSMichael Lange   /* Clean up */
170532c4e7dSMichael Lange   ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr);
171532c4e7dSMichael Lange   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
172532c4e7dSMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
173532c4e7dSMichael Lange   PetscFunctionReturn(0);
174532c4e7dSMichael Lange }
175532c4e7dSMichael Lange 
176d5577e40SMatthew G. Knepley /*@C
177d5577e40SMatthew G. Knepley   DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format.
178d5577e40SMatthew G. Knepley 
179d5577e40SMatthew G. Knepley   Collective
180d5577e40SMatthew G. Knepley 
181d5577e40SMatthew G. Knepley   Input Arguments:
182d5577e40SMatthew G. Knepley + dm - The DMPlex
183d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0)
184d5577e40SMatthew G. Knepley 
185d5577e40SMatthew G. Knepley   Output Arguments:
186d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh.
187d5577e40SMatthew G. Knepley . offsets     - The offset to the adjacency list for each cell
188d5577e40SMatthew G. Knepley - adjacency   - The adjacency list for all cells
189d5577e40SMatthew G. Knepley 
190d5577e40SMatthew G. Knepley   Note: This is suitable for input to a mesh partitioner like ParMetis.
191d5577e40SMatthew G. Knepley 
192d5577e40SMatthew G. Knepley   Level: advanced
193d5577e40SMatthew G. Knepley 
194d5577e40SMatthew G. Knepley .seealso: DMPlexCreate()
195d5577e40SMatthew G. Knepley @*/
19670034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency)
19770034214SMatthew G. Knepley {
19870034214SMatthew G. Knepley   const PetscInt maxFaceCases = 30;
19970034214SMatthew G. Knepley   PetscInt       numFaceCases = 0;
20070034214SMatthew G. Knepley   PetscInt       numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */
20170034214SMatthew G. Knepley   PetscInt      *off, *adj;
20270034214SMatthew G. Knepley   PetscInt      *neighborCells = NULL;
20370034214SMatthew G. Knepley   PetscInt       dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell;
20470034214SMatthew G. Knepley   PetscErrorCode ierr;
20570034214SMatthew G. Knepley 
20670034214SMatthew G. Knepley   PetscFunctionBegin;
20770034214SMatthew G. Knepley   /* For parallel partitioning, I think you have to communicate supports */
208c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
20970034214SMatthew G. Knepley   cellDim = dim - cellHeight;
21070034214SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
21170034214SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
21270034214SMatthew G. Knepley   if (cEnd - cStart == 0) {
21370034214SMatthew G. Knepley     if (numVertices) *numVertices = 0;
21470034214SMatthew G. Knepley     if (offsets)   *offsets   = NULL;
21570034214SMatthew G. Knepley     if (adjacency) *adjacency = NULL;
21670034214SMatthew G. Knepley     PetscFunctionReturn(0);
21770034214SMatthew G. Knepley   }
21870034214SMatthew G. Knepley   numCells  = cEnd - cStart;
21970034214SMatthew G. Knepley   faceDepth = depth - cellHeight;
22070034214SMatthew G. Knepley   if (dim == depth) {
22170034214SMatthew G. Knepley     PetscInt f, fStart, fEnd;
22270034214SMatthew G. Knepley 
22370034214SMatthew G. Knepley     ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
22470034214SMatthew G. Knepley     /* Count neighboring cells */
22570034214SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
22670034214SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
22770034214SMatthew G. Knepley       const PetscInt *support;
22870034214SMatthew G. Knepley       PetscInt        supportSize;
22970034214SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
23070034214SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
23170034214SMatthew G. Knepley       if (supportSize == 2) {
2329ffc88e4SToby Isaac         PetscInt numChildren;
2339ffc88e4SToby Isaac 
2349ffc88e4SToby Isaac         ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2359ffc88e4SToby Isaac         if (!numChildren) {
23670034214SMatthew G. Knepley           ++off[support[0]-cStart+1];
23770034214SMatthew G. Knepley           ++off[support[1]-cStart+1];
23870034214SMatthew G. Knepley         }
23970034214SMatthew G. Knepley       }
2409ffc88e4SToby Isaac     }
24170034214SMatthew G. Knepley     /* Prefix sum */
24270034214SMatthew G. Knepley     for (c = 1; c <= numCells; ++c) off[c] += off[c-1];
24370034214SMatthew G. Knepley     if (adjacency) {
24470034214SMatthew G. Knepley       PetscInt *tmp;
24570034214SMatthew G. Knepley 
24670034214SMatthew G. Knepley       ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
247854ce69bSBarry Smith       ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr);
24870034214SMatthew G. Knepley       ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr);
24970034214SMatthew G. Knepley       /* Get neighboring cells */
25070034214SMatthew G. Knepley       for (f = fStart; f < fEnd; ++f) {
25170034214SMatthew G. Knepley         const PetscInt *support;
25270034214SMatthew G. Knepley         PetscInt        supportSize;
25370034214SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
25470034214SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
25570034214SMatthew G. Knepley         if (supportSize == 2) {
2569ffc88e4SToby Isaac           PetscInt numChildren;
2579ffc88e4SToby Isaac 
2589ffc88e4SToby Isaac           ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2599ffc88e4SToby Isaac           if (!numChildren) {
26070034214SMatthew G. Knepley             adj[tmp[support[0]-cStart]++] = support[1];
26170034214SMatthew G. Knepley             adj[tmp[support[1]-cStart]++] = support[0];
26270034214SMatthew G. Knepley           }
26370034214SMatthew G. Knepley         }
2649ffc88e4SToby Isaac       }
26570034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
26670034214SMatthew 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);
26770034214SMatthew G. Knepley #endif
26870034214SMatthew G. Knepley       ierr = PetscFree(tmp);CHKERRQ(ierr);
26970034214SMatthew G. Knepley     }
27070034214SMatthew G. Knepley     if (numVertices) *numVertices = numCells;
27170034214SMatthew G. Knepley     if (offsets)   *offsets   = off;
27270034214SMatthew G. Knepley     if (adjacency) *adjacency = adj;
27370034214SMatthew G. Knepley     PetscFunctionReturn(0);
27470034214SMatthew G. Knepley   }
27570034214SMatthew G. Knepley   /* Setup face recognition */
27670034214SMatthew G. Knepley   if (faceDepth == 1) {
27770034214SMatthew 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 */
27870034214SMatthew G. Knepley 
27970034214SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
28070034214SMatthew G. Knepley       PetscInt corners;
28170034214SMatthew G. Knepley 
28270034214SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr);
28370034214SMatthew G. Knepley       if (!cornersSeen[corners]) {
28470034214SMatthew G. Knepley         PetscInt nFV;
28570034214SMatthew G. Knepley 
28670034214SMatthew G. Knepley         if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases");
28770034214SMatthew G. Knepley         cornersSeen[corners] = 1;
28870034214SMatthew G. Knepley 
28970034214SMatthew G. Knepley         ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr);
29070034214SMatthew G. Knepley 
29170034214SMatthew G. Knepley         numFaceVertices[numFaceCases++] = nFV;
29270034214SMatthew G. Knepley       }
29370034214SMatthew G. Knepley     }
29470034214SMatthew G. Knepley   }
29570034214SMatthew G. Knepley   ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
29670034214SMatthew G. Knepley   /* Count neighboring cells */
29770034214SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
29870034214SMatthew G. Knepley     PetscInt numNeighbors = PETSC_DETERMINE, n;
29970034214SMatthew G. Knepley 
3008b0b4c70SToby Isaac     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
30170034214SMatthew G. Knepley     /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
30270034214SMatthew G. Knepley     for (n = 0; n < numNeighbors; ++n) {
30370034214SMatthew G. Knepley       PetscInt        cellPair[2];
30470034214SMatthew G. Knepley       PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
30570034214SMatthew G. Knepley       PetscInt        meetSize = 0;
30670034214SMatthew G. Knepley       const PetscInt *meet    = NULL;
30770034214SMatthew G. Knepley 
30870034214SMatthew G. Knepley       cellPair[0] = cell; cellPair[1] = neighborCells[n];
30970034214SMatthew G. Knepley       if (cellPair[0] == cellPair[1]) continue;
31070034214SMatthew G. Knepley       if (!found) {
31170034214SMatthew G. Knepley         ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
31270034214SMatthew G. Knepley         if (meetSize) {
31370034214SMatthew G. Knepley           PetscInt f;
31470034214SMatthew G. Knepley 
31570034214SMatthew G. Knepley           for (f = 0; f < numFaceCases; ++f) {
31670034214SMatthew G. Knepley             if (numFaceVertices[f] == meetSize) {
31770034214SMatthew G. Knepley               found = PETSC_TRUE;
31870034214SMatthew G. Knepley               break;
31970034214SMatthew G. Knepley             }
32070034214SMatthew G. Knepley           }
32170034214SMatthew G. Knepley         }
32270034214SMatthew G. Knepley         ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
32370034214SMatthew G. Knepley       }
32470034214SMatthew G. Knepley       if (found) ++off[cell-cStart+1];
32570034214SMatthew G. Knepley     }
32670034214SMatthew G. Knepley   }
32770034214SMatthew G. Knepley   /* Prefix sum */
32870034214SMatthew G. Knepley   for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1];
32970034214SMatthew G. Knepley 
33070034214SMatthew G. Knepley   if (adjacency) {
33170034214SMatthew G. Knepley     ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
33270034214SMatthew G. Knepley     /* Get neighboring cells */
33370034214SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
33470034214SMatthew G. Knepley       PetscInt numNeighbors = PETSC_DETERMINE, n;
33570034214SMatthew G. Knepley       PetscInt cellOffset   = 0;
33670034214SMatthew G. Knepley 
3378b0b4c70SToby Isaac       ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
33870034214SMatthew G. Knepley       /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
33970034214SMatthew G. Knepley       for (n = 0; n < numNeighbors; ++n) {
34070034214SMatthew G. Knepley         PetscInt        cellPair[2];
34170034214SMatthew G. Knepley         PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
34270034214SMatthew G. Knepley         PetscInt        meetSize = 0;
34370034214SMatthew G. Knepley         const PetscInt *meet    = NULL;
34470034214SMatthew G. Knepley 
34570034214SMatthew G. Knepley         cellPair[0] = cell; cellPair[1] = neighborCells[n];
34670034214SMatthew G. Knepley         if (cellPair[0] == cellPair[1]) continue;
34770034214SMatthew G. Knepley         if (!found) {
34870034214SMatthew G. Knepley           ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
34970034214SMatthew G. Knepley           if (meetSize) {
35070034214SMatthew G. Knepley             PetscInt f;
35170034214SMatthew G. Knepley 
35270034214SMatthew G. Knepley             for (f = 0; f < numFaceCases; ++f) {
35370034214SMatthew G. Knepley               if (numFaceVertices[f] == meetSize) {
35470034214SMatthew G. Knepley                 found = PETSC_TRUE;
35570034214SMatthew G. Knepley                 break;
35670034214SMatthew G. Knepley               }
35770034214SMatthew G. Knepley             }
35870034214SMatthew G. Knepley           }
35970034214SMatthew G. Knepley           ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
36070034214SMatthew G. Knepley         }
36170034214SMatthew G. Knepley         if (found) {
36270034214SMatthew G. Knepley           adj[off[cell-cStart]+cellOffset] = neighborCells[n];
36370034214SMatthew G. Knepley           ++cellOffset;
36470034214SMatthew G. Knepley         }
36570034214SMatthew G. Knepley       }
36670034214SMatthew G. Knepley     }
36770034214SMatthew G. Knepley   }
36870034214SMatthew G. Knepley   ierr = PetscFree(neighborCells);CHKERRQ(ierr);
36970034214SMatthew G. Knepley   if (numVertices) *numVertices = numCells;
37070034214SMatthew G. Knepley   if (offsets)   *offsets   = off;
37170034214SMatthew G. Knepley   if (adjacency) *adjacency = adj;
37270034214SMatthew G. Knepley   PetscFunctionReturn(0);
37370034214SMatthew G. Knepley }
37470034214SMatthew G. Knepley 
37577623264SMatthew G. Knepley /*@C
37677623264SMatthew G. Knepley   PetscPartitionerRegister - Adds a new PetscPartitioner implementation
37777623264SMatthew G. Knepley 
37877623264SMatthew G. Knepley   Not Collective
37977623264SMatthew G. Knepley 
38077623264SMatthew G. Knepley   Input Parameters:
38177623264SMatthew G. Knepley + name        - The name of a new user-defined creation routine
38277623264SMatthew G. Knepley - create_func - The creation routine itself
38377623264SMatthew G. Knepley 
38477623264SMatthew G. Knepley   Notes:
38577623264SMatthew G. Knepley   PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners
38677623264SMatthew G. Knepley 
38777623264SMatthew G. Knepley   Sample usage:
38877623264SMatthew G. Knepley .vb
38977623264SMatthew G. Knepley     PetscPartitionerRegister("my_part", MyPetscPartitionerCreate);
39077623264SMatthew G. Knepley .ve
39177623264SMatthew G. Knepley 
39277623264SMatthew G. Knepley   Then, your PetscPartitioner type can be chosen with the procedural interface via
39377623264SMatthew G. Knepley .vb
39477623264SMatthew G. Knepley     PetscPartitionerCreate(MPI_Comm, PetscPartitioner *);
39577623264SMatthew G. Knepley     PetscPartitionerSetType(PetscPartitioner, "my_part");
39677623264SMatthew G. Knepley .ve
39777623264SMatthew G. Knepley    or at runtime via the option
39877623264SMatthew G. Knepley .vb
39977623264SMatthew G. Knepley     -petscpartitioner_type my_part
40077623264SMatthew G. Knepley .ve
40177623264SMatthew G. Knepley 
40277623264SMatthew G. Knepley   Level: advanced
40377623264SMatthew G. Knepley 
40477623264SMatthew G. Knepley .keywords: PetscPartitioner, register
40577623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy()
40677623264SMatthew G. Knepley 
40777623264SMatthew G. Knepley @*/
40877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner))
40977623264SMatthew G. Knepley {
41077623264SMatthew G. Knepley   PetscErrorCode ierr;
41177623264SMatthew G. Knepley 
41277623264SMatthew G. Knepley   PetscFunctionBegin;
41377623264SMatthew G. Knepley   ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr);
41477623264SMatthew G. Knepley   PetscFunctionReturn(0);
41577623264SMatthew G. Knepley }
41677623264SMatthew G. Knepley 
41777623264SMatthew G. Knepley /*@C
41877623264SMatthew G. Knepley   PetscPartitionerSetType - Builds a particular PetscPartitioner
41977623264SMatthew G. Knepley 
42077623264SMatthew G. Knepley   Collective on PetscPartitioner
42177623264SMatthew G. Knepley 
42277623264SMatthew G. Knepley   Input Parameters:
42377623264SMatthew G. Knepley + part - The PetscPartitioner object
42477623264SMatthew G. Knepley - name - The kind of partitioner
42577623264SMatthew G. Knepley 
42677623264SMatthew G. Knepley   Options Database Key:
42777623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types
42877623264SMatthew G. Knepley 
42977623264SMatthew G. Knepley   Level: intermediate
43077623264SMatthew G. Knepley 
43177623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type
43277623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate()
43377623264SMatthew G. Knepley @*/
43477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name)
43577623264SMatthew G. Knepley {
43677623264SMatthew G. Knepley   PetscErrorCode (*r)(PetscPartitioner);
43777623264SMatthew G. Knepley   PetscBool      match;
43877623264SMatthew G. Knepley   PetscErrorCode ierr;
43977623264SMatthew G. Knepley 
44077623264SMatthew G. Knepley   PetscFunctionBegin;
44177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
44277623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr);
44377623264SMatthew G. Knepley   if (match) PetscFunctionReturn(0);
44477623264SMatthew G. Knepley 
4450f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
44677623264SMatthew G. Knepley   ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr);
44777623264SMatthew G. Knepley   if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name);
44877623264SMatthew G. Knepley 
44977623264SMatthew G. Knepley   if (part->ops->destroy) {
45077623264SMatthew G. Knepley     ierr              = (*part->ops->destroy)(part);CHKERRQ(ierr);
45177623264SMatthew G. Knepley   }
45209161815SVaclav Hapla   ierr = PetscMemzero(part->ops, sizeof(struct _PetscPartitionerOps));CHKERRQ(ierr);
45377623264SMatthew G. Knepley   ierr = (*r)(part);CHKERRQ(ierr);
45477623264SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr);
45577623264SMatthew G. Knepley   PetscFunctionReturn(0);
45677623264SMatthew G. Knepley }
45777623264SMatthew G. Knepley 
45877623264SMatthew G. Knepley /*@C
45977623264SMatthew G. Knepley   PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object.
46077623264SMatthew G. Knepley 
46177623264SMatthew G. Knepley   Not Collective
46277623264SMatthew G. Knepley 
46377623264SMatthew G. Knepley   Input Parameter:
46477623264SMatthew G. Knepley . part - The PetscPartitioner
46577623264SMatthew G. Knepley 
46677623264SMatthew G. Knepley   Output Parameter:
46777623264SMatthew G. Knepley . name - The PetscPartitioner type name
46877623264SMatthew G. Knepley 
46977623264SMatthew G. Knepley   Level: intermediate
47077623264SMatthew G. Knepley 
47177623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name
47277623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate()
47377623264SMatthew G. Knepley @*/
47477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name)
47577623264SMatthew G. Knepley {
47677623264SMatthew G. Knepley   PetscErrorCode ierr;
47777623264SMatthew G. Knepley 
47877623264SMatthew G. Knepley   PetscFunctionBegin;
47977623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
48077623264SMatthew G. Knepley   PetscValidPointer(name, 2);
4810f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
48277623264SMatthew G. Knepley   *name = ((PetscObject) part)->type_name;
48377623264SMatthew G. Knepley   PetscFunctionReturn(0);
48477623264SMatthew G. Knepley }
48577623264SMatthew G. Knepley 
48677623264SMatthew G. Knepley /*@C
48777623264SMatthew G. Knepley   PetscPartitionerView - Views a PetscPartitioner
48877623264SMatthew G. Knepley 
48977623264SMatthew G. Knepley   Collective on PetscPartitioner
49077623264SMatthew G. Knepley 
49177623264SMatthew G. Knepley   Input Parameter:
49277623264SMatthew G. Knepley + part - the PetscPartitioner object to view
49377623264SMatthew G. Knepley - v    - the viewer
49477623264SMatthew G. Knepley 
49577623264SMatthew G. Knepley   Level: developer
49677623264SMatthew G. Knepley 
49777623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy()
49877623264SMatthew G. Knepley @*/
49977623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v)
50077623264SMatthew G. Knepley {
5012abdaa70SMatthew G. Knepley   PetscInt       size;
5022abdaa70SMatthew G. Knepley   PetscBool      isascii;
50377623264SMatthew G. Knepley   PetscErrorCode ierr;
50477623264SMatthew G. Knepley 
50577623264SMatthew G. Knepley   PetscFunctionBegin;
50677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
50777623264SMatthew G. Knepley   if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);}
5082abdaa70SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
5092abdaa70SMatthew G. Knepley   if (isascii) {
5102abdaa70SMatthew G. Knepley     ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
5112abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "Graph Partitioner: %D MPI Process%s\n", size, size > 1 ? "es" : "");CHKERRQ(ierr);
5122abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "  type: %s\n", part->hdr.type_name);CHKERRQ(ierr);
5132abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
5142abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "edge cut: %D\n", part->edgeCut);CHKERRQ(ierr);
5152abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "balance:  %.2g\n", part->balance);CHKERRQ(ierr);
5162abdaa70SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
5172abdaa70SMatthew G. Knepley   }
51877623264SMatthew G. Knepley   if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);}
51977623264SMatthew G. Knepley   PetscFunctionReturn(0);
52077623264SMatthew G. Knepley }
52177623264SMatthew G. Knepley 
522a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType)
523a0058e54SToby Isaac {
524a0058e54SToby Isaac   PetscFunctionBegin;
525a0058e54SToby Isaac   if (!currentType) {
526a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO)
527a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERCHACO;
528a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS)
529a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERPARMETIS;
530137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH)
531137cd93aSLisandro Dalcin     *defaultType = PETSCPARTITIONERPTSCOTCH;
532a0058e54SToby Isaac #else
533a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERSIMPLE;
534a0058e54SToby Isaac #endif
535a0058e54SToby Isaac   } else {
536a0058e54SToby Isaac     *defaultType = currentType;
537a0058e54SToby Isaac   }
538a0058e54SToby Isaac   PetscFunctionReturn(0);
539a0058e54SToby Isaac }
540a0058e54SToby Isaac 
54177623264SMatthew G. Knepley /*@
54277623264SMatthew G. Knepley   PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database
54377623264SMatthew G. Knepley 
54477623264SMatthew G. Knepley   Collective on PetscPartitioner
54577623264SMatthew G. Knepley 
54677623264SMatthew G. Knepley   Input Parameter:
54777623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for
54877623264SMatthew G. Knepley 
54977623264SMatthew G. Knepley   Level: developer
55077623264SMatthew G. Knepley 
55177623264SMatthew G. Knepley .seealso: PetscPartitionerView()
55277623264SMatthew G. Knepley @*/
55377623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part)
55477623264SMatthew G. Knepley {
5556bb9daa8SLisandro Dalcin   const char    *defaultType;
5566bb9daa8SLisandro Dalcin   char           name[256];
5576bb9daa8SLisandro Dalcin   PetscBool      flg;
55877623264SMatthew G. Knepley   PetscErrorCode ierr;
55977623264SMatthew G. Knepley 
56077623264SMatthew G. Knepley   PetscFunctionBegin;
56177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
5626bb9daa8SLisandro Dalcin   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
5636bb9daa8SLisandro Dalcin   ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr);
56477623264SMatthew G. Knepley   ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr);
5656bb9daa8SLisandro Dalcin   ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, sizeof(name), &flg);CHKERRQ(ierr);
5666bb9daa8SLisandro Dalcin   if (flg) {
5676bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr);
5686bb9daa8SLisandro Dalcin   } else if (!((PetscObject) part)->type_name) {
5696bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr);
5706bb9daa8SLisandro Dalcin   }
5716bb9daa8SLisandro Dalcin   if (part->ops->setfromoptions) {
5726bb9daa8SLisandro Dalcin     ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr);
5736bb9daa8SLisandro Dalcin   }
57477623264SMatthew G. Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5750633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr);
57677623264SMatthew G. Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
57777623264SMatthew G. Knepley   PetscFunctionReturn(0);
57877623264SMatthew G. Knepley }
57977623264SMatthew G. Knepley 
58077623264SMatthew G. Knepley /*@C
58177623264SMatthew G. Knepley   PetscPartitionerSetUp - Construct data structures for the PetscPartitioner
58277623264SMatthew G. Knepley 
58377623264SMatthew G. Knepley   Collective on PetscPartitioner
58477623264SMatthew G. Knepley 
58577623264SMatthew G. Knepley   Input Parameter:
58677623264SMatthew G. Knepley . part - the PetscPartitioner object to setup
58777623264SMatthew G. Knepley 
58877623264SMatthew G. Knepley   Level: developer
58977623264SMatthew G. Knepley 
59077623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy()
59177623264SMatthew G. Knepley @*/
59277623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part)
59377623264SMatthew G. Knepley {
59477623264SMatthew G. Knepley   PetscErrorCode ierr;
59577623264SMatthew G. Knepley 
59677623264SMatthew G. Knepley   PetscFunctionBegin;
59777623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
59877623264SMatthew G. Knepley   if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);}
59977623264SMatthew G. Knepley   PetscFunctionReturn(0);
60077623264SMatthew G. Knepley }
60177623264SMatthew G. Knepley 
60277623264SMatthew G. Knepley /*@
60377623264SMatthew G. Knepley   PetscPartitionerDestroy - Destroys a PetscPartitioner object
60477623264SMatthew G. Knepley 
60577623264SMatthew G. Knepley   Collective on PetscPartitioner
60677623264SMatthew G. Knepley 
60777623264SMatthew G. Knepley   Input Parameter:
60877623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy
60977623264SMatthew G. Knepley 
61077623264SMatthew G. Knepley   Level: developer
61177623264SMatthew G. Knepley 
61277623264SMatthew G. Knepley .seealso: PetscPartitionerView()
61377623264SMatthew G. Knepley @*/
61477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part)
61577623264SMatthew G. Knepley {
61677623264SMatthew G. Knepley   PetscErrorCode ierr;
61777623264SMatthew G. Knepley 
61877623264SMatthew G. Knepley   PetscFunctionBegin;
61977623264SMatthew G. Knepley   if (!*part) PetscFunctionReturn(0);
62077623264SMatthew G. Knepley   PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1);
62177623264SMatthew G. Knepley 
62277623264SMatthew G. Knepley   if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);}
62377623264SMatthew G. Knepley   ((PetscObject) (*part))->refct = 0;
62477623264SMatthew G. Knepley 
62577623264SMatthew G. Knepley   if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);}
62677623264SMatthew G. Knepley   ierr = PetscHeaderDestroy(part);CHKERRQ(ierr);
62777623264SMatthew G. Knepley   PetscFunctionReturn(0);
62877623264SMatthew G. Knepley }
62977623264SMatthew G. Knepley 
63077623264SMatthew G. Knepley /*@
63177623264SMatthew G. Knepley   PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType().
63277623264SMatthew G. Knepley 
63377623264SMatthew G. Knepley   Collective on MPI_Comm
63477623264SMatthew G. Knepley 
63577623264SMatthew G. Knepley   Input Parameter:
63677623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object
63777623264SMatthew G. Knepley 
63877623264SMatthew G. Knepley   Output Parameter:
63977623264SMatthew G. Knepley . part - The PetscPartitioner object
64077623264SMatthew G. Knepley 
64177623264SMatthew G. Knepley   Level: beginner
64277623264SMatthew G. Knepley 
643dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER
64477623264SMatthew G. Knepley @*/
64577623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part)
64677623264SMatthew G. Knepley {
64777623264SMatthew G. Knepley   PetscPartitioner p;
648a0058e54SToby Isaac   const char       *partitionerType = NULL;
64977623264SMatthew G. Knepley   PetscErrorCode   ierr;
65077623264SMatthew G. Knepley 
65177623264SMatthew G. Knepley   PetscFunctionBegin;
65277623264SMatthew G. Knepley   PetscValidPointer(part, 2);
65377623264SMatthew G. Knepley   *part = NULL;
65483cde681SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
65577623264SMatthew G. Knepley 
65673107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr);
657a0058e54SToby Isaac   ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr);
658a0058e54SToby Isaac   ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr);
65977623264SMatthew G. Knepley 
66072379da4SMatthew G. Knepley   p->edgeCut = 0;
66172379da4SMatthew G. Knepley   p->balance = 0.0;
66272379da4SMatthew G. Knepley 
66377623264SMatthew G. Knepley   *part = p;
66477623264SMatthew G. Knepley   PetscFunctionReturn(0);
66577623264SMatthew G. Knepley }
66677623264SMatthew G. Knepley 
66777623264SMatthew G. Knepley /*@
66877623264SMatthew G. Knepley   PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh
66977623264SMatthew G. Knepley 
67077623264SMatthew G. Knepley   Collective on DM
67177623264SMatthew G. Knepley 
67277623264SMatthew G. Knepley   Input Parameters:
67377623264SMatthew G. Knepley + part    - The PetscPartitioner
674f8987ae8SMichael Lange - dm      - The mesh DM
67577623264SMatthew G. Knepley 
67677623264SMatthew G. Knepley   Output Parameters:
67777623264SMatthew G. Knepley + partSection     - The PetscSection giving the division of points by partition
678f8987ae8SMichael Lange - partition       - The list of points by partition
67977623264SMatthew G. Knepley 
68077623264SMatthew G. Knepley   Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight()
68177623264SMatthew G. Knepley 
68277623264SMatthew G. Knepley   Level: developer
68377623264SMatthew G. Knepley 
68477623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate()
6854b15ede2SMatthew G. Knepley @*/
686f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition)
68777623264SMatthew G. Knepley {
68877623264SMatthew G. Knepley   PetscMPIInt    size;
68977623264SMatthew G. Knepley   PetscErrorCode ierr;
69077623264SMatthew G. Knepley 
69177623264SMatthew G. Knepley   PetscFunctionBegin;
69277623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
69377623264SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
69477623264SMatthew G. Knepley   PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4);
69577623264SMatthew G. Knepley   PetscValidPointer(partition, 5);
69677623264SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
69777623264SMatthew G. Knepley   if (size == 1) {
69877623264SMatthew G. Knepley     PetscInt *points;
69977623264SMatthew G. Knepley     PetscInt  cStart, cEnd, c;
70077623264SMatthew G. Knepley 
70177623264SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
70277623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr);
70377623264SMatthew G. Knepley     ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr);
70477623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
70577623264SMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr);
70677623264SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) points[c] = c;
70777623264SMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
70877623264SMatthew G. Knepley     PetscFunctionReturn(0);
70977623264SMatthew G. Knepley   }
71077623264SMatthew G. Knepley   if (part->height == 0) {
71177623264SMatthew G. Knepley     PetscInt numVertices;
71277623264SMatthew G. Knepley     PetscInt *start     = NULL;
71377623264SMatthew G. Knepley     PetscInt *adjacency = NULL;
7143fa7752dSToby Isaac     IS       globalNumbering;
71577623264SMatthew G. Knepley 
7163fa7752dSToby Isaac     ierr = DMPlexCreatePartitionerGraph(dm, 0, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr);
71777623264SMatthew G. Knepley     if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type");
71877623264SMatthew G. Knepley     ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
71977623264SMatthew G. Knepley     ierr = PetscFree(start);CHKERRQ(ierr);
72077623264SMatthew G. Knepley     ierr = PetscFree(adjacency);CHKERRQ(ierr);
7213fa7752dSToby Isaac     if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */
7223fa7752dSToby Isaac       const PetscInt *globalNum;
7233fa7752dSToby Isaac       const PetscInt *partIdx;
7243fa7752dSToby Isaac       PetscInt *map, cStart, cEnd;
7253fa7752dSToby Isaac       PetscInt *adjusted, i, localSize, offset;
7263fa7752dSToby Isaac       IS    newPartition;
7273fa7752dSToby Isaac 
7283fa7752dSToby Isaac       ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr);
7293fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr);
7303fa7752dSToby Isaac       ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7313fa7752dSToby Isaac       ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr);
7323fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr);
7333fa7752dSToby Isaac       ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
7343fa7752dSToby Isaac       for (i = cStart, offset = 0; i < cEnd; i++) {
7353fa7752dSToby Isaac         if (globalNum[i - cStart] >= 0) map[offset++] = i;
7363fa7752dSToby Isaac       }
7373fa7752dSToby Isaac       for (i = 0; i < localSize; i++) {
7383fa7752dSToby Isaac         adjusted[i] = map[partIdx[i]];
7393fa7752dSToby Isaac       }
7403fa7752dSToby Isaac       ierr = PetscFree(map);CHKERRQ(ierr);
7413fa7752dSToby Isaac       ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr);
7423fa7752dSToby Isaac       ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
7433fa7752dSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr);
7443fa7752dSToby Isaac       ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr);
7453fa7752dSToby Isaac       ierr = ISDestroy(partition);CHKERRQ(ierr);
7463fa7752dSToby Isaac       *partition = newPartition;
7473fa7752dSToby Isaac     }
74877623264SMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height);
7492abdaa70SMatthew G. Knepley   ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr);
75077623264SMatthew G. Knepley   PetscFunctionReturn(0);
75177623264SMatthew G. Knepley }
75277623264SMatthew G. Knepley 
753d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part)
75477623264SMatthew G. Knepley {
75577623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
75677623264SMatthew G. Knepley   PetscErrorCode          ierr;
75777623264SMatthew G. Knepley 
75877623264SMatthew G. Knepley   PetscFunctionBegin;
75977623264SMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
76077623264SMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
76177623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
76277623264SMatthew G. Knepley   PetscFunctionReturn(0);
76377623264SMatthew G. Knepley }
76477623264SMatthew G. Knepley 
765d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer)
76677623264SMatthew G. Knepley {
767077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
76877623264SMatthew G. Knepley   PetscErrorCode          ierr;
76977623264SMatthew G. Knepley 
77077623264SMatthew G. Knepley   PetscFunctionBegin;
771077101c0SMatthew G. Knepley   if (p->random) {
772077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
773077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr);
774077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
775077101c0SMatthew G. Knepley   }
77677623264SMatthew G. Knepley   PetscFunctionReturn(0);
77777623264SMatthew G. Knepley }
77877623264SMatthew G. Knepley 
779d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer)
78077623264SMatthew G. Knepley {
78177623264SMatthew G. Knepley   PetscBool      iascii;
78277623264SMatthew G. Knepley   PetscErrorCode ierr;
78377623264SMatthew G. Knepley 
78477623264SMatthew G. Knepley   PetscFunctionBegin;
78577623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
78677623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
78777623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
78877623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);}
78977623264SMatthew G. Knepley   PetscFunctionReturn(0);
79077623264SMatthew G. Knepley }
79177623264SMatthew G. Knepley 
792d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
793077101c0SMatthew G. Knepley {
794077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
795077101c0SMatthew G. Knepley   PetscErrorCode          ierr;
796077101c0SMatthew G. Knepley 
797077101c0SMatthew G. Knepley   PetscFunctionBegin;
798077101c0SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");CHKERRQ(ierr);
799077101c0SMatthew G. Knepley   ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr);
800077101c0SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
801077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
802077101c0SMatthew G. Knepley }
803077101c0SMatthew G. Knepley 
804d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
80577623264SMatthew G. Knepley {
80677623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
80777623264SMatthew G. Knepley   PetscInt                np;
80877623264SMatthew G. Knepley   PetscErrorCode          ierr;
80977623264SMatthew G. Knepley 
81077623264SMatthew G. Knepley   PetscFunctionBegin;
811077101c0SMatthew G. Knepley   if (p->random) {
812077101c0SMatthew G. Knepley     PetscRandom r;
813aa1d5631SMatthew G. Knepley     PetscInt   *sizes, *points, v, p;
814aa1d5631SMatthew G. Knepley     PetscMPIInt rank;
815077101c0SMatthew G. Knepley 
816aa1d5631SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
817077101c0SMatthew G. Knepley     ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
818c717d290SMatthew G. Knepley     ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr);
819077101c0SMatthew G. Knepley     ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr);
820077101c0SMatthew G. Knepley     ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr);
821aa1d5631SMatthew G. Knepley     for (v = 0; v < numVertices; ++v) {points[v] = v;}
822ac9a96f1SMichael Lange     for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);}
823aa1d5631SMatthew G. Knepley     for (v = numVertices-1; v > 0; --v) {
824077101c0SMatthew G. Knepley       PetscReal val;
825aa1d5631SMatthew G. Knepley       PetscInt  w, tmp;
826077101c0SMatthew G. Knepley 
827aa1d5631SMatthew G. Knepley       ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr);
828077101c0SMatthew G. Knepley       ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr);
829aa1d5631SMatthew G. Knepley       w    = PetscFloorReal(val);
830aa1d5631SMatthew G. Knepley       tmp       = points[v];
831aa1d5631SMatthew G. Knepley       points[v] = points[w];
832aa1d5631SMatthew G. Knepley       points[w] = tmp;
833077101c0SMatthew G. Knepley     }
834077101c0SMatthew G. Knepley     ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
835077101c0SMatthew G. Knepley     ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr);
836077101c0SMatthew G. Knepley     ierr = PetscFree2(sizes, points);CHKERRQ(ierr);
837077101c0SMatthew G. Knepley   }
838c717d290SMatthew G. Knepley   if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()");
83977623264SMatthew G. Knepley   ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr);
84077623264SMatthew G. Knepley   if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np);
84177623264SMatthew G. Knepley   ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr);
84277623264SMatthew G. Knepley   if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np);
8435680f57bSMatthew G. Knepley   ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr);
84477623264SMatthew G. Knepley   *partition = p->partition;
84577623264SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr);
84677623264SMatthew G. Knepley   PetscFunctionReturn(0);
84777623264SMatthew G. Knepley }
84877623264SMatthew G. Knepley 
849d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part)
85077623264SMatthew G. Knepley {
85177623264SMatthew G. Knepley   PetscFunctionBegin;
85277623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_Shell;
853077101c0SMatthew G. Knepley   part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell;
85477623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_Shell;
85577623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_Shell;
85677623264SMatthew G. Knepley   PetscFunctionReturn(0);
85777623264SMatthew G. Knepley }
85877623264SMatthew G. Knepley 
85977623264SMatthew G. Knepley /*MC
86077623264SMatthew G. Knepley   PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object
86177623264SMatthew G. Knepley 
86277623264SMatthew G. Knepley   Level: intermediate
86377623264SMatthew G. Knepley 
86477623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
86577623264SMatthew G. Knepley M*/
86677623264SMatthew G. Knepley 
86777623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part)
86877623264SMatthew G. Knepley {
86977623264SMatthew G. Knepley   PetscPartitioner_Shell *p;
87077623264SMatthew G. Knepley   PetscErrorCode          ierr;
87177623264SMatthew G. Knepley 
87277623264SMatthew G. Knepley   PetscFunctionBegin;
87377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
87477623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
87577623264SMatthew G. Knepley   part->data = p;
87677623264SMatthew G. Knepley 
87777623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr);
878077101c0SMatthew G. Knepley   p->random = PETSC_FALSE;
87977623264SMatthew G. Knepley   PetscFunctionReturn(0);
88077623264SMatthew G. Knepley }
88177623264SMatthew G. Knepley 
8825680f57bSMatthew G. Knepley /*@C
8835680f57bSMatthew G. Knepley   PetscPartitionerShellSetPartition - Set an artifical partition for a mesh
8845680f57bSMatthew G. Knepley 
885077101c0SMatthew G. Knepley   Collective on Part
8865680f57bSMatthew G. Knepley 
8875680f57bSMatthew G. Knepley   Input Parameters:
8885680f57bSMatthew G. Knepley + part     - The PetscPartitioner
8899852e123SBarry Smith . size - The number of partitions
8909852e123SBarry Smith . sizes    - array of size size (or NULL) providing the number of points in each partition
8919758bf69SToby 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.)
8925680f57bSMatthew G. Knepley 
8935680f57bSMatthew G. Knepley   Level: developer
8945680f57bSMatthew G. Knepley 
895b7e49471SLawrence Mitchell   Notes:
896b7e49471SLawrence Mitchell 
897b7e49471SLawrence Mitchell     It is safe to free the sizes and points arrays after use in this routine.
898b7e49471SLawrence Mitchell 
8995680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate()
9005680f57bSMatthew G. Knepley @*/
9019852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[])
9025680f57bSMatthew G. Knepley {
9035680f57bSMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
9045680f57bSMatthew G. Knepley   PetscInt                proc, numPoints;
9055680f57bSMatthew G. Knepley   PetscErrorCode          ierr;
9065680f57bSMatthew G. Knepley 
9075680f57bSMatthew G. Knepley   PetscFunctionBegin;
9085680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
9095680f57bSMatthew G. Knepley   if (sizes)  {PetscValidPointer(sizes, 3);}
910c717d290SMatthew G. Knepley   if (points) {PetscValidPointer(points, 4);}
9115680f57bSMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
9125680f57bSMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
9135680f57bSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr);
9149852e123SBarry Smith   ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr);
9155680f57bSMatthew G. Knepley   if (sizes) {
9169852e123SBarry Smith     for (proc = 0; proc < size; ++proc) {
9175680f57bSMatthew G. Knepley       ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr);
9185680f57bSMatthew G. Knepley     }
9195680f57bSMatthew G. Knepley   }
9205680f57bSMatthew G. Knepley   ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr);
9215680f57bSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr);
9225680f57bSMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr);
9235680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
9245680f57bSMatthew G. Knepley }
9255680f57bSMatthew G. Knepley 
926077101c0SMatthew G. Knepley /*@
927077101c0SMatthew G. Knepley   PetscPartitionerShellSetRandom - Set the flag to use a random partition
928077101c0SMatthew G. Knepley 
929077101c0SMatthew G. Knepley   Collective on Part
930077101c0SMatthew G. Knepley 
931077101c0SMatthew G. Knepley   Input Parameters:
932077101c0SMatthew G. Knepley + part   - The PetscPartitioner
933077101c0SMatthew G. Knepley - random - The flag to use a random partition
934077101c0SMatthew G. Knepley 
935077101c0SMatthew G. Knepley   Level: intermediate
936077101c0SMatthew G. Knepley 
937077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate()
938077101c0SMatthew G. Knepley @*/
939077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random)
940077101c0SMatthew G. Knepley {
941077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
942077101c0SMatthew G. Knepley 
943077101c0SMatthew G. Knepley   PetscFunctionBegin;
944077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
945077101c0SMatthew G. Knepley   p->random = random;
946077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
947077101c0SMatthew G. Knepley }
948077101c0SMatthew G. Knepley 
949077101c0SMatthew G. Knepley /*@
950077101c0SMatthew G. Knepley   PetscPartitionerShellGetRandom - get the flag to use a random partition
951077101c0SMatthew G. Knepley 
952077101c0SMatthew G. Knepley   Collective on Part
953077101c0SMatthew G. Knepley 
954077101c0SMatthew G. Knepley   Input Parameter:
955077101c0SMatthew G. Knepley . part   - The PetscPartitioner
956077101c0SMatthew G. Knepley 
957077101c0SMatthew G. Knepley   Output Parameter
958077101c0SMatthew G. Knepley . random - The flag to use a random partition
959077101c0SMatthew G. Knepley 
960077101c0SMatthew G. Knepley   Level: intermediate
961077101c0SMatthew G. Knepley 
962077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate()
963077101c0SMatthew G. Knepley @*/
964077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random)
965077101c0SMatthew G. Knepley {
966077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
967077101c0SMatthew G. Knepley 
968077101c0SMatthew G. Knepley   PetscFunctionBegin;
969077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
970077101c0SMatthew G. Knepley   PetscValidPointer(random, 2);
971077101c0SMatthew G. Knepley   *random = p->random;
972077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
973077101c0SMatthew G. Knepley }
974077101c0SMatthew G. Knepley 
975d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part)
976555a9cf8SMatthew G. Knepley {
977555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data;
978555a9cf8SMatthew G. Knepley   PetscErrorCode          ierr;
979555a9cf8SMatthew G. Knepley 
980555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
981555a9cf8SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
982555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
983555a9cf8SMatthew G. Knepley }
984555a9cf8SMatthew G. Knepley 
985d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer)
986555a9cf8SMatthew G. Knepley {
987555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
988555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
989555a9cf8SMatthew G. Knepley }
990555a9cf8SMatthew G. Knepley 
991d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer)
992555a9cf8SMatthew G. Knepley {
993555a9cf8SMatthew G. Knepley   PetscBool      iascii;
994555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
995555a9cf8SMatthew G. Knepley 
996555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
997555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
998555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
999555a9cf8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1000555a9cf8SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);}
1001555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1002555a9cf8SMatthew G. Knepley }
1003555a9cf8SMatthew G. Knepley 
1004d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1005555a9cf8SMatthew G. Knepley {
1006cead94edSToby Isaac   MPI_Comm       comm;
1007555a9cf8SMatthew G. Knepley   PetscInt       np;
1008cead94edSToby Isaac   PetscMPIInt    size;
1009555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
1010555a9cf8SMatthew G. Knepley 
1011555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1012cead94edSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
1013cead94edSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1014555a9cf8SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1015555a9cf8SMatthew G. Knepley   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1016cead94edSToby Isaac   if (size == 1) {
1017cead94edSToby Isaac     for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);}
1018cead94edSToby Isaac   }
1019cead94edSToby Isaac   else {
1020cead94edSToby Isaac     PetscMPIInt rank;
1021cead94edSToby Isaac     PetscInt nvGlobal, *offsets, myFirst, myLast;
1022cead94edSToby Isaac 
1023a679a563SToby Isaac     ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr);
1024cead94edSToby Isaac     offsets[0] = 0;
1025cead94edSToby Isaac     ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr);
1026cead94edSToby Isaac     for (np = 2; np <= size; np++) {
1027cead94edSToby Isaac       offsets[np] += offsets[np-1];
1028cead94edSToby Isaac     }
1029cead94edSToby Isaac     nvGlobal = offsets[size];
1030cead94edSToby Isaac     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1031cead94edSToby Isaac     myFirst = offsets[rank];
1032cead94edSToby Isaac     myLast  = offsets[rank + 1] - 1;
1033cead94edSToby Isaac     ierr = PetscFree(offsets);CHKERRQ(ierr);
1034cead94edSToby Isaac     if (numVertices) {
1035cead94edSToby Isaac       PetscInt firstPart = 0, firstLargePart = 0;
1036cead94edSToby Isaac       PetscInt lastPart = 0, lastLargePart = 0;
1037cead94edSToby Isaac       PetscInt rem = nvGlobal % nparts;
1038cead94edSToby Isaac       PetscInt pSmall = nvGlobal/nparts;
1039cead94edSToby Isaac       PetscInt pBig = nvGlobal/nparts + 1;
1040cead94edSToby Isaac 
1041cead94edSToby Isaac 
1042cead94edSToby Isaac       if (rem) {
1043cead94edSToby Isaac         firstLargePart = myFirst / pBig;
1044cead94edSToby Isaac         lastLargePart  = myLast  / pBig;
1045cead94edSToby Isaac 
1046cead94edSToby Isaac         if (firstLargePart < rem) {
1047cead94edSToby Isaac           firstPart = firstLargePart;
1048cead94edSToby Isaac         }
1049cead94edSToby Isaac         else {
1050cead94edSToby Isaac           firstPart = rem + (myFirst - (rem * pBig)) / pSmall;
1051cead94edSToby Isaac         }
1052cead94edSToby Isaac         if (lastLargePart < rem) {
1053cead94edSToby Isaac           lastPart = lastLargePart;
1054cead94edSToby Isaac         }
1055cead94edSToby Isaac         else {
1056cead94edSToby Isaac           lastPart = rem + (myLast - (rem * pBig)) / pSmall;
1057cead94edSToby Isaac         }
1058cead94edSToby Isaac       }
1059cead94edSToby Isaac       else {
1060cead94edSToby Isaac         firstPart = myFirst / (nvGlobal/nparts);
1061cead94edSToby Isaac         lastPart  = myLast  / (nvGlobal/nparts);
1062cead94edSToby Isaac       }
1063cead94edSToby Isaac 
1064cead94edSToby Isaac       for (np = firstPart; np <= lastPart; np++) {
1065cead94edSToby Isaac         PetscInt PartStart =  np    * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np);
1066cead94edSToby Isaac         PetscInt PartEnd   = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1);
1067cead94edSToby Isaac 
1068cead94edSToby Isaac         PartStart = PetscMax(PartStart,myFirst);
1069cead94edSToby Isaac         PartEnd   = PetscMin(PartEnd,myLast+1);
1070cead94edSToby Isaac         ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr);
1071cead94edSToby Isaac       }
1072cead94edSToby Isaac     }
1073cead94edSToby Isaac   }
1074cead94edSToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1075555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1076555a9cf8SMatthew G. Knepley }
1077555a9cf8SMatthew G. Knepley 
1078d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part)
1079555a9cf8SMatthew G. Knepley {
1080555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1081555a9cf8SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Simple;
1082555a9cf8SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Simple;
1083555a9cf8SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Simple;
1084555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1085555a9cf8SMatthew G. Knepley }
1086555a9cf8SMatthew G. Knepley 
1087555a9cf8SMatthew G. Knepley /*MC
1088555a9cf8SMatthew G. Knepley   PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object
1089555a9cf8SMatthew G. Knepley 
1090555a9cf8SMatthew G. Knepley   Level: intermediate
1091555a9cf8SMatthew G. Knepley 
1092555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1093555a9cf8SMatthew G. Knepley M*/
1094555a9cf8SMatthew G. Knepley 
1095555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part)
1096555a9cf8SMatthew G. Knepley {
1097555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p;
1098555a9cf8SMatthew G. Knepley   PetscErrorCode           ierr;
1099555a9cf8SMatthew G. Knepley 
1100555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1101555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1102555a9cf8SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1103555a9cf8SMatthew G. Knepley   part->data = p;
1104555a9cf8SMatthew G. Knepley 
1105555a9cf8SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr);
1106555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1107555a9cf8SMatthew G. Knepley }
1108555a9cf8SMatthew G. Knepley 
1109d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part)
1110dae52e14SToby Isaac {
1111dae52e14SToby Isaac   PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data;
1112dae52e14SToby Isaac   PetscErrorCode          ierr;
1113dae52e14SToby Isaac 
1114dae52e14SToby Isaac   PetscFunctionBegin;
1115dae52e14SToby Isaac   ierr = PetscFree(p);CHKERRQ(ierr);
1116dae52e14SToby Isaac   PetscFunctionReturn(0);
1117dae52e14SToby Isaac }
1118dae52e14SToby Isaac 
1119d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer)
1120dae52e14SToby Isaac {
1121dae52e14SToby Isaac   PetscFunctionBegin;
1122dae52e14SToby Isaac   PetscFunctionReturn(0);
1123dae52e14SToby Isaac }
1124dae52e14SToby Isaac 
1125d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer)
1126dae52e14SToby Isaac {
1127dae52e14SToby Isaac   PetscBool      iascii;
1128dae52e14SToby Isaac   PetscErrorCode ierr;
1129dae52e14SToby Isaac 
1130dae52e14SToby Isaac   PetscFunctionBegin;
1131dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1132dae52e14SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1133dae52e14SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1134dae52e14SToby Isaac   if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);}
1135dae52e14SToby Isaac   PetscFunctionReturn(0);
1136dae52e14SToby Isaac }
1137dae52e14SToby Isaac 
1138d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1139dae52e14SToby Isaac {
1140dae52e14SToby Isaac   PetscInt       np;
1141dae52e14SToby Isaac   PetscErrorCode ierr;
1142dae52e14SToby Isaac 
1143dae52e14SToby Isaac   PetscFunctionBegin;
1144dae52e14SToby Isaac   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1145dae52e14SToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1146dae52e14SToby Isaac   ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr);
1147dae52e14SToby Isaac   for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);}
1148dae52e14SToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1149dae52e14SToby Isaac   PetscFunctionReturn(0);
1150dae52e14SToby Isaac }
1151dae52e14SToby Isaac 
1152d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part)
1153dae52e14SToby Isaac {
1154dae52e14SToby Isaac   PetscFunctionBegin;
1155dae52e14SToby Isaac   part->ops->view      = PetscPartitionerView_Gather;
1156dae52e14SToby Isaac   part->ops->destroy   = PetscPartitionerDestroy_Gather;
1157dae52e14SToby Isaac   part->ops->partition = PetscPartitionerPartition_Gather;
1158dae52e14SToby Isaac   PetscFunctionReturn(0);
1159dae52e14SToby Isaac }
1160dae52e14SToby Isaac 
1161dae52e14SToby Isaac /*MC
1162dae52e14SToby Isaac   PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object
1163dae52e14SToby Isaac 
1164dae52e14SToby Isaac   Level: intermediate
1165dae52e14SToby Isaac 
1166dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1167dae52e14SToby Isaac M*/
1168dae52e14SToby Isaac 
1169dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part)
1170dae52e14SToby Isaac {
1171dae52e14SToby Isaac   PetscPartitioner_Gather *p;
1172dae52e14SToby Isaac   PetscErrorCode           ierr;
1173dae52e14SToby Isaac 
1174dae52e14SToby Isaac   PetscFunctionBegin;
1175dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1176dae52e14SToby Isaac   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1177dae52e14SToby Isaac   part->data = p;
1178dae52e14SToby Isaac 
1179dae52e14SToby Isaac   ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr);
1180dae52e14SToby Isaac   PetscFunctionReturn(0);
1181dae52e14SToby Isaac }
1182dae52e14SToby Isaac 
1183dae52e14SToby Isaac 
1184d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part)
118577623264SMatthew G. Knepley {
118677623264SMatthew G. Knepley   PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data;
118777623264SMatthew G. Knepley   PetscErrorCode          ierr;
118877623264SMatthew G. Knepley 
118977623264SMatthew G. Knepley   PetscFunctionBegin;
119077623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
119177623264SMatthew G. Knepley   PetscFunctionReturn(0);
119277623264SMatthew G. Knepley }
119377623264SMatthew G. Knepley 
1194d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer)
119577623264SMatthew G. Knepley {
119677623264SMatthew G. Knepley   PetscFunctionBegin;
119777623264SMatthew G. Knepley   PetscFunctionReturn(0);
119877623264SMatthew G. Knepley }
119977623264SMatthew G. Knepley 
1200d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer)
120177623264SMatthew G. Knepley {
120277623264SMatthew G. Knepley   PetscBool      iascii;
120377623264SMatthew G. Knepley   PetscErrorCode ierr;
120477623264SMatthew G. Knepley 
120577623264SMatthew G. Knepley   PetscFunctionBegin;
120677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
120777623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
120877623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
120977623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);}
121077623264SMatthew G. Knepley   PetscFunctionReturn(0);
121177623264SMatthew G. Knepley }
121277623264SMatthew G. Knepley 
121370034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
121470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
121570034214SMatthew G. Knepley #include <unistd.h>
121670034214SMatthew G. Knepley #endif
121711d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
121811d1e910SBarry Smith #include <chaco.h>
121911d1e910SBarry Smith #else
122011d1e910SBarry Smith /* Older versions of Chaco do not have an include file */
122170034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
122270034214SMatthew G. Knepley                        float *ewgts, float *x, float *y, float *z, char *outassignname,
122370034214SMatthew G. Knepley                        char *outfilename, short *assignment, int architecture, int ndims_tot,
122470034214SMatthew G. Knepley                        int mesh_dims[3], double *goal, int global_method, int local_method,
122570034214SMatthew G. Knepley                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
122611d1e910SBarry Smith #endif
122770034214SMatthew G. Knepley extern int FREE_GRAPH;
122877623264SMatthew G. Knepley #endif
122970034214SMatthew G. Knepley 
1230d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
123170034214SMatthew G. Knepley {
123277623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
123370034214SMatthew G. Knepley   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
123470034214SMatthew G. Knepley   MPI_Comm       comm;
123570034214SMatthew G. Knepley   int            nvtxs          = numVertices; /* number of vertices in full graph */
123670034214SMatthew G. Knepley   int           *vwgts          = NULL;   /* weights for all vertices */
123770034214SMatthew G. Knepley   float         *ewgts          = NULL;   /* weights for all edges */
123870034214SMatthew G. Knepley   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
123970034214SMatthew G. Knepley   char          *outassignname  = NULL;   /*  name of assignment output file */
124070034214SMatthew G. Knepley   char          *outfilename    = NULL;   /* output file name */
124170034214SMatthew G. Knepley   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
124270034214SMatthew G. Knepley   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
124370034214SMatthew G. Knepley   int            mesh_dims[3];            /* dimensions of mesh of processors */
124470034214SMatthew G. Knepley   double        *goal          = NULL;    /* desired set sizes for each set */
124570034214SMatthew G. Knepley   int            global_method = 1;       /* global partitioning algorithm */
124670034214SMatthew G. Knepley   int            local_method  = 1;       /* local partitioning algorithm */
124770034214SMatthew G. Knepley   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
124870034214SMatthew G. Knepley   int            vmax          = 200;     /* how many vertices to coarsen down to? */
124970034214SMatthew G. Knepley   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
125070034214SMatthew G. Knepley   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
125170034214SMatthew G. Knepley   long           seed          = 123636512; /* for random graph mutations */
125211d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
125311d1e910SBarry Smith   int           *assignment;              /* Output partition */
125411d1e910SBarry Smith #else
125570034214SMatthew G. Knepley   short int     *assignment;              /* Output partition */
125611d1e910SBarry Smith #endif
125770034214SMatthew G. Knepley   int            fd_stdout, fd_pipe[2];
125870034214SMatthew G. Knepley   PetscInt      *points;
125970034214SMatthew G. Knepley   int            i, v, p;
126070034214SMatthew G. Knepley   PetscErrorCode ierr;
126170034214SMatthew G. Knepley 
126270034214SMatthew G. Knepley   PetscFunctionBegin;
126370034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
126407ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG)
126507ed3857SLisandro Dalcin   {
126607ed3857SLisandro Dalcin     int ival,isum;
126707ed3857SLisandro Dalcin     PetscBool distributed;
126807ed3857SLisandro Dalcin 
126907ed3857SLisandro Dalcin     ival = (numVertices > 0);
127007ed3857SLisandro Dalcin     ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr);
127107ed3857SLisandro Dalcin     distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE;
127207ed3857SLisandro Dalcin     if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph");
127307ed3857SLisandro Dalcin   }
127407ed3857SLisandro Dalcin #endif
127570034214SMatthew G. Knepley   if (!numVertices) {
127677623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
127777623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
127870034214SMatthew G. Knepley     ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
127970034214SMatthew G. Knepley     PetscFunctionReturn(0);
128070034214SMatthew G. Knepley   }
128170034214SMatthew G. Knepley   FREE_GRAPH = 0;                         /* Do not let Chaco free my memory */
128270034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
128370034214SMatthew G. Knepley 
128470034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
128570034214SMatthew G. Knepley     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
128670034214SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
128770034214SMatthew G. Knepley   }
128877623264SMatthew G. Knepley   mesh_dims[0] = nparts;
128970034214SMatthew G. Knepley   mesh_dims[1] = 1;
129070034214SMatthew G. Knepley   mesh_dims[2] = 1;
129170034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr);
129270034214SMatthew G. Knepley   /* Chaco outputs to stdout. We redirect this to a buffer. */
129370034214SMatthew G. Knepley   /* TODO: check error codes for UNIX calls */
129470034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
129570034214SMatthew G. Knepley   {
129670034214SMatthew G. Knepley     int piperet;
129770034214SMatthew G. Knepley     piperet = pipe(fd_pipe);
129870034214SMatthew G. Knepley     if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe");
129970034214SMatthew G. Knepley     fd_stdout = dup(1);
130070034214SMatthew G. Knepley     close(1);
130170034214SMatthew G. Knepley     dup2(fd_pipe[1], 1);
130270034214SMatthew G. Knepley   }
130370034214SMatthew G. Knepley #endif
130470034214SMatthew G. Knepley   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
130570034214SMatthew G. Knepley                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
130670034214SMatthew G. Knepley                    vmax, ndims, eigtol, seed);
130770034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
130870034214SMatthew G. Knepley   {
130970034214SMatthew G. Knepley     char msgLog[10000];
131070034214SMatthew G. Knepley     int  count;
131170034214SMatthew G. Knepley 
131270034214SMatthew G. Knepley     fflush(stdout);
131370034214SMatthew G. Knepley     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
131470034214SMatthew G. Knepley     if (count < 0) count = 0;
131570034214SMatthew G. Knepley     msgLog[count] = 0;
131670034214SMatthew G. Knepley     close(1);
131770034214SMatthew G. Knepley     dup2(fd_stdout, 1);
131870034214SMatthew G. Knepley     close(fd_stdout);
131970034214SMatthew G. Knepley     close(fd_pipe[0]);
132070034214SMatthew G. Knepley     close(fd_pipe[1]);
132170034214SMatthew G. Knepley     if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
132270034214SMatthew G. Knepley   }
132307ed3857SLisandro Dalcin #else
132407ed3857SLisandro Dalcin   if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout");
132570034214SMatthew G. Knepley #endif
132670034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
132777623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
132870034214SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {
132977623264SMatthew G. Knepley     ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);
133070034214SMatthew G. Knepley   }
133177623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
133270034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
133377623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
133470034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
133570034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
133670034214SMatthew G. Knepley     }
133770034214SMatthew G. Knepley   }
133870034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
133970034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
134070034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
134170034214SMatthew G. Knepley     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
134270034214SMatthew G. Knepley   }
134370034214SMatthew G. Knepley   ierr = PetscFree(assignment);CHKERRQ(ierr);
134470034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
134570034214SMatthew G. Knepley   PetscFunctionReturn(0);
134677623264SMatthew G. Knepley #else
134777623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
134870034214SMatthew G. Knepley #endif
134977623264SMatthew G. Knepley }
135077623264SMatthew G. Knepley 
1351d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part)
135277623264SMatthew G. Knepley {
135377623264SMatthew G. Knepley   PetscFunctionBegin;
135477623264SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Chaco;
135577623264SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Chaco;
135677623264SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Chaco;
135777623264SMatthew G. Knepley   PetscFunctionReturn(0);
135877623264SMatthew G. Knepley }
135977623264SMatthew G. Knepley 
136077623264SMatthew G. Knepley /*MC
136177623264SMatthew G. Knepley   PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library
136277623264SMatthew G. Knepley 
136377623264SMatthew G. Knepley   Level: intermediate
136477623264SMatthew G. Knepley 
136577623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
136677623264SMatthew G. Knepley M*/
136777623264SMatthew G. Knepley 
136877623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part)
136977623264SMatthew G. Knepley {
137077623264SMatthew G. Knepley   PetscPartitioner_Chaco *p;
137177623264SMatthew G. Knepley   PetscErrorCode          ierr;
137277623264SMatthew G. Knepley 
137377623264SMatthew G. Knepley   PetscFunctionBegin;
137477623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
137577623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
137677623264SMatthew G. Knepley   part->data = p;
137777623264SMatthew G. Knepley 
137877623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr);
137977623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr);
138077623264SMatthew G. Knepley   PetscFunctionReturn(0);
138177623264SMatthew G. Knepley }
138277623264SMatthew G. Knepley 
13835b440754SMatthew G. Knepley static const char *ptypes[] = {"kway", "rb"};
13845b440754SMatthew G. Knepley 
1385d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part)
138677623264SMatthew G. Knepley {
138777623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
138877623264SMatthew G. Knepley   PetscErrorCode             ierr;
138977623264SMatthew G. Knepley 
139077623264SMatthew G. Knepley   PetscFunctionBegin;
139177623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
139277623264SMatthew G. Knepley   PetscFunctionReturn(0);
139377623264SMatthew G. Knepley }
139477623264SMatthew G. Knepley 
1395d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer)
139677623264SMatthew G. Knepley {
13972abdaa70SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
139877623264SMatthew G. Knepley   PetscErrorCode             ierr;
139977623264SMatthew G. Knepley 
140077623264SMatthew G. Knepley   PetscFunctionBegin;
14012abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
14022abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "ParMetis type: %s\n", ptypes[p->ptype]);CHKERRQ(ierr);
14032abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "load imbalance ratio %g\n", (double) p->imbalanceRatio);CHKERRQ(ierr);
14042abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "debug flag %D\n", p->debugFlag);CHKERRQ(ierr);
14052abdaa70SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
140677623264SMatthew G. Knepley   PetscFunctionReturn(0);
140777623264SMatthew G. Knepley }
140877623264SMatthew G. Knepley 
1409d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer)
141077623264SMatthew G. Knepley {
141177623264SMatthew G. Knepley   PetscBool      iascii;
141277623264SMatthew G. Knepley   PetscErrorCode ierr;
141377623264SMatthew G. Knepley 
141477623264SMatthew G. Knepley   PetscFunctionBegin;
141577623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
141677623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
141777623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
141877623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);}
141977623264SMatthew G. Knepley   PetscFunctionReturn(0);
142077623264SMatthew G. Knepley }
142170034214SMatthew G. Knepley 
142244d8be81SLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_ParMetis(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
142344d8be81SLisandro Dalcin {
142444d8be81SLisandro Dalcin   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
142544d8be81SLisandro Dalcin   PetscErrorCode            ierr;
142644d8be81SLisandro Dalcin 
142744d8be81SLisandro Dalcin   PetscFunctionBegin;
142844d8be81SLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner ParMetis Options");CHKERRQ(ierr);
142944d8be81SLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_parmetis_type", "Partitioning method", "", ptypes, 2, ptypes[p->ptype], &p->ptype, NULL);CHKERRQ(ierr);
14305b440754SMatthew G. Knepley   ierr = PetscOptionsReal("-petscpartitioner_parmetis_imbalance_ratio", "Load imbalance ratio limit", "", p->imbalanceRatio, &p->imbalanceRatio, NULL);CHKERRQ(ierr);
14315b440754SMatthew G. Knepley   ierr = PetscOptionsInt("-petscpartitioner_parmetis_debug", "Debugging flag", "", p->debugFlag, &p->debugFlag, NULL);CHKERRQ(ierr);
143244d8be81SLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
143344d8be81SLisandro Dalcin   PetscFunctionReturn(0);
143444d8be81SLisandro Dalcin }
143544d8be81SLisandro Dalcin 
143670034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
143770034214SMatthew G. Knepley #include <parmetis.h>
143877623264SMatthew G. Knepley #endif
143970034214SMatthew G. Knepley 
1440d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
144170034214SMatthew G. Knepley {
144277623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
14435b440754SMatthew G. Knepley   PetscPartitioner_ParMetis *pm = (PetscPartitioner_ParMetis *) part->data;
144470034214SMatthew G. Knepley   MPI_Comm       comm;
1445deea36a5SMatthew G. Knepley   PetscSection   section;
144670034214SMatthew G. Knepley   PetscInt       nvtxs       = numVertices; /* The number of vertices in full graph */
144770034214SMatthew G. Knepley   PetscInt      *vtxdist;                   /* Distribution of vertices across processes */
144870034214SMatthew G. Knepley   PetscInt      *xadj        = start;       /* Start of edge list for each vertex */
144970034214SMatthew G. Knepley   PetscInt      *adjncy      = adjacency;   /* Edge lists for all vertices */
145070034214SMatthew G. Knepley   PetscInt      *vwgt        = NULL;        /* Vertex weights */
145170034214SMatthew G. Knepley   PetscInt      *adjwgt      = NULL;        /* Edge weights */
145270034214SMatthew G. Knepley   PetscInt       wgtflag     = 0;           /* Indicates which weights are present */
145370034214SMatthew G. Knepley   PetscInt       numflag     = 0;           /* Indicates initial offset (0 or 1) */
145470034214SMatthew G. Knepley   PetscInt       ncon        = 1;           /* The number of weights per vertex */
14555b440754SMatthew G. Knepley   PetscInt       metis_ptype = pm->ptype;   /* kway or recursive bisection */
1456fb83b9f2SMichael Gegg   real_t        *tpwgts;                    /* The fraction of vertex weights assigned to each partition */
1457fb83b9f2SMichael Gegg   real_t        *ubvec;                     /* The balance intolerance for vertex weights */
1458b3ce585bSLisandro Dalcin   PetscInt       options[64];               /* Options */
145970034214SMatthew G. Knepley   /* Outputs */
1460b3ce585bSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1461b3ce585bSLisandro Dalcin   PetscMPIInt    size, rank, p;
146270034214SMatthew G. Knepley   PetscErrorCode ierr;
146370034214SMatthew G. Knepley 
146470034214SMatthew G. Knepley   PetscFunctionBegin;
146577623264SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr);
1466b3ce585bSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
146770034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
146870034214SMatthew G. Knepley   /* Calculate vertex distribution */
1469b3ce585bSLisandro Dalcin   ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr);
147070034214SMatthew G. Knepley   vtxdist[0] = 0;
147170034214SMatthew G. Knepley   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1472b3ce585bSLisandro Dalcin   for (p = 2; p <= size; ++p) {
147370034214SMatthew G. Knepley     vtxdist[p] += vtxdist[p-1];
147470034214SMatthew G. Knepley   }
147544d8be81SLisandro Dalcin   /* Calculate partition weights */
147670034214SMatthew G. Knepley   for (p = 0; p < nparts; ++p) {
147770034214SMatthew G. Knepley     tpwgts[p] = 1.0/nparts;
147870034214SMatthew G. Knepley   }
14795b440754SMatthew G. Knepley   ubvec[0] = pm->imbalanceRatio;
1480deea36a5SMatthew G. Knepley   /* Weight cells by dofs on cell by default */
1481e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1482deea36a5SMatthew G. Knepley   if (section) {
1483deea36a5SMatthew G. Knepley     PetscInt cStart, cEnd, dof;
148470034214SMatthew G. Knepley 
1485deea36a5SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1486deea36a5SMatthew G. Knepley     for (v = cStart; v < cEnd; ++v) {
1487deea36a5SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1488925b1076SLisandro Dalcin       /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1489925b1076SLisandro Dalcin       /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1490925b1076SLisandro Dalcin       if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1);
1491deea36a5SMatthew G. Knepley     }
1492deea36a5SMatthew G. Knepley   } else {
1493deea36a5SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1494cd0de0f2SShri   }
149544d8be81SLisandro Dalcin   wgtflag |= 2; /* have weights on graph vertices */
1496cd0de0f2SShri 
149770034214SMatthew G. Knepley   if (nparts == 1) {
14989fc93327SToby Isaac     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
149970034214SMatthew G. Knepley   } else {
1500b3ce585bSLisandro Dalcin     for (p = 0; !vtxdist[p+1] && p < size; ++p);
1501b3ce585bSLisandro Dalcin     if (vtxdist[p+1] == vtxdist[size]) {
1502b3ce585bSLisandro Dalcin       if (rank == p) {
150344d8be81SLisandro Dalcin         ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */
150444d8be81SLisandro Dalcin         if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()");
150544d8be81SLisandro Dalcin         if (metis_ptype == 1) {
150644d8be81SLisandro Dalcin           PetscStackPush("METIS_PartGraphRecursive");
150772379da4SMatthew G. Knepley           ierr = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
150844d8be81SLisandro Dalcin           PetscStackPop;
150944d8be81SLisandro Dalcin           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphRecursive()");
151044d8be81SLisandro Dalcin         } else {
151144d8be81SLisandro Dalcin           /*
151244d8be81SLisandro Dalcin            It would be nice to activate the two options below, but they would need some actual testing.
151344d8be81SLisandro Dalcin            - Turning on these options may exercise path of the METIS code that have bugs and may break production runs.
151444d8be81SLisandro 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.
151544d8be81SLisandro Dalcin           */
151644d8be81SLisandro Dalcin           /* options[METIS_OPTION_CONTIG]  = 1; */ /* try to produce partitions that are contiguous */
151744d8be81SLisandro Dalcin           /* options[METIS_OPTION_MINCONN] = 1; */ /* minimize the maximum degree of the subdomain graph */
151870034214SMatthew G. Knepley           PetscStackPush("METIS_PartGraphKway");
151972379da4SMatthew G. Knepley           ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment);
152070034214SMatthew G. Knepley           PetscStackPop;
152170034214SMatthew G. Knepley           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
152270034214SMatthew G. Knepley         }
152344d8be81SLisandro Dalcin       }
152470034214SMatthew G. Knepley     } else {
15255b440754SMatthew G. Knepley       options[0] = 1;
15265b440754SMatthew G. Knepley       options[1] = pm->debugFlag;
152770034214SMatthew G. Knepley       PetscStackPush("ParMETIS_V3_PartKway");
152872379da4SMatthew G. Knepley       ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &part->edgeCut, assignment, &comm);
152970034214SMatthew G. Knepley       PetscStackPop;
1530c717d290SMatthew G. Knepley       if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr);
153170034214SMatthew G. Knepley     }
153270034214SMatthew G. Knepley   }
153370034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
153477623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
153577623264SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
153677623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
153770034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
153877623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
153970034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
154070034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
154170034214SMatthew G. Knepley     }
154270034214SMatthew G. Knepley   }
154370034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
154470034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1545cd0de0f2SShri   ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr);
15469b80ac48SMatthew G. Knepley   PetscFunctionReturn(0);
154770034214SMatthew G. Knepley #else
154877623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis.");
154970034214SMatthew G. Knepley #endif
155070034214SMatthew G. Knepley }
155170034214SMatthew G. Knepley 
1552d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part)
155377623264SMatthew G. Knepley {
155477623264SMatthew G. Knepley   PetscFunctionBegin;
155577623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_ParMetis;
155644d8be81SLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_ParMetis;
155777623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_ParMetis;
155877623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_ParMetis;
155977623264SMatthew G. Knepley   PetscFunctionReturn(0);
156077623264SMatthew G. Knepley }
156177623264SMatthew G. Knepley 
156277623264SMatthew G. Knepley /*MC
156377623264SMatthew G. Knepley   PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library
156477623264SMatthew G. Knepley 
156577623264SMatthew G. Knepley   Level: intermediate
156677623264SMatthew G. Knepley 
156777623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
156877623264SMatthew G. Knepley M*/
156977623264SMatthew G. Knepley 
157077623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part)
157177623264SMatthew G. Knepley {
157277623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p;
157377623264SMatthew G. Knepley   PetscErrorCode          ierr;
157477623264SMatthew G. Knepley 
157577623264SMatthew G. Knepley   PetscFunctionBegin;
157677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
157777623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
157877623264SMatthew G. Knepley   part->data = p;
157977623264SMatthew G. Knepley 
15805b440754SMatthew G. Knepley   p->ptype          = 0;
15815b440754SMatthew G. Knepley   p->imbalanceRatio = 1.05;
15825b440754SMatthew G. Knepley   p->debugFlag      = 0;
15835b440754SMatthew G. Knepley 
158477623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr);
158577623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr);
158670034214SMatthew G. Knepley   PetscFunctionReturn(0);
158770034214SMatthew G. Knepley }
158870034214SMatthew G. Knepley 
1589137cd93aSLisandro Dalcin 
1590137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE;
1591137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] =
1592137cd93aSLisandro Dalcin   "@article{PTSCOTCH,\n"
1593137cd93aSLisandro Dalcin   "  author  = {C. Chevalier and F. Pellegrini},\n"
1594137cd93aSLisandro Dalcin   "  title   = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n"
1595137cd93aSLisandro Dalcin   "  journal = {Parallel Computing},\n"
1596137cd93aSLisandro Dalcin   "  volume  = {34},\n"
1597137cd93aSLisandro Dalcin   "  number  = {6},\n"
1598137cd93aSLisandro Dalcin   "  pages   = {318--331},\n"
1599137cd93aSLisandro Dalcin   "  year    = {2008},\n"
1600137cd93aSLisandro Dalcin   "  doi     = {https://doi.org/10.1016/j.parco.2007.12.001}\n"
1601137cd93aSLisandro Dalcin   "}\n";
1602137cd93aSLisandro Dalcin 
1603137cd93aSLisandro Dalcin typedef struct {
1604137cd93aSLisandro Dalcin   PetscInt  strategy;
1605137cd93aSLisandro Dalcin   PetscReal imbalance;
1606137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch;
1607137cd93aSLisandro Dalcin 
1608137cd93aSLisandro Dalcin static const char *const
1609137cd93aSLisandro Dalcin PTScotchStrategyList[] = {
1610137cd93aSLisandro Dalcin   "DEFAULT",
1611137cd93aSLisandro Dalcin   "QUALITY",
1612137cd93aSLisandro Dalcin   "SPEED",
1613137cd93aSLisandro Dalcin   "BALANCE",
1614137cd93aSLisandro Dalcin   "SAFETY",
1615137cd93aSLisandro Dalcin   "SCALABILITY",
1616137cd93aSLisandro Dalcin   "RECURSIVE",
1617137cd93aSLisandro Dalcin   "REMAP"
1618137cd93aSLisandro Dalcin };
1619137cd93aSLisandro Dalcin 
1620137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1621137cd93aSLisandro Dalcin 
1622137cd93aSLisandro Dalcin EXTERN_C_BEGIN
1623137cd93aSLisandro Dalcin #include <ptscotch.h>
1624137cd93aSLisandro Dalcin EXTERN_C_END
1625137cd93aSLisandro Dalcin 
1626137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0)
1627137cd93aSLisandro Dalcin 
1628137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy)
1629137cd93aSLisandro Dalcin {
1630137cd93aSLisandro Dalcin   switch (strategy) {
1631137cd93aSLisandro Dalcin   case  0: return SCOTCH_STRATDEFAULT;
1632137cd93aSLisandro Dalcin   case  1: return SCOTCH_STRATQUALITY;
1633137cd93aSLisandro Dalcin   case  2: return SCOTCH_STRATSPEED;
1634137cd93aSLisandro Dalcin   case  3: return SCOTCH_STRATBALANCE;
1635137cd93aSLisandro Dalcin   case  4: return SCOTCH_STRATSAFETY;
1636137cd93aSLisandro Dalcin   case  5: return SCOTCH_STRATSCALABILITY;
1637137cd93aSLisandro Dalcin   case  6: return SCOTCH_STRATRECURSIVE;
1638137cd93aSLisandro Dalcin   case  7: return SCOTCH_STRATREMAP;
1639137cd93aSLisandro Dalcin   default: return SCOTCH_STRATDEFAULT;
1640137cd93aSLisandro Dalcin   }
1641137cd93aSLisandro Dalcin }
1642137cd93aSLisandro Dalcin 
1643137cd93aSLisandro Dalcin 
1644137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1645137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[])
1646137cd93aSLisandro Dalcin {
1647137cd93aSLisandro Dalcin   SCOTCH_Graph   grafdat;
1648137cd93aSLisandro Dalcin   SCOTCH_Strat   stradat;
1649137cd93aSLisandro Dalcin   SCOTCH_Num     vertnbr = n;
1650137cd93aSLisandro Dalcin   SCOTCH_Num     edgenbr = xadj[n];
1651137cd93aSLisandro Dalcin   SCOTCH_Num*    velotab = vtxwgt;
1652137cd93aSLisandro Dalcin   SCOTCH_Num*    edlotab = adjwgt;
1653137cd93aSLisandro Dalcin   SCOTCH_Num     flagval = strategy;
1654137cd93aSLisandro Dalcin   double         kbalval = imbalance;
1655137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1656137cd93aSLisandro Dalcin 
1657137cd93aSLisandro Dalcin   PetscFunctionBegin;
1658d99a0000SVaclav Hapla   {
1659d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1660d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1661d99a0000SVaclav Hapla     if (!flg) velotab = NULL;
1662d99a0000SVaclav Hapla   }
1663137cd93aSLisandro Dalcin   ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr);
1664137cd93aSLisandro Dalcin   ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr);
1665137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1666137cd93aSLisandro Dalcin   ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr);
1667137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1668137cd93aSLisandro Dalcin   ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1669137cd93aSLisandro Dalcin #endif
1670137cd93aSLisandro Dalcin   ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr);
1671137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1672137cd93aSLisandro Dalcin   SCOTCH_graphExit(&grafdat);
1673137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1674137cd93aSLisandro Dalcin }
1675137cd93aSLisandro Dalcin 
1676137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1677137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm)
1678137cd93aSLisandro Dalcin {
1679137cd93aSLisandro Dalcin   PetscMPIInt     procglbnbr;
1680137cd93aSLisandro Dalcin   PetscMPIInt     proclocnum;
1681137cd93aSLisandro Dalcin   SCOTCH_Arch     archdat;
1682137cd93aSLisandro Dalcin   SCOTCH_Dgraph   grafdat;
1683137cd93aSLisandro Dalcin   SCOTCH_Dmapping mappdat;
1684137cd93aSLisandro Dalcin   SCOTCH_Strat    stradat;
1685137cd93aSLisandro Dalcin   SCOTCH_Num      vertlocnbr;
1686137cd93aSLisandro Dalcin   SCOTCH_Num      edgelocnbr;
1687137cd93aSLisandro Dalcin   SCOTCH_Num*     veloloctab = vtxwgt;
1688137cd93aSLisandro Dalcin   SCOTCH_Num*     edloloctab = adjwgt;
1689137cd93aSLisandro Dalcin   SCOTCH_Num      flagval = strategy;
1690137cd93aSLisandro Dalcin   double          kbalval = imbalance;
1691137cd93aSLisandro Dalcin   PetscErrorCode  ierr;
1692137cd93aSLisandro Dalcin 
1693137cd93aSLisandro Dalcin   PetscFunctionBegin;
1694d99a0000SVaclav Hapla   {
1695d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1696d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1697d99a0000SVaclav Hapla     if (!flg) veloloctab = NULL;
1698d99a0000SVaclav Hapla   }
1699137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr);
1700137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr);
1701137cd93aSLisandro Dalcin   vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum];
1702137cd93aSLisandro Dalcin   edgelocnbr = xadj[vertlocnbr];
1703137cd93aSLisandro Dalcin 
1704137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr);
1705137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr);
1706137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1707137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1708137cd93aSLisandro Dalcin #endif
1709137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1710137cd93aSLisandro Dalcin   ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr);
1711137cd93aSLisandro Dalcin   ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr);
1712137cd93aSLisandro Dalcin   ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr);
1713137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr);
1714137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr);
1715137cd93aSLisandro Dalcin   SCOTCH_dgraphMapExit(&grafdat, &mappdat);
1716137cd93aSLisandro Dalcin   SCOTCH_archExit(&archdat);
1717137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1718137cd93aSLisandro Dalcin   SCOTCH_dgraphExit(&grafdat);
1719137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1720137cd93aSLisandro Dalcin }
1721137cd93aSLisandro Dalcin 
1722137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */
1723137cd93aSLisandro Dalcin 
1724137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part)
1725137cd93aSLisandro Dalcin {
1726137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1727137cd93aSLisandro Dalcin   PetscErrorCode             ierr;
1728137cd93aSLisandro Dalcin 
1729137cd93aSLisandro Dalcin   PetscFunctionBegin;
1730137cd93aSLisandro Dalcin   ierr = PetscFree(p);CHKERRQ(ierr);
1731137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1732137cd93aSLisandro Dalcin }
1733137cd93aSLisandro Dalcin 
1734137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer)
1735137cd93aSLisandro Dalcin {
1736137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1737137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1738137cd93aSLisandro Dalcin 
1739137cd93aSLisandro Dalcin   PetscFunctionBegin;
1740137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1741137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr);
1742137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr);
1743137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1744137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1745137cd93aSLisandro Dalcin }
1746137cd93aSLisandro Dalcin 
1747137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer)
1748137cd93aSLisandro Dalcin {
1749137cd93aSLisandro Dalcin   PetscBool      iascii;
1750137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1751137cd93aSLisandro Dalcin 
1752137cd93aSLisandro Dalcin   PetscFunctionBegin;
1753137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1754137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1755137cd93aSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1756137cd93aSLisandro Dalcin   if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);}
1757137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1758137cd93aSLisandro Dalcin }
1759137cd93aSLisandro Dalcin 
1760137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
1761137cd93aSLisandro Dalcin {
1762137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1763137cd93aSLisandro Dalcin   const char *const         *slist = PTScotchStrategyList;
1764137cd93aSLisandro Dalcin   PetscInt                  nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0]));
1765137cd93aSLisandro Dalcin   PetscBool                 flag;
1766137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1767137cd93aSLisandro Dalcin 
1768137cd93aSLisandro Dalcin   PetscFunctionBegin;
1769137cd93aSLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner PTScotch Options");CHKERRQ(ierr);
1770137cd93aSLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr);
1771137cd93aSLisandro Dalcin   ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr);
1772137cd93aSLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
1773137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1774137cd93aSLisandro Dalcin }
1775137cd93aSLisandro Dalcin 
1776137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1777137cd93aSLisandro Dalcin {
1778137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1779137cd93aSLisandro Dalcin   MPI_Comm       comm       = PetscObjectComm((PetscObject)part);
1780137cd93aSLisandro Dalcin   PetscInt       nvtxs      = numVertices; /* The number of vertices in full graph */
1781137cd93aSLisandro Dalcin   PetscInt      *vtxdist;                  /* Distribution of vertices across processes */
1782137cd93aSLisandro Dalcin   PetscInt      *xadj       = start;       /* Start of edge list for each vertex */
1783137cd93aSLisandro Dalcin   PetscInt      *adjncy     = adjacency;   /* Edge lists for all vertices */
1784137cd93aSLisandro Dalcin   PetscInt      *vwgt       = NULL;        /* Vertex weights */
1785137cd93aSLisandro Dalcin   PetscInt      *adjwgt     = NULL;        /* Edge weights */
1786137cd93aSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1787137cd93aSLisandro Dalcin   PetscMPIInt    size, rank, p;
1788137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1789137cd93aSLisandro Dalcin 
1790137cd93aSLisandro Dalcin   PetscFunctionBegin;
1791137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1792137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1793137cd93aSLisandro Dalcin   ierr = PetscMalloc2(nparts+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr);
1794137cd93aSLisandro Dalcin 
1795137cd93aSLisandro Dalcin   /* Calculate vertex distribution */
1796137cd93aSLisandro Dalcin   vtxdist[0] = 0;
1797137cd93aSLisandro Dalcin   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1798137cd93aSLisandro Dalcin   for (p = 2; p <= size; ++p) {
1799137cd93aSLisandro Dalcin     vtxdist[p] += vtxdist[p-1];
1800137cd93aSLisandro Dalcin   }
1801137cd93aSLisandro Dalcin 
1802137cd93aSLisandro Dalcin   if (nparts == 1) {
1803137cd93aSLisandro Dalcin     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
1804137cd93aSLisandro Dalcin   } else {
1805137cd93aSLisandro Dalcin     PetscSection section;
1806137cd93aSLisandro Dalcin     /* Weight cells by dofs on cell by default */
1807137cd93aSLisandro Dalcin     ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr);
1808e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1809137cd93aSLisandro Dalcin     if (section) {
1810137cd93aSLisandro Dalcin       PetscInt vStart, vEnd, dof;
1811137cd93aSLisandro Dalcin       ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1812137cd93aSLisandro Dalcin       for (v = vStart; v < vEnd; ++v) {
1813137cd93aSLisandro Dalcin         ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1814137cd93aSLisandro Dalcin         /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1815137cd93aSLisandro Dalcin         /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1816137cd93aSLisandro Dalcin         if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1);
1817137cd93aSLisandro Dalcin       }
1818137cd93aSLisandro Dalcin     } else {
1819137cd93aSLisandro Dalcin       for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1820137cd93aSLisandro Dalcin     }
1821137cd93aSLisandro Dalcin     {
1822137cd93aSLisandro Dalcin       PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data;
1823137cd93aSLisandro Dalcin       int                       strat = PTScotch_Strategy(pts->strategy);
1824137cd93aSLisandro Dalcin       double                    imbal = (double)pts->imbalance;
1825137cd93aSLisandro Dalcin 
1826137cd93aSLisandro Dalcin       for (p = 0; !vtxdist[p+1] && p < size; ++p);
1827137cd93aSLisandro Dalcin       if (vtxdist[p+1] == vtxdist[size]) {
1828137cd93aSLisandro Dalcin         if (rank == p) {
1829137cd93aSLisandro Dalcin           ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr);
1830137cd93aSLisandro Dalcin         }
1831137cd93aSLisandro Dalcin       } else {
1832137cd93aSLisandro Dalcin         ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr);
1833137cd93aSLisandro Dalcin       }
1834137cd93aSLisandro Dalcin     }
1835137cd93aSLisandro Dalcin     ierr = PetscFree(vwgt);CHKERRQ(ierr);
1836137cd93aSLisandro Dalcin   }
1837137cd93aSLisandro Dalcin 
1838137cd93aSLisandro Dalcin   /* Convert to PetscSection+IS */
1839137cd93aSLisandro Dalcin   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1840137cd93aSLisandro Dalcin   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
1841137cd93aSLisandro Dalcin   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1842137cd93aSLisandro Dalcin   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
1843137cd93aSLisandro Dalcin   for (p = 0, i = 0; p < nparts; ++p) {
1844137cd93aSLisandro Dalcin     for (v = 0; v < nvtxs; ++v) {
1845137cd93aSLisandro Dalcin       if (assignment[v] == p) points[i++] = v;
1846137cd93aSLisandro Dalcin     }
1847137cd93aSLisandro Dalcin   }
1848137cd93aSLisandro Dalcin   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
1849137cd93aSLisandro Dalcin   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1850137cd93aSLisandro Dalcin 
1851137cd93aSLisandro Dalcin   ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr);
1852137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1853137cd93aSLisandro Dalcin #else
1854137cd93aSLisandro Dalcin   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch.");
1855137cd93aSLisandro Dalcin #endif
1856137cd93aSLisandro Dalcin }
1857137cd93aSLisandro Dalcin 
1858137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part)
1859137cd93aSLisandro Dalcin {
1860137cd93aSLisandro Dalcin   PetscFunctionBegin;
1861137cd93aSLisandro Dalcin   part->ops->view           = PetscPartitionerView_PTScotch;
1862137cd93aSLisandro Dalcin   part->ops->destroy        = PetscPartitionerDestroy_PTScotch;
1863137cd93aSLisandro Dalcin   part->ops->partition      = PetscPartitionerPartition_PTScotch;
1864137cd93aSLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch;
1865137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1866137cd93aSLisandro Dalcin }
1867137cd93aSLisandro Dalcin 
1868137cd93aSLisandro Dalcin /*MC
1869137cd93aSLisandro Dalcin   PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library
1870137cd93aSLisandro Dalcin 
1871137cd93aSLisandro Dalcin   Level: intermediate
1872137cd93aSLisandro Dalcin 
1873137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1874137cd93aSLisandro Dalcin M*/
1875137cd93aSLisandro Dalcin 
1876137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part)
1877137cd93aSLisandro Dalcin {
1878137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p;
1879137cd93aSLisandro Dalcin   PetscErrorCode          ierr;
1880137cd93aSLisandro Dalcin 
1881137cd93aSLisandro Dalcin   PetscFunctionBegin;
1882137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1883137cd93aSLisandro Dalcin   ierr = PetscNewLog(part, &p);CHKERRQ(ierr);
1884137cd93aSLisandro Dalcin   part->data = p;
1885137cd93aSLisandro Dalcin 
1886137cd93aSLisandro Dalcin   p->strategy  = 0;
1887137cd93aSLisandro Dalcin   p->imbalance = 0.01;
1888137cd93aSLisandro Dalcin 
1889137cd93aSLisandro Dalcin   ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr);
1890137cd93aSLisandro Dalcin   ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr);
1891137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1892137cd93aSLisandro Dalcin }
1893137cd93aSLisandro Dalcin 
1894137cd93aSLisandro Dalcin 
18955680f57bSMatthew G. Knepley /*@
18965680f57bSMatthew G. Knepley   DMPlexGetPartitioner - Get the mesh partitioner
18975680f57bSMatthew G. Knepley 
18985680f57bSMatthew G. Knepley   Not collective
18995680f57bSMatthew G. Knepley 
19005680f57bSMatthew G. Knepley   Input Parameter:
19015680f57bSMatthew G. Knepley . dm - The DM
19025680f57bSMatthew G. Knepley 
19035680f57bSMatthew G. Knepley   Output Parameter:
19045680f57bSMatthew G. Knepley . part - The PetscPartitioner
19055680f57bSMatthew G. Knepley 
19065680f57bSMatthew G. Knepley   Level: developer
19075680f57bSMatthew G. Knepley 
190898599a47SLawrence Mitchell   Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner.
190998599a47SLawrence Mitchell 
191098599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate()
19115680f57bSMatthew G. Knepley @*/
19125680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part)
19135680f57bSMatthew G. Knepley {
19145680f57bSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
19155680f57bSMatthew G. Knepley 
19165680f57bSMatthew G. Knepley   PetscFunctionBegin;
19175680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19185680f57bSMatthew G. Knepley   PetscValidPointer(part, 2);
19195680f57bSMatthew G. Knepley   *part = mesh->partitioner;
19205680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
19215680f57bSMatthew G. Knepley }
19225680f57bSMatthew G. Knepley 
192371bb2955SLawrence Mitchell /*@
192471bb2955SLawrence Mitchell   DMPlexSetPartitioner - Set the mesh partitioner
192571bb2955SLawrence Mitchell 
192671bb2955SLawrence Mitchell   logically collective on dm and part
192771bb2955SLawrence Mitchell 
192871bb2955SLawrence Mitchell   Input Parameters:
192971bb2955SLawrence Mitchell + dm - The DM
193071bb2955SLawrence Mitchell - part - The partitioner
193171bb2955SLawrence Mitchell 
193271bb2955SLawrence Mitchell   Level: developer
193371bb2955SLawrence Mitchell 
193471bb2955SLawrence Mitchell   Note: Any existing PetscPartitioner will be destroyed.
193571bb2955SLawrence Mitchell 
193671bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate()
193771bb2955SLawrence Mitchell @*/
193871bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part)
193971bb2955SLawrence Mitchell {
194071bb2955SLawrence Mitchell   DM_Plex       *mesh = (DM_Plex *) dm->data;
194171bb2955SLawrence Mitchell   PetscErrorCode ierr;
194271bb2955SLawrence Mitchell 
194371bb2955SLawrence Mitchell   PetscFunctionBegin;
194471bb2955SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
194571bb2955SLawrence Mitchell   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2);
194671bb2955SLawrence Mitchell   ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr);
194771bb2955SLawrence Mitchell   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
194871bb2955SLawrence Mitchell   mesh->partitioner = part;
194971bb2955SLawrence Mitchell   PetscFunctionReturn(0);
195071bb2955SLawrence Mitchell }
195171bb2955SLawrence Mitchell 
1952e8f14785SLisandro Dalcin static PetscErrorCode DMPlexAddClosure_Tree(DM dm, PetscHSetI ht, PetscInt point, PetscBool up, PetscBool down)
1953270bba0cSToby Isaac {
1954270bba0cSToby Isaac   PetscErrorCode ierr;
1955270bba0cSToby Isaac 
1956270bba0cSToby Isaac   PetscFunctionBegin;
19576a5a2ffdSToby Isaac   if (up) {
19586a5a2ffdSToby Isaac     PetscInt parent;
19596a5a2ffdSToby Isaac 
1960270bba0cSToby Isaac     ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr);
19616a5a2ffdSToby Isaac     if (parent != point) {
19626a5a2ffdSToby Isaac       PetscInt closureSize, *closure = NULL, i;
19636a5a2ffdSToby Isaac 
1964270bba0cSToby Isaac       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1965270bba0cSToby Isaac       for (i = 0; i < closureSize; i++) {
1966270bba0cSToby Isaac         PetscInt cpoint = closure[2*i];
1967270bba0cSToby Isaac 
1968e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
19691b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr);
1970270bba0cSToby Isaac       }
1971270bba0cSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
19726a5a2ffdSToby Isaac     }
19736a5a2ffdSToby Isaac   }
19746a5a2ffdSToby Isaac   if (down) {
19756a5a2ffdSToby Isaac     PetscInt numChildren;
19766a5a2ffdSToby Isaac     const PetscInt *children;
19776a5a2ffdSToby Isaac 
19786a5a2ffdSToby Isaac     ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr);
19796a5a2ffdSToby Isaac     if (numChildren) {
19806a5a2ffdSToby Isaac       PetscInt i;
19816a5a2ffdSToby Isaac 
19826a5a2ffdSToby Isaac       for (i = 0; i < numChildren; i++) {
19836a5a2ffdSToby Isaac         PetscInt cpoint = children[i];
19846a5a2ffdSToby Isaac 
1985e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
19861b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr);
19876a5a2ffdSToby Isaac       }
19886a5a2ffdSToby Isaac     }
19896a5a2ffdSToby Isaac   }
1990270bba0cSToby Isaac   PetscFunctionReturn(0);
1991270bba0cSToby Isaac }
1992270bba0cSToby Isaac 
19935abbe4feSMichael Lange /*@
19945abbe4feSMichael Lange   DMPlexPartitionLabelClosure - Add the closure of all points to the partition label
19955abbe4feSMichael Lange 
19965abbe4feSMichael Lange   Input Parameters:
19975abbe4feSMichael Lange + dm     - The DM
19985abbe4feSMichael Lange - label  - DMLabel assinging ranks to remote roots
19995abbe4feSMichael Lange 
20005abbe4feSMichael Lange   Level: developer
20015abbe4feSMichael Lange 
20025abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
20035abbe4feSMichael Lange @*/
20045abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label)
20059ffc88e4SToby Isaac {
20065abbe4feSMichael Lange   IS              rankIS,   pointIS;
20075abbe4feSMichael Lange   const PetscInt *ranks,   *points;
20085abbe4feSMichael Lange   PetscInt        numRanks, numPoints, r, p, c, closureSize;
20095abbe4feSMichael Lange   PetscInt       *closure = NULL;
20101b807c88SLisandro Dalcin   DM_Plex        *mesh    = (DM_Plex *)dm->data;
20111b807c88SLisandro Dalcin   PetscBool       hasTree = (mesh->parentSection || mesh->childSection) ? PETSC_TRUE : PETSC_FALSE;
20129ffc88e4SToby Isaac   PetscErrorCode  ierr;
20139ffc88e4SToby Isaac 
20149ffc88e4SToby Isaac   PetscFunctionBegin;
20155abbe4feSMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
20165abbe4feSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
20175abbe4feSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
20181b807c88SLisandro Dalcin 
20195abbe4feSMichael Lange   for (r = 0; r < numRanks; ++r) {
20205abbe4feSMichael Lange     const PetscInt rank = ranks[r];
2021e8f14785SLisandro Dalcin     PetscHSetI     ht;
2022e8f14785SLisandro Dalcin     PetscInt       nelems, *elems, off = 0;
20239ffc88e4SToby Isaac 
20245abbe4feSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
20255abbe4feSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
20265abbe4feSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2027e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
2028e8f14785SLisandro Dalcin     ierr = PetscHSetIResize(ht, numPoints*16);CHKERRQ(ierr);
20295abbe4feSMichael Lange     for (p = 0; p < numPoints; ++p) {
20305abbe4feSMichael Lange       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2031270bba0cSToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
2032e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, closure[c]);CHKERRQ(ierr);
20331b807c88SLisandro Dalcin         if (hasTree) {ierr = DMPlexAddClosure_Tree(dm, ht, closure[c], PETSC_TRUE, PETSC_TRUE);CHKERRQ(ierr);}
2034270bba0cSToby Isaac       }
20359ffc88e4SToby Isaac     }
20365abbe4feSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
20375abbe4feSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2038e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(ht, &nelems);CHKERRQ(ierr);
2039e8f14785SLisandro Dalcin     ierr = PetscMalloc1(nelems, &elems);CHKERRQ(ierr);
2040f5a7d1c1SBarry Smith     ierr = PetscHSetIGetElems(ht, &off, elems);CHKERRQ(ierr);
2041e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
2042e8f14785SLisandro Dalcin     ierr = PetscSortInt(nelems, elems);CHKERRQ(ierr);
2043e8f14785SLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF, nelems, elems, PETSC_OWN_POINTER, &pointIS);CHKERRQ(ierr);
20441b807c88SLisandro Dalcin     ierr = DMLabelSetStratumIS(label, rank, pointIS);CHKERRQ(ierr);
20451b807c88SLisandro Dalcin     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
20469ffc88e4SToby Isaac   }
20471b807c88SLisandro Dalcin 
20481b807c88SLisandro Dalcin   if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);}
20495abbe4feSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
20505abbe4feSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
20519ffc88e4SToby Isaac   PetscFunctionReturn(0);
20529ffc88e4SToby Isaac }
20539ffc88e4SToby Isaac 
205424d039d7SMichael Lange /*@
205524d039d7SMichael Lange   DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label
205624d039d7SMichael Lange 
205724d039d7SMichael Lange   Input Parameters:
205824d039d7SMichael Lange + dm     - The DM
205924d039d7SMichael Lange - label  - DMLabel assinging ranks to remote roots
206024d039d7SMichael Lange 
206124d039d7SMichael Lange   Level: developer
206224d039d7SMichael Lange 
206324d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
206424d039d7SMichael Lange @*/
206524d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label)
206670034214SMatthew G. Knepley {
206724d039d7SMichael Lange   IS              rankIS,   pointIS;
206824d039d7SMichael Lange   const PetscInt *ranks,   *points;
206924d039d7SMichael Lange   PetscInt        numRanks, numPoints, r, p, a, adjSize;
207024d039d7SMichael Lange   PetscInt       *adj = NULL;
207170034214SMatthew G. Knepley   PetscErrorCode  ierr;
207270034214SMatthew G. Knepley 
207370034214SMatthew G. Knepley   PetscFunctionBegin;
207424d039d7SMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
207524d039d7SMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
207624d039d7SMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
207724d039d7SMichael Lange   for (r = 0; r < numRanks; ++r) {
207824d039d7SMichael Lange     const PetscInt rank = ranks[r];
207970034214SMatthew G. Knepley 
208024d039d7SMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
208124d039d7SMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
208224d039d7SMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
208370034214SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
208424d039d7SMichael Lange       adjSize = PETSC_DETERMINE;
208524d039d7SMichael Lange       ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr);
208624d039d7SMichael Lange       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);}
208770034214SMatthew G. Knepley     }
208824d039d7SMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
208924d039d7SMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
209070034214SMatthew G. Knepley   }
209124d039d7SMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
209224d039d7SMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
209324d039d7SMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
209424d039d7SMichael Lange   PetscFunctionReturn(0);
209570034214SMatthew G. Knepley }
209670034214SMatthew G. Knepley 
2097be200f8dSMichael Lange /*@
2098be200f8dSMichael Lange   DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF
2099be200f8dSMichael Lange 
2100be200f8dSMichael Lange   Input Parameters:
2101be200f8dSMichael Lange + dm     - The DM
2102be200f8dSMichael Lange - label  - DMLabel assinging ranks to remote roots
2103be200f8dSMichael Lange 
2104be200f8dSMichael Lange   Level: developer
2105be200f8dSMichael Lange 
2106be200f8dSMichael Lange   Note: This is required when generating multi-level overlaps to capture
2107be200f8dSMichael Lange   overlap points from non-neighbouring partitions.
2108be200f8dSMichael Lange 
2109be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
2110be200f8dSMichael Lange @*/
2111be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label)
2112be200f8dSMichael Lange {
2113be200f8dSMichael Lange   MPI_Comm        comm;
2114be200f8dSMichael Lange   PetscMPIInt     rank;
2115be200f8dSMichael Lange   PetscSF         sfPoint;
21165d04f6ebSMichael Lange   DMLabel         lblRoots, lblLeaves;
2117be200f8dSMichael Lange   IS              rankIS, pointIS;
2118be200f8dSMichael Lange   const PetscInt *ranks;
2119be200f8dSMichael Lange   PetscInt        numRanks, r;
2120be200f8dSMichael Lange   PetscErrorCode  ierr;
2121be200f8dSMichael Lange 
2122be200f8dSMichael Lange   PetscFunctionBegin;
2123be200f8dSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
2124be200f8dSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2125be200f8dSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
21265d04f6ebSMichael Lange   /* Pull point contributions from remote leaves into local roots */
21275d04f6ebSMichael Lange   ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
21285d04f6ebSMichael Lange   ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr);
21295d04f6ebSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
21305d04f6ebSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
21315d04f6ebSMichael Lange   for (r = 0; r < numRanks; ++r) {
21325d04f6ebSMichael Lange     const PetscInt remoteRank = ranks[r];
21335d04f6ebSMichael Lange     if (remoteRank == rank) continue;
21345d04f6ebSMichael Lange     ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr);
21355d04f6ebSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
21365d04f6ebSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21375d04f6ebSMichael Lange   }
21385d04f6ebSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
21395d04f6ebSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
21405d04f6ebSMichael Lange   ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
2141be200f8dSMichael Lange   /* Push point contributions from roots into remote leaves */
2142be200f8dSMichael Lange   ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
2143be200f8dSMichael Lange   ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr);
2144be200f8dSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
2145be200f8dSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
2146be200f8dSMichael Lange   for (r = 0; r < numRanks; ++r) {
2147be200f8dSMichael Lange     const PetscInt remoteRank = ranks[r];
2148be200f8dSMichael Lange     if (remoteRank == rank) continue;
2149be200f8dSMichael Lange     ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr);
2150be200f8dSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
2151be200f8dSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2152be200f8dSMichael Lange   }
2153be200f8dSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
2154be200f8dSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
2155be200f8dSMichael Lange   ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
2156be200f8dSMichael Lange   PetscFunctionReturn(0);
2157be200f8dSMichael Lange }
2158be200f8dSMichael Lange 
21591fd9873aSMichael Lange /*@
21601fd9873aSMichael Lange   DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label
21611fd9873aSMichael Lange 
21621fd9873aSMichael Lange   Input Parameters:
21631fd9873aSMichael Lange + dm        - The DM
21641fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots
21651fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank
21661fd9873aSMichael Lange 
21671fd9873aSMichael Lange   Output Parameter:
21681fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots
21691fd9873aSMichael Lange 
21701fd9873aSMichael Lange   Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The
21711fd9873aSMichael Lange   resulting leafLabel is a receiver mapping of remote roots to their parent rank.
21721fd9873aSMichael Lange 
21731fd9873aSMichael Lange   Level: developer
21741fd9873aSMichael Lange 
21751fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
21761fd9873aSMichael Lange @*/
21771fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel)
21781fd9873aSMichael Lange {
21791fd9873aSMichael Lange   MPI_Comm           comm;
21809852e123SBarry Smith   PetscMPIInt        rank, size;
21819852e123SBarry Smith   PetscInt           p, n, numNeighbors, ssize, l, nleaves;
21821fd9873aSMichael Lange   PetscSF            sfPoint;
21831fd9873aSMichael Lange   PetscSFNode       *rootPoints, *leafPoints;
21841fd9873aSMichael Lange   PetscSection       rootSection, leafSection;
21851fd9873aSMichael Lange   const PetscSFNode *remote;
21861fd9873aSMichael Lange   const PetscInt    *local, *neighbors;
21871fd9873aSMichael Lange   IS                 valueIS;
21881fd9873aSMichael Lange   PetscErrorCode     ierr;
21891fd9873aSMichael Lange 
21901fd9873aSMichael Lange   PetscFunctionBegin;
21911fd9873aSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
21921fd9873aSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
21939852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
21941fd9873aSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
21951fd9873aSMichael Lange 
21961fd9873aSMichael Lange   /* Convert to (point, rank) and use actual owners */
21971fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
21989852e123SBarry Smith   ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr);
21991fd9873aSMichael Lange   ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr);
22001fd9873aSMichael Lange   ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr);
22011fd9873aSMichael Lange   ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr);
22021fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22031fd9873aSMichael Lange     PetscInt numPoints;
22041fd9873aSMichael Lange 
22051fd9873aSMichael Lange     ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr);
22061fd9873aSMichael Lange     ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr);
22071fd9873aSMichael Lange   }
22081fd9873aSMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
22099852e123SBarry Smith   ierr = PetscSectionGetStorageSize(rootSection, &ssize);CHKERRQ(ierr);
22109852e123SBarry Smith   ierr = PetscMalloc1(ssize, &rootPoints);CHKERRQ(ierr);
22111fd9873aSMichael Lange   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
22121fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
22131fd9873aSMichael Lange     IS              pointIS;
22141fd9873aSMichael Lange     const PetscInt *points;
22151fd9873aSMichael Lange     PetscInt        off, numPoints, p;
22161fd9873aSMichael Lange 
22171fd9873aSMichael Lange     ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr);
22181fd9873aSMichael Lange     ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr);
22191fd9873aSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22201fd9873aSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
22211fd9873aSMichael Lange     for (p = 0; p < numPoints; ++p) {
2222f8987ae8SMichael Lange       if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);}
2223f8987ae8SMichael Lange       else       {l = -1;}
22241fd9873aSMichael Lange       if (l >= 0) {rootPoints[off+p] = remote[l];}
22251fd9873aSMichael Lange       else        {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;}
22261fd9873aSMichael Lange     }
22271fd9873aSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
22281fd9873aSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22291fd9873aSMichael Lange   }
22301fd9873aSMichael Lange   ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr);
22311fd9873aSMichael Lange   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
22321fd9873aSMichael Lange   /* Communicate overlap */
22331fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
22341fd9873aSMichael Lange   ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr);
22351fd9873aSMichael Lange   /* Filter remote contributions (ovLeafPoints) into the overlapSF */
22369852e123SBarry Smith   ierr = PetscSectionGetStorageSize(leafSection, &ssize);CHKERRQ(ierr);
22379852e123SBarry Smith   for (p = 0; p < ssize; p++) {
22381fd9873aSMichael Lange     ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr);
22391fd9873aSMichael Lange   }
22401fd9873aSMichael Lange   ierr = PetscFree(rootPoints);CHKERRQ(ierr);
22411fd9873aSMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
22421fd9873aSMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
22431fd9873aSMichael Lange   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
22441fd9873aSMichael Lange   PetscFunctionReturn(0);
22451fd9873aSMichael Lange }
22461fd9873aSMichael Lange 
2247aa3148a8SMichael Lange /*@
2248aa3148a8SMichael Lange   DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points
2249aa3148a8SMichael Lange 
2250aa3148a8SMichael Lange   Input Parameters:
2251aa3148a8SMichael Lange + dm    - The DM
2252aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots
2253aa3148a8SMichael Lange 
2254aa3148a8SMichael Lange   Output Parameter:
2255aa3148a8SMichael Lange - sf    - The star forest communication context encapsulating the defined mapping
2256aa3148a8SMichael Lange 
2257aa3148a8SMichael Lange   Note: The incoming label is a receiver mapping of remote points to their parent rank.
2258aa3148a8SMichael Lange 
2259aa3148a8SMichael Lange   Level: developer
2260aa3148a8SMichael Lange 
2261aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap
2262aa3148a8SMichael Lange @*/
2263aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf)
2264aa3148a8SMichael Lange {
22659852e123SBarry Smith   PetscMPIInt     rank, size;
226643f7d02bSMichael Lange   PetscInt        n, numRemote, p, numPoints, pStart, pEnd, idx = 0;
2267aa3148a8SMichael Lange   PetscSFNode    *remotePoints;
226843f7d02bSMichael Lange   IS              remoteRootIS;
226943f7d02bSMichael Lange   const PetscInt *remoteRoots;
2270aa3148a8SMichael Lange   PetscErrorCode ierr;
2271aa3148a8SMichael Lange 
2272aa3148a8SMichael Lange   PetscFunctionBegin;
227343f7d02bSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
22749852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
2275aa3148a8SMichael Lange 
22769852e123SBarry Smith   for (numRemote = 0, n = 0; n < size; ++n) {
2277aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
2278aa3148a8SMichael Lange     numRemote += numPoints;
2279aa3148a8SMichael Lange   }
2280aa3148a8SMichael Lange   ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr);
228143f7d02bSMichael Lange   /* Put owned points first */
228243f7d02bSMichael Lange   ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr);
228343f7d02bSMichael Lange   if (numPoints > 0) {
228443f7d02bSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr);
228543f7d02bSMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
228643f7d02bSMichael Lange     for (p = 0; p < numPoints; p++) {
228743f7d02bSMichael Lange       remotePoints[idx].index = remoteRoots[p];
228843f7d02bSMichael Lange       remotePoints[idx].rank = rank;
228943f7d02bSMichael Lange       idx++;
229043f7d02bSMichael Lange     }
229143f7d02bSMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
229243f7d02bSMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
229343f7d02bSMichael Lange   }
229443f7d02bSMichael Lange   /* Now add remote points */
22959852e123SBarry Smith   for (n = 0; n < size; ++n) {
2296aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
229743f7d02bSMichael Lange     if (numPoints <= 0 || n == rank) continue;
2298aa3148a8SMichael Lange     ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr);
2299aa3148a8SMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2300aa3148a8SMichael Lange     for (p = 0; p < numPoints; p++) {
2301aa3148a8SMichael Lange       remotePoints[idx].index = remoteRoots[p];
2302aa3148a8SMichael Lange       remotePoints[idx].rank = n;
2303aa3148a8SMichael Lange       idx++;
2304aa3148a8SMichael Lange     }
2305aa3148a8SMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2306aa3148a8SMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
2307aa3148a8SMichael Lange   }
2308aa3148a8SMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr);
2309aa3148a8SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2310aa3148a8SMichael Lange   ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
231170034214SMatthew G. Knepley   PetscFunctionReturn(0);
231270034214SMatthew G. Knepley }
2313