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
	  if (mapLocalGlobal)
89
	    *ptr_eind = (*mapLocalGlobal)[element->getDof(i, 0)];
90
	  else
91
	    *ptr_eind = element->getDof(i, 0);
92

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
  void ParMetisPartitioner::fillCoarsePartitionVec(std::map<int, int> *partitionVec)
  {
352
353
354
    FUNCNAME("ParMetisPartitioner::fillCoarsePartitionVec()");

    TEST_EXIT_DBG(partitionVec)("no partition vector\n");
355
356
357
358

    partitionVec->clear();

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

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

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

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

375
    std::vector<int> partitionElements(nElements);
376
377

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

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

392

393
394
  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
395
396
    FUNCNAME("ParMetisPartitioner::distributePartitioning()");

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

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

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

412

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

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

427
    for (int i = 0; i < mpiSize; i++)
428
429
      partitionPtr[i] = partitionElements + bufferOffset[i];

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

435
436
437
      if (partition != mpiRank)
	sendElements[partition].push_back(amdisIndex);

438
439
440
441
442
      *(partitionPtr[partition]) = amdisIndex;
      ++(partitionPtr[partition]);
    }

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

449
    mpiComm->Alltoallv(partitionElements, 
450
		       nPartitionElements,
451
452
453
		       bufferOffset,
		       MPI_INT,
		       rankElements,
454
		       nRankElements,
455
456
		       recvBufferOffset,
		       MPI_INT);
457
    
458
459
460
461
    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;
462

463
464
    // Create map which stores for each element index on ther partitioning level
    // if the element is in the partition of this rank.
465
    recvElements.clear();
466
    for (int i = 0; i < mpiSize; i++) {
467
      int *rankStart = rankElements + recvBufferOffset[i];
468
      int *rankEnd = rankStart + nRankElements[i];
469
470
      for (int *rankPtr = rankStart; rankPtr < rankEnd; ++rankPtr) {
	elementInRank[*rankPtr] = true;
471
472
	if (i != mpiRank)
	  recvElements[i].push_back(*rankPtr);
473
474
475
      }
    }

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

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

}