Skip to content

Commit

Permalink
also use Ncpus option
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Dec 6, 2023
1 parent a0e8fcd commit 33ea5da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions R/onAttach.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
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 as set by options(ranger.num.threads = N). Can be overwritten with num.threads.")
} else if (threads_env != "") {
threads_option1 <- getOption("ranger.num.threads")
threads_option2 <- getOption("Ncpus")

if (threads_env != "") {
thread_string <- paste(threads_env, "threads as set by environment variable R_RANGER_NUM_THREADS. Can be overwritten with num.threads.")
} else if (!is.null(threads_option1)) {
thread_string <- paste(threads_option1, "threads as set by options(ranger.num.threads = N). Can be overwritten with num.threads.")
} else if (!is.null(threads_option2)) {
thread_string <- paste(threads_option2, "threads as set by options(Ncpus = N). Can be overwritten with 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."
}
Expand Down
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 <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", 2L)))
num.threads <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", getOption("Ncpus", 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 <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", 2L)))
num.threads <- as.integer(Sys.getenv("R_RANGER_NUM_THREADS", getOption("ranger.num.threads", getOption("Ncpus", 2L))))
} else if (!is.numeric(num.threads) || num.threads < 0) {
stop("Error: Invalid value for num.threads")
}
Expand Down

0 comments on commit 33ea5da

Please sign in to comment.