File: ThreadPool.h

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (387 lines) | stat: -rw-r--r-- 11,275 bytes parent folder | download
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
375
376
377
378
379
380
381
382
383
384
385
386
387

#ifndef _THREADPOOL_H
#define _THREADPOOL_H


#ifndef THREADPOOL

//#include <boost/thread/future.hpp>
#include  <functional>

namespace ThreadPool {
	template<class F, class... Args>
	static inline void enqueue(F&& f, Args&&... args)
	{
		f(args ...);
	}
	//-> std::shared_ptr<boost::unique_future<typename std::result_of<F(Args...)>::type>> {}

	static inline void SetThreadCount(int num) {}
	static inline void SetThreadSpinTime(int milliSeconds) {}
	static inline int GetThreadNum() { return 0; }
	static inline int GetMaxThreads() { return 1; }
	static inline int GetNumThreads() { return 1; }
	static inline void NotifyWorkerThreads() {}
	static inline bool HasThreads() { return false; }

	static constexpr int MAX_THREADS = 1;
}


static inline void for_mt(int start, int end, int step, const std::function<void(const int i)>&& f)
{
	for (int i = start; i < end; i += step) {
		f(i);
	}
}


static inline void for_mt(int start, int end, const std::function<void(const int i)>&& f)
{
	for_mt(start, end, 1, std::move(f));
}


static inline void parallel(const std::function<void()>&& f)
{
	f();
}


template<class F, class G>
static inline auto parallel_reduce(F&& f, G&& g) -> typename std::result_of<F()>::type
{
	return f();
}

#else

#include "TimeProfiler.h"
#include "System/Log/ILog.h"
#include "System/Platform/Threading.h"

#include <deque>
#include <vector>
#include <list>
#include <boost/optional.hpp>
#include <numeric>
#include <atomic>

// mingw is missing c++11 thread support atm, so for KISS always prefer boost atm
#include <boost/thread/future.hpp>
#undef gt
#include <boost/chrono/include.hpp>
#include <memory>

#ifdef UNITSYNC
	#undef SCOPED_MT_TIMER
	#define SCOPED_MT_TIMER(x)
#endif



class ITaskGroup
{
public:
	virtual ~ITaskGroup() {}

	virtual boost::optional<std::function<void()>> GetTask() = 0;
	virtual bool IsFinished() const = 0;
	virtual bool IsEmpty() const = 0;

	virtual int RemainingTasks() const = 0;

	template< class Rep, class Period >
	bool wait_for(const boost::chrono::duration<Rep, Period>& rel_time) const {
		const auto end = boost::chrono::high_resolution_clock::now() + rel_time;
		while (!IsFinished() && (boost::chrono::high_resolution_clock::now() < end)) {
		}
		return IsFinished();
	}
private:
	//virtual void FinishedATask() = 0;
};


namespace ThreadPool {
	template<class F, class... Args>
	static auto enqueue(F&& f, Args&&... args)
	-> std::shared_ptr<boost::unique_future<typename std::result_of<F(Args...)>::type>>;

	void PushTaskGroup(std::shared_ptr<ITaskGroup> taskgroup);
	void WaitForFinished(std::shared_ptr<ITaskGroup> taskgroup);

	template<typename T>
	inline void PushTaskGroup(std::shared_ptr<T> taskgroup) { PushTaskGroup(std::static_pointer_cast<ITaskGroup>(taskgroup)); }
	template<typename T>
	inline void WaitForFinished(std::shared_ptr<T> taskgroup) { WaitForFinished(std::static_pointer_cast<ITaskGroup>(taskgroup)); }

	void SetThreadCount(int num);
	void SetThreadSpinTime(int milliSeconds);
	int GetThreadNum();
	bool HasThreads();
	int GetMaxThreads();
	int GetNumThreads();
	void NotifyWorkerThreads();

	static constexpr int MAX_THREADS = 16;
}


template<class F, class... Args>
class SingleTask : public ITaskGroup
{
public:
	typedef typename std::result_of<F(Args...)>::type return_type;

	SingleTask(F&& f, Args&&... args) : finished(false), done(false) {
		auto p = std::make_shared<boost::packaged_task<return_type>>(
			std::bind(std::forward<F>(f), std::forward<Args>(args)...)
		);
		result = std::make_shared<boost::unique_future<return_type>>(p->get_future());
		task = [&,p]{ (*p)(); finished.store(true, std::memory_order_release); };
	}

	boost::optional<std::function<void()>> GetTask() { return (!done.exchange(true, std::memory_order_relaxed)) ? boost::optional<std::function<void()>>(task) : boost::optional<std::function<void()>>(); }

	bool IsEmpty() const    { return done.load(std::memory_order_relaxed); }
	bool IsFinished() const { return finished.load(std::memory_order_relaxed); }
	int RemainingTasks() const { return done ? 0 : 1; }
	std::shared_ptr<boost::unique_future<return_type>> GetFuture() { assert(result->valid()); return std::move(result); } //FIXME rethrow exceptions some time

private:
	//void FinishedATask() { finished = true; }

public:
	std::atomic<bool> finished;
	std::atomic<bool> done;
	std::function<void()> task;
	std::shared_ptr<boost::unique_future<return_type>> result;
};


template<class F, class... Args>
class TaskGroup : public ITaskGroup
{
public:
	TaskGroup(const int num = 0) : remainingTasks(0), curtask(0), latency(0) {
		//start = boost::chrono::high_resolution_clock::now();
		results.reserve(num);
		tasks.reserve(num);
	}

	virtual ~TaskGroup() {}

	typedef typename std::result_of<F(Args...)>::type return_type;

	void enqueue(F& f, Args&... args)
	{
		auto task = std::make_shared<boost::packaged_task<return_type>>(
			std::bind(f, args ...)
		);
		results.emplace_back(task->get_future());
		// workaround a Fedora gcc bug else it reports in the lambda below:
		// error: no 'operator--(int)' declared for postfix '--'
		auto* atomicCounter = &remainingTasks;
		tasks.emplace_back([task,atomicCounter]{ (*task)(); atomicCounter->fetch_sub(1, std::memory_order_release); });
		remainingTasks.fetch_add(1, std::memory_order_release);
	}

	void enqueue(F&& f, Args&&... args)
	{
		auto task = std::make_shared< boost::packaged_task<return_type> >(
			std::bind(std::forward<F>(f), std::forward<Args>(args)...)
		);
		results.emplace_back(task->get_future());
		// workaround a Fedora gcc bug else it reports in the lambda below:
		// error: no 'operator--(int)' declared for postfix '--'
		auto* atomicCounter = &remainingTasks;
		tasks.emplace_back([task,atomicCounter]{ (*task)(); atomicCounter->fetch_sub(1, std::memory_order_release); });
		remainingTasks.fetch_add(1, std::memory_order_release);
	}


	virtual boost::optional<std::function<void()>> GetTask()
	{
		const int pos = curtask.fetch_add(1, std::memory_order_relaxed);
		if (pos < tasks.size()) {
			/*if (latency.count() == 0) {
				auto now = boost::chrono::high_resolution_clock::now();
				latency = (now - start);
				LOG("latency %fms", latency.count() / 1000000.f);
			}*/
			return tasks[pos];
		}
		return boost::optional<std::function<void()>>();
	}

	virtual bool IsEmpty() const { return curtask.load(std::memory_order_relaxed) >= tasks.size(); }
	bool IsFinished() const      { return (remainingTasks.load(std::memory_order_relaxed) == 0); }
	int RemainingTasks() const   { return remainingTasks; }

	template<typename G>
	return_type GetResult(const G&& g) {
		return std::accumulate(results.begin(), results.end(), 0, g);
	}

private:
	//void FinishedATask() { remainingTasks--; }

public:
	std::atomic<int> remainingTasks;
	std::atomic<int> curtask;
	std::vector<std::function<void()>> tasks;
	std::vector<boost::unique_future<return_type>> results;

	boost::chrono::time_point<boost::chrono::high_resolution_clock> start; // use for latency profiling!
	boost::chrono::nanoseconds latency;
};


template<class F, class... Args>
class ParallelTaskGroup : public TaskGroup<F,Args...>
{
public:
	ParallelTaskGroup(const int num = 0) : TaskGroup<F,Args...>(num) {
		uniqueTasks.resize(ThreadPool::GetNumThreads());
	}

	typedef typename std::result_of<F(Args...)>::type return_type;

	void enqueue_unique(const int threadNum, F& f, Args&... args)
	{
		auto task = std::make_shared< boost::packaged_task<return_type> >(
			std::bind(std::forward<F>(f), std::forward<Args>(args)...)
		);
		this->results.emplace_back(task->get_future());
		uniqueTasks[threadNum].emplace_back([&,task]{ (*task)(); (this->remainingTasks)--; });
		this->remainingTasks++;
	}

	void enqueue_unique(const int threadNum, F&& f, Args&&... args)
	{
		auto task = std::make_shared< boost::packaged_task<return_type> >(
			std::bind(std::forward<F>(f), std::forward<Args>(args)...)
		);
		this->results.emplace_back(task->get_future());
		uniqueTasks[threadNum].emplace_back([&,task]{ (*task)(); (this->remainingTasks)--; });
		this->remainingTasks++;
	}


	boost::optional<std::function<void()>> GetTask()
	{
		auto& ut = uniqueTasks[ThreadPool::GetThreadNum()];
		if (!ut.empty()) {
			// no need to make threadsafe cause each thread got its own container
			auto t = ut.front();
			ut.pop_front();
			return t;
		}

		return TaskGroup<F,Args...>::GetTask();
	}

	bool IsEmpty() const {
		for(auto& ut: uniqueTasks) { if (!ut.empty()) return false; }
		return TaskGroup<F,Args...>::IsEmpty();
	}

public:
	std::vector<std::deque<std::function<void()>>> uniqueTasks;
};



static inline void for_mt(int start, int end, int step, const std::function<void(const int i)>&& f)
{
	if (end <= start)
		return;

	const bool singleIteration = (end - start) < step;

	// do not use HasThreads because that counts main as a worker
	if (!ThreadPool::HasThreads() || singleIteration) {
		for (int i = start; i < end; i += step) {
			f(i);
		}
		return;
	}

	ThreadPool::NotifyWorkerThreads();
	SCOPED_MT_TIMER("::ThreadWorkers (real)");
	auto taskgroup = std::make_shared<TaskGroup<const std::function<void(const int)>, const int>>((end-start)/step);
	for (int i = start; i < end; i += step) { //FIXME optimize worksize (group tasks in bigger ones than 1-steps)
		taskgroup->enqueue(f, i);
	}
	ThreadPool::PushTaskGroup(taskgroup);
	ThreadPool::WaitForFinished(taskgroup);
}


static inline void for_mt(int start, int end, const std::function<void(const int i)>&& f)
{
	for_mt(start, end, 1, std::move(f));
}


static inline void parallel(const std::function<void()>&& f)
{
	if (!ThreadPool::HasThreads())
		return f();

	ThreadPool::NotifyWorkerThreads();
	SCOPED_MT_TIMER("::ThreadWorkers (real)");

	auto taskgroup = std::make_shared<ParallelTaskGroup<const std::function<void()>>>();
	for (int i = 0; i < ThreadPool::GetNumThreads(); ++i) {
		taskgroup->enqueue_unique(i, f);
	}
	ThreadPool::PushTaskGroup(taskgroup);
	ThreadPool::WaitForFinished(taskgroup);
}


template<class F, class G>
static inline auto parallel_reduce(F&& f, G&& g) -> typename std::result_of<F()>::type
{
	if (!ThreadPool::HasThreads())
		return f();

	ThreadPool::NotifyWorkerThreads();
	SCOPED_MT_TIMER("::ThreadWorkers (real)");

	auto taskgroup = std::make_shared<ParallelTaskGroup<F>>();
	for (int i = 0; i < ThreadPool::GetNumThreads(); ++i) {
		taskgroup->enqueue_unique(i, f);
	}

	ThreadPool::PushTaskGroup(taskgroup);
	ThreadPool::WaitForFinished(taskgroup);
	return taskgroup->GetResult(std::move(g));
}


namespace ThreadPool {
	template<class F, class... Args>
	static inline auto enqueue(F&& f, Args&&... args)
	-> std::shared_ptr<boost::unique_future<typename std::result_of<F(Args...)>::type>>
	{
		typedef typename std::result_of<F(Args...)>::type return_type;

		if (!ThreadPool::HasThreads()) {
			// directly process when there are no worker threads
			auto task = std::make_shared< boost::packaged_task<return_type> >(std::bind(f, args ...));
			auto fut = std::make_shared<boost::unique_future<return_type>>(task->get_future());
			(*task)();
			return fut;
		}

		auto singletask = std::make_shared<SingleTask<F, Args...>>(std::forward<F>(f), std::forward<Args>(args)...);
		ThreadPool::PushTaskGroup(singletask);
		return singletask->GetFuture();
	}
}

#endif
#endif