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 check the currently used LAPACK and BLAS within an R session, run
# These functions will not work on windows OS La_library() extSoftVersion()["BLAS"]
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
Rlapack.dll files for the open source R at
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:
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|
|Matrix Multiply||Windows Intel MKL||5.81|
|Ubuntu Intel MKL||6.08|
|Cholesky||Ubuntu Intel MKL||1.09|
|Windows Intel MKL||1.11|
|SVD||Ubuntu Intel MKL||5.26|
|Windows Intel MKL||5.41|
|PCA||Ubuntu Intel MKL||13.81|
|Windows Intel MKL||14.23|
|LDA||Ubuntu Intel MKL||22.43|
|Windows Intel MKL||23.66|
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 2018-11-25