Using Intel® oneAPI Math Kernel Library (oneMKL) with R

ID 660477
Updated 2/27/2019
Version Latest
Public

author-image

By

Overview

R is a programming language for statistical computing. The open source package also provides an environment for creating and running R programs. This guide will show how to use the BLAS and LAPACK libraries within Intel® oneAPI Math Kernel Library (oneMKL) to improve the performance of R. To use other oneMKL functions, you read this article on Extending R with oneMKL.

Reference: The R Project for Statistical Computing

Prerequisites

Build R use with oneMKL

R can be configured to use the oneMKL BLAS and LAPACK by using --with-blas.

If you are building R with GNU gcc and GNU gfortran, to use the GNU OpenMP* threaded version of oneMKL in R on a Linux operating system add the following:

source /opt/intel/compilers_and_libraries_2019.2.187/linux/mkl/bin/mklvars.sh intel64
MKL="-Wl,--no-as-needed -lmkl_gf_lp64 -Wl,--start-group -lmkl_gnu_thread  -lmkl_core  -Wl,--end-group -fopenmp  -ldl -lpthread -lm"
./configure --with-blas="$MKL" --with-lapack 

The order of the libraries is important. --with-lapack will cause the LAPACK from oneMKL to be used as well.

The oneMKL libraries link line actually is varied based on different compiler and threading model. Developer can select the right libraries by the tool named oneMKL Link Advisor to suit their own needs

Intel® oneAPI Math Kernel Library (oneMKL) Link Line Advisor v5.0

Here are some other variations:

A: If you are building R with Intel® C++ Compiler Classic icc and Intel® Fortran Compiler Classic ifort, then please use

source/opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/compilervars.sh intel64

source /opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/mklvars.sh intel64

MKL=" -L${MKLROOT}/lib/intel64 -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm -ldl "

./configure --with-blas="$MKL" --with-lapack 

or

source /opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/compilervars.sh intel64

source /opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/mklvars.sh intel64

MKL="-Wl,--no-as-neede nbsp; -lmkl_rt -liomp5 -lpthread -lm"

	./configure --with-blas="$MKL" --with-lapack

The default number of threads will equal the number of physical cores on the system, but can be controlled by setting OMP_NUM_THREADS or MKL_NUM_THREADS.

B. You can use the following if you want to dynamically linking a GNU threading version of oneMKL and using GNU GFORTRAN and GCC:

<

source /opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/compilervars.sh intel64

MKL="-L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl "

	

C. The following will link oneMKL statically the GNU threading runtime:

source /opt/intel/compilers_and_libraries_2019.x.xxx/linux/bin/compilervars.sh intel64

MKL=" -L${MKLROOT}/lib/intel64 -Wl,--start-group 
${MKLROOT}/lib/intel64/libmkl_gf_lp64.a 
 ${MKLROOT}/lib/intel64/libmkl_gnu_thread.a
${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lgomp -lpthread"

Check If oneMKL Is Working

1. Check the config.log to see if oneMKL was working

configure:29075: checking for dgemm_ in -L/opt/intel/compilers_and_libraries_2019.2.187/linux/mkl/lib/intel64/ -lmkl_gf_lp64 -lmkl_sequential -lmkl_core
configure:29096: gcc -std=gnu99 -o conftest -g -O2 -I/usr/local/include -L/usr/local/lib64 conftest.c -L/opt/intel/compilers_and_libraries_2019.2.187/linux/mkl/lib/intel64/ -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -lgfortran -lm -lrt -ldl -lm >&5
conftest.c: In function 'main':
conftest.c:210: warning: implicit declaration of function 'dgemm_'
configure:29096: $? = 0
configure:29103: result: yes
configure:29620: checking whether double complex BLAS can be used
configure:29691: result: yes
configure:29711: checking whether the BLAS is complete

Or after build, If MKL library was dynamically linked in the R, type the simple command to verify this

./R-3.5.2/lib/libRblas.so
linux-vdso.so.1 =>&nbsp; (0x00007fffa45ff000)
libmkl_gf_lp64.so => /opt/intel/compilers_and_libraries_2019.2.187/linux/mkl/lib/intel64/libmkl_gf_lp64.so (0x00007fe963097000)

 ......

2. export MKL_VERBOSE=1

Performance Results

To provide some indication of the performance improvements that this can provide, I ran R-benchmark-25.R found on the R benchmarks site on a system with a 48-core Intel(R) Xeon(R) Platinum 8168 CPU @ 2.70GHz with 196 Gb of RAM and running RHEL 7 x86_64. I first installed R-2.15.3 and ran the benchmark without oneMKL to get:

32.795 sec

After installing oneMKL 2019.0 update 2, and following the methods above I was able to get:

4.09 sec

Known Issues

1. oneMKL blocks the use of FFTW in R

Problem: When oneMKL library was linked in R as the BLAS, The MKL libraries like libmkl_rt.so also integrated FFTW3 interface by default. As not all of FFTW3 are implemented in library, it may cause some R extension based on FFTW* failed. Please see the threads.MKL's FFTW Wrappers Block FFTW on Linux Forum

Solution: Build libRblas.so Using the Custom Shared Object Builder. oneMKL provide the tools under install folder:

> cp -r /opt/intel/mkl/tools/builder .
>cd builder; source /opt/intel/mkl/bin/mklvars.sh intel64; make libintel64 export=blas_example_list parallel=gnu name=libRblas

2. Intel OpenMP library hang R when upgrade to 2018 update 1 &2018 update 2.

Problem: Please see the threads oneMKL 2018 Update 2 Freezes During Numpy Test Forum, It is libiomp5.so bug, so affect R with oneMKL, which based on the libiomp5 libraries.

Solution:The issue is fixed in 2018 update 3.

Additional Resources

Quick link oneMKL to R