forked from turanszkij/WickedEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiJobSystem.cpp
187 lines (157 loc) · 4.52 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
#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>
#include <oneapi/tbb/task_arena.h>
#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;
};
// 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<oneapi::tbb::task_arena> arena;
void ShutDown()
{
if (arena) {
if (arena->is_active()) {
arena->terminate();
}
}
}
~InternalState()
{
ShutDown();
}
} static internal_state;
context::~context() noexcept {
if (IsBusy(*this)) {
wi::backlog::post_backlog("wi::jobsystem::context destruction of busy context", wi::backlog::LogLevel::Fatal);
}
Wait(*this);
}
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();
uint32_t default_concurrency = (uint32_t) tbb::info::default_concurrency();
internal_state.numThreads = std::min(maxThreadCount, std::max(1u, default_concurrency));
// Calculate the actual number of worker threads we want (-1 main thread):
internal_state.arena.reset(new oneapi::tbb::task_arena(internal_state.numThreads));
internal_state.arena->initialize();
wi::backlog::post_backlog("wi::jobsystem Initialized with [" + std::to_string(internal_state.numCores) + " cores] [" + std::to_string(internal_state.numThreads) + " max concurrency] (" + std::to_string((int)std::round(timer.elapsed())) + " ms)");
}
void ShutDown()
{
internal_state.ShutDown();
}
uint32_t GetThreadCount()
{
return internal_state.numThreads;
}
static void Execute(Job &job) {
internal_state.arena->execute([job] {
job.ctx->group.run([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);
});
});
}
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;
Execute(job);
}
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);
Execute(job);
}
}
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))
const_cast<oneapi::tbb::task_group&>(ctx.group).wait();
}
}