1 #include <petscvec.h> 2 #include <petsc/private/partitionerimpl.h> /*I "petscpartitioner.h" I*/ 3 4 typedef struct { 5 PetscBool useGrid; /* Flag to use a grid layout */ 6 PetscInt gridDim; /* The grid dimension */ 7 PetscInt nodeGrid[3]; /* Dimension of node grid */ 8 PetscInt processGrid[3]; /* Dimension of local process grid on each node */ 9 } PetscPartitioner_Simple; 10 11 static PetscErrorCode PetscPartitionerDestroy_Simple(PetscPartitioner part) 12 { 13 PetscFunctionBegin; 14 PetscCall(PetscFree(part->data)); 15 PetscFunctionReturn(0); 16 } 17 18 static PetscErrorCode PetscPartitionerView_Simple_ASCII(PetscPartitioner part, PetscViewer viewer) 19 { 20 PetscFunctionBegin; 21 PetscFunctionReturn(0); 22 } 23 24 static PetscErrorCode PetscPartitionerView_Simple(PetscPartitioner part, PetscViewer viewer) 25 { 26 PetscBool iascii; 27 28 PetscFunctionBegin; 29 PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 30 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 31 PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii)); 32 if (iascii) PetscCall(PetscPartitionerView_Simple_ASCII(part, viewer)); 33 PetscFunctionReturn(0); 34 } 35 36 static PetscErrorCode PetscPartitionerSetFromOptions_Simple(PetscOptionItems *PetscOptionsObject, PetscPartitioner part) 37 { 38 PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data; 39 PetscInt num, i; 40 PetscBool flg; 41 42 PetscFunctionBegin; 43 for (i = 0; i < 3; ++i) p->processGrid[i] = p->nodeGrid[i] = 1; 44 PetscCall(PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Simple Options")); 45 num = 3; 46 PetscCall(PetscOptionsIntArray("-petscpartitioner_simple_node_grid", "Number of nodes in each dimension", "", p->nodeGrid, &num, &flg)); 47 if (flg) {p->useGrid = PETSC_TRUE; p->gridDim = num;} 48 num = 3; 49 PetscCall(PetscOptionsIntArray("-petscpartitioner_simple_process_grid", "Number of local processes in each dimension for a given node", "", p->processGrid, &num, &flg)); 50 if (flg) { 51 p->useGrid = PETSC_TRUE; 52 if (p->gridDim < 0) p->gridDim = num; 53 else PetscCheckFalse(p->gridDim != num,PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_INCOMP, "Process grid dimension %D != %D node grid dimension", num, p->gridDim); 54 } 55 PetscCall(PetscOptionsTail()); 56 PetscFunctionReturn(0); 57 } 58 59 static PetscErrorCode PetscPartitionerPartition_Simple_Grid(PetscPartitioner part, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection vertSection, PetscSection targetSection, PetscSection partSection, IS *partition) 60 { 61 PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data; 62 const PetscInt *nodes = p->nodeGrid; 63 const PetscInt *procs = p->processGrid; 64 PetscInt *cellproc, *offsets, cells[3] = {1, 1, 1}, pcells[3] = {1, 1, 1}; 65 PetscInt Np = 1, Nr, np, nk, nj, ni, pk, pj, pi, ck, cj, ci, i; 66 MPI_Comm comm; 67 PetscMPIInt size; 68 69 PetscFunctionBegin; 70 if (vertSection) PetscCall(PetscInfo(part, "PETSCPARTITIONERSIMPLE ignores vertex weights when using grid partition\n")); 71 if (targetSection) PetscCall(PetscInfo(part, "PETSCPARTITIONERSIMPLE ignores partition weights when using grid partition\n")); 72 PetscCall(PetscObjectGetComm((PetscObject) part, &comm)); 73 PetscCallMPI(MPI_Comm_size(comm, &size)); 74 /* Check grid */ 75 for (i = 0; i < 3; ++i) Np *= nodes[i]*procs[i]; 76 PetscCheckFalse(nparts != Np,comm, PETSC_ERR_ARG_INCOMP, "Number of partitions %D != %D grid size", nparts, Np); 77 PetscCheckFalse(nparts != size,comm, PETSC_ERR_ARG_INCOMP, "Number of partitions %D != %D processes", nparts, size); 78 PetscCheckFalse(numVertices % nparts,comm, PETSC_ERR_ARG_INCOMP, "Number of cells %D is not divisible by number of partitions %D", numVertices, nparts); 79 for (i = 0; i < p->gridDim; ++i) cells[i] = nodes[i]*procs[i]; 80 Nr = numVertices / nparts; 81 while (Nr > 1) { 82 for (i = 0; i < p->gridDim; ++i) { 83 cells[i] *= 2; 84 Nr /= 2; 85 } 86 } 87 PetscCheckFalse(numVertices && Nr != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Odd number of cells %D. Must be nprocs*2^k", numVertices); 88 for (i = 0; i < p->gridDim; ++i) { 89 PetscCheckFalse(cells[i] % (nodes[i]*procs[i]),PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "dir %D. Number of cells (%D) mod number of processors %D", i, cells[i], nodes[i]*procs[i]); 90 pcells[i] = cells[i] / (nodes[i]*procs[i]); 91 } 92 /* Compute sizes */ 93 for (np = 0; np < nparts; ++np) PetscCall(PetscSectionSetDof(partSection, np, numVertices/nparts)); 94 PetscCall(PetscSectionSetUp(partSection)); 95 PetscCall(PetscCalloc1(nparts, &offsets)); 96 for (np = 0; np < nparts; ++np) PetscCall(PetscSectionGetOffset(partSection, np, &offsets[np])); 97 if (!numVertices) pcells[0] = pcells[1] = pcells[2] = 0; 98 /* Compute partition */ 99 PetscCall(PetscMalloc1(numVertices, &cellproc)); 100 for (nk = 0; nk < nodes[2]; ++nk) { 101 for (nj = 0; nj < nodes[1]; ++nj) { 102 for (ni = 0; ni < nodes[0]; ++ni) { 103 const PetscInt nid = (nk*nodes[1] + nj)*nodes[0] + ni; 104 105 for (pk = 0; pk < procs[2]; ++pk) { 106 for (pj = 0; pj < procs[1]; ++pj) { 107 for (pi = 0; pi < procs[0]; ++pi) { 108 const PetscInt pid = ((nid*procs[2] + pk)*procs[1] + pj)*procs[0] + pi; 109 110 /* Assume that cells are originally numbered lexicographically */ 111 for (ck = 0; ck < pcells[2]; ++ck) { 112 for (cj = 0; cj < pcells[1]; ++cj) { 113 for (ci = 0; ci < pcells[0]; ++ci) { 114 const PetscInt cid = (((nk*procs[2] + pk)*pcells[2] + ck)*cells[1] + ((nj*procs[1] + pj)*pcells[1] + cj))*cells[0] + (ni*procs[0] + pi)*pcells[0] + ci; 115 116 cellproc[offsets[pid]++] = cid; 117 } 118 } 119 } 120 } 121 } 122 } 123 } 124 } 125 } 126 for (np = 1; np < nparts; ++np) PetscCheckFalse(offsets[np] - offsets[np-1] != numVertices/nparts,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Offset %D != %D partition size", offsets[np], numVertices/nparts); 127 PetscCall(PetscFree(offsets)); 128 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numVertices, cellproc, PETSC_OWN_POINTER, partition)); 129 PetscFunctionReturn(0); 130 } 131 132 static PetscErrorCode PetscPartitionerPartition_Simple(PetscPartitioner part, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection vertSection, PetscSection targetSection, PetscSection partSection, IS *partition) 133 { 134 PetscPartitioner_Simple *p = (PetscPartitioner_Simple *) part->data; 135 MPI_Comm comm; 136 PetscInt np, *tpwgts = NULL, sumw = 0, numVerticesGlobal = 0; 137 PetscMPIInt size; 138 139 PetscFunctionBegin; 140 if (p->useGrid) { 141 PetscCall(PetscPartitionerPartition_Simple_Grid(part, nparts, numVertices, start, adjacency, vertSection, targetSection, partSection, partition)); 142 PetscFunctionReturn(0); 143 } 144 if (vertSection) PetscCall(PetscInfo(part,"PETSCPARTITIONERSIMPLE ignores vertex weights\n")); 145 PetscCall(PetscObjectGetComm((PetscObject) part, &comm)); 146 PetscCallMPI(MPI_Comm_size(comm, &size)); 147 if (targetSection) { 148 PetscCall(MPIU_Allreduce(&numVertices, &numVerticesGlobal, 1, MPIU_INT, MPI_SUM, comm)); 149 PetscCall(PetscCalloc1(nparts,&tpwgts)); 150 for (np = 0; np < nparts; ++np) { 151 PetscCall(PetscSectionGetDof(targetSection,np,&tpwgts[np])); 152 sumw += tpwgts[np]; 153 } 154 if (sumw) { 155 PetscInt m,mp; 156 for (np = 0; np < nparts; ++np) tpwgts[np] = (tpwgts[np]*numVerticesGlobal)/sumw; 157 for (np = 0, m = -1, mp = 0, sumw = 0; np < nparts; ++np) { 158 if (m < tpwgts[np]) { m = tpwgts[np]; mp = np; } 159 sumw += tpwgts[np]; 160 } 161 if (sumw != numVerticesGlobal) tpwgts[mp] += numVerticesGlobal - sumw; 162 } 163 if (!sumw) PetscCall(PetscFree(tpwgts)); 164 } 165 166 PetscCall(ISCreateStride(PETSC_COMM_SELF, numVertices, 0, 1, partition)); 167 if (size == 1) { 168 if (tpwgts) { 169 for (np = 0; np < nparts; ++np) { 170 PetscCall(PetscSectionSetDof(partSection, np, tpwgts[np])); 171 } 172 } else { 173 for (np = 0; np < nparts; ++np) { 174 PetscCall(PetscSectionSetDof(partSection, np, numVertices/nparts + ((numVertices % nparts) > np))); 175 } 176 } 177 } else { 178 if (tpwgts) { 179 Vec v; 180 PetscScalar *array; 181 PetscInt st,j; 182 PetscMPIInt rank; 183 184 PetscCall(VecCreate(comm,&v)); 185 PetscCall(VecSetSizes(v,numVertices,numVerticesGlobal)); 186 PetscCall(VecSetType(v,VECSTANDARD)); 187 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 188 for (np = 0,st = 0; np < nparts; ++np) { 189 if (rank == np || (rank == size-1 && size < nparts && np >= size)) { 190 for (j = 0; j < tpwgts[np]; j++) { 191 PetscCall(VecSetValue(v,st+j,np,INSERT_VALUES)); 192 } 193 } 194 st += tpwgts[np]; 195 } 196 PetscCall(VecAssemblyBegin(v)); 197 PetscCall(VecAssemblyEnd(v)); 198 PetscCall(VecGetArray(v,&array)); 199 for (j = 0; j < numVertices; ++j) { 200 PetscCall(PetscSectionAddDof(partSection,PetscRealPart(array[j]),1)); 201 } 202 PetscCall(VecRestoreArray(v,&array)); 203 PetscCall(VecDestroy(&v)); 204 } else { 205 PetscMPIInt rank; 206 PetscInt nvGlobal, *offsets, myFirst, myLast; 207 208 PetscCall(PetscMalloc1(size+1,&offsets)); 209 offsets[0] = 0; 210 PetscCallMPI(MPI_Allgather(&numVertices,1,MPIU_INT,&offsets[1],1,MPIU_INT,comm)); 211 for (np = 2; np <= size; np++) { 212 offsets[np] += offsets[np-1]; 213 } 214 nvGlobal = offsets[size]; 215 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 216 myFirst = offsets[rank]; 217 myLast = offsets[rank + 1] - 1; 218 PetscCall(PetscFree(offsets)); 219 if (numVertices) { 220 PetscInt firstPart = 0, firstLargePart = 0; 221 PetscInt lastPart = 0, lastLargePart = 0; 222 PetscInt rem = nvGlobal % nparts; 223 PetscInt pSmall = nvGlobal/nparts; 224 PetscInt pBig = nvGlobal/nparts + 1; 225 226 if (rem) { 227 firstLargePart = myFirst / pBig; 228 lastLargePart = myLast / pBig; 229 230 if (firstLargePart < rem) { 231 firstPart = firstLargePart; 232 } else { 233 firstPart = rem + (myFirst - (rem * pBig)) / pSmall; 234 } 235 if (lastLargePart < rem) { 236 lastPart = lastLargePart; 237 } else { 238 lastPart = rem + (myLast - (rem * pBig)) / pSmall; 239 } 240 } else { 241 firstPart = myFirst / (nvGlobal/nparts); 242 lastPart = myLast / (nvGlobal/nparts); 243 } 244 245 for (np = firstPart; np <= lastPart; np++) { 246 PetscInt PartStart = np * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np); 247 PetscInt PartEnd = (np+1) * (nvGlobal/nparts) + PetscMin(nvGlobal % nparts,np+1); 248 249 PartStart = PetscMax(PartStart,myFirst); 250 PartEnd = PetscMin(PartEnd,myLast+1); 251 PetscCall(PetscSectionSetDof(partSection,np,PartEnd-PartStart)); 252 } 253 } 254 } 255 } 256 PetscCall(PetscFree(tpwgts)); 257 PetscFunctionReturn(0); 258 } 259 260 static PetscErrorCode PetscPartitionerInitialize_Simple(PetscPartitioner part) 261 { 262 PetscFunctionBegin; 263 part->noGraph = PETSC_TRUE; 264 part->ops->view = PetscPartitionerView_Simple; 265 part->ops->setfromoptions = PetscPartitionerSetFromOptions_Simple; 266 part->ops->destroy = PetscPartitionerDestroy_Simple; 267 part->ops->partition = PetscPartitionerPartition_Simple; 268 PetscFunctionReturn(0); 269 } 270 271 /*MC 272 PETSCPARTITIONERSIMPLE = "simple" - A PetscPartitioner object 273 274 Level: intermediate 275 276 .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType() 277 M*/ 278 279 PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Simple(PetscPartitioner part) 280 { 281 PetscPartitioner_Simple *p; 282 283 PetscFunctionBegin; 284 PetscValidHeaderSpecific(part, PETSCPARTITIONER_CLASSID, 1); 285 PetscCall(PetscNewLog(part, &p)); 286 p->gridDim = -1; 287 part->data = p; 288 289 PetscCall(PetscPartitionerInitialize_Simple(part)); 290 PetscFunctionReturn(0); 291 } 292