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