/
ThreadPool.h
374 lines (332 loc) · 12.5 KB
/
ThreadPool.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
364
365
366
367
368
369
370
371
372
373
374
#pragma once
#include <functional>
#include <thread>
#include <queue>
#include <mutex>
#include <tuple>
#include <vector>
#include <iostream>
#include <cmath>
#include <future>
#include <array>
#include <cassert>
#include "CPUUtil.h"
/*
* Thread pool that respects cache locality on HyperThreaded CPUs (WIN32 API dependent)
*
* Each job is described as an array of N functions. (ideal N=2 for HT)
* For each job, N threads are created and assigned respective functions.
* For a given job, all threads are guaranteed to be on the same physical core.
* No two threads from different jobs are allowed on the same physical core.
*
*
* Why?
* When doing multithreading on cache sensitive tasks,
* we want to keep threads that operate on same or contiguous memory region
* on the same physical core s.t they share the same L2 cache.
*
* Reference: This code is influenced by writeup that explains thread pools at
* https://github.com/mtrebi/thread-pool/blob/master/README.md
*
* Structure:
* CPUUtil:
* Uses Windows API to detect the number of physical cores, cache sizes
* and mapping between physical and logical processors.
*
* HWLocalThreadPool:
* Submission:
* initializer list or vector of (void function (void)) of length N
* where N is the num of threads that will spawn on the same core,
* and, the length of the std::function array.
* ith thread handles repective ith function
*
* Core Handlers:
* We create NumHWCores many CoreHandler objects.
* These objects are responsible for managing their cores.
* They check the main pool for jobs, when a job is found,
* if N==1 , they call the only function in the job description.
* if N>1 , they assign N-1 threads on the same physical core to,
* respective functions in the array. The CoreHandler is
* assigned to the first function.
* Once CoreHandler finishes its own task, it waits for other threads,
* Then its available for new jobs, waiting to be notified by the pool manager.
*
* Thread Handlers:
* Responsible for handling tasks handed away by the CoreHandler.
* When they finish execution, they signal to notify CoreHandler
* Then, they wait for a new task to run until they are terminated.
*
* Notes:
*
* DON'T KEEP THESE TASKS TOO SMALL.
* We don't want our CoreHandler to check its childrens states constantly,
* So, when a thread finishes a task, we signal the CoreHandler.
* This might become a overhead if the task itself is trivial.
* In that case you probably shouldn't be using this structure anyways,
* But if you want to, you can change it so that,
* CoreHandler periodically checks m_childThreadOnline array and sleeps in between.
*
*/
class HWLocalThreadPool {
public:
HWLocalThreadPool(int _numOfCoresToUse, int _numThreadsPerCore) : m_terminate(false)
{
m_numHWCores = CPUUtil::GetNumHWCores();
if (_numOfCoresToUse <= 0) {
m_numCoreHandlers = m_numHWCores;
} else {
m_numCoreHandlers = _numOfCoresToUse;
}
if (_numThreadsPerCore <= 0) {
m_numThreadsPerCore =
CPUUtil::GetNumLogicalProcessors() / m_numCoreHandlers;
} else {
m_numThreadsPerCore = _numThreadsPerCore;
}
/* malloc m_coreHandlers s.t no default initialization takes place,
we construct every object with placement new */
m_coreHandlers = (CoreHandler*)malloc(m_numCoreHandlers * sizeof(CoreHandler));
m_coreHandlerThreads = new std::thread[m_numCoreHandlers];
for (int i = 0; i < m_numCoreHandlers; ++i) {
ULONG_PTR processAffinityMask;
int maskQueryRetCode = CPUUtil::GetProcessorMask(i, processAffinityMask);
if (maskQueryRetCode) {
assert(0, "Can't query processor relations.");
return;
}
CoreHandler* coreHandler =
new (&m_coreHandlers[i]) CoreHandler(this, i, processAffinityMask);
m_coreHandlerThreads[i] = std::thread(std::ref(m_coreHandlers[i]));
}
}
~HWLocalThreadPool()
{
if (!m_terminate)
Close();
}
void Add(std::vector<std::function<void()>> const& F)
{
m_queue.Push(F);
m_queueToCoreNotifier.notify_one();
}
/* if finishQueue is set, cores will termianate after handling every job at the queue
if not, they will finish the current job they have and terminate. */
void Close(const bool finishQueue = true)
{
{
std::unique_lock<std::mutex> lock(m_queueMutex);
m_terminate = 1;
m_waitToFinish = finishQueue;
m_queueToCoreNotifier.notify_all();
}
for (int i = 0; i < m_numCoreHandlers; ++i) {
if (m_coreHandlerThreads[i].joinable())
m_coreHandlerThreads[i].join();
}
/* free doesn't call the destructor, so */
for (int i = 0; i < m_numCoreHandlers; ++i) {
m_coreHandlers[i].~CoreHandler();
}
free(m_coreHandlers);
delete[] m_coreHandlerThreads;
}
const unsigned NumCores()
{
return m_numHWCores;
}
const unsigned NumThreadsPerCore()
{
return m_numThreadsPerCore;
}
template <typename F, typename... Args>
static std::function<void()> WrapFunc(F&& f, Args&&... args)
{
std::function<decltype(f(args...))()> func =
std::bind(std::forward<F>(f), std::forward<Args>(args)...);
auto task_ptr =
std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
std::function<void()> wrapper_func = [task_ptr]() { (*task_ptr)(); };
return wrapper_func;
}
protected:
template <typename T> class Queue {
public:
Queue()
{
}
~Queue()
{
}
void Push(T const& element)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_queue.push(std::move(element));
}
bool Pop(T& function)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_queue.empty()) {
function = std::move(m_queue.front());
m_queue.pop();
return true;
}
return false;
}
int Size()
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_queue.size();
}
private:
std::queue<T> m_queue;
std::mutex m_mutex;
};
class CoreHandler {
public:
CoreHandler(HWLocalThreadPool* const _parent, const unsigned _id,
const ULONG_PTR& _processorMask)
: m_parent(_parent), m_id(_id), m_processorAffinityMask(_processorMask),
m_terminate(false), m_numChildThreads(_parent->m_numThreadsPerCore - 1)
{
if (m_numChildThreads > 0) {
m_childThreads = new std::thread[m_numChildThreads];
m_childThreadOnline = new bool[m_numChildThreads];
std::unique_lock<std::mutex> lock(m_threadMutex);
for (int i = 0; i < m_numChildThreads; ++i) {
m_childThreadOnline[i] = 0;
m_childThreads[i] =
std::thread(ThreadHandler(this, i, m_processorAffinityMask));
}
}
}
void WaitForChildThreads()
{
if (!m_childThreads || m_numChildThreads < 1)
return;
std::unique_lock<std::mutex> lock(m_threadMutex);
bool anyOnline = 1;
while (anyOnline) {
anyOnline = 0;
for (int i = 0; i < m_numChildThreads; ++i) {
anyOnline |= m_childThreadOnline[i];
}
if (anyOnline) {
m_threadToCoreNotifier.wait(lock);
}
}
}
void CloseChildThreads()
{
if (m_terminate || m_numChildThreads < 1)
return;
{
std::unique_lock<std::mutex> lock(m_threadMutex);
m_terminate = 1;
m_coreToThreadNotifier.notify_all();
}
/* Core closing threads */
for (int i = 0; i < m_numChildThreads; ++i) {
if (m_childThreads[i].joinable()) {
m_childThreads[i].join();
}
}
delete[] m_childThreads;
delete[] m_childThreadOnline;
}
void operator()()
{
SetThreadAffinityMask(GetCurrentThread(), m_processorAffinityMask);
bool dequeued;
while (1) {
{
std::unique_lock<std::mutex> lock(m_parent->m_queueMutex);
if (m_parent->m_terminate &&
!(m_parent->m_waitToFinish && m_parent->m_queue.Size() > 0)) {
break;
}
if (m_parent->m_queue.Size() == 0) {
m_parent->m_queueToCoreNotifier.wait(lock);
}
dequeued = m_parent->m_queue.Pop(m_job);
}
if (dequeued) {
m_ownJob = std::move(m_job[0]);
if (m_numChildThreads < 1) {
m_ownJob();
} else {
{
std::unique_lock<std::mutex> lock(m_threadMutex);
for (int i = 0; i < m_numChildThreads; ++i) {
m_childThreadOnline[i] = 1;
}
m_coreToThreadNotifier.notify_all();
}
m_ownJob();
WaitForChildThreads();
}
}
}
CloseChildThreads();
}
class ThreadHandler {
public:
ThreadHandler(CoreHandler* _parent, const unsigned _id,
const ULONG_PTR& _processorAffinityMask)
: m_parent(_parent), m_processorAffinityMask(_processorAffinityMask),
m_id(_id), m_jobSlot(_id + 1)
{
}
void operator()()
{
SetThreadAffinityMask(GetCurrentThread(), m_processorAffinityMask);
while (1) {
{
std::unique_lock<std::mutex> lock(m_parent->m_threadMutex);
if (m_parent->m_terminate)
break;
if (!m_parent->m_childThreadOnline[m_id]) {
m_parent->m_coreToThreadNotifier.wait(lock);
}
}
bool online = 0;
{
std::unique_lock<std::mutex> lock(m_parent->m_threadMutex);
online = m_parent->m_childThreadOnline[m_id];
}
if (online) {
func = std::move(m_parent->m_job[m_jobSlot]);
func();
std::unique_lock<std::mutex> lock(m_parent->m_threadMutex);
m_parent->m_childThreadOnline[m_id] = 0;
m_parent->m_threadToCoreNotifier.notify_one();
}
}
}
const unsigned m_id;
const unsigned m_jobSlot;
CoreHandler* m_parent;
ULONG_PTR m_processorAffinityMask;
std::function<void()> func;
};
const unsigned m_id;
HWLocalThreadPool* const m_parent;
const ULONG_PTR m_processorAffinityMask;
const unsigned m_numChildThreads;
std::thread* m_childThreads;
bool* m_childThreadOnline;
bool m_terminate;
std::vector<std::function<void()>> m_job;
std::function<void()> m_ownJob;
std::mutex m_threadMutex;
std::condition_variable m_coreToThreadNotifier;
std::condition_variable m_threadToCoreNotifier;
};
private:
unsigned m_numHWCores, m_numCoreHandlers, m_numThreadsPerCore;
CoreHandler* m_coreHandlers;
std::thread* m_coreHandlerThreads;
Queue<std::vector<std::function<void()>>> m_queue;
bool m_terminate, m_waitToFinish;
std::mutex m_queueMutex;
std::condition_variable m_queueToCoreNotifier;
};