ParMetisPartitioner.cc 12.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include <queue>
14
15
16
17
18
19
20
21
22
23
24
#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 {

25
  ParMetisMesh::ParMetisMesh(Mesh *mesh, MPI::Intracomm *comm, 
26
			     std::map<int, bool>& elementInRank,
27
			     std::map<DegreeOfFreedom, DegreeOfFreedom> *mapLocalGlobal)
Thomas Witkowski's avatar
Thomas Witkowski committed
28
29
    : dim(mesh->getDim()),
      nElements(0),
30
      mpiComm(comm)
31
32
  {
    FUNCNAME("ParMetisMesh::ParMetisMesh()");
33
34

    int mpiSize = mpiComm->Get_size();
35
36
37
38
39
    int nodeCounter = 0;
    int elementCounter = 0;
    int dow = Global::getGeo(WORLD);

    TraverseStack stack;
40
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
41
    while (elInfo) {
42
      if (elementInRank[elInfo->getElement()->getIndex()])
43
	elementCounter++;      
44
45
46
47

      elInfo = stack.traverseNext(elInfo);
    }

48
    nElements = elementCounter;
49

50
    TEST_EXIT(nElements > 0)("No elements in ParMETIS mesh!\n");
51
52

    // allocate memory
Thomas Witkowski's avatar
Thomas Witkowski committed
53
    eptr = new int[nElements + 1];
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
54
    eind = new int[nElements * (dim + 1)];
Thomas Witkowski's avatar
Thomas Witkowski committed
55
56
    elmdist = new int[mpiSize + 1];
    elem_p2a = new int[nElements];
57

58
    if (dim == dow)
Thomas Witkowski's avatar
Thomas Witkowski committed
59
      xyz = new float[nElements * dim];
60
61
    else
      xyz = NULL;    
62

Thomas Witkowski's avatar
Thomas Witkowski committed
63
    eptr[0] = 0;
64

Thomas Witkowski's avatar
Thomas Witkowski committed
65
66
67
    int *ptr_eptr = eptr + 1;
    int *ptr_eind = eind;
    float *ptr_xyz = xyz;
68
69
    
    // gather element numbers and create elmdist
70
    mpiComm->Allgather(&nElements, 1, MPI_INT, elmdist + 1, 1, MPI_INT);
71

72
    elmdist[0] = 0;
73
    for (int i = 2; i < mpiSize + 1; i++)
74
      elmdist[i] += elmdist[i - 1];
75
76

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

    elementCounter = 0;

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

      // if element in partition
88
      if (elementInRank[index]) {
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
	  if (mapLocalGlobal)
101
	    *ptr_eind = (*mapLocalGlobal)[element->getDof(i, 0)];
102
	  else
103
	    *ptr_eind = element->getDof(i, 0);
104

105
106
107
108
	  ptr_eind++;
	}

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

	elementCounter++;
      }
119

120
121
122
123
      elInfo = stack.traverseNext(elInfo);
    }
  }

124

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

143

144
  ParMetisGraph::ParMetisGraph(ParMetisMesh *parMesh,
145
			       MPI::Intracomm *comm,
146
			       int ncommonnodes)
147
    : parMetisMesh(parMesh)
148
  {
149
150
151
152
153
    FUNCNAME("ParMetisGraph::ParMetisGraph()");

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

154
155
    int numflag = 0;

156
157
    if (ncommonnodes == -1) 
      ncommonnodes = parMetisMesh->getDim();
158

159
    MPI_Comm tmpComm = MPI_Comm(*comm);
160

161
162
163
    ParMETIS_V3_Mesh2Dual(parMetisMesh->getElementDist(),
			  parMetisMesh->getElementPtr(),
			  parMetisMesh->getElementInd(),
164
165
			  &numflag,
			  &ncommonnodes,
Thomas Witkowski's avatar
Thomas Witkowski committed
166
167
			  &xadj,
			  &adjncy,
168
			  &tmpComm);
169
170
  }

171

172
173
  ParMetisGraph::~ParMetisGraph()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
174
175
    free(xadj);
    free(adjncy);
176
177
  }

178

179
180
  void ParMetisPartitioner::createPartitionData() 
  {
181
182
    FUNCNAME("ParMetrisPartitioner::createPartitionData()");

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

188
    // === Create initial partitioning of the AMDiS mesh. ===
189

190
191
    elementInRank.clear();

192
    TraverseStack stack;
193
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
194
    while (elInfo) {
195
196
      Element *element = elInfo->getElement();

197
198
199
200
      if ((element->getIndex() >= mpiRank * elPerRank &&
	   element->getIndex() < (mpiRank + 1) * elPerRank) ||
	  (element->getIndex() >= mpiSize * elPerRank &&
	   mpiRank == mpiSize - 1))
201
	elementInRank[element->getIndex()] = true;
202
      else
203
	elementInRank[element->getIndex()] = false;
204
      
205
206
207
208
      elInfo = stack.traverseNext(elInfo);
    }
  }

209

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

216
    int mpiSize = mpiComm->Get_size();
217
218

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

222
223
    TEST_EXIT_DBG(elementInRank.size() != 0)("Should not happen!\n");

224
    parMetisMesh = new ParMetisMesh(mesh, mpiComm, elementInRank, mapLocalGlobal);
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
    float maxWgt = 0.0;
    float *ptr_floatWgts = floatWgts;

234
    TraverseStack stack;
235
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
236
    while (elInfo) {
237
      int index = elInfo->getElement()->getIndex();
238

239
      if (elementInRank[index]) {
240
241
242
243
244
245
246
247
248
249
250
251
	// 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;
252
    mpiComm->Allreduce(&maxWgt, &tmp, 1, MPI_FLOAT, MPI_MAX);
253
254
255
    maxWgt = tmp;

    // === create dual graph ===
256
    ParMetisGraph parMetisGraph(parMetisMesh, mpiComm);
257
258
259
260
261
262

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

Thomas Witkowski's avatar
Thomas Witkowski committed
264
    float *tpwgts = elemWeights ? new float[mpiSize] : NULL;
265
266
267
    float ubvec = 1.05;
    int options[3] = {0, 0, 0}; // default options
    int edgecut = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
268
    int *part = new int[nElements];
269

270
    if (elemWeights) {
Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
      // set tpwgts
      for (int i = 0; i < mpiSize; i++)
273
	tpwgts[i] = 1.0 / nparts;
274

275
      float scale = 10000.0 / maxWgt;
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
      // scale wgts
      for (int i = 0; i < nElements; i++)
278
279
280
	wgts[i] = static_cast<int>(floatWgts[i] * scale);
    }

281
282
    MPI_Comm tmpComm = MPI_Comm(*mpiComm);

283
    switch (mode) {
284
    case INITIAL:
285
      ParMETIS_V3_PartKway(parMetisMesh->getElementDist(),
286
287
			   parMetisGraph.getXAdj(),
			   parMetisGraph.getAdjncy(),
288
			   wgts,
289
			   NULL,
290
			   &wgtflag,
291
292
293
294
295
296
297
298
			   &numflag,
			   &ncon,
			   &nparts,
			   tpwgts,
			   &ubvec,
			   options,
			   &edgecut,
			   part,
299
			   &tmpComm);
300
301
302
      break;
    case ADAPTIVE_REPART:
      {
Thomas Witkowski's avatar
Thomas Witkowski committed
303
304
	int *vsize = new int[nElements];
	for (int i = 0; i < nElements; i++)
305
	  vsize[i] = 1;
306
	ParMETIS_V3_AdaptiveRepart(parMetisMesh->getElementDist(),
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
				   parMetisGraph.getXAdj(),
				   parMetisGraph.getAdjncy(),
				   wgts,
				   NULL,
				   vsize,
				   &wgtflag,
				   &numflag,
				   &ncon,
				   &nparts,
				   tpwgts,
				   &ubvec,
				   &itr,
				   options,
				   &edgecut,
				   part,
322
				   &tmpComm);
323

Thomas Witkowski's avatar
Thomas Witkowski committed
324
	delete [] vsize;
325
326
327
      }
      break;
    case REFINE_PART:
328
      ParMETIS_V3_RefineKway(parMetisMesh->getElementDist(),
329
330
331
332
333
334
335
336
337
338
339
340
341
			     parMetisGraph.getXAdj(),
			     parMetisGraph.getAdjncy(),
			     wgts,
			     NULL,
			     &wgtflag,
			     &numflag,
			     &ncon,
			     &nparts,
			     tpwgts,
			     &ubvec,
			     options,
			     &edgecut,
			     part,
342
			     &tmpComm);
343

344
345
346
347
348
349
350
351
      break;
    default: 
      ERROR_EXIT("unknown partitioning mode\n");
    }

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

352
    if (floatWgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
353
      delete [] floatWgts;
354
    if (wgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
355
      delete [] wgts;
356
    if (tpwgts) 
Thomas Witkowski's avatar
Thomas Witkowski committed
357
358
359
      delete [] tpwgts;

    delete [] part;
360
361
  }

362

363
364
  void ParMetisPartitioner::fillCoarsePartitionVec(std::map<int, int> *partitionVec)
  {
365
366
367
    FUNCNAME("ParMetisPartitioner::fillCoarsePartitionVec()");

    TEST_EXIT_DBG(partitionVec)("no partition vector\n");
368
369
370
371

    partitionVec->clear();

    // update ParMETIS mesh to new partitioning
372
    if (!parMetisMesh)
373
      parMetisMesh = new ParMetisMesh(mesh, mpiComm, elementInRank, mapLocalGlobal);
374

375
376
    int mpiRank = mpiComm->Get_rank();
    int mpiSize = mpiComm->Get_size();
377
    std::vector<int> nPartitionElements(mpiSize);
378
    int *elmdist = parMetisMesh->getElementDist();
Thomas Witkowski's avatar
Thomas Witkowski committed
379

380
    for (int i = 0; i < mpiSize; i++)
381
      nPartitionElements[i] = elmdist[i + 1] - elmdist[i];
382
383

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

388
    std::vector<int> partitionElements(nElements);
389
390

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

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

405

406
407
  void ParMetisPartitioner::distributePartitioning(int *part) 
  {
408
409
    FUNCNAME("ParMetisPartitioner::distributePartitioning()");

410
411
    int mpiSize = mpiComm->Get_size();
    int mpiRank = mpiComm->Get_rank();
412
    int nElements = parMetisMesh->getNumElements();
413

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

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

425

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

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

440
    for (int i = 0; i < mpiSize; i++)
441
442
      partitionPtr[i] = partitionElements + bufferOffset[i];

443
    sendElements.clear();
444
    for (int i = 0; i < nElements; i++) {
445
      int partition = part[i];
446
      int amdisIndex = parMetisMesh->getAMDiSIndex(i);
447

448
449
450
      if (partition != mpiRank)
	sendElements[partition].push_back(amdisIndex);

451
452
453
454
455
      *(partitionPtr[partition]) = amdisIndex;
      ++(partitionPtr[partition]);
    }

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

462
    mpiComm->Alltoallv(partitionElements, 
463
		       nPartitionElements,
464
465
466
		       bufferOffset,
		       MPI_INT,
		       rankElements,
467
		       nRankElements,
468
469
		       recvBufferOffset,
		       MPI_INT);
470
    
471
472
473
474
    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;
475

476
477
    // Create map which stores for each element index on ther partitioning level
    // if the element is in the partition of this rank.
478
    recvElements.clear();
479
    for (int i = 0; i < mpiSize; i++) {
480
      int *rankStart = rankElements + recvBufferOffset[i];
481
      int *rankEnd = rankStart + nRankElements[i];
482
483
      for (int *rankPtr = rankStart; rankPtr < rankEnd; ++rankPtr) {
	elementInRank[*rankPtr] = true;
484
485
	if (i != mpiRank)
	  recvElements[i].push_back(*rankPtr);
486
487
488
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
489
    delete parMetisMesh;
490
    parMetisMesh = NULL;
491

Thomas Witkowski's avatar
Thomas Witkowski committed
492
493
494
495
496
497
498
    delete [] rankElements;
    delete [] nPartitionElements;
    delete [] nRankElements;
    delete [] sumPartitionElements;
    delete [] partitionElements;
    delete [] partitionPtr;
    delete [] bufferOffset;
Thomas Witkowski's avatar
huh    
Thomas Witkowski committed
499
    delete [] recvBufferOffset;
500
501
502
  }

}