Skip to content

Commit 4a89cc4

Browse files
committed
tthread / windows options
1 parent 162b352 commit 4a89cc4

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

R/options.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ setThreadOptions <- function(numThreads = "auto", stackSize = "auto") {
5252
else
5353
stackSize <- as.integer(stackSize)
5454

55-
invisible(.Call("setThreadOptions", numThreads, stackSize,
56-
PACKAGE = "RcppParallel"))
55+
if (Sys.info()['sysname'] != "Windows") {
56+
invisible(.Call("setThreadOptions", numThreads, stackSize,
57+
PACKAGE = "RcppParallel"))
58+
}
59+
60+
if (numThreads != -1)
61+
Sys.setenv(RCPP_PARALLEL_NUM_THREADS = numThreads)
5762
}
5863

5964
defaultNumThreads <- function() {

inst/include/RcppParallel.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct Worker {
3131
// tinythread library
3232
#include <tthread/tinythread.h>
3333

34+
#include <stdio.h>
35+
3436
#include <vector>
3537

3638
namespace RcppParallel {
@@ -88,9 +90,15 @@ extern "C" inline void workerThread(void* data) {
8890
// Function to calculate the ranges for a given input
8991
std::vector<IndexRange> splitInputRange(const IndexRange& range) {
9092

91-
// max threads is based on hardware concurrency
93+
// determine number of threads
9294
std::size_t threads = tthread::thread::hardware_concurrency();
93-
95+
char* numThreads = ::getenv("RCPP_PARALLEL_NUM_THREADS");
96+
if (numThreads != NULL) {
97+
int parsedThreads = ::atoi(numThreads);
98+
if (parsedThreads > 0)
99+
threads = parsedThreads;
100+
}
101+
94102
// determine the chunk size
95103
std::size_t length = range.end() - range.begin();
96104
std::size_t chunkSize = length / threads;

src/options.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ extern "C" SEXP defaultNumThreads() {
4343
return threadsSEXP;
4444
}
4545

46-
#else
46+
#else // Windows
4747

48-
extern "C" SEXP setThreadOptions(SEXP numThreadsSEXP, SEXP stackSizeSEXP) {
49-
return R_NilValue;
50-
}
48+
#include <tthread/tinythread.h>
5149

5250
extern "C" SEXP defaultNumThreads() {
5351
SEXP threadsSEXP = Rf_allocVector(INTSXP, 1);
54-
INTEGER(threadsSEXP)[0] = 4;
52+
INTEGER(threadsSEXP)[0] = tthread::thread::hardware_concurrency();
5553
return threadsSEXP;
5654
}
5755

0 commit comments

Comments
 (0)