-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.h
90 lines (73 loc) · 2.63 KB
/
parallel.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
#include <algorithm>
#include <stdint.h>
#include <boost/iterator/counting_iterator.hpp>
/*
1. USE_TBB - 3rdparty library, should be explicitly enabled
2. USE_OPENMP - integrated to compiler, defined iff HAVE_TBB is not
defined and _OPENMP is defined
3. USE_SERIAL
*/
#if defined USE_TBB
#define PARALLEL_FRAMEWORK "tbb"
#include "tbb/tbb.h"
#elif defined(_OPENMP) && defined(USE_OPENMP)
#define PARALLEL_FRAMEWORK "openmp"
#include <omp.h>
#else
#define PARALLEL_FRAMEWORK "serial"
#endif // PARALLEL_FRAMEWORK
/* ===================== ThreadSafe ===================== */
template <typename Arg> inline bool ThreadSafe(const Arg workspace) {
return workspace->threadSafe();
}
template <typename Arg, typename... Args>
inline bool ThreadSafe(const Arg &workspace, const Args &... others) {
return workspace->threadSafe() && ThreadSafe(others...);
}
/* ==================== parallel_for_each ==================== */
enum mantid_threading_library { TBB, OPENMP, NONE };
enum execution_policy : bool { seq, par }; // parallel stl also includes par_vec
template <mantid_threading_library type, typename func>
inline typename std::enable_if<type == NONE>::type
parallel_for_each(execution_policy /*policy*/, std::size_t start, std::size_t end,
func body) {
std::for_each(boost::counting_iterator<std::size_t>(start),
boost::counting_iterator<std::size_t>(end), body);
}
#if defined USE_TBB
template <mantid_threading_library type, typename func>
inline typename std::enable_if<type == TBB>::type
parallel_for_each(execution_policy policy, std::size_t start, std::size_t end,
func body) {
if (policy) {
tbb::parallel_for(start, end, body);
} else {
parallel_for_each<NONE>(seq, start, end, body);
}
}
template <typename... Args> inline void parallel_for_each(Args &&... args) {
parallel_for_each<TBB>(std::forward<Args>(args)...);
}
#elif USE_OPENMP
template <mantid_threading_library type, typename func>
inline typename std::enable_if<type == OPENMP>::type
parallel_for_each(execution_policy policy, std::size_t start, std::size_t end,
func body) {
#ifdef _MSC_VER
#pragma omp parallel for if (policy)
for (int64_t i = start; i < end; ++i)
body(static_cast<std::size_t>(i));
#else
#pragma omp parallel for if (policy)
for (std::size_t i = start; i < end; ++i)
body(i);
#endif
}
template <typename... Args> inline void parallel_for_each(Args &&... args) {
parallel_for_each<OPENMP>(std::forward<Args>(args)...);
}
#else
template <typename... Args> inline void parallel_for(Args &&... args) {
parallel_for_each<NONE>(std::forward<Args>(args)...);
}
#endif