Intel MKL integration to R on Windows

Referencing back to this topic: Using alternative BLAS and LAPACK linear algebra routines like Intel oneMKL on Windows, makes R work much faster

Intel MKL libraries offer a huge speed improvement for certain classes of matrix calculations, particularly on Intel chips (but interestingly the docs suggest some improvements on any multi-threaded chipset, not just Intel - I have no evidence to confirm that). I have been integrating these into R using a simple method (linked below) internally in my company since R version 3.6.1. I am now attempting to do it properly.

Integrating MKL into R is a minor pain in the behind, but easily doable if you don't mind losing the FFTW procedures, which breaks various packages (igraph being the big one) - this was a known bug in the MKL packages but docs state there is now a fix for it, provided the more complex integration method is used. Easy method I've used until now is documented on stackoverflow here: Linking Intel's Math Kernel Library (MKL) to R on Windows - Stack Overflow

Purpose of this topic is to go over how to do this using oneMKL (Intel's current name for their MKL libraries/APIs and builder/linking program for the MKL libraries/APIs). I intend to come back and update here when I've figured it all out, because every 2-3 versions I have to do this for my team anyway and it's best to spread the love on this sort of figuring out around so other people don't need to do the thinking work. If you've already figured it out before I come back then please update method below for good of everyone working on R in windows.

For now here's my restatement of the stackoverflow version:

  1. Install R
  2. Obtain the MKL library from Intel
  3. Install the MKL library
  4. Get the symbols from your installed R distribution for Rblas.dll and Rlapack.dll, as below:
    4.1. Open the Developer Command Prompt for Visual Studio 2017 (or your version of VS, but the paths will need modifying accordingly in that case)
    4.2. Within the developer prompt, initialise the 64 bit environment:
    call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64
    4.3. Copy the symbols to a list for each file:
    dumpbin /exports C:\Program Files\R\R- 3.6 . 1 \bin\x64\Rblas.dll > Rblas_list
    dumpbin /exports C:\Program Files\R\R- 3.6 . 1 \bin\x64\Rlapack.dll > Rlapack_list_R
  5. Edit both lists to only leave the names
  6. Copy the Rblas_list and Rlapack_list files you have just created into the builder directory here: C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019.5.281\windows\mkl\tools\builder
  7. Make the libraries:
    7.1. nmake libintel64 export=Rblas_list name=Rblas
    nmake libintel64 export=Rlapack_list_R name=Rlapack > undefined_symbols_list
    Note that the export and name parameters take paths as arguments, so the expectation is that in this example the command will be run from the builder directory with the list files present in that dir.
    7.2. Making Rblas should be successful and result in an Rblas.dll being created, but the creation of Rlapack is supposed to fail at this stage, instead generating a list of undefined symbols.
    7.3. Edit the undefined_symbols_list file to leave just the names, as you did in step 5.
    7.4. To get only the valid symbols for the Rlapack library, diff the undefined_symbols_list against the Rlapack_list_R symbols list
    findstr /v /g:undefined_symbols_list Rlapack_list_R > Rlapack_list
    7.5. Make the Rlapack library from the new Rlapack_list
    nmake libintel64 export=Rlapack_list name=Rlapack
  8. Move the dll's into the R distribution, overwriting the existing Rblas.dll and Rlapack.dll files
    8.1. cp Rblas.dll C:\Program Files\R\R- 3.6 . 1 \bin\x64\Rblas.dll
    cp Rlapack.dll C:\Program Files\R\R- 3.6 . 1 \bin\x64\Rlapack.dll
  9. Test that MKL is being used, by running some R code that uses it, e.g.:
    9.1. Sys.setenv(MKL_VERBOSE= 1 )
    lm(dist~speed,cars)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.