-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathoptions.cpp
More file actions
60 lines (44 loc) · 1.38 KB
/
options.cpp
File metadata and controls
60 lines (44 loc) · 1.38 KB
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
#include <RcppParallel.h>
#include <Rinternals.h>
#if RCPP_PARALLEL_USE_TBB // TBB support turned on
#include <string>
#include <exception>
#include <tbb/task_scheduler_init.h>
extern "C" SEXP setThreadOptions(SEXP numThreadsSEXP, SEXP stackSizeSEXP) {
static tbb::task_scheduler_init* s_pTaskScheduler = NULL;
if (s_pTaskScheduler != NULL)
return Rf_ScalarLogical(0);
int numThreads = Rf_asInteger(numThreadsSEXP);
int stackSize = Rf_asInteger(stackSizeSEXP);
try
{
s_pTaskScheduler = new tbb::task_scheduler_init(numThreads, stackSize);
}
catch(const std::exception& e)
{
const char* fmt = "Error loading TBB: %s\n";
Rf_error(fmt, e.what());
}
catch(...)
{
const char* fmt = "Error loading TBB: %s\n";
Rf_error(fmt, "(Unknown error)");
}
return Rf_ScalarLogical(1);
}
extern "C" SEXP defaultNumThreads() {
SEXP threadsSEXP = Rf_allocVector(INTSXP, 1);
INTEGER(threadsSEXP)[0] = tbb::task_scheduler_init::default_num_threads();
return threadsSEXP;
}
#else // TBB support not turned on
#include <tthread/tinythread.h>
extern "C" SEXP setThreadOptions(SEXP numThreadsSEXP, SEXP stackSizeSEXP) {
return R_NilValue;
}
extern "C" SEXP defaultNumThreads() {
SEXP threadsSEXP = Rf_allocVector(INTSXP, 1);
INTEGER(threadsSEXP)[0] = tthread::thread::hardware_concurrency();
return threadsSEXP;
}
#endif