set_threads function

Set the Number of Threads

Set the Number of Threads

Control the parallelism via threading while calling external packages from list("mlr3").

For example, the random forest implementation in package list("ranger") (connected via list("mlr3learners")) supports threading via OpenMP. The number of threads to use can be set via hyperparameter num.threads, and defaults to 1. By calling set_threads(x, 4) with x being a ranger learner, the hyperparameter is changed so that 4 cores are used.

If the object x does not support threading, x is returned as-is. If applied to a list, recurses through all list elements.

Note that threading is incompatible with other parallelization techniques such as forking via the future::plan future::multicore . For this reason all learners connected to list("mlr3")

have threading disabled in their defaults.

set_threads(x, n = availableCores(), ...) ## Default S3 method: set_threads(x, n = availableCores(), ...) ## S3 method for class 'R6' set_threads(x, n = availableCores(), ...) ## S3 method for class 'list' set_threads(x, n = availableCores(), ...)

Arguments

  • x: (any)

    Object to set threads for, e.g. a Learner . This object is modified in-place.

  • n: (integer(1))

    Number of threads to use. Defaults to parallelly::availableCores().

  • ...: (any)

    Additional arguments.

Returns

Same object as input x (changed in-place), with possibly updated parameter values.