From da533d5aa4f7a6a2a87ed64a88572f388ceb76c3 Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Mon, 1 Apr 2024 21:41:31 -0700 Subject: [PATCH] Add shuffle and use it in the mapper. --- src/deterministic_random.h | 4 ++++ src/deterministic_random.inl | 22 ++++++++++++++++++++++ src/mapper.cc | 33 ++++++++++++++++++++------------- src/mapper.h | 5 +++++ 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/deterministic_random.h b/src/deterministic_random.h index 56015f9..3a18c79 100644 --- a/src/deterministic_random.h +++ b/src/deterministic_random.h @@ -63,6 +63,8 @@ class RngStream { public: uint64_t uniform_uint64_t(); uint64_t uniform_range(uint64_t range_lo, uint64_t range_hi /* inclusive */); + template + void shuffle(std::vector &vec); template RngChannel make_channel(const T &hashable) const; @@ -84,6 +86,8 @@ class RngChannel { public: uint64_t uniform_uint64_t(); uint64_t uniform_range(uint64_t range_lo, uint64_t range_hi /* inclusive */); + template + void shuffle(std::vector &vec); private: const uint64_t seed; diff --git a/src/deterministic_random.inl b/src/deterministic_random.inl index f5247ac..1714684 100644 --- a/src/deterministic_random.inl +++ b/src/deterministic_random.inl @@ -21,3 +21,25 @@ RngChannel RngStream::make_channel(const T &hashable) const { template RngChannel::RngChannel(uint64_t _seed, uint64_t _stream, const T &hashable) : seed(_seed), stream(_stream), channel(hash(hashable)), seq(0) {} + +template +void shuffle(T &rng, std::vector &vec) { + if (vec.empty()) { + return; + } + + for (uint64_t idx = vec.size() - 1; idx > 0; --idx) { + uint64_t target = rng.uniform_range(0, idx); + std::swap(vec[idx], vec[target]); + } +} + +template +void RngStream::shuffle(std::vector &vec) { + ::shuffle(*this, vec); +} + +template +void RngChannel::shuffle(std::vector &vec) { + ::shuffle(*this, vec); +} diff --git a/src/mapper.cc b/src/mapper.cc index ba54f35..1b91598 100644 --- a/src/mapper.cc +++ b/src/mapper.cc @@ -23,7 +23,9 @@ enum MapperCallIDs { SELECT_TASKS_TO_MAP, SLICE_TASK, MAP_TASK, + SELECT_TASK_SOURCES, MAP_INLINE, + SELECT_INLINE_SOURCES, }; static Logger log_map("fuzz_mapper"); @@ -33,6 +35,7 @@ FuzzMapper::FuzzMapper(MapperRuntime *rt, Machine machine, Processor local, RngS stream(st), select_tasks_to_map_channel(st.make_channel(int32_t(SELECT_TASKS_TO_MAP))), map_inline_channel(st.make_channel(int32_t(MAP_INLINE))), + select_inline_sources_channel(st.make_channel(int32_t(SELECT_INLINE_SOURCES))), local_proc(local) { // TODO: something other than CPU processor { @@ -141,10 +144,8 @@ void FuzzMapper::replicate_task(MapperContext ctx, const Task &task, void FuzzMapper::select_task_sources(const MapperContext ctx, const Task &task, const SelectTaskSrcInput &input, SelectTaskSrcOutput &output) { - // TODO: shuffle the source instances - output.chosen_ranking.insert(output.chosen_ranking.end(), - input.source_instances.begin(), - input.source_instances.end()); + RngChannel rng = make_task_channel(SELECT_TASK_SOURCES, task); + random_sources(rng, input.source_instances, output.chosen_ranking); } void FuzzMapper::map_inline(const MapperContext ctx, const InlineMapping &inline_op, @@ -158,10 +159,8 @@ void FuzzMapper::select_inline_sources(const MapperContext ctx, const InlineMapping &inline_op, const SelectInlineSrcInput &input, SelectInlineSrcOutput &output) { - // TODO: shuffle the source instances - output.chosen_ranking.insert(output.chosen_ranking.end(), - input.source_instances.begin(), - input.source_instances.end()); + RngChannel &rng = select_inline_sources_channel; + random_sources(rng, input.source_instances, output.chosen_ranking); } void FuzzMapper::select_partition_projection(const MapperContext ctx, @@ -263,9 +262,9 @@ void FuzzMapper::random_mapping(const MapperContext ctx, RngChannel &rng, auto it = query.begin(); std::advance(it, target); memory = *it; - log_map.debug() << "map_task: Selected memory " << memory << " kind " - << memory.kind(); } + log_map.debug() << "random_mapping: Selected memory " << memory << " kind " + << memory.kind(); LayoutConstraintSet constraints; if (req.privilege == LEGION_REDUCE) { @@ -300,7 +299,7 @@ void FuzzMapper::random_mapping(const MapperContext ctx, RngChannel &rng, dimension_ordering.at(i) = static_cast(static_cast(LEGION_DIM_X) + i); dimension_ordering[dim] = LEGION_DIM_F; - // TODO: shuffle this ordering + rng.shuffle(dimension_ordering); bool contiguous = rng.uniform_range(0, 1) == 0; constraints.add_constraint(OrderingConstraint(dimension_ordering, contiguous)); } @@ -319,7 +318,7 @@ void FuzzMapper::random_mapping(const MapperContext ctx, RngChannel &rng, if (rng.uniform_range(0, 1) == 0) { if (!runtime->create_physical_instance(ctx, memory, constraints, regions, instance, true /* acquire */, LEGION_GC_MAX_PRIORITY)) { - log_map.fatal() << "map_task: Failed to create instance"; + log_map.fatal() << "random_mapping: Failed to create instance"; abort(); } } else { @@ -327,11 +326,19 @@ void FuzzMapper::random_mapping(const MapperContext ctx, RngChannel &rng, if (!runtime->find_or_create_physical_instance(ctx, memory, constraints, regions, instance, created, true /* acquire */, LEGION_GC_NEVER_PRIORITY)) { - log_map.fatal() << "map_task: Failed to create instance"; + log_map.fatal() << "random_mapping: Failed to create instance"; abort(); } } output.push_back(instance); } +void FuzzMapper::random_sources(RngChannel &rng, + const std::vector &source_instances, + std::deque &chosen_ranking) { + std::vector sources(source_instances.begin(), source_instances.end()); + rng.shuffle(sources); + chosen_ranking.insert(chosen_ranking.end(), sources.begin(), sources.end()); +} + } // namespace FuzzMapper diff --git a/src/mapper.h b/src/mapper.h index 00cc462..cfe2009 100644 --- a/src/mapper.h +++ b/src/mapper.h @@ -84,10 +84,15 @@ class FuzzMapper : public NullMapper { const RegionRequirement &req, std::vector &output); + void random_sources(RngChannel &rng, + const std::vector &source_instances, + std::deque &chosen_ranking); + private: RngStream stream; RngChannel select_tasks_to_map_channel; RngChannel map_inline_channel; + RngChannel select_inline_sources_channel; Processor local_proc; std::vector local_procs;