Skip to content
Merged
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
26 changes: 19 additions & 7 deletions multithread/parallel_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ template <ISolver Solver> class ParallelRunner {
int num_threads_;
std::vector<Solver> instances;
std::vector<std::optional<typename Solver::Ret>> rets;
int num_failures_;

std::mutex mtx;

ParallelRunner(int num_threads = std::thread::hardware_concurrency())
: num_threads_(num_threads > 0 ? num_threads : 1) {
: num_threads_(num_threads > 0 ? num_threads : 1), num_failures_(0) {
std::cerr << "num_threads: " << num_threads_ << std::endl;
}

Expand All @@ -40,6 +41,10 @@ template <ISolver Solver> class ParallelRunner {
}
}

void show_result() const {
std::cerr << "Done: " << num_failures_ << " failures." << std::endl;
}

void run_sequential() {
rets.assign(instances.size(), std::nullopt);

Expand All @@ -49,6 +54,8 @@ template <ISolver Solver> class ParallelRunner {
mhc_stdout_(instances.at(index), rets.at(index).value(), index);
}
}

show_result();
}

void run_parallel(int num_skip = 0) {
Expand Down Expand Up @@ -85,6 +92,8 @@ template <ISolver Solver> class ParallelRunner {
}

for (auto &f : futures) f.get();

show_result();
}

void run_single_(int current_index) {
Expand All @@ -100,10 +109,12 @@ template <ISolver Solver> class ParallelRunner {
} catch (const std::exception &e) {
std::unique_lock<std::mutex> lock(mtx);
std::cerr << "Error in Case #" << current_index + 1 << ": " << e.what() << std::endl;
++num_failures_;
return;
} catch (...) {
std::unique_lock<std::mutex> lock(mtx);
std::cerr << "Unknown error in Case #" << current_index + 1 << std::endl;
++num_failures_;
return;
}

Expand All @@ -122,7 +133,6 @@ template <ISolver Solver> class ParallelRunner {
std::cout << std::flush;
}
};

#endif // PARALLEL_RUNNER_HPP

/* Usage:
Expand All @@ -143,10 +153,12 @@ struct Solver {
}
};

int T;
cin >> T;
int main() {
int T;
cin >> T;

ParallelRunner<Solver> pm;
pm.read_all(T);
pm.run_parallel();
ParallelRunner<Solver> pm;
pm.read_all(T);
pm.run_parallel();
}
*/