Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Intel's Math Kernel Library (MKL) to R on Windows

Using an alternative BLAS for R has several advantages, see e.g. https://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.pdf.

Microsoft R Open https://mran.revolutionanalytics.com/documents/rro/installation/#sysreq is using Intel's MKL instead of the default Reference BLAS to speed up calculations.

My question is:

What would be the exact steps to link Intel's MKL library **manually to R**'s most recent version on Windows (https://cran.r-project.org/bin/windows/base/)?


UPDATE 20-07-2016: Here is very detailed description on how to build a OpenBLAS-based Rblas.dll for 64-bit R for Windows for R ≥ 3.3.0: http://www.avrahamadler.com/r-tips/build-openblas-for-windows-r64/

like image 771
majom Avatar asked Jun 29 '16 04:06

majom


People also ask

How do you link library in MKL?

To link to MKL when compiling, for serial code, use the linker flags -lmkl_gf_lp64 -lmkl_core -lmkl_sequential . To link to MKL when compiling, for multithreaded code, use the openmp flag -fopenmp and the linker flags -lmkl_gf_lp64 -lmkl_core -lmkl_gnu_thread .

Can I use MKL with GCC?

Intel MKL may be used with the GCC compilers installed on Apocrita. The Intel MKL Link Line Advisor tool can be used to determine the required compiler and link flags to access the package. The variable MKLROOT , referred to in the advisor output, should be set to the directory where the version of MKL is installed.

How do you load a MKL?

To load the default MKL, run the following command: module load mkl . To load a particular version, use module load mkl/version . For example, use module load mkl/11.3. 3 to load MKL version 11.3.


1 Answers

Easier solution than having to recompile R against the Intel MKL libraries on Windows is just to

  1. Install Microsoft R Open from https://mran.microsoft.com/download, which comes with the outdated R version 3.5.3 but also with the Intel MKL multithreaded BLAS libraries
  2. Install the latest version of R from https://cran.r-project.org/bin/windows/base/, i.e. currently R 3.6.2
  3. copy files libiomp5md.dll, Rblas.dll and Rlapack.dll from C:\Program Files\Microsoft\R Open\R-3.5.3\bin\x64 to C:\Program Files\R\R-3.6.2\bin\x64 (you can back up your existing default non-hyperthreaded Rblas.dll and Rlapack.dll files first if you like)
  4. copy Microsoft R Open libraries/packages MicrosoftR, RevoIOQ, RevoMods, RevoUtils, RevoUtilsMath and doParallel from C:\Program Files\Microsoft\R Open\R-3.5.5\library to your default package directory, e.g. C:\Documents\R\win-library\3.6
  5. copy files Rprofile.site and Renviron.site from directory C:\Program Files\Microsoft\R Open\R-3.5.5\etc to C:\Progral Files\R\R-3.6.2\etc
  6. replace line 24 in file Rprofile.site options(repos=r) with options(repos="https://cran.rstudio.com") (or your favourite CRAN repository - you can also use "https://cran.revolutionanalytics.com", the MRO repository that has the latest daily builds of all packages) to make sure that it will install the latest CRAN packages as opposed to the outdated mran.microsoft.com mirror that has outdated package versions, frozen at the 15th of April 2019. Also comment out lines 153, 154 and 155 with a #

Then restart RStudio to check that it works, with small SVD benchmark on my Intel Core i7-4700HQ 2.4GHz 4 core/8 thread laptop:

getMKLthreads() 4  # Singular Value Decomposition m <- 10000 n <- 2000 A <- matrix (runif (m*n),m,n) system.time (S <- svd (A,nu=0,nv=0))    user  system elapsed    15.20    0.64    4.17 

That same benchmark without Intel MKL installed ran at

   user  system elapsed    35.11    0.10   35.21  

so we get a >8 fold speed increase here!

Screenshot of Microsoft R Open 6.2 with Intel MKL up and running:

enter image description here

Alternatively, if you don't like copying files from MRO to your latest R installation, you can also copy the files from the free Intel MKL installation to your R installation to get multithreaded operation (as outlined in the other answer below):

  1. Install Intel MKL from https://software.intel.com/en-us/mkl/choose-download (free)

Copy all the contents from inside these folders

C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\mkl C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\compiler 

to

C:\Program Files\R\R-3.6.1\bin\x64 
  1. Inside the destination folder, create 2 copies of mkl_rt.dll and rename one of them Rblas.dll and the other Rlapack.dll replacing the originals and also keeping mkl_rt.dll.
  2. This will not provide you with the function setMKLthreads() and getMKLthreads() functions though to set the nr of intel MKL threads, as these come with the MRO package RevoUtilsMath. But for most people the default nr of threads set equal to the nr of physical cores will be OK though...

Not sure what's up with Microsoft, and why they are no longer updatig MRO... And why they also dropped Mac OS X support...

I hope that, given that Intel MKL is free now, the R core people will sooner or later provide a precompiled R version that is compiled to use the Intel MKL libs, or possibly detect at run time if Intel MKL is installed, and if it is, use it. I think this is important, especially since the easy availability of a good multithreaded BLAS also determines how one would develop packages - e.g. if a good multithreaded BLAS would be available for all OSes one would veer towards using RcppArmadillo, which falls back on whatever BLAS one has installed (but on Windows would give drastically worse timings if Intel MKL is not installed), and if not RcppEigen would be the best option, as that has its own multithreaded matrix algebra, irrespective of the BLAS against which R is compiled......

On Ubuntu btw it's very easy to make R use Intel MKL, without having to recompile R, as outlined here: https://github.com/eddelbuettel/mkl4deb

PS Slight problem is that running setMKLthreads(4) will crash RStudio (that was already a problem in the official MRO 3.5.3 though) but it does work OK in the R console...

like image 113
Tom Wenseleers Avatar answered Sep 18 '22 08:09

Tom Wenseleers