::openmp.on() SLmetrics
#> OpenMP enabled!
{SLmetrics} supports parallelization through OpenMP. In this section this functionality is introduced.
OpenMP is disabled by default
, but can be enabled as follows:
::openmp.on() SLmetrics
#> OpenMP enabled!
And disabled as follows:
::openmp.off() SLmetrics
#> OpenMP disabled!
By default
all threads are used. To control the amount of threads, see the following code:
::openmp.threads(3) SLmetrics
#> Using 3 threads.
To use all threads:
::openmp.threads(NULL) SLmetrics
#> Using 4 threads.
The number of available threads are detected automatically, but can also be viewed using SLmetrics::openmp.threads()
without passing any arguments. See below:
::openmp.threads() SLmetrics
#> [1] 4
To benchmark the performance gain on enabling OpenMP, the same setup as in Chapter 3 is used. Below is the actual
and predicted
values are generated.
# 1) set seed for reproducibility
set.seed(1903)
# 2) create classification
# problem
<- create_factor()
fct_actual <- create_factor() fct_predicted
::openmp.on()
SLmetrics
benchmark(
`With OpenMP` = SLmetrics::cmatrix(fct_actual, fct_predicted)
)
#> # A tibble: 1 × 4
#> expression execution_time memory_usage gc_calls
#> <fct> <bch:tm> <bch:byt> <dbl>
#> 1 With OpenMP 3.79ms 0B 0
::openmp.off()
SLmetrics
benchmark(
`Wihtout OpenMP` = SLmetrics::cmatrix(fct_actual, fct_predicted)
)
#> # A tibble: 1 × 4
#> expression execution_time memory_usage gc_calls
#> <fct> <bch:tm> <bch:byt> <dbl>
#> 1 Wihtout OpenMP 8.54ms 0B 0
Enabling OpenMP support can decrease computation time significantly - but it should only be used consciously, and with care, to avoid function calls competing for the same threads. This is especially the case if you are running a, say, neural network in parallel.