forked from sideeffects/WindingNumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUT_ParallelUtil.h
226 lines (198 loc) · 7.26 KB
/
UT_ParallelUtil.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
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Simple wrappers on tbb interface
*/
#ifndef __UT_ParallelUtil__
#define __UT_ParallelUtil__
#include "UT_Array.h"
#include <thread> // This is just included for std::thread::hardware_concurrency()
namespace UT_Thread { int getNumProcessors() {
return std::thread::hardware_concurrency();
}}
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
//namespace tbb { class split; }
/// Declare prior to use.
template <typename T>
using UT_BlockedRange = tbb::blocked_range<T>;
// Default implementation that calls range.size()
template< typename RANGE >
struct UT_EstimatorNumItems
{
UT_EstimatorNumItems() {}
size_t operator()(const RANGE& range) const
{
return range.size();
}
};
/// This is needed by UT_CoarsenedRange
template <typename RANGE>
inline size_t UTestimatedNumItems(const RANGE& range)
{
return UT_EstimatorNumItems<RANGE>()(range);
}
/// UT_CoarsenedRange: This should be used only inside
/// UT_ParallelFor and UT_ParallelReduce
/// This class wraps an existing range with a new range.
/// This allows us to use simple_partitioner, rather than
/// auto_partitioner, which has disastrous performance with
/// the default grain size in ttb 4.
template< typename RANGE >
class UT_CoarsenedRange : public RANGE
{
public:
// Compiler-generated versions are fine:
// ~UT_CoarsenedRange();
// UT_CoarsenedRange(const UT_CoarsenedRange&);
// Split into two sub-ranges:
UT_CoarsenedRange(UT_CoarsenedRange& range, tbb::split spl) :
RANGE(range, spl),
myGrainSize(range.myGrainSize)
{
}
// Inherited: bool empty() const
bool is_divisible() const
{
return
RANGE::is_divisible() &&
(UTestimatedNumItems(static_cast<const RANGE&>(*this)) > myGrainSize);
}
private:
size_t myGrainSize;
UT_CoarsenedRange(const RANGE& base_range, const size_t grain_size) :
RANGE(base_range),
myGrainSize(grain_size)
{
}
template <typename Range, typename Body>
friend void UTparallelFor(
const Range &range, const Body &body,
const int subscribe_ratio, const int min_grain_size
);
};
/// Run the @c body function over a range in parallel.
/// UTparallelFor attempts to spread the range out over at most
/// subscribe_ratio * num_processor tasks.
/// The factor subscribe_ratio can be used to help balance the load.
/// UTparallelFor() uses tbb for its implementation.
/// The used grain size is the maximum of min_grain_size and
/// if UTestimatedNumItems(range) / (subscribe_ratio * num_processor).
/// If subscribe_ratio == 0, then a grain size of min_grain_size will be used.
/// A range can be split only when UTestimatedNumItems(range) exceeds the
/// grain size the range is divisible.
///
/// Requirements for the Range functor are:
/// - the requirements of the tbb Range Concept
/// - UT_estimatorNumItems<Range> must return the the estimated number of work items
/// for the range. When Range::size() is not the correct estimate, then a
/// (partial) specialization of UT_estimatorNumItemsimatorRange must be provided
/// for the type Range.
///
/// Requirements for the Body function are:
/// - @code Body(const Body &); @endcode @n
/// Copy Constructor
/// - @code Body()::~Body(); @endcode @n
/// Destructor
/// - @code void Body::operator()(const Range &range) const; @endcode
/// Function call to perform operation on the range. Note the operator is
/// @b const.
///
/// The requirements for a Range object are:
/// - @code Range::Range(const Range&); @endcode @n
/// Copy constructor
/// - @code Range::~Range(); @endcode @n
/// Destructor
/// - @code bool Range::is_divisible() const; @endcode @n
/// True if the range can be partitioned into two sub-ranges
/// - @code bool Range::empty() const; @endcode @n
/// True if the range is empty
/// - @code Range::Range(Range &r, UT_Split) const; @endcode @n
/// Split the range @c r into two sub-ranges (i.e. modify @c r and *this)
///
/// Example: @code
/// class Square {
/// public:
/// Square(double *data) : myData(data) {}
/// ~Square();
/// void operator()(const UT_BlockedRange<int64> &range) const
/// {
/// for (int64 i = range.begin(); i != range.end(); ++i)
/// myData[i] *= myData[i];
/// }
/// double *myData;
/// };
/// ...
///
/// void
/// parallel_square(double *array, int64 length)
/// {
/// UTparallelFor(UT_BlockedRange<int64>(0, length), Square(array));
/// }
/// @endcode
///
/// @see UTparallelReduce(), UT_BlockedRange()
template <typename Range, typename Body>
void UTparallelFor(
const Range &range, const Body &body,
const int subscribe_ratio = 2,
const int min_grain_size = 1
)
{
const size_t num_processors( UT_Thread::getNumProcessors() );
UT_ASSERT( num_processors >= 1 );
UT_ASSERT( min_grain_size >= 1 );
UT_ASSERT( subscribe_ratio >= 0 );
const size_t est_range_size( UTestimatedNumItems(range) );
// Don't run on an empty range!
if (est_range_size == 0)
return;
// Avoid tbb overhead if entire range needs to be single threaded
if (num_processors == 1 || est_range_size <= min_grain_size)
{
body(range);
return;
}
size_t grain_size(min_grain_size);
if( subscribe_ratio > 0 )
grain_size = std::max(
grain_size,
est_range_size / (subscribe_ratio * num_processors)
);
UT_CoarsenedRange< Range > coarsened_range(range, grain_size);
tbb::parallel_for(coarsened_range, body, tbb::simple_partitioner());
}
/// Version of UTparallelFor that is tuned for the case where the range
/// consists of lightweight items, for example,
/// float additions or matrix-vector multiplications.
template <typename Range, typename Body>
void
UTparallelForLightItems(const Range &range, const Body &body)
{
UTparallelFor(range, body, 2, 1024);
}
/// UTserialFor can be used as a debugging tool to quickly replace a parallel
/// for with a serial for.
template <typename Range, typename Body>
void UTserialFor(const Range &range, const Body &body)
{ body(range); }
#endif