File: options.R

package info (click to toggle)
r-cran-rcppparallel 5.1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 544 kB
  • sloc: cpp: 1,848; sh: 19; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#' Thread options for RcppParallel
#' 
#' Set thread options (number of threads to use for task scheduling and stack
#' size per-thread) for RcppParallel.
#' 
#' RcppParallel is automatically initialized with the default number of threads
#' and thread stack size when it loads. You can call `setThreadOptions()` at
#' any time to change the defaults.
#' 
#' The `parallelFor()` and `parallelReduce()` also accept `numThreads` as
#' an argument, if you'd like to control the number of threads specifically
#' to be made available for a particular parallel function call. Note that
#' this value is advisory, and TBB may choose a smaller number of threads
#' if the number of requested threads cannot be honored on the system.
#' 
#' @aliases setThreadOptions defaultNumThreads
#' 
#' @param numThreads
#'   Number of threads to use for task scheduling. Call `defaultNumThreads()`
#'   to determine the the default value used for "auto".
#'   
#' @param stackSize
#'   Stack size (in bytes) to use for worker threads. The
#'   default used for "auto" is 2MB on 32-bit systems and 4MB on 64-bit systems
#'   (note that this parameter has no effect on Windows).
#'   
#' @return
#'   `defaultNumThreads()` returns the default number of threads used by
#'   RcppParallel, if another value isn't specified either via
#'   `setThreadOptions()` or explicitly in calls to `parallelFor()` and
#'   `parallelReduce()`.
#'
#' @examples
#' 
#' \dontrun{
#' library(RcppParallel)
#' setThreadOptions(numThreads = 4)
#' defaultNumThreads()
#' }
#' 
#' @export setThreadOptions
setThreadOptions <- function(numThreads = "auto",
                             stackSize = "auto")
{
   # validate and resolve numThreads
   if (identical(numThreads, "auto"))
      numThreads <- -1L
   else if (!is.numeric(numThreads))
      stop("numThreads must be an integer")
   else
      numThreads <- as.integer(numThreads)
   
   # validate and resolve stackSize
   if (identical(stackSize, "auto"))
      stackSize <- 0L
   else if (!is.numeric(stackSize))
      stop("stackSize must be an integer")
   else
      stackSize <- as.integer(stackSize)
   
   # set RCPP_PARALLEL_NUM_THREADS
   if (numThreads == -1L)
      Sys.unsetenv("RCPP_PARALLEL_NUM_THREADS")
   else
      Sys.setenv(RCPP_PARALLEL_NUM_THREADS = numThreads)
   
   # set RCPP_PARALLEL_STACK_SIZE
   if (stackSize == 0L)
      Sys.unsetenv("RCPP_PARALLEL_STACK_SIZE")
   else
      Sys.setenv(RCPP_PARALLEL_STACK_SIZE = stackSize)
}

#' @rdname setThreadOptions
#' @export
defaultNumThreads <- function() {
   .Call("defaultNumThreads", PACKAGE = "RcppParallel")
}

isUsingTbb <- function() {
   backend <- Sys.getenv("RCPP_PARALLEL_BACKEND", "tbb")
   identical(backend, "tbb")
}