Skip to content

Commit

Permalink
Add shuffle and use it in the mapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Apr 2, 2024
1 parent 5d4085a commit da533d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/deterministic_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
void shuffle(std::vector<T> &vec);

template <typename T>
RngChannel make_channel(const T &hashable) const;
Expand All @@ -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 <typename T>
void shuffle(std::vector<T> &vec);

private:
const uint64_t seed;
Expand Down
22 changes: 22 additions & 0 deletions src/deterministic_random.inl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ RngChannel RngStream::make_channel(const T &hashable) const {
template <typename T>
RngChannel::RngChannel(uint64_t _seed, uint64_t _stream, const T &hashable)
: seed(_seed), stream(_stream), channel(hash(hashable)), seq(0) {}

template <typename T, typename S>
void shuffle(T &rng, std::vector<S> &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 <typename T>
void RngStream::shuffle(std::vector<T> &vec) {
::shuffle(*this, vec);
}

template <typename T>
void RngChannel::shuffle(std::vector<T> &vec) {
::shuffle(*this, vec);
}
33 changes: 20 additions & 13 deletions src/mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -300,7 +299,7 @@ void FuzzMapper::random_mapping(const MapperContext ctx, RngChannel &rng,
dimension_ordering.at(i) =
static_cast<DimensionKind>(static_cast<int>(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));
}
Expand All @@ -319,19 +318,27 @@ 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 {
bool created;
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<PhysicalInstance> &source_instances,
std::deque<PhysicalInstance> &chosen_ranking) {
std::vector<PhysicalInstance> sources(source_instances.begin(), source_instances.end());
rng.shuffle(sources);
chosen_ranking.insert(chosen_ranking.end(), sources.begin(), sources.end());
}

} // namespace FuzzMapper
5 changes: 5 additions & 0 deletions src/mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ class FuzzMapper : public NullMapper {
const RegionRequirement &req,
std::vector<PhysicalInstance> &output);

void random_sources(RngChannel &rng,
const std::vector<PhysicalInstance> &source_instances,
std::deque<PhysicalInstance> &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<Processor> local_procs;
Expand Down

0 comments on commit da533d5

Please sign in to comment.