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
119
120
	    *ptr_xyz = static_cast<float>(world[i]); 
	    ptr_xyz++;
	  }
	}

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

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

137
  ParMetisGraph::ParMetisGraph(ParMetisMesh *parMesh,
138
			       MPI::Intracomm *comm,
139
			       int ncommonnodes)
140
    : parMetisMesh(parMesh)
141
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
  }

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

165

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

177
178
  void ParMetisPartitioner::createPartitionData() 
  {
179
180
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
181
182
    int nLeaves = mesh->getNumberOfLeaves();
    int elPerRank = nLeaves / mpiSize;
183

184
    // === Create initial partitioning of the AMDiS mesh. ===
185

186
187
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
188
    while (elInfo) {
189
190
191
192
193
194
      Element *element = elInfo->getElement();

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

      PartitionElementData *elData = 
Thomas Witkowski's avatar
Thomas Witkowski committed
195
	new PartitionElementData(element->getElementData());
196
197
      element->setElementData(elData);

198
199
      if (element->getIndex() >= mpiRank * elPerRank &&
	  element->getIndex() < (mpiRank + 1) * elPerRank)
200
	elData->setPartitionStatus(IN);
201
      else
202
	elData->setPartitionStatus(UNDEFINED);
203
      
204
205
206
207
208
209
210
211
      elInfo = stack.traverseNext(elInfo);
    }
  }

  void ParMetisPartitioner::partition(std::map<int, double> *elemWeights,
				      PartitionMode mode,
				      float itr) 
  {
212
    FUNCNAME("ParMetisPartitioner::partition()");
213

214
    int mpiSize = mpiComm->Get_size();
215
216

    // === create parmetis mesh ===
217
    if (parMetisMesh) 
Thomas Witkowski's avatar
Thomas Witkowski committed
218
      delete parMetisMesh;
219
220

    parMetisMesh = new ParMetisMesh(mesh, mpiComm);
221

222
    int nElements = parMetisMesh->getNumElements();
223
224

    // === create weight array ===
Thomas Witkowski's avatar
Thomas Witkowski committed
225
226
    int *wgts = elemWeights ? new int[nElements] : NULL;
    float *floatWgts = elemWeights ? new float[nElements] : NULL;
227
228
229
    float maxWgt = 0.0;
    float *ptr_floatWgts = floatWgts;

230
231
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
232
    while (elInfo) {
233
234
235
236
237
238
      Element *element = elInfo->getElement();

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

239
240
241
      if (partitionData &&
	  partitionData->getPartitionStatus() == IN &&
	  partitionData->getLevel() == 0) {
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
	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;
257
    mpiComm->Allreduce(&maxWgt, &tmp, 1, MPI_FLOAT, MPI_MAX);
258
259
260
    maxWgt = tmp;

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

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

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

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

286
287
    MPI_Comm tmpComm = MPI_Comm(*mpiComm);

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

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

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

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

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

    partitionVec->clear();

    // update ParMETIS mesh to new partitioning
374
375
    if (!parMetisMesh)
      parMetisMesh = new ParMetisMesh(mesh, mpiComm);    
376

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

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

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

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

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

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

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

  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
412
413
    FUNCNAME("ParMetisPartitioner::distributePartitioning()");

414
415
    int mpiSize = mpiComm->Get_size();
    int mpiRank = mpiComm->Get_rank();
416
    int nElements = parMetisMesh->getNumElements();
417

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

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

    // sum up partition elements over all ranks
Thomas Witkowski's avatar
Thomas Witkowski committed
430
    int *sumPartitionElements = new int[mpiSize];
431
    mpiComm->Allreduce(nPartitionElements, sumPartitionElements, mpiSize,
432
		       MPI_INT, MPI_SUM);
433
434
    
    // prepare distribution (fill partitionElements with AMDiS indices)
Thomas Witkowski's avatar
Thomas Witkowski committed
435
    int *bufferOffset = new int[mpiSize];
436
    bufferOffset[0] = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
437
    for (int i = 1; i < mpiSize; i++)
438
      bufferOffset[i] = bufferOffset[i - 1] + nPartitionElements[i - 1];
439

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

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

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

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

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

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

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

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

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

      elInfo = stack.traverseNext(elInfo);
    }

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

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

  void ParMetisPartitioner::descendPartitionData(Element *element) 
  {
517
518
    FUNCNAME("ParMetisPartitioner::descendPartitionData()");

519
    if (!element->isLeaf()) {
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
545
546
      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;
547
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
548
    while (elInfo) {
549
550
551
      Element *element = elInfo->getElement();
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));
552
      if (partitionData) {
553
	if (partitionData->getLevel() == 0)
554
	  partition = (*(coarseVec))[element->getIndex()];
555
	if (element->isLeaf())
556
557
558
559
560
561
	  (*(fineVec))[element->getIndex()] = partition;
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }
}