Brett Klamer

Faster BLAS in R

R uses BLAS and LAPACK libraries for linear algebra operations. Because the default libraries have not been properly optimized, you could see large increases in speed, for code that is dependent on linear algebra computations, by switching to a different BLAS library. The traditional open source solutions for this have been ATLAS, GotoBLAS (no longer developed), and more recently OpenBLAS (GotoBLAS2 fork).

The Intel MKL, another linear algebra library, is provided with Revolution Analytic's offerings. The Intel MKL is nice, as it's designed for Intel processors by Intel, but it requires licensing fees and doesn't have as great performance with AMD. Revolution Analytics likes to pretend this makes their product quite special, but you can make plain old R just as good with the open source libraries.

Normally you would compile R and the BLAS libraries from source for the best optimization. However, the process for doing this is dynamic and can result in unexpected behavior. Even using reference BLAS libraries outside of R's default may not be the best (see this). With that in mind, the following should provide an easy and conservative approach to replacing the standard BLAS.

Faster BLAS in R for Ubuntu

In Ubuntu, you have two free and open source solutions, ATLAS and OpenBLAS, and one closed source option, Revolution R Open with the Intel MKL. To install ATLAS and OpenBLAS, use

# install OpenBLAS
sudo apt-get install libopenblas-base
# install ATLAS
sudo apt-get install libatlas3-base liblapack3

Fortunately, if you install both, you can easily switch between them using

sudo update-alternatives --config libblas.so.3

which should show

@ubuntu:~$ sudo update-alternatives --config libblas.so.3
There are 3 choices for the alternative libblas.so.3 (providing /usr/lib/libblas.so.3).

  Selection    Path                                    Priority   Status
------------------------------------------------------------
* 0            /usr/lib/openblas-base/libblas.so.3      40        auto mode
  1            /usr/lib/atlas-base/atlas/libblas.so.3   35        manual mode
  2            /usr/lib/libblas/libblas.so.3            10        manual mode
  3            /usr/lib/openblas-base/libblas.so.3      40        manual mode

Press enter to keep the current choice[*], or type selection number:

When using ATLAS, you will probably want to choose its own LAPACK. Run

sudo update-alternatives --config liblapack.so.3

which should show

@ubuntu:~$ sudo update-alternatives --config liblapack.so.3
There are 2 choices for the alternative liblapack.so.3 (providing /usr/lib/liblapack.so.3).

  Selection    Path                                      Priority   Status
------------------------------------------------------------
* 0            /usr/lib/lapack/liblapack.so.3             10        auto mode
  1            /usr/lib/atlas-base/atlas/liblapack.so.3   5         manual mode
  2            /usr/lib/lapack/liblapack.so.3             10        manual mode

Press enter to keep the current choice[*], or type selection number:

To install Revolution R, see their install guide.

Faster BLAS in R for Windows

It's a bit tougher for Windows. I don't know of any current precompiled ATLAS or OpenBLAS files that can be dropped in. There are two sources of older optimized BLAS files, here and here, but it's probably not something you want to rely on. The only easy choice is using Revolution R. Of course, if you were curious, you would find that the Intel MKL libraries are located at

C:\Program Files\RRO\R-3.1.1\bin\x64\libiomp5md.dll
C:\Program Files\RRO\R-3.1.1\bin\x64\Rblas.dll
C:\Program Files\RRO\R-3.1.1\bin\x64\Rlapack.dll

and these three files can be dropped in and replace the Rblas.dll and Rlapack.dll files for the open source R at

C:\Program Files\R\R-3.1.1\bin\x64\

But, you would never do that considering the Intel MKL license agreement.

Intel(R) Math Kernel Library
----------------------------

The proprietary third party component included in the software offering is
known as the Intel(R) Math Kernel Library (the "MKL").  The MKL is distributed
to you by Revolution Analytics. The MKL is provided "AS IS" without warranty
of any kind. 


You are prohibited from (a) disassembly and reverse engineering of the MKL
files, (b) using the MKL files in conjunction with any software other than
Revolution R Open and (c) removing any copyright or other proprietary notices
from the MKL files. The files affected by these restrictions are as follows: 

Benchmarks

Some artificial benchmarks using the following scripts:

This one is also interesting (from here). All were run using R 3.1.1 or Revolution R Open 8.0 Beta, Ubuntu 14.04 or Windows 8.1, and on an Intel i5-3320M.

R-benchmark-25.R Total time (sec)
Windows Intel MKL 6.95
Ubuntu Intel MKL 7.06
Ubuntu OpenBLAS 7.41
Ubuntu ATLAS 10.37
Windows default 32.47
Ubuntu default 32.89
revo-script.R Library elapsed (sec)
Matrix Multiply Windows Intel MKL 5.81
Ubuntu OpenBLAS 5.99
Ubuntu Intel MKL 6.08
Ubuntu ATLAS 21.37
Ubuntu default 122.44
Windows default 126.43
Cholesky Ubuntu Intel MKL 1.09
Windows Intel MKL 1.11
Ubuntu OpenBLAS 1.49
Ubuntu ATLAS 3.75
Ubuntu default 19.74
Windows default 20.45
SVD Ubuntu Intel MKL 5.26
Windows Intel MKL 5.41
Ubuntu OpenBLAS 5.91
Ubuntu ATLAS 13.88
Windows default 39.21
Ubuntu default 41.78
PCA Ubuntu Intel MKL 13.81
Windows Intel MKL 14.23
Ubuntu OpenBLAS 15.03
Ubuntu ATLAS 41.21
Windows default 142.35
Ubuntu default 153.66
LDA Ubuntu Intel MKL 22.43
Ubuntu OpenBLAS 23.25
Windows Intel MKL 23.66
Ubuntu ATLAS 41.09
Ubuntu default 92.82
Windows default 104.58

Not surprisingly, the Intel MKL libraries compiled by Revolution Analytics are the fastest. However, using OpenBLAS in Linux has almost the same performance without the licensing restrictions and fees. ATLAS lags behind a bit as it didn't seem to make use of both CPU cores. Don't disregard ATLAS too quickly though. It can be useful as a backup to OpenBLAS for any numerical difficulties.

Last updated 2014-11-16 - Feel free to email me with any questions or comments.