ParMetisPartitioner.cc 14.8 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
27
28
    int nodeCounter = 0;
    int elementCounter = 0;
    int dow = Global::getGeo(WORLD);

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

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

      elInfo = stack.traverseNext(elInfo);
    }

43
    nElements = elementCounter;
44

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
58
    eptr[0] = 0;
59

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

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

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

    elementCounter = 0;

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

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

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

	// write eptr entry
Thomas Witkowski's avatar
Thomas Witkowski committed
97
	nodeCounter += dim + 1;
98
99
100
101
	*ptr_eptr = nodeCounter;
	ptr_eptr++;

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

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

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

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

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

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

150
    MPI_Comm tmpComm = MPI_Comm(*comm);
151

152
153
154
    ParMETIS_V3_Mesh2Dual(parMetisMesh->getElementDist(),
			  parMetisMesh->getElementPtr(),
			  parMetisMesh->getElementInd(),
155
156
			  &numflag,
			  &ncommonnodes,
Thomas Witkowski's avatar
Thomas Witkowski committed
157
158
			  &xadj,
			  &adjncy,
159
			  &tmpComm);
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
172
173
  void ParMetisPartitioner::deletePartitionData() 
  {
    TraverseStack stack;
    ElInfo *elInfo;
    elInfo = stack.traverseFirst(mesh_, -1, Mesh::CALL_EVERY_EL_PREORDER);
174
    while (elInfo) {
175
176
177
178
179
180
181
      Element *element = elInfo->getElement();
      element->deleteElementData(PARTITION_ED);
      elInfo = stack.traverseNext(elInfo);
    }
  }

  void ParMetisPartitioner::createPartitionData() {
182
183
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
184
185
186
187
188
189
190

    TraverseStack stack;
    ElInfo *elInfo;

    // === create initial partitioning on AMDiS mesh ===
    int totalElementCounter = 0;
    elInfo = stack.traverseFirst(mesh_, -1, Mesh::CALL_LEAF_EL);
191
    while (elInfo) {
192
193
194
195
196
197
      Element *element = elInfo->getElement();

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

      PartitionElementData *elData = 
Thomas Witkowski's avatar
Thomas Witkowski committed
198
	new PartitionElementData(element->getElementData());
199
200
      element->setElementData(elData);

201
      if (totalElementCounter % mpiSize == mpiRank)
202
	elData->setPartitionStatus(IN);
203
      else
204
	elData->setPartitionStatus(UNDEFINED);
205
      
206
207
208
209
210
211
212
213
214
215
      totalElementCounter++;

      elInfo = stack.traverseNext(elInfo);
    }
  }

  void ParMetisPartitioner::partition(std::map<int, double> *elemWeights,
				      PartitionMode mode,
				      float itr) 
  {
216
    int mpiSize = mpiComm->Get_size();
217
218
219
220
221

    TraverseStack stack;
    ElInfo *elInfo;

    // === create parmetis mesh ===
222
    if (parMetisMesh) 
Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
      delete parMetisMesh;
    parMetisMesh = new ParMetisMesh(mesh_, mpiComm);
225

226
    int nElements = parMetisMesh->getNumElements();
227
228

    // === create weight array ===
Thomas Witkowski's avatar
Thomas Witkowski committed
229
230
    int *wgts = elemWeights ? new int[nElements] : NULL;
    float *floatWgts = elemWeights ? new float[nElements] : NULL;
231
232
233
234
    float maxWgt = 0.0;
    float *ptr_floatWgts = floatWgts;

    elInfo = stack.traverseFirst(mesh_, -1, Mesh::CALL_EVERY_EL_PREORDER);
235
    while (elInfo) {
236
237
238
239
240
241
      Element *element = elInfo->getElement();

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

242
243
244
      if (partitionData &&
	  partitionData->getPartitionStatus() == IN &&
	  partitionData->getLevel() == 0) {
245
246
247
248
249
250
251
252
253
254
255
256
257
258
	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;
259
    mpiComm->Allreduce(&maxWgt, &tmp, 1, MPI_FLOAT, MPI_MAX);
260
261
262
    maxWgt = tmp;

    // === create dual graph ===
263
    ParMetisGraph parMetisGraph(parMetisMesh, mpiComm);
264
265
266
267
268
269

    // === 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
Thomas Witkowski's avatar
Thomas Witkowski committed
270
    float *tpwgts = elemWeights ? new float[mpiSize] : NULL;
271
272
273
    float ubvec = 1.05;
    int options[3] = {0, 0, 0}; // default options
    int edgecut = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
274
    int *part = new int[nElements];
275
    
276
    if (elemWeights) {
Thomas Witkowski's avatar
Thomas Witkowski committed
277
278
      // set tpwgts
      for (int i = 0; i < mpiSize; i++)
279
	tpwgts[i] = 1.0 / nparts;
280
281

      float scale = 10000 / maxWgt;
Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
      // scale wgts
      for (int i = 0; i < nElements; i++)
284
285
286
	wgts[i] = static_cast<int>(floatWgts[i] * scale);
    }

287
288
    MPI_Comm tmpComm = MPI_Comm(*mpiComm);

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

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

356
    if (floatWgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
357
      delete [] floatWgts;
358
    if (wgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
359
      delete [] wgts;
360
    if (tpwgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
361
362
363
      delete [] tpwgts;

    delete [] part;
364
365
366
367
368
369
370
371
372
  }

  void ParMetisPartitioner::fillCoarsePartitionVec(std::map<int, int> *partitionVec)
  {
    TEST_EXIT(partitionVec)("no partition vector\n");

    partitionVec->clear();

    // update ParMETIS mesh to new partitioning
373
    if (!parMetisMesh) 
Thomas Witkowski's avatar
Thomas Witkowski committed
374
      parMetisMesh = new ParMetisMesh(mesh_, mpiComm);
375

376
377
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
Thomas Witkowski's avatar
Thomas Witkowski committed
378
    int *nPartitionElements = new int[mpiSize];
379
    int *elmdist = parMetisMesh->getElementDist();
Thomas Witkowski's avatar
Thomas Witkowski committed
380

Thomas Witkowski's avatar
Thomas Witkowski committed
381
    for (int i = 0;  i < mpiSize; i++)
382
      nPartitionElements[i] = elmdist[i + 1] - elmdist[i];
383
384

    // === count number of elements ===
385
386
387
    int nElements = 0;
    int localElements = parMetisMesh->getNumElements();
    mpiComm->Allreduce(&localElements, &nElements, 1, MPI_INT, MPI_SUM);
388

Thomas Witkowski's avatar
Thomas Witkowski committed
389
    int *partitionElements = new int[nElements];
390
391

    // distribute partition elements
392
393
    mpiComm->Allgatherv(parMetisMesh->getAMDiSIndices(),
			nPartitionElements[mpiRank], 
394
395
			MPI_INT, 
			partitionElements, 
396
			nPartitionElements, 
397
398
			elmdist, 
			MPI_INT);
399
400

    // fill partitionVec
Thomas Witkowski's avatar
Thomas Witkowski committed
401
402
    for (int i = 0; i < mpiSize; i++)
      for (int j = 0; j < nPartitionElements[i]; j++)
403
404
	(*partitionVec)[partitionElements[elmdist[i] + j]] = i;

Thomas Witkowski's avatar
Thomas Witkowski committed
405
406
    delete [] partitionElements;
    delete [] nPartitionElements;
407
408
409
410
  }

  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
411
412
    int mpiSize = mpiComm->Get_size();
    int mpiRank = mpiComm->Get_rank();
413
    int nElements = parMetisMesh->getNumElements();
414

415
    // nPartitionElements[i] is the number of elements for the i-th partition
Thomas Witkowski's avatar
Thomas Witkowski committed
416
    int *nPartitionElements = new int[mpiSize];
417
    for (int i = 0; i < mpiSize; i++) 
418
      nPartitionElements[i] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
419
    for (int i = 0; i < nElements; i++)
420
      nPartitionElements[part[i]]++;
421
422

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

    // sum up partition elements over all ranks
Thomas Witkowski's avatar
Thomas Witkowski committed
428
    int *sumPartitionElements = new int[mpiSize];
429
    mpiComm->Allreduce(nPartitionElements, sumPartitionElements, mpiSize,
430
		       MPI_INT, MPI_SUM);
431
432
433

    
    // prepare distribution (fill partitionElements with AMDiS indices)
Thomas Witkowski's avatar
Thomas Witkowski committed
434
    int *bufferOffset = new int[mpiSize];
435
    bufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
436
    for (int i = 1; i < mpiSize; i++)
437
      bufferOffset[i] = bufferOffset[i - 1] + nPartitionElements[i - 1];
438

Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
    int *partitionElements = new int[nElements];
    int **partitionPtr = new int*[mpiSize];
441

Thomas Witkowski's avatar
Thomas Witkowski committed
442
    for (int i = 0; i < mpiSize; i++)
443
444
      partitionPtr[i] = partitionElements + bufferOffset[i];

445
    for (int i = 0; i < nElements; i++) {
446
      int partition = part[i];
447
      int amdisIndex = parMetisMesh->getAMDiSIndex(i);
448
449
450
451
452
      *(partitionPtr[partition]) = amdisIndex;
      ++(partitionPtr[partition]);
    }

    // all to all: partition elements to rank elements
Thomas Witkowski's avatar
Thomas Witkowski committed
453
454
    int *rankElements = new int[sumPartitionElements[mpiRank]];
    int *recvBufferOffset = new int[mpiSize];
455
    recvBufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
456
    for (int i = 1; i < mpiSize; i++)
457
      recvBufferOffset[i] = recvBufferOffset[i - 1] + nRankElements[i - 1];
458

459
    mpiComm->Alltoallv(partitionElements, 
460
		       nPartitionElements,
461
462
463
		       bufferOffset,
		       MPI_INT,
		       rankElements,
464
		       nRankElements,
465
466
		       recvBufferOffset,
		       MPI_INT);
467
468
    

469
470
    // Create map which stores for each element index on ther partitioning level
    // if the element is in the partition of this rank.
471
    std::map<int, bool> elementInPartition;
472
    for (int i = 0; i < mpiSize; i++) {
473
      int *rankStart = rankElements + recvBufferOffset[i];
474
      int *rankEnd = rankStart + nRankElements[i];
475
      for (int *rankPtr = rankStart; rankPtr < rankEnd; ++rankPtr)
476
477
478
479
480
	elementInPartition[*rankPtr] = true;
    }

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh_, -1, Mesh::CALL_EVERY_EL_PREORDER);
481
    while (elInfo) {
482
483
484
485
486
487
      Element *element = elInfo->getElement();

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

488
      if (partitionData && partitionData->getLevel() == 0) {
489
	int amdisIndex = element->getIndex();
490
	if (elementInPartition[amdisIndex])
491
	  partitionData->setPartitionStatus(IN);
492
	else
493
	  partitionData->setPartitionStatus(OUT);
494
	
495
496
497
498
499
500
	descendPartitionData(element);
      }

      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
501
    delete parMetisMesh;
502
    parMetisMesh = NULL;
503

Thomas Witkowski's avatar
Thomas Witkowski committed
504
505
506
507
508
509
510
    delete [] rankElements;
    delete [] nPartitionElements;
    delete [] nRankElements;
    delete [] sumPartitionElements;
    delete [] partitionElements;
    delete [] partitionPtr;
    delete [] bufferOffset;
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
511
    delete [] recvBufferOffset;
512
513
514
515
  }

  void ParMetisPartitioner::descendPartitionData(Element *element) 
  {
516
    if (!element->isLeaf()) {
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
      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;
    ElInfo *elInfo = stack.traverseFirst(mesh_, -1, Mesh::CALL_EVERY_EL_PREORDER);
545
    while (elInfo) {
546
547
548
      Element *element = elInfo->getElement();
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));
549
      if (partitionData) {
550
	if (partitionData->getLevel() == 0)
551
	  partition = (*(coarseVec))[element->getIndex()];
552
	if (element->isLeaf())
553
554
555
556
557
558
	  (*(fineVec))[element->getIndex()] = partition;
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }
}