-
Notifications
You must be signed in to change notification settings - Fork 57
/
hooks.R
48 lines (40 loc) · 1.3 KB
/
hooks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
dllInfo <- NULL
mallocDllInfo <- NULL
.onLoad <- function(libname, pkgname) {
# load tbb and tbbmalloc on supported platforms
tbb <- tbbLibPath()
if (!is.null(tbb)) {
if (!file.exists(tbb)) {
warning(paste("TBB library", tbb, "not found."))
} else {
dllInfo <<- dyn.load(tbb, local = FALSE, now = TRUE)
}
}
tbbMalloc <- tbbLibPath("malloc")
if (!is.null(tbbMalloc)) {
if (!file.exists(tbbMalloc)) {
warning(paste("TBB malloc library", tbbMalloc, "not found."))
} else {
mallocDllInfo <<- dyn.load(tbbMalloc, local = FALSE, now = TRUE)
}
}
# load the package library
library.dynam("RcppParallel", pkgname, libname)
# set default thread options
numThreads <- "auto"
numThreadsEnv <- Sys.getenv("RCPP_PARALLEL_NUM_THREADS", unset = NA)
if (!is.na(numThreadsEnv))
setThreadOptions(numThreads = as.integer(numThreadsEnv))
else
setThreadOptions(numThreads = "auto")
}
.onUnload <- function(libpath) {
# unload the package library
library.dynam.unload("RcppParallel", libpath)
# unload tbb if we loaded it
if (!is.null(dllInfo))
dyn.unload(dllInfo[["path"]])
# unload tbbmalloc if we loaded it
if (!is.null(mallocDllInfo))
dyn.unload(mallocDllInfo[["path"]])
}