Distribution.cpp 34.3 KB
Newer Older
1
2
3
4
5
6
7
#include "Distribution.hpp"
#include "Parallel.hpp"

#include <algorithm>
#include <list>
#include <map>

8

9
void Distribution::DistributeMesh(Mesh &mesh) {
10
  mout.StartBlock("Distribute");
11
  int startDist = (int) distributedCells.size();
12
13
14
  init(mesh);
  int rekDepth = int(log2(PPM->Size(commSplit)));

15
16
  if (mesh.IsSTMesh()) {
    DistributeSTMesh(mesh);
17
18
  } else {
    if (_distName == "Stripes" || _distName == "Stripes_x")
19
      Stripes_x(rekDepth, startDist, cells.size());
20
    else if (_distName == "Stripes_y")
21
      Stripes_y(rekDepth, startDist, cells.size());
22
    else if (_distName == "Stripes_z")
23
      Stripes_z(rekDepth, startDist, cells.size());
24
25
26

      // Recursive coordinate bisection
    else if (_distName == "RCB" || _distName == "RCBx")
27
      RCB_x(rekDepth, startDist, cells.size());
28
    else if (_distName == "RCBy")
29
      RCB_y(rekDepth, startDist, cells.size());
30
    else if (_distName == "RCBz")
31
      RCB_z(rekDepth, startDist, cells.size());
32
33

    else if (_distName == "RCBG" || _distName == "RCBGeom_x" || _distName == "RCBGx")
34
      RCBGeom_x(rekDepth, startDist, cells.size());
35
    else if (_distName == "RCBGy" || _distName == "RCBGeom_y")
36
      RCBGeom_y(rekDepth, startDist, cells.size());
37
    else if (_distName == "RCBGz" || _distName == "RCBGeom_z")
38
      RCBGeom_z(rekDepth, startDist, cells.size());
39
40
41
42

      // Dimension could be considered one time,
      // for now it does the same as RCB.
    else if (_distName == "RCB2D" || _distName == "RCB2Dx")
43
      RCB2D_x(rekDepth, startDist, cells.size());
44
    else if (_distName == "RCB2Dy")
45
      RCB2D_y(rekDepth, startDist, cells.size());
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

    else if (_distName == "KAFFPA") THROW("Kaffpa is not included anymore")
      // See https://git.scc.kit.edu/mpp/mpp/-/blob/2.0.0/src/Distribution.C#L1517
      // See https://git.scc.kit.edu/mpp/mpp/-/issues/124
      // See https://github.com/KaHIP/KaHIP

      // Recursive inertia bisection
    else if (_distName == "RIB")
      THROW("Recursive inertia bisection is not implemented anymore")
      // See https://git.scc.kit.edu/mpp/mpp/-/blob/2.0.0/src/Distribution.C#L1587

      // These cases should be removed at some point
    else if (_distName == "Stripes_Old")
      Stripes_Old();
    else if (_distName == "RCB_Old" || _distName == "RCB2")
61
      RCB_Old(rekDepth, startDist, cells.size());
62
    else if (_distName == "RCB_Geom_Old")
63
      RCB_Geom_Old(rekDepth, startDist, cells.size(), 'x');
64
65
66
67

    else if (_distName == "NoDistribution")
      distribute = false;
    else Exit(_distName + " not implemented")
68
  }
69
70

  if (distribute) {
71

72
    for (int i = startDist; i < cells.size(); ++i) {
73
74
75
      distributedCells.emplace_back(cells[i]());
    }

76
77
78
79
80
81
82
83
84
85
86
87
    for (int i = 0; i < cells.size(); ++i)
      markedCells[procsForCells[i]].push_back(cells[i]);

    Communicate(mesh);
  }

  int cellCount = mesh.CellCount();
  pvout(1) << cellCount << " cells on logging process. "
           << mesh.CellCountGeometry() << " cells on all processes."
           << endl;

  if (PPM->Min(cellCount, commSplit) == 0)
88
    VerboseWarning("At least one processor has no cells!", 1);
89

90
  emptyData(clearDistributed);
91
  mout.EndBlock(true);
92
93
}

94
void Distribution::Communicate(Mesh &mesh) {
95
  vector<std::list<bnd_face>> MarkedBnd(PPM->Size(commSplit));
96
97
  for (int q = 0; q < PPM->Size(commSplit); ++q) {
    for (auto c = markedCells[q].begin(); c != markedCells[q].end(); ++c) {
98
      for (int i = 0; i < c->Corners(); ++i) {
99
        mesh.procSets.Add(c->Corner(i), q);
100
      }
101
102
      for (int i = 0; i < c->SpaceCell().Edges(); ++i)
        mesh.procSets.Add(c->SpaceCell().Edge(i), q);
103
104
105
106
107
108
109
      for (int i = 0; i < c->Faces(); ++i) {
        Point z = c->Face(i);
        mesh.procSets.Add(z, q);
        bnd_face b = mesh.find_bnd_face(z);
        if (b != mesh.bnd_faces_end())
          MarkedBnd[q].push_back(b);
      }
110
    }
111
  }
112
113
114
115
116
117
118
119
120
  for (identifyset is = mesh.identifysets(); is != mesh.identifysets_end(); ++is) {
    if (!is.master()) continue;
    procset p = mesh.find_procset(is());
    if (p == mesh.procsets_end()) continue;
    for (int j = 0; j < is.size(); ++j) {
      procset q = mesh.find_procset(is[j]);
      if (q == mesh.procsets_end()) continue;
      for (int i = 0; i < q.size(); ++i)
        mesh.procSets.Append(is(), q[i]);
121
    }
122
123
124
125
126
127
128
129
130
131
132
133
    for (int j = 0; j < is.size(); ++j)
      mesh.procSets.Copy(p, is[j]);
  }
  for (identifyset is = mesh.identifysets(); is != mesh.identifysets_end(); ++is) {
    if (is.master()) continue;
    procset p = mesh.find_procset(is());
    if (p == mesh.procsets_end()) continue;
    for (int j = 0; j < is.size(); ++j) {
      procset q = mesh.find_procset(is[j]);
      if (q == mesh.procsets_end()) continue;
      for (int i = 0; i < q.size(); ++i)
        mesh.procSets.Append(is(), q[i]);
134
    }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    for (int j = 0; j < is.size(); ++j)
      mesh.procSets.Copy(p, is[j]);
  }
  for (identifyset is = mesh.identifysets(); is != mesh.identifysets_end(); ++is) {
    if (!is.master()) continue;
    procset p = mesh.find_procset(is());
    for (int j = 0; j < is.size(); ++j)
      mesh.procSets.Copy(p, is[j]);
  }
  ExchangeBuffer exBuffer(commSplit);
  for (identifyset is = mesh.identifysets(); is != mesh.identifysets_end(); ++is) {
    procset p = mesh.find_procset(is());
    if (p == mesh.procsets_end()) continue;
    for (int i = 0; i < p.size(); ++i) {
      if (p[i] == PPM->Proc(commSplit)) continue;
      exBuffer.Send(p[i]) << short(is.size()) << is();
      for (int j = 0; j < is.size(); ++j)
        exBuffer.Send(p[i]) << is[j];
153
    }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  }
  mesh.procSets.RemoveSingle();
  for (int q = 0; q < PPM->Size(commSplit); ++q) {
    if (PPM->Proc(commSplit) == q) continue;
    if (markedCells[q].size() == 0) continue;
    exBuffer.Send(q) << short(0);
  }
  for (procset p = mesh.procsets(); p != mesh.procsets_end(); ++p)
    for (int i = 0; i < p.size(); ++i)
      if (p[i] != PPM->Proc(commSplit)) {
        short m = 1;
        face f = mesh.find_face(p());
        if (f != mesh.faces_end()) ++m;
        exBuffer.Send(p[i]) << m << p() << *p;
        if (m > 1) exBuffer.Send(p[i]) << *f;
      }
  for (int q = 0; q < PPM->Size(commSplit); ++q) {
    if (PPM->Proc(commSplit) == q) continue;
    if (markedCells[q].size() == 0) continue;
    exBuffer.Send(q) << short(0);
    exBuffer.Send(q) << int(MarkedBnd[q].size());
175
    for (auto b = MarkedBnd[q].begin(); b != MarkedBnd[q].end(); ++b)
176
      exBuffer.Send(q) << *b;
177
    for (auto c = markedCells[q].begin(); c != markedCells[q].end(); ++c) {
178
      exBuffer.Send(q) << *c;
179
#if USE_DATAMESH
180
181
182
183
184
      for (int i = 0; i < c->Corners(); i++) {
        exBuffer.Send(q) << mesh.find_vertex(c->Corner(i)).GetData();
      }
#endif
      mesh.RemoveCell(*c);
185
    }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  }
  exBuffer.Communicate();
  for (short q = 0; q < PPM->Size(commSplit); ++q) {
    if (exBuffer.Receive(q).Size() == 0) continue;
    short m;
    exBuffer.Receive(q) >> m;
    while (m) {
      Point x;
      exBuffer.Receive(q) >> x;
      for (int i = 0; i < m; ++i) {
        Point y;
        exBuffer.Receive(q) >> y;
        mesh.identifySets.Insert(x, y);
      }
      exBuffer.Receive(q) >> m;
201
    }
202
203
204
205
206
207
208
209
210
211
212
213
    exBuffer.Receive(q) >> m;
    while (m) {
      Point z;
      ProcSet P(mesh.CommSplit());
      exBuffer.Receive(q) >> z >> P;
      mesh.procSets.Insert(z, P);
      if (m > 1) {
        Face F;
        exBuffer.Receive(q) >> F;
        mesh.InsertFace(z, F);
      }
      exBuffer.Receive(q) >> m;
214
    }
215
216
    int n;
    exBuffer.Receive(q) >> n;
217
218
    for (int i = 0; i < n; ++i)
      exBuffer.Receive(q) >> mesh.bnd_ref();
219
220
221
222
223
    while (exBuffer.Receive(q).size() < exBuffer.Receive(q).Size())
      exBuffer.Receive(q) >> mesh;
  }
  exBuffer.ClearBuffers();
  mesh.procSets.Clean();
224
225
  for (int q = 0; q < PPM->Size(commSplit); ++q)
    mesh.procSets.Add(Infty, q);
226
  mesh.FinishParallel();
227
}
228

229
int Distribution::setProcsForCellsAndReturnMid(int rekDepth, int begin, int end) {
230
231
232
233
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i)
    procsForCells[i] += 1 << (rekDepth - 1); // 2^(rekDepth - 1)
  return mid;
234
}
235

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
bool Less_x(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P[2] < Q[2] - GeometricTolerance) return true;
  return false;
}

bool Less_y(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  if (P[0] < Q[0] - GeometricTolerance) return true;
  return false;
}

bool Less_z(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P[1] < Q[1] - GeometricTolerance) return true;
  return false;
}

bool Less_t(const cell &c0, const cell &c1) {
  return (c0() < c1());
}

bool TLess_x(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P.t() < Q.t() - GeometricTolerance) return true;
  if (P.t() > Q.t() + GeometricTolerance) return false;
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  return false;
}
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
311
312
313
bool TLess_y(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P.t() < Q.t() - GeometricTolerance) return true;
  if (P.t() > Q.t() + GeometricTolerance) return false;
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  return false;
}

bool TLess_z(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P.t() < Q.t() - GeometricTolerance) return true;
  if (P.t() > Q.t() + GeometricTolerance) return false;
  return false;
}
314

315
316
317
318
319
320
321
322
323
324
325
326
327
bool TLess_t(const cell &c0, const cell &c1) {
  const Point &P = c0();
  const Point &Q = c1();
  if (P.t() < Q.t() - GeometricTolerance) return true;
  if (P.t() > Q.t() + GeometricTolerance) return false;
  if (P[0] < Q[0] - GeometricTolerance) return true;
  if (P[0] > Q[0] + GeometricTolerance) return false;
  if (P[1] < Q[1] - GeometricTolerance) return true;
  if (P[1] > Q[1] + GeometricTolerance) return false;
  if (P[2] < Q[2] - GeometricTolerance) return true;
  if (P[2] > Q[2] + GeometricTolerance) return false;
  return false;
}
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
std::function<bool(const cell &, const cell &)> GetLess(char var) {
  switch (var) {
    case 'x':
      return Less_x;
    case 'y':
      return Less_y;
    case 'z':
      return Less_z;
    case 't':
      return Less_t;
    default:
      THROW("Dimension " + std::to_string(var) + " not known");
  }
}

344
void Distribution::RCB_x(int rekDepth, int begin, int end) {
345
346
347
348
349
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_x);
  int mid = setProcsForCellsAndReturnMid(rekDepth, begin, end);
  RCB_y(rekDepth - 1, begin, mid);
  RCB_y(rekDepth - 1, mid, end);
350
351
352
}

void Distribution::RCB_y(int rekDepth, int begin, int end) {
353
354
355
356
357
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_y);
  int mid = setProcsForCellsAndReturnMid(rekDepth, begin, end);
  RCB_z(rekDepth - 1, begin, mid);
  RCB_z(rekDepth - 1, mid, end);
358
359
360
}

void Distribution::RCB_z(int rekDepth, int begin, int end) {
361
362
363
364
365
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_z);
  int mid = setProcsForCellsAndReturnMid(rekDepth, begin, end);
  RCB_x(rekDepth - 1, begin, mid);
  RCB_x(rekDepth - 1, mid, end);
366
367
}

368
369
370
371
372
373
374
375
//void Distribution::RCB_t(int rekDepth, int begin, int end) {
//  if (rekDepth == 0) return;
//  sort(cells.begin() + begin, cells.begin() + end, Less_t);
//  int mid = setProcsForCellsAndReturnMid(rekDepth, begin, end);
//  RCB_t(rekDepth - 1, begin, mid);
//  RCB_t(rekDepth - 1, mid, end);
//}

376
void Distribution::Stripes_x(int rekDepth, int begin, int end) {
377
378
379
380
381
382
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_x);
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCB_x(rekDepth - 1, begin, mid);
  RCB_x(rekDepth - 1, mid, end);
383
384
385
}

void Distribution::Stripes_y(int rekDepth, int begin, int end) {
386
387
388
389
390
391
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_y);
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCB_y(rekDepth - 1, begin, mid);
  RCB_y(rekDepth - 1, mid, end);
392
393
394
}

void Distribution::Stripes_z(int rekDepth, int begin, int end) {
395
396
397
398
399
400
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_z);
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCB_z(rekDepth - 1, begin, mid);
  RCB_z(rekDepth - 1, mid, end);
401
402
}

403
404
405
406
407
408
409
410
411
412
//void Distribution::Stripes_t(int rekDepth, int begin, int end) {
//  if (rekDepth == 0) return;
//  sort(cells.begin() + begin, cells.begin() + end, Less_t);
//  int mid = begin + (end - begin) / 2;
//  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
//  RCB_t(rekDepth - 1, begin, mid);
//  RCB_t(rekDepth - 1, mid, end);
//}


413
void Distribution::RCB2D_x(int rekDepth, int begin, int end) {
414
415
416
417
418
419
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_x);
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCB2D_y(rekDepth - 1, begin, mid);
  RCB2D_y(rekDepth - 1, mid, end);
420
}
421

422
void Distribution::RCB2D_y(int rekDepth, int begin, int end) {
423
424
425
426
427
428
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_y);
  int mid = begin + (end - begin) / 2;
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCB2D_x(rekDepth - 1, begin, mid);
  RCB2D_x(rekDepth - 1, mid, end);
429
430
}

431
double Distribution::getMidOfLowestAndHighestCoord(int begin, int end, int coordinate) {
432
433
434
435
436
437
438
439
440
  double low = infty;
  double high = -infty;
  for (int i = begin; i < end; ++i) {
    if (low > cells[i]()[coordinate])
      low = cells[i]()[coordinate];
    if (high < cells[i]()[coordinate])
      high = cells[i]()[coordinate];
  }
  return (low + high) / 2.0;
441
442
443
}

void Distribution::RCBGeom_x(int rekDepth, int begin, int end) {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_x);
  double midlh = getMidOfLowestAndHighestCoord(begin, end, 0);
  int mid = end;
  for (int i = begin; i < end; ++i) {
    bool con = false;
    if (midlh > cells[i]()[0]) con = true;
    if (con) continue;
    mid = i;
    break;
  }
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCBGeom_y(rekDepth - 1, begin, mid);
  RCBGeom_y(rekDepth - 1, mid, end);
458
459
460
}

void Distribution::RCBGeom_y(int rekDepth, int begin, int end) {
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_y);
  auto midlh = getMidOfLowestAndHighestCoord(begin, end, 1);
  int mid = end;
  for (int i = begin; i < end; ++i) {
    bool con = false;
    if (midlh > cells[i]()[1]) con = true;
    if (con) continue;
    mid = i;
    break;
  }
  for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
  RCBGeom_x(rekDepth - 1, begin, mid);
  RCBGeom_x(rekDepth - 1, mid, end);
475
//    RCBGeom_z(rekDepth - 1, begin, mid); <- Was used in old implementation
476
477
478
479
//    RCBGeom_z(rekDepth - 1, mid, end);
}

void Distribution::RCBGeom_z(int rekDepth, int begin, int end) {
480
481
482
483
484
485
486
487
488
489
490
  if (rekDepth == 0) return;
  sort(cells.begin() + begin, cells.begin() + end, Less_z);
  auto midlh = getMidOfLowestAndHighestCoord(begin, end, 2);
  int mid = end;
  for (int i = begin; i < end; ++i) {
    bool con = false;
    if (midlh > cells[i]()[2]) con = true;
    if (con) continue;
    mid = i;
    break;
  }
491
492
  for (int i = mid; i < end; ++i)
    procsForCells[i] += 1 << (rekDepth - 1);
493
494
  RCBGeom_x(rekDepth - 1, begin, mid);
  RCBGeom_x(rekDepth - 1, mid, end);
495
496
}

497
std::string Distribution::Name() const {
498
  return _distName;
499
500
501
}

void Distribution::init(Mesh &mesh) {
502
503
504
505
506
507
  // Ensure correct numeration of previously distributed cells
  for (auto p: distributedCells) {
    cells.emplace_back(mesh.find_cell(p));
  }

  // Add new cells from mesh
508
  for (cell c = mesh.cells(); c != mesh.cells_end(); ++c) {
509
510
    if (std::none_of(distributedCells.begin(), distributedCells.end(), [&c](const Point &p) {
      return c() == p;
511
512
    })) {
      cells.emplace_back(c);
513
514
515
      procsForCells.push_back(0);
      weights[c()] = 1;
    }
516
  }
517
518
}

519
void Distribution::emptyData(bool clearAll) {
520
521
  cells.clear();
  markedCells.clear();
522
523
524
525
526
527
  markedCells.resize(PPM->Size(commSplit));
  if (clearAll) {
    markedCells.clear();
    procsForCells.clear();
    distributedCells.clear();
  }
528
529
}

530
void Distribution::Stripes_Old() {
531
  sort(cells.begin(), cells.end(), Less_x);
532
533
  int m = (int(cells.size()) + PPM->Size(commSplit) - 1) / PPM->Size(commSplit);
  int r = int(cells.size()) - (m - 1) * PPM->Size(commSplit);
534
535
536
537
538
539
540
  int k = 0;
  int q = 0;
  for (int i = 0; i < cells.size(); ++i) {
    if (k >= m) {
      k = 0;
      ++q;
      if (q == r) --m;
541
    }
542
543
544
    ++k;
    procsForCells[i] = q;
  }
545
546
547
}

void Distribution::RCB_Old(int rekDepth, int begin, int end) {
548
549
550
551
552
553
554
555
556
557
558
  double dist_x = 0;
  double dist_y = 0;
  double dist_z = 0;
  for (int i = begin; i < end; ++i) {
    for (int j = begin; j < end; ++j) {
      if (dist_x < abs(cells[i]()[0] - cells[j]()[0]))
        dist_x = abs(cells[i]()[0] - cells[j]()[0]);
      if (dist_y < abs(cells[i]()[1] - cells[j]()[1]))
        dist_y = abs(cells[i]()[1] - cells[j]()[1]);
      if (dist_z < abs(cells[i]()[2] - cells[j]()[2]))
        dist_z = abs(cells[i]()[2] - cells[j]()[2]);
559
    }
560
561
562
  }
  if (dist_x < dist_y)
    if (dist_z < dist_y) sort(cells.begin() + begin, cells.begin() + end, LessCell_y);
563
    else sort(cells.begin() + begin, cells.begin() + end, LessCell_z);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  else if (dist_z < dist_x)
    sort(cells.begin() + begin,
         cells.begin() + end,
         LessCell_x);
  else sort(cells.begin() + begin, cells.begin() + end, LessCell_z);

  if (rekDepth == 0) {
    return;
  } else {
    int mid = begin + (end - begin) / 2;
    for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
    RCB_Old(rekDepth - 1, begin, mid);
    RCB_Old(rekDepth - 1, mid, end);
  }
578
}
579

580
void Distribution::RCB_Geom_Old(int rekDepth, int begin, int end, char d) {
581
  switch (d) {
582
583
584
585
586
587
588
589
590
    case 'x':
      sort(cells.begin() + begin, cells.begin() + end, Less_x);
      break;
    case 'y':
      sort(cells.begin() + begin, cells.begin() + end, Less_y);
      break;
    case 'z':
      sort(cells.begin() + begin, cells.begin() + end, Less_z);
      break;
591
592
    default:
      break;
593
594
595
596
597
598
599
600
  }
  if (rekDepth == 0) {
    return;
  } else {
    int mid;
    double low = infty;
    double high = -infty;
    for (int i = begin; i < end; ++i) {
601
      switch (d) {
602
603
604
605
606
607
608
609
610
611
612
613
        case 'x':
          if (low > cells[i]()[0]) low = cells[i]()[0];
          if (high < cells[i]()[0]) high = cells[i]()[0];
          break;
        case 'y':
          if (low > cells[i]()[1]) low = cells[i]()[1];
          if (high < cells[i]()[1]) high = cells[i]()[1];
          break;
        case 'z':
          if (low > cells[i]()[2]) low = cells[i]()[2];
          if (high < cells[i]()[2]) high = cells[i]()[2];
          break;
614
615
        default:
          break;
616
617
618
619
620
621
      }
    }
    double midlh = (high + low) / 2.;
    mid = end;
    for (int i = begin; i < end; ++i) {
      bool con = false;
622
      switch (d) {
623
        case 'x':
624
625
          if (midlh > cells[i]()[0]) con = true;
          break;
626
        case 'y':
627
628
          if (midlh > cells[i]()[1]) con = true;
          break;
629
        case 'z':
630
631
          if (midlh > cells[i]()[2]) con = true;
          break;
632
633
        default:
          break;
634
635
636
637
      }
      if (con) continue;
      mid = i;
      break;
638
    }
639
    for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (rekDepth - 1);
640
    switch (d) {
641
642
643
644
645
646
647
648
649
      case 'x':
        RCB_Geom_Old(rekDepth - 1, begin, mid, 'y');
        RCB_Geom_Old(rekDepth - 1, mid, end, 'y');
        break;
      case 'y':
      case 'z':
        RCB_Geom_Old(rekDepth - 1, begin, mid, 'x');
        RCB_Geom_Old(rekDepth - 1, mid, end, 'x');
        break;
650
651
      default:
        break;
652
    }
653
  }
654
}
655

656
void Distribution::trcb(int level, int begin, int end, char d) {
657
  int N = end - begin;
658
  switch (d) {
659
660
661
    case 'x':
    case 'y':
    case 'z':
662
      sort(cells.begin() + begin, cells.begin() + end, TLess_t);
663
664
665
    case 'a':
    case 'c':
    case 'e':
666
      sort(cells.begin() + begin, cells.begin() + end, TLess_x);
667
      break;
668
669
    case 'b':
    case 'd':
670
    case 'f':
671
672
673
      sort(cells.begin() + begin, cells.begin() + end, TLess_y);
      break;
    default:
674
675
676
677
678
679
      break;
  }
  if (level == 0) {
    return;
  } else {
    int mid = begin + (end - begin) / 2;
680
    for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (level - 1);
681
    switch (d) {
682
      case 'x':
683
684
        trcb(level - 1, begin, mid, 'y');
        trcb(level - 1, mid, end, 'y');
685
686
        break;
      case 'y':
687
688
        trcb(level - 1, begin, mid, 'z');
        trcb(level - 1, mid, end, 'z');
689
690
        break;
      case 'z':
691
692
        trcb(level - 1, begin, mid, 'a');
        trcb(level - 1, mid, end, 'a');
693
694
        break;
      case 'a':
695
696
        trcb(level - 1, begin, mid, 'b');
        trcb(level - 1, mid, end, 'b');
697
698
        break;
      case 'b':
699
700
        trcb(level - 1, begin, mid, 'c');
        trcb(level - 1, mid, end, 'c');
701
702
        break;
      case 'c':
703
704
        trcb(level - 1, begin, mid, 'd');
        trcb(level - 1, mid, end, 'd');
705
706
        break;
      case 'd':
707
708
        trcb(level - 1, begin, mid, 'e');
        trcb(level - 1, mid, end, 'e');
709
710
        break;
      case 'e':
711
712
        trcb(level - 1, begin, mid, 'f');
        trcb(level - 1, mid, end, 'f');
713
714
        break;
      case 'f':
715
716
717
718
        trcb(level - 1, begin, mid, 'x');
        trcb(level - 1, mid, end, 'x');
        break;
      default:
719
720
721
722
723
        break;
    }
  }
}

724
void Distribution::trcb_space(int level, int begin, int end, char d) {
725
  int N = end - begin;
726
  switch (d) {
727
    case 'x':
728
      sort(cells.begin() + begin, cells.begin() + end, TLess_x);
729
730
      break;
    case 'y':
731
732
733
      sort(cells.begin() + begin, cells.begin() + end, TLess_y);
      break;
    default:
734
735
736
737
738
739
      break;
  }
  if (level == 0) {
    return;
  } else {
    int mid = begin + (end - begin) / 2;
740
    for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (level - 1);
741
    switch (d) {
742
      case 'x':
743
744
        trcb_space(level - 1, begin, mid, 'y');
        trcb_space(level - 1, mid, end, 'y');
745
746
        break;
      case 'y':
747
748
749
750
        trcb_space(level - 1, begin, mid, 'x');
        trcb_space(level - 1, mid, end, 'x');
        break;
      default:
751
752
753
754
755
        break;
    }
  }
}

756
void Distribution::timeOpt(int tProc, int level, int begin, int end) {
757
  int N = end - begin;
758
  sort(cells.begin() + begin, cells.begin() + end, TLess_t);
759
760
761
762
763
764
765
766
767
768

  if (PPM->proc() == 0) {
    vector<int> P(tProc);
    for (int i = 0; i < tProc; ++i)
      P[i] = ((end - 1) / tProc) * (i + 1);
    P[tProc - 1] = end - 1;

    for (int i = 0; i < tProc - 1; ++i) {
      int old_mid = P[i];
      int r_mid, l_mid;
769
770
      Point a = cells[P[i]]().CopyWithT(0.0);
      Point b = cells[P[i] - 1]().CopyWithT(0.0);
771

772
      while (cells[P[i]]().t() == cells[P[i] - 1]().t() || a == b) {
773
        ++P[i];
774
775
        a = cells[P[i]]().CopyWithT(0.0);
        b = cells[P[i] - 1]().CopyWithT(0.0);
776
777
778
779
      }
      r_mid = P[i];

      P[i] = old_mid;
780
781
      a = cells[P[i]]().CopyWithT(0.0);
      b = cells[P[i] - 1]().CopyWithT(0.0);
782

783
      while (cells[P[i]]().t() == cells[P[i] - 1]().t() || a == b) {
784
        --P[i];
785
786
        a = cells[P[i]]().CopyWithT(0.0);
        b = cells[P[i] + 1]().CopyWithT(0.0);
787
788
789
790
791
792
793
794
795
796
        if (P[i] == 0) break;
      }
      l_mid = P[i];

      if (old_mid - l_mid < r_mid - old_mid)
        P[i] = l_mid - 1;
      else
        P[i] = r_mid - 1;
    }

797
    if (level > 0) trcb_space(level, 0, P[0], 'x');
798

799
    int spaceProc = int(std::pow(2.0, double(level)) + 1e-10);
800
801
802

    if (tProc > 1)
      for (int i = 1; i < tProc; ++i) {
803
804
        for (int j = P[i - 1] + 1; j <= P[i]; ++j) procsForCells[j] += i * spaceProc;
        if (level > 0) trcb_space(level, P[i - 1] + 1, P[i], 'x');
805
806
807
808
      }
  }
}

809
void Distribution::timercb(int level, int begin, int end, char d) {
810
  int N = end - begin;
811
  sort(cells.begin() + begin, cells.begin() + end, GetLess(d));
812
813
814
815
816
817
818
  if (level == 0) {
    return;
  } else {
    int mid = begin + (end - begin) / 2;
    if (PPM->proc() == 0) {
      int old_mid = mid;
      int r_mid, l_mid;
819
820
      Point a = cells[mid]().CopyWithT(0.0);
      Point b = cells[mid - 1]().CopyWithT(0.0);
821

822
      while (cells[mid]().t() == cells[mid - 1]().t() || a == b) {
823
        ++mid;
824
825
        a = cells[mid]().CopyWithT(0.0);
        b = cells[mid - 1]().CopyWithT(0.0);
826
827
828
829
      }
      r_mid = mid;

      mid = old_mid;
830
831
      a = cells[mid]().CopyWithT(0.0);
      b = cells[mid - 1]().CopyWithT(0.0);
832

833
      while (cells[mid]().t() == cells[mid - 1]().t() || a == b) {
834
        --mid;
835
836
        a = cells[mid]().CopyWithT(0.0);
        b = cells[mid + 1]().CopyWithT(0.0);
837
838
839
840
841
842
843
      }
      l_mid = mid;
      if (old_mid - l_mid < r_mid - old_mid)
        mid = l_mid;
      else
        mid = r_mid;
    }
844
    for (int i = mid; i < end; ++i) procsForCells[i] += 1 << (level - 1);
845
    switch (d) {
846
      case 'x':
847
848
        timercb(level - 1, begin, mid, 'y');
        timercb(level - 1, mid, end, 'y');
849
850
        break;
      case 'y':
851
852
        timercb(level - 1, begin, mid, 't');
        timercb(level - 1, mid, end, 't');
853
854
        break;
      case 't':
855
856
857
858
        timercb(level - 1, begin, mid, 'x');
        timercb(level - 1, mid, end, 'x');
        break;
      default:
859
860
861
862
863
        break;
    }
  }
}

864
865
866
867
void Distribution::timercb_weighted(int tlevel, int xlevel, int ylevel,
                                    int begin, int end, char d,
                                    std::unordered_map<Point, short> &W,
                                    int default_weight) {
868
869
  int N = end - begin;
  int level = tlevel + xlevel + ylevel;
870
  sort(cells.begin() + begin, cells.begin() + end, GetLess(d));
871
872
873
  if (PPM->proc() == 0) {
    int W_sum = 0;
    for (int i = begin; i < end; ++i) {
874
875
      if (W.find(cells[i]()) != W.end())
        W_sum += W[cells[i]()];
876
877
878
879
880
881
882
      else
        W_sum += default_weight;
    }

    int W_mid = 0;
    int mid = begin;
    while (W_mid < W_sum / 2) {
883
884
      if (W.find(cells[mid]()) != W.end())
        W_mid += W[cells[mid]()];
885
886
887
888
889
890
891
      else
        W_mid += default_weight;
      ++mid;
    }

    int old_mid = mid;
    int r_mid, l_mid;
892
893
    Point a = cells[mid]().CopyWithT(0.0);
    Point b = cells[mid - 1]().CopyWithT(0.0);
894

895
    while (cells[mid]().t() == cells[mid - 1]().t() || a == b) {
896
897
      ++mid;
      if (mid >= end) break;
898
899
      a = cells[mid]().CopyWithT(0.0);
      b = cells[mid - 1]().CopyWithT(0.0);
900
901
902
903
    }
    r_mid = mid;

    mid = old_mid;
904
905
    a = cells[mid]().CopyWithT(0.0);
    b = cells[mid - 1]().CopyWithT(0.0);
906

907
    while (cells[mid]().t() == cells[mid - 1]().t() || a == b) {
908
      --mid;
909
      if (mid < begin || mid == 0) break;
910
911
      a = cells[mid]().CopyWithT(0.0);
      b = cells[mid + 1]().CopyWithT(0.0);
912
913
914
915
916
917
918
919
920
921
922
923
    }
    l_mid = mid;

    if (old_mid - l_mid < r_mid - old_mid)
      mid = l_mid;
    else
      mid = r_mid;

    if (mid == begin || mid == end) {
      mid = old_mid;
    }
    for (int i = mid; i < end; ++i)
924
      procsForCells[i] += 1 << (level);
925
926

    if (tlevel > 0) {
927
      timercb_weighted(tlevel - 1, xlevel, ylevel, begin, mid, 't', W,
928
                       default_weight);
929
      timercb_weighted(tlevel - 1, xlevel, ylevel, mid, end, 't', W,
930
931
932
933
                       default_weight);
      return;
    }
    if (xlevel > 0) {
934
      timercb_weighted(tlevel, xlevel - 1, ylevel, begin, mid, 'x', W,
935
                       default_weight);
936
      timercb_weighted(tlevel, xlevel - 1, ylevel, mid, end, 'x', W,
937
938
939
940
                       default_weight);
      return;
    }
    if (ylevel > 0) {
941
      timercb_weighted(tlevel, xlevel, ylevel - 1, begin, mid, 'y', W,
942
                       default_weight);
943
      timercb_weighted(tlevel, xlevel, ylevel - 1, mid, end, 'y', W,
944
945
946
947
948
949
                       default_weight);
      return;
    }
  }
}

950
void Distribution::time_stripes(int n_procs, int begin, int end) {
951
  int N = end - begin;
952
  sort(cells.begin() + begin, cells.begin() + end, TLess_t);
953
954
955
956
957
958
959
960
961
962

  if (PPM->proc() == 0) {
    vector<int> P(n_procs);
    for (int i = 0; i < n_procs; ++i)
      P[i] = ((end - 1) / n_procs) * (i + 1);
    P[n_procs - 1] = end - 1;

    for (int i = 0; i < n_procs - 1; ++i) {
      int old_mid = P[i];
      int r_mid, l_mid;
963
964
      Point a = cells[P[i]]().CopyWithT(0.0);
      Point b = cells[P[i] - 1]().CopyWithT(0.0);
965

966
      while (cells[P[i]]().t() == cells[P[i] - 1]().t() || a == b) {
967
        ++P[i];
968
969
        a = cells[P[i]]().CopyWithT(0.0);
        b = cells[P[i] - 1]().CopyWithT(0.0);
970
971
972
973
      }
      r_mid = P[i];

      P[i] = old_mid;
974
975
      a = cells[P[i]]().CopyWithT(0.0);
      b = cells[P[i] - 1]().CopyWithT(0.0);
976

977
      while (cells[P[i]]().t() == cells[P[i] - 1]().t() || a == b) {
978
        --P[i];
979
980
        a = cells[P[i]]().CopyWithT(0.0);
        b = cells[P[i] + 1]().CopyWithT(0.0);
981
982
983
984
985
986
987
988
989
990
      }
      l_mid = P[i];

      if (old_mid - l_mid < r_mid - old_mid)
        P[i] = l_mid - 1;
      else
        P[i] = r_mid - 1;
    }

    for (int j = 0; j <= P[0]; ++j)
991
      procsForCells[j] = 0;
992
993
994

    for (int i = 1; i < n_procs; ++i)
      for (int j = P[i - 1] + 1; j <= P[i]; ++j)
995
        procsForCells[j] = i;
996
997
  }
}
998

999
void Distribution::DistributeSTMesh(Mesh &mesh) {
1000
  if (PPM->size() == 1) return;
1001
  if (_distName == "NoDistribution") return;
1002
  const int N = mesh.CellCount();
1003
  int P = PPM->size();
1004
  int rekDepth = int(log2(PPM->Size(commSplit)));
1005
1006
1007

  int default_weight = 1;
  config.get("lb_default_weight", default_weight);
1008
1009

  if (_distName == "Stripes") {
1010
1011
1012
    sort(cells.begin(), cells.end(), Less_t);
    int m = (N + PPM->size() - 1) / PPM->size();
    int r = N - (m - 1) * PPM->size();
1013
1014
    int k = 0;
    int q = 0;
1015
    for (int i = 0; i < cells.size(); ++i) {
1016
1017
1018
1019
1020
1021
      if (k >= m) {
        k = 0;
        ++q;
        if (q == r) --m;
      }
      ++k;
1022
      markedCells[q].push_back(cells[i]);
1023
    }
1024
  } else if (_distName == "st-opt") {
1025
1026
    int S = mesh.steps() - 1;
    int T_Proc = int(double(P) / std::pow(2.0, double(rekDepth)) + 1e-10);
1027
    T_Proc = std::min(T_Proc, S);
1028
    timeOpt(T_Proc, rekDepth, 0, cells.size());
1029
  } else if (_distName == "RCB") {
1030
    trcb(rekDepth, 0, cells.size(), 'x');
1031
  } else if (_distName == "deformed") {
1032
    timercb(rekDepth, 0, cells.size(), 't');
1033
  } else if (_distName == "deformed_optimized") {
1034
    int S = mesh.steps() - 1;
1035
    int Slice_Maximum = int(log(double(S)) / log(2.0) + 1e-10);
1036
    int X_Maximum = int(log(double(sqrt(N / S))) / log(2.0) + 1e-10);