Using Intel MKL in R without root (or local installation)

Optimized versions of linear algebra libraries can offer substantial time savings for numeric computing. But getting them installed and your programs linked against them can be a real odyssey.

In R, the blas library is left undefined until runtime by looking for a library in R RHOME called libRblas.so. In principal, you can drop in any replacement for blas by replacing this symlink.

If you don’t have write acces to RHOME, you could beg your sysadmin to make a custom install. Or you could compile and manage your own installation. A third option, however is available by making use of a library variable R consults at startup R_LD_LIBRARY_PATH.

By setting this to a directory that you can write to that contains symlinking to your desired alternatives, you can set this on a per-instance basis. For instance I make a directory 3.9-blas-mkl containing the following:

> [3.9-blas-mkl]$ ls -l
-rwxrwx--x 1  7936 Oct  9 12:02 libRblas.so
lrwxrwxrwx 1  11 Oct  9 12:02 libRlapack.so -> libRblas.so
lrwxrwxrwx 1   40 Oct  9 12:15 libR.so -> /software/r/3.6.1/b2/lib64/R/lib/libR.so
-rw-rw---- 1  245 Oct  9 12:03 README
-rw-rw---- 1  39 Oct  9 12:01 shim.c

Here libRblas.so is a library that contains links to all the stuff needed for Intel MKL, and was generated by compiling shim.c using icc following directions here.

Detailed instructions

  1. Install or find a version of intel MKL. I was able to load one from my HPC with module load intel. This puts icc and bunch of other stuff on the path.
  2. Make a directory to hold symlinks you are going to drop into R and change to it.
  3. Download and compile shim.c following directions here. This is probably optional, but elegantly handles the fact that I didn’t want to hard code in symlinks to the mysterious locations of the intel MKL libraries in the module that I loaded, and may protect against other subtle issues. This should give you a libRblas.so linked against MKL.
  4. Make a symlink from libRlapack.so -> libRblas.so
  5. Make a symlink from R RHOME libR.so to libR.so. I did this because otherwise R complained that it couldn’t find libR.so with the value of R_LD_LIBRARY_PATH. This may also be possible to avoid, but was an easy fix for me.
  6. export R_LD_LIBRARY_PATH=”/home/R/3.9-blas-mkl”. Note I got tripped up trying to use the shell expansion ~ here and had to use an absolute path.
  7. Start R and verify success by examining sessionInfo().