Politely select a number of threads to use. See Details for the algorithm used.
Details
There are two limits and we will take the smaller of the two.
The first limit comes from piggy-backing off of R's normal
parallel configuration; we will use the MC_CORES
environment
variable and mc.cores
option as a guide to how many cores you
are happy to use. We take mc.cores
first, then MC_CORES
, which
is the same behaviour as parallel::mclapply
and friends.
The second limit comes from OpenMP. If you do not have OpenMP
support, then we use one thread (higher numbers have no effect at
all in this case). If you do have OpenMP support, we take the
smallest of the number of "processors" (reported by
omp_get_num_procs()
) the "max threads" (reported by
omp_get_max_threads()
and "thread_limit" (reported by
omp_get_thread_limit()
.
See dust_openmp_support()
for the values of all the values that
go into this calculation.
Examples
# Default number of threads; tries to pick something friendly,
# erring on the conservative side.
dust_openmp_threads(NULL)
#> [1] 1
# Try to pick something silly and it will be reduced for you
dust_openmp_threads(1000, action = "fix")
#> ! Requested number of threads '1000' exceeds a limit of '1'
#> ℹ See `?dust_openmp_threads()()` for details
#> [1] 1