diff options
| author | TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> | 2026-11-25 02:05:25 +0100 |
|---|---|---|
| committer | TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> | 2026-11-25 04:46:33 +0100 |
| commit | a3980cd437b28a047f8e0da3a698df32a34ecbc0 (patch) | |
| tree | 920815a3a444490ad3156a5bc334beba92cfc656 /runtime/src | |
| parent | 5129be10d47e16ecf5d8c41cfec12a59a2f25512 (diff) | |
runtime: a bounded worker pool for pipeline compiles (RT-PIPELINE-COMPILE-WORKERS)
PipelineWorkers::run is a blocking parallel loop: the calling thread works on its own batch beside the workers, so a batch completes with no worker left and nothing outlives the call. CPU test: every job once and before run returns, no workers, exceptions, several callers, stop with jobs queued, and the LEONIDA_PIPELINE_THREADS text.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/pipeline_workers.h | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/runtime/src/pipeline_workers.h b/runtime/src/pipeline_workers.h new file mode 100644 index 000000000..664bfe0b8 --- /dev/null +++ b/runtime/src/pipeline_workers.h | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | // A bounded pool that compiles the pipelines of one submission side by side (docs/d3d12_vulkan.md, "Pipeline creation"). | ||
| 2 | // | ||
| 3 | // run() is a blocking parallel loop: the calling thread works on its own batch beside the workers, so a batch completes | ||
| 4 | // even with no worker left (no thread could be started, or stop() was called), and nothing outlives the call. Several | ||
| 5 | // threads may call run() at once; they share the workers. The workers are started by the first batch that can use them, | ||
| 6 | // so a device that never compiles at draw time has none. They are plain std::threads: a driver's compiler needs the 8 MB | ||
| 7 | // of stack the loader's header gives those (docs/pipeline_check.md, "Stack reserve"). | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <condition_variable> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <cstdint> | ||
| 14 | #include <deque> | ||
| 15 | #include <exception> | ||
| 16 | #include <functional> | ||
| 17 | #include <mutex> | ||
| 18 | #include <system_error> | ||
| 19 | #include <thread> | ||
| 20 | #include <utility> | ||
| 21 | #include <vector> | ||
| 22 | |||
| 23 | namespace leonida { | ||
| 24 | |||
| 25 | class PipelineWorkers { | ||
| 26 | public: | ||
| 27 | static constexpr unsigned kMaxThreads = 64; | ||
| 28 | |||
| 29 | // What the pool has done: batches handed to the workers, the jobs in them, and the jobs running now. | ||
| 30 | struct Stats { | ||
| 31 | unsigned threads = 0; // the most workers the pool starts | ||
| 32 | std::size_t started = 0; // the workers it has started | ||
| 33 | std::uint64_t batches = 0; | ||
| 34 | std::uint64_t jobs = 0; | ||
| 35 | std::size_t running = 0; | ||
| 36 | }; | ||
| 37 | |||
| 38 | explicit PipelineWorkers(unsigned threads) : limit_(std::min(threads, kMaxThreads)) {} | ||
| 39 | ~PipelineWorkers() { stop(); } | ||
| 40 | PipelineWorkers(const PipelineWorkers&) = delete; | ||
| 41 | PipelineWorkers& operator=(const PipelineWorkers&) = delete; | ||
| 42 | |||
| 43 | unsigned threads() const { return limit_; } | ||
| 44 | |||
| 45 | // Calls job(0) .. job(count - 1) and returns when every call has returned. When calls threw, rethrows what the | ||
| 46 | // lowest of them threw, after the last has returned. With no workers or a single job the calls are made in order on | ||
| 47 | // this thread, and the first exception ends them. | ||
| 48 | void run(std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 49 | if (!limit_ || count < 2) { | ||
| 50 | for (std::size_t i = 0; i < count; ++i) job(i); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | Batch batch{&job, count}; | ||
| 54 | std::unique_lock lock(mutex_); | ||
| 55 | queue_.push_back(&batch); | ||
| 56 | ++batches_; | ||
| 57 | jobs_ += count; | ||
| 58 | start(count - 1); | ||
| 59 | work_.notify_all(); | ||
| 60 | while (batch.next < batch.count) step(batch, lock); | ||
| 61 | batch.done.wait(lock, [&] { return !batch.running; }); | ||
| 62 | lock.unlock(); | ||
| 63 | if (batch.error) std::rethrow_exception(batch.error); | ||
| 64 | } | ||
| 65 | |||
| 66 | // Ends the workers once each has finished the job it is in. A run() in progress on another thread finishes its | ||
| 67 | // remaining jobs itself, and so does every later one. | ||
| 68 | void stop() { | ||
| 69 | std::vector<std::thread> threads; | ||
| 70 | { | ||
| 71 | std::lock_guard lock(mutex_); | ||
| 72 | stopping_ = true; | ||
| 73 | threads.swap(threads_); | ||
| 74 | } | ||
| 75 | work_.notify_all(); | ||
| 76 | for (std::thread& thread : threads) thread.join(); | ||
| 77 | } | ||
| 78 | |||
| 79 | Stats stats() { | ||
| 80 | std::lock_guard lock(mutex_); | ||
| 81 | return {limit_, started_, batches_, jobs_, running_}; | ||
| 82 | } | ||
| 83 | |||
| 84 | private: | ||
| 85 | struct Batch { // on the stack of the run() that made it; guarded by mutex_ | ||
| 86 | const std::function<void(std::size_t)>* job; | ||
| 87 | std::size_t count; | ||
| 88 | std::size_t next = 0; | ||
| 89 | std::size_t running = 0; | ||
| 90 | std::exception_ptr error{}; | ||
| 91 | std::size_t error_index = 0; | ||
| 92 | std::condition_variable done{}; | ||
| 93 | }; | ||
| 94 | |||
| 95 | // With mutex_ held: enough workers for `wanted` more jobs, within the limit. | ||
| 96 | void start(std::size_t wanted) { | ||
| 97 | wanted = std::min<std::size_t>(wanted, limit_); | ||
| 98 | while (!stopping_ && !exhausted_ && threads_.size() < wanted) { | ||
| 99 | try { | ||
| 100 | threads_.emplace_back([this] { work(); }); | ||
| 101 | ++started_; | ||
| 102 | } catch (const std::system_error&) { | ||
| 103 | exhausted_ = true; // the workers there are, and the callers, do it all | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | void work() { | ||
| 109 | std::unique_lock lock(mutex_); | ||
| 110 | for (;;) { | ||
| 111 | work_.wait(lock, [this] { return stopping_ || !queue_.empty(); }); | ||
| 112 | if (stopping_) return; | ||
| 113 | step(*queue_.front(), lock); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | // Takes the next job of `batch` (there is one), runs it unlocked, and returns locked. The batch leaves the queue | ||
| 118 | // with its last job, so no worker can reach it once run() has seen nothing left and nothing running. | ||
| 119 | void step(Batch& batch, std::unique_lock<std::mutex>& lock) { | ||
| 120 | const std::size_t index = batch.next++; | ||
| 121 | if (batch.next == batch.count) std::erase(queue_, &batch); | ||
| 122 | ++batch.running; | ||
| 123 | ++running_; | ||
| 124 | lock.unlock(); | ||
| 125 | std::exception_ptr error; | ||
| 126 | try { | ||
| 127 | (*batch.job)(index); | ||
| 128 | } catch (...) { | ||
| 129 | error = std::current_exception(); | ||
| 130 | } | ||
| 131 | lock.lock(); | ||
| 132 | --running_; | ||
| 133 | if (error && (!batch.error || index < batch.error_index)) { | ||
| 134 | batch.error = std::move(error); | ||
| 135 | batch.error_index = index; | ||
| 136 | } | ||
| 137 | // Notified with the mutex held: run() cannot return, and end the batch, before this has. | ||
| 138 | if (!--batch.running && batch.next == batch.count) batch.done.notify_all(); | ||
| 139 | } | ||
| 140 | |||
| 141 | const unsigned limit_; | ||
| 142 | std::mutex mutex_; | ||
| 143 | std::condition_variable work_; | ||
| 144 | std::deque<Batch*> queue_; // batches with jobs nobody has taken yet | ||
| 145 | std::vector<std::thread> threads_; | ||
| 146 | bool stopping_ = false; | ||
| 147 | bool exhausted_ = false; | ||
| 148 | std::size_t started_ = 0; | ||
| 149 | std::uint64_t batches_ = 0; | ||
| 150 | std::uint64_t jobs_ = 0; | ||
| 151 | std::size_t running_ = 0; | ||
| 152 | }; | ||
| 153 | |||
| 154 | // LEONIDA_PIPELINE_THREADS: how many workers, 0 for none (pipelines compile on the thread that submits, as before). | ||
| 155 | // Unset or empty: half the logical processors and at most 8, past which the compiles of one submission gained little | ||
| 156 | // (docs/d3d12_vulkan.md). `valid` is false for text that is not a number, which counts as unset. | ||
| 157 | struct PipelineThreads { | ||
| 158 | unsigned count = 0; | ||
| 159 | bool valid = true; | ||
| 160 | }; | ||
| 161 | |||
| 162 | inline PipelineThreads pipeline_threads(const char* text, unsigned logical_processors) { | ||
| 163 | const unsigned automatic = std::min(8u, logical_processors / 2); | ||
| 164 | if (!text || !*text) return {automatic, true}; | ||
| 165 | unsigned value = 0; | ||
| 166 | for (const char* c = text; *c; ++c) { | ||
| 167 | if (*c < '0' || *c > '9') return {automatic, false}; | ||
| 168 | value = std::min(PipelineWorkers::kMaxThreads, value * 10 + unsigned(*c - '0')); | ||
| 169 | } | ||
| 170 | return {value, true}; | ||
| 171 | } | ||
| 172 | |||
| 173 | } // namespace leonida | ||