src/util/parallel.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/util/parallel.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <atomic> | ||
| 7 | #include <condition_variable> | ||
| 8 | #include <deque> | ||
| 9 | #include <exception> | ||
| 10 | #include <memory> | ||
| 11 | #include <mutex> | ||
| 12 | #include <thread> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// One `ParallelFor` call: its tasks, handed out in order to the caller and its helpers. | ||
| 20 | struct Job { | ||
| 21 | std::size_t count = 0; | ||
| 22 | const std::function<void(std::size_t)>* task = nullptr; | ||
| 23 | std::atomic<std::size_t> next{0}; | ||
| 24 | std::atomic<int> slots{0}; | ||
| 25 | std::atomic<bool> failed{false}; | ||
| 26 | std::mutex mutex; | ||
| 27 | std::condition_variable idle; | ||
| 28 | int active = 0; | ||
| 29 | std::exception_ptr error; | ||
| 30 | |||
| 31 | /// Runs tasks until none are left or one has failed. | ||
| 32 | 4320 | void Work() { | |
| 33 | 102043 | while (!failed.load()) { | |
| 34 | 97719 | const std::size_t i = next.fetch_add(1); | |
| 35 | 97719 | if (i >= count) { | |
| 36 | return; | ||
| 37 | } | ||
| 38 | 93402 | try { | |
| 39 | 93402 | (*task)(i); | |
| 40 | 2 | } catch (...) { | |
| 41 | 3 | const std::lock_guard lock(mutex); | |
| 42 | 3 | if (!error) { | |
| 43 | 2 | error = std::current_exception(); | |
| 44 | } | ||
| 45 | 3 | failed.store(true); | |
| 46 | 3 | } | |
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | /// Whether helpers have nothing left to take. | ||
| 51 | 4257 | bool Exhausted() const { | |
| 52 | 4257 | return failed.load() || next.load() >= count; | |
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | /// Threads kept for the life of the program, helping whichever jobs are waiting. The pool is | ||
| 57 | /// never destroyed: joining threads while statics are torn down can deadlock, as when a library is | ||
| 58 | /// unloaded on Windows, and the threads end with the process. | ||
| 59 | class Pool { | ||
| 60 | public: | ||
| 61 | 240 | Pool() { | |
| 62 | 240 | const unsigned size = std::max(1u, std::thread::hardware_concurrency()) - 1; | |
| 63 | 960 | for (unsigned t = 0; t < size; ++t) { | |
| 64 | 1440 | std::thread([this] { Loop(); }).detach(); | |
| 65 | } | ||
| 66 | 240 | } | |
| 67 | |||
| 68 | Pool(const Pool&) = delete; | ||
| 69 | Pool& operator=(const Pool&) = delete; | ||
| 70 | |||
| 71 | /// Offers `job` to the pool's threads. | ||
| 72 | 2081 | void Post(const std::shared_ptr<Job>& job) { | |
| 73 | 2081 | { | |
| 74 | 2081 | const std::lock_guard lock(m_mutex); | |
| 75 | 2081 | m_jobs.push_back(job); | |
| 76 | 2081 | } | |
| 77 | 2081 | m_wake.notify_all(); | |
| 78 | 2081 | } | |
| 79 | |||
| 80 | private: | ||
| 81 | 626 | void Loop() { | |
| 82 | 626 | std::unique_lock lock(m_mutex); | |
| 83 | 4873 | while (true) { | |
| 84 | 9032 | m_wake.wait(lock, [this] { return !m_jobs.empty(); }); | |
| 85 | 4257 | std::shared_ptr<Job> job = m_jobs.front(); | |
| 86 | 4257 | if (job->Exhausted() || job->slots.fetch_sub(1) <= 0) { | |
| 87 | 2015 | m_jobs.pop_front(); | |
| 88 | 2015 | continue; | |
| 89 | } | ||
| 90 | 2242 | lock.unlock(); | |
| 91 | 2241 | { | |
| 92 | 2241 | const std::lock_guard jobLock(job->mutex); | |
| 93 | 2242 | ++job->active; | |
| 94 | 2242 | } | |
| 95 | 2242 | job->Work(); | |
| 96 | 2242 | { | |
| 97 | 2242 | const std::lock_guard jobLock(job->mutex); | |
| 98 | 2242 | --job->active; | |
| 99 | 2242 | } | |
| 100 | 2242 | job->idle.notify_all(); | |
| 101 | 2233 | lock.lock(); | |
| 102 | 4247 | } | |
| 103 | } | ||
| 104 | |||
| 105 | std::mutex m_mutex; | ||
| 106 | std::condition_variable m_wake; | ||
| 107 | std::deque<std::shared_ptr<Job>> m_jobs; | ||
| 108 | }; | ||
| 109 | |||
| 110 | 2081 | Pool& SharedPool() { | |
| 111 | 2081 | static Pool* const pool = new Pool; | |
| 112 | 2081 | return *pool; | |
| 113 | } | ||
| 114 | |||
| 115 | /// The number of threads a request resolves to: `threads` when positive, otherwise the hardware | ||
| 116 | /// thread count (at least 1). | ||
| 117 | 245413 | unsigned ResolveThreads(unsigned threads) { | |
| 118 | 245413 | if (threads > 0) { | |
| 119 | return threads; | ||
| 120 | } | ||
| 121 | 5060 | return std::max(1u, std::thread::hardware_concurrency()); | |
| 122 | } | ||
| 123 | |||
| 124 | } // namespace | ||
| 125 | |||
| 126 | 245089 | void ParallelFor(std::size_t count, unsigned threads, | |
| 127 | const std::function<void(std::size_t index)>& task) { | ||
| 128 | 245089 | const std::size_t workers = std::min<std::size_t>(ResolveThreads(threads), count); | |
| 129 | 245466 | if (workers <= 1) { | |
| 130 | 541547 | for (std::size_t i = 0; i < count; ++i) { | |
| 131 | 298096 | task(i); | |
| 132 | } | ||
| 133 | 243451 | return; | |
| 134 | } | ||
| 135 | 2081 | auto job = std::make_shared<Job>(); | |
| 136 | 2081 | job->count = count; | |
| 137 | 2081 | job->task = &task; | |
| 138 | 2081 | job->slots.store(static_cast<int>(workers - 1)); | |
| 139 | 2081 | SharedPool().Post(job); | |
| 140 | 2081 | job->Work(); | |
| 141 | 2081 | { | |
| 142 | 2081 | std::unique_lock lock(job->mutex); | |
| 143 | 2680 | job->idle.wait(lock, [&] { return job->active == 0; }); | |
| 144 | } | ||
| 145 | 2081 | job->slots.store(0); | |
| 146 | 2081 | if (job->error) { | |
| 147 | 6 | std::rethrow_exception(job->error); | |
| 148 | } | ||
| 149 | 2079 | } | |
| 150 | |||
| 151 | } // namespace terraingen | ||
| 152 |