forked from StanfordLegion/task-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
1161 lines (972 loc) · 45.1 KB
/
main.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
/* Copyright 2020 Stanford University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "main.h"
#include <algorithm>
#include <climits>
#include <vector>
#include <float.h>
#include "realm.h"
#include "core.h"
using namespace Realm;
using namespace Realm::Serialization;
TYPE_IS_SERIALIZABLE(TaskGraph);
// Hack: Track argc/argv in globals to avoid needing to serialize arguments to top_level task.
static int global_argc = 0;
static char **global_argv = NULL;
enum {
TOP_LEVEL_TASK = Processor::TASK_ID_FIRST_AVAILABLE,
SHARD_TASK,
LEAF_TASK,
};
enum {
REDOP_MIN = 11,
REDOP_MAX = 12,
};
enum {
FID_FIRST = 100,
};
#define DECLARE_REDUCTION(CLASS, T, U, APPLY_OP, FOLD_OP, ID) \
class CLASS { \
public: \
typedef T LHS, RHS; \
template <bool EXCLUSIVE> \
static void apply(LHS &lhs, RHS rhs); \
template <bool EXCLUSIVE> \
static void fold(RHS &rhs1, RHS rhs2); \
static const T identity; \
}; \
\
const T CLASS::identity = ID; \
\
template <> \
void CLASS::apply<true>(LHS & lhs, RHS rhs) \
{ \
lhs = APPLY_OP(lhs, rhs); \
} \
\
template <> \
void CLASS::apply<false>(LHS & lhs, RHS rhs) \
{ \
volatile U *target = (U *)&(lhs); \
union { \
U as_U; \
T as_T; \
} oldval, newval; \
do { \
oldval.as_U = *target; \
newval.as_T = APPLY_OP(oldval.as_T, rhs); \
} while (!__sync_bool_compare_and_swap(target, oldval.as_U, newval.as_U)); \
} \
\
template <> \
void CLASS::fold<true>(RHS & rhs1, RHS rhs2) \
{ \
rhs1 = FOLD_OP(rhs1, rhs2); \
} \
\
template <> \
void CLASS::fold<false>(RHS & rhs1, RHS rhs2) \
{ \
volatile U *target = (U *)&rhs1; \
union { \
U as_U; \
T as_T; \
} oldval, newval; \
do { \
oldval.as_U = *target; \
newval.as_T = FOLD_OP(oldval.as_T, rhs2); \
} while (!__sync_bool_compare_and_swap(target, oldval.as_U, newval.as_U)); \
}
DECLARE_REDUCTION(RedopMin, unsigned long long, unsigned long long,
std::min, std::min, std::numeric_limits<unsigned long long>::max())
DECLARE_REDUCTION(RedopMax, unsigned long long, unsigned long long,
std::max, std::max, std::numeric_limits<unsigned long long>::min())
#undef DECLARE_REDUCTION
Event copy(RegionInstance src_inst, RegionInstance dst_inst, FieldID fid,
size_t value_size, Event wait_for)
{
CopySrcDstField src_field;
src_field.inst = src_inst;
src_field.field_id = fid;
src_field.size = value_size;
std::vector<CopySrcDstField> src_fields;
src_fields.push_back(src_field);
CopySrcDstField dst_field;
dst_field.inst = dst_inst;
dst_field.field_id = fid;
dst_field.size = value_size;
std::vector<CopySrcDstField> dst_fields;
dst_fields.push_back(dst_field);
return dst_inst.get_indexspace<1, coord_t>().copy(
src_fields, dst_fields, ProfilingRequestSet(), wait_for);
}
Event fill(RegionInstance dst_inst, FieldID fid,
const void *value, size_t value_size,
Event wait_for)
{
CopySrcDstField dst_field;
dst_field.inst = dst_inst;
dst_field.field_id = fid;
dst_field.size = value_size;
std::vector<CopySrcDstField> dst_fields;
dst_fields.push_back(dst_field);
return dst_inst.get_indexspace<1, coord_t>().fill(
dst_fields, ProfilingRequestSet(), value, value_size, wait_for);
}
char *get_base(RegionInstance inst, FieldID fid)
{
AffineAccessor<char, 1, coord_t> acc =
AffineAccessor<char, 1, coord_t>(inst, fid);
return reinterpret_cast<char *>(
acc.ptr(inst.get_indexspace<1, coord_t>().bounds.lo));
}
void leaf_task(const void *args, size_t arglen, const void *userdata,
size_t userlen, Processor p)
{
LeafArgs a;
std::vector<uintptr_t> input_ptr;
std::vector<size_t> input_bytes;
{
FixedBufferDeserializer ser(args, arglen);
ser >> a;
ser >> input_ptr;
ser >> input_bytes;
assert(ser.bytes_left() == 0);
}
a.graph.execute_point(a.timestep, a.point,
a.output_ptr, a.output_bytes,
(const char **)input_ptr.data(), (size_t *)input_bytes.data(), a.n_inputs,
a.scratch_ptr, a.scratch_bytes);
}
void shard_task(const void *args, size_t arglen, const void *userdata,
size_t userlen, Processor p)
{
ShardArgs a;
std::vector<TaskGraph> graphs;
std::vector<std::vector<RegionInstance> > task_results;
std::vector<std::vector<RegionInstance> > task_inputs;
std::vector<std::vector<RegionInstance> > raw_exchange;
std::vector<std::vector<RegionInstance> > war_exchange;
{
FixedBufferDeserializer ser(args, arglen);
ser >> a;
ser >> graphs;
ser >> task_results;
ser >> task_inputs;
ser >> raw_exchange;
ser >> war_exchange;
assert(ser.bytes_left() == 0);
}
long proc_index = a.proc_index;
long num_procs = a.num_procs;
long num_fields = a.num_fields;
long force_copies = a.force_copies;
Memory sysmem = a.sysmem;
Memory regmem = a.regmem;
Barrier sync = a.sync;
Barrier first_start = a.first_start;
Barrier last_start = a.last_start;
Barrier first_stop = a.first_stop;
Barrier last_stop = a.last_stop;
// Figure out who we're going to be communicating with.
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
for (long point = first_point; point <= last_point; ++point) {
task_results.at(graph_index).at(point).fetch_metadata(p).wait();
task_inputs.at(graph_index).at(point).fetch_metadata(p).wait();
raw_exchange.at(graph_index).at(point).fetch_metadata(p).wait();
war_exchange.at(graph_index).at(point).fetch_metadata(p).wait();
}
}
// graph -> point -> [remote point]
std::vector<std::vector<std::set<long> > > raw_exchange_points(graphs.size());
std::vector<std::vector<std::set<long> > > war_exchange_points(graphs.size());
// graph -> [remote point]
std::vector<std::set<long> > raw_all_points(graphs.size());
std::vector<std::set<long> > war_all_points(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
raw_exchange_points.at(graph_index).resize(last_point - first_point + 1);
war_exchange_points.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
long max_dset = graph.max_dependence_sets();
for (long dset = 0; dset < max_dset; ++dset) {
for (auto interval : graph.dependencies(dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
raw_exchange_points.at(graph_index).at(point - first_point).insert(dep);
raw_all_points.at(graph_index).insert(dep);
}
}
for (auto interval : graph.reverse_dependencies(dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
war_exchange_points.at(graph_index).at(point - first_point).insert(dep);
war_all_points.at(graph_index).insert(dep);
}
}
}
}
}
// graph -> point -> dset -> [remote point]
std::vector<std::vector<std::vector<std::vector<long> > > > raw_points_not_in_dset(graphs.size());
std::vector<std::vector<std::vector<std::vector<long> > > > war_points_not_in_dset(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
raw_points_not_in_dset.at(graph_index).resize(last_point - first_point + 1);
war_points_not_in_dset.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
long max_dset = graph.max_dependence_sets();
raw_points_not_in_dset.at(graph_index).at(point - first_point).resize(max_dset);
war_points_not_in_dset.at(graph_index).at(point - first_point).resize(max_dset);
for (long dset = 0; dset < max_dset; ++dset) {
std::set<long> raw_points = raw_exchange_points.at(graph_index).at(point - first_point);
std::set<long> war_points = war_exchange_points.at(graph_index).at(point - first_point);
for (auto interval : graph.dependencies(dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
raw_points.erase(dep);
}
}
for (auto interval : graph.reverse_dependencies(dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
war_points.erase(dep);
}
}
raw_points_not_in_dset.at(graph_index).at(point - first_point).at(dset).assign(raw_points.begin(), raw_points.end());
war_points_not_in_dset.at(graph_index).at(point - first_point).at(dset).assign(war_points.begin(), war_points.end());
}
}
}
// Create local input regions.
// graph -> point -> max deps
std::vector<std::vector<long> > max_deps(graphs.size());
long all_max_deps = 0;
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
max_deps.at(graph_index).resize(graph.max_width, 0);
for (long point = 0; point < graph.max_width; ++point) {
long max_dset = graph.max_dependence_sets();
for (long dset = 0; dset < max_dset; ++dset) {
long deps = 0;
for (auto interval : graph.dependencies(dset, point)) {
deps += interval.second - interval.first + 1;
}
max_deps.at(graph_index).at(point) = std::max(max_deps.at(graph_index).at(point), deps);
all_max_deps = std::max(all_max_deps, deps);
}
}
}
// graph -> point -> dep -> instance
std::vector<std::vector<std::vector<RegionInstance> > > inputs(graphs.size());
// graph -> point -> dep -> field -> base pointer
std::vector<std::vector<std::vector<std::vector<char *> > > > input_base(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
Rect1 bounds(Point1(0), Point1(graph.output_bytes_per_task - 1));
std::map<FieldID, size_t> field_sizes;
for (unsigned fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
field_sizes[fid] = sizeof(char);
}
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
inputs.at(graph_index).resize(last_point - first_point + 1);
input_base.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
long deps = max_deps.at(graph_index).at(point);
inputs.at(graph_index).at(point - first_point).resize(deps);
input_base.at(graph_index).at(point - first_point).resize(deps);
AffineAccessor<RegionInstance, 1, coord_t> task_input =
AffineAccessor<RegionInstance, 1, coord_t>(task_inputs.at(graph_index).at(point), FID_FIRST);
for (long dep = 0; dep < deps; ++dep) {
RegionInstance &inst = inputs.at(graph_index).at(point - first_point).at(dep);
RegionInstance::create_instance(inst, regmem, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet())
.wait();
input_base.at(graph_index).at(point - first_point).at(dep).resize(num_fields);
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
input_base.at(graph_index).at(point - first_point).at(dep).at(fid - FID_FIRST) =
get_base(inst, fid);
}
task_input[dep] = inst;
}
for (long dep = deps; dep < all_max_deps; ++dep) {
task_input[dep] = RegionInstance::NO_INST;
}
}
}
// Create barriers.
// graph -> point -> field -> remote point -> barrier
std::vector<std::vector<std::vector<std::map<long, Barrier> > > > raw_in(graphs.size());
std::vector<std::vector<std::vector<std::map<long, Barrier> > > > war_in(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
raw_in.at(graph_index).resize(last_point - first_point + 1);
war_in.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
auto &raw_points = raw_exchange_points.at(graph_index).at(point - first_point);
auto &war_points = war_exchange_points.at(graph_index).at(point - first_point);
raw_in.at(graph_index).at(point - first_point).resize(num_fields);
war_in.at(graph_index).at(point - first_point).resize(num_fields);
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
const Barrier no_barrier = Barrier::NO_BARRIER;
fill(raw_exchange.at(graph_index).at(point), fid,
&no_barrier, sizeof(no_barrier), Event::NO_EVENT)
.wait();
fill(war_exchange.at(graph_index).at(point), fid,
&no_barrier, sizeof(no_barrier), Event::NO_EVENT)
.wait();
AffineAccessor<Barrier, 1, coord_t> raw =
AffineAccessor<Barrier, 1, coord_t>(raw_exchange.at(graph_index).at(point), fid);
AffineAccessor<Barrier, 1, coord_t> war =
AffineAccessor<Barrier, 1, coord_t>(war_exchange.at(graph_index).at(point), fid);
for (auto dep : raw_points) {
Barrier bar = Barrier::create_barrier(1);
raw_in.at(graph_index).at(point - first_point).at(fid - FID_FIRST)[dep] = bar;
raw[dep] = bar;
}
for (auto dep : war_points) {
Barrier bar = Barrier::create_barrier(1);
war_in.at(graph_index).at(point - first_point).at(fid - FID_FIRST)[dep] = bar;
war[dep] = bar;
}
}
}
}
// Perform input region/barrier exchange.
sync.arrive(1);
sync.wait();
sync = sync.advance_barrier();
// graph -> remote point -> instance
std::vector<std::map<long, RegionInstance> > raw_local_out(graphs.size());
std::vector<std::map<long, RegionInstance> > war_local_out(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
Rect1 bounds(Point1(0), Point1(graph.max_width - 1));
std::map<FieldID, size_t> field_sizes;
for (unsigned fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
field_sizes[fid] = sizeof(Barrier);
}
for (auto &dep : war_all_points.at(graph_index)) {
RegionInstance inst;
RegionInstance::create_instance(inst, sysmem, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet())
.wait();
raw_local_out.at(graph_index)[dep] = inst;
}
for (auto &dep : raw_all_points.at(graph_index)) {
RegionInstance inst;
RegionInstance::create_instance(inst, sysmem, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet())
.wait();
war_local_out.at(graph_index)[dep] = inst;
}
}
std::vector<Event> events;
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
for (auto &dep : war_all_points.at(graph_index)) {
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
events.push_back(
copy(raw_exchange.at(graph_index).at(dep),
raw_local_out.at(graph_index).at(dep),
fid, sizeof(Barrier), Event::NO_EVENT));
}
}
for (auto &dep : raw_all_points.at(graph_index)) {
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
events.push_back(
copy(war_exchange.at(graph_index).at(dep),
war_local_out.at(graph_index).at(dep),
fid, sizeof(Barrier), Event::NO_EVENT));
}
}
}
Event::merge_events(events).wait();
events.clear();
// graph -> point -> field -> remote point -> barrier
std::vector<std::vector<std::vector<std::map<long, Barrier> > > > raw_out(graphs.size());
std::vector<std::vector<std::vector<std::map<long, Barrier> > > > war_out(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
raw_out.at(graph_index).resize(last_point - first_point + 1);
war_out.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
auto &raw_points = raw_exchange_points.at(graph_index).at(point - first_point);
auto &war_points = war_exchange_points.at(graph_index).at(point - first_point);
raw_out.at(graph_index).at(point - first_point).resize(num_fields);
war_out.at(graph_index).at(point - first_point).resize(num_fields);
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
for (auto dep : war_points) {
AffineAccessor<Barrier, 1, coord_t> raw =
AffineAccessor<Barrier, 1, coord_t>(raw_local_out.at(graph_index).at(dep), fid);
Barrier bar = raw[point];
assert(bar != Barrier::NO_BARRIER);
raw_out.at(graph_index).at(point - first_point).at(fid - FID_FIRST)[dep] = bar;
}
for (auto dep : raw_points) {
AffineAccessor<Barrier, 1, coord_t> war =
AffineAccessor<Barrier, 1, coord_t>(war_local_out.at(graph_index).at(dep), fid);
Barrier bar = war[point];
assert(bar != Barrier::NO_BARRIER);
war_out.at(graph_index).at(point - first_point).at(fid - FID_FIRST)[dep] = bar;
}
}
}
}
// graph -> remote point -> instance
std::vector<std::map<long, RegionInstance> > remote_task_inputs(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
Rect1 bounds(Point1(0), Point1(all_max_deps - 1));
std::map<FieldID, size_t> field_sizes;
field_sizes[FID_FIRST] = sizeof(RegionInstance);
for (auto &dep : war_all_points.at(graph_index)) {
RegionInstance inst;
RegionInstance::create_instance(inst, sysmem, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet())
.wait();
remote_task_inputs.at(graph_index)[dep] = inst;
}
}
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
for (auto &dep : war_all_points.at(graph_index)) {
events.push_back(
copy(task_inputs.at(graph_index).at(dep),
remote_task_inputs.at(graph_index).at(dep),
FID_FIRST, sizeof(RegionInstance), Event::NO_EVENT));
}
}
Event::merge_events(events).wait();
events.clear();
// graph -> point -> remote point -> dep -> instance
std::vector<std::vector<std::map<long, std::vector<RegionInstance> > > > remote_inputs(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
remote_inputs.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
auto &war_points = war_exchange_points.at(graph_index).at(point - first_point);
for (auto dep : war_points) {
remote_inputs.at(graph_index).at(point - first_point)[dep].resize(all_max_deps);
AffineAccessor<RegionInstance, 1, coord_t> remote_task_input =
AffineAccessor<RegionInstance, 1, coord_t>(remote_task_inputs.at(graph_index).at(dep), FID_FIRST);
for (long remote_dep = 0; remote_dep < all_max_deps; ++remote_dep) {
RegionInstance inst = remote_task_input[remote_dep];
remote_inputs.at(graph_index).at(point - first_point)[dep].at(remote_dep) = inst;
}
}
}
}
// graph -> point -> dset -> remote point -> slot
std::vector<std::vector<std::vector<std::map<long, long> > > > remote_input_slot(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
remote_input_slot.at(graph_index).resize(last_point - first_point + 1);
for (long point = first_point; point <= last_point; ++point) {
long max_dset = graph.max_dependence_sets();
remote_input_slot.at(graph_index).at(point - first_point).resize(max_dset);
for (long dset = 0; dset < max_dset; ++dset) {
for (auto interval : graph.reverse_dependencies(dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
long slot = 0;
bool found = false;
for (auto interval : graph.dependencies(dset, dep)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
if (point == dep) {
found = true;
break;
}
slot++;
}
if (found) break;
}
assert(found);
remote_input_slot.at(graph_index).at(point - first_point).at(dset)[dep] = slot;
}
}
}
}
}
// graph -> point -> field -> base pointer
std::vector<std::vector<std::vector<char *> > > result_base(graphs.size());
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
result_base.at(graph_index).resize(graph.max_width);
for (long point = 0; point < graph.max_width; ++point) {
result_base.at(graph_index).at(point).resize(num_fields, NULL);
auto &inst = task_results.at(graph_index).at(point);
if (inst.get_location() == sysmem || inst.get_location() == regmem) {
for (long fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
result_base.at(graph_index).at(point).at(fid - FID_FIRST) =
get_base(inst, fid);
}
}
}
}
std::vector<uintptr_t> input_ptr(all_max_deps, 0);
std::vector<size_t> input_bytes(all_max_deps, 0);
size_t max_scratch_bytes = 0;
for (auto graph : graphs) {
max_scratch_bytes = std::max(max_scratch_bytes, graph.scratch_bytes_per_task);
}
// It's ok to use only a single scratch buffer because the tasks
// will be effectively serialized on this processor.
char *scratch_ptr = reinterpret_cast<char *>(malloc(max_scratch_bytes));
assert(scratch_ptr);
TaskGraph::prepare_scratch(scratch_ptr, max_scratch_bytes);
// Statically allocate buffer to use for task input
size_t leaf_bufsize = 0;
{
LeafArgs leaf_args;
ByteCountSerializer ser;
ser << leaf_args;
ser << input_ptr;
ser << input_bytes;
leaf_bufsize = ser.bytes_used();
}
void *leaf_buffer = malloc(leaf_bufsize);
assert(leaf_buffer);
// Sync again to avoid staggered start
sync.arrive(1);
sync.wait();
sync = sync.advance_barrier();
// Main loop
unsigned long long start_time = 0, stop_time = 0;
std::vector<Event> preconditions;
std::vector<std::vector<std::vector<Event> > > copy_postconditions;
for (long rep = 0; rep < 1; ++rep) {
start_time = Clock::current_time_in_nanoseconds();
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
std::fill(input_bytes.begin(), input_bytes.end(), graph.output_bytes_per_task);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
// Don't leak copy postconditions between graphs.
copy_postconditions.resize(last_point - first_point + 1);
for (auto &point_postconditions : copy_postconditions) {
point_postconditions.resize(num_fields);
for (auto &postconditions : point_postconditions) {
postconditions.clear();
}
}
for (long timestep = 0; timestep < graph.timesteps; ++timestep) {
long dset = graph.dependence_set_at_timestep(timestep);
long next_dset = graph.dependence_set_at_timestep(timestep + 1);
long last_field_dset = graph.dependence_set_at_timestep(std::max(timestep - num_fields + 1, 0L));
long offset = graph.offset_at_timestep(timestep);
long width = graph.width_at_timestep(timestep);
long last_offset = graph.offset_at_timestep(timestep-1);
long last_width = graph.width_at_timestep(timestep-1);
long last_field_offset = graph.offset_at_timestep(timestep - num_fields + 1);
long last_field_width = graph.width_at_timestep(timestep - num_fields + 1);
long next_offset = timestep + 1 < graph.timesteps ? graph.offset_at_timestep(timestep+1) : 0;
long next_width = timestep + 1 < graph.timesteps ? graph.width_at_timestep(timestep+1) : 0;
long fid = FID_FIRST + timestep % num_fields;
long last_fid = FID_FIRST + (timestep + num_fields - 1) % num_fields;
for (long point = first_point; point <= last_point; ++point) {
// Gather inputs
long n_inputs = 0, slot = 0;
preconditions.clear();
preconditions.insert(preconditions.begin(),
copy_postconditions.at(point - first_point).at(fid - FID_FIRST).begin(),
copy_postconditions.at(point - first_point).at(fid - FID_FIRST).end());
const auto &dset_deps = graph.dependencies(dset, point);
for (auto interval : dset_deps) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
Barrier &ready = raw_in.at(graph_index).at(point - first_point).at(last_fid - FID_FIRST).at(dep);
preconditions.push_back(ready.get_previous_phase());
if (dep >= last_offset && dep < last_offset + last_width) {
char *data = result_base.at(graph_index).at(dep).at(last_fid - FID_FIRST);
if (point >= offset && point < offset + width) {
if (data && !force_copies) {
// Data available locally
} else {
// Data is remote
data = input_base.at(graph_index).at(point - first_point).at(slot).at(last_fid - FID_FIRST);
}
}
input_ptr.at(n_inputs) = reinterpret_cast<uintptr_t>(data);
n_inputs++;
}
slot++;
}
}
// Dependencies can occur in one of two ways:
// 1. The dependent task is local, so copy is not necessary.
// (In this case the dependency catches on the task itself.)
// 2. The dependent task is non-local, so copy is necessary.
// (In this case the dependency catches on the copy.)
// WAR dependencies (part 1)
for (auto interval : graph.reverse_dependencies(last_field_dset, point)) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
if (dep >= last_field_offset && dep < last_field_offset + last_field_width) {
// Only copy when the dependent task doesn't live in the same address space.
if (!force_copies && result_base.at(graph_index).at(dep).at(last_fid - FID_FIRST)) {
Barrier &ready = war_in.at(graph_index).at(point - first_point).at(fid - FID_FIRST).at(dep);
preconditions.push_back(ready.get_previous_phase());
}
}
}
}
// WAR dependencies (part 2)
const auto &next_dset_rev_deps = graph.reverse_dependencies(next_dset, point);
for (auto interval : next_dset_rev_deps) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
if (force_copies || !result_base.at(graph_index).at(dep).at(last_fid - FID_FIRST)) {
Barrier &ready = war_in.at(graph_index).at(point - first_point).at(fid - FID_FIRST).at(dep);
preconditions.push_back(ready.get_previous_phase());
}
}
}
// Launch task
Event task_postcondition = Event::NO_EVENT;
if (point >= offset && point < offset + width) {
LeafArgs leaf_args;
leaf_args.point = point;
leaf_args.timestep = timestep;
leaf_args.graph = graph;
leaf_args.output_ptr = result_base.at(graph_index).at(point).at(fid - FID_FIRST);
leaf_args.output_bytes = graph.output_bytes_per_task;
leaf_args.scratch_ptr = scratch_ptr;
leaf_args.scratch_bytes = graph.scratch_bytes_per_task;
leaf_args.n_inputs = n_inputs;
FixedBufferSerializer ser(leaf_buffer, leaf_bufsize);
ser << leaf_args;
ser << input_ptr;
ser << input_bytes;
assert(ser.bytes_left() == 0);
task_postcondition =
p.spawn(LEAF_TASK, leaf_buffer, leaf_bufsize,
Event::merge_events(preconditions));
// FIXME: Figure out which tasks we actually need to wait on
events.push_back(task_postcondition);
}
copy_postconditions.at(point - first_point).at(fid - FID_FIRST).clear();
// RAW dependencies
for (auto interval : next_dset_rev_deps) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
Barrier &complete = raw_out.at(graph_index).at(point - first_point).at(fid - FID_FIRST).at(dep);
Event postcondition = task_postcondition;
if (dep >= next_offset && dep < next_offset + next_width) {
// Only copy when the dependent task doesn't live in the same address space.
if (force_copies || !result_base.at(graph_index).at(dep).at(fid - FID_FIRST)) {
long slot = remote_input_slot.at(graph_index).at(point - first_point).at(next_dset).at(dep);
postcondition = copy(
task_results.at(graph_index).at(point),
remote_inputs
.at(graph_index)
.at(point - first_point)
.at(dep)
.at(slot),
fid, graph.output_bytes_per_task,
task_postcondition);
copy_postconditions.at(point - first_point).at(fid - FID_FIRST).push_back(postcondition);
}
}
complete.arrive(1, postcondition);
}
}
// Also need to arrive at any points not included in this
// dset, otherwise we'll deadlock.
for (long dep : war_points_not_in_dset.at(graph_index).at(point - first_point).at(next_dset)) {
raw_out.at(graph_index).at(point - first_point).at(fid - FID_FIRST).at(dep).arrive(1);
}
// WAR dependencies
for (auto interval : dset_deps) {
for (long dep = interval.first; dep <= interval.second; ++dep) {
Barrier &complete = war_out.at(graph_index).at(point - first_point).at(last_fid - FID_FIRST).at(dep);
complete.arrive(1, task_postcondition);
}
}
// Also need to arrive at any points not included in this
// dset, otherwise we'll deadlock.
for (long dep : raw_points_not_in_dset.at(graph_index).at(point - first_point).at(dset)) {
war_out.at(graph_index).at(point - first_point).at(last_fid - FID_FIRST).at(dep).arrive(1, task_postcondition);
}
for (auto &bar : raw_in.at(graph_index).at(point - first_point).at(fid - FID_FIRST)) {
bar.second = bar.second.advance_barrier();
}
for (auto &bar : raw_out.at(graph_index).at(point - first_point).at(fid - FID_FIRST)) {
bar.second = bar.second.advance_barrier();
}
for (auto &bar : war_in.at(graph_index).at(point - first_point).at(last_fid - FID_FIRST)) {
bar.second = bar.second.advance_barrier();
}
for (auto &bar : war_out.at(graph_index).at(point - first_point).at(last_fid - FID_FIRST)) {
bar.second = bar.second.advance_barrier();
}
}
}
}
Event::merge_events(events).wait();
events.clear();
stop_time = Clock::current_time_in_nanoseconds();
}
first_start.arrive(1, Event::NO_EVENT, &start_time, sizeof(start_time));
last_start.arrive(1, Event::NO_EVENT, &start_time, sizeof(start_time));
first_stop.arrive(1, Event::NO_EVENT, &stop_time, sizeof(stop_time));
last_stop.arrive(1, Event::NO_EVENT, &stop_time, sizeof(stop_time));
free(leaf_buffer);
free(scratch_ptr);
}
void top_level_task(const void *args, size_t arglen, const void *userdata,
size_t userlen, Processor p)
{
assert(global_argv != NULL);
App app(global_argc, global_argv);
auto &graphs = app.graphs;
long num_fields = 5;
bool force_copies = false;
for (int i = 1; i < global_argc; i++) {
if (!strcmp(global_argv[i], "-field")) {
long value = atol(global_argv[++i]);
if (value <= 0) {
fprintf(stderr, "error: Invalid flag \"-field %ld\" must be > 1\n", value);
abort();
}
num_fields = value;
}
if (!strcmp(global_argv[i], "-force-copies")) {
force_copies = true;
}
}
app.display();
Machine machine = Machine::get_machine();
std::vector<Processor> procs;
{
Machine::ProcessorQuery query(machine);
query.only_kind(Processor::LOC_PROC);
procs.insert(procs.end(), query.begin(), query.end());
}
long num_procs = procs.size();
std::map<Processor, Memory> proc_sysmems;
std::map<Processor, Memory> proc_regmems;
{
std::vector<Machine::ProcessorMemoryAffinity> proc_mem_affinities;
machine.get_proc_mem_affinity(proc_mem_affinities);
for (size_t i = 0; i < proc_mem_affinities.size(); ++i) {
Machine::ProcessorMemoryAffinity &affinity = proc_mem_affinities[i];
if (affinity.p.kind() == Processor::LOC_PROC) {
if (affinity.m.kind() == Memory::SYSTEM_MEM) {
proc_sysmems[affinity.p] = affinity.m;
if (proc_regmems.find(affinity.p) == proc_regmems.end())
proc_regmems[affinity.p] = affinity.m;
} else if (affinity.m.kind() == Memory::REGDMA_MEM)
proc_regmems[affinity.p] = affinity.m;
}
}
}
long all_max_deps = 0;
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
for (long point = 0; point <= graph.max_width; ++point) {
long max_dset = graph.max_dependence_sets();
for (long dset = 0; dset < max_dset; ++dset) {
long deps = 0;
for (auto interval : graph.dependencies(dset, point)) {
deps += interval.second - interval.first + 1;
}
all_max_deps = std::max(all_max_deps, deps);
}
}
}
std::vector<Event> events;
std::vector<std::vector<RegionInstance> > task_results(graphs.size());
{
std::map<FieldID, size_t> field_sizes;
for (unsigned fid = FID_FIRST; fid < FID_FIRST + num_fields; ++fid) {
field_sizes[fid] = sizeof(char);
}
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
task_results.at(graph_index).resize(graph.max_width);
Rect1 bounds(Point1(0), Point1(graph.output_bytes_per_task - 1));
for (long proc_index = 0; proc_index < num_procs; ++proc_index) {
auto proc = procs.at(proc_index);
auto memory = proc_regmems.at(proc);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
for (long point = first_point; point <= last_point; ++point) {
events.push_back(
RegionInstance::create_instance(task_results.at(graph_index).at(point), memory, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet()));
}
}
}
}
std::vector<std::vector<RegionInstance> > task_inputs(graphs.size());
{
std::map<FieldID, size_t> field_sizes;
field_sizes[FID_FIRST] = sizeof(RegionInstance);
for (size_t graph_index = 0; graph_index < graphs.size(); ++graph_index) {
auto graph = graphs.at(graph_index);
task_inputs.at(graph_index).resize(graph.max_width);
Rect1 bounds(Point1(0), Point1(all_max_deps - 1));
for (long proc_index = 0; proc_index < num_procs; ++proc_index) {
auto proc = procs.at(proc_index);
auto memory = proc_regmems.at(proc);
long first_point = proc_index * graph.max_width / num_procs;
long last_point = (proc_index + 1) * graph.max_width / num_procs - 1;
for (long point = first_point; point <= last_point; ++point) {
events.push_back(
RegionInstance::create_instance(task_inputs.at(graph_index).at(point), memory, bounds, field_sizes,
0 /*SOA*/, ProfilingRequestSet()));
}
}
}