xref: /petsc/src/dm/impls/plex/plexpartition.c (revision f5a7d1c1723563a4dc0b31d5466b5c43921d574a)
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 {
5343f7d02bSMichael Lange   PetscInt       p, pStart, pEnd, a, adjSize, idx, size, nroots;
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;
61532c4e7dSMichael Lange   PetscMPIInt    rank;
62532c4e7dSMichael Lange   PetscErrorCode ierr;
63532c4e7dSMichael Lange 
64532c4e7dSMichael Lange   PetscFunctionBegin;
65532c4e7dSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
66532c4e7dSMichael Lange   ierr = DMPlexGetHeightStratum(dm, height, &pStart, &pEnd);CHKERRQ(ierr);
678cfe4c1fSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
688cfe4c1fSMichael Lange   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
69532c4e7dSMichael Lange   /* Build adjacency graph via a section/segbuffer */
70532c4e7dSMichael Lange   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
71532c4e7dSMichael Lange   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
72532c4e7dSMichael Lange   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);CHKERRQ(ierr);
73532c4e7dSMichael Lange   /* Always use FVM adjacency to create partitioner graph */
74532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseCone(dm, &useCone);CHKERRQ(ierr);
75532c4e7dSMichael Lange   ierr = DMPlexGetAdjacencyUseClosure(dm, &useClosure);CHKERRQ(ierr);
76532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, PETSC_TRUE);CHKERRQ(ierr);
77532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, PETSC_FALSE);CHKERRQ(ierr);
78f0927f4eSMatthew G. Knepley   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &cellNumbering);CHKERRQ(ierr);
793fa7752dSToby Isaac   if (globalNumbering) {
803fa7752dSToby Isaac     ierr = PetscObjectReference((PetscObject)cellNumbering);CHKERRQ(ierr);
813fa7752dSToby Isaac     *globalNumbering = cellNumbering;
823fa7752dSToby Isaac   }
838cfe4c1fSMichael Lange   ierr = ISGetIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
848cfe4c1fSMichael Lange   for (*numVertices = 0, p = pStart; p < pEnd; p++) {
858cfe4c1fSMichael Lange     /* Skip non-owned cells in parallel (ParMetis expects no overlap) */
868cfe4c1fSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
87532c4e7dSMichael Lange     adjSize = PETSC_DETERMINE;
88532c4e7dSMichael Lange     ierr = DMPlexGetAdjacency(dm, p, &adjSize, &adj);CHKERRQ(ierr);
89532c4e7dSMichael Lange     for (a = 0; a < adjSize; ++a) {
90532c4e7dSMichael Lange       const PetscInt point = adj[a];
91532c4e7dSMichael Lange       if (point != p && pStart <= point && point < pEnd) {
92532c4e7dSMichael Lange         PetscInt *PETSC_RESTRICT pBuf;
93532c4e7dSMichael Lange         ierr = PetscSectionAddDof(section, p, 1);CHKERRQ(ierr);
94532c4e7dSMichael Lange         ierr = PetscSegBufferGetInts(adjBuffer, 1, &pBuf);CHKERRQ(ierr);
95532c4e7dSMichael Lange         *pBuf = point;
96532c4e7dSMichael Lange       }
97532c4e7dSMichael Lange     }
988cfe4c1fSMichael Lange     (*numVertices)++;
99532c4e7dSMichael Lange   }
100532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseCone(dm, useCone);CHKERRQ(ierr);
101532c4e7dSMichael Lange   ierr = DMPlexSetAdjacencyUseClosure(dm, useClosure);CHKERRQ(ierr);
102532c4e7dSMichael Lange   /* Derive CSR graph from section/segbuffer */
103532c4e7dSMichael Lange   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
104532c4e7dSMichael Lange   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
105389e55d8SMichael Lange   ierr = PetscMalloc1(*numVertices+1, &vOffsets);CHKERRQ(ierr);
10643f7d02bSMichael Lange   for (idx = 0, p = pStart; p < pEnd; p++) {
10743f7d02bSMichael Lange     if (nroots > 0) {if (cellNum[p] < 0) continue;}
10843f7d02bSMichael Lange     ierr = PetscSectionGetOffset(section, p, &(vOffsets[idx++]));CHKERRQ(ierr);
10943f7d02bSMichael Lange   }
110532c4e7dSMichael Lange   vOffsets[*numVertices] = size;
111532c4e7dSMichael Lange   if (offsets) *offsets = vOffsets;
112389e55d8SMichael Lange   ierr = PetscSegBufferExtractAlloc(adjBuffer, &graph);CHKERRQ(ierr);
113bf4602e4SToby Isaac   {
1148cfe4c1fSMichael Lange     ISLocalToGlobalMapping ltogCells;
1158cfe4c1fSMichael Lange     PetscInt n, size, *cells_arr;
1168cfe4c1fSMichael Lange     /* In parallel, apply a global cell numbering to the graph */
1178cfe4c1fSMichael Lange     ierr = ISRestoreIndices(cellNumbering, &cellNum);CHKERRQ(ierr);
1188cfe4c1fSMichael Lange     ierr = ISLocalToGlobalMappingCreateIS(cellNumbering, &ltogCells);CHKERRQ(ierr);
1198cfe4c1fSMichael Lange     ierr = ISLocalToGlobalMappingGetSize(ltogCells, &size);CHKERRQ(ierr);
1208cfe4c1fSMichael Lange     ierr = ISLocalToGlobalMappingGetIndices(ltogCells, (const PetscInt**)&cells_arr);CHKERRQ(ierr);
1218cfe4c1fSMichael Lange     /* Convert to positive global cell numbers */
1228cfe4c1fSMichael Lange     for (n=0; n<size; n++) {if (cells_arr[n] < 0) cells_arr[n] = -(cells_arr[n]+1);}
1238cfe4c1fSMichael Lange     ierr = ISLocalToGlobalMappingRestoreIndices(ltogCells, (const PetscInt**)&cells_arr);CHKERRQ(ierr);
124389e55d8SMichael Lange     ierr = ISLocalToGlobalMappingApplyBlock(ltogCells, vOffsets[*numVertices], graph, graph);CHKERRQ(ierr);
1258cfe4c1fSMichael Lange     ierr = ISLocalToGlobalMappingDestroy(&ltogCells);CHKERRQ(ierr);
126f0927f4eSMatthew G. Knepley     ierr = ISDestroy(&cellNumbering);CHKERRQ(ierr);
1278cfe4c1fSMichael Lange   }
128389e55d8SMichael Lange   if (adjacency) *adjacency = graph;
129532c4e7dSMichael Lange   /* Clean up */
130532c4e7dSMichael Lange   ierr = PetscSegBufferDestroy(&adjBuffer);CHKERRQ(ierr);
131532c4e7dSMichael Lange   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
132532c4e7dSMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
133532c4e7dSMichael Lange   PetscFunctionReturn(0);
134532c4e7dSMichael Lange }
135532c4e7dSMichael Lange 
136d5577e40SMatthew G. Knepley /*@C
137d5577e40SMatthew G. Knepley   DMPlexCreateNeighborCSR - Create a mesh graph (cell-cell adjacency) in parallel CSR format.
138d5577e40SMatthew G. Knepley 
139d5577e40SMatthew G. Knepley   Collective
140d5577e40SMatthew G. Knepley 
141d5577e40SMatthew G. Knepley   Input Arguments:
142d5577e40SMatthew G. Knepley + dm - The DMPlex
143d5577e40SMatthew G. Knepley - cellHeight - The height of mesh points to treat as cells (default should be 0)
144d5577e40SMatthew G. Knepley 
145d5577e40SMatthew G. Knepley   Output Arguments:
146d5577e40SMatthew G. Knepley + numVertices - The number of local vertices in the graph, or cells in the mesh.
147d5577e40SMatthew G. Knepley . offsets     - The offset to the adjacency list for each cell
148d5577e40SMatthew G. Knepley - adjacency   - The adjacency list for all cells
149d5577e40SMatthew G. Knepley 
150d5577e40SMatthew G. Knepley   Note: This is suitable for input to a mesh partitioner like ParMetis.
151d5577e40SMatthew G. Knepley 
152d5577e40SMatthew G. Knepley   Level: advanced
153d5577e40SMatthew G. Knepley 
154d5577e40SMatthew G. Knepley .seealso: DMPlexCreate()
155d5577e40SMatthew G. Knepley @*/
15670034214SMatthew G. Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency)
15770034214SMatthew G. Knepley {
15870034214SMatthew G. Knepley   const PetscInt maxFaceCases = 30;
15970034214SMatthew G. Knepley   PetscInt       numFaceCases = 0;
16070034214SMatthew G. Knepley   PetscInt       numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */
16170034214SMatthew G. Knepley   PetscInt      *off, *adj;
16270034214SMatthew G. Knepley   PetscInt      *neighborCells = NULL;
16370034214SMatthew G. Knepley   PetscInt       dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell;
16470034214SMatthew G. Knepley   PetscErrorCode ierr;
16570034214SMatthew G. Knepley 
16670034214SMatthew G. Knepley   PetscFunctionBegin;
16770034214SMatthew G. Knepley   /* For parallel partitioning, I think you have to communicate supports */
168c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16970034214SMatthew G. Knepley   cellDim = dim - cellHeight;
17070034214SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17170034214SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
17270034214SMatthew G. Knepley   if (cEnd - cStart == 0) {
17370034214SMatthew G. Knepley     if (numVertices) *numVertices = 0;
17470034214SMatthew G. Knepley     if (offsets)   *offsets   = NULL;
17570034214SMatthew G. Knepley     if (adjacency) *adjacency = NULL;
17670034214SMatthew G. Knepley     PetscFunctionReturn(0);
17770034214SMatthew G. Knepley   }
17870034214SMatthew G. Knepley   numCells  = cEnd - cStart;
17970034214SMatthew G. Knepley   faceDepth = depth - cellHeight;
18070034214SMatthew G. Knepley   if (dim == depth) {
18170034214SMatthew G. Knepley     PetscInt f, fStart, fEnd;
18270034214SMatthew G. Knepley 
18370034214SMatthew G. Knepley     ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
18470034214SMatthew G. Knepley     /* Count neighboring cells */
18570034214SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
18670034214SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
18770034214SMatthew G. Knepley       const PetscInt *support;
18870034214SMatthew G. Knepley       PetscInt        supportSize;
18970034214SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
19070034214SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
19170034214SMatthew G. Knepley       if (supportSize == 2) {
1929ffc88e4SToby Isaac         PetscInt numChildren;
1939ffc88e4SToby Isaac 
1949ffc88e4SToby Isaac         ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
1959ffc88e4SToby Isaac         if (!numChildren) {
19670034214SMatthew G. Knepley           ++off[support[0]-cStart+1];
19770034214SMatthew G. Knepley           ++off[support[1]-cStart+1];
19870034214SMatthew G. Knepley         }
19970034214SMatthew G. Knepley       }
2009ffc88e4SToby Isaac     }
20170034214SMatthew G. Knepley     /* Prefix sum */
20270034214SMatthew G. Knepley     for (c = 1; c <= numCells; ++c) off[c] += off[c-1];
20370034214SMatthew G. Knepley     if (adjacency) {
20470034214SMatthew G. Knepley       PetscInt *tmp;
20570034214SMatthew G. Knepley 
20670034214SMatthew G. Knepley       ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
207854ce69bSBarry Smith       ierr = PetscMalloc1(numCells+1, &tmp);CHKERRQ(ierr);
20870034214SMatthew G. Knepley       ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr);
20970034214SMatthew G. Knepley       /* Get neighboring cells */
21070034214SMatthew G. Knepley       for (f = fStart; f < fEnd; ++f) {
21170034214SMatthew G. Knepley         const PetscInt *support;
21270034214SMatthew G. Knepley         PetscInt        supportSize;
21370034214SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
21470034214SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
21570034214SMatthew G. Knepley         if (supportSize == 2) {
2169ffc88e4SToby Isaac           PetscInt numChildren;
2179ffc88e4SToby Isaac 
2189ffc88e4SToby Isaac           ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2199ffc88e4SToby Isaac           if (!numChildren) {
22070034214SMatthew G. Knepley             adj[tmp[support[0]-cStart]++] = support[1];
22170034214SMatthew G. Knepley             adj[tmp[support[1]-cStart]++] = support[0];
22270034214SMatthew G. Knepley           }
22370034214SMatthew G. Knepley         }
2249ffc88e4SToby Isaac       }
22570034214SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
22670034214SMatthew 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);
22770034214SMatthew G. Knepley #endif
22870034214SMatthew G. Knepley       ierr = PetscFree(tmp);CHKERRQ(ierr);
22970034214SMatthew G. Knepley     }
23070034214SMatthew G. Knepley     if (numVertices) *numVertices = numCells;
23170034214SMatthew G. Knepley     if (offsets)   *offsets   = off;
23270034214SMatthew G. Knepley     if (adjacency) *adjacency = adj;
23370034214SMatthew G. Knepley     PetscFunctionReturn(0);
23470034214SMatthew G. Knepley   }
23570034214SMatthew G. Knepley   /* Setup face recognition */
23670034214SMatthew G. Knepley   if (faceDepth == 1) {
23770034214SMatthew 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 */
23870034214SMatthew G. Knepley 
23970034214SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
24070034214SMatthew G. Knepley       PetscInt corners;
24170034214SMatthew G. Knepley 
24270034214SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr);
24370034214SMatthew G. Knepley       if (!cornersSeen[corners]) {
24470034214SMatthew G. Knepley         PetscInt nFV;
24570034214SMatthew G. Knepley 
24670034214SMatthew G. Knepley         if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases");
24770034214SMatthew G. Knepley         cornersSeen[corners] = 1;
24870034214SMatthew G. Knepley 
24970034214SMatthew G. Knepley         ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr);
25070034214SMatthew G. Knepley 
25170034214SMatthew G. Knepley         numFaceVertices[numFaceCases++] = nFV;
25270034214SMatthew G. Knepley       }
25370034214SMatthew G. Knepley     }
25470034214SMatthew G. Knepley   }
25570034214SMatthew G. Knepley   ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
25670034214SMatthew G. Knepley   /* Count neighboring cells */
25770034214SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
25870034214SMatthew G. Knepley     PetscInt numNeighbors = PETSC_DETERMINE, n;
25970034214SMatthew G. Knepley 
2608b0b4c70SToby Isaac     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
26170034214SMatthew G. Knepley     /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
26270034214SMatthew G. Knepley     for (n = 0; n < numNeighbors; ++n) {
26370034214SMatthew G. Knepley       PetscInt        cellPair[2];
26470034214SMatthew G. Knepley       PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
26570034214SMatthew G. Knepley       PetscInt        meetSize = 0;
26670034214SMatthew G. Knepley       const PetscInt *meet    = NULL;
26770034214SMatthew G. Knepley 
26870034214SMatthew G. Knepley       cellPair[0] = cell; cellPair[1] = neighborCells[n];
26970034214SMatthew G. Knepley       if (cellPair[0] == cellPair[1]) continue;
27070034214SMatthew G. Knepley       if (!found) {
27170034214SMatthew G. Knepley         ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
27270034214SMatthew G. Knepley         if (meetSize) {
27370034214SMatthew G. Knepley           PetscInt f;
27470034214SMatthew G. Knepley 
27570034214SMatthew G. Knepley           for (f = 0; f < numFaceCases; ++f) {
27670034214SMatthew G. Knepley             if (numFaceVertices[f] == meetSize) {
27770034214SMatthew G. Knepley               found = PETSC_TRUE;
27870034214SMatthew G. Knepley               break;
27970034214SMatthew G. Knepley             }
28070034214SMatthew G. Knepley           }
28170034214SMatthew G. Knepley         }
28270034214SMatthew G. Knepley         ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
28370034214SMatthew G. Knepley       }
28470034214SMatthew G. Knepley       if (found) ++off[cell-cStart+1];
28570034214SMatthew G. Knepley     }
28670034214SMatthew G. Knepley   }
28770034214SMatthew G. Knepley   /* Prefix sum */
28870034214SMatthew G. Knepley   for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1];
28970034214SMatthew G. Knepley 
29070034214SMatthew G. Knepley   if (adjacency) {
29170034214SMatthew G. Knepley     ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
29270034214SMatthew G. Knepley     /* Get neighboring cells */
29370034214SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
29470034214SMatthew G. Knepley       PetscInt numNeighbors = PETSC_DETERMINE, n;
29570034214SMatthew G. Knepley       PetscInt cellOffset   = 0;
29670034214SMatthew G. Knepley 
2978b0b4c70SToby Isaac       ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &numNeighbors, &neighborCells);CHKERRQ(ierr);
29870034214SMatthew G. Knepley       /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
29970034214SMatthew G. Knepley       for (n = 0; n < numNeighbors; ++n) {
30070034214SMatthew G. Knepley         PetscInt        cellPair[2];
30170034214SMatthew G. Knepley         PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
30270034214SMatthew G. Knepley         PetscInt        meetSize = 0;
30370034214SMatthew G. Knepley         const PetscInt *meet    = NULL;
30470034214SMatthew G. Knepley 
30570034214SMatthew G. Knepley         cellPair[0] = cell; cellPair[1] = neighborCells[n];
30670034214SMatthew G. Knepley         if (cellPair[0] == cellPair[1]) continue;
30770034214SMatthew G. Knepley         if (!found) {
30870034214SMatthew G. Knepley           ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
30970034214SMatthew G. Knepley           if (meetSize) {
31070034214SMatthew G. Knepley             PetscInt f;
31170034214SMatthew G. Knepley 
31270034214SMatthew G. Knepley             for (f = 0; f < numFaceCases; ++f) {
31370034214SMatthew G. Knepley               if (numFaceVertices[f] == meetSize) {
31470034214SMatthew G. Knepley                 found = PETSC_TRUE;
31570034214SMatthew G. Knepley                 break;
31670034214SMatthew G. Knepley               }
31770034214SMatthew G. Knepley             }
31870034214SMatthew G. Knepley           }
31970034214SMatthew G. Knepley           ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
32070034214SMatthew G. Knepley         }
32170034214SMatthew G. Knepley         if (found) {
32270034214SMatthew G. Knepley           adj[off[cell-cStart]+cellOffset] = neighborCells[n];
32370034214SMatthew G. Knepley           ++cellOffset;
32470034214SMatthew G. Knepley         }
32570034214SMatthew G. Knepley       }
32670034214SMatthew G. Knepley     }
32770034214SMatthew G. Knepley   }
32870034214SMatthew G. Knepley   ierr = PetscFree(neighborCells);CHKERRQ(ierr);
32970034214SMatthew G. Knepley   if (numVertices) *numVertices = numCells;
33070034214SMatthew G. Knepley   if (offsets)   *offsets   = off;
33170034214SMatthew G. Knepley   if (adjacency) *adjacency = adj;
33270034214SMatthew G. Knepley   PetscFunctionReturn(0);
33370034214SMatthew G. Knepley }
33470034214SMatthew G. Knepley 
33577623264SMatthew G. Knepley /*@C
33677623264SMatthew G. Knepley   PetscPartitionerRegister - Adds a new PetscPartitioner implementation
33777623264SMatthew G. Knepley 
33877623264SMatthew G. Knepley   Not Collective
33977623264SMatthew G. Knepley 
34077623264SMatthew G. Knepley   Input Parameters:
34177623264SMatthew G. Knepley + name        - The name of a new user-defined creation routine
34277623264SMatthew G. Knepley - create_func - The creation routine itself
34377623264SMatthew G. Knepley 
34477623264SMatthew G. Knepley   Notes:
34577623264SMatthew G. Knepley   PetscPartitionerRegister() may be called multiple times to add several user-defined PetscPartitioners
34677623264SMatthew G. Knepley 
34777623264SMatthew G. Knepley   Sample usage:
34877623264SMatthew G. Knepley .vb
34977623264SMatthew G. Knepley     PetscPartitionerRegister("my_part", MyPetscPartitionerCreate);
35077623264SMatthew G. Knepley .ve
35177623264SMatthew G. Knepley 
35277623264SMatthew G. Knepley   Then, your PetscPartitioner type can be chosen with the procedural interface via
35377623264SMatthew G. Knepley .vb
35477623264SMatthew G. Knepley     PetscPartitionerCreate(MPI_Comm, PetscPartitioner *);
35577623264SMatthew G. Knepley     PetscPartitionerSetType(PetscPartitioner, "my_part");
35677623264SMatthew G. Knepley .ve
35777623264SMatthew G. Knepley    or at runtime via the option
35877623264SMatthew G. Knepley .vb
35977623264SMatthew G. Knepley     -petscpartitioner_type my_part
36077623264SMatthew G. Knepley .ve
36177623264SMatthew G. Knepley 
36277623264SMatthew G. Knepley   Level: advanced
36377623264SMatthew G. Knepley 
36477623264SMatthew G. Knepley .keywords: PetscPartitioner, register
36577623264SMatthew G. Knepley .seealso: PetscPartitionerRegisterAll(), PetscPartitionerRegisterDestroy()
36677623264SMatthew G. Knepley 
36777623264SMatthew G. Knepley @*/
36877623264SMatthew G. Knepley PetscErrorCode PetscPartitionerRegister(const char sname[], PetscErrorCode (*function)(PetscPartitioner))
36977623264SMatthew G. Knepley {
37077623264SMatthew G. Knepley   PetscErrorCode ierr;
37177623264SMatthew G. Knepley 
37277623264SMatthew G. Knepley   PetscFunctionBegin;
37377623264SMatthew G. Knepley   ierr = PetscFunctionListAdd(&PetscPartitionerList, sname, function);CHKERRQ(ierr);
37477623264SMatthew G. Knepley   PetscFunctionReturn(0);
37577623264SMatthew G. Knepley }
37677623264SMatthew G. Knepley 
37777623264SMatthew G. Knepley /*@C
37877623264SMatthew G. Knepley   PetscPartitionerSetType - Builds a particular PetscPartitioner
37977623264SMatthew G. Knepley 
38077623264SMatthew G. Knepley   Collective on PetscPartitioner
38177623264SMatthew G. Knepley 
38277623264SMatthew G. Knepley   Input Parameters:
38377623264SMatthew G. Knepley + part - The PetscPartitioner object
38477623264SMatthew G. Knepley - name - The kind of partitioner
38577623264SMatthew G. Knepley 
38677623264SMatthew G. Knepley   Options Database Key:
38777623264SMatthew G. Knepley . -petscpartitioner_type <type> - Sets the PetscPartitioner type; use -help for a list of available types
38877623264SMatthew G. Knepley 
38977623264SMatthew G. Knepley   Level: intermediate
39077623264SMatthew G. Knepley 
39177623264SMatthew G. Knepley .keywords: PetscPartitioner, set, type
39277623264SMatthew G. Knepley .seealso: PetscPartitionerGetType(), PetscPartitionerCreate()
39377623264SMatthew G. Knepley @*/
39477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetType(PetscPartitioner part, PetscPartitionerType name)
39577623264SMatthew G. Knepley {
39677623264SMatthew G. Knepley   PetscErrorCode (*r)(PetscPartitioner);
39777623264SMatthew G. Knepley   PetscBool      match;
39877623264SMatthew G. Knepley   PetscErrorCode ierr;
39977623264SMatthew G. Knepley 
40077623264SMatthew G. Knepley   PetscFunctionBegin;
40177623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
40277623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) part, name, &match);CHKERRQ(ierr);
40377623264SMatthew G. Knepley   if (match) PetscFunctionReturn(0);
40477623264SMatthew G. Knepley 
4050f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
40677623264SMatthew G. Knepley   ierr = PetscFunctionListFind(PetscPartitionerList, name, &r);CHKERRQ(ierr);
40777623264SMatthew G. Knepley   if (!r) SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscPartitioner type: %s", name);
40877623264SMatthew G. Knepley 
40977623264SMatthew G. Knepley   if (part->ops->destroy) {
41077623264SMatthew G. Knepley     ierr              = (*part->ops->destroy)(part);CHKERRQ(ierr);
41177623264SMatthew G. Knepley   }
41209161815SVaclav Hapla   ierr = PetscMemzero(part->ops, sizeof(struct _PetscPartitionerOps));CHKERRQ(ierr);
41377623264SMatthew G. Knepley   ierr = (*r)(part);CHKERRQ(ierr);
41477623264SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) part, name);CHKERRQ(ierr);
41577623264SMatthew G. Knepley   PetscFunctionReturn(0);
41677623264SMatthew G. Knepley }
41777623264SMatthew G. Knepley 
41877623264SMatthew G. Knepley /*@C
41977623264SMatthew G. Knepley   PetscPartitionerGetType - Gets the PetscPartitioner type name (as a string) from the object.
42077623264SMatthew G. Knepley 
42177623264SMatthew G. Knepley   Not Collective
42277623264SMatthew G. Knepley 
42377623264SMatthew G. Knepley   Input Parameter:
42477623264SMatthew G. Knepley . part - The PetscPartitioner
42577623264SMatthew G. Knepley 
42677623264SMatthew G. Knepley   Output Parameter:
42777623264SMatthew G. Knepley . name - The PetscPartitioner type name
42877623264SMatthew G. Knepley 
42977623264SMatthew G. Knepley   Level: intermediate
43077623264SMatthew G. Knepley 
43177623264SMatthew G. Knepley .keywords: PetscPartitioner, get, type, name
43277623264SMatthew G. Knepley .seealso: PetscPartitionerSetType(), PetscPartitionerCreate()
43377623264SMatthew G. Knepley @*/
43477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerGetType(PetscPartitioner part, PetscPartitionerType *name)
43577623264SMatthew G. Knepley {
43677623264SMatthew G. Knepley   PetscErrorCode ierr;
43777623264SMatthew G. Knepley 
43877623264SMatthew G. Knepley   PetscFunctionBegin;
43977623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
44077623264SMatthew G. Knepley   PetscValidPointer(name, 2);
4410f51fdf8SToby Isaac   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
44277623264SMatthew G. Knepley   *name = ((PetscObject) part)->type_name;
44377623264SMatthew G. Knepley   PetscFunctionReturn(0);
44477623264SMatthew G. Knepley }
44577623264SMatthew G. Knepley 
44677623264SMatthew G. Knepley /*@C
44777623264SMatthew G. Knepley   PetscPartitionerView - Views a PetscPartitioner
44877623264SMatthew G. Knepley 
44977623264SMatthew G. Knepley   Collective on PetscPartitioner
45077623264SMatthew G. Knepley 
45177623264SMatthew G. Knepley   Input Parameter:
45277623264SMatthew G. Knepley + part - the PetscPartitioner object to view
45377623264SMatthew G. Knepley - v    - the viewer
45477623264SMatthew G. Knepley 
45577623264SMatthew G. Knepley   Level: developer
45677623264SMatthew G. Knepley 
45777623264SMatthew G. Knepley .seealso: PetscPartitionerDestroy()
45877623264SMatthew G. Knepley @*/
45977623264SMatthew G. Knepley PetscErrorCode PetscPartitionerView(PetscPartitioner part, PetscViewer v)
46077623264SMatthew G. Knepley {
46177623264SMatthew G. Knepley   PetscErrorCode ierr;
46277623264SMatthew G. Knepley 
46377623264SMatthew G. Knepley   PetscFunctionBegin;
46477623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
46577623264SMatthew G. Knepley   if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) part), &v);CHKERRQ(ierr);}
46677623264SMatthew G. Knepley   if (part->ops->view) {ierr = (*part->ops->view)(part, v);CHKERRQ(ierr);}
46777623264SMatthew G. Knepley   PetscFunctionReturn(0);
46877623264SMatthew G. Knepley }
46977623264SMatthew G. Knepley 
470a0058e54SToby Isaac static PetscErrorCode PetscPartitionerGetDefaultType(const char *currentType, const char **defaultType)
471a0058e54SToby Isaac {
472a0058e54SToby Isaac   PetscFunctionBegin;
473a0058e54SToby Isaac   if (!currentType) {
474a0058e54SToby Isaac #if defined(PETSC_HAVE_CHACO)
475a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERCHACO;
476a0058e54SToby Isaac #elif defined(PETSC_HAVE_PARMETIS)
477a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERPARMETIS;
478137cd93aSLisandro Dalcin #elif defined(PETSC_HAVE_PTSCOTCH)
479137cd93aSLisandro Dalcin     *defaultType = PETSCPARTITIONERPTSCOTCH;
480a0058e54SToby Isaac #else
481a0058e54SToby Isaac     *defaultType = PETSCPARTITIONERSIMPLE;
482a0058e54SToby Isaac #endif
483a0058e54SToby Isaac   } else {
484a0058e54SToby Isaac     *defaultType = currentType;
485a0058e54SToby Isaac   }
486a0058e54SToby Isaac   PetscFunctionReturn(0);
487a0058e54SToby Isaac }
488a0058e54SToby Isaac 
48977623264SMatthew G. Knepley /*@
49077623264SMatthew G. Knepley   PetscPartitionerSetFromOptions - sets parameters in a PetscPartitioner from the options database
49177623264SMatthew G. Knepley 
49277623264SMatthew G. Knepley   Collective on PetscPartitioner
49377623264SMatthew G. Knepley 
49477623264SMatthew G. Knepley   Input Parameter:
49577623264SMatthew G. Knepley . part - the PetscPartitioner object to set options for
49677623264SMatthew G. Knepley 
49777623264SMatthew G. Knepley   Level: developer
49877623264SMatthew G. Knepley 
49977623264SMatthew G. Knepley .seealso: PetscPartitionerView()
50077623264SMatthew G. Knepley @*/
50177623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetFromOptions(PetscPartitioner part)
50277623264SMatthew G. Knepley {
5036bb9daa8SLisandro Dalcin   const char    *defaultType;
5046bb9daa8SLisandro Dalcin   char           name[256];
5056bb9daa8SLisandro Dalcin   PetscBool      flg;
50677623264SMatthew G. Knepley   PetscErrorCode ierr;
50777623264SMatthew G. Knepley 
50877623264SMatthew G. Knepley   PetscFunctionBegin;
50977623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
5106bb9daa8SLisandro Dalcin   ierr = PetscPartitionerRegisterAll();CHKERRQ(ierr);
5116bb9daa8SLisandro Dalcin   ierr = PetscPartitionerGetDefaultType(((PetscObject) part)->type_name,&defaultType);CHKERRQ(ierr);
51277623264SMatthew G. Knepley   ierr = PetscObjectOptionsBegin((PetscObject) part);CHKERRQ(ierr);
5136bb9daa8SLisandro Dalcin   ierr = PetscOptionsFList("-petscpartitioner_type", "Graph partitioner", "PetscPartitionerSetType", PetscPartitionerList, defaultType, name, sizeof(name), &flg);CHKERRQ(ierr);
5146bb9daa8SLisandro Dalcin   if (flg) {
5156bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, name);CHKERRQ(ierr);
5166bb9daa8SLisandro Dalcin   } else if (!((PetscObject) part)->type_name) {
5176bb9daa8SLisandro Dalcin     ierr = PetscPartitionerSetType(part, defaultType);CHKERRQ(ierr);
5186bb9daa8SLisandro Dalcin   }
5196bb9daa8SLisandro Dalcin   if (part->ops->setfromoptions) {
5206bb9daa8SLisandro Dalcin     ierr = (*part->ops->setfromoptions)(PetscOptionsObject,part);CHKERRQ(ierr);
5216bb9daa8SLisandro Dalcin   }
52277623264SMatthew G. Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5230633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) part);CHKERRQ(ierr);
52477623264SMatthew G. Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
52577623264SMatthew G. Knepley   ierr = PetscPartitionerViewFromOptions(part, NULL, "-petscpartitioner_view");CHKERRQ(ierr);
52677623264SMatthew G. Knepley   PetscFunctionReturn(0);
52777623264SMatthew G. Knepley }
52877623264SMatthew G. Knepley 
52977623264SMatthew G. Knepley /*@C
53077623264SMatthew G. Knepley   PetscPartitionerSetUp - Construct data structures for the PetscPartitioner
53177623264SMatthew G. Knepley 
53277623264SMatthew G. Knepley   Collective on PetscPartitioner
53377623264SMatthew G. Knepley 
53477623264SMatthew G. Knepley   Input Parameter:
53577623264SMatthew G. Knepley . part - the PetscPartitioner object to setup
53677623264SMatthew G. Knepley 
53777623264SMatthew G. Knepley   Level: developer
53877623264SMatthew G. Knepley 
53977623264SMatthew G. Knepley .seealso: PetscPartitionerView(), PetscPartitionerDestroy()
54077623264SMatthew G. Knepley @*/
54177623264SMatthew G. Knepley PetscErrorCode PetscPartitionerSetUp(PetscPartitioner part)
54277623264SMatthew G. Knepley {
54377623264SMatthew G. Knepley   PetscErrorCode ierr;
54477623264SMatthew G. Knepley 
54577623264SMatthew G. Knepley   PetscFunctionBegin;
54677623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
54777623264SMatthew G. Knepley   if (part->ops->setup) {ierr = (*part->ops->setup)(part);CHKERRQ(ierr);}
54877623264SMatthew G. Knepley   PetscFunctionReturn(0);
54977623264SMatthew G. Knepley }
55077623264SMatthew G. Knepley 
55177623264SMatthew G. Knepley /*@
55277623264SMatthew G. Knepley   PetscPartitionerDestroy - Destroys a PetscPartitioner object
55377623264SMatthew G. Knepley 
55477623264SMatthew G. Knepley   Collective on PetscPartitioner
55577623264SMatthew G. Knepley 
55677623264SMatthew G. Knepley   Input Parameter:
55777623264SMatthew G. Knepley . part - the PetscPartitioner object to destroy
55877623264SMatthew G. Knepley 
55977623264SMatthew G. Knepley   Level: developer
56077623264SMatthew G. Knepley 
56177623264SMatthew G. Knepley .seealso: PetscPartitionerView()
56277623264SMatthew G. Knepley @*/
56377623264SMatthew G. Knepley PetscErrorCode PetscPartitionerDestroy(PetscPartitioner *part)
56477623264SMatthew G. Knepley {
56577623264SMatthew G. Knepley   PetscErrorCode ierr;
56677623264SMatthew G. Knepley 
56777623264SMatthew G. Knepley   PetscFunctionBegin;
56877623264SMatthew G. Knepley   if (!*part) PetscFunctionReturn(0);
56977623264SMatthew G. Knepley   PetscValidHeaderSpecific((*part), PETSCPARTITIONER_CLASSID, 1);
57077623264SMatthew G. Knepley 
57177623264SMatthew G. Knepley   if (--((PetscObject)(*part))->refct > 0) {*part = 0; PetscFunctionReturn(0);}
57277623264SMatthew G. Knepley   ((PetscObject) (*part))->refct = 0;
57377623264SMatthew G. Knepley 
57477623264SMatthew G. Knepley   if ((*part)->ops->destroy) {ierr = (*(*part)->ops->destroy)(*part);CHKERRQ(ierr);}
57577623264SMatthew G. Knepley   ierr = PetscHeaderDestroy(part);CHKERRQ(ierr);
57677623264SMatthew G. Knepley   PetscFunctionReturn(0);
57777623264SMatthew G. Knepley }
57877623264SMatthew G. Knepley 
57977623264SMatthew G. Knepley /*@
58077623264SMatthew G. Knepley   PetscPartitionerCreate - Creates an empty PetscPartitioner object. The type can then be set with PetscPartitionerSetType().
58177623264SMatthew G. Knepley 
58277623264SMatthew G. Knepley   Collective on MPI_Comm
58377623264SMatthew G. Knepley 
58477623264SMatthew G. Knepley   Input Parameter:
58577623264SMatthew G. Knepley . comm - The communicator for the PetscPartitioner object
58677623264SMatthew G. Knepley 
58777623264SMatthew G. Knepley   Output Parameter:
58877623264SMatthew G. Knepley . part - The PetscPartitioner object
58977623264SMatthew G. Knepley 
59077623264SMatthew G. Knepley   Level: beginner
59177623264SMatthew G. Knepley 
592dae52e14SToby Isaac .seealso: PetscPartitionerSetType(), PETSCPARTITIONERCHACO, PETSCPARTITIONERPARMETIS, PETSCPARTITIONERSHELL, PETSCPARTITIONERSIMPLE, PETSCPARTITIONERGATHER
59377623264SMatthew G. Knepley @*/
59477623264SMatthew G. Knepley PetscErrorCode PetscPartitionerCreate(MPI_Comm comm, PetscPartitioner *part)
59577623264SMatthew G. Knepley {
59677623264SMatthew G. Knepley   PetscPartitioner p;
597a0058e54SToby Isaac   const char       *partitionerType = NULL;
59877623264SMatthew G. Knepley   PetscErrorCode   ierr;
59977623264SMatthew G. Knepley 
60077623264SMatthew G. Knepley   PetscFunctionBegin;
60177623264SMatthew G. Knepley   PetscValidPointer(part, 2);
60277623264SMatthew G. Knepley   *part = NULL;
60383cde681SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
60477623264SMatthew G. Knepley 
60573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(p, PETSCPARTITIONER_CLASSID, "PetscPartitioner", "Graph Partitioner", "PetscPartitioner", comm, PetscPartitionerDestroy, PetscPartitionerView);CHKERRQ(ierr);
606a0058e54SToby Isaac   ierr = PetscPartitionerGetDefaultType(NULL,&partitionerType);CHKERRQ(ierr);
607a0058e54SToby Isaac   ierr = PetscPartitionerSetType(p,partitionerType);CHKERRQ(ierr);
60877623264SMatthew G. Knepley 
60977623264SMatthew G. Knepley   *part = p;
61077623264SMatthew G. Knepley   PetscFunctionReturn(0);
61177623264SMatthew G. Knepley }
61277623264SMatthew G. Knepley 
61377623264SMatthew G. Knepley /*@
61477623264SMatthew G. Knepley   PetscPartitionerPartition - Create a non-overlapping partition of the cells in the mesh
61577623264SMatthew G. Knepley 
61677623264SMatthew G. Knepley   Collective on DM
61777623264SMatthew G. Knepley 
61877623264SMatthew G. Knepley   Input Parameters:
61977623264SMatthew G. Knepley + part    - The PetscPartitioner
620f8987ae8SMichael Lange - dm      - The mesh DM
62177623264SMatthew G. Knepley 
62277623264SMatthew G. Knepley   Output Parameters:
62377623264SMatthew G. Knepley + partSection     - The PetscSection giving the division of points by partition
624f8987ae8SMichael Lange - partition       - The list of points by partition
62577623264SMatthew G. Knepley 
62677623264SMatthew G. Knepley   Note: Instead of cells, points at a given height can be partitioned by calling PetscPartitionerSetPointHeight()
62777623264SMatthew G. Knepley 
62877623264SMatthew G. Knepley   Level: developer
62977623264SMatthew G. Knepley 
63077623264SMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerSetPointHeight(), PetscPartitionerCreate()
6314b15ede2SMatthew G. Knepley @*/
632f8987ae8SMichael Lange PetscErrorCode PetscPartitionerPartition(PetscPartitioner part, DM dm, PetscSection partSection, IS *partition)
63377623264SMatthew G. Knepley {
63477623264SMatthew G. Knepley   PetscMPIInt    size;
63577623264SMatthew G. Knepley   PetscErrorCode ierr;
63677623264SMatthew G. Knepley 
63777623264SMatthew G. Knepley   PetscFunctionBegin;
63877623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
63977623264SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
64077623264SMatthew G. Knepley   PetscValidHeaderSpecific(partSection, PETSC_SECTION_CLASSID, 4);
64177623264SMatthew G. Knepley   PetscValidPointer(partition, 5);
64277623264SMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) part), &size);CHKERRQ(ierr);
64377623264SMatthew G. Knepley   if (size == 1) {
64477623264SMatthew G. Knepley     PetscInt *points;
64577623264SMatthew G. Knepley     PetscInt  cStart, cEnd, c;
64677623264SMatthew G. Knepley 
64777623264SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
64877623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, size);CHKERRQ(ierr);
64977623264SMatthew G. Knepley     ierr = PetscSectionSetDof(partSection, 0, cEnd-cStart);CHKERRQ(ierr);
65077623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
65177623264SMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cStart, &points);CHKERRQ(ierr);
65277623264SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) points[c] = c;
65377623264SMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
65477623264SMatthew G. Knepley     PetscFunctionReturn(0);
65577623264SMatthew G. Knepley   }
65677623264SMatthew G. Knepley   if (part->height == 0) {
65777623264SMatthew G. Knepley     PetscInt numVertices;
65877623264SMatthew G. Knepley     PetscInt *start     = NULL;
65977623264SMatthew G. Knepley     PetscInt *adjacency = NULL;
6603fa7752dSToby Isaac     IS       globalNumbering;
66177623264SMatthew G. Knepley 
6623fa7752dSToby Isaac     ierr = DMPlexCreatePartitionerGraph(dm, 0, &numVertices, &start, &adjacency, &globalNumbering);CHKERRQ(ierr);
66377623264SMatthew G. Knepley     if (!part->ops->partition) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONGSTATE, "PetscPartitioner has no type");
66477623264SMatthew G. Knepley     ierr = (*part->ops->partition)(part, dm, size, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
66577623264SMatthew G. Knepley     ierr = PetscFree(start);CHKERRQ(ierr);
66677623264SMatthew G. Knepley     ierr = PetscFree(adjacency);CHKERRQ(ierr);
6673fa7752dSToby Isaac     if (globalNumbering) { /* partition is wrt global unique numbering: change this to be wrt local numbering */
6683fa7752dSToby Isaac       const PetscInt *globalNum;
6693fa7752dSToby Isaac       const PetscInt *partIdx;
6703fa7752dSToby Isaac       PetscInt *map, cStart, cEnd;
6713fa7752dSToby Isaac       PetscInt *adjusted, i, localSize, offset;
6723fa7752dSToby Isaac       IS    newPartition;
6733fa7752dSToby Isaac 
6743fa7752dSToby Isaac       ierr = ISGetLocalSize(*partition,&localSize);CHKERRQ(ierr);
6753fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&adjusted);CHKERRQ(ierr);
6763fa7752dSToby Isaac       ierr = ISGetIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
6773fa7752dSToby Isaac       ierr = ISGetIndices(*partition,&partIdx);CHKERRQ(ierr);
6783fa7752dSToby Isaac       ierr = PetscMalloc1(localSize,&map);CHKERRQ(ierr);
6793fa7752dSToby Isaac       ierr = DMPlexGetHeightStratum(dm, part->height, &cStart, &cEnd);CHKERRQ(ierr);
6803fa7752dSToby Isaac       for (i = cStart, offset = 0; i < cEnd; i++) {
6813fa7752dSToby Isaac         if (globalNum[i - cStart] >= 0) map[offset++] = i;
6823fa7752dSToby Isaac       }
6833fa7752dSToby Isaac       for (i = 0; i < localSize; i++) {
6843fa7752dSToby Isaac         adjusted[i] = map[partIdx[i]];
6853fa7752dSToby Isaac       }
6863fa7752dSToby Isaac       ierr = PetscFree(map);CHKERRQ(ierr);
6873fa7752dSToby Isaac       ierr = ISRestoreIndices(*partition,&partIdx);CHKERRQ(ierr);
6883fa7752dSToby Isaac       ierr = ISRestoreIndices(globalNumbering,&globalNum);CHKERRQ(ierr);
6893fa7752dSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,localSize,adjusted,PETSC_OWN_POINTER,&newPartition);CHKERRQ(ierr);
6903fa7752dSToby Isaac       ierr = ISDestroy(&globalNumbering);CHKERRQ(ierr);
6913fa7752dSToby Isaac       ierr = ISDestroy(partition);CHKERRQ(ierr);
6923fa7752dSToby Isaac       *partition = newPartition;
6933fa7752dSToby Isaac     }
69477623264SMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_OUTOFRANGE, "Invalid height %D for points to partition", part->height);
69577623264SMatthew G. Knepley   PetscFunctionReturn(0);
6963fa7752dSToby Isaac 
69777623264SMatthew G. Knepley }
69877623264SMatthew G. Knepley 
699d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part)
70077623264SMatthew G. Knepley {
70177623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
70277623264SMatthew G. Knepley   PetscErrorCode          ierr;
70377623264SMatthew G. Knepley 
70477623264SMatthew G. Knepley   PetscFunctionBegin;
70577623264SMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
70677623264SMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
70777623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
70877623264SMatthew G. Knepley   PetscFunctionReturn(0);
70977623264SMatthew G. Knepley }
71077623264SMatthew G. Knepley 
711d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell_Ascii(PetscPartitioner part, PetscViewer viewer)
71277623264SMatthew G. Knepley {
713077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
71477623264SMatthew G. Knepley   PetscViewerFormat       format;
71577623264SMatthew G. Knepley   PetscErrorCode          ierr;
71677623264SMatthew G. Knepley 
71777623264SMatthew G. Knepley   PetscFunctionBegin;
71877623264SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
71977623264SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Shell Graph Partitioner:\n");CHKERRQ(ierr);
720077101c0SMatthew G. Knepley   if (p->random) {
721077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
722077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "using random partition\n");CHKERRQ(ierr);
723077101c0SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
724077101c0SMatthew G. Knepley   }
72577623264SMatthew G. Knepley   PetscFunctionReturn(0);
72677623264SMatthew G. Knepley }
72777623264SMatthew G. Knepley 
728d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer)
72977623264SMatthew G. Knepley {
73077623264SMatthew G. Knepley   PetscBool      iascii;
73177623264SMatthew G. Knepley   PetscErrorCode ierr;
73277623264SMatthew G. Knepley 
73377623264SMatthew G. Knepley   PetscFunctionBegin;
73477623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
73577623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
73677623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
73777623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Shell_Ascii(part, viewer);CHKERRQ(ierr);}
73877623264SMatthew G. Knepley   PetscFunctionReturn(0);
73977623264SMatthew G. Knepley }
74077623264SMatthew G. Knepley 
741d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
742077101c0SMatthew G. Knepley {
743077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
744077101c0SMatthew G. Knepley   PetscErrorCode          ierr;
745077101c0SMatthew G. Knepley 
746077101c0SMatthew G. Knepley   PetscFunctionBegin;
747077101c0SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");CHKERRQ(ierr);
748077101c0SMatthew G. Knepley   ierr = PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);CHKERRQ(ierr);
749077101c0SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
750077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
751077101c0SMatthew G. Knepley }
752077101c0SMatthew G. Knepley 
753d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
75477623264SMatthew G. Knepley {
75577623264SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
75677623264SMatthew G. Knepley   PetscInt                np;
75777623264SMatthew G. Knepley   PetscErrorCode          ierr;
75877623264SMatthew G. Knepley 
75977623264SMatthew G. Knepley   PetscFunctionBegin;
760077101c0SMatthew G. Knepley   if (p->random) {
761077101c0SMatthew G. Knepley     PetscRandom r;
762aa1d5631SMatthew G. Knepley     PetscInt   *sizes, *points, v, p;
763aa1d5631SMatthew G. Knepley     PetscMPIInt rank;
764077101c0SMatthew G. Knepley 
765aa1d5631SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
766077101c0SMatthew G. Knepley     ierr = PetscRandomCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
767c717d290SMatthew G. Knepley     ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);CHKERRQ(ierr);
768077101c0SMatthew G. Knepley     ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr);
769077101c0SMatthew G. Knepley     ierr = PetscCalloc2(nparts, &sizes, numVertices, &points);CHKERRQ(ierr);
770aa1d5631SMatthew G. Knepley     for (v = 0; v < numVertices; ++v) {points[v] = v;}
771ac9a96f1SMichael Lange     for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);}
772aa1d5631SMatthew G. Knepley     for (v = numVertices-1; v > 0; --v) {
773077101c0SMatthew G. Knepley       PetscReal val;
774aa1d5631SMatthew G. Knepley       PetscInt  w, tmp;
775077101c0SMatthew G. Knepley 
776aa1d5631SMatthew G. Knepley       ierr = PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));CHKERRQ(ierr);
777077101c0SMatthew G. Knepley       ierr = PetscRandomGetValueReal(r, &val);CHKERRQ(ierr);
778aa1d5631SMatthew G. Knepley       w    = PetscFloorReal(val);
779aa1d5631SMatthew G. Knepley       tmp       = points[v];
780aa1d5631SMatthew G. Knepley       points[v] = points[w];
781aa1d5631SMatthew G. Knepley       points[w] = tmp;
782077101c0SMatthew G. Knepley     }
783077101c0SMatthew G. Knepley     ierr = PetscRandomDestroy(&r);CHKERRQ(ierr);
784077101c0SMatthew G. Knepley     ierr = PetscPartitionerShellSetPartition(part, nparts, sizes, points);CHKERRQ(ierr);
785077101c0SMatthew G. Knepley     ierr = PetscFree2(sizes, points);CHKERRQ(ierr);
786077101c0SMatthew G. Knepley   }
787c717d290SMatthew G. Knepley   if (!p->section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()");
78877623264SMatthew G. Knepley   ierr = PetscSectionGetChart(p->section, NULL, &np);CHKERRQ(ierr);
78977623264SMatthew G. Knepley   if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np);
79077623264SMatthew G. Knepley   ierr = ISGetLocalSize(p->partition, &np);CHKERRQ(ierr);
79177623264SMatthew G. Knepley   if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np);
7925680f57bSMatthew G. Knepley   ierr = PetscSectionCopy(p->section, partSection);CHKERRQ(ierr);
79377623264SMatthew G. Knepley   *partition = p->partition;
79477623264SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) p->partition);CHKERRQ(ierr);
79577623264SMatthew G. Knepley   PetscFunctionReturn(0);
79677623264SMatthew G. Knepley }
79777623264SMatthew G. Knepley 
798d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part)
79977623264SMatthew G. Knepley {
80077623264SMatthew G. Knepley   PetscFunctionBegin;
80177623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_Shell;
802077101c0SMatthew G. Knepley   part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell;
80377623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_Shell;
80477623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_Shell;
80577623264SMatthew G. Knepley   PetscFunctionReturn(0);
80677623264SMatthew G. Knepley }
80777623264SMatthew G. Knepley 
80877623264SMatthew G. Knepley /*MC
80977623264SMatthew G. Knepley   PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object
81077623264SMatthew G. Knepley 
81177623264SMatthew G. Knepley   Level: intermediate
81277623264SMatthew G. Knepley 
81377623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
81477623264SMatthew G. Knepley M*/
81577623264SMatthew G. Knepley 
81677623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part)
81777623264SMatthew G. Knepley {
81877623264SMatthew G. Knepley   PetscPartitioner_Shell *p;
81977623264SMatthew G. Knepley   PetscErrorCode          ierr;
82077623264SMatthew G. Knepley 
82177623264SMatthew G. Knepley   PetscFunctionBegin;
82277623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
82377623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
82477623264SMatthew G. Knepley   part->data = p;
82577623264SMatthew G. Knepley 
82677623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Shell(part);CHKERRQ(ierr);
827077101c0SMatthew G. Knepley   p->random = PETSC_FALSE;
82877623264SMatthew G. Knepley   PetscFunctionReturn(0);
82977623264SMatthew G. Knepley }
83077623264SMatthew G. Knepley 
8315680f57bSMatthew G. Knepley /*@C
8325680f57bSMatthew G. Knepley   PetscPartitionerShellSetPartition - Set an artifical partition for a mesh
8335680f57bSMatthew G. Knepley 
834077101c0SMatthew G. Knepley   Collective on Part
8355680f57bSMatthew G. Knepley 
8365680f57bSMatthew G. Knepley   Input Parameters:
8375680f57bSMatthew G. Knepley + part     - The PetscPartitioner
8389852e123SBarry Smith . size - The number of partitions
8399852e123SBarry Smith . sizes    - array of size size (or NULL) providing the number of points in each partition
8409758bf69SToby 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.)
8415680f57bSMatthew G. Knepley 
8425680f57bSMatthew G. Knepley   Level: developer
8435680f57bSMatthew G. Knepley 
844b7e49471SLawrence Mitchell   Notes:
845b7e49471SLawrence Mitchell 
846b7e49471SLawrence Mitchell     It is safe to free the sizes and points arrays after use in this routine.
847b7e49471SLawrence Mitchell 
8485680f57bSMatthew G. Knepley .seealso DMPlexDistribute(), PetscPartitionerCreate()
8495680f57bSMatthew G. Knepley @*/
8509852e123SBarry Smith PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[])
8515680f57bSMatthew G. Knepley {
8525680f57bSMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
8535680f57bSMatthew G. Knepley   PetscInt                proc, numPoints;
8545680f57bSMatthew G. Knepley   PetscErrorCode          ierr;
8555680f57bSMatthew G. Knepley 
8565680f57bSMatthew G. Knepley   PetscFunctionBegin;
8575680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
8585680f57bSMatthew G. Knepley   if (sizes)  {PetscValidPointer(sizes, 3);}
859c717d290SMatthew G. Knepley   if (points) {PetscValidPointer(points, 4);}
8605680f57bSMatthew G. Knepley   ierr = PetscSectionDestroy(&p->section);CHKERRQ(ierr);
8615680f57bSMatthew G. Knepley   ierr = ISDestroy(&p->partition);CHKERRQ(ierr);
8625680f57bSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);CHKERRQ(ierr);
8639852e123SBarry Smith   ierr = PetscSectionSetChart(p->section, 0, size);CHKERRQ(ierr);
8645680f57bSMatthew G. Knepley   if (sizes) {
8659852e123SBarry Smith     for (proc = 0; proc < size; ++proc) {
8665680f57bSMatthew G. Knepley       ierr = PetscSectionSetDof(p->section, proc, sizes[proc]);CHKERRQ(ierr);
8675680f57bSMatthew G. Knepley     }
8685680f57bSMatthew G. Knepley   }
8695680f57bSMatthew G. Knepley   ierr = PetscSectionSetUp(p->section);CHKERRQ(ierr);
8705680f57bSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(p->section, &numPoints);CHKERRQ(ierr);
8715680f57bSMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);CHKERRQ(ierr);
8725680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
8735680f57bSMatthew G. Knepley }
8745680f57bSMatthew G. Knepley 
875077101c0SMatthew G. Knepley /*@
876077101c0SMatthew G. Knepley   PetscPartitionerShellSetRandom - Set the flag to use a random partition
877077101c0SMatthew G. Knepley 
878077101c0SMatthew G. Knepley   Collective on Part
879077101c0SMatthew G. Knepley 
880077101c0SMatthew G. Knepley   Input Parameters:
881077101c0SMatthew G. Knepley + part   - The PetscPartitioner
882077101c0SMatthew G. Knepley - random - The flag to use a random partition
883077101c0SMatthew G. Knepley 
884077101c0SMatthew G. Knepley   Level: intermediate
885077101c0SMatthew G. Knepley 
886077101c0SMatthew G. Knepley .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate()
887077101c0SMatthew G. Knepley @*/
888077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random)
889077101c0SMatthew G. Knepley {
890077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
891077101c0SMatthew G. Knepley 
892077101c0SMatthew G. Knepley   PetscFunctionBegin;
893077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
894077101c0SMatthew G. Knepley   p->random = random;
895077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
896077101c0SMatthew G. Knepley }
897077101c0SMatthew G. Knepley 
898077101c0SMatthew G. Knepley /*@
899077101c0SMatthew G. Knepley   PetscPartitionerShellGetRandom - get the flag to use a random partition
900077101c0SMatthew G. Knepley 
901077101c0SMatthew G. Knepley   Collective on Part
902077101c0SMatthew G. Knepley 
903077101c0SMatthew G. Knepley   Input Parameter:
904077101c0SMatthew G. Knepley . part   - The PetscPartitioner
905077101c0SMatthew G. Knepley 
906077101c0SMatthew G. Knepley   Output Parameter
907077101c0SMatthew G. Knepley . random - The flag to use a random partition
908077101c0SMatthew G. Knepley 
909077101c0SMatthew G. Knepley   Level: intermediate
910077101c0SMatthew G. Knepley 
911077101c0SMatthew G. Knepley .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate()
912077101c0SMatthew G. Knepley @*/
913077101c0SMatthew G. Knepley PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random)
914077101c0SMatthew G. Knepley {
915077101c0SMatthew G. Knepley   PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
916077101c0SMatthew G. Knepley 
917077101c0SMatthew G. Knepley   PetscFunctionBegin;
918077101c0SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
919077101c0SMatthew G. Knepley   PetscValidPointer(random, 2);
920077101c0SMatthew G. Knepley   *random = p->random;
921077101c0SMatthew G. Knepley   PetscFunctionReturn(0);
922077101c0SMatthew G. Knepley }
923077101c0SMatthew G. Knepley 
924d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part)
925555a9cf8SMatthew G. Knepley {
926555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data;
927555a9cf8SMatthew G. Knepley   PetscErrorCode          ierr;
928555a9cf8SMatthew G. Knepley 
929555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
930555a9cf8SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
931555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
932555a9cf8SMatthew G. Knepley }
933555a9cf8SMatthew G. Knepley 
934d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple_Ascii(PetscPartitioner part, PetscViewer viewer)
935555a9cf8SMatthew G. Knepley {
936555a9cf8SMatthew G. Knepley   PetscViewerFormat format;
937555a9cf8SMatthew G. Knepley   PetscErrorCode    ierr;
938555a9cf8SMatthew G. Knepley 
939555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
940555a9cf8SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
941555a9cf8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Simple Graph Partitioner:\n");CHKERRQ(ierr);
942555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
943555a9cf8SMatthew G. Knepley }
944555a9cf8SMatthew G. Knepley 
945d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer)
946555a9cf8SMatthew G. Knepley {
947555a9cf8SMatthew G. Knepley   PetscBool      iascii;
948555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
949555a9cf8SMatthew G. Knepley 
950555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
951555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
952555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
953555a9cf8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
954555a9cf8SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Simple_Ascii(part, viewer);CHKERRQ(ierr);}
955555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
956555a9cf8SMatthew G. Knepley }
957555a9cf8SMatthew G. Knepley 
958d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
959555a9cf8SMatthew G. Knepley {
960cead94edSToby Isaac   MPI_Comm       comm;
961555a9cf8SMatthew G. Knepley   PetscInt       np;
962cead94edSToby Isaac   PetscMPIInt    size;
963555a9cf8SMatthew G. Knepley   PetscErrorCode ierr;
964555a9cf8SMatthew G. Knepley 
965555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
966cead94edSToby Isaac   comm = PetscObjectComm((PetscObject)dm);
967cead94edSToby Isaac   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
968555a9cf8SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
969555a9cf8SMatthew G. Knepley   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
970cead94edSToby Isaac   if (size == 1) {
971cead94edSToby Isaac     for (np = 0; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np));CHKERRQ(ierr);}
972cead94edSToby Isaac   }
973cead94edSToby Isaac   else {
974cead94edSToby Isaac     PetscMPIInt rank;
975cead94edSToby Isaac     PetscInt nvGlobal, *offsets, myFirst, myLast;
976cead94edSToby Isaac 
977a679a563SToby Isaac     ierr = PetscMalloc1(size+1,&offsets);CHKERRQ(ierr);
978cead94edSToby Isaac     offsets[0] = 0;
979cead94edSToby Isaac     ierr = MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm);CHKERRQ(ierr);
980cead94edSToby Isaac     for (np = 2; np <= size; np++) {
981cead94edSToby Isaac       offsets[np] += offsets[np-1];
982cead94edSToby Isaac     }
983cead94edSToby Isaac     nvGlobal = offsets[size];
984cead94edSToby Isaac     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
985cead94edSToby Isaac     myFirst = offsets[rank];
986cead94edSToby Isaac     myLast  = offsets[rank + 1] - 1;
987cead94edSToby Isaac     ierr = PetscFree(offsets);CHKERRQ(ierr);
988cead94edSToby Isaac     if (numVertices) {
989cead94edSToby Isaac       PetscInt firstPart = 0, firstLargePart = 0;
990cead94edSToby Isaac       PetscInt lastPart = 0, lastLargePart = 0;
991cead94edSToby Isaac       PetscInt rem = nvGlobal % nparts;
992cead94edSToby Isaac       PetscInt pSmall = nvGlobal/nparts;
993cead94edSToby Isaac       PetscInt pBig = nvGlobal/nparts + 1;
994cead94edSToby Isaac 
995cead94edSToby Isaac 
996cead94edSToby Isaac       if (rem) {
997cead94edSToby Isaac         firstLargePart = myFirst / pBig;
998cead94edSToby Isaac         lastLargePart  = myLast  / pBig;
999cead94edSToby Isaac 
1000cead94edSToby Isaac         if (firstLargePart < rem) {
1001cead94edSToby Isaac           firstPart = firstLargePart;
1002cead94edSToby Isaac         }
1003cead94edSToby Isaac         else {
1004cead94edSToby Isaac           firstPart = rem + (myFirst - (rem * pBig)) / pSmall;
1005cead94edSToby Isaac         }
1006cead94edSToby Isaac         if (lastLargePart < rem) {
1007cead94edSToby Isaac           lastPart = lastLargePart;
1008cead94edSToby Isaac         }
1009cead94edSToby Isaac         else {
1010cead94edSToby Isaac           lastPart = rem + (myLast - (rem * pBig)) / pSmall;
1011cead94edSToby Isaac         }
1012cead94edSToby Isaac       }
1013cead94edSToby Isaac       else {
1014cead94edSToby Isaac         firstPart = myFirst / (nvGlobal/nparts);
1015cead94edSToby Isaac         lastPart  = myLast  / (nvGlobal/nparts);
1016cead94edSToby Isaac       }
1017cead94edSToby Isaac 
1018cead94edSToby Isaac       for (np = firstPart; np <= lastPart; np++) {
1019cead94edSToby Isaac         PetscInt PartStart =  np    * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np);
1020cead94edSToby Isaac         PetscInt PartEnd   = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1);
1021cead94edSToby Isaac 
1022cead94edSToby Isaac         PartStart = PetscMax(PartStart,myFirst);
1023cead94edSToby Isaac         PartEnd   = PetscMin(PartEnd,myLast+1);
1024cead94edSToby Isaac         ierr = PetscSectionSetDof(partSection,np,PartEnd-PartStart);CHKERRQ(ierr);
1025cead94edSToby Isaac       }
1026cead94edSToby Isaac     }
1027cead94edSToby Isaac   }
1028cead94edSToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1029555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1030555a9cf8SMatthew G. Knepley }
1031555a9cf8SMatthew G. Knepley 
1032d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part)
1033555a9cf8SMatthew G. Knepley {
1034555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1035555a9cf8SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Simple;
1036555a9cf8SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Simple;
1037555a9cf8SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Simple;
1038555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1039555a9cf8SMatthew G. Knepley }
1040555a9cf8SMatthew G. Knepley 
1041555a9cf8SMatthew G. Knepley /*MC
1042555a9cf8SMatthew G. Knepley   PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object
1043555a9cf8SMatthew G. Knepley 
1044555a9cf8SMatthew G. Knepley   Level: intermediate
1045555a9cf8SMatthew G. Knepley 
1046555a9cf8SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1047555a9cf8SMatthew G. Knepley M*/
1048555a9cf8SMatthew G. Knepley 
1049555a9cf8SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part)
1050555a9cf8SMatthew G. Knepley {
1051555a9cf8SMatthew G. Knepley   PetscPartitioner_Simple *p;
1052555a9cf8SMatthew G. Knepley   PetscErrorCode           ierr;
1053555a9cf8SMatthew G. Knepley 
1054555a9cf8SMatthew G. Knepley   PetscFunctionBegin;
1055555a9cf8SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1056555a9cf8SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1057555a9cf8SMatthew G. Knepley   part->data = p;
1058555a9cf8SMatthew G. Knepley 
1059555a9cf8SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Simple(part);CHKERRQ(ierr);
1060555a9cf8SMatthew G. Knepley   PetscFunctionReturn(0);
1061555a9cf8SMatthew G. Knepley }
1062555a9cf8SMatthew G. Knepley 
1063d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Gather(PetscPartitioner part)
1064dae52e14SToby Isaac {
1065dae52e14SToby Isaac   PetscPartitioner_Gather *p = (PetscPartitioner_Gather *) part->data;
1066dae52e14SToby Isaac   PetscErrorCode          ierr;
1067dae52e14SToby Isaac 
1068dae52e14SToby Isaac   PetscFunctionBegin;
1069dae52e14SToby Isaac   ierr = PetscFree(p);CHKERRQ(ierr);
1070dae52e14SToby Isaac   PetscFunctionReturn(0);
1071dae52e14SToby Isaac }
1072dae52e14SToby Isaac 
1073d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather_Ascii(PetscPartitioner part, PetscViewer viewer)
1074dae52e14SToby Isaac {
1075dae52e14SToby Isaac   PetscViewerFormat format;
1076dae52e14SToby Isaac   PetscErrorCode    ierr;
1077dae52e14SToby Isaac 
1078dae52e14SToby Isaac   PetscFunctionBegin;
1079dae52e14SToby Isaac   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1080dae52e14SToby Isaac   ierr = PetscViewerASCIIPrintf(viewer, "Gather Graph Partitioner:\n");CHKERRQ(ierr);
1081dae52e14SToby Isaac   PetscFunctionReturn(0);
1082dae52e14SToby Isaac }
1083dae52e14SToby Isaac 
1084d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Gather(PetscPartitioner part, PetscViewer viewer)
1085dae52e14SToby Isaac {
1086dae52e14SToby Isaac   PetscBool      iascii;
1087dae52e14SToby Isaac   PetscErrorCode ierr;
1088dae52e14SToby Isaac 
1089dae52e14SToby Isaac   PetscFunctionBegin;
1090dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1091dae52e14SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1092dae52e14SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1093dae52e14SToby Isaac   if (iascii) {ierr = PetscPartitionerView_Gather_Ascii(part, viewer);CHKERRQ(ierr);}
1094dae52e14SToby Isaac   PetscFunctionReturn(0);
1095dae52e14SToby Isaac }
1096dae52e14SToby Isaac 
1097d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Gather(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1098dae52e14SToby Isaac {
1099dae52e14SToby Isaac   PetscInt       np;
1100dae52e14SToby Isaac   PetscErrorCode ierr;
1101dae52e14SToby Isaac 
1102dae52e14SToby Isaac   PetscFunctionBegin;
1103dae52e14SToby Isaac   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1104dae52e14SToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition);CHKERRQ(ierr);
1105dae52e14SToby Isaac   ierr = PetscSectionSetDof(partSection,0,numVertices);CHKERRQ(ierr);
1106dae52e14SToby Isaac   for (np = 1; np < nparts; ++np) {ierr = PetscSectionSetDof(partSection, np, 0);CHKERRQ(ierr);}
1107dae52e14SToby Isaac   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1108dae52e14SToby Isaac   PetscFunctionReturn(0);
1109dae52e14SToby Isaac }
1110dae52e14SToby Isaac 
1111d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Gather(PetscPartitioner part)
1112dae52e14SToby Isaac {
1113dae52e14SToby Isaac   PetscFunctionBegin;
1114dae52e14SToby Isaac   part->ops->view      = PetscPartitionerView_Gather;
1115dae52e14SToby Isaac   part->ops->destroy   = PetscPartitionerDestroy_Gather;
1116dae52e14SToby Isaac   part->ops->partition = PetscPartitionerPartition_Gather;
1117dae52e14SToby Isaac   PetscFunctionReturn(0);
1118dae52e14SToby Isaac }
1119dae52e14SToby Isaac 
1120dae52e14SToby Isaac /*MC
1121dae52e14SToby Isaac   PETSCPARTITIONERGATHER = "gather" - A PetscPartitioner object
1122dae52e14SToby Isaac 
1123dae52e14SToby Isaac   Level: intermediate
1124dae52e14SToby Isaac 
1125dae52e14SToby Isaac .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1126dae52e14SToby Isaac M*/
1127dae52e14SToby Isaac 
1128dae52e14SToby Isaac PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Gather(PetscPartitioner part)
1129dae52e14SToby Isaac {
1130dae52e14SToby Isaac   PetscPartitioner_Gather *p;
1131dae52e14SToby Isaac   PetscErrorCode           ierr;
1132dae52e14SToby Isaac 
1133dae52e14SToby Isaac   PetscFunctionBegin;
1134dae52e14SToby Isaac   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1135dae52e14SToby Isaac   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
1136dae52e14SToby Isaac   part->data = p;
1137dae52e14SToby Isaac 
1138dae52e14SToby Isaac   ierr = PetscPartitionerInitialize_Gather(part);CHKERRQ(ierr);
1139dae52e14SToby Isaac   PetscFunctionReturn(0);
1140dae52e14SToby Isaac }
1141dae52e14SToby Isaac 
1142dae52e14SToby Isaac 
1143d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part)
114477623264SMatthew G. Knepley {
114577623264SMatthew G. Knepley   PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data;
114677623264SMatthew G. Knepley   PetscErrorCode          ierr;
114777623264SMatthew G. Knepley 
114877623264SMatthew G. Knepley   PetscFunctionBegin;
114977623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
115077623264SMatthew G. Knepley   PetscFunctionReturn(0);
115177623264SMatthew G. Knepley }
115277623264SMatthew G. Knepley 
1153d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco_Ascii(PetscPartitioner part, PetscViewer viewer)
115477623264SMatthew G. Knepley {
115577623264SMatthew G. Knepley   PetscViewerFormat format;
115677623264SMatthew G. Knepley   PetscErrorCode    ierr;
115777623264SMatthew G. Knepley 
115877623264SMatthew G. Knepley   PetscFunctionBegin;
115977623264SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
116077623264SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Chaco Graph Partitioner:\n");CHKERRQ(ierr);
116177623264SMatthew G. Knepley   PetscFunctionReturn(0);
116277623264SMatthew G. Knepley }
116377623264SMatthew G. Knepley 
1164d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer)
116577623264SMatthew G. Knepley {
116677623264SMatthew G. Knepley   PetscBool      iascii;
116777623264SMatthew G. Knepley   PetscErrorCode ierr;
116877623264SMatthew G. Knepley 
116977623264SMatthew G. Knepley   PetscFunctionBegin;
117077623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
117177623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
117277623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
117377623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_Chaco_Ascii(part, viewer);CHKERRQ(ierr);}
117477623264SMatthew G. Knepley   PetscFunctionReturn(0);
117577623264SMatthew G. Knepley }
117677623264SMatthew G. Knepley 
117770034214SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
117870034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
117970034214SMatthew G. Knepley #include <unistd.h>
118070034214SMatthew G. Knepley #endif
118111d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
118211d1e910SBarry Smith #include <chaco.h>
118311d1e910SBarry Smith #else
118411d1e910SBarry Smith /* Older versions of Chaco do not have an include file */
118570034214SMatthew G. Knepley PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
118670034214SMatthew G. Knepley                        float *ewgts, float *x, float *y, float *z, char *outassignname,
118770034214SMatthew G. Knepley                        char *outfilename, short *assignment, int architecture, int ndims_tot,
118870034214SMatthew G. Knepley                        int mesh_dims[3], double *goal, int global_method, int local_method,
118970034214SMatthew G. Knepley                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
119011d1e910SBarry Smith #endif
119170034214SMatthew G. Knepley extern int FREE_GRAPH;
119277623264SMatthew G. Knepley #endif
119370034214SMatthew G. Knepley 
1194d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
119570034214SMatthew G. Knepley {
119677623264SMatthew G. Knepley #if defined(PETSC_HAVE_CHACO)
119770034214SMatthew G. Knepley   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
119870034214SMatthew G. Knepley   MPI_Comm       comm;
119970034214SMatthew G. Knepley   int            nvtxs          = numVertices; /* number of vertices in full graph */
120070034214SMatthew G. Knepley   int           *vwgts          = NULL;   /* weights for all vertices */
120170034214SMatthew G. Knepley   float         *ewgts          = NULL;   /* weights for all edges */
120270034214SMatthew G. Knepley   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
120370034214SMatthew G. Knepley   char          *outassignname  = NULL;   /*  name of assignment output file */
120470034214SMatthew G. Knepley   char          *outfilename    = NULL;   /* output file name */
120570034214SMatthew G. Knepley   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
120670034214SMatthew G. Knepley   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
120770034214SMatthew G. Knepley   int            mesh_dims[3];            /* dimensions of mesh of processors */
120870034214SMatthew G. Knepley   double        *goal          = NULL;    /* desired set sizes for each set */
120970034214SMatthew G. Knepley   int            global_method = 1;       /* global partitioning algorithm */
121070034214SMatthew G. Knepley   int            local_method  = 1;       /* local partitioning algorithm */
121170034214SMatthew G. Knepley   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
121270034214SMatthew G. Knepley   int            vmax          = 200;     /* how many vertices to coarsen down to? */
121370034214SMatthew G. Knepley   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
121470034214SMatthew G. Knepley   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
121570034214SMatthew G. Knepley   long           seed          = 123636512; /* for random graph mutations */
121611d1e910SBarry Smith #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
121711d1e910SBarry Smith   int           *assignment;              /* Output partition */
121811d1e910SBarry Smith #else
121970034214SMatthew G. Knepley   short int     *assignment;              /* Output partition */
122011d1e910SBarry Smith #endif
122170034214SMatthew G. Knepley   int            fd_stdout, fd_pipe[2];
122270034214SMatthew G. Knepley   PetscInt      *points;
122370034214SMatthew G. Knepley   int            i, v, p;
122470034214SMatthew G. Knepley   PetscErrorCode ierr;
122570034214SMatthew G. Knepley 
122670034214SMatthew G. Knepley   PetscFunctionBegin;
122770034214SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
122807ed3857SLisandro Dalcin #if defined (PETSC_USE_DEBUG)
122907ed3857SLisandro Dalcin   {
123007ed3857SLisandro Dalcin     int ival,isum;
123107ed3857SLisandro Dalcin     PetscBool distributed;
123207ed3857SLisandro Dalcin 
123307ed3857SLisandro Dalcin     ival = (numVertices > 0);
123407ed3857SLisandro Dalcin     ierr = MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr);
123507ed3857SLisandro Dalcin     distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE;
123607ed3857SLisandro Dalcin     if (distributed) SETERRQ(comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph");
123707ed3857SLisandro Dalcin   }
123807ed3857SLisandro Dalcin #endif
123970034214SMatthew G. Knepley   if (!numVertices) {
124077623264SMatthew G. Knepley     ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
124177623264SMatthew G. Knepley     ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
124270034214SMatthew G. Knepley     ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
124370034214SMatthew G. Knepley     PetscFunctionReturn(0);
124470034214SMatthew G. Knepley   }
124570034214SMatthew G. Knepley   FREE_GRAPH = 0;                         /* Do not let Chaco free my memory */
124670034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
124770034214SMatthew G. Knepley 
124870034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
124970034214SMatthew G. Knepley     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
125070034214SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
125170034214SMatthew G. Knepley   }
125277623264SMatthew G. Knepley   mesh_dims[0] = nparts;
125370034214SMatthew G. Knepley   mesh_dims[1] = 1;
125470034214SMatthew G. Knepley   mesh_dims[2] = 1;
125570034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr);
125670034214SMatthew G. Knepley   /* Chaco outputs to stdout. We redirect this to a buffer. */
125770034214SMatthew G. Knepley   /* TODO: check error codes for UNIX calls */
125870034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
125970034214SMatthew G. Knepley   {
126070034214SMatthew G. Knepley     int piperet;
126170034214SMatthew G. Knepley     piperet = pipe(fd_pipe);
126270034214SMatthew G. Knepley     if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe");
126370034214SMatthew G. Knepley     fd_stdout = dup(1);
126470034214SMatthew G. Knepley     close(1);
126570034214SMatthew G. Knepley     dup2(fd_pipe[1], 1);
126670034214SMatthew G. Knepley   }
126770034214SMatthew G. Knepley #endif
126870034214SMatthew G. Knepley   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
126970034214SMatthew G. Knepley                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
127070034214SMatthew G. Knepley                    vmax, ndims, eigtol, seed);
127170034214SMatthew G. Knepley #if defined(PETSC_HAVE_UNISTD_H)
127270034214SMatthew G. Knepley   {
127370034214SMatthew G. Knepley     char msgLog[10000];
127470034214SMatthew G. Knepley     int  count;
127570034214SMatthew G. Knepley 
127670034214SMatthew G. Knepley     fflush(stdout);
127770034214SMatthew G. Knepley     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
127870034214SMatthew G. Knepley     if (count < 0) count = 0;
127970034214SMatthew G. Knepley     msgLog[count] = 0;
128070034214SMatthew G. Knepley     close(1);
128170034214SMatthew G. Knepley     dup2(fd_stdout, 1);
128270034214SMatthew G. Knepley     close(fd_stdout);
128370034214SMatthew G. Knepley     close(fd_pipe[0]);
128470034214SMatthew G. Knepley     close(fd_pipe[1]);
128570034214SMatthew G. Knepley     if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
128670034214SMatthew G. Knepley   }
128707ed3857SLisandro Dalcin #else
128807ed3857SLisandro Dalcin   if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout");
128970034214SMatthew G. Knepley #endif
129070034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
129177623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
129270034214SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {
129377623264SMatthew G. Knepley     ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);
129470034214SMatthew G. Knepley   }
129577623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
129670034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
129777623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
129870034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
129970034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
130070034214SMatthew G. Knepley     }
130170034214SMatthew G. Knepley   }
130270034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
130370034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
130470034214SMatthew G. Knepley   if (global_method == INERTIAL_METHOD) {
130570034214SMatthew G. Knepley     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
130670034214SMatthew G. Knepley   }
130770034214SMatthew G. Knepley   ierr = PetscFree(assignment);CHKERRQ(ierr);
130870034214SMatthew G. Knepley   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
130970034214SMatthew G. Knepley   PetscFunctionReturn(0);
131077623264SMatthew G. Knepley #else
131177623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
131270034214SMatthew G. Knepley #endif
131377623264SMatthew G. Knepley }
131477623264SMatthew G. Knepley 
1315d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part)
131677623264SMatthew G. Knepley {
131777623264SMatthew G. Knepley   PetscFunctionBegin;
131877623264SMatthew G. Knepley   part->ops->view      = PetscPartitionerView_Chaco;
131977623264SMatthew G. Knepley   part->ops->destroy   = PetscPartitionerDestroy_Chaco;
132077623264SMatthew G. Knepley   part->ops->partition = PetscPartitionerPartition_Chaco;
132177623264SMatthew G. Knepley   PetscFunctionReturn(0);
132277623264SMatthew G. Knepley }
132377623264SMatthew G. Knepley 
132477623264SMatthew G. Knepley /*MC
132577623264SMatthew G. Knepley   PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library
132677623264SMatthew G. Knepley 
132777623264SMatthew G. Knepley   Level: intermediate
132877623264SMatthew G. Knepley 
132977623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
133077623264SMatthew G. Knepley M*/
133177623264SMatthew G. Knepley 
133277623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part)
133377623264SMatthew G. Knepley {
133477623264SMatthew G. Knepley   PetscPartitioner_Chaco *p;
133577623264SMatthew G. Knepley   PetscErrorCode          ierr;
133677623264SMatthew G. Knepley 
133777623264SMatthew G. Knepley   PetscFunctionBegin;
133877623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
133977623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
134077623264SMatthew G. Knepley   part->data = p;
134177623264SMatthew G. Knepley 
134277623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_Chaco(part);CHKERRQ(ierr);
134377623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionercite);CHKERRQ(ierr);
134477623264SMatthew G. Knepley   PetscFunctionReturn(0);
134577623264SMatthew G. Knepley }
134677623264SMatthew G. Knepley 
1347d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerDestroy_ParMetis(PetscPartitioner part)
134877623264SMatthew G. Knepley {
134977623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
135077623264SMatthew G. Knepley   PetscErrorCode             ierr;
135177623264SMatthew G. Knepley 
135277623264SMatthew G. Knepley   PetscFunctionBegin;
135377623264SMatthew G. Knepley   ierr = PetscFree(p);CHKERRQ(ierr);
135477623264SMatthew G. Knepley   PetscFunctionReturn(0);
135577623264SMatthew G. Knepley }
135677623264SMatthew G. Knepley 
1357d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis_Ascii(PetscPartitioner part, PetscViewer viewer)
135877623264SMatthew G. Knepley {
135977623264SMatthew G. Knepley   PetscViewerFormat format;
136077623264SMatthew G. Knepley   PetscErrorCode    ierr;
136177623264SMatthew G. Knepley 
136277623264SMatthew G. Knepley   PetscFunctionBegin;
136377623264SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
136477623264SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "ParMetis Graph Partitioner:\n");CHKERRQ(ierr);
136577623264SMatthew G. Knepley   PetscFunctionReturn(0);
136677623264SMatthew G. Knepley }
136777623264SMatthew G. Knepley 
1368d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerView_ParMetis(PetscPartitioner part, PetscViewer viewer)
136977623264SMatthew G. Knepley {
137077623264SMatthew G. Knepley   PetscBool      iascii;
137177623264SMatthew G. Knepley   PetscErrorCode ierr;
137277623264SMatthew G. Knepley 
137377623264SMatthew G. Knepley   PetscFunctionBegin;
137477623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
137577623264SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
137677623264SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
137777623264SMatthew G. Knepley   if (iascii) {ierr = PetscPartitionerView_ParMetis_Ascii(part, viewer);CHKERRQ(ierr);}
137877623264SMatthew G. Knepley   PetscFunctionReturn(0);
137977623264SMatthew G. Knepley }
138070034214SMatthew G. Knepley 
138144d8be81SLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_ParMetis(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
138244d8be81SLisandro Dalcin {
138344d8be81SLisandro Dalcin   static const char         *ptypes[] = {"kway", "rb"};
138444d8be81SLisandro Dalcin   PetscPartitioner_ParMetis *p = (PetscPartitioner_ParMetis *) part->data;
138544d8be81SLisandro Dalcin   PetscErrorCode            ierr;
138644d8be81SLisandro Dalcin 
138744d8be81SLisandro Dalcin   PetscFunctionBegin;
138844d8be81SLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner ParMetis Options");CHKERRQ(ierr);
138944d8be81SLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_parmetis_type", "Partitioning method", "", ptypes, 2, ptypes[p->ptype], &p->ptype, NULL);CHKERRQ(ierr);
139044d8be81SLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
139144d8be81SLisandro Dalcin   PetscFunctionReturn(0);
139244d8be81SLisandro Dalcin }
139344d8be81SLisandro Dalcin 
139470034214SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
139570034214SMatthew G. Knepley #include <parmetis.h>
139677623264SMatthew G. Knepley #endif
139770034214SMatthew G. Knepley 
1398d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerPartition_ParMetis(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
139970034214SMatthew G. Knepley {
140077623264SMatthew G. Knepley #if defined(PETSC_HAVE_PARMETIS)
140170034214SMatthew G. Knepley   MPI_Comm       comm;
1402deea36a5SMatthew G. Knepley   PetscSection   section;
140370034214SMatthew G. Knepley   PetscInt       nvtxs      = numVertices; /* The number of vertices in full graph */
140470034214SMatthew G. Knepley   PetscInt      *vtxdist;                  /* Distribution of vertices across processes */
140570034214SMatthew G. Knepley   PetscInt      *xadj       = start;       /* Start of edge list for each vertex */
140670034214SMatthew G. Knepley   PetscInt      *adjncy     = adjacency;   /* Edge lists for all vertices */
140770034214SMatthew G. Knepley   PetscInt      *vwgt       = NULL;        /* Vertex weights */
140870034214SMatthew G. Knepley   PetscInt      *adjwgt     = NULL;        /* Edge weights */
140970034214SMatthew G. Knepley   PetscInt       wgtflag    = 0;           /* Indicates which weights are present */
141070034214SMatthew G. Knepley   PetscInt       numflag    = 0;           /* Indicates initial offset (0 or 1) */
141170034214SMatthew G. Knepley   PetscInt       ncon       = 1;           /* The number of weights per vertex */
1412fb83b9f2SMichael Gegg   real_t        *tpwgts;                   /* The fraction of vertex weights assigned to each partition */
1413fb83b9f2SMichael Gegg   real_t        *ubvec;                    /* The balance intolerance for vertex weights */
1414b3ce585bSLisandro Dalcin   PetscInt       options[64];              /* Options */
141570034214SMatthew G. Knepley   /* Outputs */
141670034214SMatthew G. Knepley   PetscInt       edgeCut;                  /* The number of edges cut by the partition */
1417b3ce585bSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1418b3ce585bSLisandro Dalcin   PetscMPIInt    size, rank, p;
141944d8be81SLisandro Dalcin   PetscInt       metis_ptype = ((PetscPartitioner_ParMetis *) part->data)->ptype;
142070034214SMatthew G. Knepley   PetscErrorCode ierr;
142170034214SMatthew G. Knepley 
142270034214SMatthew G. Knepley   PetscFunctionBegin;
142377623264SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) part, &comm);CHKERRQ(ierr);
1424b3ce585bSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
142570034214SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
142670034214SMatthew G. Knepley   /* Calculate vertex distribution */
1427b3ce585bSLisandro Dalcin   ierr = PetscMalloc5(size+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment,nvtxs,&vwgt);CHKERRQ(ierr);
142870034214SMatthew G. Knepley   vtxdist[0] = 0;
142970034214SMatthew G. Knepley   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1430b3ce585bSLisandro Dalcin   for (p = 2; p <= size; ++p) {
143170034214SMatthew G. Knepley     vtxdist[p] += vtxdist[p-1];
143270034214SMatthew G. Knepley   }
143344d8be81SLisandro Dalcin   /* Calculate partition weights */
143470034214SMatthew G. Knepley   for (p = 0; p < nparts; ++p) {
143570034214SMatthew G. Knepley     tpwgts[p] = 1.0/nparts;
143670034214SMatthew G. Knepley   }
143770034214SMatthew G. Knepley   ubvec[0] = 1.05;
1438deea36a5SMatthew G. Knepley   /* Weight cells by dofs on cell by default */
1439e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1440deea36a5SMatthew G. Knepley   if (section) {
1441deea36a5SMatthew G. Knepley     PetscInt cStart, cEnd, dof;
144270034214SMatthew G. Knepley 
1443deea36a5SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1444deea36a5SMatthew G. Knepley     for (v = cStart; v < cEnd; ++v) {
1445deea36a5SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1446925b1076SLisandro Dalcin       /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1447925b1076SLisandro Dalcin       /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1448925b1076SLisandro Dalcin       if (v-cStart < numVertices) vwgt[v-cStart] = PetscMax(dof, 1);
1449deea36a5SMatthew G. Knepley     }
1450deea36a5SMatthew G. Knepley   } else {
1451deea36a5SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1452cd0de0f2SShri   }
145344d8be81SLisandro Dalcin   wgtflag |= 2; /* have weights on graph vertices */
1454cd0de0f2SShri 
145570034214SMatthew G. Knepley   if (nparts == 1) {
14569fc93327SToby Isaac     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
145770034214SMatthew G. Knepley   } else {
1458b3ce585bSLisandro Dalcin     for (p = 0; !vtxdist[p+1] && p < size; ++p);
1459b3ce585bSLisandro Dalcin     if (vtxdist[p+1] == vtxdist[size]) {
1460b3ce585bSLisandro Dalcin       if (rank == p) {
146144d8be81SLisandro Dalcin         ierr = METIS_SetDefaultOptions(options); /* initialize all defaults */
146244d8be81SLisandro Dalcin         if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_SetDefaultOptions()");
146344d8be81SLisandro Dalcin         if (metis_ptype == 1) {
146444d8be81SLisandro Dalcin           PetscStackPush("METIS_PartGraphRecursive");
146544d8be81SLisandro Dalcin           ierr = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &edgeCut, assignment);
146644d8be81SLisandro Dalcin           PetscStackPop;
146744d8be81SLisandro Dalcin           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphRecursive()");
146844d8be81SLisandro Dalcin         } else {
146944d8be81SLisandro Dalcin           /*
147044d8be81SLisandro Dalcin            It would be nice to activate the two options below, but they would need some actual testing.
147144d8be81SLisandro Dalcin            - Turning on these options may exercise path of the METIS code that have bugs and may break production runs.
147244d8be81SLisandro 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.
147344d8be81SLisandro Dalcin           */
147444d8be81SLisandro Dalcin           /* options[METIS_OPTION_CONTIG]  = 1; */ /* try to produce partitions that are contiguous */
147544d8be81SLisandro Dalcin           /* options[METIS_OPTION_MINCONN] = 1; */ /* minimize the maximum degree of the subdomain graph */
147670034214SMatthew G. Knepley           PetscStackPush("METIS_PartGraphKway");
147744d8be81SLisandro Dalcin           ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, options, &edgeCut, assignment);
147870034214SMatthew G. Knepley           PetscStackPop;
147970034214SMatthew G. Knepley           if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
148070034214SMatthew G. Knepley         }
148144d8be81SLisandro Dalcin       }
148270034214SMatthew G. Knepley     } else {
148344d8be81SLisandro Dalcin       options[0] = 0; /* use all defaults */
148470034214SMatthew G. Knepley       PetscStackPush("ParMETIS_V3_PartKway");
148570034214SMatthew G. Knepley       ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgeCut, assignment, &comm);
148670034214SMatthew G. Knepley       PetscStackPop;
1487c717d290SMatthew G. Knepley       if (ierr != METIS_OK) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error %d in ParMETIS_V3_PartKway()", ierr);
148870034214SMatthew G. Knepley     }
148970034214SMatthew G. Knepley   }
149070034214SMatthew G. Knepley   /* Convert to PetscSection+IS */
149177623264SMatthew G. Knepley   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
149277623264SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
149377623264SMatthew G. Knepley   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
149470034214SMatthew G. Knepley   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
149577623264SMatthew G. Knepley   for (p = 0, i = 0; p < nparts; ++p) {
149670034214SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
149770034214SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
149870034214SMatthew G. Knepley     }
149970034214SMatthew G. Knepley   }
150070034214SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
150170034214SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1502cd0de0f2SShri   ierr = PetscFree5(vtxdist,tpwgts,ubvec,assignment,vwgt);CHKERRQ(ierr);
15039b80ac48SMatthew G. Knepley   PetscFunctionReturn(0);
150470034214SMatthew G. Knepley #else
150577623264SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-parmetis.");
150670034214SMatthew G. Knepley #endif
150770034214SMatthew G. Knepley }
150870034214SMatthew G. Knepley 
1509d5577e40SMatthew G. Knepley static PetscErrorCode PetscPartitionerInitialize_ParMetis(PetscPartitioner part)
151077623264SMatthew G. Knepley {
151177623264SMatthew G. Knepley   PetscFunctionBegin;
151277623264SMatthew G. Knepley   part->ops->view           = PetscPartitionerView_ParMetis;
151344d8be81SLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_ParMetis;
151477623264SMatthew G. Knepley   part->ops->destroy        = PetscPartitionerDestroy_ParMetis;
151577623264SMatthew G. Knepley   part->ops->partition      = PetscPartitionerPartition_ParMetis;
151677623264SMatthew G. Knepley   PetscFunctionReturn(0);
151777623264SMatthew G. Knepley }
151877623264SMatthew G. Knepley 
151977623264SMatthew G. Knepley /*MC
152077623264SMatthew G. Knepley   PETSCPARTITIONERPARMETIS = "parmetis" - A PetscPartitioner object using the ParMetis library
152177623264SMatthew G. Knepley 
152277623264SMatthew G. Knepley   Level: intermediate
152377623264SMatthew G. Knepley 
152477623264SMatthew G. Knepley .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
152577623264SMatthew G. Knepley M*/
152677623264SMatthew G. Knepley 
152777623264SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_ParMetis(PetscPartitioner part)
152877623264SMatthew G. Knepley {
152977623264SMatthew G. Knepley   PetscPartitioner_ParMetis *p;
153077623264SMatthew G. Knepley   PetscErrorCode          ierr;
153177623264SMatthew G. Knepley 
153277623264SMatthew G. Knepley   PetscFunctionBegin;
153377623264SMatthew G. Knepley   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
153477623264SMatthew G. Knepley   ierr       = PetscNewLog(part, &p);CHKERRQ(ierr);
153577623264SMatthew G. Knepley   part->data = p;
153677623264SMatthew G. Knepley 
153777623264SMatthew G. Knepley   ierr = PetscPartitionerInitialize_ParMetis(part);CHKERRQ(ierr);
153877623264SMatthew G. Knepley   ierr = PetscCitationsRegister(ParMetisPartitionerCitation, &ParMetisPartitionercite);CHKERRQ(ierr);
153970034214SMatthew G. Knepley   PetscFunctionReturn(0);
154070034214SMatthew G. Knepley }
154170034214SMatthew G. Knepley 
1542137cd93aSLisandro Dalcin 
1543137cd93aSLisandro Dalcin PetscBool PTScotchPartitionercite = PETSC_FALSE;
1544137cd93aSLisandro Dalcin const char PTScotchPartitionerCitation[] =
1545137cd93aSLisandro Dalcin   "@article{PTSCOTCH,\n"
1546137cd93aSLisandro Dalcin   "  author  = {C. Chevalier and F. Pellegrini},\n"
1547137cd93aSLisandro Dalcin   "  title   = {{PT-SCOTCH}: a tool for efficient parallel graph ordering},\n"
1548137cd93aSLisandro Dalcin   "  journal = {Parallel Computing},\n"
1549137cd93aSLisandro Dalcin   "  volume  = {34},\n"
1550137cd93aSLisandro Dalcin   "  number  = {6},\n"
1551137cd93aSLisandro Dalcin   "  pages   = {318--331},\n"
1552137cd93aSLisandro Dalcin   "  year    = {2008},\n"
1553137cd93aSLisandro Dalcin   "  doi     = {https://doi.org/10.1016/j.parco.2007.12.001}\n"
1554137cd93aSLisandro Dalcin   "}\n";
1555137cd93aSLisandro Dalcin 
1556137cd93aSLisandro Dalcin typedef struct {
1557137cd93aSLisandro Dalcin   PetscInt  strategy;
1558137cd93aSLisandro Dalcin   PetscReal imbalance;
1559137cd93aSLisandro Dalcin } PetscPartitioner_PTScotch;
1560137cd93aSLisandro Dalcin 
1561137cd93aSLisandro Dalcin static const char *const
1562137cd93aSLisandro Dalcin PTScotchStrategyList[] = {
1563137cd93aSLisandro Dalcin   "DEFAULT",
1564137cd93aSLisandro Dalcin   "QUALITY",
1565137cd93aSLisandro Dalcin   "SPEED",
1566137cd93aSLisandro Dalcin   "BALANCE",
1567137cd93aSLisandro Dalcin   "SAFETY",
1568137cd93aSLisandro Dalcin   "SCALABILITY",
1569137cd93aSLisandro Dalcin   "RECURSIVE",
1570137cd93aSLisandro Dalcin   "REMAP"
1571137cd93aSLisandro Dalcin };
1572137cd93aSLisandro Dalcin 
1573137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1574137cd93aSLisandro Dalcin 
1575137cd93aSLisandro Dalcin EXTERN_C_BEGIN
1576137cd93aSLisandro Dalcin #include <ptscotch.h>
1577137cd93aSLisandro Dalcin EXTERN_C_END
1578137cd93aSLisandro Dalcin 
1579137cd93aSLisandro Dalcin #define CHKERRPTSCOTCH(ierr) do { if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error calling PT-Scotch library"); } while(0)
1580137cd93aSLisandro Dalcin 
1581137cd93aSLisandro Dalcin static int PTScotch_Strategy(PetscInt strategy)
1582137cd93aSLisandro Dalcin {
1583137cd93aSLisandro Dalcin   switch (strategy) {
1584137cd93aSLisandro Dalcin   case  0: return SCOTCH_STRATDEFAULT;
1585137cd93aSLisandro Dalcin   case  1: return SCOTCH_STRATQUALITY;
1586137cd93aSLisandro Dalcin   case  2: return SCOTCH_STRATSPEED;
1587137cd93aSLisandro Dalcin   case  3: return SCOTCH_STRATBALANCE;
1588137cd93aSLisandro Dalcin   case  4: return SCOTCH_STRATSAFETY;
1589137cd93aSLisandro Dalcin   case  5: return SCOTCH_STRATSCALABILITY;
1590137cd93aSLisandro Dalcin   case  6: return SCOTCH_STRATRECURSIVE;
1591137cd93aSLisandro Dalcin   case  7: return SCOTCH_STRATREMAP;
1592137cd93aSLisandro Dalcin   default: return SCOTCH_STRATDEFAULT;
1593137cd93aSLisandro Dalcin   }
1594137cd93aSLisandro Dalcin }
1595137cd93aSLisandro Dalcin 
1596137cd93aSLisandro Dalcin 
1597137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_Seq(SCOTCH_Num strategy, double imbalance, SCOTCH_Num n, SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1598137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[])
1599137cd93aSLisandro Dalcin {
1600137cd93aSLisandro Dalcin   SCOTCH_Graph   grafdat;
1601137cd93aSLisandro Dalcin   SCOTCH_Strat   stradat;
1602137cd93aSLisandro Dalcin   SCOTCH_Num     vertnbr = n;
1603137cd93aSLisandro Dalcin   SCOTCH_Num     edgenbr = xadj[n];
1604137cd93aSLisandro Dalcin   SCOTCH_Num*    velotab = vtxwgt;
1605137cd93aSLisandro Dalcin   SCOTCH_Num*    edlotab = adjwgt;
1606137cd93aSLisandro Dalcin   SCOTCH_Num     flagval = strategy;
1607137cd93aSLisandro Dalcin   double         kbalval = imbalance;
1608137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1609137cd93aSLisandro Dalcin 
1610137cd93aSLisandro Dalcin   PetscFunctionBegin;
1611d99a0000SVaclav Hapla   {
1612d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1613d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1614d99a0000SVaclav Hapla     if (!flg) velotab = NULL;
1615d99a0000SVaclav Hapla   }
1616137cd93aSLisandro Dalcin   ierr = SCOTCH_graphInit(&grafdat);CHKERRPTSCOTCH(ierr);
1617137cd93aSLisandro Dalcin   ierr = SCOTCH_graphBuild(&grafdat, 0, vertnbr, xadj, xadj + 1, velotab, NULL, edgenbr, adjncy, edlotab);CHKERRPTSCOTCH(ierr);
1618137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1619137cd93aSLisandro Dalcin   ierr = SCOTCH_stratGraphMapBuild(&stradat, flagval, nparts, kbalval);CHKERRPTSCOTCH(ierr);
1620137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1621137cd93aSLisandro Dalcin   ierr = SCOTCH_graphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1622137cd93aSLisandro Dalcin #endif
1623137cd93aSLisandro Dalcin   ierr = SCOTCH_graphPart(&grafdat, nparts, &stradat, part);CHKERRPTSCOTCH(ierr);
1624137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1625137cd93aSLisandro Dalcin   SCOTCH_graphExit(&grafdat);
1626137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1627137cd93aSLisandro Dalcin }
1628137cd93aSLisandro Dalcin 
1629137cd93aSLisandro Dalcin static PetscErrorCode PTScotch_PartGraph_MPI(SCOTCH_Num strategy, double imbalance, SCOTCH_Num vtxdist[], SCOTCH_Num xadj[], SCOTCH_Num adjncy[],
1630137cd93aSLisandro Dalcin                                              SCOTCH_Num vtxwgt[], SCOTCH_Num adjwgt[], SCOTCH_Num nparts, SCOTCH_Num part[], MPI_Comm comm)
1631137cd93aSLisandro Dalcin {
1632137cd93aSLisandro Dalcin   PetscMPIInt     procglbnbr;
1633137cd93aSLisandro Dalcin   PetscMPIInt     proclocnum;
1634137cd93aSLisandro Dalcin   SCOTCH_Arch     archdat;
1635137cd93aSLisandro Dalcin   SCOTCH_Dgraph   grafdat;
1636137cd93aSLisandro Dalcin   SCOTCH_Dmapping mappdat;
1637137cd93aSLisandro Dalcin   SCOTCH_Strat    stradat;
1638137cd93aSLisandro Dalcin   SCOTCH_Num      vertlocnbr;
1639137cd93aSLisandro Dalcin   SCOTCH_Num      edgelocnbr;
1640137cd93aSLisandro Dalcin   SCOTCH_Num*     veloloctab = vtxwgt;
1641137cd93aSLisandro Dalcin   SCOTCH_Num*     edloloctab = adjwgt;
1642137cd93aSLisandro Dalcin   SCOTCH_Num      flagval = strategy;
1643137cd93aSLisandro Dalcin   double          kbalval = imbalance;
1644137cd93aSLisandro Dalcin   PetscErrorCode  ierr;
1645137cd93aSLisandro Dalcin 
1646137cd93aSLisandro Dalcin   PetscFunctionBegin;
1647d99a0000SVaclav Hapla   {
1648d99a0000SVaclav Hapla     PetscBool flg = PETSC_TRUE;
1649d99a0000SVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-petscpartititoner_ptscotch_vertex_weight", &flg, NULL);CHKERRQ(ierr);
1650d99a0000SVaclav Hapla     if (!flg) veloloctab = NULL;
1651d99a0000SVaclav Hapla   }
1652137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &procglbnbr);CHKERRQ(ierr);
1653137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &proclocnum);CHKERRQ(ierr);
1654137cd93aSLisandro Dalcin   vertlocnbr = vtxdist[proclocnum + 1] - vtxdist[proclocnum];
1655137cd93aSLisandro Dalcin   edgelocnbr = xadj[vertlocnbr];
1656137cd93aSLisandro Dalcin 
1657137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphInit(&grafdat, comm);CHKERRPTSCOTCH(ierr);
1658137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphBuild(&grafdat, 0, vertlocnbr, vertlocnbr, xadj, xadj + 1, veloloctab, NULL, edgelocnbr, edgelocnbr, adjncy, NULL, edloloctab);CHKERRPTSCOTCH(ierr);
1659137cd93aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
1660137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphCheck(&grafdat);CHKERRPTSCOTCH(ierr);
1661137cd93aSLisandro Dalcin #endif
1662137cd93aSLisandro Dalcin   ierr = SCOTCH_stratInit(&stradat);CHKERRPTSCOTCH(ierr);
1663137cd93aSLisandro Dalcin   ierr = SCOTCH_stratDgraphMapBuild(&stradat, flagval, procglbnbr, nparts, kbalval);CHKERRQ(ierr);
1664137cd93aSLisandro Dalcin   ierr = SCOTCH_archInit(&archdat);CHKERRPTSCOTCH(ierr);
1665137cd93aSLisandro Dalcin   ierr = SCOTCH_archCmplt(&archdat, nparts);CHKERRPTSCOTCH(ierr);
1666137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapInit(&grafdat, &mappdat, &archdat, part);CHKERRPTSCOTCH(ierr);
1667137cd93aSLisandro Dalcin   ierr = SCOTCH_dgraphMapCompute(&grafdat, &mappdat, &stradat);CHKERRPTSCOTCH(ierr);
1668137cd93aSLisandro Dalcin   SCOTCH_dgraphMapExit(&grafdat, &mappdat);
1669137cd93aSLisandro Dalcin   SCOTCH_archExit(&archdat);
1670137cd93aSLisandro Dalcin   SCOTCH_stratExit(&stradat);
1671137cd93aSLisandro Dalcin   SCOTCH_dgraphExit(&grafdat);
1672137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1673137cd93aSLisandro Dalcin }
1674137cd93aSLisandro Dalcin 
1675137cd93aSLisandro Dalcin #endif /* PETSC_HAVE_PTSCOTCH */
1676137cd93aSLisandro Dalcin 
1677137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerDestroy_PTScotch(PetscPartitioner part)
1678137cd93aSLisandro Dalcin {
1679137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1680137cd93aSLisandro Dalcin   PetscErrorCode             ierr;
1681137cd93aSLisandro Dalcin 
1682137cd93aSLisandro Dalcin   PetscFunctionBegin;
1683137cd93aSLisandro Dalcin   ierr = PetscFree(p);CHKERRQ(ierr);
1684137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1685137cd93aSLisandro Dalcin }
1686137cd93aSLisandro Dalcin 
1687137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch_Ascii(PetscPartitioner part, PetscViewer viewer)
1688137cd93aSLisandro Dalcin {
1689137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1690137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1691137cd93aSLisandro Dalcin 
1692137cd93aSLisandro Dalcin   PetscFunctionBegin;
1693137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "PTScotch Graph Partitioner:\n");CHKERRQ(ierr);
1694137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1695137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using partitioning strategy %s\n",PTScotchStrategyList[p->strategy]);CHKERRQ(ierr);
1696137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPrintf(viewer, "using load imbalance ratio %g\n",(double)p->imbalance);CHKERRQ(ierr);
1697137cd93aSLisandro Dalcin   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1698137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1699137cd93aSLisandro Dalcin }
1700137cd93aSLisandro Dalcin 
1701137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerView_PTScotch(PetscPartitioner part, PetscViewer viewer)
1702137cd93aSLisandro Dalcin {
1703137cd93aSLisandro Dalcin   PetscBool      iascii;
1704137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1705137cd93aSLisandro Dalcin 
1706137cd93aSLisandro Dalcin   PetscFunctionBegin;
1707137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1708137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1709137cd93aSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1710137cd93aSLisandro Dalcin   if (iascii) {ierr = PetscPartitionerView_PTScotch_Ascii(part, viewer);CHKERRQ(ierr);}
1711137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1712137cd93aSLisandro Dalcin }
1713137cd93aSLisandro Dalcin 
1714137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerSetFromOptions_PTScotch(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
1715137cd93aSLisandro Dalcin {
1716137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p = (PetscPartitioner_PTScotch *) part->data;
1717137cd93aSLisandro Dalcin   const char *const         *slist = PTScotchStrategyList;
1718137cd93aSLisandro Dalcin   PetscInt                  nlist = (PetscInt)(sizeof(PTScotchStrategyList)/sizeof(PTScotchStrategyList[0]));
1719137cd93aSLisandro Dalcin   PetscBool                 flag;
1720137cd93aSLisandro Dalcin   PetscErrorCode            ierr;
1721137cd93aSLisandro Dalcin 
1722137cd93aSLisandro Dalcin   PetscFunctionBegin;
1723137cd93aSLisandro Dalcin   ierr = PetscOptionsHead(PetscOptionsObject, "PetscPartitioner PTScotch Options");CHKERRQ(ierr);
1724137cd93aSLisandro Dalcin   ierr = PetscOptionsEList("-petscpartitioner_ptscotch_strategy","Partitioning strategy","",slist,nlist,slist[p->strategy],&p->strategy,&flag);CHKERRQ(ierr);
1725137cd93aSLisandro Dalcin   ierr = PetscOptionsReal("-petscpartitioner_ptscotch_imbalance","Load imbalance ratio","",p->imbalance,&p->imbalance,&flag);CHKERRQ(ierr);
1726137cd93aSLisandro Dalcin   ierr = PetscOptionsTail();CHKERRQ(ierr);
1727137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1728137cd93aSLisandro Dalcin }
1729137cd93aSLisandro Dalcin 
1730137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerPartition_PTScotch(PetscPartitioner part, DM dm, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection partSection, IS *partition)
1731137cd93aSLisandro Dalcin {
1732137cd93aSLisandro Dalcin #if defined(PETSC_HAVE_PTSCOTCH)
1733137cd93aSLisandro Dalcin   MPI_Comm       comm       = PetscObjectComm((PetscObject)part);
1734137cd93aSLisandro Dalcin   PetscInt       nvtxs      = numVertices; /* The number of vertices in full graph */
1735137cd93aSLisandro Dalcin   PetscInt      *vtxdist;                  /* Distribution of vertices across processes */
1736137cd93aSLisandro Dalcin   PetscInt      *xadj       = start;       /* Start of edge list for each vertex */
1737137cd93aSLisandro Dalcin   PetscInt      *adjncy     = adjacency;   /* Edge lists for all vertices */
1738137cd93aSLisandro Dalcin   PetscInt      *vwgt       = NULL;        /* Vertex weights */
1739137cd93aSLisandro Dalcin   PetscInt      *adjwgt     = NULL;        /* Edge weights */
1740137cd93aSLisandro Dalcin   PetscInt       v, i, *assignment, *points;
1741137cd93aSLisandro Dalcin   PetscMPIInt    size, rank, p;
1742137cd93aSLisandro Dalcin   PetscErrorCode ierr;
1743137cd93aSLisandro Dalcin 
1744137cd93aSLisandro Dalcin   PetscFunctionBegin;
1745137cd93aSLisandro Dalcin   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1746137cd93aSLisandro Dalcin   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1747137cd93aSLisandro Dalcin   ierr = PetscMalloc2(nparts+1,&vtxdist,PetscMax(nvtxs,1),&assignment);CHKERRQ(ierr);
1748137cd93aSLisandro Dalcin 
1749137cd93aSLisandro Dalcin   /* Calculate vertex distribution */
1750137cd93aSLisandro Dalcin   vtxdist[0] = 0;
1751137cd93aSLisandro Dalcin   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
1752137cd93aSLisandro Dalcin   for (p = 2; p <= size; ++p) {
1753137cd93aSLisandro Dalcin     vtxdist[p] += vtxdist[p-1];
1754137cd93aSLisandro Dalcin   }
1755137cd93aSLisandro Dalcin 
1756137cd93aSLisandro Dalcin   if (nparts == 1) {
1757137cd93aSLisandro Dalcin     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));CHKERRQ(ierr);
1758137cd93aSLisandro Dalcin   } else {
1759137cd93aSLisandro Dalcin     PetscSection section;
1760137cd93aSLisandro Dalcin     /* Weight cells by dofs on cell by default */
1761137cd93aSLisandro Dalcin     ierr = PetscMalloc1(PetscMax(nvtxs,1),&vwgt);CHKERRQ(ierr);
1762e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1763137cd93aSLisandro Dalcin     if (section) {
1764137cd93aSLisandro Dalcin       PetscInt vStart, vEnd, dof;
1765137cd93aSLisandro Dalcin       ierr = DMPlexGetHeightStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1766137cd93aSLisandro Dalcin       for (v = vStart; v < vEnd; ++v) {
1767137cd93aSLisandro Dalcin         ierr = PetscSectionGetDof(section, v, &dof);CHKERRQ(ierr);
1768137cd93aSLisandro Dalcin         /* WARNING: Assumes that meshes with overlap have the overlapped cells at the end of the stratum. */
1769137cd93aSLisandro Dalcin         /* To do this properly, we should use the cell numbering created in DMPlexCreatePartitionerGraph. */
1770137cd93aSLisandro Dalcin         if (v-vStart < numVertices) vwgt[v-vStart] = PetscMax(dof, 1);
1771137cd93aSLisandro Dalcin       }
1772137cd93aSLisandro Dalcin     } else {
1773137cd93aSLisandro Dalcin       for (v = 0; v < nvtxs; ++v) vwgt[v] = 1;
1774137cd93aSLisandro Dalcin     }
1775137cd93aSLisandro Dalcin     {
1776137cd93aSLisandro Dalcin       PetscPartitioner_PTScotch *pts = (PetscPartitioner_PTScotch *) part->data;
1777137cd93aSLisandro Dalcin       int                       strat = PTScotch_Strategy(pts->strategy);
1778137cd93aSLisandro Dalcin       double                    imbal = (double)pts->imbalance;
1779137cd93aSLisandro Dalcin 
1780137cd93aSLisandro Dalcin       for (p = 0; !vtxdist[p+1] && p < size; ++p);
1781137cd93aSLisandro Dalcin       if (vtxdist[p+1] == vtxdist[size]) {
1782137cd93aSLisandro Dalcin         if (rank == p) {
1783137cd93aSLisandro Dalcin           ierr = PTScotch_PartGraph_Seq(strat, imbal, nvtxs, xadj, adjncy, vwgt, adjwgt, nparts, assignment);CHKERRQ(ierr);
1784137cd93aSLisandro Dalcin         }
1785137cd93aSLisandro Dalcin       } else {
1786137cd93aSLisandro Dalcin         ierr = PTScotch_PartGraph_MPI(strat, imbal, vtxdist, xadj, adjncy, vwgt, adjwgt, nparts, assignment, comm);CHKERRQ(ierr);
1787137cd93aSLisandro Dalcin       }
1788137cd93aSLisandro Dalcin     }
1789137cd93aSLisandro Dalcin     ierr = PetscFree(vwgt);CHKERRQ(ierr);
1790137cd93aSLisandro Dalcin   }
1791137cd93aSLisandro Dalcin 
1792137cd93aSLisandro Dalcin   /* Convert to PetscSection+IS */
1793137cd93aSLisandro Dalcin   ierr = PetscSectionSetChart(partSection, 0, nparts);CHKERRQ(ierr);
1794137cd93aSLisandro Dalcin   for (v = 0; v < nvtxs; ++v) {ierr = PetscSectionAddDof(partSection, assignment[v], 1);CHKERRQ(ierr);}
1795137cd93aSLisandro Dalcin   ierr = PetscSectionSetUp(partSection);CHKERRQ(ierr);
1796137cd93aSLisandro Dalcin   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
1797137cd93aSLisandro Dalcin   for (p = 0, i = 0; p < nparts; ++p) {
1798137cd93aSLisandro Dalcin     for (v = 0; v < nvtxs; ++v) {
1799137cd93aSLisandro Dalcin       if (assignment[v] == p) points[i++] = v;
1800137cd93aSLisandro Dalcin     }
1801137cd93aSLisandro Dalcin   }
1802137cd93aSLisandro Dalcin   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
1803137cd93aSLisandro Dalcin   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
1804137cd93aSLisandro Dalcin 
1805137cd93aSLisandro Dalcin   ierr = PetscFree2(vtxdist,assignment);CHKERRQ(ierr);
1806137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1807137cd93aSLisandro Dalcin #else
1808137cd93aSLisandro Dalcin   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-ptscotch.");
1809137cd93aSLisandro Dalcin #endif
1810137cd93aSLisandro Dalcin }
1811137cd93aSLisandro Dalcin 
1812137cd93aSLisandro Dalcin static PetscErrorCode PetscPartitionerInitialize_PTScotch(PetscPartitioner part)
1813137cd93aSLisandro Dalcin {
1814137cd93aSLisandro Dalcin   PetscFunctionBegin;
1815137cd93aSLisandro Dalcin   part->ops->view           = PetscPartitionerView_PTScotch;
1816137cd93aSLisandro Dalcin   part->ops->destroy        = PetscPartitionerDestroy_PTScotch;
1817137cd93aSLisandro Dalcin   part->ops->partition      = PetscPartitionerPartition_PTScotch;
1818137cd93aSLisandro Dalcin   part->ops->setfromoptions = PetscPartitionerSetFromOptions_PTScotch;
1819137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1820137cd93aSLisandro Dalcin }
1821137cd93aSLisandro Dalcin 
1822137cd93aSLisandro Dalcin /*MC
1823137cd93aSLisandro Dalcin   PETSCPARTITIONERPTSCOTCH = "ptscotch" - A PetscPartitioner object using the PT-Scotch library
1824137cd93aSLisandro Dalcin 
1825137cd93aSLisandro Dalcin   Level: intermediate
1826137cd93aSLisandro Dalcin 
1827137cd93aSLisandro Dalcin .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
1828137cd93aSLisandro Dalcin M*/
1829137cd93aSLisandro Dalcin 
1830137cd93aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_PTScotch(PetscPartitioner part)
1831137cd93aSLisandro Dalcin {
1832137cd93aSLisandro Dalcin   PetscPartitioner_PTScotch *p;
1833137cd93aSLisandro Dalcin   PetscErrorCode          ierr;
1834137cd93aSLisandro Dalcin 
1835137cd93aSLisandro Dalcin   PetscFunctionBegin;
1836137cd93aSLisandro Dalcin   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
1837137cd93aSLisandro Dalcin   ierr = PetscNewLog(part, &p);CHKERRQ(ierr);
1838137cd93aSLisandro Dalcin   part->data = p;
1839137cd93aSLisandro Dalcin 
1840137cd93aSLisandro Dalcin   p->strategy  = 0;
1841137cd93aSLisandro Dalcin   p->imbalance = 0.01;
1842137cd93aSLisandro Dalcin 
1843137cd93aSLisandro Dalcin   ierr = PetscPartitionerInitialize_PTScotch(part);CHKERRQ(ierr);
1844137cd93aSLisandro Dalcin   ierr = PetscCitationsRegister(PTScotchPartitionerCitation, &PTScotchPartitionercite);CHKERRQ(ierr);
1845137cd93aSLisandro Dalcin   PetscFunctionReturn(0);
1846137cd93aSLisandro Dalcin }
1847137cd93aSLisandro Dalcin 
1848137cd93aSLisandro Dalcin 
18495680f57bSMatthew G. Knepley /*@
18505680f57bSMatthew G. Knepley   DMPlexGetPartitioner - Get the mesh partitioner
18515680f57bSMatthew G. Knepley 
18525680f57bSMatthew G. Knepley   Not collective
18535680f57bSMatthew G. Knepley 
18545680f57bSMatthew G. Knepley   Input Parameter:
18555680f57bSMatthew G. Knepley . dm - The DM
18565680f57bSMatthew G. Knepley 
18575680f57bSMatthew G. Knepley   Output Parameter:
18585680f57bSMatthew G. Knepley . part - The PetscPartitioner
18595680f57bSMatthew G. Knepley 
18605680f57bSMatthew G. Knepley   Level: developer
18615680f57bSMatthew G. Knepley 
186298599a47SLawrence Mitchell   Note: This gets a borrowed reference, so the user should not destroy this PetscPartitioner.
186398599a47SLawrence Mitchell 
186498599a47SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexSetPartitioner(), PetscPartitionerCreate()
18655680f57bSMatthew G. Knepley @*/
18665680f57bSMatthew G. Knepley PetscErrorCode DMPlexGetPartitioner(DM dm, PetscPartitioner *part)
18675680f57bSMatthew G. Knepley {
18685680f57bSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
18695680f57bSMatthew G. Knepley 
18705680f57bSMatthew G. Knepley   PetscFunctionBegin;
18715680f57bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18725680f57bSMatthew G. Knepley   PetscValidPointer(part, 2);
18735680f57bSMatthew G. Knepley   *part = mesh->partitioner;
18745680f57bSMatthew G. Knepley   PetscFunctionReturn(0);
18755680f57bSMatthew G. Knepley }
18765680f57bSMatthew G. Knepley 
187771bb2955SLawrence Mitchell /*@
187871bb2955SLawrence Mitchell   DMPlexSetPartitioner - Set the mesh partitioner
187971bb2955SLawrence Mitchell 
188071bb2955SLawrence Mitchell   logically collective on dm and part
188171bb2955SLawrence Mitchell 
188271bb2955SLawrence Mitchell   Input Parameters:
188371bb2955SLawrence Mitchell + dm - The DM
188471bb2955SLawrence Mitchell - part - The partitioner
188571bb2955SLawrence Mitchell 
188671bb2955SLawrence Mitchell   Level: developer
188771bb2955SLawrence Mitchell 
188871bb2955SLawrence Mitchell   Note: Any existing PetscPartitioner will be destroyed.
188971bb2955SLawrence Mitchell 
189071bb2955SLawrence Mitchell .seealso DMPlexDistribute(), DMPlexGetPartitioner(), PetscPartitionerCreate()
189171bb2955SLawrence Mitchell @*/
189271bb2955SLawrence Mitchell PetscErrorCode DMPlexSetPartitioner(DM dm, PetscPartitioner part)
189371bb2955SLawrence Mitchell {
189471bb2955SLawrence Mitchell   DM_Plex       *mesh = (DM_Plex *) dm->data;
189571bb2955SLawrence Mitchell   PetscErrorCode ierr;
189671bb2955SLawrence Mitchell 
189771bb2955SLawrence Mitchell   PetscFunctionBegin;
189871bb2955SLawrence Mitchell   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
189971bb2955SLawrence Mitchell   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 2);
190071bb2955SLawrence Mitchell   ierr = PetscObjectReference((PetscObject)part);CHKERRQ(ierr);
190171bb2955SLawrence Mitchell   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
190271bb2955SLawrence Mitchell   mesh->partitioner = part;
190371bb2955SLawrence Mitchell   PetscFunctionReturn(0);
190471bb2955SLawrence Mitchell }
190571bb2955SLawrence Mitchell 
1906e8f14785SLisandro Dalcin static PetscErrorCode DMPlexAddClosure_Tree(DM dm, PetscHSetI ht, PetscInt point, PetscBool up, PetscBool down)
1907270bba0cSToby Isaac {
1908270bba0cSToby Isaac   PetscErrorCode ierr;
1909270bba0cSToby Isaac 
1910270bba0cSToby Isaac   PetscFunctionBegin;
19116a5a2ffdSToby Isaac   if (up) {
19126a5a2ffdSToby Isaac     PetscInt parent;
19136a5a2ffdSToby Isaac 
1914270bba0cSToby Isaac     ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr);
19156a5a2ffdSToby Isaac     if (parent != point) {
19166a5a2ffdSToby Isaac       PetscInt closureSize, *closure = NULL, i;
19176a5a2ffdSToby Isaac 
1918270bba0cSToby Isaac       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1919270bba0cSToby Isaac       for (i = 0; i < closureSize; i++) {
1920270bba0cSToby Isaac         PetscInt cpoint = closure[2*i];
1921270bba0cSToby Isaac 
1922e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
19231b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr);
1924270bba0cSToby Isaac       }
1925270bba0cSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
19266a5a2ffdSToby Isaac     }
19276a5a2ffdSToby Isaac   }
19286a5a2ffdSToby Isaac   if (down) {
19296a5a2ffdSToby Isaac     PetscInt numChildren;
19306a5a2ffdSToby Isaac     const PetscInt *children;
19316a5a2ffdSToby Isaac 
19326a5a2ffdSToby Isaac     ierr = DMPlexGetTreeChildren(dm,point,&numChildren,&children);CHKERRQ(ierr);
19336a5a2ffdSToby Isaac     if (numChildren) {
19346a5a2ffdSToby Isaac       PetscInt i;
19356a5a2ffdSToby Isaac 
19366a5a2ffdSToby Isaac       for (i = 0; i < numChildren; i++) {
19376a5a2ffdSToby Isaac         PetscInt cpoint = children[i];
19386a5a2ffdSToby Isaac 
1939e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, cpoint);CHKERRQ(ierr);
19401b807c88SLisandro Dalcin         ierr = DMPlexAddClosure_Tree(dm,ht,cpoint,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr);
19416a5a2ffdSToby Isaac       }
19426a5a2ffdSToby Isaac     }
19436a5a2ffdSToby Isaac   }
1944270bba0cSToby Isaac   PetscFunctionReturn(0);
1945270bba0cSToby Isaac }
1946270bba0cSToby Isaac 
19475abbe4feSMichael Lange /*@
19485abbe4feSMichael Lange   DMPlexPartitionLabelClosure - Add the closure of all points to the partition label
19495abbe4feSMichael Lange 
19505abbe4feSMichael Lange   Input Parameters:
19515abbe4feSMichael Lange + dm     - The DM
19525abbe4feSMichael Lange - label  - DMLabel assinging ranks to remote roots
19535abbe4feSMichael Lange 
19545abbe4feSMichael Lange   Level: developer
19555abbe4feSMichael Lange 
19565abbe4feSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
19575abbe4feSMichael Lange @*/
19585abbe4feSMichael Lange PetscErrorCode DMPlexPartitionLabelClosure(DM dm, DMLabel label)
19599ffc88e4SToby Isaac {
19605abbe4feSMichael Lange   IS              rankIS,   pointIS;
19615abbe4feSMichael Lange   const PetscInt *ranks,   *points;
19625abbe4feSMichael Lange   PetscInt        numRanks, numPoints, r, p, c, closureSize;
19635abbe4feSMichael Lange   PetscInt       *closure = NULL;
19641b807c88SLisandro Dalcin   DM_Plex        *mesh    = (DM_Plex *)dm->data;
19651b807c88SLisandro Dalcin   PetscBool       hasTree = (mesh->parentSection || mesh->childSection) ? PETSC_TRUE : PETSC_FALSE;
19669ffc88e4SToby Isaac   PetscErrorCode  ierr;
19679ffc88e4SToby Isaac 
19689ffc88e4SToby Isaac   PetscFunctionBegin;
19695abbe4feSMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
19705abbe4feSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
19715abbe4feSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
19721b807c88SLisandro Dalcin 
19735abbe4feSMichael Lange   for (r = 0; r < numRanks; ++r) {
19745abbe4feSMichael Lange     const PetscInt rank = ranks[r];
1975e8f14785SLisandro Dalcin     PetscHSetI     ht;
1976e8f14785SLisandro Dalcin     PetscInt       nelems, *elems, off = 0;
19779ffc88e4SToby Isaac 
19785abbe4feSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
19795abbe4feSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
19805abbe4feSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1981e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
1982e8f14785SLisandro Dalcin     ierr = PetscHSetIResize(ht, numPoints*16);CHKERRQ(ierr);
19835abbe4feSMichael Lange     for (p = 0; p < numPoints; ++p) {
19845abbe4feSMichael Lange       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1985270bba0cSToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
1986e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(ht, closure[c]);CHKERRQ(ierr);
19871b807c88SLisandro Dalcin         if (hasTree) {ierr = DMPlexAddClosure_Tree(dm, ht, closure[c], PETSC_TRUE, PETSC_TRUE);CHKERRQ(ierr);}
1988270bba0cSToby Isaac       }
19899ffc88e4SToby Isaac     }
19905abbe4feSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
19915abbe4feSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1992e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(ht, &nelems);CHKERRQ(ierr);
1993e8f14785SLisandro Dalcin     ierr = PetscMalloc1(nelems, &elems);CHKERRQ(ierr);
1994*f5a7d1c1SBarry Smith     ierr = PetscHSetIGetElems(ht, &off, elems);CHKERRQ(ierr);
1995e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
1996e8f14785SLisandro Dalcin     ierr = PetscSortInt(nelems, elems);CHKERRQ(ierr);
1997e8f14785SLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF, nelems, elems, PETSC_OWN_POINTER, &pointIS);CHKERRQ(ierr);
19981b807c88SLisandro Dalcin     ierr = DMLabelSetStratumIS(label, rank, pointIS);CHKERRQ(ierr);
19991b807c88SLisandro Dalcin     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
20009ffc88e4SToby Isaac   }
20011b807c88SLisandro Dalcin 
20021b807c88SLisandro Dalcin   if (closure) {ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);}
20035abbe4feSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
20045abbe4feSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
20059ffc88e4SToby Isaac   PetscFunctionReturn(0);
20069ffc88e4SToby Isaac }
20079ffc88e4SToby Isaac 
200824d039d7SMichael Lange /*@
200924d039d7SMichael Lange   DMPlexPartitionLabelAdjacency - Add one level of adjacent points to the partition label
201024d039d7SMichael Lange 
201124d039d7SMichael Lange   Input Parameters:
201224d039d7SMichael Lange + dm     - The DM
201324d039d7SMichael Lange - label  - DMLabel assinging ranks to remote roots
201424d039d7SMichael Lange 
201524d039d7SMichael Lange   Level: developer
201624d039d7SMichael Lange 
201724d039d7SMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
201824d039d7SMichael Lange @*/
201924d039d7SMichael Lange PetscErrorCode DMPlexPartitionLabelAdjacency(DM dm, DMLabel label)
202070034214SMatthew G. Knepley {
202124d039d7SMichael Lange   IS              rankIS,   pointIS;
202224d039d7SMichael Lange   const PetscInt *ranks,   *points;
202324d039d7SMichael Lange   PetscInt        numRanks, numPoints, r, p, a, adjSize;
202424d039d7SMichael Lange   PetscInt       *adj = NULL;
202570034214SMatthew G. Knepley   PetscErrorCode  ierr;
202670034214SMatthew G. Knepley 
202770034214SMatthew G. Knepley   PetscFunctionBegin;
202824d039d7SMichael Lange   ierr = DMLabelGetValueIS(label, &rankIS);CHKERRQ(ierr);
202924d039d7SMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
203024d039d7SMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
203124d039d7SMichael Lange   for (r = 0; r < numRanks; ++r) {
203224d039d7SMichael Lange     const PetscInt rank = ranks[r];
203370034214SMatthew G. Knepley 
203424d039d7SMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &pointIS);CHKERRQ(ierr);
203524d039d7SMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
203624d039d7SMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
203770034214SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
203824d039d7SMichael Lange       adjSize = PETSC_DETERMINE;
203924d039d7SMichael Lange       ierr = DMPlexGetAdjacency(dm, points[p], &adjSize, &adj);CHKERRQ(ierr);
204024d039d7SMichael Lange       for (a = 0; a < adjSize; ++a) {ierr = DMLabelSetValue(label, adj[a], rank);CHKERRQ(ierr);}
204170034214SMatthew G. Knepley     }
204224d039d7SMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
204324d039d7SMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
204470034214SMatthew G. Knepley   }
204524d039d7SMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
204624d039d7SMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
204724d039d7SMichael Lange   ierr = PetscFree(adj);CHKERRQ(ierr);
204824d039d7SMichael Lange   PetscFunctionReturn(0);
204970034214SMatthew G. Knepley }
205070034214SMatthew G. Knepley 
2051be200f8dSMichael Lange /*@
2052be200f8dSMichael Lange   DMPlexPartitionLabelPropagate - Propagate points in a partition label over the point SF
2053be200f8dSMichael Lange 
2054be200f8dSMichael Lange   Input Parameters:
2055be200f8dSMichael Lange + dm     - The DM
2056be200f8dSMichael Lange - label  - DMLabel assinging ranks to remote roots
2057be200f8dSMichael Lange 
2058be200f8dSMichael Lange   Level: developer
2059be200f8dSMichael Lange 
2060be200f8dSMichael Lange   Note: This is required when generating multi-level overlaps to capture
2061be200f8dSMichael Lange   overlap points from non-neighbouring partitions.
2062be200f8dSMichael Lange 
2063be200f8dSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
2064be200f8dSMichael Lange @*/
2065be200f8dSMichael Lange PetscErrorCode DMPlexPartitionLabelPropagate(DM dm, DMLabel label)
2066be200f8dSMichael Lange {
2067be200f8dSMichael Lange   MPI_Comm        comm;
2068be200f8dSMichael Lange   PetscMPIInt     rank;
2069be200f8dSMichael Lange   PetscSF         sfPoint;
20705d04f6ebSMichael Lange   DMLabel         lblRoots, lblLeaves;
2071be200f8dSMichael Lange   IS              rankIS, pointIS;
2072be200f8dSMichael Lange   const PetscInt *ranks;
2073be200f8dSMichael Lange   PetscInt        numRanks, r;
2074be200f8dSMichael Lange   PetscErrorCode  ierr;
2075be200f8dSMichael Lange 
2076be200f8dSMichael Lange   PetscFunctionBegin;
2077be200f8dSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
2078be200f8dSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2079be200f8dSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
20805d04f6ebSMichael Lange   /* Pull point contributions from remote leaves into local roots */
20815d04f6ebSMichael Lange   ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
20825d04f6ebSMichael Lange   ierr = DMLabelGetValueIS(lblLeaves, &rankIS);CHKERRQ(ierr);
20835d04f6ebSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
20845d04f6ebSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
20855d04f6ebSMichael Lange   for (r = 0; r < numRanks; ++r) {
20865d04f6ebSMichael Lange     const PetscInt remoteRank = ranks[r];
20875d04f6ebSMichael Lange     if (remoteRank == rank) continue;
20885d04f6ebSMichael Lange     ierr = DMLabelGetStratumIS(lblLeaves, remoteRank, &pointIS);CHKERRQ(ierr);
20895d04f6ebSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
20905d04f6ebSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
20915d04f6ebSMichael Lange   }
20925d04f6ebSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
20935d04f6ebSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
20945d04f6ebSMichael Lange   ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
2095be200f8dSMichael Lange   /* Push point contributions from roots into remote leaves */
2096be200f8dSMichael Lange   ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
2097be200f8dSMichael Lange   ierr = DMLabelGetValueIS(lblRoots, &rankIS);CHKERRQ(ierr);
2098be200f8dSMichael Lange   ierr = ISGetLocalSize(rankIS, &numRanks);CHKERRQ(ierr);
2099be200f8dSMichael Lange   ierr = ISGetIndices(rankIS, &ranks);CHKERRQ(ierr);
2100be200f8dSMichael Lange   for (r = 0; r < numRanks; ++r) {
2101be200f8dSMichael Lange     const PetscInt remoteRank = ranks[r];
2102be200f8dSMichael Lange     if (remoteRank == rank) continue;
2103be200f8dSMichael Lange     ierr = DMLabelGetStratumIS(lblRoots, remoteRank, &pointIS);CHKERRQ(ierr);
2104be200f8dSMichael Lange     ierr = DMLabelInsertIS(label, pointIS, remoteRank);CHKERRQ(ierr);
2105be200f8dSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2106be200f8dSMichael Lange   }
2107be200f8dSMichael Lange   ierr = ISRestoreIndices(rankIS, &ranks);CHKERRQ(ierr);
2108be200f8dSMichael Lange   ierr = ISDestroy(&rankIS);CHKERRQ(ierr);
2109be200f8dSMichael Lange   ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
2110be200f8dSMichael Lange   PetscFunctionReturn(0);
2111be200f8dSMichael Lange }
2112be200f8dSMichael Lange 
21131fd9873aSMichael Lange /*@
21141fd9873aSMichael Lange   DMPlexPartitionLabelInvert - Create a partition label of remote roots from a local root label
21151fd9873aSMichael Lange 
21161fd9873aSMichael Lange   Input Parameters:
21171fd9873aSMichael Lange + dm        - The DM
21181fd9873aSMichael Lange . rootLabel - DMLabel assinging ranks to local roots
21191fd9873aSMichael Lange . processSF - A star forest mapping into the local index on each remote rank
21201fd9873aSMichael Lange 
21211fd9873aSMichael Lange   Output Parameter:
21221fd9873aSMichael Lange - leafLabel - DMLabel assinging ranks to remote roots
21231fd9873aSMichael Lange 
21241fd9873aSMichael Lange   Note: The rootLabel defines a send pattern by mapping local points to remote target ranks. The
21251fd9873aSMichael Lange   resulting leafLabel is a receiver mapping of remote roots to their parent rank.
21261fd9873aSMichael Lange 
21271fd9873aSMichael Lange   Level: developer
21281fd9873aSMichael Lange 
21291fd9873aSMichael Lange .seealso: DMPlexPartitionLabelCreateSF, DMPlexDistribute(), DMPlexCreateOverlap
21301fd9873aSMichael Lange @*/
21311fd9873aSMichael Lange PetscErrorCode DMPlexPartitionLabelInvert(DM dm, DMLabel rootLabel, PetscSF processSF, DMLabel leafLabel)
21321fd9873aSMichael Lange {
21331fd9873aSMichael Lange   MPI_Comm           comm;
21349852e123SBarry Smith   PetscMPIInt        rank, size;
21359852e123SBarry Smith   PetscInt           p, n, numNeighbors, ssize, l, nleaves;
21361fd9873aSMichael Lange   PetscSF            sfPoint;
21371fd9873aSMichael Lange   PetscSFNode       *rootPoints, *leafPoints;
21381fd9873aSMichael Lange   PetscSection       rootSection, leafSection;
21391fd9873aSMichael Lange   const PetscSFNode *remote;
21401fd9873aSMichael Lange   const PetscInt    *local, *neighbors;
21411fd9873aSMichael Lange   IS                 valueIS;
21421fd9873aSMichael Lange   PetscErrorCode     ierr;
21431fd9873aSMichael Lange 
21441fd9873aSMichael Lange   PetscFunctionBegin;
21451fd9873aSMichael Lange   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
21461fd9873aSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
21479852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
21481fd9873aSMichael Lange   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
21491fd9873aSMichael Lange 
21501fd9873aSMichael Lange   /* Convert to (point, rank) and use actual owners */
21511fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
21529852e123SBarry Smith   ierr = PetscSectionSetChart(rootSection, 0, size);CHKERRQ(ierr);
21531fd9873aSMichael Lange   ierr = DMLabelGetValueIS(rootLabel, &valueIS);CHKERRQ(ierr);
21541fd9873aSMichael Lange   ierr = ISGetLocalSize(valueIS, &numNeighbors);CHKERRQ(ierr);
21551fd9873aSMichael Lange   ierr = ISGetIndices(valueIS, &neighbors);CHKERRQ(ierr);
21561fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
21571fd9873aSMichael Lange     PetscInt numPoints;
21581fd9873aSMichael Lange 
21591fd9873aSMichael Lange     ierr = DMLabelGetStratumSize(rootLabel, neighbors[n], &numPoints);CHKERRQ(ierr);
21601fd9873aSMichael Lange     ierr = PetscSectionAddDof(rootSection, neighbors[n], numPoints);CHKERRQ(ierr);
21611fd9873aSMichael Lange   }
21621fd9873aSMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
21639852e123SBarry Smith   ierr = PetscSectionGetStorageSize(rootSection, &ssize);CHKERRQ(ierr);
21649852e123SBarry Smith   ierr = PetscMalloc1(ssize, &rootPoints);CHKERRQ(ierr);
21651fd9873aSMichael Lange   ierr = PetscSFGetGraph(sfPoint, NULL, &nleaves, &local, &remote);CHKERRQ(ierr);
21661fd9873aSMichael Lange   for (n = 0; n < numNeighbors; ++n) {
21671fd9873aSMichael Lange     IS              pointIS;
21681fd9873aSMichael Lange     const PetscInt *points;
21691fd9873aSMichael Lange     PetscInt        off, numPoints, p;
21701fd9873aSMichael Lange 
21711fd9873aSMichael Lange     ierr = PetscSectionGetOffset(rootSection, neighbors[n], &off);CHKERRQ(ierr);
21721fd9873aSMichael Lange     ierr = DMLabelGetStratumIS(rootLabel, neighbors[n], &pointIS);CHKERRQ(ierr);
21731fd9873aSMichael Lange     ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21741fd9873aSMichael Lange     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
21751fd9873aSMichael Lange     for (p = 0; p < numPoints; ++p) {
2176f8987ae8SMichael Lange       if (local) {ierr = PetscFindInt(points[p], nleaves, local, &l);CHKERRQ(ierr);}
2177f8987ae8SMichael Lange       else       {l = -1;}
21781fd9873aSMichael Lange       if (l >= 0) {rootPoints[off+p] = remote[l];}
21791fd9873aSMichael Lange       else        {rootPoints[off+p].index = points[p]; rootPoints[off+p].rank = rank;}
21801fd9873aSMichael Lange     }
21811fd9873aSMichael Lange     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
21821fd9873aSMichael Lange     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21831fd9873aSMichael Lange   }
21841fd9873aSMichael Lange   ierr = ISRestoreIndices(valueIS, &neighbors);CHKERRQ(ierr);
21851fd9873aSMichael Lange   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
21861fd9873aSMichael Lange   /* Communicate overlap */
21871fd9873aSMichael Lange   ierr = PetscSectionCreate(comm, &leafSection);CHKERRQ(ierr);
21881fd9873aSMichael Lange   ierr = DMPlexDistributeData(dm, processSF, rootSection, MPIU_2INT, rootPoints, leafSection, (void**) &leafPoints);CHKERRQ(ierr);
21891fd9873aSMichael Lange   /* Filter remote contributions (ovLeafPoints) into the overlapSF */
21909852e123SBarry Smith   ierr = PetscSectionGetStorageSize(leafSection, &ssize);CHKERRQ(ierr);
21919852e123SBarry Smith   for (p = 0; p < ssize; p++) {
21921fd9873aSMichael Lange     ierr = DMLabelSetValue(leafLabel, leafPoints[p].index, leafPoints[p].rank);CHKERRQ(ierr);
21931fd9873aSMichael Lange   }
21941fd9873aSMichael Lange   ierr = PetscFree(rootPoints);CHKERRQ(ierr);
21951fd9873aSMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
21961fd9873aSMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
21971fd9873aSMichael Lange   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
21981fd9873aSMichael Lange   PetscFunctionReturn(0);
21991fd9873aSMichael Lange }
22001fd9873aSMichael Lange 
2201aa3148a8SMichael Lange /*@
2202aa3148a8SMichael Lange   DMPlexPartitionLabelCreateSF - Create a star forest from a label that assigns ranks to points
2203aa3148a8SMichael Lange 
2204aa3148a8SMichael Lange   Input Parameters:
2205aa3148a8SMichael Lange + dm    - The DM
2206aa3148a8SMichael Lange . label - DMLabel assinging ranks to remote roots
2207aa3148a8SMichael Lange 
2208aa3148a8SMichael Lange   Output Parameter:
2209aa3148a8SMichael Lange - sf    - The star forest communication context encapsulating the defined mapping
2210aa3148a8SMichael Lange 
2211aa3148a8SMichael Lange   Note: The incoming label is a receiver mapping of remote points to their parent rank.
2212aa3148a8SMichael Lange 
2213aa3148a8SMichael Lange   Level: developer
2214aa3148a8SMichael Lange 
2215aa3148a8SMichael Lange .seealso: DMPlexDistribute(), DMPlexCreateOverlap
2216aa3148a8SMichael Lange @*/
2217aa3148a8SMichael Lange PetscErrorCode DMPlexPartitionLabelCreateSF(DM dm, DMLabel label, PetscSF *sf)
2218aa3148a8SMichael Lange {
22199852e123SBarry Smith   PetscMPIInt     rank, size;
222043f7d02bSMichael Lange   PetscInt        n, numRemote, p, numPoints, pStart, pEnd, idx = 0;
2221aa3148a8SMichael Lange   PetscSFNode    *remotePoints;
222243f7d02bSMichael Lange   IS              remoteRootIS;
222343f7d02bSMichael Lange   const PetscInt *remoteRoots;
2224aa3148a8SMichael Lange   PetscErrorCode ierr;
2225aa3148a8SMichael Lange 
2226aa3148a8SMichael Lange   PetscFunctionBegin;
222743f7d02bSMichael Lange   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
22289852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
2229aa3148a8SMichael Lange 
22309852e123SBarry Smith   for (numRemote = 0, n = 0; n < size; ++n) {
2231aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
2232aa3148a8SMichael Lange     numRemote += numPoints;
2233aa3148a8SMichael Lange   }
2234aa3148a8SMichael Lange   ierr = PetscMalloc1(numRemote, &remotePoints);CHKERRQ(ierr);
223543f7d02bSMichael Lange   /* Put owned points first */
223643f7d02bSMichael Lange   ierr = DMLabelGetStratumSize(label, rank, &numPoints);CHKERRQ(ierr);
223743f7d02bSMichael Lange   if (numPoints > 0) {
223843f7d02bSMichael Lange     ierr = DMLabelGetStratumIS(label, rank, &remoteRootIS);CHKERRQ(ierr);
223943f7d02bSMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
224043f7d02bSMichael Lange     for (p = 0; p < numPoints; p++) {
224143f7d02bSMichael Lange       remotePoints[idx].index = remoteRoots[p];
224243f7d02bSMichael Lange       remotePoints[idx].rank = rank;
224343f7d02bSMichael Lange       idx++;
224443f7d02bSMichael Lange     }
224543f7d02bSMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
224643f7d02bSMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
224743f7d02bSMichael Lange   }
224843f7d02bSMichael Lange   /* Now add remote points */
22499852e123SBarry Smith   for (n = 0; n < size; ++n) {
2250aa3148a8SMichael Lange     ierr = DMLabelGetStratumSize(label, n, &numPoints);CHKERRQ(ierr);
225143f7d02bSMichael Lange     if (numPoints <= 0 || n == rank) continue;
2252aa3148a8SMichael Lange     ierr = DMLabelGetStratumIS(label, n, &remoteRootIS);CHKERRQ(ierr);
2253aa3148a8SMichael Lange     ierr = ISGetIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2254aa3148a8SMichael Lange     for (p = 0; p < numPoints; p++) {
2255aa3148a8SMichael Lange       remotePoints[idx].index = remoteRoots[p];
2256aa3148a8SMichael Lange       remotePoints[idx].rank = n;
2257aa3148a8SMichael Lange       idx++;
2258aa3148a8SMichael Lange     }
2259aa3148a8SMichael Lange     ierr = ISRestoreIndices(remoteRootIS, &remoteRoots);CHKERRQ(ierr);
2260aa3148a8SMichael Lange     ierr = ISDestroy(&remoteRootIS);CHKERRQ(ierr);
2261aa3148a8SMichael Lange   }
2262aa3148a8SMichael Lange   ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), sf);CHKERRQ(ierr);
2263aa3148a8SMichael Lange   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2264aa3148a8SMichael Lange   ierr = PetscSFSetGraph(*sf, pEnd-pStart, numRemote, NULL, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
226570034214SMatthew G. Knepley   PetscFunctionReturn(0);
226670034214SMatthew G. Knepley }
2267