ZoltanPartitioner.cc 13.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
//
// 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.

12
#ifdef HAVE_ZOLTAN
13

14
15
16
17
18
19
20
21
22
#include <boost/lexical_cast.hpp>
#include "parallel/ZoltanPartitioner.h"
#include "Traverse.h"
#include "ElInfo.h"

namespace AMDiS {

  ZoltanPartitioner::ZoltanPartitioner(MPI::Intracomm *comm)
    : MeshPartitioner(comm),
23
24
      zoltan(*comm),
      elWeights(NULL)
Thomas Witkowski's avatar
Thomas Witkowski committed
25
  {}
26
27


28
  bool ZoltanPartitioner::partition(map<int, double> &weights,
29
30
31
32
				    PartitionMode mode)
  {
    FUNCNAME("ZoltanPartitioner::partition()");

Thomas Witkowski's avatar
Thomas Witkowski committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    if (!boxPartitioning) {
      zoltan.Set_Num_Obj_Fn(ZoltanFunctions::getNumObj, this);
      zoltan.Set_Obj_List_Fn(ZoltanFunctions::getObjectList, this);
      zoltan.Set_Num_Geom_Fn(ZoltanFunctions::getNumGeom, this);
      zoltan.Set_Geom_Multi_Fn(ZoltanFunctions::getGeomMulti, this);
      zoltan.Set_Num_Edges_Multi_Fn(ZoltanFunctions::getNumEdgeMulti, this);
      zoltan.Set_Edge_List_Multi_Fn(ZoltanFunctions::getEdgeListMulti, this);
    } else {
      zoltan.Set_Num_Obj_Fn(ZoltanFunctions::box_getNumObj, this);
      zoltan.Set_Obj_List_Fn(ZoltanFunctions::box_getObjectList, this);
      zoltan.Set_Num_Geom_Fn(ZoltanFunctions::box_getNumGeom, this);
      zoltan.Set_Geom_Multi_Fn(ZoltanFunctions::box_getGeomMulti, this);
      zoltan.Set_Num_Edges_Multi_Fn(ZoltanFunctions::box_getNumEdgeMulti, this);
      zoltan.Set_Edge_List_Multi_Fn(ZoltanFunctions::box_getEdgeListMulti, this);
    }

49
50
51
    if (mode != INITIAL)
      elWeights = &weights;

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    using boost::lexical_cast;

    int changes;
    int nGid, nLid;

    int nImportEls;   
    ZOLTAN_ID_PTR import_global_ids, import_local_ids;
    int *import_procs;
    int *import_to_part;

    int nExportEls;
    ZOLTAN_ID_PTR export_global_ids, export_local_ids;
    int *export_procs;
    int *export_to_part;

67
68
    if (mode == INITIAL) {
      zoltan.Set_Param("LB_APPROACH", "PARTITION");
Thomas Witkowski's avatar
Thomas Witkowski committed
69
70
      if (boxPartitioning)
	zoltan.Set_Param("LB_METHOD", "GRAPH");
71
72
73
74
75
    } else {
       zoltan.Set_Param("LB_APPROACH", "REPARTITION");
       zoltan.Set_Param("LB_METHOD", "GRAPH");
    }

76
77
78
79
80
81
82
83
    zoltan.Set_Param("OBJ_WEIGHT_DIM", "1");

#if (DEBUG != 0)
    zoltan.Set_Param("DEBUG_LEVEL", "1");
#else
    zoltan.Set_Param("DEBUG_LEVEL", "0");
#endif

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    int err = zoltan.LB_Partition(changes, nGid, nLid, 
				  nImportEls,
				  import_global_ids,
				  import_local_ids,
				  import_procs,
				  import_to_part,
				  nExportEls,
				  export_global_ids,
				  export_local_ids,
				  export_procs,
				  export_to_part);
    
    recvElements.clear();
    sendElements.clear();

    if (err == ZOLTAN_OK && changes != 0) {   
      if (nImportEls > 0) {
	for (int i = 0; i < nImportEls; i++) {
	  int recvElIndex = import_global_ids[i];
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
104
105
106
107
108
109
110
111
112
113
114
	  if (!boxPartitioning) {
	    elementInRank[recvElIndex] = true;
	    recvElements[import_procs[i]].push_back(recvElIndex);
	  } else {
	    set<int>& box = boxSplitting[recvElIndex];

	    for (set<int>::iterator it = box.begin(); it != box.end(); ++it) {
	      elementInRank[*it] = true;
	      recvElements[import_procs[i]].push_back(*it);
	    }
	  }
115
116
117
118
119
120
121
	}
      }
      
      if (nExportEls > 0) {
	for (int i = 0; i < nExportEls; i++) {
	  int sendElIndex = export_global_ids[i];
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
122
123
124
125
126
127
128
129
130
131
132
	  if (!boxPartitioning) {
	    elementInRank[sendElIndex] = false;
	    sendElements[export_procs[i]].push_back(sendElIndex);
	  } else {
	    set<int>& box = boxSplitting[sendElIndex];

	    for (set<int>::iterator it = box.begin(); it != box.end(); ++it) {
	      elementInRank[*it] = false;
	      sendElements[export_procs[i]].push_back(*it);
	    }
	  }
133
134
	}      
      }
135
136

      createPartitionMap();
137
138
139
140
141
    }

    zoltan.LB_Free_Part(&import_global_ids, &import_local_ids, 
			&import_procs, &import_to_part);
    zoltan.LB_Free_Part(&export_global_ids, &export_local_ids, 
142
143
			&export_procs, &export_to_part);    
    elWeights = NULL;
144
145
146
147
148
149
150
151

    if (err != ZOLTAN_OK)
      return false;

    return true;
  }


152
  void ZoltanPartitioner::createPartitionMap()
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  {
    FUNCNAME("ZoltanPartitioner::getPartitionMap()");

    int mpiSize = mpiComm->Get_size();

    vector<int> localElements;
    for (map<int, bool>::iterator it = elementInRank.begin(); 
	 it != elementInRank.end(); ++it)
      if (it->second)
	localElements.push_back(it->first);
 
    int nLocalElements = localElements.size();
    vector<int> nPartitionElements(mpiSize);
    vector<int> elDist(mpiSize + 1);
    mpiComm->Allgather(&nLocalElements, 1, MPI_INT, &(elDist[1]), 1, MPI_INT);

    elDist[0] = 0;
    nPartitionElements[0] = elDist[1];

    for (int i = 2; i <= mpiSize; i++) {
      nPartitionElements[i - 1] = elDist[i];
      elDist[i] += elDist[i - 1];
    }

    int nOverallElements = elDist[mpiSize];

    TEST_EXIT_DBG(nOverallElements == static_cast<int>(elementInRank.size()))
      ("Number of elements differs: %d %d!\n", nOverallElements, elementInRank.size());

    vector<int> partitionElements(nOverallElements);

    // distribute partition elements
    mpiComm->Allgatherv(&(localElements[0]),
			nLocalElements, 
			MPI_INT, 
			&(partitionElements[0]), 
			&(nPartitionElements[0]), 
			&(elDist[0]),
			MPI_INT);

    // fill partitionMap
194
    partitionMap.clear();
195
196
197
198
199
200
    for (int i = 0; i < mpiSize; i++)
      for (int j = 0; j < nPartitionElements[i]; j++)
	partitionMap[partitionElements[elDist[i] + j]] = i;
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
201
202
203
204

  // === Zoltan query function for "normal" element partitioning. ===

  
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  int ZoltanFunctions::getNumObj(void *data, int *ierr)
  {
    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    map<int, bool>& elInRank = zoltan->getElementInRank();

    int nObjects = 0;
    for (map<int, bool>::iterator it = elInRank.begin(); it != elInRank.end(); ++it)
      if (it->second)
	nObjects++;

    *ierr = ZOLTAN_OK;

    return nObjects;
  }


  void ZoltanFunctions::getObjectList(void *data, 
				      int sizeGid, 
				      int sizeLid,
				      ZOLTAN_ID_PTR globalId, 
				      ZOLTAN_ID_PTR localId,
				      int wgt_dim, 
				      float *obj_wgts, 
				      int *ierr)
  {
    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    map<int, bool>& elInRank = zoltan->getElementInRank();
    int localCounter = 0;

    for (map<int, bool>::iterator it = elInRank.begin(); it != elInRank.end(); ++it) {
      if (it->second) {
	globalId[localCounter] = it->first;
	localId[localCounter] = localCounter;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	
	if (wgt_dim > 0) {
	  TEST_EXIT_DBG(wgt_dim == 1)("Should not happen!\n");

	  if (zoltan->elWeights) {
	    TEST_EXIT_DBG(zoltan->elWeights->count(it->first))
	      ("No element weight for element index %d\n", it->first);
	    
	    obj_wgts[localCounter] = static_cast<float>((*zoltan->elWeights)[it->first]);
	  } else {
	    obj_wgts[localCounter] = 1.0;
	  }
	}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
	localCounter++;
      }
    }

    *ierr = ZOLTAN_OK;
  }


  int ZoltanFunctions::getNumGeom(void *data, int *ierr)
  {
    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    *ierr = ZOLTAN_OK;

    return zoltan->getMesh()->getGeo(WORLD);
  }


  void ZoltanFunctions::getGeomMulti(void *data, 
				     int num_gid_entries, 
				     int num_lid_entries, 
				     int num_obj, 
				     ZOLTAN_ID_PTR global_ids, 
				     ZOLTAN_ID_PTR local_ids, 
				     int num_dim, 
				     double *geom_vec, 
				     int *ierr)
  {
    FUNCNAME("ZoltanFunctions::getGeomMulti()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    Mesh *mesh = zoltan->getMesh();

    TEST_EXIT_DBG(num_dim == mesh->getGeo(WORLD))("Should not happen!\n");

    map<int, WorldVector<double> > elIndexToCoords;    
    DimVec<double> bary(mesh->getDim(), DEFAULT_VALUE, 1.0 / mesh->getGeo(VERTEX));
    WorldVector<double> coords;
    TraverseStack stack;
    ElInfo *elInfo = 
      stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL | Mesh::FILL_COORDS);
    while (elInfo) {
      elInfo->coordToWorld(bary, coords);
      elIndexToCoords[elInfo->getElement()->getIndex()] = coords;

      elInfo = stack.traverseNext(elInfo);
    }

    int c = 0;
    for (int i = 0; i < num_obj; i++) {
      int elIndex = global_ids[i];

      TEST_EXIT_DBG(elIndexToCoords.count(elIndex))("Should not happen!\n");
      
      for (int j = 0; j < num_dim; j++)
	geom_vec[c++] = elIndexToCoords[elIndex][j];
    }

    *ierr = ZOLTAN_OK;
  }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334


  void ZoltanFunctions::getNumEdgeMulti(void *data, 
					int num_gid_entries, 
					int num_lid_entries, 
					int num_obj, 
					ZOLTAN_ID_PTR global_ids, 
					ZOLTAN_ID_PTR local_ids, 
					int *num_edges, 
					int *ierr)
  {
    FUNCNAME("ZoltanFunctions::getNumEdgeMulti()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;

    for (int i = 0; i < num_obj; i++) {
      TEST_EXIT_DBG(zoltan->elNeighbours.count(global_ids[i]))("Should not happen!\n");

      num_edges[i] = zoltan->elNeighbours[global_ids[i]].size();
    }

    *ierr = ZOLTAN_OK;
  }

335
  
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  void ZoltanFunctions::getEdgeListMulti(void *data, 
					 int num_gid_entries, 
					 int num_lid_entries, 
					 int num_obj, 
					 ZOLTAN_ID_PTR global_ids, 
					 ZOLTAN_ID_PTR local_ids, 
					 int *num_edges, 
					 ZOLTAN_ID_PTR nbor_global_id, 
					 int *nbor_procs, 
					 int wgt_dim, 
					 float *ewgts, 
					 int *ierr)
  {
    FUNCNAME("ZoltanFunctions::getEdgeListMulti()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;

    int c = 0;
    for (int i = 0; i < num_obj; i++) {
      TEST_EXIT_DBG(static_cast<int>(zoltan->elNeighbours[global_ids[i]].size()) == 
		    num_edges[i])
	("Wrong sizes for global el index %d: %d %d\n",
	 global_ids[i],
	 zoltan->elNeighbours[global_ids[i]].size(),
	 num_edges[i]);

      for (int j = 0; j < num_edges[i]; j++) {
	int neighIndex = zoltan->elNeighbours[global_ids[i]][j];
	nbor_global_id[c] = neighIndex;
	nbor_procs[c] = zoltan->partitionMap[neighIndex];
	c++;
      }
    }


    TEST_EXIT_DBG(wgt_dim == 0)("Edge weights are not handled yet!\n");

    *ierr = ZOLTAN_OK;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537


  // === Zoltan query functions for box partitioning. ===

  int ZoltanFunctions::box_getNumObj(void *data, int *ierr)
  {
    FUNCNAME("ZoltanFunctions::box_getNumObj()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    map<int, bool>& elInRank = zoltan->getElementInRank();

    int nObjects = 0;
    for (map<int, bool>::iterator it = elInRank.begin(); it != elInRank.end(); ++it)
      if (it->second)
	nObjects++;

    TEST_EXIT_DBG(nObjects % 6 == 0)("Should not happen!\n");

    nObjects = nObjects / 6;

    *ierr = ZOLTAN_OK;

    return nObjects;
  }


  void ZoltanFunctions::box_getObjectList(void *data, 
					  int sizeGid, 
					  int sizeLid,
					  ZOLTAN_ID_PTR globalId, 
					  ZOLTAN_ID_PTR localId,
					  int wgt_dim, 
					  float *obj_wgts, 
					  int *ierr)
  {
    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    map<int, bool>& elInRank = zoltan->getElementInRank();
    set<int> boxes;
    map<int, double> boxWeight;

    for (map<int, bool>::iterator it = elInRank.begin(); it != elInRank.end(); ++it)
      if (it->second) {
	int boxId = zoltan->elInBox[it->first];
	boxes.insert(boxId);
	
	if (zoltan->elWeights) {
	  TEST_EXIT_DBG(zoltan->elWeights->count(it->first))
	    ("No element weight for element index %d\n", it->first);
	  
	  boxWeight[boxId] += (*zoltan->elWeights)[it->first];
	}
      }

    int localCounter = 0;
    for (set<int>::iterator it = boxes.begin(); it != boxes.end(); ++it) {
      globalId[localCounter] = *it;
      localId[localCounter] = localCounter;
	
      if (wgt_dim > 0) {
	TEST_EXIT_DBG(wgt_dim == 1)("Should not happen!\n");
	
	if (zoltan->elWeights)
	  obj_wgts[localCounter] = static_cast<float>(boxWeight[*it]);
	else
	  obj_wgts[localCounter] = 1.0;	
      }
      
      localCounter++;
    }

    *ierr = ZOLTAN_OK;
  }


  int ZoltanFunctions::box_getNumGeom(void *data, int *ierr)
  {
    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;
    *ierr = ZOLTAN_OK;

    return zoltan->getMesh()->getGeo(WORLD);
  }


  void ZoltanFunctions::box_getGeomMulti(void *data, 
					 int num_gid_entries, 
					 int num_lid_entries, 
					 int num_obj, 
					 ZOLTAN_ID_PTR global_ids, 
					 ZOLTAN_ID_PTR local_ids, 
					 int num_dim, 
					 double *geom_vec, 
					 int *ierr)
  {
    FUNCNAME("ZoltanFunctions::box_getGeomMulti()");

    ERROR_EXIT("Not yet supported!\n");
  }


  void ZoltanFunctions::box_getNumEdgeMulti(void *data, 
					    int num_gid_entries, 
					    int num_lid_entries, 
					    int num_obj, 
					    ZOLTAN_ID_PTR global_ids, 
					    ZOLTAN_ID_PTR local_ids, 
					    int *num_edges, 
					    int *ierr)
  {
    FUNCNAME("ZoltanFunctions::box_getNumEdgeMulti()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;

    for (int i = 0; i < num_obj; i++) {
      TEST_EXIT_DBG(zoltan->boxNeighbours.count(global_ids[i]))("Should not happen!\n");

      num_edges[i] = zoltan->boxNeighbours[global_ids[i]].size();
    }

    *ierr = ZOLTAN_OK;
  }


  void ZoltanFunctions::box_getEdgeListMulti(void *data, 
					     int num_gid_entries, 
					     int num_lid_entries, 
					     int num_obj, 
					     ZOLTAN_ID_PTR global_ids, 
					     ZOLTAN_ID_PTR local_ids, 
					     int *num_edges, 
					     ZOLTAN_ID_PTR nbor_global_id, 
					     int *nbor_procs, 
					     int wgt_dim, 
					     float *ewgts, 
					     int *ierr)
  {
    FUNCNAME("ZoltanFunctions::box_getEdgeListMulti()");

    ZoltanPartitioner* zoltan = (ZoltanPartitioner*)data;

    int c = 0;
    for (int i = 0; i < num_obj; i++) {
      TEST_EXIT_DBG(static_cast<int>(zoltan->boxNeighbours[global_ids[i]].size()) == 
		    num_edges[i])
	("Wrong sizes for global el index %d: %d %d\n",
	 global_ids[i],
	 zoltan->boxNeighbours[global_ids[i]].size(),
	 num_edges[i]);

      set<int> &neigh = zoltan->boxNeighbours[global_ids[i]];
      for (set<int>::iterator it = neigh.begin(); it != neigh.end(); ++it) {
	nbor_global_id[c] = *it;
	nbor_procs[c] = zoltan->partitionMap[*(zoltan->boxSplitting[*it].begin())];
	c++;
      }
    }

    TEST_EXIT_DBG(wgt_dim == 0)("Edge weights are not handled yet!\n");

    *ierr = ZOLTAN_OK;
  }


538
}
539
540

#endif