/
itkPoolMultiThreader.cxx
298 lines (265 loc) · 9 KB
/
itkPoolMultiThreader.cxx
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
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#include "itkPoolMultiThreader.h"
#include "itkNumericTraits.h"
#include "itkProcessObject.h"
#include "itkImageSourceCommon.h"
#include <algorithm>
#include <exception>
#include <iostream>
#include <string>
namespace itk
{
namespace
{
class ExceptionHandler
{
public:
template <typename TFunction>
explicit ExceptionHandler(const TFunction & function)
{
try
{
function();
}
catch (...)
{
m_FirstCaughtException = std::current_exception();
}
}
void
TryToGetFuture(PoolMultiThreader::ThreadPoolInfoStruct & threadPoolInfo)
{
try
{
threadPoolInfo.Future.get();
}
catch (...)
{
if (m_FirstCaughtException == nullptr)
{
m_FirstCaughtException = std::current_exception();
}
}
}
void
RethrowFirstCaughtException() const
{
if (m_FirstCaughtException != nullptr)
{
std::rethrow_exception(m_FirstCaughtException);
}
}
private:
std::exception_ptr m_FirstCaughtException;
};
} // namespace
PoolMultiThreader::PoolMultiThreader()
: m_ThreadPool(ThreadPool::GetInstance())
{
for (ThreadIdType i = 0; i < ITK_MAX_THREADS; ++i)
{
m_ThreadInfoArray[i].WorkUnitID = i;
}
ThreadIdType defaultThreads = std::max(1u, GetGlobalDefaultNumberOfThreads());
#if !defined(ITKV4_COMPATIBILITY)
if (defaultThreads > 1) // one work unit for only one thread
{
defaultThreads *= 4;
}
#endif
m_NumberOfWorkUnits = std::min<ThreadIdType>(ITK_MAX_THREADS, defaultThreads);
m_MaximumNumberOfThreads = m_ThreadPool->GetMaximumNumberOfThreads();
}
PoolMultiThreader::~PoolMultiThreader() = default;
void
PoolMultiThreader::SetSingleMethod(ThreadFunctionType f, void * data)
{
m_SingleMethod = f;
m_SingleData = data;
}
void
PoolMultiThreader::SetMaximumNumberOfThreads(ThreadIdType numberOfThreads)
{
Superclass::SetMaximumNumberOfThreads(numberOfThreads);
ThreadIdType threadCount = m_ThreadPool->GetMaximumNumberOfThreads();
if (threadCount < m_MaximumNumberOfThreads)
{
m_ThreadPool->AddThreads(m_MaximumNumberOfThreads - threadCount);
}
m_MaximumNumberOfThreads = m_ThreadPool->GetMaximumNumberOfThreads();
}
void
PoolMultiThreader::SingleMethodExecute()
{
ThreadIdType threadLoop = 0;
if (!m_SingleMethod)
{
itkExceptionMacro(<< "No single method set!");
}
// obey the global maximum number of threads limit
m_NumberOfWorkUnits = std::min(this->GetGlobalMaximumNumberOfThreads(), m_NumberOfWorkUnits);
for (threadLoop = 1; threadLoop < m_NumberOfWorkUnits; ++threadLoop)
{
m_ThreadInfoArray[threadLoop].UserData = m_SingleData;
m_ThreadInfoArray[threadLoop].NumberOfWorkUnits = m_NumberOfWorkUnits;
m_ThreadInfoArray[threadLoop].Future = m_ThreadPool->AddWork(m_SingleMethod, &m_ThreadInfoArray[threadLoop]);
}
// Now, the parent thread calls this->SingleMethod() itself
m_ThreadInfoArray[0].UserData = m_SingleData;
m_ThreadInfoArray[0].NumberOfWorkUnits = m_NumberOfWorkUnits;
ExceptionHandler exceptionHandler([this] { m_SingleMethod(&m_ThreadInfoArray[0]); });
// The parent thread has finished SingleMethod()
// so now it waits for each of the other work units to finish
for (threadLoop = 1; threadLoop < m_NumberOfWorkUnits; ++threadLoop)
{
exceptionHandler.TryToGetFuture(m_ThreadInfoArray[threadLoop]);
}
exceptionHandler.RethrowFirstCaughtException();
}
void
PoolMultiThreader ::ParallelizeArray(SizeValueType firstIndex,
SizeValueType lastIndexPlus1,
ArrayThreadingFunctorType aFunc,
ProcessObject * filter)
{
MultiThreaderBase::HandleFilterProgress(filter, 0.0f);
if (firstIndex + 1 < lastIndexPlus1)
{
SizeValueType chunkSize = (lastIndexPlus1 - firstIndex) / m_NumberOfWorkUnits;
if ((lastIndexPlus1 - firstIndex) % m_NumberOfWorkUnits > 0)
{
chunkSize++; // we want slightly bigger chunks to be processed first
}
auto lambda = [aFunc](SizeValueType start, SizeValueType end) {
for (SizeValueType ii = start; ii < end; ii++)
{
aFunc(ii);
}
// make this lambda have the same signature as m_SingleMethod
return ITK_THREAD_RETURN_DEFAULT_VALUE;
};
SizeValueType workUnit = 1;
for (SizeValueType i = firstIndex + chunkSize; i < lastIndexPlus1; i += chunkSize)
{
m_ThreadInfoArray[workUnit++].Future = m_ThreadPool->AddWork(lambda, i, std::min(i + chunkSize, lastIndexPlus1));
}
itkAssertOrThrowMacro(workUnit <= m_NumberOfWorkUnits, "Number of work units was somehow miscounted!");
// execute this thread's share
ExceptionHandler exceptionHandler([lambda, firstIndex, chunkSize] { lambda(firstIndex, firstIndex + chunkSize); });
// now wait for the other computations to finish
for (SizeValueType i = 1; i < workUnit; i++)
{
if (filter)
{
filter->UpdateProgress(i / float(workUnit));
}
exceptionHandler.TryToGetFuture(m_ThreadInfoArray[i]);
}
exceptionHandler.RethrowFirstCaughtException();
}
else if (firstIndex + 1 == lastIndexPlus1)
{
aFunc(firstIndex);
}
// else nothing needs to be executed
MultiThreaderBase::HandleFilterProgress(filter, 1.0f);
}
void
PoolMultiThreader ::ParallelizeImageRegion(unsigned int dimension,
const IndexValueType index[],
const SizeValueType size[],
ThreadingFunctorType funcP,
ProcessObject * filter)
{
MultiThreaderBase::HandleFilterProgress(filter, 0.0f);
if (m_NumberOfWorkUnits == 1) // no multi-threading wanted
{
funcP(index, size); // process whole region
}
else
{
ImageIORegion region(dimension);
for (unsigned d = 0; d < dimension; d++)
{
region.SetIndex(d, index[d]);
region.SetSize(d, size[d]);
}
if (region.GetNumberOfPixels() <= 1)
{
funcP(index, size); // process whole region
}
else
{
const ImageRegionSplitterBase * splitter = ImageSourceCommon::GetGlobalDefaultSplitter();
ThreadIdType splitCount = splitter->GetNumberOfSplits(region, m_NumberOfWorkUnits);
itkAssertOrThrowMacro(splitCount <= m_NumberOfWorkUnits, "Split count is greater than number of work units!");
ImageIORegion iRegion;
ThreadIdType total;
for (ThreadIdType i = 1; i < splitCount; i++)
{
iRegion = region;
total = splitter->GetSplit(i, splitCount, iRegion);
if (i < total)
{
m_ThreadInfoArray[i].Future = m_ThreadPool->AddWork([funcP, iRegion]() {
funcP(&iRegion.GetIndex()[0], &iRegion.GetSize()[0]);
// make this lambda have the same signature as m_SingleMethod
return ITK_THREAD_RETURN_DEFAULT_VALUE;
});
}
else
{
itkExceptionMacro("Could not get work unit "
<< i << " even though we checked possible number of splits beforehand!");
}
}
iRegion = region;
total = splitter->GetSplit(0, splitCount, iRegion);
// execute this thread's share
ExceptionHandler exceptionHandler([funcP, iRegion] { funcP(&iRegion.GetIndex()[0], &iRegion.GetSize()[0]); });
// now wait for the other computations to finish
for (ThreadIdType i = 1; i < splitCount; i++)
{
if (filter)
{
filter->UpdateProgress(i / float(splitCount));
}
exceptionHandler.TryToGetFuture(m_ThreadInfoArray[i]);
}
exceptionHandler.RethrowFirstCaughtException();
}
}
MultiThreaderBase::HandleFilterProgress(filter, 1.0f);
}
void
PoolMultiThreader::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // namespace itk