Skip to content
Instructions for compiling R, Openblas and R binding with Openblas
Branch: master
Clone or download
Latest commit 9a87df0 Jul 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Feb 21, 2019
README.md Update Jul 9, 2019

README.md

Instructions for compiling R, OpenBLAS and linking R with OpenBLAS (GNU/Linux)

DEPENDENCES: make, cmake, gcc, gcc-fortran and tk.

Important: I'll be at all times assuming that the project R has been cloned into the directory ~/Downloads. Also, I will consider the /opt directory as the installation directory for the OpenBLAS library and of the R language. You can choose a directory of your choice.

Compiling OpenBLAS

Initially download the R and OpenBLAS (Open Optimized BLAS Library) source codes in OpenBLAS. In the file directory, perform the following steps.

cd $HOME/Downloads
tar -zxvf OpenBLAS*
cd OpenBLAs*
make -j $(nproc)
sudo make install

or

cd $HOME/Downloads
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS*
git checkout v0.3.6
make -j $(nproc)
sudo make install

Note: This will make the compilation run faster using all the features of your CPU. To know the number of cores, do: nproc. The default installation directory is /opt/OpenBLAS.

Compiling Armadillo C++ with OpenBLAS

For those who use C++ codes in R using the library Rcpp, configure the Armadillo with the library OpenBLAS be something fruitful.

cd $HOME/Downloads
tar -xvf armadillo*
cd armadillo*
export LD_LIBRARY_PATH=/opt/OpenBLAS/lib/
./configure -DCMAKE_INSTALL_PREFIX=/opt/armadillo
cmake . -DCMAKE_INSTALL_PREFIX=/opt/armadillo
make -j $(nproc)
sudo make install

Note: Further details regarding the compilation of the library Armadillo can be found at https://gitlab.com/conradsnicta/armadillo-code.

Compiling R with OpenBLAS

After compiling OpenBLAS, download the R code. It is not necessary to compile R to make use of OpenBLAS, but compiling the language may bring some benefits that may be insignificant depending on what is being done in R. That way, download the source code of the language R.

Note: In my operating system, Arch Linux, OpenBLAS was installed in the /opt directory. Search for the OpenBLAS installation directory in your GNU/Linux distribution.

In the directory where the R was downloaded, do the following:

tar -zxvf R*
cd R-* 
export LD_LIBRARY_PATH=/opt/OpenBLAS/lib/
./configure --prefix=/opt/R/3.6.0 --enable-R-shlib --enable-threads=posix --with-blas="-lopenblas -L/opt/OpenBLAS/lib -I/opt/OpenBLAS/include -m64 -lpthread -lm"
make -j $(nproc)
sudo make install

Note: The language R will be configured and installed in the /opt/R/3.6.0 directory. Replace 3.6.0 by the version of R being installed. This allows us to have multiple versions of R in /opt/R/.

Most likely the OpenBLAS library will be bound to R. To check, run in the R the sessionInfo() code. Something like the output below should appear:

Matrix products: default
BLAS/LAPACK: /opt/OpenBLAS/lib/libopenblas_haswellp-r0.3.6.so

If linking does not occur, follow the steps outlined in the code below.

We need to link the R with the file libopenblas_*, created in the process of compiling the library OpenBLAS. In my case, the file is libopenblas_haswellp-r0.3.6.so. Look for this in /opt/OpenBLAS/lib or in the directory where OpenBLAS was installed on your GNU/Linux system. Also look for the libRblas.so file directory found in the R language installation directory. In Arch, this directory is /usr/local/lib64/R/lib.

cd /usr/local/lib64/R/lib
sudo mv libRblas.so libRblas.so.keep
sudo ln -s /opt/OpenBLAS/lib/libopenblas_haswellp-r0.3.6.so libRblas.so

Start a section of language R and do sessionInfo(). You should note something like:

Matrix products: default
BLAS/LAPACK: /opt/OpenBLAS/lib/libopenblas_haswellp-r0.3.6.so

Installing RStudio and binding to compiled R in (Arch Linux)

sudo ln -sf /opt/R/3.6.0/lib64/R/bin/R /bin/R
sudo ln -sf /opt/R/3.6.0/lib64/R/bin/Rscript /bin/Rscript 

Now install the rstudio-desktop-bin package from the AUR repository. When prompted, edit the PKGBUILD file and remove the R from dependencies.

For distributions based on Arch Linux

All steps, for distributions based on Arch Linux, could be summarized in:

yay -S openblas-lapack --noconfirm
sudo pacman -S r

Installing R from the form just above is much simpler. However, the advantage of compiling and installing R in the /opt directory is that we can have several configured versions of the language.

Multithreaded processing in R

To make use of multithreaded processing, do export OPENBLAS_NUM_THREADS=1 before starting a R section.

export OPENBLAS_NUM_THREADS=1
export GOTO_NUM_THREADS=1
export OMP_NUM_THREADS=1

NOTE: For intel processors,sudo cpupower frequency-set -g performance, can boost performance. Read more at https://wiki.archlinux.org/index.php/CPU_frequency_scaling.

Running R code in bash terminal

The two traditional ways to execute an R code in the bash terminal is by using the codes below:

Rscript code.R

or

R CMD BATCH code.R

Another way to execute the R code is to introduce #!/usr/bin/env Rscript in the first line of the file code.R. Thus, we can execute the code as follows:

./code.R

Note: You must give permission to execute the file code.R. So, make sudo chmod +x code.R.

You can’t perform that action at this time.