Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- Update README to clarify high-level purpose and usage of the project (#1264)
- Remove unused breaks_travel_margin members from TWRoute and associated code (#1295)
- Run routing requests in parallel (#1218)
- Refactor parallel solving (#1305)

#### CI

Expand Down
74 changes: 39 additions & 35 deletions src/problems/vrp.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,47 +203,51 @@ class VRP {

SolvingContext<Route> context(_input, nb_searches);

// Split the heuristic parameters among threads.
std::vector<std::vector<std::size_t>>
thread_ranks(nb_threads, std::vector<std::size_t>());
for (std::size_t i = 0; i < nb_searches; ++i) {
thread_ranks[i % nb_threads].push_back(i);
}

std::exception_ptr ep = nullptr;
std::mutex ep_m;

auto run_solving =
[&context, &parameters, &timeout, &ep, &ep_m, depth, this](
const std::vector<std::size_t>& param_ranks) {
try {
// Decide time allocated for each search.
Timeout search_time;
if (timeout.has_value()) {
search_time = timeout.value() / param_ranks.size();
}

for (auto rank : param_ranks) {
run_single_search<Route, LocalSearch>(_input,
parameters[rank],
rank,
depth,
search_time,
context);
}
} catch (...) {
const std::scoped_lock<std::mutex> lock(ep_m);
ep = std::current_exception();
}
};
const auto actual_nb_threads = std::min(nb_searches, nb_threads);
assert(actual_nb_threads <= 32);
std::counting_semaphore<32> semaphore(actual_nb_threads);

Timeout search_time;
if (timeout.has_value()) {
// Max number of solving per thread.
const auto dv = std::div(static_cast<long>(nb_searches),
static_cast<long>(actual_nb_threads));
const unsigned max_solving_number = dv.quot + ((dv.rem == 0) ? 0 : 1);
search_time = timeout.value() / max_solving_number;
}

auto run_solving = [&context,
&semaphore,
&search_time,
&parameters,
&timeout,
&ep,
&ep_m,
depth,
this](const unsigned rank) {
semaphore.acquire();
try {
run_single_search<Route, LocalSearch>(_input,
parameters[rank],
rank,
depth,
search_time,
context);
} catch (...) {
const std::scoped_lock<std::mutex> lock(ep_m);
ep = std::current_exception();
}
semaphore.release();
};

std::vector<std::jthread> solving_threads;
solving_threads.reserve(nb_threads);
solving_threads.reserve(nb_searches);

for (const auto& param_ranks : thread_ranks) {
if (!param_ranks.empty()) {
solving_threads.emplace_back(run_solving, param_ranks);
}
for (unsigned i = 0; i < nb_searches; ++i) {
solving_threads.emplace_back(run_solving, i);
}

for (auto& t : solving_threads) {
Expand Down