diff --git a/R/onAttach.R b/R/onAttach.R index e956271e..972d2edf 100644 --- a/R/onAttach.R +++ b/R/onAttach.R @@ -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." } diff --git a/R/predict.R b/R/predict.R index 0530654b..0b45c3ec 100644 --- a/R/predict.R +++ b/R/predict.R @@ -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") } diff --git a/R/ranger.R b/R/ranger.R index dc4dd823..eeaf80bf 100644 --- a/R/ranger.R +++ b/R/ranger.R @@ -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") }