This function estimates the number of CPU cores that can be safely used for parallel processing, taking into account a minimum threshold, the total number of physical cores, and currently active threads.

numCoresToUse(min = 2, max = NULL)

Arguments

min

An integer specifying the minimum number of cores to use. Default is 2.

max

An integer specifying the maximum number of cores available, typically the number of physical cores. Default is max(1L, getOption("Ncpus", 1L), parallel::detectCores() - 1, logical = FALSE, na.rm = TRUE).

Value

An integer representing the number of cores that can be used for parallel tasks, ensuring at least min cores are used, while subtracting one for the current process and an estimate of actively used threads (via detectActiveCores()).

Note

This function depends on detectActiveCores() and is not supported on Windows systems.

Examples

if (FALSE) {
  numCoresToUse()
  numCoresToUse(min = 4)
}