Skip to content

Commit 688f292

Browse files
committed
add tbbmalloc library (scalable multi-threaded allocator)
1 parent 3082223 commit 688f292

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ RcppParallel 4.3.7
33

44
* Fix failure to compile with Rtools 3.3
55
* Fix failure to compile on OS X Snow Leopard R toolchain
6+
* Add tbbmalloc library
67

78
RcppParallel 4.3.6
89
------------------------------------------------------------------------

R/build.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ tbbLdFlags <- function() {
3939
# on Windows we need to explicitly link against tbb.dll
4040
if (Sys.info()['sysname'] == "Windows") {
4141
tbb <- tbbLibPath()
42-
paste("-L", asBuildPath(dirname(tbb)), " -ltbb", sep="")
42+
paste("-L", asBuildPath(dirname(tbb)), " -ltbb", "-ltbbmalloc", sep = "")
4343
} else {
4444
""
4545
}
4646
}
4747

4848
# Determine the platform-specific path to the TBB library
49-
tbbLibPath <- function() {
49+
tbbLibPath <- function(suffix = "") {
5050
sysname <- Sys.info()['sysname']
5151
tbbSupported <- list(
52-
"Darwin" = "libtbb.dylib",
53-
"Linux" = "libtbb.so.2",
54-
"Windows" = "tbb.dll"
52+
"Darwin" = paste("libtbb", suffix, ".dylib", sep = ""),
53+
"Linux" = paste("libtbb", suffix, ".so.2", sep = ""),
54+
"Windows" = paste("tbb", suffix, ".dll", sep = "")
5555
)
5656
if (sysname %in% names(tbbSupported)) {
5757
libDir <- "lib/"

R/hooks.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
dllInfo <- NULL
3-
3+
mallocDllInfo <- NULL
44

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

7-
# load tbb on supported platforms
7+
# load tbb and tbbmalloc on supported platforms
88
tbb <- tbbLibPath()
99
if (!is.null(tbb)) {
1010
if (!file.exists(tbb)) {
@@ -13,6 +13,14 @@ dllInfo <- NULL
1313
dllInfo <<- dyn.load(tbb, local = FALSE, now = TRUE)
1414
}
1515
}
16+
tbbMalloc <- tbbLibPath("malloc")
17+
if (!is.null(tbbMalloc)) {
18+
if (!file.exists(tbbMalloc)) {
19+
warning(paste("TBB malloc library", tbbMalloc, "not found."))
20+
} else {
21+
mallocDllInfo <<- dyn.load(tbbMalloc, local = FALSE, now = TRUE)
22+
}
23+
}
1624

1725
# load the package library
1826
library.dynam("RcppParallel", pkgname, libname)
@@ -29,4 +37,7 @@ dllInfo <- NULL
2937
# unload tbb if we loaded it
3038
if (!is.null(dllInfo))
3139
dyn.unload(dllInfo[["path"]])
40+
# unload tbbmalloc if we loaded it
41+
if (!is.null(mallocDllInfo))
42+
dyn.unload(mallocDllInfo[["path"]])
3243
}

inst/include/RcppParallel/TBB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Common.h"
55

66
#include <tbb/tbb.h>
7+
#include <tbb/scalable_allocator.h>
78

89
namespace RcppParallel {
910

src/Makevars

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PKG_CPPFLAGS += -I../inst/include/
44
ifeq ($(OS), Windows_NT)
55

66
USE_TBB=Windows
7-
TBB_COPY_PATTERN=tbb.dll
7+
TBB_COPY_PATTERN=tbb*.dll
88

99
# Extra PATH entry for gcc (required by TBB)
1010
GCC_PATH = $(shell cygpath $(dir $(CC))):
@@ -14,7 +14,7 @@ ifeq ($(OS), Windows_NT)
1414
else
1515

1616
UNAME := $(shell uname)
17-
TBB_COPY_PATTERN=libtbb.*
17+
TBB_COPY_PATTERN=libtbb*.*
1818
ifeq ($(UNAME), Darwin)
1919
USE_TBB=Mac
2020
# We don't set this on OS X because it caused build failures on
@@ -34,7 +34,7 @@ ifdef USE_TBB
3434

3535
PKG_CPPFLAGS += -DRCPP_PARALLEL_USE_TBB=1
3636

37-
MAKE_ARGS := CXXFLAGS=-DTBB_NO_LEGACY=1 tbb_release tbb_build_prefix=lib
37+
MAKE_ARGS := CXXFLAGS=-DTBB_NO_LEGACY=1 tbb_release tbbmalloc_release tbb_build_prefix=lib
3838

3939
# What I really want is startswith but this doesn't appear to be available
4040
ifneq (,$(findstring clang,$(CC)))
@@ -56,7 +56,7 @@ ifeq ($(USE_TBB), Windows)
5656

5757
# Linker needs access to the tbb dll; otherwise you get errors such as:
5858
# "undefined reference to `tbb::task_scheduler_init::terminate()'"
59-
PKG_LIBS += -L../inst/lib/$(ARCH_DIR) -ltbb
59+
PKG_LIBS += -L../inst/lib/$(ARCH_DIR) -ltbb -ltbbmalloc
6060

6161
endif
6262

tests/testthat/cpp/malloc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include <Rcpp.h>
3+
using namespace Rcpp;
4+
5+
// [[Rcpp::depends(RcppParallel)]]
6+
#include <RcppParallel.h>
7+
using namespace RcppParallel;
8+
9+
// [[Rcpp::export]]
10+
void tbbMalloc() {
11+
12+
#if RCPP_PARALLEL_USE_TBB
13+
14+
std::vector<int,tbb::tbb_allocator<int> > vec;
15+
vec.push_back(42);
16+
17+
std::vector<int,tbb::scalable_allocator<int> > scalableVec;
18+
scalableVec.push_back(42);
19+
20+
std::vector<int,tbb::cache_aligned_allocator<int> > cacheAlignedVec;
21+
cacheAlignedVec.push_back(42);
22+
23+
#endif
24+
25+
}
26+

tests/testthat/pkg/RcppParallelTest/src/test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,22 @@ double parallelVectorSum(NumericVector x) {
4141
return sum.value;
4242
}
4343

44+
// [[Rcpp::export]]
45+
void testMalloc() {
46+
47+
#if RCPP_PARALLEL_USE_TBB
48+
49+
std::vector<int,tbb::tbb_allocator<int> > vec;
50+
vec.push_back(42);
51+
52+
std::vector<int,tbb::scalable_allocator<int> > scalableVec;
53+
scalableVec.push_back(42);
54+
55+
std::vector<int,tbb::cache_aligned_allocator<int> > cacheAlignedVec;
56+
cacheAlignedVec.push_back(42);
57+
58+
#endif
59+
60+
}
61+
4462

tests/testthat/test-malloc.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
context( "malloc" )
2+
3+
test_that( "tbbmalloc works", {
4+
sourceCpp( "cpp/malloc.cpp" )
5+
})
6+

0 commit comments

Comments
 (0)