Skip to contents

Converts either an absolute thread count or a proportion of available CPU cores into an integer number of worker threads. The number of available cores is determined by parallel::detectCores().

Usage

set_threads(x)

Arguments

x

A numeric scalar. Values greater than or equal to 1 specify an absolute number of threads and are rounded to the nearest integer. Values strictly between 0 and 1 specify a proportion of available cores; the resulting number of threads is also rounded to the nearest integer.

Value

An integer-like numeric scalar giving the normalized number of worker threads. A message reports the selected and available thread counts.

Details

Absolute thread counts exceeding the number of cores reported by parallel::detectCores() are silently capped to that limit with a message. Values less than or equal to zero and non-numeric inputs produce an error.

This function is also used by refine_records() to normalize its threads argument.

Examples

set_threads(1)
#> 1/4 threads used
#> [1] 1
set_threads(0.5)
#> 2/4 threads used
#> [1] 2