Skip to content

Commit d216cfe

Browse files
committed
- Hacks to get TBB support on Windows/RTools
- One single entry point (Makevars) to turn TBB support on or off
1 parent 29ab791 commit d216cfe

File tree

7 files changed

+77
-39
lines changed

7 files changed

+77
-39
lines changed

R/options.R

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ dllInfo <- NULL
44

55
.onLoad <- function(libname, pkgname) {
66

7-
# load tbb if we aren't on windows
7+
# load tbb on supported platforms
88
sysname <- Sys.info()['sysname']
9-
if (sysname %in% c("Linux", "Darwin")) {
10-
if (sysname == "Darwin")
11-
ext = ".dylib"
12-
else if (sysname == "Linux")
13-
ext = ".so.2"
14-
dll <- system.file(paste("lib/libtbb", ext, sep = ""), package = "RcppParallel")
15-
dllInfo <<- dyn.load(dll, local = FALSE, now = TRUE)
9+
tbbSupported <- list(
10+
"Darwin" = "libtbb.dylib", "Linux" = "libtbb.so.2", "Windows" = "tbb.dll"
11+
)
12+
if (sysname %in% names(tbbSupported)) {
13+
dll <- system.file(paste("lib/", tbbSupported[[sysname]], sep = ""), package = "RcppParallel")
14+
if (!file.exists(dll)) {
15+
warning(paste("TBB library", dll, "not found."))
16+
} else {
17+
dllInfo <<- dyn.load(dll, local = FALSE, now = TRUE)
18+
}
1619
}
1720

1821
# load the package library
@@ -27,7 +30,7 @@ dllInfo <- NULL
2730
# unload the package library
2831
library.dynam.unload("RcppParallel", libpath)
2932

30-
# unload tbb if we loaded it (i.e. aren't on windows)
33+
# unload tbb if we loaded it
3134
if (!is.null(dllInfo))
3235
dyn.unload(dllInfo[["path"]])
3336
}
@@ -50,7 +53,8 @@ setThreadOptions <- function(numThreads = "auto", stackSize = "auto") {
5053
else
5154
stackSize <- as.integer(stackSize)
5255

53-
if (Sys.info()['sysname'] %in% c("Linux", "Darwin")) {
56+
# Call setThreadOptions if using tbb
57+
if (!is.null(dllInfo)) {
5458
invisible(.Call("setThreadOptions", numThreads, stackSize,
5559
PACKAGE = "RcppParallel"))
5660
}

inst/include/RcppParallel.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
// TinyThread implementation
66
#include "RcppParallel/TinyThread.h"
77

8-
// Use TBB only where it's known to compile and work correctly
8+
// Makevars owns setting this to 1 if TBB supported
99
#ifndef RCPP_PARALLEL_USE_TBB
10-
#if defined(__APPLE__) || defined(__gnu_linux__)
11-
#define RCPP_PARALLEL_USE_TBB 1
12-
#include "RcppParallel/TBB.h"
13-
#else
1410
#define RCPP_PARALLEL_USE_TBB 0
1511
#endif
12+
13+
#if RCPP_PARALLEL_USE_TBB
14+
#include "RcppParallel/TBB.h"
1615
#endif
1716

1817
#include "RcppParallel/RVector.h"

src/Makevars

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,59 @@ CXX_STD = CXX11
33

44
PKG_CPPFLAGS += -I../inst/include/
55

6+
ifeq ($(OS), Windows_NT)
7+
USE_TBB=Windows
8+
TBB_COPY_PATTERN=tbb.dll
9+
else
10+
611
UNAME := $(shell uname)
12+
TBB_COPY_PATTERN=libtbb.*
713

814
ifeq ($(UNAME), Darwin)
9-
USE_TBB=yes
15+
USE_TBB=Mac
1016
endif
1117
ifeq ($(UNAME), Linux)
12-
USE_TBB=yes
18+
USE_TBB=Linux
19+
endif
20+
# Note: regular MinGW not supported
21+
1322
endif
1423

1524
ifdef USE_TBB
1625

26+
# Tell options.cpp that TBB support is turned on
27+
PKG_CPPFLAGS += -DRCPP_PARALLEL_USE_TBB=1
28+
29+
MAKE_ARGS := CXXFLAGS=-DTBB_NO_LEGACY=1 tbb_release tbb_build_prefix=lib
30+
31+
# What I really want is startswith but this doesn't appear to be available
32+
ifneq (,$(findstring clang,$(CC)))
33+
MAKE_ARGS += compiler=clang
34+
endif
35+
36+
ifeq ($(USE_TBB), Windows)
37+
# rtools: turn on hacks to compensate for make and shell differences rtools<=>MinGW
38+
# compiler: overwrite default (which is cl = MS compiler)
39+
# arch: overwrite default (= ia32)
40+
MAKE_ARGS += rtools=true compiler=gcc arch=intel64
41+
# Linker needs access to the tbb dll; otherwise you get errors such as:
42+
# "undefined reference to `tbb::task_scheduler_init::terminate()'"
43+
PKG_LIBS += -L../inst/lib -ltbb
44+
endif
45+
1746
.PHONY: all tbb
1847

19-
all: $(SHLIB) tbb
48+
# Order is important in Windows' case. See PKG_LIBS above
49+
all: tbb $(SHLIB)
2050

2151
tbb:
2252
mkdir -p ../inst/lib
23-
if [[ "$(CC)" == clang* ]]; then \
24-
(cd tbb/src; make compiler=clang CXXFLAGS=-DTBB_NO_LEGACY=1 tbb_release tbb_build_prefix=lib) \
25-
else \
26-
(cd tbb/src; make CXXFLAGS=-DTBB_NO_LEGACY=1 tbb_release tbb_build_prefix=lib) \
27-
fi
28-
cp tbb/build/lib_release/libtbb.* ../inst/lib
53+
( cd tbb/src; make $(MAKE_ARGS) )
54+
cp tbb/build/lib_release/$(TBB_COPY_PATTERN) ../inst/lib
2955

3056
clean:
3157
(cd tbb/src; make clean)
3258

3359
endif
3460

3561

36-

src/Makevars.win

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/options.cpp

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

44
#include <Rinternals.h>
55

6-
#if RCPP_PARALLEL_USE_TBB
6+
#if RCPP_PARALLEL_USE_TBB // TBB support turned on
77

88
#include <string>
99
#include <exception>
@@ -45,7 +45,7 @@ extern "C" SEXP defaultNumThreads() {
4545
return threadsSEXP;
4646
}
4747

48-
#else // Windows
48+
#else // TBB support not turned on
4949

5050
#include <tthread/tinythread.h>
5151

src/tbb/build/common_rules.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ LINK_FILES+=$(TEST_LIBS)
111111
cpp <$< | grep -v '^#' >$*.tmp
112112
$(ASM) $(ASM_FLAGS) -o $@ $*.tmp
113113

114+
ifdef rtools
115+
# Line 70 doesn't work with rtool's version of make. The symptom being that the asm rule kicks off instead, and these rules are cl only
116+
%.$(OBJ): %.cpp
117+
$(CPLUS) $(OUTPUTOBJ_KEY)$@ $(COMPILE_ONLY) $(CPLUS_FLAGS) $(CXX_ONLY_FLAGS) $(CXX_WARN_SUPPRESS) $(INCLUDES) $<
118+
endif
119+
114120
# Rule for generating .E file if needed for visual inspection
115121
# Note that due to mapping for ICL all uses of PREPROC_ONLY should be immediately followed by a file name
116122
%.E: %.cpp

src/tbb/build/windows.inc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,28 @@
1616
# by the GNU General Public License. This exception does not however invalidate any other
1717
# reasons why the executable file might be covered by the GNU General Public License.
1818

19-
export SHELL = cmd
20-
2119
ifdef tbb_build_dir
2220
test_dir:=$(tbb_build_dir)
2321
else
2422
test_dir:=.
2523
endif
2624

25+
ifndef rtools
26+
export SHELL = cmd
27+
CMD:=cmd /C
28+
SLASH=\\
29+
RM=cmd /C del /Q /F
30+
RD=cmd /C rmdir
31+
MD=cmd /c mkdir
32+
else
33+
CMD:=cmd /C
34+
export SHELL = sh.exe
35+
SLASH=/
36+
RD=rmdir
37+
MD=mkdir
38+
RM=rm
39+
endif
40+
2741
# A convenience wrapper for calls to detect.js.
2842
# $(1) is the full command line for the script, e.g. /minversion icl 12
2943
detect_js = $(shell cmd /C "cscript /nologo /E:jscript $(tbb_root)/build/detect.js $(1)")
@@ -43,10 +57,6 @@ debugger ?= devenv /debugexe
4357

4458
CMD=cmd /C
4559
CWD=$(shell cmd /C echo %CD%)
46-
RM=cmd /C del /Q /F
47-
RD=cmd /C rmdir
48-
MD=cmd /c mkdir
49-
SLASH=\\
5060
NUL = nul
5161

5262
AR=lib

0 commit comments

Comments
 (0)