Using Intel® MKL with R

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® Math Kernel Library (Intel® MKL) to improve the performance of R. To use other Intel MKL functions, you read this article on Extending R with Intel MKL.

Reference: http://www.r-project.org/

Prerequisites:

 

Building R to use with Intel MKL

R can be configured to use the Intel MKL BLAS and LAPACK by using --with-blas. To use the threaded version of Intel MKL in R on a Linux operating system add the following:

MKL_LIB_PATH=/opt/intel/composer_xe_2013.3.163/mkl/lib/intel64
OMP_LIB_PATH=/opt/intel/composer_xe_2013.3.163/compiler/lib/intel64
export LD_LIBRARY_PATH=${MKL_LIB_PATH}:${OMP_LIB_PATH} 
MKL=" -L${MKL_LIB_PATH}   -L${OMP_LIB_PATH}                \
      -Wl,--start-group                         \
          -lmkl_gf_lp64       \
          -lmkl_intel_thread    \
          -lmkl_core          \
      -Wl,--end-group                           \
      -liomp5 -lpthread"
./configure --with-blas="$MKL" --with-lapack

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

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.

Here are some other variations:

If you want to use (GNU OpenMP*)

MKL=" -L${MKL_LIB_PATH}                         \
      -Wl,--start-group                         \
          -lmkl_gf_lp64       \
          -lmkl_gnu_thread    \
          -lmkl_core          \
      -Wl,--end-group                           \
      -lgomp -lpthread"

You can use the following if you want to use a sequential version of Intel MKL:

MKL=" -L${MKL_LIB_PATH}                         \
      -Wl,--start-group                         \
          -lmkl_gf_lp64       \
          -lmkl_sequential    \
          -lmkl_core          \
      -Wl,--end-group"

Finally, the following will link Intel MKL statically the GNU threading runtime:

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

 Check the config.log to see if MKL was working, 

configure:29075: checking for dgemm_ in -L/opt/intel/composer_xe_2013.3.163/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/composer_xe_2013.3.163/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

  

$ ldd ./bin/exec/R
        linux-vdso.so.1 =>  (0x00007fffa45ff000)
        libmkl_gf_lp64.so => /opt/intel/composer_xe_2013.3.163/mkl/lib/intel64/libmkl_gf_lp64.so (0x00007fe963097000)
        ......

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 32-core Intel® Core™ i7-975 processor Extreme Edition processor (8MB LLC, 3.33GHz) with 6GB of RAM and running RHEL 6 x86_64. I first installed R-2.15.3 and ran the benchmark without Intel MKL. to get:

175.825 sec

After installing Intel MKL 11.0 update 2, and following the methods above I was able to get:

17.289 sec

Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.