Add the M01 serial simulation reference and per-tick replay
This commit is contained in:
1 parent
904bdf8827
commit
8d2870a6f8
11 files changed
+538
-28
No files matched your search
+45
-23
@@ -139,7 +139,18 @@ std::size_t checked_cell_count(std::int32_t width, std::int32_t height) {
|
||||
return static_cast<std::size_t>(width) * static_cast<std::size_t>(height);
|
||||
}
|
||||
|
||||
std::size_t checked_worker_count(std::size_t workers) {
|
||||
std::size_t checked_worker_count(std::size_t workers, ExecutionMode mode) {
|
||||
switch (mode) {
|
||||
case ExecutionMode::SerialReference:
|
||||
if (workers != 1) {
|
||||
throw std::invalid_argument("serial reference requires one logical worker");
|
||||
}
|
||||
return workers;
|
||||
case ExecutionMode::Parallel:
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument("execution mode is not supported");
|
||||
}
|
||||
if (workers < min_worker_count || workers > max_worker_count) {
|
||||
throw std::invalid_argument("worker count must be in the range 2..64");
|
||||
}
|
||||
@@ -268,11 +279,12 @@ void Storage::add_item(ItemType type) {
|
||||
}
|
||||
|
||||
World::World(std::uint64_t seed, std::int32_t width, std::int32_t height,
|
||||
std::size_t workers)
|
||||
std::size_t workers, ExecutionMode mode)
|
||||
: seed_(seed),
|
||||
width_(width),
|
||||
height_(height),
|
||||
worker_count_(checked_worker_count(workers)),
|
||||
worker_count_(checked_worker_count(workers, mode)),
|
||||
execution_mode_(mode),
|
||||
blocked_(checked_cell_count(width, height), 0) {}
|
||||
|
||||
std::size_t World::cell_index(GridPosition position) const noexcept {
|
||||
@@ -1059,19 +1071,22 @@ void World::assign_idle_agents() {
|
||||
return choices;
|
||||
};
|
||||
|
||||
std::vector<std::future<std::vector<std::size_t>>> choice_futures;
|
||||
choice_futures.reserve(chunk_count);
|
||||
for (std::size_t chunk = 0; chunk < chunk_count; ++chunk) {
|
||||
const std::size_t begin = chunk_start(chunk);
|
||||
const std::size_t end = chunk_start(chunk + 1);
|
||||
choice_futures.push_back(worker_pool_->submit([&choose_jobs, begin, end] {
|
||||
return choose_jobs(begin, end);
|
||||
}));
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::size_t>> choices;
|
||||
choices.reserve(chunk_count);
|
||||
for (auto& future : choice_futures) choices.push_back(future.get());
|
||||
if (execution_mode_ == ExecutionMode::SerialReference) {
|
||||
choices.push_back(choose_jobs(0, agents_.size()));
|
||||
} else {
|
||||
std::vector<std::future<std::vector<std::size_t>>> choice_futures;
|
||||
choice_futures.reserve(chunk_count);
|
||||
for (std::size_t chunk = 0; chunk < chunk_count; ++chunk) {
|
||||
const std::size_t begin = chunk_start(chunk);
|
||||
const std::size_t end = chunk_start(chunk + 1);
|
||||
choice_futures.push_back(worker_pool_->submit([&choose_jobs, begin, end] {
|
||||
return choose_jobs(begin, end);
|
||||
}));
|
||||
}
|
||||
for (auto& future : choice_futures) choices.push_back(future.get());
|
||||
}
|
||||
for (std::size_t chunk = 0; chunk < chunk_count; ++chunk) {
|
||||
const std::size_t begin = chunk_start(chunk);
|
||||
for (std::size_t offset = 0; offset < choices[chunk].size(); ++offset) {
|
||||
@@ -1089,17 +1104,20 @@ void World::assign_idle_agents() {
|
||||
if (requests.empty()) return;
|
||||
|
||||
std::vector<std::future<PathResult>> futures;
|
||||
futures.reserve(requests.size());
|
||||
for (const Request& request : requests) {
|
||||
futures.push_back(worker_pool_->submit([this, start = request.start, goal = request.goal] {
|
||||
return find_path(start, goal);
|
||||
}));
|
||||
if (execution_mode_ == ExecutionMode::Parallel) {
|
||||
futures.reserve(requests.size());
|
||||
for (const Request& request : requests) {
|
||||
futures.push_back(worker_pool_->submit([this, start = request.start, goal = request.goal] {
|
||||
return find_path(start, goal);
|
||||
}));
|
||||
}
|
||||
}
|
||||
for (std::size_t index = 0; index < requests.size(); ++index) {
|
||||
auto path = futures[index].get();
|
||||
const Request& request = requests[index];
|
||||
auto path = execution_mode_ == ExecutionMode::SerialReference
|
||||
? find_path(request.start, request.goal) : futures[index].get();
|
||||
++path_searches_;
|
||||
path_nodes_visited_ += path.nodes_visited;
|
||||
const Request& request = requests[index];
|
||||
Agent& agent = agents_[request.agent_index];
|
||||
WorkJob& job = jobs_[request.job_index];
|
||||
if (!path.reachable) {
|
||||
@@ -1565,7 +1583,9 @@ void World::grow_sites() {
|
||||
|
||||
void World::step(TickProfile* profile) {
|
||||
if (paused_) return;
|
||||
if (!worker_pool_ && !agents_.empty()) worker_pool_ = std::make_unique<WorkerPool>(worker_count_);
|
||||
if (execution_mode_ == ExecutionMode::Parallel && !worker_pool_ && !agents_.empty()) {
|
||||
worker_pool_ = std::make_unique<WorkerPool>(worker_count_);
|
||||
}
|
||||
using Clock = std::chrono::steady_clock;
|
||||
auto phase_start = profile ? Clock::now() : Clock::time_point{};
|
||||
const auto finish_phase = [&](std::chrono::nanoseconds TickProfile::*phase) {
|
||||
@@ -1636,7 +1656,9 @@ void World::step(TickProfile* profile) {
|
||||
return completed;
|
||||
};
|
||||
|
||||
if (!agents_.empty()) {
|
||||
if (execution_mode_ == ExecutionMode::SerialReference) {
|
||||
jobs_completed_ += advance_agents(0, agents_.size());
|
||||
} else if (!agents_.empty()) {
|
||||
const auto chunk_count = std::min(worker_count_, agents_.size());
|
||||
std::vector<std::future<std::uint64_t>> futures;
|
||||
futures.reserve(chunk_count);
|
||||
|
||||
Reference in new issue
Block a user