Quick Linking Intel® MKL BLAS, LAPACK to R

Overview

This article describes a way to force R to use Intel® oneAPI Math Kernel Library instead of default R BLAS and LAPACK functionality.

Prerequisites:

  • Intel® oneAPI Math Kernel Library

It contains highly optimized BLAS, LAPACK as well as statistical functionality of direct application to R. More information on Intel® oneAPI Math Kernel Library can be found here: Intel® oneAPI Math Kernel Library

The article is based on Intel® oneAPI Math Kernel Library 2021.3 for Linux* (and later versions) and R-4.0.5.tar.gz

Linking Intel MKL to R

The BLAS library will be used for many of the add-on packages as well as for R itself. R offers the option of compiling the BLAS into a dynamic library libRblas stored in R_HOME/lib and linking both R itself and all the other add-on packages against that library. This is the default on all platforms except IBM AIX*. Therefore it will be easy for most developers to change the BLAS without needing to re-install R and all the add-on packages, since all references to the BLAS go through libRblas, and that can be replaced. R project shows a simple way to change the BLAS by using symlink a dynamic BLAS library (such as ACML or Goto’s) to R_HOME/lib/libRblas.so. in their documentation located at http://cran.r-project.org/doc/manuals/r-release/R-admin.html#Shared-BLAS

In this article, we will illustrate the same way to link the Intel MKL BLAS library to R. Please follow the below instructions, to build R with default BLAS, LAPACK using gnu compiler chain.

$ tar -xzvf R-4.0.5.tar.gz

$ cd R-4.0.5

$ ./configure

(or $./configure --with-readline=no --with-x=no if package readline and X11 is not installed)

$make

(not $ make install, so, we do not pollute system directory)

$ ldd bin/exec/R

(To make sure it will link libRblas.so although it may show that libRblas.so => not found)

For developers who have installed R, please locate the path of libRblas.so and
libRlapack.so (or libR.so), for example, $cd /usr/local/lib64/R

$ cd lib

$ mv libRblas.so libRblas.so.keep

$ln –s $(MKLROOT)/mkl/lib/intel64/libmkl_rt.so libRblas.so

The same way, you can replace the LAPACK libRlapack.so library too

($mv libRlapack.so libRlapack.so.keep
$ln –s $(MKLROOT)/mkl/lib/intel64/libmkl_rt.so libRlapack.so)

If you have prebuilt R with libR.so, replace it with
$(MKLROOT)/mkl/lib/intel64/libmkl_rt.so

Since R is now using Intel® oneAPI Math Kernal Library, to complete preparations before running R scripts, you need to set the environment by sourcing the environment script vars.sh ( $MKLROOT/env/vars.sh).

Because R uses GNU OpenMP multithread library libgomp.so, and Intel® oneAPI Math Kernal Library uses Intel® OpenMP multithread library, the latter provides the flexibility of supporting GNU threading layer by setting certain environment variables as explained in the Intel® oneAPI Math Kernal Library developer reference

Please set the MKL interface and threading layer to GNU and LP64:

$export MKL_INTERFACE_LAYER=GNU,LP64

$export MKL_THREADING_LAYER=GNU

Now, you’re ready to run R scripts.

Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.