Build R-3.3.1 with Intel® C++ and Fortran Compilers and Intel® MKL on Linux*

R is a freely available language and environment for statistical computing and graphics which provides a wide variety of statistical and graphical techniques: linear and nonlinear modelling, statistical tests, time series analysis, classification, clustering, etc.
This guide will show how to build R with the Intel® C++ and Fortran Compilers and BLAS and LAPACK libraries within Intel® Math Kernel Library (Intel® MKL) to improve the performance of R runtime framework.

1. Download R from CRAN(Comprehensive R Archive Network).CRAN is a network of ftp and web servers around the world that store identical, up-to-date, versions of code and documentation for R. Downloads of the latest R packages are available at

2. Unpack the source tar file:

 $  tar -xvf R-3.3.1.tar.gz

$ cd R-3.3.1

3. To build the R project with the Intel® C++ and Fortran Compiler: 

$  source <install-dir>/<arch>/bin/ 

     where <install-dir> is the directory structure containing the compiler /bin directory, <arch> is either linux or mac

$  export CC="icc"

$  export CXX="icpc"

$  export AR="xiar"

$  export LD="xild"

4. If you want to get more performance gain with the following options than to use the default optimizations on your own hardware platform, can add this to the command line:

$  export CFLAGS="-O3 -ipo -openmp -xHost"

$  export CXXFLAGS="-O3 -ipo -openmp -xHost"

5. To use the threaded version of Intel MKL in R add the following, and ensure that you can see the right MKL libraries location:

$  MKL="-lmkl_gf_lp64 -lmkl_intel_thread  -lmkl_core -liomp5 -lpthread"

$  echo $MKL

6. Configure the build environment for R programming language and environment and install the R objects to related directories:

$  sudo ./configure CC=" icc -mkl -I/tmp/readline/include -L/tmp/readline/lib" CXX=" icpc -I/tmp/readline/include -mkl -L/tmp/readline/lib" FC=" ifort -mkl -I/tmp/readline/include -L/tmp/readline/lib" F77=" ifort -mkl -I/tmp/readline/include -L/tmp/readline/lib"  FPICFLAGS=" -fPIC" AR=xiar LD=xild --with-x=no --with-blas=-lmkl

$  sudo make && sudo make install 

7. Change to the R directory and run the R executable file. Type some basic R commands to verify program's correctness:                  

$  file bin/R

$  . bin/R

$  > y <- log(5)

$  > y 

After the R executes the log(5) fuction and assigns the value to its internal value 'y' then it will print to the stdout accordingly. The output should be the value  '[1] 1.609438'.

8. Use R wrapper code(pow_wrp.c) described in the article "Extending R with Intel MKL".  Build the R wrapper:

$  export LD_LIBRARY_PATH=/opt/intel/composerxe/lib/intel64/:./lib:./:$LD_LIBRARY_PATH

$  icc -O2 -fPIC -I/R-3.3.1/include -c pow_wrp.c -o pow_wrp.o

$  icc -shared -liomp5 -L/opt/intel/composerxe/mkl/lib/intel64 -lmkl_rt -o pow_wrp.o -L./lib -lR

[Here the flag ‘-fPIC’ is needed on the Intel64 platform or we will get the following error messages when linking some ELF standalone objects into related final ELF shared objects:

"ld: pow_wrp.o: relocation R_X86_64_PC32 against undefined symbol `Rf_coerceVector' can not be used when making a shared object; recompile with -fPIC .ld: final

link failed: Bad value"  ]

9. Measure the performance gain of the R program and the R runtime execution environment from using the Intel® Compiler and the BLAS and LAPACK functions within Intel® Math Kernel Library.

Use the following R test script to call mathematics functions and output the R program's execution time in the R environment compiled above:

 mkl_pow <- function(n, x, y) .Call("mkl_vdpow", n, x, y)
 n <- 1000000
 x <- runif(n, min=2, max=10)
 y <- runif(n, min=-2, max=-1)
 start <- proc.time()
 z <- mkl_pow(n, x, y)
 end1 <- proc.time() - start
##n <-1000000
i <- n
start <- proc.time()
repeat{ z[i] <- x[i]^y[i]
 i <- i - 1
 if (i==0) break() }
end2 <- proc.time() - start

The following performance comparisons demonstrate a gain of about 25x from using the MKL and an additional 5X performance gain of a decrease in CPU execution time with getting R programs and R runtime framework compiled and optimized with the Intel® Compilers.The test benchmark was performed on the system of 4-Core Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz with 4GB memory of Red Hat Enterprise Linux Server release 6.3 operating system.

  1. Program running in the default R framework/environment:

   user  system elapsed

  3.842   0.020   2.913

  2. Program running in the default R framework/environment + extended R with MKL:

   user  system elapsed

  0.115   0.019   0.337

  3. Program running in the default R framework/environment + extended R with MKL + compiled and optimized R with Intel compiler

   user  system elapsed    

  0.019   0.008   0.015          




For more complete information about compiler optimizations, see our Optimization Notice.