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

namespace AMDiS {

14
  ParMetisMesh::ParMetisMesh(Mesh *mesh, MPI::Intracomm *comm)
Thomas Witkowski's avatar
Thomas Witkowski committed
15
16
    : dim(mesh->getDim()),
      nElements(0),
17
      mpiComm(comm)
18
19
  {
    FUNCNAME("ParMetisMesh::ParMetisMesh()");
20
21

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

    TraverseStack stack;
27
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
28
    while (elInfo) {
29
30
      // get partition data
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
Thomas Witkowski's avatar
Thomas Witkowski committed
31
	(elInfo->getElement()->getElementData(PARTITION_ED));
32

33
34
      if (partitionData && 
	  partitionData->getPartitionStatus() == IN &&
35
36
	  partitionData->getLevel() == 0)
	elementCounter++;      
37
38
39
40

      elInfo = stack.traverseNext(elInfo);
    }

41
    nElements = elementCounter;
42

43
    TEST_EXIT(nElements > 0)("no elements in ParMETIS mesh\n");
44
45

    // allocate memory
Thomas Witkowski's avatar
Thomas Witkowski committed
46
    eptr = new int[nElements + 1];
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
47
    eind = new int[nElements * (dim + 1)];
Thomas Witkowski's avatar
Thomas Witkowski committed
48
49
    elmdist = new int[mpiSize + 1];
    elem_p2a = new int[nElements];
50

51
    if (dim == dow)
Thomas Witkowski's avatar
Thomas Witkowski committed
52
      xyz = new float[nElements * dim];
53
54
    else
      xyz = NULL;    
55

Thomas Witkowski's avatar
Thomas Witkowski committed
56
    eptr[0] = 0;
57

Thomas Witkowski's avatar
Thomas Witkowski committed
58
59
60
    int *ptr_eptr = eptr + 1;
    int *ptr_eind = eind;
    float *ptr_xyz = xyz;
61
62
    
    // gather element numbers and create elmdist
63
    mpiComm->Allgather(&nElements, 1, MPI_INT, elmdist + 1, 1, MPI_INT);
64

65
    elmdist[0] = 0;
66
    for (int i = 2; i < mpiSize + 1; i++)
67
      elmdist[i] += elmdist[i - 1];
68
69

    // traverse mesh and fill distributed ParMETIS data
Thomas Witkowski's avatar
Thomas Witkowski committed
70
    DimVec<double> bary(dim, DEFAULT_VALUE, 1.0 / (dim + 1));
71
72
73
74
75
    WorldVector<double> world;

    elementCounter = 0;

    elInfo = stack.traverseFirst(mesh, -1, 
76
				 Mesh::CALL_EVERY_EL_PREORDER | Mesh::FILL_COORDS);
77
    while (elInfo) {
78
79
80
81
82
83
84
85
      Element *element = elInfo->getElement();
      int index = element->getIndex();

      // get partition data
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));

      // if element in partition
86
87
88
      if (partitionData && 
	  partitionData->getPartitionStatus() == IN &&
	  partitionData->getLevel() == 0) {
89
90
91
92
93
	// remember index
	setParMetisIndex(index, elementCounter);
	setAMDiSIndex(elementCounter, index);

	// write eptr entry
Thomas Witkowski's avatar
Thomas Witkowski committed
94
	nodeCounter += dim + 1;
95
96
97
98
	*ptr_eptr = nodeCounter;
	ptr_eptr++;

	// write eind entries (element nodes)
Thomas Witkowski's avatar
Thomas Witkowski committed
99
	for (int i = 0; i < dim + 1; i++) {
100
101
102
103
104
	  *ptr_eind = element->getDOF(i, 0);
	  ptr_eind++;
	}

	// write xyz element coordinates
105
	if (ptr_xyz) {
106
	  elInfo->coordToWorld(bary, world);
Thomas Witkowski's avatar
Thomas Witkowski committed
107
	  for (int i = 0; i < dim; i++) {
108
109
110
111
112
113
114
115
116
117
118
	    *ptr_xyz = static_cast<float>(world[i]); 
	    ptr_xyz++;
	  }
	}

	elementCounter++;
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }

119

120
121
  ParMetisMesh::~ParMetisMesh()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
122
    if (eptr)
Thomas Witkowski's avatar
Thomas Witkowski committed
123
      delete [] eptr;
124
    
Thomas Witkowski's avatar
Thomas Witkowski committed
125
    if (eind)     
Thomas Witkowski's avatar
Thomas Witkowski committed
126
      delete [] eind;
127
    
128
    if (elmdist)
Thomas Witkowski's avatar
Thomas Witkowski committed
129
      delete [] elmdist;
130
    
Thomas Witkowski's avatar
Thomas Witkowski committed
131
    if (xyz)
Thomas Witkowski's avatar
Thomas Witkowski committed
132
      delete [] xyz;
133
    
Thomas Witkowski's avatar
Thomas Witkowski committed
134
    if (elem_p2a) 
Thomas Witkowski's avatar
Thomas Witkowski committed
135
      delete [] elem_p2a;
136
137
  }

138

139
  ParMetisGraph::ParMetisGraph(ParMetisMesh *parMesh,
140
			       MPI::Intracomm *comm,
141
			       int ncommonnodes)
142
    : parMetisMesh(parMesh)
143
144
145
  {
    int numflag = 0;

146
147
    if (ncommonnodes == -1) 
      ncommonnodes = parMetisMesh->getDim();
148

149
    MPI_Comm tmpComm = MPI_Comm(*comm);
150

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

161

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

168

169
170
171
  void ParMetisPartitioner::deletePartitionData() 
  {
    TraverseStack stack;
172
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
173
    while (elInfo) {
174
175
176
177
178
179
      Element *element = elInfo->getElement();
      element->deleteElementData(PARTITION_ED);
      elInfo = stack.traverseNext(elInfo);
    }
  }

180

181
182
  void ParMetisPartitioner::createPartitionData() 
  {
183
184
    FUNCNAME("ParMetrisPartitioner::createPartitionData()");

185
186
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
187
188
    int nLeaves = mesh->getNumberOfLeaves();
    int elPerRank = nLeaves / mpiSize;
189

190
    // === Create initial partitioning of the AMDiS mesh. ===
191

192
193
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
194
    while (elInfo) {
195
196
197
198
199
200
      Element *element = elInfo->getElement();

      TEST_EXIT(element->getElementData(PARTITION_ED) == NULL)
	("mesh already partitioned\n");

      PartitionElementData *elData = 
Thomas Witkowski's avatar
Thomas Witkowski committed
201
	new PartitionElementData(element->getElementData());
202
203
      element->setElementData(elData);

204
205
206
207
      if ((element->getIndex() >= mpiRank * elPerRank &&
	   element->getIndex() < (mpiRank + 1) * elPerRank) ||
	  (element->getIndex() >= mpiSize * elPerRank &&
	   mpiRank == mpiSize - 1))
208
	elData->setPartitionStatus(IN);
209
      else
210
	elData->setPartitionStatus(UNDEFINED);
211
      
212
213
214
215
      elInfo = stack.traverseNext(elInfo);
    }
  }

216

217
218
219
220
  void ParMetisPartitioner::partition(std::map<int, double> *elemWeights,
				      PartitionMode mode,
				      float itr) 
  {
221
    FUNCNAME("ParMetisPartitioner::partition()");
222

223
    int mpiSize = mpiComm->Get_size();
224
225

    // === create parmetis mesh ===
226
    if (parMetisMesh) 
Thomas Witkowski's avatar
Thomas Witkowski committed
227
      delete parMetisMesh;
228
229

    parMetisMesh = new ParMetisMesh(mesh, mpiComm);
230

231
    int nElements = parMetisMesh->getNumElements();
232
233

    // === create weight array ===
Thomas Witkowski's avatar
Thomas Witkowski committed
234
235
    int *wgts = elemWeights ? new int[nElements] : NULL;
    float *floatWgts = elemWeights ? new float[nElements] : NULL;
236
237
238
    float maxWgt = 0.0;
    float *ptr_floatWgts = floatWgts;

239
240
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
241
    while (elInfo) {
242
243
244
245
246
247
      Element *element = elInfo->getElement();

      // get partition data
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));

248
249
250
      if (partitionData &&
	  partitionData->getPartitionStatus() == IN &&
	  partitionData->getLevel() == 0) {
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
	int index = element->getIndex();

	// 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;
266
    mpiComm->Allreduce(&maxWgt, &tmp, 1, MPI_FLOAT, MPI_MAX);
267
268
269
    maxWgt = tmp;

    // === create dual graph ===
270
    ParMetisGraph parMetisGraph(parMetisMesh, mpiComm);
271
272
273
274
275
276

    // === 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
277

Thomas Witkowski's avatar
Thomas Witkowski committed
278
    float *tpwgts = elemWeights ? new float[mpiSize] : NULL;
279
280
281
    float ubvec = 1.05;
    int options[3] = {0, 0, 0}; // default options
    int edgecut = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
282
    int *part = new int[nElements];
283
    
284
    if (elemWeights) {
Thomas Witkowski's avatar
Thomas Witkowski committed
285
286
      // set tpwgts
      for (int i = 0; i < mpiSize; i++)
287
	tpwgts[i] = 1.0 / nparts;
288

289
      float scale = 10000.0 / maxWgt;
Thomas Witkowski's avatar
Thomas Witkowski committed
290
291
      // scale wgts
      for (int i = 0; i < nElements; i++)
292
293
294
	wgts[i] = static_cast<int>(floatWgts[i] * scale);
    }

295
296
    MPI_Comm tmpComm = MPI_Comm(*mpiComm);

297
298
    switch(mode) {
    case INITIAL:
299
      ParMETIS_V3_PartKway(parMetisMesh->getElementDist(),
300
301
			   parMetisGraph.getXAdj(),
			   parMetisGraph.getAdjncy(),
302
			   wgts,
303
			   NULL,
304
			   &wgtflag,
305
306
307
308
309
310
311
312
			   &numflag,
			   &ncon,
			   &nparts,
			   tpwgts,
			   &ubvec,
			   options,
			   &edgecut,
			   part,
313
			   &tmpComm);
314
315
316
      break;
    case ADAPTIVE_REPART:
      {
Thomas Witkowski's avatar
Thomas Witkowski committed
317
318
	int *vsize = new int[nElements];
	for (int i = 0; i < nElements; i++)
319
	  vsize[i] = 1;
320
	ParMETIS_V3_AdaptiveRepart(parMetisMesh->getElementDist(),
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
				   parMetisGraph.getXAdj(),
				   parMetisGraph.getAdjncy(),
				   wgts,
				   NULL,
				   vsize,
				   &wgtflag,
				   &numflag,
				   &ncon,
				   &nparts,
				   tpwgts,
				   &ubvec,
				   &itr,
				   options,
				   &edgecut,
				   part,
336
				   &tmpComm);
Thomas Witkowski's avatar
Thomas Witkowski committed
337
	delete [] vsize;
338
339
340
      }
      break;
    case REFINE_PART:
341
      ParMETIS_V3_RefineKway(parMetisMesh->getElementDist(),
342
343
344
345
346
347
348
349
350
351
352
353
354
			     parMetisGraph.getXAdj(),
			     parMetisGraph.getAdjncy(),
			     wgts,
			     NULL,
			     &wgtflag,
			     &numflag,
			     &ncon,
			     &nparts,
			     tpwgts,
			     &ubvec,
			     options,
			     &edgecut,
			     part,
355
			     &tmpComm);
356
357
358
359
360
361
362
363
      break;
    default: 
      ERROR_EXIT("unknown partitioning mode\n");
    }

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

364
    if (floatWgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
365
      delete [] floatWgts;
366
    if (wgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
367
      delete [] wgts;
368
    if (tpwgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
369
370
371
      delete [] tpwgts;

    delete [] part;
372
373
  }

374

375
376
377
378
379
380
381
  void ParMetisPartitioner::fillCoarsePartitionVec(std::map<int, int> *partitionVec)
  {
    TEST_EXIT(partitionVec)("no partition vector\n");

    partitionVec->clear();

    // update ParMETIS mesh to new partitioning
382
383
    if (!parMetisMesh)
      parMetisMesh = new ParMetisMesh(mesh, mpiComm);    
384

385
386
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
Thomas Witkowski's avatar
Thomas Witkowski committed
387
    int *nPartitionElements = new int[mpiSize];
388
    int *elmdist = parMetisMesh->getElementDist();
Thomas Witkowski's avatar
Thomas Witkowski committed
389

Thomas Witkowski's avatar
Thomas Witkowski committed
390
    for (int i = 0;  i < mpiSize; i++)
391
      nPartitionElements[i] = elmdist[i + 1] - elmdist[i];
392
393

    // === count number of elements ===
394
395
396
    int nElements = 0;
    int localElements = parMetisMesh->getNumElements();
    mpiComm->Allreduce(&localElements, &nElements, 1, MPI_INT, MPI_SUM);
397

Thomas Witkowski's avatar
Thomas Witkowski committed
398
    int *partitionElements = new int[nElements];
399
400

    // distribute partition elements
401
402
    mpiComm->Allgatherv(parMetisMesh->getAMDiSIndices(),
			nPartitionElements[mpiRank], 
403
404
			MPI_INT, 
			partitionElements, 
405
			nPartitionElements, 
406
407
			elmdist, 
			MPI_INT);
408
409

    // fill partitionVec
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
    for (int i = 0; i < mpiSize; i++)
      for (int j = 0; j < nPartitionElements[i]; j++)
412
413
	(*partitionVec)[partitionElements[elmdist[i] + j]] = i;

Thomas Witkowski's avatar
Thomas Witkowski committed
414
415
    delete [] partitionElements;
    delete [] nPartitionElements;
416
417
  }

418

419
420
  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
421
422
    FUNCNAME("ParMetisPartitioner::distributePartitioning()");

423
424
    int mpiSize = mpiComm->Get_size();
    int mpiRank = mpiComm->Get_rank();
425
    int nElements = parMetisMesh->getNumElements();
426

427
    // nPartitionElements[i] is the number of elements for the i-th partition
Thomas Witkowski's avatar
Thomas Witkowski committed
428
    int *nPartitionElements = new int[mpiSize];
429
    for (int i = 0; i < mpiSize; i++) 
430
      nPartitionElements[i] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
431
    for (int i = 0; i < nElements; i++)
432
      nPartitionElements[part[i]]++;
433
434

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

    // sum up partition elements over all ranks
Thomas Witkowski's avatar
Thomas Witkowski committed
439
    int *sumPartitionElements = new int[mpiSize];
440
    mpiComm->Allreduce(nPartitionElements, sumPartitionElements, mpiSize,
441
		       MPI_INT, MPI_SUM);
442
443
    
    // prepare distribution (fill partitionElements with AMDiS indices)
Thomas Witkowski's avatar
Thomas Witkowski committed
444
    int *bufferOffset = new int[mpiSize];
445
    bufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
446
    for (int i = 1; i < mpiSize; i++)
447
      bufferOffset[i] = bufferOffset[i - 1] + nPartitionElements[i - 1];
448

Thomas Witkowski's avatar
Thomas Witkowski committed
449
450
    int *partitionElements = new int[nElements];
    int **partitionPtr = new int*[mpiSize];
451

Thomas Witkowski's avatar
Thomas Witkowski committed
452
    for (int i = 0; i < mpiSize; i++)
453
454
      partitionPtr[i] = partitionElements + bufferOffset[i];

455
    for (int i = 0; i < nElements; i++) {
456
      int partition = part[i];
457
      int amdisIndex = parMetisMesh->getAMDiSIndex(i);
458
459
460
461
462
      *(partitionPtr[partition]) = amdisIndex;
      ++(partitionPtr[partition]);
    }

    // all to all: partition elements to rank elements
Thomas Witkowski's avatar
Thomas Witkowski committed
463
464
    int *rankElements = new int[sumPartitionElements[mpiRank]];
    int *recvBufferOffset = new int[mpiSize];
465
    recvBufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
466
    for (int i = 1; i < mpiSize; i++)
467
      recvBufferOffset[i] = recvBufferOffset[i - 1] + nRankElements[i - 1];
468

469
    mpiComm->Alltoallv(partitionElements, 
470
		       nPartitionElements,
471
472
473
		       bufferOffset,
		       MPI_INT,
		       rankElements,
474
		       nRankElements,
475
476
		       recvBufferOffset,
		       MPI_INT);
477
478
    

479
480
    // Create map which stores for each element index on ther partitioning level
    // if the element is in the partition of this rank.
481
    std::map<int, bool> elementInPartition;
482
    for (int i = 0; i < mpiSize; i++) {
483
      int *rankStart = rankElements + recvBufferOffset[i];
484
      int *rankEnd = rankStart + nRankElements[i];
485
      for (int *rankPtr = rankStart; rankPtr < rankEnd; ++rankPtr)
486
487
488
489
	elementInPartition[*rankPtr] = true;
    }

    TraverseStack stack;
490
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
491
    while (elInfo) {
492
493
494
495
496
497
      Element *element = elInfo->getElement();

      // get partition data
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));

498
      if (partitionData && partitionData->getLevel() == 0) {
499
	int amdisIndex = element->getIndex();
500
	if (elementInPartition[amdisIndex])
501
	  partitionData->setPartitionStatus(IN);
502
	else
503
	  partitionData->setPartitionStatus(OUT);
504
	
505
506
507
508
509
510
	descendPartitionData(element);
      }

      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
511
    delete parMetisMesh;
512
    parMetisMesh = NULL;
513

Thomas Witkowski's avatar
Thomas Witkowski committed
514
515
516
517
518
519
520
    delete [] rankElements;
    delete [] nPartitionElements;
    delete [] nRankElements;
    delete [] sumPartitionElements;
    delete [] partitionElements;
    delete [] partitionPtr;
    delete [] bufferOffset;
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
521
    delete [] recvBufferOffset;
522
523
  }

524

525
526
  void ParMetisPartitioner::descendPartitionData(Element *element) 
  {
527
528
    FUNCNAME("ParMetisPartitioner::descendPartitionData()");

529
    if (!element->isLeaf()) {
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
      Element *child0 = element->getChild(0);
      Element *child1 = element->getChild(1);

      // get partition data
      PartitionElementData *parentData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));
      PartitionElementData *child0Data = dynamic_cast<PartitionElementData*>
	(child0->getElementData(PARTITION_ED));
      PartitionElementData *child1Data = dynamic_cast<PartitionElementData*>
	(child1->getElementData(PARTITION_ED));
      
      TEST_EXIT(parentData && child0Data && child1Data)("no partition data\n");

      child0Data->setPartitionStatus(parentData->getPartitionStatus());
      child1Data->setPartitionStatus(parentData->getPartitionStatus());

      descendPartitionData(child0);
      descendPartitionData(child1);
    }
  }


  void ParMetisPartitioner::fillLeafPartitionVec(std::map<int, int> *coarseVec,
						 std::map<int, int> *fineVec)
  {
    int partition = -1;
    TraverseStack stack;
557
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
558
    while (elInfo) {
559
560
561
      Element *element = elInfo->getElement();
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));
562
      if (partitionData) {
563
	if (partitionData->getLevel() == 0)
564
	  partition = (*(coarseVec))[element->getIndex()];
565
	if (element->isLeaf())
566
567
568
569
570
571
	  (*(fineVec))[element->getIndex()] = partition;
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }
}