Skip to content

Commit da83a9a

Browse files
committed
use global control to set stack size
1 parent 02e60ed commit da83a9a

File tree

3 files changed

+77
-31
lines changed

3 files changed

+77
-31
lines changed

inst/include/RcppParallel.h

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,6 @@
2828

2929
namespace RcppParallel {
3030

31-
namespace {
32-
33-
template <typename T, typename U>
34-
int resolveValue(const char* envvar,
35-
T requestedValue,
36-
U defaultValue)
37-
{
38-
// if the requested value is non-zero and not the default, we can use it
39-
if (requestedValue != defaultValue && requestedValue > 0)
40-
return requestedValue;
41-
42-
// otherwise, try reading the default from associated envvar
43-
// if the environment variable is unset, use the default
44-
const char* var = getenv(envvar);
45-
if (var == NULL)
46-
return defaultValue;
47-
48-
// try to convert the string to a number
49-
// if an error occurs during conversion, just use default
50-
errno = 0;
51-
char* end;
52-
long value = strtol(var, &end, 10);
53-
if (errno != 0)
54-
return defaultValue;
55-
56-
// okay, return the parsed environment variable value
57-
return value;
58-
}
59-
60-
} // end anonymous namespace
61-
6231
inline void parallelFor(std::size_t begin,
6332
std::size_t end,
6433
Worker& worker,

inst/include/RcppParallel/Common.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
#ifndef __RCPP_PARALLEL_COMMON__
22
#define __RCPP_PARALLEL_COMMON__
33

4+
#include <cerrno>
45
#include <cstddef>
6+
#include <cstdlib>
57

68
namespace RcppParallel {
79

10+
template <typename T, typename U>
11+
inline int resolveValue(const char* envvar,
12+
T requestedValue,
13+
U defaultValue)
14+
{
15+
// if the requested value is non-zero and not the default, we can use it
16+
if (requestedValue != defaultValue && requestedValue > 0)
17+
return requestedValue;
18+
19+
// otherwise, try reading the default from associated envvar
20+
// if the environment variable is unset, use the default
21+
const char* var = getenv(envvar);
22+
if (var == NULL)
23+
return defaultValue;
24+
25+
// try to convert the string to a number
26+
// if an error occurs during conversion, just use default
27+
errno = 0;
28+
char* end;
29+
long value = strtol(var, &end, 10);
30+
31+
// check for conversion failure
32+
if (end == var || *end != '\0' || errno == ERANGE)
33+
return defaultValue;
34+
35+
// okay, return the parsed environment variable value
36+
return value;
37+
}
38+
839
// Work executed within a background thread. We implement dynamic
940
// dispatch using vtables so we can have a stable type to cast
1041
// to from the void* passed to the worker thread (required because

inst/include/RcppParallel/TBB.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
#include "Common.h"
55

6+
#ifndef TBB_PREVIEW_GLOBAL_CONTROL
7+
# define TBB_PREVIEW_GLOBAL_CONTROL 1
8+
#endif
9+
610
#include <tbb/tbb.h>
11+
#include <tbb/global_control.h>
712
#include <tbb/scalable_allocator.h>
813

914
namespace RcppParallel {
@@ -178,6 +183,43 @@ class TBBArenaParallelReduceExecutor
178183
std::size_t end_;
179184
std::size_t grainSize_;
180185
};
186+
187+
class ThreadStackSizeControl
188+
{
189+
public:
190+
191+
ThreadStackSizeControl()
192+
: control_(nullptr)
193+
{
194+
int stackSize = resolveValue("RCPP_PARALLEL_STACK_SIZE", 0, 0);
195+
if (stackSize > 0)
196+
{
197+
control_ = new tbb::global_control(
198+
tbb::global_control::thread_stack_size,
199+
stackSize
200+
);
201+
}
202+
}
203+
204+
~ThreadStackSizeControl()
205+
{
206+
if (control_ != nullptr)
207+
{
208+
delete control_;
209+
control_ = nullptr;
210+
}
211+
}
212+
213+
private:
214+
215+
// COPYING: not copyable
216+
ThreadStackSizeControl(const ThreadStackSizeControl&);
217+
ThreadStackSizeControl& operator=(const ThreadStackSizeControl&);
218+
219+
// private members
220+
tbb::global_control* control_;
221+
222+
};
181223

182224
} // anonymous namespace
183225

@@ -188,6 +230,8 @@ inline void tbbParallelFor(std::size_t begin,
188230
std::size_t grainSize = 1,
189231
int numThreads = tbb::task_arena::automatic)
190232
{
233+
ThreadStackSizeControl control;
234+
191235
tbb::task_arena arena(numThreads);
192236
tbb::task_group group;
193237

@@ -202,6 +246,8 @@ inline void tbbParallelReduce(std::size_t begin,
202246
std::size_t grainSize = 1,
203247
int numThreads = tbb::task_arena::automatic)
204248
{
249+
ThreadStackSizeControl control;
250+
205251
tbb::task_arena arena(numThreads);
206252
tbb::task_group group;
207253

0 commit comments

Comments
 (0)