Skip to content

Commit

Permalink
make sure RcppParallel still builds with older R
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Jan 14, 2025
1 parent f3fcdfd commit c328085
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
## RcppParallel 5.1.10 (UNRELEASED)

* RcppParallel now bundles oneTBB 2022.0.0.

* On Windows, RcppParallel now uses the copy of TBB provided by Rtools, if any.
If TBB is not available, RcppParallel will use only the fallback 'tinythread'
implementation. In practice, this implies that RcppParallel will now only
provide a TBB backend with R (>= 4.2.0).

## RcppParallel 5.1.9

Expand Down
1 change: 1 addition & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

# stubs that get overridden via configure script
TBB_ENABLED <- TRUE
TBB_LIB <- ""
TBB_INC <- ""
TBB_NAME <- "tbb"
1 change: 1 addition & 0 deletions R/tbb-autodetected.R.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

TBB_ENABLED <- @TBB_ENABLED@
TBB_LIB <- "@TBB_LIB@"
TBB_INC <- "@TBB_INC@"
TBB_NAME <- "@TBB_NAME@"
3 changes: 2 additions & 1 deletion R/tbb.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ tbbCxxFlags <- function() {

# opt-in to TBB on Windows
if (is_windows()) {
flags <- c(flags, "-DRCPP_PARALLEL_USE_TBB=1")
enabled <- if (TBB_ENABLED) "1" else "0"
flags <- c(flags, sprintf("-DRCPP_PARALLEL_USE_TBB=%s", enabled))
if (R.version$arch == "aarch64") {
# TBB does not have assembly code for Windows ARM64
# so we need to use compiler builtins
Expand Down
2 changes: 1 addition & 1 deletion src/Makevars.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TBB_INC = @TBB_INC@
TBB_NAME = @TBB_NAME@

PKG_CPPFLAGS = @PKG_CPPFLAGS@
PKG_CXXFLAGS = @CXX11STD@ -DRCPP_PARALLEL_USE_TBB=1
PKG_CXXFLAGS = @PKG_CXXFLAGS@

PKG_LIBS = @PKG_LIBS@ @PKG_LIBS_EXTRA@

Expand Down
51 changes: 44 additions & 7 deletions src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,56 @@ useBundledTbb <- function() {
useTbbPreamble("tbb/include")
dir.create("tbb/build-tbb", showWarnings = FALSE)

cmake <- Sys.getenv("CMAKE", unset = "cmake")
buildType <- Sys.getenv("CMAKE_BUILD_TYPE", unset = "Release")
verbose <- Sys.getenv("VERBOSE", unset = "0")

cmakeFlags <- c(
sprintf("-DCMAKE_BUILD_TYPE=%s", buildType),
"-DTBB_TEST=0",
"-DTBB_EXAMPLES=0",
"-DTBB_STRICT=0",
".."
)

writeLines("*** configuring tbb")
status <- system("cd tbb/build-tbb; cmake -DTBB_TEST=0 -DTBB_EXAMPLES=0 -DTBB_STRICT=0 .. > build.log 2>&1")
if (status != 0L) {
system("cat build.log")
owd <- setwd("tbb/build-tbb")
output <- system2(cmake, shQuote(cmakeFlags), stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (is.numeric(status) && status != 0L) {
writeLines(output)
stop("error configuring tbb (status code ", status, ")")
} else if (!identical(verbose, "0")) {
writeLines(output)
}
setwd(owd)

writeLines("*** building tbb")
status <- system("cd tbb/build-tbb; cmake --build . > build.log 2>&1")
if (status != 0L) {
system("cat build.log")
owd <- setwd("tbb/build-tbb")
output <- system2(cmake, c("--build", ".", "--config", "release"), stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (is.numeric(status) && status != 0L) {
writeLines(output)
stop("error building tbb (status code ", status, ")")
} else if (!identical(verbose, "0")) {
writeLines(output)
}
setwd(owd)

shlibPattern <- switch(
Sys.info()[["sysname"]],
Windows = "^tbb.*\\.dll$",
Darwin = "^libtbb.*\\.dylib$",
"^libtbb.*\\.so.*$"
)

tbbFiles <- list.files(
"tbb/build-tbb",
pattern = shlibPattern,
recursive = TRUE,
full.names = TRUE
)

tbbFiles <- list.files(pattern = "^libtbb\\.(so|dylib)", recursive = TRUE)
tbbDir <- dirname(tbbFiles[[1L]])

dir.create("tbb/build", showWarnings = FALSE)
Expand All @@ -123,6 +158,8 @@ args <- commandArgs(trailingOnly = TRUE)
if (identical(args, "build")) {
if (nzchar(tbbLib) && nzchar(tbbInc)) {
useSystemTbb(tbbLib, tbbInc)
} else if (.Platform$OS.type == "windows") {
writeLines("** building RcppParallel without tbb backend")
} else {
useBundledTbb()
}
Expand Down
30 changes: 21 additions & 9 deletions tools/config/configure.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,26 @@ switch(
# on Windows, check for Rtools; if it exists, and we have tbb, use it
if (.Platform$OS.type == "windows") {

tbbPattern <- "lib(tbb[^[:alpha:]]*)\\.a$"
gccPath <- normalizePath(Sys.which("gcc"), winslash = "/")

tbbLib <- Sys.getenv("TBB_LIB", unset = NA)
if (is.na(tbbLib)) {
if (is.na(tbbLib))
tbbLib <- normalizePath(file.path(gccPath, "../../lib"), winslash = "/")
Sys.setenv(TBB_LIB = tbbLib)
}

tbbInc <- Sys.getenv("TBB_INC", unset = NA)
if (is.na(tbbInc)) {
if (is.na(tbbInc))
tbbInc <- normalizePath(file.path(gccPath, "../../include"), winslash = "/")
Sys.setenv(TBB_INC = tbbInc)
}

tbbPattern <- "lib(tbb[^[:alpha:]]*)\\.a$"
tbbLibs <- list.files(tbbLib, pattern = tbbPattern)
if (length(tbbLibs)) {
tbbName <- gsub(tbbPattern, "\\1", tbbLibs[[1L]])
Sys.setenv(TBB_NAME = tbbName)
Sys.setenv(
TBB_LIB = tbbLib,
TBB_INC = tbbInc,
TBB_NAME = tbbName
)
}

}

# try and figure out path to TBB
Expand Down Expand Up @@ -220,6 +219,11 @@ pkgLibs <- if (!is.na(tbbLib)) {
"-l$(TBB_NAME)",
"-ltbbmalloc"
)

} else if (.Platform$OS.type == "windows") {

NULL

} else {

c(
Expand Down Expand Up @@ -283,6 +287,14 @@ if (!is.na(tbbLib)) {
define(PKG_CPPFLAGS = "-I../inst/include")
}

# PKG_CXXFLAGS
if (.Platform$OS.type == "windows" && is.na(tbbLib)) {
define(TBB_ENABLED = FALSE)
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=0")
} else {
define(TBB_ENABLED = TRUE)
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=1")
}

# macOS needs some extra flags set
if (Sys.info()[["sysname"]] == "Darwin") {
Expand Down

0 comments on commit c328085

Please sign in to comment.