Skip to contents

Politely select a number of threads to use. See Details for the algorithm used.

Usage

dust_openmp_threads(n = NULL, action = "error")

Arguments

n

Either NULL (select automatically) or an integer as your proposed number of threads.

action

An action to perform if n exceeds the maximum number of threads you can use. Options are "error" (the default, throw an error) or "fix" (print a message and reduce n down to the limit).

Value

An integer, indicating the number of threads that you can use

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