-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibjobs.h
363 lines (304 loc) · 10.7 KB
/
libjobs.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#pragma once
#include <condition_variable>
#include <algorithm>
#include <atomic>
#include <vector>
#include <array>
#include <sstream>
#include <limits>
#include <thread>
#include <string_view>
#include <cstring>
#include <immintrin.h>
#include <functional>
#include <memory>
#include <cstdint>
#ifdef _WIN32
#include <malloc.h>
#define libjobs_alloca(x) _alloca(x)
#else
#include <alloca.h>
#define libjobs_alloca(x) alloca(x)
#endif
namespace libjobs {
namespace platform {
enum class ThreadPriority : int32_t {
Lowest = -2,
BelowNormal = -1,
Normal = 0,
AboveNormal = 1,
Highest = 2
};
inline void yield() noexcept {
#if defined(_WIN32)
YieldProcessor();
#elif defined(SSE2) || defined(__SSE2__)
_mm_pause();
#else
#warning No yield implementation, using std::this_thread::yield()
std::this_thread::yield();
#endif
}
inline void setThreadName(std::thread& thread, std::string_view name) noexcept {
#ifdef _WIN32
auto wideName = std::wstring(name.begin(), name.end());
::SetThreadDescription(thread.native_handle(), wideName.c_str());
#else
std::array<char, 16> posixName = {};
std::strncpy(posixName.data(), name.data(), 15);
::pthread_setname_np(thread.native_handle(), posixName.data());
#endif
}
inline void setThreadPriority(std::thread& thread, ThreadPriority priority) noexcept {
#ifdef _WIN32
::SetThreadPriority(thread.native_handle(), static_cast<int32_t>(priority));
#else
::sched_param param = {};
int32_t policy;
switch (priority) {
case ThreadPriority::Highest:
policy = SCHED_FIFO;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
break;
case ThreadPriority::AboveNormal:
policy = SCHED_FIFO;
param.sched_priority = sched_get_priority_max(SCHED_FIFO) / 2;
break;
default:
case ThreadPriority::Normal:
policy = SCHED_OTHER;
break;
case ThreadPriority::BelowNormal:
policy = SCHED_BATCH;
break;
case ThreadPriority::Lowest:
policy = SCHED_IDLE;
break;
}
::pthread_setschedparam(thread.native_handle(), policy, ¶m);
#endif
}
inline void pinThread(std::thread& thread, uint32_t index) noexcept {
#ifdef _WIN32
DWORD mask = 1 << index;
::SetThreadAffinityMask(thread.native_handle(), mask);
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(index, &cpuset);
::pthread_setaffinity_np(thread.native_handle(), sizeof(cpu_set_t), &cpuset);
#endif
}
inline uint32_t threadCount() noexcept {
return std::thread::hardware_concurrency();
}
}
using platform::ThreadPriority;
template <typename T, size_t Capacity>
class RingBuffer {
public:
inline void push_back(const T& item) noexcept {
while (!try_push_back(item))
platform::yield();
}
inline bool try_push_back(const T& item) noexcept {
std::unique_lock lock(m_mutex);
if (full())
return false;
m_data[m_head] = item;
m_head = next(m_head);
return true;
}
inline bool pop_front(T& item) noexcept {
std::unique_lock lock(m_mutex);
if (empty())
return false;
item = m_data[m_tail];
m_tail = next(m_tail);
return true;
}
constexpr size_t capacity() const noexcept { return Capacity; }
private:
// Not thread safe, internally used.
[[nodiscard]] constexpr bool full() const noexcept { return next(m_head) == m_tail; }
[[nodiscard]] constexpr bool empty() const noexcept { return m_tail == m_head; }
[[nodiscard]] constexpr size_t next(size_t index) const noexcept { return (index + 1) % Capacity; }
std::array<T, Capacity> m_data = {};
size_t m_head = 0;
size_t m_tail = 0;
std::mutex m_mutex;
};
struct JobContext {
uint32_t localInvocationIndex; // The job index relative to the group, Akin to gl_LocalInvocationIndex, SV_GroupIndex.
uint32_t localInvocationCount; // The number of invocations for this group.
uint32_t globalInvocationIndex; // The job index, akin to gl_GlobalInvocationID, SV_DispatchThreadID.
uint32_t workgroupId; // The group index relative to the global invocation, akin to gl_WorkGroupID, SV_GroupID.
void* sharedMemory;
constexpr bool isFirstJobInGroup() const noexcept { return localInvocationIndex == 0; }
constexpr bool isLastJobInGroup() const noexcept { return localInvocationIndex == localInvocationCount - 1; }
};
using JobFunction = std::function<bool(JobContext&&)>;
class JobWatcher {
public:
[[nodiscard]]
inline bool busy() const noexcept { return m_counter.load() > 0; }
inline void wait() const noexcept { while(busy()) platform::yield(); }
protected:
template <size_t MaxJobCount> friend class JobRunner;
inline void newJob() noexcept { m_counter.fetch_add(1, std::memory_order_acquire); }
inline void jobComplete() noexcept { m_counter.fetch_sub(1, std::memory_order_release); }
private:
std::atomic<uint32_t> m_counter = { 0 };
};
template <size_t MaxJobCount>
class JobRunner {
public:
JobRunner(
uint32_t maxThreads = std::numeric_limits<uint32_t>::max(),
std::string_view threadPrefix = "libjobs",
ThreadPriority priority = ThreadPriority::Normal,
bool pinToCore = false) {
uint32_t numThreads = std::min(platform::threadCount(), maxThreads);
for (uint32_t i = 0; i < numThreads; i++) {
auto thread = std::thread([this, &mutex = m_wakeMutex, &condition = m_wakeCondition]() noexcept {
for (;;) {
WorkerState state = work();
if (state == WorkerState::Work) {
continue;
} else if (state == WorkerState::Sleep) {
auto lock = std::unique_lock(mutex);
condition.wait(lock);
continue;
} else if (state == WorkerState::Terminate) {
break;
}
}
});
std::string threadName;
{
std::stringstream ss;
ss << threadPrefix << "_" << i;
threadName = ss.str();
}
platform::setThreadName(thread, threadName);
platform::setThreadPriority(thread, priority);
if (pinToCore)
platform::pinThread(thread, i);
m_threads.emplace_back(std::move(thread));
}
}
~JobRunner() {
// As each thread dies, it won't be able to take another terminate job.
JobWatcher terminateWatcher;
dispatch(terminateWatcher, [](JobContext&& ctx) { return false; }, threadCount(), 1);
terminateWatcher.wait();
for (auto& thread : m_threads)
thread.join();
}
inline void execute(JobWatcher& watcher, JobFunction function) noexcept {
watcher.newJob();
JobDesc desc = {
.watcher = &watcher,
.function = function,
.workgroupId = 0,
.localInvocationOffset = 0,
.localInvocationCount = 1,
.sharedMemorySize = 0
};
m_queue.push_back(desc);
m_wakeCondition.notify_one();
}
inline void execute(JobFunction function) noexcept {
execute(m_throwawayWatcher, function);
}
inline void dispatch(JobWatcher& watcher, JobFunction function, uint32_t jobCount, uint32_t groupSize, uint32_t sharedMemorySize = 0) noexcept {
if (jobCount == 0 || groupSize == 0)
return;
uint32_t groupCount = (jobCount + groupSize - 1) / groupSize;
for (uint32_t i = 0; i < groupCount; i++) {
watcher.newJob();
JobDesc desc = {
.watcher = &watcher,
.function = function,
.workgroupId = i,
.localInvocationOffset = i * groupSize,
.localInvocationCount = std::min(groupSize, jobCount),
.sharedMemorySize = sharedMemorySize
};
jobCount -= groupSize;
m_queue.push_back(desc);
}
m_wakeCondition.notify_all();
}
inline void dispatch(JobFunction function, uint32_t jobCount, uint32_t groupSize, uint32_t sharedMemorySize = 0) noexcept {
dispatch(m_throwawayWatcher, function, jobCount, groupSize, sharedMemorySize);
}
inline uint32_t threadCount() const noexcept { return uint32_t(m_threads.size()); }
inline void waitAndWork(JobWatcher& watcher) noexcept {
while (watcher.busy()) {
if (work() != WorkerState::Work)
platform::yield();
}
}
inline void synchronise() noexcept {
std::atomic<uint32_t> counter = { 0 };
dispatch([&counter](JobContext&& ctx) {
counter++;
return true;
}, threadCount(), 1);
while (counter != threadCount())
platform::yield();
}
inline void barrier() noexcept {
auto counter = std::make_shared<std::atomic<uint32_t>>(0);
dispatch([counter, threadCount = threadCount()](JobContext&& ctx) {
++*counter;
while (*counter != threadCount)
platform::yield();
return true;
}, threadCount(), 1);
}
private:
struct JobDesc {
JobWatcher* watcher;
JobFunction function;
uint32_t workgroupId;
uint32_t localInvocationOffset;
uint32_t localInvocationCount;
uint32_t sharedMemorySize;
};
enum class WorkerState {
Work,
Sleep,
Terminate
};
[[nodiscard]]
inline WorkerState work() noexcept {
JobDesc desc;
if (!m_queue.pop_front(desc))
return WorkerState::Sleep;
void* sharedMemory = libjobs_alloca(desc.sharedMemorySize);
for (uint32_t i = 0; i < desc.localInvocationCount; i++) {
JobContext ctx = {
.localInvocationIndex = i,
.localInvocationCount = desc.localInvocationCount,
.globalInvocationIndex = i + desc.localInvocationOffset,
.workgroupId = desc.workgroupId,
.sharedMemory = sharedMemory
};
bool shouldTerminate = !desc.function(std::move(ctx));
if (shouldTerminate) {
desc.watcher->jobComplete();
return WorkerState::Terminate;
}
}
desc.watcher->jobComplete();
return WorkerState::Work;
}
std::vector<std::thread> m_threads;
std::mutex m_wakeMutex;
std::condition_variable m_wakeCondition;
RingBuffer<JobDesc, MaxJobCount> m_queue;
JobWatcher m_throwawayWatcher;
};
}