Skip to content

Commit

Permalink
default to 2 threads but give a startup message
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Dec 6, 2023
1 parent 24a24bf commit 93dd105
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions R/onAttach.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

.onAttach = function(libname, pkgname) {
if (!interactive()) {
return()
}

threads_option <- getOption("ranger.num.threads")
threads_env <- Sys.getenv("R_RANGER_NUM_THREADS")
if (!is.null(threads_option)) {
thread_string <- paste(threads_option, "threads (set by options(ranger.num.threads = N).")
} else if (threads_env != "") {
thread_string <- paste(threads_env, "threads (set by environment variable 'R_RANGER_NUM_THREADS').")
} else {
thread_string <- "2 threads (default). Change with num.threads in ranger() and predict(), options(ranger.num.threads = N) or environment variable 'R_RANGER_NUM_THREADS'."
}

packageStartupMessage(paste("ranger", packageVersion("ranger"), "using", thread_string))
}
2 changes: 1 addition & 1 deletion R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ predict.ranger.forest <- function(object, data, predict.all = FALSE,
## Num threads
## Default 0 -> detect from system in C++.
if (is.null(num.threads)) {
num.threads = 0
num.threads <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", 2L)))
} else if (!is.numeric(num.threads) || num.threads < 0) {
stop("Error: Invalid value for num.threads")
}
Expand Down
2 changes: 1 addition & 1 deletion R/ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL,
## Num threads
## Default 0 -> detect from system in C++.
if (is.null(num.threads)) {
num.threads = 0
num.threads <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", 2L)))
} else if (!is.numeric(num.threads) || num.threads < 0) {
stop("Error: Invalid value for num.threads")
}
Expand Down

0 comments on commit 93dd105

Please sign in to comment.