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

By Devorah Hayman, Devorah Hayman

Published:06/24/2013   Last Updated:06/19/2018

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 latest R package 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 .

Build dependencies required for R. 

$ sudo yum-builddep R

2. Unpack the source tar file: 

$  tar -xvf R-3.4.2.tar.gz
$ cd R-3.4.2

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

$  source <install-dir>/<arch>/bin/ [ia32|intel64]

     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 F77="ifort"
export FC="ifort"
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="-fPIC -qopenmp -O3 -ipo -xHost -multiple-processes=8"
export CXXFLAGS="-fPIC -qopenmp -O3 -ipo -xHost -multiple-processes=8"
export FFLAGS="-fPIC -qopenmp -O3 -ipo -xHost -multiple-processes=8"
export FCFLAGS="-fPIC -qopenmp -O3 -ipo -xHost -multiple-processes=8"
export LDFLAGS="-qopenmp"

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_rt -liomp5 -lpthread"
  echo $MKL

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

 ./configure --prefix=$HOME/R-3.4.2/build --enable-R-shlib --with-x=no --with-blas="$MKL" --with-lapack
(optional)& 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:                  

$  cd 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'.

So far, R is ready for use. Please refer to R manuals:

(Optional) The below instructions are intend to use one special example to show the performance of Intel® Compilers​ + Intel®​ MKL+R

Performance Test

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. The program running in the default R framework/environment:
   user  system elapsed
  3.842   0.020   2.913
  2. The 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               

Questions or Problems?

If you need further assistance please see our Get Help page for your support options.

Product and Performance Information


Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804