aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/LoadBalanceReal/splitregions_recursively.cc
blob: fd31488644c4fd06260607e4d992f1cd2ea28eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
194
195
196
197
198
199
200
201
202
203
204
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
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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <cctk.h>
#include <cctk_Parameters.h>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <sstream>
#include <string>
#include <vector>

#include <sys/stat.h>
#include <sys/types.h>

#ifdef CCTK_MPI
#  include <mpi.h>
#else
#  include "nompi.h"
#endif

#include <loopcontrol.h>

#include <bbox.hh>
#include <bboxset.hh>
#include <defs.hh>
#include <dh.hh>
#include <gh.hh>
#include <region.hh>
#include <vect.hh>

#include <carpet.hh>
#include <modes.hh>
#include <variables.hh>
#include <Timers.hh>



namespace Carpet {
  
  using namespace std;
  
  
  
  // Helper routines for spliting regions automatically
  
  // The cost for a region, assuming a cost of 1 per interior point
  static rvect
  cost (region_t const & reg)
  {
    DECLARE_CCTK_PARAMETERS;
    static rvect costfactor;
    static bool initialised = false;
    if (not initialised) {
      costfactor = rvect(1.0);
      if (dim > 0) costfactor[0] = 1.0 / aspect_ratio_x;
      if (dim > 1) costfactor[1] = 1.0 / aspect_ratio_y;
      if (dim > 2) costfactor[2] = 1.0 / aspect_ratio_z;
    }
    if (reg.extent.empty()) return rvect(0);
    return rvect (reg.extent.shape() / reg.extent.stride()) * costfactor;
  }
  
  
  
  struct f_range {
    int lower, upper, stride;
  };
  
  struct f_bbox {
    f_range dim[3];
    
    f_bbox () {}
    f_bbox (ibbox const& box)
    {
      assert (::dim == 3);
      for (int d=0; d<3; ++d) {
        dim[d].lower  = box.lower()[d];
        dim[d].upper  = box.upper()[d];
        dim[d].stride = box.stride()[d];
      }
    }
    /*explicit*/ operator ibbox () const
    {
      ivect lower, upper, stride;
      assert (::dim == 3);
      for (int d=0; d<3; ++d) {
        lower [d] = dim[d].lower;
        upper [d] = dim[d].upper;
        stride[d] = dim[d].stride;
      }
      return ibbox (lower, upper, stride);
    }
  };
  
  struct f_boundary {
    int obound[2][3];
    
    f_boundary () {}
    f_boundary (b2vect const& ob)
    {
      assert (::dim == 3);
      for (int d=0; d<3; ++d) {
        for (int f=0; f<2; ++f) {
          obound[f][d] = ob[f][d];
        }
      }
    }
    /*explicit*/ operator b2vect () const
    {
      b2vect ob;
      assert (::dim == 3);
      for (int d=0; d<3; ++d) {
        for (int f=0; f<2; ++f) {
          ob[f][d] = obound[f][d];
        }
      }
      return ob;
    }
  };
  
  struct f_superregion2slim {
    f_bbox     extent;
    f_boundary outer_boundaries;
    int        map;
    int        processor;
    
    /*explicit*/ operator region_t () const
    {
      region_t reg;
      reg.extent           = ibbox(extent);
      reg.outer_boundaries = b2vect(outer_boundaries);
      reg.map              = map;
      reg.processor        = processor;
      reg.processors       = NULL;
      return reg;
    }
    
    /*explicit*/ operator pseudoregion_t () const
    {
      pseudoregion_t preg;
      preg.extent    = ibbox(extent);
      preg.component = processor;
      return preg;
    }
  };
  
  
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(splitregions_recursively) (CCTK_POINTER const& cxx_superregs,
                                        int const& nsuperregs,
                                        CCTK_POINTER const& cxx_regs,
                                        int const& nprocs,
                                        int const& ghostsize,
                                        CCTK_REAL const& alpha,
                                        int const& limit_size,
                                        int const& procid);
  
  void
  SplitRegionsMaps_Recursively (cGH const * const cctkGH,
                                vector<vector<region_t> > & superregss,
                                vector<vector<region_t> > & regss)
  {
    DECLARE_CCTK_PARAMETERS;
    
    if (recompose_verbose) cout << "SRMR enter" << endl;
    
    int const nmaps = superregss.size();
    assert (int(regss.size()) == nmaps);
    int map_offset = 1000000000;
    int max_map = 0;
    vector<bool> have_map(maps, false);
    for (int m=0; m<nmaps; ++m) {
      for (int r=0; r<int(superregss.AT(m).size()); ++r) {
        map_offset = min (map_offset, superregss.AT(m).AT(r).map);
        max_map = max (max_map, superregss.AT(m).AT(r).map);
        have_map.AT(superregss.AT(m).AT(r).map) = true;
      }
    }
    // Apparently this is not always the case:
    // assert (max_map - map_offset == nmaps - 1);
    
    int nsuperregs = 0;
    for (int m=0; m<nmaps; ++m) {
      nsuperregs += superregss.AT(m).size();
    }
    if (recompose_verbose) cout << "SRMR nsuperregs " << nsuperregs << endl;
    
    // Something to do?
    if (nsuperregs == 0) return;
    
    // Collect slices
    vector<region_t> superregs;
    {
      for (int m=0; m<nmaps; ++m) {
        combine_regions (superregss.AT(m), superregs);
      }
      nsuperregs = superregs.size();
      
      // If the last region was removed, add a new empty region again.
      // A set of regions (corresponding to a refinement level or a
      // grid array) cannot be empty.
      if (nsuperregs == 0) {
        assert (nmaps == 1);    // we should only be here for grid
                                // arrays
        region_t reg;
        reg.extent           = ibbox (ivect (0), ivect (-1), ivect (1));
        reg.outer_boundaries = b2vect (bvect (true), bvect (true));
        reg.map              = 0;
        superregs.push_back (reg);
        nsuperregs = superregs.size();
      }
    }
    
    // Create a mapping from this list of regions to maps, and set the
    // map to the superregion index instead
    vector<int> superreg_maps(nsuperregs);
    for (int r=0; r<nsuperregs; ++r) {
      superreg_maps.AT(r) = superregs.AT(r).map;
      superregs.AT(r).map = r;
    }
    
    // Handle granularity: artificially reduce the size of the
    // superregions
    for (int r=0; r<nsuperregs; ++r) {
      region_t& superreg = superregs.AT(r);
      ibbox& ext = superregs.AT(r).extent;
      ivect lo = ext.lower();
      ivect hi = ext.upper() + ext.stride();
      ivect str = ext.stride();
      // cut off outer boundaries
      lo += ivect(superreg.outer_boundaries[0]) * granularity_boundary * str;
      hi -= ivect(superreg.outer_boundaries[1]) * granularity_boundary * str;
      // multiply stride by granularity
      str *= granularity;
      assert(all((hi-lo)%str==0));
      ext = ibbox(lo, hi-str, str);
    }
    
    int const real_nprocs = CCTK_nProcs (cctkGH);
    if (recompose_verbose) cout << "SRMR real_nprocs " << real_nprocs << endl;
    
    // Deactivate some processors if there are too many
    int nprocs;
    if (min_points_per_proc == 0) {
      nprocs = real_nprocs;
    } else {
      CCTK_REAL mycost = 0;
      for (int r=0; r<nsuperregs; ++r) {
        mycost += prod (cost (superregs.AT(r)));
      }
      int const goodnprocs = int (floor (mycost / min_points_per_proc));
      nprocs = max (1, min (real_nprocs, goodnprocs));
    }
    if (recompose_verbose) cout << "SRMR nprocs " << nprocs << endl;
    
    // Distribute load
    CCTK_POINTER const cxx_superregs = & superregs;
    assert ((int)superregs.size() == nsuperregs);
    vector<region_t> regs;
    // regs.reserve (...);
    CCTK_POINTER const cxx_regs = & regs;
    int const ghostsize = vdd.AT(0)->ghost_widths.AT(0)[0][0];
    for (int m=0; m<maps; ++m) {
      for (int rl=0; rl<reflevels; ++rl) {
        for (int f=0; f<2; ++f) {
          for (int d=0; d<dim; ++d) {
            assert (vdd.AT(m)->ghost_widths.AT(rl)[f][d] == ghostsize);
          }
        }
      }
    }
    CCTK_REAL const alpha = ghost_zone_cost;
    int const limit_size = true;
    int const procid = CCTK_MyProc(cctkGH);
    CCTK_FNAME(splitregions_recursively)
      (cxx_superregs, nsuperregs, cxx_regs, nprocs,
       ghostsize, alpha, limit_size, procid);
    int nregs = regs.size();
    
    // Handle granularity: increase the size of the superregions and
    // regions again
    for (int r=0; r<nsuperregs; ++r) {
      region_t& superreg = superregs.AT(r);
      ibbox& ext = superregs.AT(r).extent;
      ivect lo = ext.lower();
      ivect hi = ext.upper() + ext.stride();
      ivect str = ext.stride();
      // divide stride by granularity
      assert(all(str%granularity==0));
      str /= granularity;
      // add outer boundaries again
      lo -= ivect(superreg.outer_boundaries[0]) * granularity_boundary * str;
      hi += ivect(superreg.outer_boundaries[1]) * granularity_boundary * str;
      ext = ibbox(lo, hi-str, str);
    }
    for (int r=0; r<nregs; ++r) {
      region_t& reg = regs.AT(r);
      ibbox& ext = regs.AT(r).extent;
      ivect lo = ext.lower();
      ivect hi = ext.upper() + ext.stride();
      ivect str = ext.stride();
      // divide stride by granularity
      assert(all(str%granularity==0));
      str /= granularity;
      // add outer boundaries again
      lo -= ivect(reg.outer_boundaries[0]) * granularity_boundary * str;
      hi += ivect(reg.outer_boundaries[1]) * granularity_boundary * str;
      ext = ibbox(lo, hi-str, str);
    }
    
    if (same_number_of_components_on_each_process) {
      // Ensure all processes have the same number of components
      vector<int> ncomps(nprocs, 0);
      for (int r=0; r<nregs; ++r) {
        int const p = regs.AT(r).processor;
        assert (p>=0);
        ++ncomps.AT(p);
      }
      int maxncomps = 0;
      int sumncomps = 0;
      for (int p=0; p<nprocs; ++p) {
        maxncomps = max(maxncomps, ncomps.AT(p));
        sumncomps += ncomps.AT(p);
      }
      int const missingcomps = maxncomps * nprocs - sumncomps;
      if (missingcomps > 0) {
        // Invent a dummy component
        ibbox const& ext = superregss.AT(0).AT(0).extent;
        region_t dummy;
        dummy.extent =
          ibbox(ext.lower(), ext.lower()-ext.stride(), ext.stride());
        assert (dummy.extent.empty());
        dummy.outer_boundaries = b2vect(true);
        dummy.map = nmaps-1;      // arbitrary choice
        // Insert dummy regions at the end
        regs.resize(nregs + missingcomps, dummy);
        for (int p=0; p<nprocs; ++p) {
          for (int i=ncomps.AT(p); i<maxncomps; ++i) {
            regs.AT(nregs++).processor = p;
          }
        }
        assert (nregs == int(regs.size()));
        // Insert a superregion
        // TODO: Do we need this? Should we skip this?
        pseudoregion_t sample;
        sample.extent = dummy.extent;
        // sample.component remains unset (component will be set later)
        if (missingcomps == 1) {
          dummy.processors = new ipfulltree(sample);
        } else {
          int const dir = 0;      // arbitrary choice
          vector<int> bounds(missingcomps+1);
          for (int n=0; n<missingcomps+1; ++n) {
            bounds.AT(n) = dummy.extent.lower()[dir];
          }
          vector<ipfulltree*> subtrees(missingcomps);
          for (int n=0; n<missingcomps; ++n) {
            subtrees.AT(n) = new ipfulltree(sample);
          }
          dummy.processors = new ipfulltree(dir, bounds, subtrees);
        }
        superregs.push_back (dummy);
      }
    }
    
    // Allocate regions, saving the old regions for debugging or
    // self-checking
    vector<vector<region_t> > old_superregss;
    swap (superregss, old_superregss);
    superregss.resize (old_superregss.size());
    assert ((int)regss.size() == nmaps);
    for (int m=0; m<nmaps; ++m) {
      assert (regss.AT(m).empty());
      // regss.AT(m).reserve (...);
      // superregss.AT(m).clear();
      assert (superregss.AT(m).empty());
      // superregss.AT(m).reserve (...);
    }
    // Assign regions
    for (int r=0; r<nsuperregs; ++r) {
      superregs.AT(r).map = superreg_maps.AT(r); // correct map
      int const m = superregs.AT(r).map - map_offset;
      assert (m>=0 and m<nmaps);
      superregss.AT(m).push_back (superregs.AT(r));
    }
    // Renumber components
    for (int m=0; m<nmaps; ++m) {
      int c=0;
      for (size_t r=0; r<superregss.AT(m).size(); ++r) {
        ipfulltree* const procs = superregss.AT(m).AT(r).processors;
        assert (procs);
        for (ipfulltree::iterator it (*procs); not it.done(); ++it) {
          (*it).payload().component = c++;
        }
      }
    }
    for (int r=0; r<nregs; ++r) {
      int const s = regs.AT(r).map;
      assert (s>=0 and s<nsuperregs);
      regs.AT(r).map = superreg_maps.AT(s); // correct map
      int const m = regs.AT(r).map - map_offset;
      assert (m>=0 and m<nmaps);
      regss.AT(m).push_back (regs.AT(r));
    }
    // Output regions
    if (recompose_verbose) {
      region_t::full_output = true;
      cout << "SRMR superregss " << superregss << endl;
      cout << "SRMR regss " << regss << endl;
      region_t::full_output = false;
    }
    
    // Consistency check
    bool has_error = false;
    vector<ibset> all_old_superregss(maps);
    for (size_t m=0; m<superregss.size(); ++m) {
      for (size_t r=0; r<old_superregss.AT(m).size(); ++r) {
        region_t const& reg = old_superregss.AT(m).AT(r);
        if (all_old_superregss.AT(reg.map).intersects(reg.extent)) {
          has_error = true;
          cout << "SRMR: old_superregss:\n"
               << "m=" << m << " r=" << r << " reg=" << reg << "\n";
        }
        all_old_superregss.AT(reg.map) += reg.extent;
      }
    }
    vector<ibset> all_superregss(maps);
    for (size_t m=0; m<superregss.size(); ++m) {
      for (size_t r=0; r<superregss.AT(m).size(); ++r) {
        region_t const& reg = superregss.AT(m).AT(r);
        if (all_superregss.AT(reg.map).intersects(reg.extent)) {
          has_error = true;
          cout << "SRMR: all_superregss:\n"
               << "m=" << m << " r=" << r << " reg=" << reg << "\n";
        }
        all_superregss.AT(reg.map) += reg.extent;
      }
    }
    for (int m=0; m<maps; ++m) {
      if (not (all_superregss.AT(m) == all_old_superregss.AT(m))) {
        has_error = true;
        cout << "SRMR: all_superregss m=" << m << "\n";
      }
    }
    vector<ibset> all_regss(maps);
    for (size_t m=0; m<regss.size(); ++m) {
      for (size_t r=0; r<regss.AT(m).size(); ++r) {
        region_t const& reg = regss.AT(m).AT(r);
        if (all_regss.AT(reg.map).intersects(reg.extent)) {
          has_error = true;
          cout << "SRMR: all_regss:\n"
               << "m=" << m << " r=" << r << " reg=" << reg << "\n";
        }
        all_regss.AT(reg.map) += reg.extent;
      }
    }
    for (int m=0; m<maps; ++m) {
      if (not (all_regss.AT(m) == all_old_superregss.AT(m))) {
        has_error = true;
        cout << "SRMR: all_regss m=" << m << "\n";
      }
    }
    if (has_error) {
      region_t::full_output = true;
      cout << "SRMR: all_old_superregss=" << all_old_superregss << "\n"
           << "SRMR: all_superregss=" << all_superregss << "\n"
           << "SRMR: all_regss=" << all_regss << "\n";
      region_t::full_output = false;
      CCTK_WARN(CCTK_WARN_ABORT, "Internal error");
    }
    
    if (recompose_verbose) cout << "SRMR exit" << endl;
  }
  
  
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_get_region) (CCTK_POINTER& cxx_superregs,
                                 int const& i,
                                 CCTK_POINTER& cxx_superreg)
  {
    vector<region_t>& superregs =
      *static_cast<vector<region_t>*>(cxx_superregs);
    region_t& superreg = superregs.AT(i);
    cxx_superreg = &superreg;
  }
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_get_bbox) (CCTK_POINTER& cxx_superreg,
                               f_bbox& box, f_boundary& obound)
  {
    region_t& superreg = *static_cast<region_t*>(cxx_superreg);
    box = f_bbox(superreg.extent);
    obound = f_boundary(superreg.outer_boundaries);
  }
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_insert_region) (CCTK_POINTER& cxx_regs,
                                    f_superregion2slim const& reg)
  {
    vector<region_t>& regs = *static_cast<vector<region_t>*>(cxx_regs);
    regs.push_back(region_t(reg));
  }
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_create_tree_branch) (int const& nch,
                                         int const& dir,
                                         int const fbounds[],
                                         CCTK_POINTER cxx_subtrees[],
                                         CCTK_POINTER& cxx_tree)
  {
    vector<int> bounds(nch+1);
    vector<ipfulltree*> subtrees(nch);
    for (int i=0; i<nch+1; ++i) {
      bounds.AT(i) = fbounds[i];
    }
    for (int i=0; i<nch; ++i) {
      ipfulltree* const tree = static_cast<ipfulltree*>(cxx_subtrees[i]);
      assert (tree->invariant());
      subtrees.AT(i) = tree;
    }
    cxx_tree = new ipfulltree (dir, bounds, subtrees);
  }
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_create_tree_leaf) (f_superregion2slim const& sreg,
                                       CCTK_POINTER& cxx_tree)
  {
    cxx_tree = new ipfulltree(pseudoregion_t(sreg));
  }
  
  extern "C"
  CCTK_FCALL void
  CCTK_FNAME(carpet_set_tree) (CCTK_POINTER& cxx_superreg,
                               CCTK_POINTER& cxx_tree)
  {
    region_t& superreg = *static_cast<region_t*>(cxx_superreg);
    ipfulltree* tree = static_cast<ipfulltree*>(cxx_tree);
    assert (not superreg.processors);
    superreg.processors = tree;
  }
  
} // namespace Carpet