-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeslaJobSystem.cpp
More file actions
344 lines (302 loc) · 8.3 KB
/
Copy pathTeslaJobSystem.cpp
File metadata and controls
344 lines (302 loc) · 8.3 KB
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
// WARNING:
// This C++ unit was built by the applicant (Zihong Ye) and
// will be provided to Codility for the audition.
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <chrono>
#include <immintrin.h>
#include <string_view>
#include <optional>
#include <algorithm>
// 1. Mutexes
// std::mutex std::timed_mutex std::recursive_mutex
//
// 2. Lock Mgrs
// std::lock_guard std::scoped_lock std::unique_lock std::shared_lock
// std::lock(...)
//
// 3. Atomics
// std::atomic<T> load, store, fetch_add, fetch_sub
//
// 4. CV
// std::condition_variable (with std::unique_ptr)
// wait(lock, []{ return ready; }), notify_one, notify_all
namespace TeslaMT
{
int32_t WorkerThreadCount = 4; // Can change it when start the application
constexpr int32_t CacheLineSize = 64;
constexpr uint32_t ArraySize = 32;
using Job = std::function<void()>;
using Action = Job;
using Worker = void (*)(const std::atomic<bool>& stop_token, const int32_t threadIdx);
struct GenConditionVar
{
std::mutex mute;
std::condition_variable cvar;
uint64_t generation = 0;
void Wait(uint64_t old_gen)
{
std::unique_lock lock(mute);
// Wait until the generation num is changed.
cvar.wait(lock, [this, old_gen] { return generation != old_gen; });
}
void Notify()
{
{
std::lock_guard<std::mutex> lock(mute);
generation++;
}
cvar.notify_all();
}
};
// Simulate Barrier in C++ 20.
class Barrier
{
private:
const int32_t WorkerCount;
const Action Completion;
std::mutex mute;
GenConditionVar barrierCV;
int32_t count = 0;
uint64_t currentGeneration = 0;
public:
Barrier(int32_t workerCount, Action completion) :
WorkerCount(workerCount),
Completion(completion)
{
count = workerCount;
}
void ArriveAndWait()
{
std::unique_lock lock(mute);
if (count < 1) return;
uint64_t currentGen = currentGeneration;
count--;
if (count == 0) // The last guest.
{
if (Completion) Completion();
count = WorkerCount;
currentGeneration++;
lock.unlock();
barrierCV.Notify();
}
else
{
lock.unlock(); // Unlock it earlier.
barrierCV.Wait(currentGen);
}
}
};
inline void Yield()
{
std::this_thread::yield();
}
inline void SpinLoopHint()
{
#if defined(_MSC_VER) && defined(_M_X64)
_mm_pause();
#elif defined(__clang__) && (defined(__aarch64__) || defined(__arm__))
__builtin_arm_yield();
#else
// Fake spin; it's NOT a spin.
Yield();
#endif
}
inline void RequestStop(std::atomic<bool>& stop_token)
{
stop_token.store(true, std::memory_order_release);
}
inline bool IsStopped(const std::atomic<bool>& stop_token)
{
return stop_token.load(std::memory_order_acquire);
}
}
namespace TeslaMT::JobSystem
{
struct Task
{
std::promise<bool> Result;
std::function<void()> Action;
};
class alignas(CacheLineSize) WorkStealingQueue // Eliminate fake sharing.
{
private:
// BACK(Enter) >>>>>> FRONT(Leave)
std::deque<Task> taskQueue;
std::mutex mute;
public:
WorkStealingQueue()
{
//...
}
bool IsEmpty()
{
std::lock_guard<std::mutex> lock(mute);
return taskQueue.empty();
}
std::future<bool> Push(std::function<void()> job)
{
if (!job)
{
std::promise<bool> dumyPromise;
dumyPromise.set_value(false);
return dumyPromise.get_future();
}
std::lock_guard<std::mutex> lock(mute);
taskQueue.emplace_back(Task{ std::promise<bool>{}, std::move(job) });
return taskQueue.back().Result.get_future();
}
bool PopOrSteal(Task& task, const int32_t threadIdx);
};
// Used by the main thread.
std::array<std::atomic<bool>, ArraySize> StopTokens{};
std::array<WorkStealingQueue, ArraySize> WSQueues{};
std::vector<std::thread> Workers{};
bool WorkStealingQueue::PopOrSteal(Task& task, const int32_t threadIdx)
{
std::unique_lock<std::mutex> lock(mute);
if (taskQueue.empty() == false) // Work
{
task = std::move(taskQueue.front());
taskQueue.pop_front();
return true;
}
else // Steal
{
// Don't access current queue now, unlock it.
lock.unlock();
for (int32_t i = 0; i < WorkerThreadCount; i++)
{
if (i == threadIdx) continue;
// Try stealing
{
WorkStealingQueue& victim = WSQueues[i];
std::lock_guard<std::mutex> lock2(victim.mute);
if (victim.taskQueue.empty()) continue;
task = std::move(victim.taskQueue.back());
victim.taskQueue.pop_back();
return true;
}
}
}
return false;
}
// Worker thread.
void Worker(const std::atomic<bool>& stop_token, const int32_t threadIdx)
{
WorkStealingQueue& queue = WSQueues[threadIdx];
Task task;
while (true)
{
// Simulate the worst situation to validate the MT design.
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (IsStopped(stop_token)) return;
if (queue.PopOrSteal(task, threadIdx)) // Get a job
{
// *** CORE WORKLOAD ***
try
{
task.Action();
task.Result.set_value(true);
}
catch (...)
{
//task.Result.set_exception(std::current_exception());
task.Result.set_value(false);
}
}
else // Delay
{
SpinLoopHint();
}
}
}
inline void Initialize(const int32_t threadCount)
{
WorkerThreadCount = threadCount;
// Activate workers.
for (int32_t i = 0; i < threadCount; i++)
{
Workers.emplace_back(Worker, std::ref(StopTokens[i]), i);
}
}
inline void Flush(std::future<bool>* futures, int32_t count)
{
for (int32_t i = 0; i < count; i++)
{
futures[i].get();
}
}
// Please flush queues before termination !!
inline void Terminate()
{
// Stop
for (int32_t i = 0; i < WorkerThreadCount; i++)
{
RequestStop(StopTokens[i]);
Workers[i].join();
}
}
}
using namespace TeslaMT;
namespace Demo
{
constexpr int32_t JobNum = 1000;
int32_t SimpleResult[JobNum]{};
std::future<bool> Futures[JobNum]{};
std::mutex mute;
bool ValidateResult()
{
std::lock_guard lock(mute);
for (int32_t i = 0; i < JobNum; i++)
{
if (SimpleResult[i] == 0) return false;
}
return true;
}
void OneJob(const int32_t jobIdx, const int32_t threadIdx)
{
int32_t integer = std::max(1, threadIdx + 1);
integer *= integer;
integer /= WorkerThreadCount;
integer = std::max(1, integer);
std::lock_guard lock(mute);
SimpleResult[jobIdx] = integer;
}
}
void mainTeslaJobSystem()
{
int32_t threadNum = 8;
std::cout << "Tesla Job System Demo" << std::endl;
std::cout << "THREAD_NUM = " << threadNum << std::endl;
std::cout << "JOB_NUM = " << Demo::JobNum << std::endl;
int32_t round = 0;
JobSystem::Initialize(threadNum);
while (true)
{
round++;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// 1. Reset
{
std::lock_guard lock(Demo::mute);
for (int32_t i = 0; i < Demo::JobNum; i++)
{
Demo::SimpleResult[i] = 0;
}
}
// 2. Push jobs
for (int32_t i = 0; i < Demo::JobNum; i++)
{
int32_t workerIdx = i % WorkerThreadCount;
Demo::Futures[i] = JobSystem::WSQueues[workerIdx].Push([=] { Demo::OneJob(i, workerIdx); });
}
// 3. Check
JobSystem::Flush(Demo::Futures, Demo::JobNum);
std::cout << "ROUND = " << round << ", RESULT = " << (Demo::ValidateResult() ? "True" : "False") << std::endl;
if (round == 10) break;
}
JobSystem::Terminate();
}