4  OpenMP

{SLmetrics} supports parallelization through OpenMP. In this section this functionality is introduced.

4.1 Enabling/Disabling OpenMP

OpenMP is disabled by default, but can be enabled as follows:

SLmetrics::setUseOpenMP(TRUE)
#> OpenMP usage set to: enabled

And disabled as follows:

SLmetrics::setUseOpenMP(FALSE)
#> OpenMP usage set to: disabled

By default all cores are used. To control the amount of cores, see the following code:

SLmetrics::setNumberThreads(3)
#> Number of threads set to: 3

To use all cores:

SLmetrics::setNumberThreads(-1)
#> Number of threads set to: All (4 threads)

4.2 Benchmarking OpenMP

# 1) set seed for reproducibility
set.seed(1903)

# 2) create classification
# problem
fct_actual <- create_factor()
fct_predicted <- create_factor()
Code
SLmetrics::setUseOpenMP(TRUE)
#> OpenMP usage set to: enabled
Code
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.85ms           0B        0
Code
SLmetrics::setUseOpenMP(FALSE)
#> OpenMP usage set to: disabled
Code
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.6ms           0B        0