/
legion_instances.cc
4221 lines (4044 loc) · 166 KB
/
legion_instances.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 2024 Stanford University, NVIDIA Corporation
*
* 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 "legion.h"
#include "legion/runtime.h"
#include "legion/legion_ops.h"
#include "legion/legion_tasks.h"
#include "legion/region_tree.h"
#include "legion/legion_spy.h"
#include "legion/legion_context.h"
#include "legion/legion_profiling.h"
#include "legion/legion_instances.h"
#include "legion/legion_views.h"
#include "legion/legion_replication.h"
namespace Legion {
namespace Internal {
LEGION_EXTERN_LOGGER_DECLARATIONS
// This is the shit right here: super-cool helper function
//--------------------------------------------------------------------------
template<unsigned LOG2MAX>
static inline void compress_mask(FieldMask &x, FieldMask m)
//--------------------------------------------------------------------------
{
FieldMask mk, mp, mv, t;
// See hacker's delight 7-4
x = x & m;
mk = ~m << 1;
for (unsigned i = 0; i < LOG2MAX; i++)
{
mp = mk ^ (mk << 1);
for (unsigned idx = 1; idx < LOG2MAX; idx++)
mp = mp ^ (mp << (1 << idx));
mv = mp & m;
m = (m ^ mv) | (mv >> (1 << i));
t = x & mv;
x = (x ^ t) | (t >> (1 << i));
mk = mk & ~mp;
}
}
/////////////////////////////////////////////////////////////
// Copy Across Helper
/////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------
void CopyAcrossHelper::compute_across_offsets(const FieldMask &src_mask,
std::vector<CopySrcDstField> &dst_fields)
//--------------------------------------------------------------------------
{
FieldMask compressed;
bool found_in_cache = false;
for (LegionDeque<std::pair<FieldMask,FieldMask> >::const_iterator
it = compressed_cache.begin(); it != compressed_cache.end(); it++)
{
if (it->first == src_mask)
{
compressed = it->second;
found_in_cache = true;
break;
}
}
if (!found_in_cache)
{
compressed = src_mask;
compress_mask<STATIC_LOG2(LEGION_MAX_FIELDS)>(compressed, full_mask);
compressed_cache.push_back(
std::pair<FieldMask,FieldMask>(src_mask, compressed));
}
const unsigned pop_count = FieldMask::pop_count(compressed);
#ifdef DEBUG_LEGION
assert(pop_count == FieldMask::pop_count(src_mask));
#endif
unsigned offset = dst_fields.size();
dst_fields.resize(offset + pop_count);
int next_start = 0;
for (unsigned idx = 0; idx < pop_count; idx++)
{
int index = compressed.find_next_set(next_start);
CopySrcDstField &field = dst_fields[offset+idx];
field = offsets[index];
// We'll start looking again at the next index after this one
next_start = index + 1;
}
}
//--------------------------------------------------------------------------
FieldMask CopyAcrossHelper::convert_src_to_dst(const FieldMask &src_mask)
//--------------------------------------------------------------------------
{
FieldMask dst_mask;
if (!src_mask)
return dst_mask;
if (forward_map.empty())
{
#ifdef DEBUG_LEGION
assert(src_indexes.size() == dst_indexes.size());
#endif
for (unsigned idx = 0; idx < src_indexes.size(); idx++)
{
#ifdef DEBUG_LEGION
assert(forward_map.find(src_indexes[idx]) == forward_map.end());
#endif
forward_map[src_indexes[idx]] = dst_indexes[idx];
}
}
int index = src_mask.find_first_set();
while (index >= 0)
{
#ifdef DEBUG_LEGION
assert(forward_map.find(index) != forward_map.end());
#endif
dst_mask.set_bit(forward_map[index]);
index = src_mask.find_next_set(index+1);
}
return dst_mask;
}
//--------------------------------------------------------------------------
FieldMask CopyAcrossHelper::convert_dst_to_src(const FieldMask &dst_mask)
//--------------------------------------------------------------------------
{
FieldMask src_mask;
if (!dst_mask)
return src_mask;
if (backward_map.empty())
{
#ifdef DEBUG_LEGION
assert(src_indexes.size() == dst_indexes.size());
#endif
for (unsigned idx = 0; idx < dst_indexes.size(); idx++)
{
#ifdef DEBUG_LEGION
assert(backward_map.find(dst_indexes[idx]) == backward_map.end());
#endif
backward_map[dst_indexes[idx]] = src_indexes[idx];
}
}
int index = dst_mask.find_first_set();
while (index >= 0)
{
#ifdef DEBUG_LEGION
assert(backward_map.find(index) != backward_map.end());
#endif
src_mask.set_bit(backward_map[index]);
index = dst_mask.find_next_set(index+1);
}
return src_mask;
}
//--------------------------------------------------------------------------
unsigned CopyAcrossHelper::convert_src_to_dst(unsigned index)
//--------------------------------------------------------------------------
{
if (forward_map.empty())
{
#ifdef DEBUG_LEGION
assert(src_indexes.size() == dst_indexes.size());
#endif
for (unsigned idx = 0; idx < src_indexes.size(); idx++)
{
#ifdef DEBUG_LEGION
assert(forward_map.find(src_indexes[idx]) == forward_map.end());
#endif
forward_map[src_indexes[idx]] = dst_indexes[idx];
}
}
#ifdef DEBUG_LEGION
assert(forward_map.find(index) != forward_map.end());
#endif
return forward_map[index];
}
//--------------------------------------------------------------------------
unsigned CopyAcrossHelper::convert_dst_to_src(unsigned index)
//--------------------------------------------------------------------------
{
if (backward_map.empty())
{
#ifdef DEBUG_LEGION
assert(src_indexes.size() == dst_indexes.size());
#endif
for (unsigned idx = 0; idx < dst_indexes.size(); idx++)
{
#ifdef DEBUG_LEGION
assert(backward_map.find(dst_indexes[idx]) == backward_map.end());
#endif
backward_map[dst_indexes[idx]] = src_indexes[idx];
}
}
#ifdef DEBUG_LEGION
assert(backward_map.find(index) != backward_map.end());
#endif
return backward_map[index];
}
/////////////////////////////////////////////////////////////
// Layout Description
/////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------
LayoutDescription::LayoutDescription(FieldSpaceNode *own,
const FieldMask &mask,
const unsigned dims,
LayoutConstraints *con,
const std::vector<unsigned> &mask_index_map,
const std::vector<FieldID> &field_ids,
const std::vector<size_t> &field_sizes,
const std::vector<CustomSerdezID> &serdez)
: allocated_fields(mask), constraints(con), owner(own), total_dims(dims)
//--------------------------------------------------------------------------
{
constraints->add_base_gc_ref(LAYOUT_DESC_REF);
field_infos.resize(field_sizes.size());
// Switch data structures from layout by field order to order
// of field locations in the bit mask
#ifdef DEBUG_LEGION
// Greater than or equal because local fields can alias onto the
// same index for the allocated instances, note that the fields
// themselves still get allocated their own space in the instance
assert(mask_index_map.size() >=
size_t(FieldMask::pop_count(allocated_fields)));
#endif
for (unsigned idx = 0; idx < mask_index_map.size(); idx++)
{
// This gives us the index in the field ordered data structures
unsigned index = mask_index_map[idx];
FieldID fid = field_ids[index];
field_indexes[fid] = idx;
CopySrcDstField &info = field_infos[idx];
info.size = field_sizes[index];
info.field_id = fid;
info.serdez_id = serdez[index];
}
}
//--------------------------------------------------------------------------
LayoutDescription::LayoutDescription(const FieldMask &mask,
LayoutConstraints *con)
: allocated_fields(mask), constraints(con), owner(NULL), total_dims(0)
//--------------------------------------------------------------------------
{
constraints->add_base_gc_ref(LAYOUT_DESC_REF);
}
//--------------------------------------------------------------------------
LayoutDescription::LayoutDescription(const LayoutDescription &rhs)
: allocated_fields(rhs.allocated_fields), constraints(rhs.constraints),
owner(rhs.owner), total_dims(rhs.total_dims)
//--------------------------------------------------------------------------
{
// should never be called
assert(false);
}
//--------------------------------------------------------------------------
LayoutDescription::~LayoutDescription(void)
//--------------------------------------------------------------------------
{
comp_cache.clear();
if (constraints->remove_base_gc_ref(LAYOUT_DESC_REF))
delete (constraints);
}
//--------------------------------------------------------------------------
LayoutDescription& LayoutDescription::operator=(
const LayoutDescription &rhs)
//--------------------------------------------------------------------------
{
// should never be called
assert(false);
return *this;
}
//--------------------------------------------------------------------------
void LayoutDescription::log_instance_layout(LgEvent inst_event) const
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(implicit_runtime->legion_spy_enabled);
#endif
for (std::map<FieldID,unsigned>::const_iterator it =
field_indexes.begin(); it != field_indexes.end(); it++)
LegionSpy::log_physical_instance_field(inst_event, it->first);
}
//--------------------------------------------------------------------------
void LayoutDescription::compute_copy_offsets(const FieldMask ©_mask,
const PhysicalInstance instance,
std::vector<CopySrcDstField> &fields)
//--------------------------------------------------------------------------
{
uint64_t hash_key = copy_mask.get_hash_key();
bool found_in_cache = false;
FieldMask compressed;
// First check to see if we've memoized this result
{
AutoLock o_lock(layout_lock,1,false/*exclusive*/);
std::map<LEGION_FIELD_MASK_FIELD_TYPE,
LegionList<std::pair<FieldMask,FieldMask> > >::const_iterator
finder = comp_cache.find(hash_key);
if (finder != comp_cache.end())
{
for (LegionList<std::pair<FieldMask,FieldMask> >::const_iterator it =
finder->second.begin(); it != finder->second.end(); it++)
{
if (it->first == copy_mask)
{
found_in_cache = true;
compressed = it->second;
break;
}
}
}
}
if (!found_in_cache)
{
compressed = copy_mask;
compress_mask<STATIC_LOG2(LEGION_MAX_FIELDS)>(compressed,
allocated_fields);
// Save the result in the cache, duplicates from races here are benign
AutoLock o_lock(layout_lock);
comp_cache[hash_key].push_back(
std::pair<FieldMask,FieldMask>(copy_mask,compressed));
}
// It is absolutely imperative that these infos be added in
// the order in which they appear in the field mask so that
// they line up in the same order with the source/destination infos
// (depending on the calling context of this function
const unsigned pop_count = FieldMask::pop_count(compressed);
#ifdef DEBUG_LEGION
assert(pop_count == FieldMask::pop_count(copy_mask));
#endif
unsigned offset = fields.size();
fields.resize(offset + pop_count);
int next_start = 0;
for (unsigned idx = 0; idx < pop_count; idx++)
{
int index = compressed.find_next_set(next_start);
CopySrcDstField &field = fields[offset+idx];
field = field_infos[index];
// Our field infos are annonymous so specify the instance now
field.inst = instance;
// We'll start looking again at the next index after this one
next_start = index + 1;
}
}
//--------------------------------------------------------------------------
void LayoutDescription::compute_copy_offsets(
const std::vector<FieldID> ©_fields,
const PhysicalInstance instance,
std::vector<CopySrcDstField> &fields)
//--------------------------------------------------------------------------
{
unsigned offset = fields.size();
fields.resize(offset + copy_fields.size());
for (unsigned idx = 0; idx < copy_fields.size(); idx++)
{
std::map<FieldID,unsigned>::const_iterator
finder = field_indexes.find(copy_fields[idx]);
#ifdef DEBUG_LEGION
assert(finder != field_indexes.end());
#endif
CopySrcDstField &info = fields[offset+idx];
info = field_infos[finder->second];
// Since instances are annonymous in layout descriptions we
// have to fill them in when we add the field info
info.inst = instance;
}
}
//--------------------------------------------------------------------------
void LayoutDescription::get_fields(std::set<FieldID> &fields) const
//--------------------------------------------------------------------------
{
for (std::map<FieldID,unsigned>::const_iterator
it = field_indexes.begin(); it != field_indexes.end(); ++it)
fields.insert(it->first);
}
//--------------------------------------------------------------------------
bool LayoutDescription::has_field(FieldID fid) const
//--------------------------------------------------------------------------
{
return (field_indexes.find(fid) != field_indexes.end());
}
//--------------------------------------------------------------------------
void LayoutDescription::has_fields(std::map<FieldID,bool> &to_test) const
//--------------------------------------------------------------------------
{
for (std::map<FieldID,bool>::iterator it = to_test.begin();
it != to_test.end(); it++)
{
if (field_indexes.find(it->first) != field_indexes.end())
it->second = true;
else
it->second = false;
}
}
//--------------------------------------------------------------------------
void LayoutDescription::remove_space_fields(std::set<FieldID> &filter) const
//--------------------------------------------------------------------------
{
std::vector<FieldID> to_remove;
for (std::set<FieldID>::const_iterator it = filter.begin();
it != filter.end(); it++)
{
if (field_indexes.find(*it) != field_indexes.end())
to_remove.push_back(*it);
}
if (!to_remove.empty())
{
for (std::vector<FieldID>::const_iterator it = to_remove.begin();
it != to_remove.end(); it++)
filter.erase(*it);
}
}
//--------------------------------------------------------------------------
const CopySrcDstField& LayoutDescription::find_field_info(FieldID fid) const
//--------------------------------------------------------------------------
{
std::map<FieldID,unsigned>::const_iterator finder =
field_indexes.find(fid);
#ifdef DEBUG_LEGION
assert(finder != field_indexes.end());
#endif
return field_infos[finder->second];
}
//--------------------------------------------------------------------------
size_t LayoutDescription::get_total_field_size(void) const
//--------------------------------------------------------------------------
{
size_t result = 0;
// Add up all the field sizes
for (std::vector<CopySrcDstField>::const_iterator it =
field_infos.begin(); it != field_infos.end(); it++)
{
result += it->size;
}
return result;
}
//--------------------------------------------------------------------------
void LayoutDescription::get_fields(std::vector<FieldID>& fields) const
//--------------------------------------------------------------------------
{
fields = constraints->field_constraint.get_field_set();
}
//--------------------------------------------------------------------------
void LayoutDescription::compute_destroyed_fields(
std::vector<PhysicalInstance::DestroyedField> &serdez_fields) const
//--------------------------------------------------------------------------
{
// See if we have any special fields which need serdez deletion
for (std::vector<CopySrcDstField>::const_iterator it =
field_infos.begin(); it != field_infos.end(); it++)
{
if (it->serdez_id > 0)
serdez_fields.push_back(PhysicalInstance::DestroyedField(it->field_id,
it->size, it->serdez_id));
}
}
//--------------------------------------------------------------------------
bool LayoutDescription::match_layout(
const LayoutConstraintSet &candidate_constraints, unsigned num_dims) const
//--------------------------------------------------------------------------
{
if (num_dims != total_dims)
return false;
// We need to check equality on the entire constraint sets
return *constraints == candidate_constraints;
}
//--------------------------------------------------------------------------
bool LayoutDescription::match_layout(const LayoutDescription *layout,
unsigned num_dims) const
//--------------------------------------------------------------------------
{
if (num_dims != total_dims)
return false;
// This is a sound test, but it doesn't guarantee that the field sets
// match since fields can be allocated and freed between instance
// creations, so while this is a necessary precondition, it is not
// sufficient that the two sets of fields are the same, to guarantee
// that we actually need to check the FieldIDs which happens next
if (layout->allocated_fields != allocated_fields)
return false;
// Check equality on the entire constraint sets
return *layout->constraints == *constraints;
}
//--------------------------------------------------------------------------
void LayoutDescription::pack_layout_description(Serializer &rez,
AddressSpaceID target)
//--------------------------------------------------------------------------
{
rez.serialize(constraints->layout_id);
}
//--------------------------------------------------------------------------
/*static*/ LayoutDescription* LayoutDescription::
handle_unpack_layout_description(LayoutConstraints *constraints,
FieldSpaceNode *field_space_node, size_t total_dims)
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(constraints != NULL);
#endif
FieldMask instance_mask;
const std::vector<FieldID> &field_set =
constraints->field_constraint.get_field_set();
std::vector<size_t> field_sizes(field_set.size());
std::vector<unsigned> mask_index_map(field_set.size());
std::vector<CustomSerdezID> serdez(field_set.size());
field_space_node->compute_field_layout(field_set, field_sizes,
mask_index_map, serdez, instance_mask);
LayoutDescription *result =
field_space_node->create_layout_description(instance_mask, total_dims,
constraints, mask_index_map, field_set, field_sizes, serdez);
#ifdef DEBUG_LEGION
assert(result != NULL);
#endif
return result;
}
/////////////////////////////////////////////////////////////
// Collective Mapping
/////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------
CollectiveMapping::CollectiveMapping(
const std::vector<AddressSpaceID> &spaces, size_t r)
: total_spaces(spaces.size()), radix(r)
//--------------------------------------------------------------------------
{
for (std::vector<AddressSpaceID>::const_iterator it =
spaces.begin(); it != spaces.end(); it++)
unique_sorted_spaces.add(*it);
}
//--------------------------------------------------------------------------
CollectiveMapping::CollectiveMapping(const ShardMapping &mapping, size_t r)
: radix(r)
//--------------------------------------------------------------------------
{
for (unsigned idx = 0; idx < mapping.size(); idx++)
unique_sorted_spaces.add(mapping[idx]);
total_spaces = unique_sorted_spaces.size();
}
//--------------------------------------------------------------------------
CollectiveMapping::CollectiveMapping(Deserializer &derez, size_t total)
: total_spaces(total)
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(total_spaces > 0);
#endif
derez.deserialize(unique_sorted_spaces);
#ifdef DEBUG_LEGION
assert(total_spaces == unique_sorted_spaces.size());
#endif
derez.deserialize(radix);
}
//--------------------------------------------------------------------------
CollectiveMapping::CollectiveMapping(const CollectiveMapping &rhs)
: Collectable(), unique_sorted_spaces(rhs.unique_sorted_spaces),
total_spaces(rhs.total_spaces), radix(rhs.radix)
//--------------------------------------------------------------------------
{
}
//--------------------------------------------------------------------------
bool CollectiveMapping::operator==(const CollectiveMapping &rhs) const
//--------------------------------------------------------------------------
{
if (radix != rhs.radix)
return false;
return unique_sorted_spaces == rhs.unique_sorted_spaces;
}
//--------------------------------------------------------------------------
bool CollectiveMapping::operator!=(const CollectiveMapping &rhs) const
//--------------------------------------------------------------------------
{
return !((*this) == rhs);
}
//--------------------------------------------------------------------------
AddressSpaceID CollectiveMapping::get_parent(const AddressSpaceID origin,
const AddressSpaceID local) const
//--------------------------------------------------------------------------
{
const unsigned local_index = find_index(local);
const unsigned origin_index = find_index(origin);
#ifdef DEBUG_LEGION
assert(local_index < total_spaces);
assert(origin_index < total_spaces);
#endif
const unsigned offset = convert_to_offset(local_index, origin_index);
const unsigned index = convert_to_index((offset-1) / radix, origin_index);
const int result = unique_sorted_spaces.get_index(index);
#ifdef DEBUG_LEGION
assert(result >= 0);
#endif
return result;
}
//--------------------------------------------------------------------------
size_t CollectiveMapping::count_children(const AddressSpaceID origin,
const AddressSpaceID local) const
//--------------------------------------------------------------------------
{
const unsigned local_index = find_index(local);
const unsigned origin_index = find_index(origin);
#ifdef DEBUG_LEGION
assert(local_index < total_spaces);
assert(origin_index < total_spaces);
#endif
const unsigned offset = radix *
convert_to_offset(local_index, origin_index);
size_t result = 0;
for (unsigned idx = 1; idx <= radix; idx++)
{
const unsigned child_offset = offset + idx;
if (child_offset < total_spaces)
result++;
}
return result;
}
//--------------------------------------------------------------------------
void CollectiveMapping::get_children(const AddressSpaceID origin,
const AddressSpaceID local, std::vector<AddressSpaceID> &children) const
//--------------------------------------------------------------------------
{
const unsigned local_index = find_index(local);
const unsigned origin_index = find_index(origin);
#ifdef DEBUG_LEGION
assert(local_index < total_spaces);
assert(origin_index < total_spaces);
#endif
const unsigned offset = radix *
convert_to_offset(local_index, origin_index);
for (unsigned idx = 1; idx <= radix; idx++)
{
const unsigned child_offset = offset + idx;
if (child_offset < total_spaces)
{
const unsigned index = convert_to_index(child_offset, origin_index);
const int child = unique_sorted_spaces.get_index(index);
#ifdef DEBUG_LEGION
assert(child >= 0);
#endif
children.push_back(child);
}
}
}
//--------------------------------------------------------------------------
AddressSpaceID CollectiveMapping::find_nearest(AddressSpaceID search) const
//--------------------------------------------------------------------------
{
unsigned first = 0;
unsigned last = size() - 1;
if (search < (*this)[first])
return (*this)[first];
if (search > (*this)[last])
return (*this)[last];
// Contained somewhere in the middle so binary
// search for the two nearest options
unsigned mid = 0;
while (first <= last)
{
mid = (first + last) / 2;
const AddressSpaceID midval = (*this)[mid];
#ifdef DEBUG_LEGION
// Should never actually find it
assert(search != midval);
#endif
if (search < midval)
last = mid - 1;
else if (midval < search)
first = mid + 1;
else
break;
}
#ifdef DEBUG_LEGION
assert(first != last);
#endif
const unsigned diff_low = search - (*this)[first];
const unsigned diff_high = (*this)[last] - search;
if (diff_low < diff_high)
return (*this)[first];
else
return (*this)[last];
}
//--------------------------------------------------------------------------
bool CollectiveMapping::contains(const CollectiveMapping &rhs) const
//--------------------------------------------------------------------------
{
return !(rhs.unique_sorted_spaces - unique_sorted_spaces);
}
//--------------------------------------------------------------------------
CollectiveMapping* CollectiveMapping::clone_with(AddressSpaceID space) const
//--------------------------------------------------------------------------
{
CollectiveMapping *result = new CollectiveMapping(*this);
result->unique_sorted_spaces.insert(space);
result->total_spaces = result->unique_sorted_spaces.size();
return result;
}
//--------------------------------------------------------------------------
void CollectiveMapping::pack(Serializer &rez) const
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(total_spaces > 0);
#endif
rez.serialize(total_spaces);
rez.serialize(unique_sorted_spaces);
rez.serialize(radix);
}
//--------------------------------------------------------------------------
/*static*/ void CollectiveMapping::pack_null(Serializer &rez)
//--------------------------------------------------------------------------
{
rez.serialize<size_t>(0);
}
//--------------------------------------------------------------------------
unsigned CollectiveMapping::convert_to_offset(unsigned index,
unsigned origin_index) const
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(index < total_spaces);
assert(origin_index < total_spaces);
#endif
if (index < origin_index)
{
// Modulus arithmetic here
return ((index + total_spaces) - origin_index);
}
else
return (index - origin_index);
}
//--------------------------------------------------------------------------
unsigned CollectiveMapping::convert_to_index(unsigned offset,
unsigned origin_index) const
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(offset < total_spaces);
assert(origin_index < total_spaces);
#endif
unsigned result = origin_index + offset;
if (result >= total_spaces)
result -= total_spaces;
return result;
}
/////////////////////////////////////////////////////////////
// InstanceManager
/////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------
InstanceManager::InstanceManager(RegionTreeForest *ctx,
DistributedID did, LayoutDescription *desc,
FieldSpaceNode *node,
IndexSpaceExpression *domain,
RegionTreeID tid, bool register_now,
CollectiveMapping *mapping)
: DistributedCollectable(ctx->runtime, did, register_now, mapping),
context(ctx), layout(desc), field_space_node(node),
instance_domain(domain), tree_id(tid)
//--------------------------------------------------------------------------
{
// Add a reference to the layout
if (layout != NULL)
layout->add_reference();
if (field_space_node != NULL)
field_space_node->add_nested_gc_ref(did);
if (instance_domain != NULL)
instance_domain->add_nested_expression_reference(did);
}
//--------------------------------------------------------------------------
InstanceManager::~InstanceManager(void)
//--------------------------------------------------------------------------
{
if ((layout != NULL) && layout->remove_reference())
delete layout;
if ((field_space_node != NULL) &&
field_space_node->remove_nested_gc_ref(did))
delete field_space_node;
if ((instance_domain != NULL) &&
instance_domain->remove_nested_expression_reference(did))
delete instance_domain;
}
//--------------------------------------------------------------------------
bool InstanceManager::entails(LayoutConstraints *constraints,
const LayoutConstraint **failed_constraint) const
//--------------------------------------------------------------------------
{
const PointerConstraint &pointer = constraints->pointer_constraint;
if (pointer.is_valid)
{
PointerConstraint pointer_constraint = get_pointer_constraint();
// Always test the pointer constraint locally
if (!pointer_constraint.entails(constraints->pointer_constraint))
{
if (failed_constraint != NULL)
*failed_constraint = &pointer;
return false;
}
}
return layout->constraints->entails(constraints,
(instance_domain != NULL) ? instance_domain->get_num_dims() : 0,
failed_constraint, false/*test pointer*/);
}
//--------------------------------------------------------------------------
bool InstanceManager::entails(const LayoutConstraintSet &constraints,
const LayoutConstraint **failed_constraint) const
//--------------------------------------------------------------------------
{
const PointerConstraint &pointer = constraints.pointer_constraint;
if (pointer.is_valid)
{
PointerConstraint pointer_constraint = get_pointer_constraint();
// Always test the pointer constraint locally
if (!pointer_constraint.entails(constraints.pointer_constraint))
{
if (failed_constraint != NULL)
*failed_constraint = &pointer;
return false;
}
}
return layout->constraints->entails(constraints,
(instance_domain != NULL) ? instance_domain->get_num_dims() : 0,
failed_constraint, false/*test pointer*/);
}
//--------------------------------------------------------------------------
bool InstanceManager::conflicts(LayoutConstraints *constraints,
const LayoutConstraint **conflict_constraint) const
//--------------------------------------------------------------------------
{
const PointerConstraint &pointer = constraints->pointer_constraint;
if (pointer.is_valid)
{
PointerConstraint pointer_constraint = get_pointer_constraint();
// Always test the pointer constraint locally
if (pointer_constraint.conflicts(constraints->pointer_constraint))
{
if (conflict_constraint != NULL)
*conflict_constraint = &pointer;
return true;
}
}
// We know our layouts don't have a pointer constraint so nothing special
return layout->constraints->conflicts(constraints,
(instance_domain != NULL) ? instance_domain->get_num_dims() : 0,
conflict_constraint);
}
//--------------------------------------------------------------------------
bool InstanceManager::conflicts(const LayoutConstraintSet &constraints,
const LayoutConstraint **conflict_constraint) const
//--------------------------------------------------------------------------
{
const PointerConstraint &pointer = constraints.pointer_constraint;
if (pointer.is_valid)
{
PointerConstraint pointer_constraint = get_pointer_constraint();
// Always test the pointer constraint locally
if (pointer_constraint.conflicts(constraints.pointer_constraint))
{
if (conflict_constraint != NULL)
*conflict_constraint = &pointer;
return true;
}
}
// We know our layouts don't have a pointer constraint so nothing special
return layout->constraints->conflicts(constraints,
(instance_domain != NULL) ? instance_domain->get_num_dims() : 0,
conflict_constraint);
}
/////////////////////////////////////////////////////////////
// PhysicalManager
/////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------
PhysicalManager::PhysicalManager(RegionTreeForest *ctx, DistributedID did,
MemoryManager *memory, PhysicalInstance inst,
IndexSpaceExpression *instance_domain,
const void *pl, size_t pl_size,
FieldSpaceNode *node, RegionTreeID tree_id,
LayoutDescription *layout, ReductionOpID redop_id,
bool register_now, size_t footprint,
ApEvent u_event, LgEvent unique, InstanceKind k,
const ReductionOp *op /*= NULL*/,
CollectiveMapping *mapping /*=NULL*/,
ApEvent p_event /*= ApEvent::NO_AP_EVENT*/)
: InstanceManager(ctx, encode_instance_did(did,
(k == EXTERNAL_ATTACHED_INSTANCE_KIND), (redop_id > 0)), layout, node,
// If we're on the owner node we need to produce the expression
// that actually describes this points in this space
// On remote nodes we'll already have it from the owner
(ctx->runtime->determine_owner(did) == ctx->runtime->address_space) &&
(k != UNBOUND_INSTANCE_KIND) ?
instance_domain->create_layout_expression(pl, pl_size) :
instance_domain, tree_id, register_now, mapping),
memory_manager(memory), unique_event(unique),
instance_footprint(footprint), reduction_op((redop_id == 0) ? NULL :
ctx->runtime->get_reduction(redop_id)), redop(redop_id),
piece_list(pl), piece_list_size(pl_size), instance(inst),
use_event(Runtime::create_ap_user_event(NULL)),
instance_ready((k == UNBOUND_INSTANCE_KIND) ?
Runtime::create_rt_user_event() : RtUserEvent::NO_RT_USER_EVENT),
kind(k), external_pointer(-1UL), producer_event(p_event),
gc_state(COLLECTABLE_GC_STATE), pending_changes(0),
failed_collection_count(0), min_gc_priority(0), added_gc_events(0),
valid_references(0), sent_valid_references(0),
received_valid_references(0), padded_reservations(NULL)
//--------------------------------------------------------------------------
{
// If the manager was initialized with a valid Realm instance,
// trigger the use event with the ready event of the instance metadata
if (kind != UNBOUND_INSTANCE_KIND)
{
#ifdef DEBUG_LEGION
assert(instance.exists());
#endif
Runtime::trigger_event(NULL,use_event,fetch_metadata(instance,u_event));
}
else // add a resource reference to remove once this manager is set
add_base_valid_ref(PENDING_UNBOUND_REF);
if (!is_owner() && !is_external_instance())
memory_manager->register_remote_instance(this);
#ifdef LEGION_GC
log_garbage.info("GC Instance Manager %lld %d " IDFMT " " IDFMT " ",
LEGION_DISTRIBUTED_ID_FILTER(this->did), local_space,
inst.id, memory->memory.id);
#endif
if (runtime->legion_spy_enabled && (kind != UNBOUND_INSTANCE_KIND))
{
#ifdef DEBUG_LEGION
assert(unique_event.exists());
#endif
LegionSpy::log_physical_instance(unique_event,inst.id,memory->memory.id,
instance_domain->expr_id, field_space_node->handle, tree_id, redop);
layout->log_instance_layout(unique_event);
}
}
//--------------------------------------------------------------------------
PhysicalManager::~PhysicalManager(void)
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(subscribers.empty());
assert(valid_references == 0);
#endif
// Remote references removed by DistributedCollectable destructor
if (!is_owner() && !is_external_instance())
memory_manager->unregister_remote_instance(this);