sudo apt-get update
sudo apt-get install libopenblas-base r-base
Compiling R with multi-threaded linear algebra libraries on Ubuntu
R
Linux
A guide for multithreaded R performance
Updated 2022-05-28: Updated links.
Motivation
You may have an R setup that is not using hardware resources in an efficient way.
Linear algebra libraries such as OpenBLAS and Intel MKL can compute matrix/vector operations with all available processing power to significantly reduce computation times.
Use apt-get instead of compiling sources
If you prefer to use apt-get
there is an option. Intel MKL is hardware specific while OpenBLAS boosts operations both for Intel and AMD
To install R linked to Intel MKL, Dirk Eddelbuettel’s blog provides detailed instructions.
To install R linked to OpenBLAS you only need this:
Compiling sources
I’ll stick to R 3.4.2 that is what I use at work.
R linked to MKL
You need to install MKL first. You can either use apt-get
or download the installer. I have downloaded the installer.
Install MKL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Option 1: Use apt-get | |
# keys taken from https://software.intel.com/en-us/articles/installing-intel-free-libs-and-python-apt-repo | |
cd ~/GitHub/r-with-intel-mkl/ | |
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB | |
apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB | |
sudo sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list' | |
sudo apt-get update && sudo apt-get install intel-mkl-64bit | |
# Option 2: Use the installer (works well on Ubuntu 16.04) | |
cd ~/GitHub/r-with-intel-mkl/ | |
wget http://registrationcenter-download.intel.com/akdlm/irc_nas/tec/12147/l_mkl_2017.4.239.tgz | |
tar xzvf l_mkl_2017.4.239.tgz | |
cd l_mkl_2017.4.239 | |
sudo ./install_GUI.sh | |
printf '/opt/intel/lib/intel64\n/opt/intel/mkl/lib/intel64\n' | sudo tee -a /etc/ld.so.conf.d/intel_mkl.conf | |
sudo ldconfig |
Compile R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1: Add RStudio to apt sources | |
# key added after sudo apt-get update returned a warning following this guide: https://support.rstudio.com/hc/en-us/articles/218004217-Building-R-from-source | |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9 | |
printf '#CRAN mirror\ndeb https://cran.rstudio.com/bin/linux/ubuntu artful/\ndeb-src https://cran.rstudio.com/bin/linux/ubuntu artful/\n' | sudo tee -a /etc/apt/sources.list.d/cran-mirror.list | |
# 2: Enable development repositories | |
# you need to enable multiverse repo or packages as xvfb won't be found | |
printf 'deb http://us.archive.ubuntu.com/ubuntu artful main restricted universe multiverse | |
deb-src http://us.archive.ubuntu.com/ubuntu artful main restricted universe multiverse\n | |
deb http://security.ubuntu.com/ubuntu artful-security main restricted universe multiverse | |
deb-src http://security.ubuntu.com/ubuntu artful-security main restricted universe multiverse\n | |
deb http://us.archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse | |
deb-src http://us.archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse\n' | sudo tee -a /etc/apt/sources.list | |
# 3: Update packages | |
sudo apt-get update | |
sudo apt-get clean | |
sudo apt-get autoclean | |
sudo apt-get autoremove | |
sudo apt-get upgrade --with-new-pkgs | |
# 4: Build R from source | |
sudo apt-get build-dep r-base | |
cd ~/GitHub/r-with-intel-mkl | |
wget https://cran.r-project.org/src/base/R-3/R-3.4.2.tar.gz | |
tar xzvf R-3.4.2.tar.gz | |
cd R-3.4.2 | |
source /opt/intel/mkl/bin/mklvars.sh intel64 | |
BLAS="-L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl" | |
./configure --prefix=/opt/R/3.4.2-mkl --enable-shared --enable-R-shlib --with-blas="$BLAS" --with-lapack | |
make && sudo make install | |
printf '\nexport RSTUDIO_WHICH_R=/usr/local/bin/R\nexport RSTUDIO_WHICH_R=/opt/R/3.4.2-mkl\n' | tee -a ~/.profile | |
sudo ln -s /opt/R/3.4.2-mkl/bin/R /usr/local/bin/R |
R linked to OpenBLAS
Compile R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1: Add RStudio to apt sources | |
# key added after sudo apt-get update returned a warning following this guide: https://support.rstudio.com/hc/en-us/articles/218004217-Building-R-from-source | |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9 | |
printf '#CRAN mirror\ndeb https://cran.rstudio.com/bin/linux/ubuntu artful/\ndeb-src https://cran.rstudio.com/bin/linux/ubuntu artful/\n' | sudo tee -a /etc/apt/sources.list.d/cran-mirror.list | |
# 2: Enable development repositories | |
# you need to enable multiverse repo or packages as xvfb won't be found | |
printf 'deb http://us.archive.ubuntu.com/ubuntu artful main restricted universe multiverse | |
deb-src http://us.archive.ubuntu.com/ubuntu artful main restricted universe multiverse\n | |
deb http://security.ubuntu.com/ubuntu artful-security main restricted universe multiverse | |
deb-src http://security.ubuntu.com/ubuntu artful-security main restricted universe multiverse\n | |
deb http://us.archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse | |
deb-src http://us.archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse\n' | sudo tee -a /etc/apt/sources.list | |
# 3: Update packages | |
sudo apt-get update | |
sudo apt-get clean | |
sudo apt-get autoclean | |
sudo apt-get autoremove | |
sudo apt-get upgrade --with-new-pkgs | |
# 4: Build R from source | |
sudo apt-get build-dep r-base libopenblas-base | |
cd ~/GitHub/r-with-intel-mkl/ | |
wget https://cran.r-project.org/src/base/R-3/R-3.4.2.tar.gz | |
tar xzvf R-3.4.2.tar.gz | |
cd R-3.4.2 | |
./configure --prefix=/opt/R/R-3.4.2-openblas --enable-R-shlib --with-blas --with-lapack | |
make && sudo make install | |
printf 'export RSTUDIO_WHICH_R=/opt/R/R-3.4.2-openblas/bin/R\n' | tee -a ~/.profile | |
sudo ln -s /opt/R/3.4.2-openblas/bin/R /usr/local/bin/R |