5

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?

user1436187
  • 3,252
  • 3
  • 26
  • 59

4 Answers4

5

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!

Flavio Barros
  • 996
  • 1
  • 11
  • 29
5

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
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Would you happen to know of good instructions to get R up and running with Intel MKL libraries enabled on Windows? As it seems that Microsoft Open R is being phased out - that always used to be the easy solution. Would it be an option to have CRAN binaries up compiled against Intel MKL? Who should I ask for that? – Tom Wenseleers Nov 06 '22 at 20:20
  • I have not worked on Windows in quite some time but @avraham is likely to have a goto resource for you. Second post at https://www.avrahamadler.com/ is for OpenBLAS which I found to be almost as fast as MKL and easier to deal with (smaller footprint). – Dirk Eddelbuettel Nov 06 '22 at 20:24
  • Many thanks for that - I'll have a try with that! Though in the benchmarks Intel MKL is slightly better. Wished there was somehow an easier way to get this up and running. E.g. via a user configurable environment variable that would allow you to switch BLAS libraries at runtime. Would be nice also if CRAN could provide pre-compiled binaries compiled against Intel MKL in same way that MRO used to provide these (now that Intel MKL is openly & freely available). I would think that most would like to have a multithreaded BLAS enabled by default... – Tom Wenseleers Nov 06 '22 at 20:49
  • I actually think they (or, in this case, Prof Ripley) do -- there used to be a 'R with Goto BLAS' download. OpenBLAS is the successor to Goto BLAS. The MKL is a little murkier as it wasn't always 'free as in beer' and still isn't open source so it does not get the same love ... – Dirk Eddelbuettel Nov 06 '22 at 22:46
0

(not a real answer: I don't use MKL, I use OpenBlas as shared BLAS as described in the R-admin manual.)

  • As quick check whether the optimized BLAS is used I do a matrix multiplication. Even if only 1 core is used, this should be faster for the optimized BLAS than for the standard BLAS R comes with.

  • To check how many cores are in use, I look at top (or a CPU usage graph/monitor) during the matrix multiplication.

  • There has been trouble in the past with CPU affinity, so that a BLAS would start $n$ threads, but they were all running on the same core, see Parallel processing in R limited.
    r-devel (3.0.0-to-be) has a function to set the CPU affinity.

Community
  • 1
  • 1
cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
0

A complete tutorial is available here:

https://software.intel.com/en-us/articles/build-r-301-with-intel-c-compiler-and-intel-mkl-on-linux

or simply using:

http://mran.revolutionanalytics.com/download/

user1436187
  • 3,252
  • 3
  • 26
  • 59