Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile r with mkl (With mulithreads support)

I compiled R by regarding these guides:

http://www.r-bloggers.com/compiling-64-bit-r-2-10-1-with-mkl-in-linux/

http://cran.r-project.org/doc/manuals/R-admin.html#MKL

But for matrix algebra R does not use all available CPUs.

I tried both:

MKL="-L${MKL_LIB_PATH} -lmkl_gf_lp64 -lmkl_gnu_thread \
      -lmkl_core -fopenmp -lpthread"

and

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"

Options.

How can I force R to use all available CPUs?

How can I check whether R use MKL or not?

like image 630
user1436187 Avatar asked Feb 21 '13 07:02

user1436187


2 Answers

I would like to add my procedure to compile R 3.0.1 with MKL libraries. I am using Debian 7.0 on a core i7 intel processor, 8G RAM. First i installed the MKL libraries, after i set MKL related environment variables (MKLROOT and LD_LIBRARY_PATH) with this command:

>source /opt/intel/mkl/bin/mklvars.sh intel64

So i used the following parameters to ./configure:

>./configure --enable-R-shlib --enable-threads=posix --with-lapack --with-blas="-fopenmp -m64 -I$MKLROOT/include -L$MKLROOT/lib/intel64 -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lpthread -lm"

and finished the installation with make and make install.

As a benchmark, i did a product between two 5000 x 5000 matrix product without MKL and got:

user system elapsed 57.455 0.104 29.033

and after compiling:

user system elapsed 15.993 0.176 4.333

a real gain!

like image 185
Flavio Barros Avatar answered Nov 17 '22 01:11

Flavio Barros


All this is now a lot easier -- a short blog post is here discussing the steps below in detail.

But in short, all you need is this:

## get archive key
cd /tmp
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB
apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB

## add MKL to apt's repo list
sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list'

## update and install (500+ mb download, 1.9gb installed)    
apt-get update
apt-get install intel-mkl-64bit-2018.2-046 

## make it system default via update alternatives
update-alternatives --install /usr/lib/x86_64-linux-gnu/libblas.so     libblas.so-x86_64-linux-gnu      /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
update-alternatives --install /usr/lib/x86_64-linux-gnu/libblas.so.3   libblas.so.3-x86_64-linux-gnu    /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
update-alternatives --install /usr/lib/x86_64-linux-gnu/liblapack.so   liblapack.so-x86_64-linux-gnu    /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
update-alternatives --install /usr/lib/x86_64-linux-gnu/liblapack.so.3 liblapack.so.3-x86_64-linux-gnu  /opt/intel/mkl/lib/intel64/libmkl_rt.so 50

## tell ldconfig
echo "/opt/intel/lib/intel64"     >  /etc/ld.so.conf.d/mkl.conf
echo "/opt/intel/mkl/lib/intel64" >> /etc/ld.so.conf.d/mkl.conf
ldconfig

That's it. Nothing else. Not recompiling or linking. And for example R now shows in sessionInfo() :

Matrix products: default
BLAS/LAPACK: /opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/lib/intel64_lin/libmkl_rt.so
like image 24
Dirk Eddelbuettel Avatar answered Nov 17 '22 02:11

Dirk Eddelbuettel