ParMetisPartitioner.cc 12.5 KB
Newer Older
1
#include <queue>
2
3
4
5
6
7
8
9
10
11
12
#include "ParMetisPartitioner.h"
#include "Mesh.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "Element.h"
#include "FixVec.h"
#include "DOFVector.h"
#include "mpi.h"

namespace AMDiS {

13
  ParMetisMesh::ParMetisMesh(Mesh *mesh, MPI::Intracomm *comm, 
14
			     std::map<int, bool>& elementInRank,
15
			     std::map<DegreeOfFreedom, DegreeOfFreedom> *mapLocalGlobal)
Thomas Witkowski's avatar
Thomas Witkowski committed
16
17
    : dim(mesh->getDim()),
      nElements(0),
18
      mpiComm(comm)
19
20
  {
    FUNCNAME("ParMetisMesh::ParMetisMesh()");
21
22

    int mpiSize = mpiComm->Get_size();
23
24
25
26
27
    int nodeCounter = 0;
    int elementCounter = 0;
    int dow = Global::getGeo(WORLD);

    TraverseStack stack;
28
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
29
    while (elInfo) {
30
      if (elementInRank[elInfo->getElement()->getIndex()])
31
	elementCounter++;      
32
33
34
35

      elInfo = stack.traverseNext(elInfo);
    }

36
    nElements = elementCounter;
37

38
    TEST_EXIT(nElements > 0)("No elements in ParMETIS mesh!\n");
39
40

    // allocate memory
Thomas Witkowski's avatar
Thomas Witkowski committed
41
    eptr = new int[nElements + 1];
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
42
    eind = new int[nElements * (dim + 1)];
Thomas Witkowski's avatar
Thomas Witkowski committed
43
44
    elmdist = new int[mpiSize + 1];
    elem_p2a = new int[nElements];
45

46
    if (dim == dow)
Thomas Witkowski's avatar
Thomas Witkowski committed
47
      xyz = new float[nElements * dim];
48
49
    else
      xyz = NULL;    
50

Thomas Witkowski's avatar
Thomas Witkowski committed
51
    eptr[0] = 0;
52

Thomas Witkowski's avatar
Thomas Witkowski committed
53
54
55
    int *ptr_eptr = eptr + 1;
    int *ptr_eind = eind;
    float *ptr_xyz = xyz;
56
57
    
    // gather element numbers and create elmdist
58
    mpiComm->Allgather(&nElements, 1, MPI_INT, elmdist + 1, 1, MPI_INT);
59

60
    elmdist[0] = 0;
61
    for (int i = 2; i < mpiSize + 1; i++)
62
      elmdist[i] += elmdist[i - 1];
63
64

    // traverse mesh and fill distributed ParMETIS data
Thomas Witkowski's avatar
Thomas Witkowski committed
65
    DimVec<double> bary(dim, DEFAULT_VALUE, 1.0 / (dim + 1));
66
67
68
69
    WorldVector<double> world;

    elementCounter = 0;

70
    elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL | Mesh::FILL_COORDS);
71
    while (elInfo) {
72
73
74
75
      Element *element = elInfo->getElement();
      int index = element->getIndex();

      // if element in partition
76
      if (elementInRank[index]) {
77
78
79
80
81
	// remember index
	setParMetisIndex(index, elementCounter);
	setAMDiSIndex(elementCounter, index);

	// write eptr entry
Thomas Witkowski's avatar
Thomas Witkowski committed
82
	nodeCounter += dim + 1;
83
84
85
86
	*ptr_eptr = nodeCounter;
	ptr_eptr++;

	// write eind entries (element nodes)
Thomas Witkowski's avatar
Thomas Witkowski committed
87
	for (int i = 0; i < dim + 1; i++) {
88
89
90
91
92
	  if (mapLocalGlobal)
	    *ptr_eind = (*mapLocalGlobal)[element->getDOF(i, 0)];
	  else
	    *ptr_eind = element->getDOF(i, 0);

93
94
95
96
	  ptr_eind++;
	}

	// write xyz element coordinates
97
	if (ptr_xyz) {
98
	  elInfo->coordToWorld(bary, world);
Thomas Witkowski's avatar
Thomas Witkowski committed
99
	  for (int i = 0; i < dim; i++) {
100
101
102
103
104
105
106
	    *ptr_xyz = static_cast<float>(world[i]); 
	    ptr_xyz++;
	  }
	}

	elementCounter++;
      }
107

108
109
110
111
      elInfo = stack.traverseNext(elInfo);
    }
  }

112

113
114
  ParMetisMesh::~ParMetisMesh()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
115
    if (eptr)
Thomas Witkowski's avatar
Thomas Witkowski committed
116
      delete [] eptr;
117
    
Thomas Witkowski's avatar
Thomas Witkowski committed
118
    if (eind)     
Thomas Witkowski's avatar
Thomas Witkowski committed
119
      delete [] eind;
120
    
121
    if (elmdist)
Thomas Witkowski's avatar
Thomas Witkowski committed
122
      delete [] elmdist;
123
    
Thomas Witkowski's avatar
Thomas Witkowski committed
124
    if (xyz)
Thomas Witkowski's avatar
Thomas Witkowski committed
125
      delete [] xyz;
126
    
Thomas Witkowski's avatar
Thomas Witkowski committed
127
    if (elem_p2a) 
Thomas Witkowski's avatar
Thomas Witkowski committed
128
      delete [] elem_p2a;
129
130
  }

131

132
  ParMetisGraph::ParMetisGraph(ParMetisMesh *parMesh,
133
			       MPI::Intracomm *comm,
134
			       int ncommonnodes)
135
    : parMetisMesh(parMesh)
136
  {
137
138
139
140
141
    FUNCNAME("ParMetisGraph::ParMetisGraph()");

    TEST_EXIT(parMesh)("No ParMetisMesh defined!\n");
    TEST_EXIT(comm)("No MPI communicator defined!\n");

142
143
    int numflag = 0;

144
145
    if (ncommonnodes == -1) 
      ncommonnodes = parMetisMesh->getDim();
146

147
    MPI_Comm tmpComm = MPI_Comm(*comm);
148

149
150
151
    ParMETIS_V3_Mesh2Dual(parMetisMesh->getElementDist(),
			  parMetisMesh->getElementPtr(),
			  parMetisMesh->getElementInd(),
152
153
			  &numflag,
			  &ncommonnodes,
Thomas Witkowski's avatar
Thomas Witkowski committed
154
155
			  &xadj,
			  &adjncy,
156
			  &tmpComm);
157
158
  }

159

160
161
  ParMetisGraph::~ParMetisGraph()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
162
163
    free(xadj);
    free(adjncy);
164
165
  }

166

167
168
  void ParMetisPartitioner::createPartitionData() 
  {
169
170
    FUNCNAME("ParMetrisPartitioner::createPartitionData()");

171
172
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
173
174
    int nLeaves = mesh->getNumberOfLeaves();
    int elPerRank = nLeaves / mpiSize;
175

176
    // === Create initial partitioning of the AMDiS mesh. ===
177

178
179
    elementInRank.clear();

180
    TraverseStack stack;
181
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
182
    while (elInfo) {
183
184
      Element *element = elInfo->getElement();

185
186
187
188
      if ((element->getIndex() >= mpiRank * elPerRank &&
	   element->getIndex() < (mpiRank + 1) * elPerRank) ||
	  (element->getIndex() >= mpiSize * elPerRank &&
	   mpiRank == mpiSize - 1))
189
	elementInRank[element->getIndex()] = true;
190
      else
191
	elementInRank[element->getIndex()] = false;
192
      
193
194
195
196
      elInfo = stack.traverseNext(elInfo);
    }
  }

197

198
199
200
201
  void ParMetisPartitioner::partition(std::map<int, double> *elemWeights,
				      PartitionMode mode,
				      float itr) 
  {
202
    FUNCNAME("ParMetisPartitioner::partition()");
203

204
    int mpiSize = mpiComm->Get_size();
205
206

    // === create parmetis mesh ===
207
    if (parMetisMesh) 
Thomas Witkowski's avatar
Thomas Witkowski committed
208
      delete parMetisMesh;
209

210
211
    TEST_EXIT(elementInRank.size() != 0)("Should not happen!\n");
    parMetisMesh = new ParMetisMesh(mesh, mpiComm, elementInRank, mapLocalGlobal);
212

213
    int nElements = parMetisMesh->getNumElements();
214
215

    // === create weight array ===
Thomas Witkowski's avatar
Thomas Witkowski committed
216
217
    int *wgts = elemWeights ? new int[nElements] : NULL;
    float *floatWgts = elemWeights ? new float[nElements] : NULL;
218
219
220
    float maxWgt = 0.0;
    float *ptr_floatWgts = floatWgts;

221
    TraverseStack stack;
222
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
223
    while (elInfo) {
224
      int index = elInfo->getElement()->getIndex();
225

226
      if (elementInRank[index]) {
227
228
229
230
231
232
233
234
235
236
237
238
	// get weight 
	float wgt = static_cast<float>((*elemWeights)[index]);
	maxWgt = max(wgt, maxWgt);

	// write float weight
	*ptr_floatWgts = wgt;
	ptr_floatWgts++;
      }
      elInfo = stack.traverseNext(elInfo);
    }

    float tmp;
239
    mpiComm->Allreduce(&maxWgt, &tmp, 1, MPI_FLOAT, MPI_MAX);
240
241
242
    maxWgt = tmp;

    // === create dual graph ===
243
    ParMetisGraph parMetisGraph(parMetisMesh, mpiComm);
244
245
246
247
248
249

    // === partitioning of dual graph ===
    int wgtflag = elemWeights ? 2 : 0; // weights at vertices only!
    int numflag = 0; // c numbering style!
    int ncon = elemWeights ? 1 : 0; // one weight at each vertex!
    int nparts = mpiSize; // number of partitions
250

Thomas Witkowski's avatar
Thomas Witkowski committed
251
    float *tpwgts = elemWeights ? new float[mpiSize] : NULL;
252
253
254
    float ubvec = 1.05;
    int options[3] = {0, 0, 0}; // default options
    int edgecut = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
255
    int *part = new int[nElements];
256

257
    if (elemWeights) {
Thomas Witkowski's avatar
Thomas Witkowski committed
258
259
      // set tpwgts
      for (int i = 0; i < mpiSize; i++)
260
	tpwgts[i] = 1.0 / nparts;
261

262
      float scale = 10000.0 / maxWgt;
Thomas Witkowski's avatar
Thomas Witkowski committed
263
264
      // scale wgts
      for (int i = 0; i < nElements; i++)
265
266
267
	wgts[i] = static_cast<int>(floatWgts[i] * scale);
    }

268
269
    MPI_Comm tmpComm = MPI_Comm(*mpiComm);

270
    switch (mode) {
271
    case INITIAL:
272
      ParMETIS_V3_PartKway(parMetisMesh->getElementDist(),
273
274
			   parMetisGraph.getXAdj(),
			   parMetisGraph.getAdjncy(),
275
			   wgts,
276
			   NULL,
277
			   &wgtflag,
278
279
280
281
282
283
284
285
			   &numflag,
			   &ncon,
			   &nparts,
			   tpwgts,
			   &ubvec,
			   options,
			   &edgecut,
			   part,
286
			   &tmpComm);
287
288
289
      break;
    case ADAPTIVE_REPART:
      {
Thomas Witkowski's avatar
Thomas Witkowski committed
290
291
	int *vsize = new int[nElements];
	for (int i = 0; i < nElements; i++)
292
	  vsize[i] = 1;
293
	ParMETIS_V3_AdaptiveRepart(parMetisMesh->getElementDist(),
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
				   parMetisGraph.getXAdj(),
				   parMetisGraph.getAdjncy(),
				   wgts,
				   NULL,
				   vsize,
				   &wgtflag,
				   &numflag,
				   &ncon,
				   &nparts,
				   tpwgts,
				   &ubvec,
				   &itr,
				   options,
				   &edgecut,
				   part,
309
				   &tmpComm);
310

Thomas Witkowski's avatar
Thomas Witkowski committed
311
	delete [] vsize;
312
313
314
      }
      break;
    case REFINE_PART:
315
      ParMETIS_V3_RefineKway(parMetisMesh->getElementDist(),
316
317
318
319
320
321
322
323
324
325
326
327
328
			     parMetisGraph.getXAdj(),
			     parMetisGraph.getAdjncy(),
			     wgts,
			     NULL,
			     &wgtflag,
			     &numflag,
			     &ncon,
			     &nparts,
			     tpwgts,
			     &ubvec,
			     options,
			     &edgecut,
			     part,
329
			     &tmpComm);
330

331
332
333
334
335
336
337
338
      break;
    default: 
      ERROR_EXIT("unknown partitioning mode\n");
    }

    // === distribute new partition data ===
    distributePartitioning(part);

339
    if (floatWgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
340
      delete [] floatWgts;
341
    if (wgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
342
      delete [] wgts;
343
    if (tpwgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
344
345
346
      delete [] tpwgts;

    delete [] part;
347
348
  }

349

350
351
352
353
354
355
356
  void ParMetisPartitioner::fillCoarsePartitionVec(std::map<int, int> *partitionVec)
  {
    TEST_EXIT(partitionVec)("no partition vector\n");

    partitionVec->clear();

    // update ParMETIS mesh to new partitioning
357
    if (!parMetisMesh)
358
      parMetisMesh = new ParMetisMesh(mesh, mpiComm, elementInRank, mapLocalGlobal);
359

360
361
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
362
    std::vector<int> nPartitionElements(mpiSize);
363
    int *elmdist = parMetisMesh->getElementDist();
Thomas Witkowski's avatar
Thomas Witkowski committed
364

365
    for (int i = 0; i < mpiSize; i++)
366
      nPartitionElements[i] = elmdist[i + 1] - elmdist[i];
367
368

    // === count number of elements ===
369
370
371
    int nElements = 0;
    int localElements = parMetisMesh->getNumElements();
    mpiComm->Allreduce(&localElements, &nElements, 1, MPI_INT, MPI_SUM);
372

373
    std::vector<int> partitionElements(nElements);
374
375

    // distribute partition elements
376
377
    mpiComm->Allgatherv(parMetisMesh->getAMDiSIndices(),
			nPartitionElements[mpiRank], 
378
			MPI_INT, 
379
380
			&(partitionElements[0]), 
			&(nPartitionElements[0]), 
381
382
			elmdist, 
			MPI_INT);
383
384

    // fill partitionVec
Thomas Witkowski's avatar
Thomas Witkowski committed
385
386
    for (int i = 0; i < mpiSize; i++)
      for (int j = 0; j < nPartitionElements[i]; j++)
387
388
389
	(*partitionVec)[partitionElements[elmdist[i] + j]] = i;
  }

390

391
392
  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
393
394
    FUNCNAME("ParMetisPartitioner::distributePartitioning()");

395
396
    int mpiSize = mpiComm->Get_size();
    int mpiRank = mpiComm->Get_rank();
397
    int nElements = parMetisMesh->getNumElements();
398

399
    // nPartitionElements[i] is the number of elements for the i-th partition
Thomas Witkowski's avatar
Thomas Witkowski committed
400
    int *nPartitionElements = new int[mpiSize];
401
    for (int i = 0; i < mpiSize; i++) 
402
      nPartitionElements[i] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
403
    for (int i = 0; i < nElements; i++)
404
      nPartitionElements[part[i]]++;    
405
406

    // collect number of partition elements from all ranks for this rank
Thomas Witkowski's avatar
Thomas Witkowski committed
407
    int *nRankElements = new int[mpiSize];
408
    mpiComm->Alltoall(nPartitionElements, 1, MPI_INT, nRankElements, 1, MPI_INT);
409

410

411
    // sum up partition elements over all ranks
Thomas Witkowski's avatar
Thomas Witkowski committed
412
    int *sumPartitionElements = new int[mpiSize];
413
    mpiComm->Allreduce(nPartitionElements, sumPartitionElements, mpiSize,
414
		       MPI_INT, MPI_SUM);
415
416
    
    // prepare distribution (fill partitionElements with AMDiS indices)
Thomas Witkowski's avatar
Thomas Witkowski committed
417
    int *bufferOffset = new int[mpiSize];
418
    bufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
419
    for (int i = 1; i < mpiSize; i++)
420
      bufferOffset[i] = bufferOffset[i - 1] + nPartitionElements[i - 1];
421

Thomas Witkowski's avatar
Thomas Witkowski committed
422
423
    int *partitionElements = new int[nElements];
    int **partitionPtr = new int*[mpiSize];
424

425
426
    sendElements.resize(mpiSize);
    for (int i = 0; i < mpiSize; i++) {
427
      partitionPtr[i] = partitionElements + bufferOffset[i];
428
429
      sendElements[i].clear();
    }
430

431
    for (int i = 0; i < nElements; i++) {
432
      int partition = part[i];
433
      int amdisIndex = parMetisMesh->getAMDiSIndex(i);
434
435

      sendElements[partition].push_back(amdisIndex);
436
437
438
439
440
      *(partitionPtr[partition]) = amdisIndex;
      ++(partitionPtr[partition]);
    }

    // all to all: partition elements to rank elements
Thomas Witkowski's avatar
Thomas Witkowski committed
441
442
    int *rankElements = new int[sumPartitionElements[mpiRank]];
    int *recvBufferOffset = new int[mpiSize];
443
    recvBufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
444
    for (int i = 1; i < mpiSize; i++)
445
      recvBufferOffset[i] = recvBufferOffset[i - 1] + nRankElements[i - 1];
446

447
    mpiComm->Alltoallv(partitionElements, 
448
		       nPartitionElements,
449
450
451
		       bufferOffset,
		       MPI_INT,
		       rankElements,
452
		       nRankElements,
453
454
		       recvBufferOffset,
		       MPI_INT);
455
    
456
457
458
459
    TEST_EXIT(elementInRank.size() != 0)("Should not happen!\n");
    for (std::map<int, bool>::iterator it = elementInRank.begin();
	 it != elementInRank.end(); ++it)
      elementInRank[it->first] = false;
460

461
462
    // Create map which stores for each element index on ther partitioning level
    // if the element is in the partition of this rank.
463
    recvElements.resize(mpiSize);
464
    for (int i = 0; i < mpiSize; i++) {
465
466
      recvElements[i].clear();

467
      int *rankStart = rankElements + recvBufferOffset[i];
468
      int *rankEnd = rankStart + nRankElements[i];
469
470
471
      for (int *rankPtr = rankStart; rankPtr < rankEnd; ++rankPtr) {
	elementInRank[*rankPtr] = true;
	recvElements[i].push_back(*rankPtr);
472
473
474
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
475
    delete parMetisMesh;
476
    parMetisMesh = NULL;
477

Thomas Witkowski's avatar
Thomas Witkowski committed
478
479
480
481
482
483
484
    delete [] rankElements;
    delete [] nPartitionElements;
    delete [] nRankElements;
    delete [] sumPartitionElements;
    delete [] partitionElements;
    delete [] partitionPtr;
    delete [] bufferOffset;
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
485
    delete [] recvBufferOffset;
486
487
488
  }

}