Home

Awesome

Using OpenBLAS in R for Windows

This is a short post illustrating how to easily get R for Windows to use OpenBLAS as the backend for linear algebra operations.

Most tutorials out there are about building R itself with OpenBLAS from source which is very complex, but this is not necessary as long as one is willing to make the installation some ~120mb heavier, which should not be a problem with today's hard disks.

In short: R comes with two DLL files Rblas.dll and Rlapack.dll, containing functions from BLAS and LAPACK, respectively. These can be replaced with other DLL files implementing the same interfaces, such as libopenblas.dll or libmkl_rt.dll. This tutorial illustrates how to replace these files with OpenBLAS'es more optimized versions.

What is OpenBLAS?

OpenBLAS is a BLAS (basic linear algebra sub-routines) and LAPACK (linear algebra package) backend, for performing fast linear algebra operations - examples: matrix multiplications, solving linear systems, calculating eigenvalues, among many others. These sound like trivial operations, but their execution speed can vary a lot across different software, so the specific backend used for them matters.

By default, R for Windows ships its own un-optimized BLAS and LAPACK replacements, which do the job in the sense that they offer all the necessary functionality (for R) and compute the results correctly, but are much slower than optimized BLASes as they do not exploit all the features of modern CPUs.

What difference does it make?

This is a time comparison of a simple operation (matrix multiplication) with the default R BLAS and with OpenBLAS:

library(microbenchmark)
set.seed(1)
m <- 1e4
n <- 1e3
k <- 3e2
X <- matrix(rnorm(m*k), nrow=m)
Y <- matrix(rnorm(n*k), ncol=n)
microbenchmark({
    Z <- X %*% Y
}, times=10L)

When ran on my own setup (AMD Ryzen 7 2700, 8c/16t, 3.2GHz), I obtain the following timings:

Unit: seconds
                 expr      min       lq     mean   median      uq      max neval
 {     Z <- X %*% Y } 2.035071 2.060824 2.072882 2.069541 2.09677 2.104811    10
Unit: milliseconds
                 expr     min      lq     mean  median       uq      max neval
 {     Z <- X %*% Y } 58.6489 71.2553 83.42615 72.7103 104.9017 110.6942    10

That is to say: using OpenBLAS made the operation almost 30x faster. No changes in the code were required, and it will speed up operations from CRAN packages which rely on BLAS too.

What you will need

The following will be required in order to follow the next steps:

Instructions in detail

image

At this point you're done and the next time you start R it will already be using OpenBLAS for accelerated linear algebra operations.

Controlling number of threads

OpenBLAS supports multi-threading, which can be controlled dynamically through the package RhpcBLASctl, or can be controlled through an environment variable OPENBLAS_NUM_THREADS. Importantly, this environment variable needs to be set before R is started, which can be done by setting it up through the windows control panel (important: changing it through Sys.setenv or through an .Renviron file has no effect, since those changes happen after the R executable is launched). This tutorial illustrates how to set the variable.

image