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
- Install or find a version of intel MKL. I was able to load one from my HPC with
module load intel
. This putsicc
and bunch of other stuff on the path. - Make a directory to hold symlinks you are going to drop into R and change to it.
- 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 themodule
that I loaded, and may protect against other subtle issues. This should give you alibRblas.so
linked against MKL. - Make a symlink from
libRlapack.so -> libRblas.so
- Make a symlink from
R RHOME libR.so
tolibR.so
. I did this because otherwise R complained that it couldn’t findlibR.so
with the value ofR_LD_LIBRARY_PATH
. This may also be possible to avoid, but was an easy fix for me. - 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. - Start R and verify success by examining
sessionInfo()
.