Skip to content

Commit ae4673a

Browse files
committed
use RCPP_PARALLEL_NUM_THREADS for default num threads
1 parent 394a729 commit ae4673a

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

R/options.R

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,20 @@ setThreadOptions <- function(numThreads = "auto", stackSize = "auto") {
2222
else
2323
stackSize <- as.integer(stackSize)
2424

25-
# Call setThreadOptions if using tbb
26-
if (!is.null(dllInfo) && isUsingTbb())
27-
setTbbThreadOptions(numThreads, stackSize)
28-
25+
# set RCPP_PARALLEL_NUM_THREADS
2926
if (numThreads == -1L)
3027
Sys.unsetenv("RCPP_PARALLEL_NUM_THREADS")
3128
else
3229
Sys.setenv(RCPP_PARALLEL_NUM_THREADS = numThreads)
33-
}
34-
35-
setTbbThreadOptions <- function(numThreads, stackSize) {
36-
.Call(
37-
"setThreadOptions",
38-
as.integer(numThreads),
39-
as.integer(stackSize),
40-
PACKAGE = "RcppParallel"
41-
)
30+
31+
# set RCPP_PARALLEL_STACK_SIZE
32+
if (stackSize == 0L)
33+
Sys.unsetenv("RCPP_PARALLEL_STACK_SIZE")
34+
else
35+
Sys.setenv(RCPP_PARALLEL_STACK_SIZE = stackSize)
36+
4237
}
4338

4439
defaultNumThreads <- function() {
45-
.Call(
46-
"defaultNumThreads",
47-
PACKAGE = "RcppParallel"
48-
)
40+
.Call("defaultNumThreads", PACKAGE = "RcppParallel")
4941
}

inst/include/RcppParallel/TBB.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,31 @@ class TBBArenaParallelReduceExecutor
178178
std::size_t end_;
179179
std::size_t grainSize_;
180180
};
181+
182+
int tbbResolveNumThreads(int numThreads)
183+
{
184+
// if the user has explicitly set the value away from the default,
185+
// then we can just honor their request
186+
if (numThreads != -1)
187+
return numThreads;
188+
189+
// otherwise, try reading the default from RCPP_PARALLEL_NUM_THREADS.
190+
// if that is unset, we'll let tbb decide
191+
const char* var = getenv("RCPP_PARALLEL_NUM_THREADS");
192+
if (var == NULL)
193+
return tbb::task_arena::automatic;
194+
195+
// try to convert the string to a number
196+
errno = 0;
197+
char* end;
198+
long threads = strtol(var, &end, 10);
199+
200+
// if an error occurred during conversion, just use default
201+
if (errno != 0)
202+
threads = tbb::task_arena::automatic;
203+
204+
return threads;
205+
}
181206

182207
} // anonymous namespace
183208

@@ -188,7 +213,7 @@ inline void tbbParallelFor(std::size_t begin,
188213
std::size_t grainSize = 1,
189214
int numThreads = -1)
190215
{
191-
tbb::task_arena arena(numThreads == -1 ? tbb::task_arena::automatic : numThreads);
216+
tbb::task_arena arena(tbbResolveNumThreads(numThreads));
192217
tbb::task_group group;
193218

194219
TBBArenaParallelForExecutor executor(group, worker, begin, end, grainSize);
@@ -202,7 +227,7 @@ inline void tbbParallelReduce(std::size_t begin,
202227
std::size_t grainSize = 1,
203228
int numThreads = -1)
204229
{
205-
tbb::task_arena arena(numThreads == -1 ? tbb::task_arena::automatic : numThreads);
230+
tbb::task_arena arena(tbbResolveNumThreads(numThreads));
206231
tbb::task_group group;
207232

208233
TBBArenaParallelReduceExecutor<Reducer> executor(group, reducer, begin, end, grainSize);

src/init.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
/* .Call calls */
88
extern "C" SEXP defaultNumThreads();
9-
extern "C" SEXP setThreadOptions(SEXP, SEXP);
109

1110
static const R_CallMethodDef CallEntries[] = {
1211
{"defaultNumThreads", (DL_FUNC) &defaultNumThreads, 0},
13-
{"setThreadOptions", (DL_FUNC) &setThreadOptions, 2},
1412
{NULL, NULL, 0}
1513
};
1614

src/options.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,6 @@
88
#include <string>
99
#include <exception>
1010

11-
#include <tbb/task_scheduler_init.h>
12-
13-
extern "C" SEXP setThreadOptions(SEXP numThreadsSEXP, SEXP stackSizeSEXP) {
14-
15-
static tbb::task_scheduler_init* s_pTaskScheduler = NULL;
16-
if (s_pTaskScheduler != NULL)
17-
return Rf_ScalarLogical(0);
18-
19-
int numThreads = Rf_asInteger(numThreadsSEXP);
20-
int stackSize = Rf_asInteger(stackSizeSEXP);
21-
22-
try
23-
{
24-
s_pTaskScheduler = new tbb::task_scheduler_init(numThreads, stackSize);
25-
}
26-
catch(const std::exception& e)
27-
{
28-
const char* fmt = "Error loading TBB: %s\n";
29-
Rf_error(fmt, e.what());
30-
}
31-
catch(...)
32-
{
33-
const char* fmt = "Error loading TBB: %s\n";
34-
Rf_error(fmt, "(Unknown error)");
35-
}
36-
37-
return Rf_ScalarLogical(1);
38-
}
39-
4011
extern "C" SEXP defaultNumThreads() {
4112
SEXP threadsSEXP = Rf_allocVector(INTSXP, 1);
4213
INTEGER(threadsSEXP)[0] = tbb::task_scheduler_init::default_num_threads();
@@ -47,10 +18,6 @@ extern "C" SEXP defaultNumThreads() {
4718

4819
#include <tthread/tinythread.h>
4920

50-
extern "C" SEXP setThreadOptions(SEXP numThreadsSEXP, SEXP stackSizeSEXP) {
51-
return R_NilValue;
52-
}
53-
5421
extern "C" SEXP defaultNumThreads() {
5522
SEXP threadsSEXP = Rf_allocVector(INTSXP, 1);
5623
INTEGER(threadsSEXP)[0] = tthread::thread::hardware_concurrency();

0 commit comments

Comments
 (0)