forked from stanfordhpccenter/HTR-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometeo_mapper.cc
1133 lines (1029 loc) · 50.9 KB
/
prometeo_mapper.cc
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <array>
#include <deque>
#include <iostream>
#include <fstream>
#include <regex>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "mappers/default_mapper.h"
#include "realm/logging.h"
#include "config_schema.h"
#include "prometeo_mapper.h"
using namespace Legion;
using namespace Legion::Mapping;
//=============================================================================
// DOCUMENTATION
//=============================================================================
// Assume we're running 2 CPU-only samples, A and B, tiled as follows:
// tiles(A) = [1,2,1], tilesPerRank(A) = [1,1,1]
// tiles(B) = [6,1,1], tilesPerRank(B) = [3,1,1]
// Based on this configuration, we calculate the following:
// #shards(A) = 2, #splintersPerShard(A) = 1
// #shards(B) = 2, #splintersPerShard(B) = 3
// Each shard is placed on a separate rank in row-major order, so we will need
// 4 ranks in total. The splinters within each shard are allocated in
// round-robin, row-major order to the processors on the corresponding rank
// (some processors may receive more than 1 splinter). Assume each rank has 2
// CPU processors. Then the mapping will be as follows:
// Sample Tile -> Shard Splinter -> Rank CPU
// --------------------------------------------
// A [0,0,0] -> 0 0 -> 0 0
// --------------------------------------------
// A [0,1,0] -> 1 0 -> 1 0
// --------------------------------------------
// B [0,0,0] -> 0 0 -> 2 0
// B [1,0,0] -> 0 1 -> 2 1
// B [2,0,0] -> 0 2 -> 2 0
// --------------------------------------------
// B [3,0,0] -> 1 0 -> 3 0
// B [4,0,0] -> 1 1 -> 3 1
// B [5,0,0] -> 1 2 -> 3 0
//=============================================================================
// HELPER CODE
//=============================================================================
static Realm::Logger LOG("prometeo_mapper");
#define CHECK(cond, ...) \
do { \
if (!(cond)) { \
LOG.error(__VA_ARGS__); \
exit(1); \
} \
} while(0)
#define EQUALS(s1, s2) (strcmp((s1), (s2)) == 0)
#define STARTS_WITH(str, prefix) \
(strncmp((str), (prefix), sizeof(prefix) - 1) == 0)
static const void* first_arg(const Task& task) {
const char* ptr = static_cast<const char*>(task.args);
// Skip over Regent-added arguments.
// XXX: This assumes Regent's calling convention won't change.
return static_cast<const void*>(ptr + sizeof(uint64_t));
}
//=============================================================================
// INTRA-SAMPLE MAPPING
//=============================================================================
typedef unsigned SplinterID;
class SampleMapping;
class SplinteringFunctor : public ShardingFunctor {
private:
static ShardingID NEXT_ID;
public:
SplinteringFunctor(Runtime* rt, SampleMapping& parent)
: id(NEXT_ID++), parent_(parent) {
rt->register_sharding_functor(id, this, true);
}
public:
AddressSpace get_rank(const DomainPoint &point);
virtual SplinterID splinter(const DomainPoint &point) = 0;
public:
const ShardingID id;
protected:
SampleMapping& parent_;
};
ShardingID SplinteringFunctor::NEXT_ID = 12345;
class SampleMapping {
public:
class Tiling3DFunctor;
class Tiling2DFunctor;
class HardcodedFunctor;
class RankTiling3DFunctor;
class RankTiling2DFunctor;
class RankHardcodedFunctor;
public:
SampleMapping(Runtime* rt, const Config& config, AddressSpace first_rank)
: tiles_per_rank_{static_cast<unsigned>(config.Mapping.tilesPerRank[0]),
static_cast<unsigned>(config.Mapping.tilesPerRank[1]),
static_cast<unsigned>(config.Mapping.tilesPerRank[2])},
ranks_per_dim_{ static_cast<unsigned>(config.Mapping.tiles[0]
/ config.Mapping.tilesPerRank[0]),
static_cast<unsigned>(config.Mapping.tiles[1]
/ config.Mapping.tilesPerRank[1]),
static_cast<unsigned>(config.Mapping.tiles[2]
/ config.Mapping.tilesPerRank[2])},
first_rank_(first_rank),
tiling_3d_functor_(new Tiling3DFunctor(rt, *this)),
tiling_2d_functors_{{new Tiling2DFunctor(rt, *this, 0, false),
new Tiling2DFunctor(rt, *this, 0, true )},
{new Tiling2DFunctor(rt, *this, 1, false),
new Tiling2DFunctor(rt, *this, 1, true )},
{new Tiling2DFunctor(rt, *this, 2, false),
new Tiling2DFunctor(rt, *this, 2, true )}},
rank_tiling_3d_functor_{new RankTiling3DFunctor(rt, *this, false),
new RankTiling3DFunctor(rt, *this, true)},
rank_tiling_2d_functors_{{new RankTiling2DFunctor(rt, *this, 0, false),
new RankTiling2DFunctor(rt, *this, 0, true )},
{new RankTiling2DFunctor(rt, *this, 1, false),
new RankTiling2DFunctor(rt, *this, 1, true )},
{new RankTiling2DFunctor(rt, *this, 2, false),
new RankTiling2DFunctor(rt, *this, 2, true )}} {
for (unsigned x = 0; x < x_tiles(); ++x) {
for (unsigned y = 0; y < y_tiles(); ++y) {
for (unsigned z = 0; z < z_tiles(); ++z) {
hardcoded_functors_.push_back(new HardcodedFunctor(rt, *this, Point<3>(x,y,z)));
}
}
}
for (unsigned x = 0; x < ranks_per_dim_[0]; ++x) {
for (unsigned y = 0; y < ranks_per_dim_[1]; ++y) {
for (unsigned z = 0; z < ranks_per_dim_[2]; ++z) {
rank_hardcoded_functors_.push_back(new RankHardcodedFunctor(rt, *this, Point<3>(x,y,z)));
}
}
}
}
SampleMapping(const SampleMapping& rhs) = delete;
SampleMapping& operator=(const SampleMapping& rhs) = delete;
public:
AddressSpace get_rank(ShardID shard_id) const {
return first_rank_ + shard_id;
}
unsigned num_ranks() const {
return ranks_per_dim_[0] * ranks_per_dim_[1] * ranks_per_dim_[2];
}
unsigned x_tiles() const {
return tiles_per_rank_[0] * ranks_per_dim_[0];
}
unsigned y_tiles() const {
return tiles_per_rank_[1] * ranks_per_dim_[1];
}
unsigned z_tiles() const {
return tiles_per_rank_[2] * ranks_per_dim_[2];
}
unsigned num_tiles() const {
return x_tiles() * y_tiles() * z_tiles();
}
Tiling3DFunctor* tiling_3d_functor() {
return tiling_3d_functor_;
}
Tiling2DFunctor* tiling_2d_functor(int dim, bool dir) {
assert(0 <= dim && dim < 3);
return tiling_2d_functors_[dim][dir];
}
HardcodedFunctor* hardcoded_functor(const DomainPoint& tile) {
assert(tile.get_dim() == 3);
assert(0 <= tile[0] && tile[0] < x_tiles());
assert(0 <= tile[1] && tile[1] < y_tiles());
assert(0 <= tile[2] && tile[2] < z_tiles());
return hardcoded_functors_[tile[0] * y_tiles() * z_tiles() +
tile[1] * z_tiles() +
tile[2]];
}
RankTiling3DFunctor* rank_tiling_3d_functor(bool fold = false) {
return rank_tiling_3d_functor_[fold];
}
RankTiling2DFunctor* rank_tiling_2d_functor(int dim, bool dir) {
assert(0 <= dim && dim < 3);
return rank_tiling_2d_functors_[dim][dir];
}
RankHardcodedFunctor* rank_hardcoded_functor(const DomainPoint& tile) {
assert(tile.get_dim() == 3);
assert(0 <= tile[0] && tile[0] < ranks_per_dim_[0]);
assert(0 <= tile[1] && tile[1] < ranks_per_dim_[1]);
assert(0 <= tile[2] && tile[2] < ranks_per_dim_[2]);
return rank_hardcoded_functors_[tile[0] * ranks_per_dim_[1] * ranks_per_dim_[2] +
tile[1] * ranks_per_dim_[2] +
tile[2]];
}
public:
// Maps tasks in a 3D index space launch according to the default tiling
// logic (see description above).
class Tiling3DFunctor : public SplinteringFunctor {
public:
Tiling3DFunctor(Runtime* rt, SampleMapping& parent)
: SplinteringFunctor(rt, parent) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
assert(point.get_dim() == 3);
CHECK(0 <= point[0] && point[0] < parent_.x_tiles() &&
0 <= point[1] && point[1] < parent_.y_tiles() &&
0 <= point[2] && point[2] < parent_.z_tiles(),
"Unexpected point on index space launch");
return (point[0] / parent_.tiles_per_rank_[0]) * parent_.ranks_per_dim_[1]
* parent_.ranks_per_dim_[2] +
(point[1] / parent_.tiles_per_rank_[1]) * parent_.ranks_per_dim_[2] +
(point[2] / parent_.tiles_per_rank_[2]);
}
virtual SplinterID splinter(const DomainPoint &point) {
assert(point.get_dim() == 3);
CHECK(0 <= point[0] && point[0] < parent_.x_tiles() &&
0 <= point[1] && point[1] < parent_.y_tiles() &&
0 <= point[2] && point[2] < parent_.z_tiles(),
"Unexpected point on index space launch");
return (point[0] % parent_.tiles_per_rank_[0]) * parent_.tiles_per_rank_[1]
* parent_.tiles_per_rank_[2] +
(point[1] % parent_.tiles_per_rank_[1]) * parent_.tiles_per_rank_[2] +
(point[2] % parent_.tiles_per_rank_[2]);
}
};
// Maps tasks in a 2D index space launch, by extending each domain point to a
// 3D tile and deferring to the default strategy.
// Parameter `dim` controls which dimension to add.
// Parameter `dir` controls which extreme of that dimension to set.
class Tiling2DFunctor : public SplinteringFunctor {
public:
Tiling2DFunctor(Runtime* rt, SampleMapping& parent,
unsigned dim, bool dir)
: SplinteringFunctor(rt, parent), dim_(dim), dir_(dir) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
return parent_.tiling_3d_functor_->shard
(to_point_3d(point), full_space, total_shards);
}
virtual SplinterID splinter(const DomainPoint &point) {
return parent_.tiling_3d_functor_->splinter(to_point_3d(point));
}
private:
DomainPoint to_point_3d(const DomainPoint& point) const {
assert(point.get_dim() == 2);
unsigned coord =
(dim_ == 0) ? (dir_ ? 0 : parent_.x_tiles()-1) :
(dim_ == 1) ? (dir_ ? 0 : parent_.y_tiles()-1) :
/*dim_ == 2*/ (dir_ ? 0 : parent_.z_tiles()-1) ;
return
(dim_ == 0) ? Point<3>(coord, point[0], point[1]) :
(dim_ == 1) ? Point<3>(point[0], coord, point[1]) :
/*dim_ == 2*/ Point<3>(point[0], point[1], coord) ;
}
private:
unsigned dim_;
bool dir_;
};
// Maps every task to the same shard & splinter (the ones corresponding to
// the tile specified in the constructor).
class HardcodedFunctor : public SplinteringFunctor {
public:
HardcodedFunctor(Runtime* rt,
SampleMapping& parent,
const DomainPoint& tile)
: SplinteringFunctor(rt, parent), tile_(tile) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
return parent_.tiling_3d_functor_->shard(tile_, full_space, total_shards);
}
virtual SplinterID splinter(const DomainPoint &point) {
return parent_.tiling_3d_functor_->splinter(tile_);
}
private:
DomainPoint tile_;
};
// Maps tasks in a 3D index space launch on ranks according to
// the default tiling logic (see description above).
class RankTiling3DFunctor : public SplinteringFunctor {
public:
RankTiling3DFunctor(Runtime* rt, SampleMapping& parent, bool fold)
: SplinteringFunctor(rt, parent), fold_(fold) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
assert(point.get_dim() == 3);
unsigned x = fold_ ? point[0] % parent_.ranks_per_dim_[0] : point[0];
unsigned y = fold_ ? point[1] % parent_.ranks_per_dim_[1] : point[1];
unsigned z = fold_ ? point[2] % parent_.ranks_per_dim_[2] : point[2];
CHECK(0 <= x && x < parent_.ranks_per_dim_[0] &&
0 <= y && y < parent_.ranks_per_dim_[1] &&
0 <= z && z < parent_.ranks_per_dim_[2],
"Unexpected point on index space launch");
return x * parent_.ranks_per_dim_[1]
* parent_.ranks_per_dim_[2] +
y * parent_.ranks_per_dim_[2] +
z;
}
virtual SplinterID splinter(const DomainPoint &point) {
assert(point.get_dim() == 3);
unsigned x = fold_ ? point[0] % parent_.ranks_per_dim_[0] : point[0];
unsigned y = fold_ ? point[1] % parent_.ranks_per_dim_[1] : point[1];
unsigned z = fold_ ? point[2] % parent_.ranks_per_dim_[2] : point[2];
CHECK(0 <= x && x < parent_.ranks_per_dim_[0] &&
0 <= y && y < parent_.ranks_per_dim_[1] &&
0 <= z && z < parent_.ranks_per_dim_[2],
"Unexpected point on index space launch");
return x * parent_.tiles_per_rank_[1]
* parent_.tiles_per_rank_[2] +
y * parent_.tiles_per_rank_[2] +
z;
}
private:
bool fold_;
};
// Maps tasks in a 2D index space launch, by extending each domain point to a
// 3D tile and deferring to the default strategy.
// Parameter `dim` controls which dimension to add.
// Parameter `dir` controls which extreme of that dimension to set.
class RankTiling2DFunctor : public SplinteringFunctor {
public:
RankTiling2DFunctor(Runtime* rt, SampleMapping& parent,
unsigned dim, bool dir)
: SplinteringFunctor(rt, parent), dim_(dim), dir_(dir) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
return parent_.rank_tiling_3d_functor_[0]->shard
(to_point_3d(point), full_space, total_shards);
}
virtual SplinterID splinter(const DomainPoint &point) {
return parent_.rank_tiling_3d_functor_[0]->splinter(to_point_3d(point));
}
private:
DomainPoint to_point_3d(const DomainPoint& point) const {
assert(point.get_dim() == 2);
unsigned coord =
(dim_ == 0) ? (dir_ ? 0 : parent_.ranks_per_dim_[0]-1) :
(dim_ == 1) ? (dir_ ? 0 : parent_.ranks_per_dim_[1]-1) :
/*dim_ == 2*/ (dir_ ? 0 : parent_.ranks_per_dim_[1]-1) ;
return
(dim_ == 0) ? Point<3>(coord, point[0], point[1]) :
(dim_ == 1) ? Point<3>(point[0], coord, point[1]) :
/*dim_ == 2*/ Point<3>(point[0], point[1], coord) ;
}
private:
unsigned dim_;
bool dir_;
};
// Maps every task to the same shard & splinter (the ones corresponding to
// the rank specified in the constructor).
class RankHardcodedFunctor : public SplinteringFunctor {
public:
RankHardcodedFunctor(Runtime* rt,
SampleMapping& parent,
const DomainPoint& tile)
: SplinteringFunctor(rt, parent), tile_(tile) {}
public:
virtual ShardID shard(const DomainPoint& point,
const Domain& full_space,
const size_t total_shards) {
return parent_.rank_tiling_3d_functor_[0]->shard(tile_, full_space, total_shards);
}
virtual SplinterID splinter(const DomainPoint &point) {
return parent_.rank_tiling_3d_functor_[0]->splinter(tile_);
}
private:
DomainPoint tile_;
};
private:
unsigned tiles_per_rank_[3];
unsigned ranks_per_dim_[3];
AddressSpace first_rank_;
Tiling3DFunctor* tiling_3d_functor_;
Tiling2DFunctor* tiling_2d_functors_[3][2];
std::vector<HardcodedFunctor*> hardcoded_functors_;
RankTiling3DFunctor* rank_tiling_3d_functor_[2];
RankTiling2DFunctor* rank_tiling_2d_functors_[3][2];
std::vector<RankHardcodedFunctor*> rank_hardcoded_functors_;
};
AddressSpace SplinteringFunctor::get_rank(const DomainPoint &point) {
return parent_.get_rank(shard(point, Domain(), 0));
}
//=============================================================================
// MAPPER CLASS: CONSTRUCTOR
//=============================================================================
class PrometeoMapper : public DefaultMapper {
public:
PrometeoMapper(Runtime* rt, Machine machine, Processor local)
: DefaultMapper(rt->get_mapper_runtime(), machine, local, "prometeo_mapper"),
all_procs_(remote_cpus.size()) {
// Set the umask of the process to clear S_IWGRP and S_IWOTH.
umask(022);
// Assign ranks sequentially to samples, each sample getting one rank for
// each super-tile.
AddressSpace reqd_ranks = 0;
auto process_config = [&](const Config& config) {
CHECK(config.Mapping.tiles[0] > 0 &&
config.Mapping.tiles[1] > 0 &&
config.Mapping.tiles[2] > 0 &&
config.Mapping.tilesPerRank[0] > 0 &&
config.Mapping.tilesPerRank[1] > 0 &&
config.Mapping.tilesPerRank[2] > 0 &&
config.Mapping.tiles[0] % config.Mapping.tilesPerRank[0] == 0 &&
config.Mapping.tiles[1] % config.Mapping.tilesPerRank[1] == 0 &&
config.Mapping.tiles[2] % config.Mapping.tilesPerRank[2] == 0,
"Invalid tiling for sample %lu", sample_mappings_.size() + 1);
sample_mappings_.emplace_back(rt, config, reqd_ranks);
};
// Locate all config files specified on the command-line arguments.
InputArgs args = Runtime::get_input_args();
for (int i = 0; i < args.argc; ++i) {
if (EQUALS(args.argv[i], "-i") && i < args.argc-1) {
Config config;
parse_Config(&config, args.argv[i+1]);
process_config(config);
reqd_ranks += sample_mappings_.back().num_ranks();
}
}
// Verify that we have enough ranks.
unsigned supplied_ranks = remote_cpus.size();
CHECK(reqd_ranks <= supplied_ranks,
"%u rank(s) required, but %u rank(s) supplied to Legion",
reqd_ranks, supplied_ranks);
if (reqd_ranks < supplied_ranks) {
LOG.warning() << supplied_ranks << " rank(s) supplied to Legion,"
<< " but only " << reqd_ranks << " required";
}
// Cache processor information.
Machine::ProcessorQuery query(machine);
for (auto it = query.begin(); it != query.end(); it++) {
AddressSpace rank = it->address_space();
Processor::Kind kind = it->kind();
get_procs(rank, kind).push_back(*it);
}
// Verify machine configuration.
for (AddressSpace rank = 0; rank < remote_cpus.size(); ++rank) {
CHECK(get_procs(rank, Processor::IO_PROC).size() > 0,
"No IO processor on rank %u", rank);
}
}
//=============================================================================
// MAPPER CLASS: MAPPING LOGIC
//=============================================================================
private:
std::vector<unsigned> find_sample_ids(const MapperContext ctx,
const Task& task) const {
std::vector<unsigned> sample_ids;
// Tasks called on regions: read the SAMPLE_ID_TAG from the region
if (task.is_index_space ||
EQUALS(task.get_task_name(), "DummyAverages") ||
EQUALS(task.get_task_name(), "ComputeRecycleAveragePosition") ||
EQUALS(task.get_task_name(), "InitializeBoundarLayerData") ||
EQUALS(task.get_task_name(), "GetRescalingData") ||
EQUALS(task.get_task_name(), "cache_grid_translation") ||
STARTS_WITH(task.get_task_name(), "FastInterp") ||
STARTS_WITH(task.get_task_name(), "readTileAttr")) {
CHECK(!task.regions.empty(),
"Expected region argument in call to %s", task.get_task_name());
const RegionRequirement& req = task.regions[0];
LogicalRegion region = req.region.exists() ? req.region
: runtime->get_parent_logical_region(ctx, req.partition);
region = get_root(ctx, region);
const void* info = NULL;
size_t info_size = 0;
bool success = runtime->retrieve_semantic_information
(ctx, region, SAMPLE_ID_TAG, info, info_size,
false/*can_fail*/, true/*wait_until_ready*/);
CHECK(success, "Missing SAMPLE_ID_TAG semantic information on region");
assert(info_size == sizeof(unsigned));
sample_ids.push_back(*static_cast<const unsigned*>(info));
}
// Tasks with Config as 1st argument: read config.Mapping.sampleId
else if (EQUALS(task.get_task_name(), "workSingle")) {
const Config* config = static_cast<const Config*>(first_arg(task));
sample_ids.push_back(static_cast<unsigned>(config->Mapping.sampleId));
}
// Helper & I/O tasks: go up one level to the work task
else if (STARTS_WITH(task.get_task_name(), "Exports.Console_Write") ||
STARTS_WITH(task.get_task_name(), "Exports.Probe_Write") ||
EQUALS(task.get_task_name(), "Exports.createDir") ||
EQUALS(task.get_task_name(), "__dummy") ||
STARTS_WITH(task.get_task_name(), "__unary_") ||
STARTS_WITH(task.get_task_name(), "__binary_") ||
STARTS_WITH(task.get_task_name(), "AffineTransform")) {
assert(task.parent_task != NULL);
sample_ids = find_sample_ids(ctx, *(task.parent_task));
}
// Other tasks: fail and notify the user
else {
CHECK(false, "Unhandled task in find_sample_ids: %s",
task.get_task_name());
}
// Sanity checks
assert(!sample_ids.empty());
for (unsigned sample_id : sample_ids) {
assert(sample_id < sample_mappings_.size());
}
return sample_ids;
}
unsigned find_sample_id(const MapperContext ctx, const Task& task) const {
return find_sample_ids(ctx, task)[0];
}
DomainPoint find_tile(const MapperContext ctx,
const Task& task) const {
// 3D index space tasks that are launched individually
if (STARTS_WITH(task.get_task_name(), "readTileAttr")) {
assert(!task.regions.empty() && task.regions[0].region.exists());
DomainPoint tile =
runtime->get_logical_region_color_point(ctx, task.regions[0].region);
return tile;
}
// Tasks that should run on the first rank of their sample's allocation
else if (EQUALS(task.get_task_name(), "workSingle") ||
EQUALS(task.get_task_name(), "workDual") ||
EQUALS(task.get_task_name(), "cache_grid_translation") ||
STARTS_WITH(task.get_task_name(), "Exports.Console_Write") ||
STARTS_WITH(task.get_task_name(), "Exports.Probe_Write") ||
EQUALS(task.get_task_name(), "Exports.createDir") ||
EQUALS(task.get_task_name(), "__dummy") ||
EQUALS(task.get_task_name(), "DummyAverages") ||
EQUALS(task.get_task_name(), "ComputeRecycleAveragePosition") ||
EQUALS(task.get_task_name(), "InitializeBoundarLayerData") ||
EQUALS(task.get_task_name(), "GetRescalingData") ||
STARTS_WITH(task.get_task_name(), "FastInterp") ||
STARTS_WITH(task.get_task_name(), "__unary_") ||
STARTS_WITH(task.get_task_name(), "__binary_") ||
STARTS_WITH(task.get_task_name(), "AffineTransform")) {
return Point<3>(0,0,0);
}
// Other tasks: fail and notify the user
else {
CHECK(false, "Unhandled task in find_tile: %s", task.get_task_name());
return Point<3>(0,0,0);
}
}
SplinteringFunctor* pick_functor(const MapperContext ctx,
const Task& task) {
// 3D index space tasks
if (task.is_index_space && task.index_domain.get_dim() == 3) {
unsigned sample_id = find_sample_id(ctx, task);
SampleMapping& mapping = sample_mappings_[sample_id];
// IO of 3D partitioned regions is managed by each rank
if (STARTS_WITH(task.get_task_name(), "dumpTile") ||
STARTS_WITH(task.get_task_name(), "loadTile") ||
STARTS_WITH(task.get_task_name(), "writeTileAttr")) {
return mapping.rank_tiling_3d_functor(true);
} else {
return mapping.tiling_3d_functor();
}
}
// 2D index space tasks
else if (task.is_index_space && task.index_domain.get_dim() == 2) {
unsigned sample_id = find_sample_id(ctx, task);
SampleMapping& mapping = sample_mappings_[sample_id];
// IO of 2D partitioned regions
if (STARTS_WITH(task.get_task_name(), "dumpTile") ||
STARTS_WITH(task.get_task_name(), "loadTile") ||
STARTS_WITH(task.get_task_name(), "writeTileAttr")) {
return mapping.hardcoded_functor(Point<3>(0,0,0));
} else {
CHECK(false, "Unexpected 2D domain on index space launch of task %s",
task.get_task_name());
return NULL;
}
}
// Sample-specific tasks that are launched individually on each tile
else if (EQUALS(task.get_task_name(), "workSingle") ||
EQUALS(task.get_task_name(), "workDual") ||
EQUALS(task.get_task_name(), "DummyAverages") ||
EQUALS(task.get_task_name(), "ComputeRecycleAveragePosition") ||
EQUALS(task.get_task_name(), "InitializeBoundarLayerData") ||
EQUALS(task.get_task_name(), "GetRescalingData") ||
EQUALS(task.get_task_name(), "cache_grid_translation") ||
STARTS_WITH(task.get_task_name(), "FastInterp") ||
STARTS_WITH(task.get_task_name(), "Exports.Console_Write") ||
STARTS_WITH(task.get_task_name(), "Exports.Probe_Write") ||
EQUALS(task.get_task_name(), "Exports.createDir") ||
EQUALS(task.get_task_name(), "__dummy") ||
STARTS_WITH(task.get_task_name(), "__unary_") ||
STARTS_WITH(task.get_task_name(), "__binary_") ||
STARTS_WITH(task.get_task_name(), "AffineTransform")) {
unsigned sample_id = find_sample_id(ctx, task);
SampleMapping& mapping = sample_mappings_[sample_id];
DomainPoint tile = find_tile(ctx, task);
return mapping.hardcoded_functor(tile);
}
// Sample-specific tasks that are launched individually on each rank
else if (STARTS_WITH(task.get_task_name(), "readTileAttr")) {
unsigned sample_id = find_sample_id(ctx, task);
SampleMapping& mapping = sample_mappings_[sample_id];
DomainPoint tile = find_tile(ctx, task);
return mapping.rank_hardcoded_functor(tile);
}
// Other tasks: fail and notify the user
else {
CHECK(false, "Unhandled task in pick_functor: %s", task.get_task_name());
return NULL;
}
}
//=============================================================================
// MAPPER CLASS: MAJOR OVERRIDES
//=============================================================================
public:
// Control-replicate work tasks.
virtual void select_task_options(const MapperContext ctx,
const Task& task,
TaskOptions& output) {
DefaultMapper::select_task_options(ctx, task, output);
output.replicate =
EQUALS(task.get_task_name(), "workSingle") ||
EQUALS(task.get_task_name(), "workDual");
}
virtual void default_policy_rank_processor_kinds(MapperContext ctx,
const Task& task,
std::vector<Processor::Kind>& ranking) {
// Work tasks: map to IO processors, so they don't get blocked by tiny
// CPU tasks.
if (EQUALS(task.get_task_name(), "workSingle") ||
EQUALS(task.get_task_name(), "workDual")) {
ranking.push_back(Processor::IO_PROC);
}
// Other tasks: defer to the default mapping policy
else {
DefaultMapper::default_policy_rank_processor_kinds(ctx, task, ranking);
}
}
#ifndef NO_LEGION_CONTROL_REPLICATION
// Replicate each work task over all ranks assigned to the corresponding
// sample(s).
virtual void map_replicate_task(const MapperContext ctx,
const Task& task,
const MapTaskInput& input,
const MapTaskOutput& default_output,
MapReplicateTaskOutput& output) {
// Read configuration.
assert(!runtime->is_MPI_interop_configured(ctx));
assert(EQUALS(task.get_task_name(), "workSingle") ||
EQUALS(task.get_task_name(), "workDual"));
VariantInfo info =
default_find_preferred_variant(task, ctx, false/*needs_tight_bound*/);
CHECK(task.regions.empty() && info.is_replicable,
"Unexpected features on work task");
std::vector<unsigned> sample_ids = find_sample_ids(ctx, task);
// Create a replicant on the first CPU processor of each sample's ranks.
for (unsigned sample_id : sample_ids) {
const SampleMapping& mapping = sample_mappings_[sample_id];
for (ShardID shard_id = 0; shard_id < mapping.num_ranks(); ++shard_id) {
AddressSpace rank = mapping.get_rank(shard_id);
Processor target_proc = get_procs(rank, info.proc_kind)[0];
output.task_mappings.push_back(default_output);
output.task_mappings.back().chosen_variant = info.variant;
output.task_mappings.back().target_procs.push_back(target_proc);
output.control_replication_map.push_back(target_proc);
}
}
}
#endif
// NOTE: Will only run if Legion is compiled with dynamic control replication.
virtual void select_sharding_functor(const MapperContext ctx,
const Task& task,
const SelectShardingFunctorInput& input,
SelectShardingFunctorOutput& output) {
output.chosen_functor = pick_functor(ctx, task)->id;
}
virtual Processor default_policy_select_initial_processor(MapperContext ctx,
const Task& task) {
// Index space tasks: defer to the default mapping policy; slice_task will
// eventually be called to do the mapping properly
if (task.is_index_space) {
return DefaultMapper::default_policy_select_initial_processor(ctx, task);
}
// Main task: defer to the default mapping policy
else if (EQUALS(task.get_task_name(), "main")) {
return DefaultMapper::default_policy_select_initial_processor(ctx, task);
}
// Other tasks
else {
unsigned sample_id = find_sample_id(ctx, task);
DomainPoint tile = find_tile(ctx, task);
VariantInfo info = default_find_preferred_variant(task, ctx, false/*needs_tight_bound*/);
SplinteringFunctor* functor = pick_functor(ctx, task);
Processor target_proc = select_proc(tile, info.proc_kind, functor);
LOG.debug() << "Sample " << sample_id
<< ": Task " << task.get_task_name()
<< ": Sequential launch"
<< ": Tile " << tile
<< ": Processor " << target_proc;
return target_proc;
}
}
virtual void slice_task(const MapperContext ctx,
const Task& task,
const SliceTaskInput& input,
SliceTaskOutput& output) {
output.verify_correctness = false;
unsigned sample_id = find_sample_id(ctx, task);
VariantInfo info = default_find_preferred_variant(task, ctx, false/*needs_tight_bound*/);
SplinteringFunctor* functor = pick_functor(ctx, task);
for (Domain::DomainPointIterator it(input.domain); it; it++) {
Processor target_proc = select_proc(it.p, info.proc_kind, functor);
output.slices.emplace_back(Domain(it.p, it.p), target_proc,
false/*recurse*/, false/*stealable*/);
LOG.debug() << "Sample " << sample_id
<< ": Task " << task.get_task_name()
<< ": Index space launch"
<< ": Tile " << it.p
<< ": Processor " << target_proc;
}
}
virtual TaskPriority default_policy_select_task_priority(MapperContext ctx,
const Task& task) {
// Unless handled specially below, all tasks have the same priority.
int priority = 0;
// Increase priority of tasks on the critical path of the fluid solve.
if (STARTS_WITH(task.get_task_name(), "Exports.GetVelocityGradients") ||
STARTS_WITH(task.get_task_name(), "UpdateShockSensor") ||
STARTS_WITH(task.get_task_name(), "UpdateUsingHybridEulerFlux") ||
STARTS_WITH(task.get_task_name(), "UpdateUsingTENOAEulerFlux") ||
STARTS_WITH(task.get_task_name(), "UpdateUsingDiffusionFlux") ||
// STARTS_WITH(task.get_task_name(), "CorrectUsingFlux") ||
STARTS_WITH(task.get_task_name(), "UpdateVars") ||
STARTS_WITH(task.get_task_name(), "Exports.UpdateChemistry")) {
priority = 1;
}
return priority;
}
// Send each cross-section explicit copy to the first rank of the first
// section, to be mapped further.
// NOTE: Will only run if Legion is compiled with dynamic control replication.
virtual void select_sharding_functor(const MapperContext ctx,
const Copy& copy,
const SelectShardingFunctorInput& input,
SelectShardingFunctorOutput& output) {
CHECK(copy.parent_task != NULL &&
EQUALS(copy.parent_task->get_task_name(), "workDual"),
"Unsupported: Sharded copy outside of workDual");
unsigned sample_id = find_sample_id(ctx, *(copy.parent_task));
SampleMapping& mapping = sample_mappings_[sample_id];
output.chosen_functor = mapping.hardcoded_functor(Point<3>(0,0,0))->id;
}
virtual void map_copy(const MapperContext ctx,
const Copy& copy,
const MapCopyInput& input,
MapCopyOutput& output) {
// For HDF copies, defer to the default mapping policy.
if (EQUALS(copy.parent_task->get_task_name(), "dumpTile") ||
EQUALS(copy.parent_task->get_task_name(), "loadTile")) {
DefaultMapper::map_copy(ctx, copy, input, output);
return;
}
CHECK(false, "Unsupported: map_copy");
}
virtual void select_sharding_functor(const MapperContext ctx,
const Fill& fill,
const SelectShardingFunctorInput& input,
SelectShardingFunctorOutput& output) {
CHECK(fill.parent_task != NULL,
"Unsupported: Sharded Fill does not have parent partition");
if (fill.is_index_space && fill.index_domain.get_dim() == 3) {
unsigned sample_id = find_sample_id(ctx, *(fill.parent_task));
SampleMapping& mapping = sample_mappings_[sample_id];
output.chosen_functor = mapping.rank_tiling_3d_functor(true)->id;
LOG.debug() << "Sample " << sample_id
<< ": Fill with parent task " << fill.parent_task->get_task_name()
<< ": sharded using rank_tiling_3d_functor";
} else {
output.chosen_functor = pick_functor(ctx, *(fill.parent_task))->id;
LOG.debug() << ": Fill parent task " << fill.parent_task->get_task_name()
<< ": sharded using pick_functor";
}
}
// NOTE: Will only run if Legion is compiled with dynamic control replication.
// Send each dependent partition operation to the rank corresponding
// to its tile (3d index_space launched) or to the rank of its parent task.
virtual void select_sharding_functor(const MapperContext ctx,
const Partition& partition,
const SelectShardingFunctorInput& input,
SelectShardingFunctorOutput& output) {
CHECK(partition.parent_task != NULL &&
(EQUALS(partition.parent_task->get_task_name(), "workSingle") ||
EQUALS(partition.parent_task->get_task_name(), "workDual")),
"Unsupported: Sharded partition outside of workSingle or workDual");
unsigned sample_id = find_sample_id(ctx, *(partition.parent_task));
SampleMapping& mapping = sample_mappings_[sample_id];
// const char *name = get_partition_name(ctx, partition.requirement.partition);
// CHECK(name != NULL, "Found an unnamed partition");
// 3D index space tasks
if (partition.is_index_space && partition.index_domain.get_dim() == 3) {
output.chosen_functor = mapping.rank_tiling_3d_functor(true)->id;
LOG.debug() << "Sample " << sample_id
<< ": Partition parent task " << partition.parent_task->get_task_name()
// << ": Partition parent partition " << name
<< ": sharded using rank_tiling_3d_functor";
} else {
LOG.debug() << "Sample " << sample_id
<< ": Partition parent task " << partition.parent_task->get_task_name()
// << ": Partition parent partition " << name
<< ": sharded using pick_functor";
output.chosen_functor = pick_functor(ctx, *(partition.parent_task))->id;
}
}
//=============================================================================
// MAPPER CLASS: MINOR OVERRIDES
//=============================================================================
public:
// TODO: Select appropriate memories for instances that will be communicated,
// (e.g. parallelizer-created ghost partitions), such as RDMA memory,
// zero-copy memory.
virtual Memory default_policy_select_target_memory(MapperContext ctx,
Processor target_proc,
const RegionRequirement& req) {
return DefaultMapper::default_policy_select_target_memory(ctx, target_proc, req);
}
// Disable an optimization done by the default mapper (attempts to reuse an
// instance that covers a superset of the requested index space, by searching
// higher up the partition tree).
virtual LogicalRegion default_policy_select_instance_region(MapperContext ctx,
Memory target_memory,
const RegionRequirement& req,
const LayoutConstraintSet& constraints,
bool force_new_instances,
bool meets_constraints) {
// A root region does not need any special care
if (!runtime->has_parent_logical_partition(ctx, req.region)) {
LOG.debug() << "Root region assigned to its own instance";
return req.region;
}
LogicalPartition parent_partition = runtime->get_parent_logical_partition(ctx, req.region);
const char *name = get_partition_name(ctx, parent_partition);
CHECK(name != NULL, "Found an unnamed partition");
if (EQUALS(name, "p_All") ||
EQUALS(name, "p_Interior") ||
EQUALS(name, "p_AllBCs") ||
EQUALS(name, "p_solved") ||
EQUALS(name, "p_GradientGhosts") ||
EQUALS(name, "p_MetricGhosts") ||
EQUALS(name, "p_x_divg") || EQUALS(name, "p_y_divg") || EQUALS(name, "p_z_divg") ||
EQUALS(name, "p_x_faces") || EQUALS(name, "p_y_faces") || EQUALS(name, "p_z_faces") ||
EQUALS(name, "p_XFluxGhosts") || EQUALS(name, "p_YFluxGhosts") || EQUALS(name, "p_ZFluxGhosts") ||
EQUALS(name, "p_XDiffGhosts") || EQUALS(name, "p_YDiffGhosts") || EQUALS(name, "p_ZDiffGhosts") ||
EQUALS(name, "p_XEulerGhosts2") || EQUALS(name, "p_YEulerGhosts2") || EQUALS(name, "p_ZEulerGhosts2") ||
EQUALS(name, "p_XSensorGhosts2") || EQUALS(name, "p_YSensorGhosts2") || EQUALS(name, "p_ZSensorGhosts2") ||
EQUALS(name, "p_XEulerGhosts") || EQUALS(name, "p_YEulerGhosts") || EQUALS(name, "p_ZEulerGhosts") ||
EQUALS(name, "p_XSensorGhosts") || EQUALS(name, "p_YSensorGhosts") || EQUALS(name, "p_ZSensorGhosts") ||
EQUALS(name, "p_Fluid_YZAvg") || EQUALS(name, "p_Fluid_XZAvg") || EQUALS(name, "p_Fluid_XYAvg") ||
EQUALS(name, "p_Fluid_XAvg") || EQUALS(name, "p_Fluid_YAvg") || EQUALS(name, "p_Fluid_ZAvg") ||
EQUALS(name, "BCPlane")) {
DomainPoint tile = runtime->get_logical_region_color_point(ctx, req.region);
LogicalRegion root_region = get_root(ctx, req.region);
LogicalPartition primary_partition = get_partition_by_name(ctx, root_region, "p_AllWithGhosts");
if (primary_partition == LogicalPartition::NO_PART) {
// If p_AllWithGhosts has not been created yet use p_All
LOG.debug() << "Region of " << name
<< ": Tile " << tile
<< " is mapped on corresponding instance of p_All";
LogicalPartition p_All = get_partition_by_name(ctx, root_region, "p_All");
assert(p_All != LogicalPartition::NO_PART);
return runtime->get_logical_subregion_by_color(ctx, p_All, tile);
} else {
// otherwise map everything on p_AllWithGhosts
LOG.debug() << "Region of " << name
<< ": Tile " << tile
<< " is mapped on corresponding instance of p_AllWithGhosts";
assert(primary_partition != LogicalPartition::NO_PART);
return runtime->get_logical_subregion_by_color(ctx, primary_partition, tile);
}
}
LOG.debug() << "Region of " << name << " is mapped on its own instance";
return req.region;
}
//--------------------------------------------------------------------------
virtual void default_policy_select_sources(MapperContext ctx,
const PhysicalInstance &target,
const std::vector<PhysicalInstance> &sources,
std::deque<PhysicalInstance> &ranking)
//--------------------------------------------------------------------------
{
// Let the default mapper sort the sources by bandwidth
DefaultMapper::default_policy_select_sources(ctx, target, sources, ranking);
// std::ostringstream srank1;
// for (std::deque<PhysicalInstance>::const_iterator it = ranking.begin();
// it != ranking.end(); it++)
// {
// srank1 << *it << " ";
// }
// LOG.debug() << "Default rank for " << target << ": " << srank1.str().c_str();
// Give priority to those with better overlapping
std::vector<std::pair<PhysicalInstance,unsigned/*size of intersection*/>>
cover_ranking(sources.size());
Domain target_domain = target.get_instance_domain();
for (std::deque<PhysicalInstance>::const_reverse_iterator it = ranking.rbegin();
it != ranking.rend(); it++)
{
const unsigned idx = it - ranking.rbegin();
const PhysicalInstance &source = (*it);
Domain source_domain = source.get_instance_domain();
Domain intersection = source_domain.intersection(target_domain);
cover_ranking[idx] = std::pair<PhysicalInstance,unsigned>(source,intersection.get_volume());
}
// Sort them by the size of intersecting area
std::stable_sort(cover_ranking.begin(), cover_ranking.end(), physical_sort_func);
// Iterate from largest intersection, bandwidth to smallest
ranking.clear();
for (std::vector<std::pair<PhysicalInstance,unsigned>>::
const_reverse_iterator it = cover_ranking.rbegin();
it != cover_ranking.rend(); it++)
ranking.push_back(it->first);
// std::ostringstream srank2;
// for (std::deque<PhysicalInstance>::const_iterator it = ranking.begin();
// it != ranking.end(); it++)