Skip to contents

Introduction

AlphaSimR leverages OpenMP-type parallelization to speed up execution of some underlying C++ code called by R code. This speed-up can be substantial for large-scale simulations. Depending on the version and source of your installed R and AlphaSimR, OpenMP might or might not be supported out of the box. In our experience, it is usually available automatically on Windows and Linux. macOS users most often need to take extra steps. We show below how to check if you can use multiple threads and how these get used in AlphaSimR. We also point to more information on installation/building to enable OpenMP.

Check whether OpenMP is available

The simplest check is via getNumThreads().

## [1] 4

If OpenMP is not available (or when you only have one available thread), getNumThreads() returns 1 and all code will run single-threaded. When getNumThreads() returns more than 1, some code will run in parallel.

Controlling the number of threads

When OpenMP is enabled, AlphaSimR uses all the available threads by default. You can control the number of threads used via the nThreads argument globally in the SP object or individually in some functions.

founderPop = quickHaplo(nInd = 4, nChr = 2, segSites = 5)
SP = SimParam$new(founderPop)
SP$nThreads
## [1] 4

Assuming that you have more than one thread, but want to use just one globally, you can specify that in the SP object:

SP$nThreads = 1L
pop = newPop(founderPop, simParam = SP)
pullSegSiteGeno(pop)
##   1_1 1_2 1_3 1_4 1_5 2_1 2_2 2_3 2_4 2_5
## 1   0   1   2   1   1   1   2   1   1   1
## 2   1   2   1   2   1   1   0   1   1   0
## 3   1   0   2   0   0   0   0   2   0   0
## 4   0   2   2   2   0   1   2   2   1   2
# Note that pullSegSiteGeno() uses internally SP$nThreads,
# when the nThreads argument is not specified.

As indicated above, we can specify the number of threads in some functions:

pullSegSiteGeno(pop, nThreads = 1L)
##   1_1 1_2 1_3 1_4 1_5 2_1 2_2 2_3 2_4 2_5
## 1   0   1   2   1   1   1   2   1   1   1
## 2   1   2   1   2   1   1   0   1   1   0
## 3   1   0   2   0   0   0   0   2   0   0
## 4   0   2   2   2   0   1   2   2   1   2

The number of threads only changes how fast AlphaSimR runs, never its results. Work is split into pieces whose number depends on the size of the problem rather than on the number of threads, and each piece draws its own random numbers, so a simulation run from the same seed gives the same results whatever nThreads is.

A demonstration

There are several AlphaSimR functions that leverage parallelization, particularly when working across chromosomes or with linear algebra operations. runMacs() is one such example. The code below reports time for simulating genomes with 10 chromosomes for 50 individuals in serial mode and in parallel mode. You can evaluate the speed-up due to parallelization by changing the number of chromosomes, and also increasing the size of the simulation by increasing the number of individuals. The parallel run below uses at most two threads, because that is the limit when this vignette is built on CRAN; replace min(2L, getNumThreads()) with getNumThreads() to use every available thread.

system.time(runMacs(nInd = 50, nChr = 10, nThreads = 1L))
##    user  system elapsed 
##   0.961   0.000   0.961
system.time(runMacs(nInd = 50, nChr = 10, nThreads = min(2L, getNumThreads())))
##    user  system elapsed 
##   0.878   0.001   0.469

How to get OpenMP support

This topic depends on your platform and R toolchain, so we keep the guidance short and point to the relevant docs:

A note on BLAS

For linear algebra-heavy work (such as fitting genomic prediction models), a multithreaded BLAS can also help. This is separate from OpenMP support. In our experience, this is even more platform- and R-build-specific than OpenMP, so it is best treated as an advanced local optimization.