Skip to content

Commit 6404681

Browse files
committed
tweaks for TBB auto-detection and handling
1 parent ac488f6 commit 6404681

File tree

11 files changed

+112
-51
lines changed

11 files changed

+112
-51
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ revdep
1111
src-i386
1212
src-x64
1313
tbb.log
14+
15+
R/tbb-autodetected.R

R/aaa.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# stubs that get overridden via configure script
3+
TBB_LIB <- ""
4+
TBB_INC <- ""

R/tbb-autodetected.R.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
TBB_LIB <- "@TBB_LIB@"
3+
TBB_INC <- "@TBB_INC@"

R/tbb.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ tbbCxxFlags <- function() {
5353
flags <- c(flags, "-DRCPP_PARALLEL_USE_TBB=1")
5454

5555
# if TBB_INC is set, apply those library paths
56-
tbbInc <- Sys.getenv("TBB_INC", unset = NA)
57-
if (!is.na(tbbInc)) {
56+
tbbInc <- Sys.getenv("TBB_INC", unset = TBB_INC)
57+
if (nzchar(tbbInc)) {
5858

5959
# add include path
6060
flags <- c(flags, paste0("-I", asBuildPath(tbbInc)))
@@ -75,8 +75,8 @@ tbbCxxFlags <- function() {
7575
tbbLdFlags <- function() {
7676

7777
# shortcut if TBB_LIB defined
78-
tbbLib <- Sys.getenv("TBB_LIB", unset = NA)
79-
if (!is.na(tbbLib)) {
78+
tbbLib <- Sys.getenv("TBB_LIB", unset = TBB_LIB)
79+
if (nzchar(tbbLib)) {
8080
fmt <- "-L%1$s -Wl,-rpath,%1$s -ltbb -ltbbmalloc"
8181
return(sprintf(fmt, asBuildPath(tbbLib)))
8282
}
@@ -96,8 +96,8 @@ tbbLdFlags <- function() {
9696

9797
tbbRoot <- function() {
9898

99-
# TODO: rstan
100-
# parts <- c("libs", if (nzchar(rArch)) rArch, "tbb")
99+
if (nzchar(TBB_LIB))
100+
return(TBB_LIB)
101101

102102
rArch <- .Platform$r_arch
103103
parts <- c("lib", if (nzchar(rArch)) rArch)

inst/include/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# These TBB libraries are copied in at configure time.
33
/index.html
4+
/oneapi
45
/serial
56
/tbb
67

inst/tests/runit.distance.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11

2+
library(Rcpp)
3+
library(RUnit)
4+
25
sourceCpp(system.file("tests/cpp/distance.cpp", package = "RcppParallel"))
36

47
test.distance <- function() {
58

6-
n = 1000
7-
m = matrix(runif(n*10), ncol = 10)
8-
m = m/rowSums(m)
9+
n <- 1000
10+
m <- matrix(runif(n*10), ncol = 10)
11+
m <- m/rowSums(m)
912

1013
checkEquals(
1114
rcpp_js_distance(m),
1215
rcpp_parallel_js_distance(m)
1316
)
17+
1418
}

inst/tests/runit.innerproduct.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
library(Rcpp)
3+
library(RUnit)
4+
25
sourceCpp(system.file("tests/cpp/innerproduct.cpp", package = "RcppParallel"))
36

47
test.innerproduct <- function() {
@@ -10,4 +13,5 @@ test.innerproduct <- function() {
1013
innerProduct(x, y),
1114
parallelInnerProduct(x, y)
1215
)
16+
1317
}

inst/tests/runit.sum.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
library(Rcpp)
3+
library(RUnit)
4+
25
sourceCpp(system.file("tests/cpp/sum.cpp", package = "RcppParallel"))
36

47
test.sum <- function() {

inst/tests/runit.transform.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
library(Rcpp)
3+
library(RUnit)
4+
25
sourceCpp(system.file("tests/cpp/transform.cpp", package = "RcppParallel"))
36

47
test.transform <- function() {

src/Makevars.in

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11

22
PKG_CXXFLAGS = @CXX11STD@
33

4-
# If TBB_ROOT is defined, use it.
5-
ifdef TBB_ROOT
6-
7-
ifndef TBB_LIB
8-
TBB_LIB = $(TBB_ROOT)/lib
9-
endif
10-
11-
ifndef TBB_INC
12-
TBB_INC = $(TBB_ROOT)/include
13-
endif
14-
15-
endif
16-
17-
# If TBB_LIB is not defined, try to use autodetection
18-
ifndef TBB_LIB
19-
TBB_LIB = @TBB_LIB_AUTO@
20-
TBB_INC = @TBB_INC_AUTO@
21-
endif
22-
23-
# If TBB_LIB is defined but TBB_INC is not, make a guess.
24-
ifdef TBB_LIB
25-
ifndef TBB_INC
26-
TBB_INC = $(TBB_LIB)/../include
27-
endif
28-
endif
4+
TBB_LIB = @TBB_LIB@
5+
TBB_INC = @TBB_INC@
296

7+
# If TBB_INC is defined, include those library paths.
308
ifdef TBB_INC
319
PKG_CPPFLAGS = -I../inst/include -I$(TBB_INC)
3210
else
3311
PKG_CPPFLAGS = -I../inst/include
3412
endif
3513

14+
# If TBB_LIB is defined, link to that explicitly.
3615
ifdef TBB_LIB
3716
PKG_LIBS = -Wl,-L,"$(TBB_LIB)" -Wl,-rpath,"$(TBB_LIB)" -ltbb -ltbbmalloc
3817
endif

tools/config/configure.R

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,81 @@ if (Sys.info()[["sysname"]] == "SunOS") {
132132
}
133133
}
134134

135-
# tbb autodetection on Unix
136-
define(TBB_LIB_AUTO = "", TBB_INC_AUTO = "")
137-
if (.Platform$OS.type == "unix") {
138-
tbbLib <- Sys.glob(c(
139-
"/usr/*/libtbb.so",
140-
"/usr/*/*/libtbb.so",
141-
"/usr/*/*/*/libtbb.so"
142-
))
143-
tbbInc <- Sys.glob(c(
144-
"/usr/include/tbb.h",
145-
"/usr/include/*/tbb.h"
146-
))
147-
if (length(tbbLib) && length(tbbInc)) {
148-
define(
149-
TBB_LIB_AUTO = dirname(tbbLib[1]),
150-
TBB_INC_AUTO = dirname(tbbInc[1])
151-
)
135+
# try and figure out path to TBB
136+
tbbRoot <- Sys.getenv("TBB_ROOT", unset = NA)
137+
tbbLib <- Sys.getenv("TBB_LIB", unset = NA)
138+
tbbInc <- Sys.getenv("TBB_INC", unset = NA)
139+
140+
# check TBB_ROOT first if defined
141+
if (!is.na(tbbRoot)) {
142+
143+
if (is.na(tbbLib)) {
144+
tbbLib <- file.path(tbbRoot, "lib")
145+
}
146+
147+
if (is.na(tbbInc)) {
148+
tbbInc <- file.path(tbbRoot, "include")
149+
}
150+
151+
}
152+
153+
# if TBB_LIB is defined, guess TBB_INC
154+
if (!is.na(tbbLib) && is.na(tbbInc)) {
155+
tbbIncCandidate <- file.path(tbbLib, "../include")
156+
if (file.exists(tbbIncCandidate)) {
157+
tbbInc <- normalizePath(tbbIncCandidate)
158+
}
159+
}
160+
161+
# if TBB_LIB and TBB_INC are still not defined, try auto-detecting
162+
tryAutoDetect <-
163+
.Platform$OS.type == "unix" &&
164+
Sys.getenv("TBB_AUTODETECT", unset = "TRUE") == "TRUE" &&
165+
is.na(tbbLib) &&
166+
is.na(tbbInc)
167+
168+
if (tryAutoDetect) {
169+
170+
sysInfo <- as.list(Sys.info())
171+
172+
homebrewPrefix <- if (sysInfo$sysname == "Darwin") {
173+
"/opt/homebrew"
174+
} else {
175+
"/usr/local"
152176
}
177+
178+
tbbLibSearch <- if (sysInfo$sysname == "Darwin") {
179+
file.path(homebrewPrefix, "opt/tbb/lib/libtbb.dylib")
180+
} else {
181+
Sys.glob(c(
182+
"/usr/*/libtbb.so",
183+
"/usr/*/*/libtbb.so",
184+
"/usr/*/*/*/libtbb.so"
185+
))
186+
}
187+
188+
tbbIncSearch <- if (sysInfo$sysname == "Darwin") {
189+
file.path(homebrewPrefix, "opt/tbb/include/tbb")
190+
} else {
191+
Sys.glob(c(
192+
"/usr/include/tbb.h",
193+
"/usr/include/*/tbb.h"
194+
))
195+
}
196+
197+
if (length(tbbLibSearch) &&
198+
length(tbbIncSearch) &&
199+
file.exists(tbbLibSearch[[1L]]) &&
200+
file.exists(tbbIncSearch[[1L]]))
201+
{
202+
tbbLib <- dirname(tbbLibSearch[[1L]])
203+
tbbInc <- dirname(tbbIncSearch[[1L]])
204+
}
205+
153206
}
154207

208+
# now, define TBB_LIB and TBB_INC as appropriate
209+
define(
210+
TBB_LIB = if (!is.na(tbbLib)) tbbLib else "",
211+
TBB_INC = if (!is.na(tbbInc)) tbbInc else ""
212+
)

0 commit comments

Comments
 (0)