xref: /petsc/src/dm/partitioner/impls/chaco/partchaco.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
1 #include <petsc/private/partitionerimpl.h>        /*I "petscpartitioner.h" I*/
2 
3 PetscBool  ChacoPartitionerCite = PETSC_FALSE;
4 const char ChacoPartitionerCitation[] =
5   "@inproceedings{Chaco95,\n"
6   "  author    = {Bruce Hendrickson and Robert Leland},\n"
7   "  title     = {A multilevel algorithm for partitioning graphs},\n"
8   "  booktitle = {Supercomputing '95: Proceedings of the 1995 ACM/IEEE Conference on Supercomputing (CDROM)},"
9   "  isbn      = {0-89791-816-9},\n"
10   "  pages     = {28},\n"
11   "  doi       = {https://doi.acm.org/10.1145/224170.224228},\n"
12   "  publisher = {ACM Press},\n"
13   "  address   = {New York},\n"
14   "  year      = {1995}\n"
15   "}\n";
16 
17 typedef struct {
18   PetscInt dummy;
19 } PetscPartitioner_Chaco;
20 
21 static PetscErrorCode PetscPartitionerDestroy_Chaco(PetscPartitioner part)
22 {
23   PetscPartitioner_Chaco *p = (PetscPartitioner_Chaco *) part->data;
24 
25   PetscFunctionBegin;
26   PetscCall(PetscFree(p));
27   PetscFunctionReturn(0);
28 }
29 
30 static PetscErrorCode PetscPartitionerView_Chaco_ASCII(PetscPartitioner part, PetscViewer viewer)
31 {
32   PetscFunctionBegin;
33   PetscFunctionReturn(0);
34 }
35 
36 static PetscErrorCode PetscPartitionerView_Chaco(PetscPartitioner part, PetscViewer viewer)
37 {
38   PetscBool      iascii;
39 
40   PetscFunctionBegin;
41   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
42   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
43   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii));
44   if (iascii) PetscCall(PetscPartitionerView_Chaco_ASCII(part, viewer));
45   PetscFunctionReturn(0);
46 }
47 
48 #if defined(PETSC_HAVE_CHACO)
49 #if defined(PETSC_HAVE_UNISTD_H)
50 #include <unistd.h>
51 #endif
52 #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
53 #include <chaco.h>
54 #else
55 /* Older versions of Chaco do not have an include file */
56 PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
57                        float *ewgts, float *x, float *y, float *z, char *outassignname,
58                        char *outfilename, short *assignment, int architecture, int ndims_tot,
59                        int mesh_dims[3], double *goal, int global_method, int local_method,
60                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
61 #endif
62 extern int FREE_GRAPH;
63 #endif
64 
65 static PetscErrorCode PetscPartitionerPartition_Chaco(PetscPartitioner part, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection vertSection, PetscSection targetSection, PetscSection partSection, IS *partition)
66 {
67 #if defined(PETSC_HAVE_CHACO)
68   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
69   MPI_Comm       comm;
70   int            nvtxs          = numVertices; /* number of vertices in full graph */
71   int           *vwgts          = NULL;   /* weights for all vertices */
72   float         *ewgts          = NULL;   /* weights for all edges */
73   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
74   char          *outassignname  = NULL;   /*  name of assignment output file */
75   char          *outfilename    = NULL;   /* output file name */
76   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
77   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
78   int            mesh_dims[3];            /* dimensions of mesh of processors */
79   double        *goal          = NULL;    /* desired set sizes for each set */
80   int            global_method = 1;       /* global partitioning algorithm */
81   int            local_method  = 1;       /* local partitioning algorithm */
82   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
83   int            vmax          = 200;     /* how many vertices to coarsen down to? */
84   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
85   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
86   long           seed          = 123636512; /* for random graph mutations */
87 #if defined(PETSC_HAVE_CHACO_INT_ASSIGNMENT)
88   int           *assignment;              /* Output partition */
89 #else
90   short int     *assignment;              /* Output partition */
91 #endif
92   int            fd_stdout, fd_pipe[2];
93   PetscInt      *points;
94   int            i, v, p;
95   PetscErrorCode ierr;
96 
97   PetscFunctionBegin;
98   PetscCall(PetscObjectGetComm((PetscObject)part,&comm));
99   if (PetscDefined (USE_DEBUG)) {
100     int ival,isum;
101     PetscBool distributed;
102 
103     ival = (numVertices > 0);
104     PetscCallMPI(MPI_Allreduce(&ival, &isum, 1, MPI_INT, MPI_SUM, comm));
105     distributed = (isum > 1) ? PETSC_TRUE : PETSC_FALSE;
106     PetscCheck(!distributed,comm, PETSC_ERR_SUP, "Chaco cannot partition a distributed graph");
107   }
108   if (!numVertices) { /* distributed case, return if not holding the graph */
109     PetscCall(ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition));
110     PetscFunctionReturn(0);
111   }
112   FREE_GRAPH = 0; /* Do not let Chaco free my memory */
113   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
114 
115   if (global_method == INERTIAL_METHOD) {
116     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
117     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
118   }
119   mesh_dims[0] = nparts;
120   mesh_dims[1] = 1;
121   mesh_dims[2] = 1;
122   PetscCall(PetscMalloc1(nvtxs, &assignment));
123   /* Chaco outputs to stdout. We redirect this to a buffer. */
124   /* TODO: check error codes for UNIX calls */
125 #if defined(PETSC_HAVE_UNISTD_H)
126   {
127     int piperet;
128     piperet = pipe(fd_pipe);
129     PetscCheck(!piperet,PETSC_COMM_SELF,PETSC_ERR_SYS,"Could not create pipe");
130     fd_stdout = dup(1);
131     close(1);
132     dup2(fd_pipe[1], 1);
133   }
134 #endif
135   if (part->usevwgt) PetscCall(PetscInfo(part,"PETSCPARTITIONERCHACO ignores vertex weights\n"));
136   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
137                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
138                    vmax, ndims, eigtol, seed);
139 #if defined(PETSC_HAVE_UNISTD_H)
140   {
141     char msgLog[10000];
142     int  count;
143 
144     fflush(stdout);
145     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
146     if (count < 0) count = 0;
147     msgLog[count] = 0;
148     close(1);
149     dup2(fd_stdout, 1);
150     close(fd_stdout);
151     close(fd_pipe[0]);
152     close(fd_pipe[1]);
153     PetscCheck(!ierr,PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
154   }
155 #else
156   PetscCheck(!ierr,PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in Chaco library: %s", "error in stdout");
157 #endif
158   /* Convert to PetscSection+IS */
159   for (v = 0; v < nvtxs; ++v) {
160     PetscCall(PetscSectionAddDof(partSection, assignment[v], 1));
161   }
162   PetscCall(PetscMalloc1(nvtxs, &points));
163   for (p = 0, i = 0; p < nparts; ++p) {
164     for (v = 0; v < nvtxs; ++v) {
165       if (assignment[v] == p) points[i++] = v;
166     }
167   }
168   PetscCheckFalse(i != nvtxs,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
169   PetscCall(ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition));
170   if (global_method == INERTIAL_METHOD) {
171     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
172   }
173   PetscCall(PetscFree(assignment));
174   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
175   PetscFunctionReturn(0);
176 #else
177   SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
178 #endif
179 }
180 
181 static PetscErrorCode PetscPartitionerInitialize_Chaco(PetscPartitioner part)
182 {
183   PetscFunctionBegin;
184   part->noGraph        = PETSC_FALSE;
185   part->ops->view      = PetscPartitionerView_Chaco;
186   part->ops->destroy   = PetscPartitionerDestroy_Chaco;
187   part->ops->partition = PetscPartitionerPartition_Chaco;
188   PetscFunctionReturn(0);
189 }
190 
191 /*MC
192   PETSCPARTITIONERCHACO = "chaco" - A PetscPartitioner object using the Chaco library
193 
194   Level: intermediate
195 
196 .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
197 M*/
198 
199 PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Chaco(PetscPartitioner part)
200 {
201   PetscPartitioner_Chaco *p;
202 
203   PetscFunctionBegin;
204   PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1);
205   PetscCall(PetscNewLog(part, &p));
206   part->data = p;
207 
208   PetscCall(PetscPartitionerInitialize_Chaco(part));
209   PetscCall(PetscCitationsRegister(ChacoPartitionerCitation, &ChacoPartitionerCite));
210   PetscFunctionReturn(0);
211 }
212