forked from turanszkij/WickedEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiJobSystem.cpp
336 lines (289 loc) · 9.59 KB
/
wiJobSystem.cpp
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
#include "wiJobSystem.h"
#include "wiSpinLock.h"
#include "wiBacklog.h"
#include "wiPlatform.h"
#include "wiTimer.h"
#include <memory>
#include <algorithm>
#include <deque>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#ifdef PLATFORM_LINUX
#include <pthread.h>
#endif // PLATFORM_LINUX
#ifdef PLATFORM_PS5
#include "wiJobSystem_PS5.h"
#endif // PLATFORM_PS5
namespace wi::jobsystem
{
struct Job
{
std::function<void(JobArgs)> task;
context* ctx;
uint32_t groupID;
uint32_t groupJobOffset;
uint32_t groupJobEnd;
uint32_t sharedmemory_size;
};
struct JobQueue
{
std::deque<Job> queue;
std::mutex locker;
inline void push_back(const Job& item)
{
std::scoped_lock lock(locker);
queue.push_back(item);
}
inline bool pop_front(Job& item)
{
std::scoped_lock lock(locker);
if (queue.empty())
{
return false;
}
item = std::move(queue.front());
queue.pop_front();
return true;
}
};
// This structure is responsible to stop worker thread loops.
// Once this is destroyed, worker threads will be woken up and end their loops.
struct InternalState
{
uint32_t numCores = 0;
uint32_t numThreads = 0;
std::unique_ptr<JobQueue[]> jobQueuePerThread[int(Priority::Count)];
std::atomic_bool alive{ true };
std::condition_variable wakeCondition;
std::mutex wakeMutex;
std::atomic<uint32_t> nextQueue{ 0 };
wi::vector<std::thread> threads[int(Priority::Count)];
void ShutDown()
{
alive.store(false); // indicate that new jobs cannot be started from this point
bool wake_loop = true;
std::thread waker([&] {
while (wake_loop)
{
wakeCondition.notify_all(); // wakes up sleeping worker threads
}
});
for (auto& thread : threads[int(Priority::High)])
{
thread.join();
}
for (auto& thread : threads[int(Priority::Low)])
{
thread.join();
}
wake_loop = false;
waker.join();
jobQueuePerThread[int(Priority::High)].reset();
jobQueuePerThread[int(Priority::Low)].reset();
threads[int(Priority::High)].clear();
threads[int(Priority::Low)].clear();
numCores = 0;
numThreads = 0;
}
~InternalState()
{
ShutDown();
}
} static internal_state;
// Start working on a job queue
// After the job queue is finished, it can switch to an other queue and steal jobs from there
inline void work(uint32_t startingQueue, Priority priority)
{
Job job;
for (uint32_t i = 0; i < internal_state.numThreads; ++i)
{
JobQueue& job_queue = internal_state.jobQueuePerThread[int(priority)][startingQueue % internal_state.numThreads];
while (job_queue.pop_front(job))
{
JobArgs args;
args.groupID = job.groupID;
if (job.sharedmemory_size > 0)
{
thread_local static wi::vector<uint8_t> shared_allocation_data;
shared_allocation_data.reserve(job.sharedmemory_size);
args.sharedmemory = shared_allocation_data.data();
}
else
{
args.sharedmemory = nullptr;
}
for (uint32_t j = job.groupJobOffset; j < job.groupJobEnd; ++j)
{
args.jobIndex = j;
args.groupIndex = j - job.groupJobOffset;
args.isFirstJobInGroup = (j == job.groupJobOffset);
args.isLastJobInGroup = (j == job.groupJobEnd - 1);
job.task(args);
}
job.ctx->counter.fetch_sub(1);
}
startingQueue++; // go to next queue
}
}
void Initialize(uint32_t maxThreadCount)
{
if (internal_state.numThreads > 0)
return;
maxThreadCount = std::max(1u, maxThreadCount);
wi::Timer timer;
// Retrieve the number of hardware threads in this system:
internal_state.numCores = std::thread::hardware_concurrency();
// Calculate the actual number of worker threads we want (-1 main thread):
internal_state.numThreads = std::min(maxThreadCount, std::max(1u, internal_state.numCores - 1));
internal_state.jobQueuePerThread[int(Priority::High)].reset(new JobQueue[internal_state.numThreads]);
internal_state.jobQueuePerThread[int(Priority::Low)].reset(new JobQueue[internal_state.numThreads]);
internal_state.threads[int(Priority::High)].reserve(internal_state.numThreads);
internal_state.threads[int(Priority::Low)].reserve(internal_state.numThreads);
for (int prio = 0; prio < int(Priority::Count); ++prio)
{
const Priority priority = (Priority)prio;
for (uint32_t threadID = 0; threadID < internal_state.numThreads; ++threadID)
{
internal_state.threads[prio].emplace_back([threadID, priority] {
while (internal_state.alive.load())
{
work(threadID, priority);
// finished with jobs, put to sleep
std::unique_lock<std::mutex> lock(internal_state.wakeMutex);
internal_state.wakeCondition.wait(lock);
}
});
std::thread& worker = internal_state.threads[prio].back();
#ifdef _WIN32
// Do Windows-specific thread setup:
HANDLE handle = (HANDLE)worker.native_handle();
// Put each thread on to dedicated core:
DWORD_PTR affinityMask = 1ull << threadID;
DWORD_PTR affinity_result = SetThreadAffinityMask(handle, affinityMask);
assert(affinity_result > 0);
if (priority == Priority::High)
{
BOOL priority_result = SetThreadPriority(handle, THREAD_PRIORITY_NORMAL);
assert(priority_result != 0);
std::wstring wthreadname = L"wi::jobsystem_" + std::to_wstring(threadID);
HRESULT hr = SetThreadDescription(handle, wthreadname.c_str());
assert(SUCCEEDED(hr));
}
else
{
BOOL priority_result = SetThreadPriority(handle, THREAD_PRIORITY_LOWEST);
assert(priority_result != 0);
std::wstring wthreadname = L"wi::jobsystem_low_" + std::to_wstring(threadID);
HRESULT hr = SetThreadDescription(handle, wthreadname.c_str());
assert(SUCCEEDED(hr));
}
#elif defined(PLATFORM_LINUX)
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); } while (0)
int ret;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
size_t cpusetsize = sizeof(cpuset);
CPU_SET(threadID, &cpuset);
ret = pthread_setaffinity_np(worker.native_handle(), cpusetsize, &cpuset);
if (ret != 0)
handle_error_en(ret, std::string(" pthread_setaffinity_np[" + std::to_string(threadID) + ']').c_str());
if (priority == Priority::High)
{
std::string thread_name = "wi::job_" + std::to_string(threadID);
ret = pthread_setname_np(worker.native_handle(), thread_name.c_str());
if (ret != 0)
handle_error_en(ret, std::string(" pthread_setname_np[" + std::to_string(threadID) + ']').c_str());
}
else
{
// TODO: set lower priority
std::string thread_name = "wi::job_low_" + std::to_string(threadID);
ret = pthread_setname_np(worker.native_handle(), thread_name.c_str());
if (ret != 0)
handle_error_en(ret, std::string(" pthread_setname_np[" + std::to_string(threadID) + ']').c_str());
}
#undef handle_error_en
#elif defined(PLATFORM_PS5)
wi::jobsystem::ps5::SetupWorker(worker, threadID);
#endif // _WIN32
}
}
wi::backlog::post("wi::jobsystem Initialized with [" + std::to_string(internal_state.numCores) + " cores] [" + std::to_string(internal_state.numThreads) + " threads] (" + std::to_string((int)std::round(timer.elapsed())) + " ms)");
}
void ShutDown()
{
internal_state.ShutDown();
}
uint32_t GetThreadCount()
{
return internal_state.numThreads;
}
void Execute(context& ctx, const std::function<void(JobArgs)>& task)
{
// Context state is updated:
ctx.counter.fetch_add(1);
Job job;
job.ctx = &ctx;
job.task = task;
job.groupID = 0;
job.groupJobOffset = 0;
job.groupJobEnd = 1;
job.sharedmemory_size = 0;
internal_state.jobQueuePerThread[int(ctx.priority)][internal_state.nextQueue.fetch_add(1) % internal_state.numThreads].push_back(job);
internal_state.wakeCondition.notify_one();
}
void Dispatch(context& ctx, uint32_t jobCount, uint32_t groupSize, const std::function<void(JobArgs)>& task, size_t sharedmemory_size)
{
if (jobCount == 0 || groupSize == 0)
{
return;
}
const uint32_t groupCount = DispatchGroupCount(jobCount, groupSize);
// Context state is updated:
ctx.counter.fetch_add(groupCount);
Job job;
job.ctx = &ctx;
job.task = task;
job.sharedmemory_size = (uint32_t)sharedmemory_size;
for (uint32_t groupID = 0; groupID < groupCount; ++groupID)
{
// For each group, generate one real job:
job.groupID = groupID;
job.groupJobOffset = groupID * groupSize;
job.groupJobEnd = std::min(job.groupJobOffset + groupSize, jobCount);
internal_state.jobQueuePerThread[int(ctx.priority)][internal_state.nextQueue.fetch_add(1) % internal_state.numThreads].push_back(job);
}
internal_state.wakeCondition.notify_all();
}
uint32_t DispatchGroupCount(uint32_t jobCount, uint32_t groupSize)
{
// Calculate the amount of job groups to dispatch (overestimate, or "ceil"):
return (jobCount + groupSize - 1) / groupSize;
}
bool IsBusy(const context& ctx)
{
// Whenever the context label is greater than zero, it means that there is still work that needs to be done
return ctx.counter.load() > 0;
}
void Wait(const context& ctx)
{
if (IsBusy(ctx))
{
// Wake any threads that might be sleeping:
internal_state.wakeCondition.notify_all();
// work() will pick up any jobs that are on stand by and execute them on this thread:
work(internal_state.nextQueue.fetch_add(1) % internal_state.numThreads, ctx.priority);
while (IsBusy(ctx))
{
// If we are here, then there are still remaining jobs that work() couldn't pick up.
// In this case those jobs are not standing by on a queue but currently executing
// on other threads, so they cannot be picked up by this thread.
// Allow to swap out this thread by OS to not spin endlessly for nothing
std::this_thread::yield();
}
}
}
}