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