This article describes a way to force R to use Intel® oneAPI Math Kernel Library (oneMKL) instead of default R BLAS and LAPACK functionality.
- Intel® oneAPI Math Kernel Library
It contains highly optimized BLAS, LAPACK as well as statistical functionality of direct application to R. More information on oneMKL can be found here: Intel® oneAPI Math Kernel Library
- Download the R package - http://www.r-project.org/
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
(or $./configure --with-readline=no --with-x=no if package readline and X11 is not installed)
(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
Since R is now using oneMKL, 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 oneMKL uses 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 Kernel Library developer reference.
Please set the MKL interface and threading layer to GNU and LP64:
Now, you’re ready to run R scripts.
Product and Performance Information
Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.