Build R-3.4.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 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.1.tar.gz
$ cd R-3.4.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="-O3m -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:

$ ./configure --prefix=$HOME/R-3.4.1/build --enable-R-shlib CC=" icc -mkl" CXX=" icpc -mkl" FC=" ifort -mkl" F77=" ifort -mkl"  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:                  

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

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.


Shawn G.'s picture

I compile R with Intel compiler on Knights Landing server. I get the following error information.

make[6]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools/src'
make[5]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools/src'
make[4]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools'
make[4]: Entering directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools'
installing 'sysdata.rda'
/bin/sh: line 1:  3107 Done                    echo "tools:::sysdata2LazyLoadDB(\"./R/sysdata.rda\",\"../../../library/tools/R\")"
      3108 Floating point exception(core dumped) | R_DEFAULT_PACKAGES=NULL LC_ALL=C ../../../bin/R --vanilla --slave
make[4]: *** [sysdata] Error 136
make[4]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools'
make[3]: *** [all] Error 2
make[3]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library/tools'
make[2]: *** [R] Error 1
make[2]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src/library'
make[1]: *** [R] Error 1
make[1]: Leaving directory `/export/lustrefs/homes/shawn/boost-R/microsoft-r-open-MRO-3.3.2/source/src'
make: *** [R] Error 1

Any ideas?

Robert C. (Intel)'s picture

Thanks for updating the instructions. The configure line has references to /tmp/readline. Is that left over from some temporary workaround?

$  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

Also, you might want to point people at this article to help with the basics:


matt w.'s picture

R has been built for me on the cluster to take advantage of the MKL libraries and the MIC.  However I have a problem when trying to install a package.  The package in question is `forecast`.  Before running R I add the intel module with

`module load intel`

The gist of the error is as follows:

`icpc -I/gpfs/stfc/local/apps/intel/R/3.2.5/lib64/R/include -DNDEBUG  -I/gpfs/stfc/local/apps/intel/utilities/include -I"/gpfs/stfc/local/HCPhi012/mjw01/mjw93-mjw01/library/R/3.2.5/Rcpp/include" -I"/gpfs/stfc/local/HCPhi012/mjw01/mjw93-mjw01/library/R/3.2.5/RcppArmadillo/include"   -fpic  -O2  -c etsTargetFunction.cpp -o etsTargetFunction.o
In file included from /gpfs/stfc/local/HCPhi012/mjw01/mjw93-mjw01/library/R/3.2.5/Rcpp/include/RcppCommon.h(64),
                 from /gpfs/stfc/local/HCPhi012/mjw01/mjw93-mjw01/library/R/3.2.5/Rcpp/include/Rcpp.h(27),
                 from etsTargetFunction.h(3),
                 from etsTargetFunction.cpp(7):
/gpfs/stfc/local/apps/intel/intel_cs/2015.2.164/composer_xe_2015.2.164/compiler/include/complex(69): error #308: member "std::complex<double>::_M_value" (declared at line 1302 of "/usr/include/c++/4.4.7/complex") is inaccessible
          _M_value = __z._M_value;

I am sure it's something to do with the intel tools when compiling the package as part of the installation.  DO you have any guidelines as to how I should install packages which require compilation?  I am not a sysadmin person and have no experience of using compilation tools, luckily I've never faced such problems on my Mac or on the university cluster which does not have R compiled for the MIC.

Ryan N.'s picture

I built R 3.2.2 with Intel Composer XE 2016 (16.0.1) by doing the following:

CC="icc" CXX="icpc" F77="ifort" FC="ifort" AR="xiar" LD="xild" CFLAGS="-O3 -ipo -openmp -xHost" CXXFLAGS="-O3 -ipo -openmp -xHost" F77FLAGS="-O3 -ipo -openmp -xHost" FFLAGS="-O3 -ipo -openmp -xHost" MKL="-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread" ./configure --with-blas="$MKL" --with-lapack

The build finishes. I'm so far running into some trouble where a NEWS.pdf that was apparently not created by the build is attempted to be installed by make install, but I will probably get that figured out (a workaround seems to be to touch NEWS.pdf and let it install the empty file).

Lech N.'s picture

@kiran: make sure to add "-openmp" to the LDFLAGS. The newer R distribution doesn't include it in one of the steps.

Lech N.'s picture

Why are you using the GNU lib (mkl_gf_lp64) instead of the intel one (mkl_intel_lp64)?

Terence K.'s picture

I tried following the instructions above and numerous variations for the current version of R (3.2.0), but without success.

It would be great if these instructions were updated, or, much better, if the standard R distribution were bundled with a reliable mechanism for compiling with icc.

R is widely used in many organizations for computationally intensive work that would benefit from icc's auto-vectorization, auto-parallelization, and MKL.  For simple operations like matrix multiplication I'm seeing 7x-8x difference between conventionally compiled R and MKL, for example.

Intel would sell a lot more icc licenses if they removed the pain & fuss from compiling R with icc.



kiran s.'s picture

i have followed the same procedure but it giving me the following error please help 


/tmp/ipo_iccZtQiIc1.o: In function `main':
ipo_out1.c:(.text.hot00001+0x4e): undefined reference to `__kmpc_begin'
ipo_out1.c:(.text.hot00001+0x86): undefined reference to `__kmpc_end'
/tmp/ipo_iccZtQiIc1.o: In function `do_colsum':
ipo_out1.c:(.text.hot0006b+0x30b3): undefined reference to `__kmpc_global_thread_num'
ipo_out1.c:(.text.hot0006b+0x30c7): undefined reference to `__kmpc_ok_to_fork'
ipo_out1.c:(.text.hot0006b+0x30ea): undefined reference to `__kmpc_push_num_threads'
ipo_out1.c:(.text.hot0006b+0x3157): undefined reference to `__kmpc_fork_call'
ipo_out1.c:(.text.hot0006b+0x3174): undefined reference to `__kmpc_serialized_parallel'
ipo_out1.c:(.text.hot0006b+0x3212): undefined reference to `__kmpc_end_serialized_parallel'
ipo_out1.c:(.text.hot0006b+0x39ca): undefined reference to `__kmpc_for_static_init_4'
ipo_out1.c:(.text.hot0006b+0x3c1e): undefined reference to `__kmpc_for_static_fini'
make[3]: *** [R.bin] Error 1
make[3]: Leaving directory `/home1/cdac/R-lang/R-3.0.1/src/main'


Humberto V.'s picture

I have same issue when running "make check" using MKL 11.1 on Windows:

running code in 'reg-tests-1a.R' ...make[3]: [reg-tests-1a.Rout] Error 1

> stopifnot(identical(predict(basis), predict(basis, newx=wh)),
+           identical(predict(bbase), predict(bbase, newx=wh)),
+           identical(predict(nbase), predict(nbase, newx=wh)))
Error: identical(predict(basis), predict(basis, newx = wh)) is not TRUE
Execution halted


Do you know what could be causing this problem?

Oriol M.'s picture

Did you run the make check after the compilation?

I am getting an error at:
> stopifnot(identical(0, -0), !identical(0, -0, num.eq = FALSE),
+ identical(NaN, -NaN), !identical(NaN, -NaN, single.NA = FALSE),
+ identical(m, m0), !identical(m, m0, = FALSE) )
Error: !identical(0, -0, num.eq = FALSE) is not TRUE
Execution halted


Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.