Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which matrix libraries my R install is using?

I am having a matrix error when using the computer cluster at my university that I cannot reproduce on my local machine. I think it might be due to a difference of matrix libraries (BLAS, LAPACK, ATLAS, etc.). I don't know much about these libraries other than what I've read here, but I'm thinking there should be a way to get R to tell me which matrix libraries it was installed with (i.e. which ones it's using), analogous to sessionInfo() to tell me which version of R packages it's using.

like image 677
rcorty Avatar asked Mar 31 '15 15:03

rcorty


2 Answers

Though there may not be an explicit R function for this, perhaps you can capitalize on shell commands (e.g., file and ldd) to get some clues without requiring rebuilding R nor root access on the cluster:

(rpath <- Sys.which("R"))
#            R
# "/usr/bin/R"

To make sure that ldd will work, see what type of file it is:

system2("file", rpath)
# /usr/bin/R: Bourne-Again shell script, ASCII text executable

If yours shows an actual executable (such as ELF 64-bit LSB executable, x86-64, ...), then skip this one step.

script <- readLines(rpath)
script[grepl("/bin/", script)]
# [1] "#!/bin/bash"
# [2] "     if [ -x \"/usr/${libnn}/R/bin/exec/R\" ]; then"
# [3] "     elif [ -x \"/usr/${libnn_fallback}/R/bin/exec/R\" ]; then"
# [4] "## some systems have a more portable sed, e.g. /usr/xpg4/bin/sed on Solaris,"
# [5] "SED=/bin/sed"
# [6] "      exec sh \"${R_HOME}/bin/Rcmd\" \"${@}\" ;;"
# [7] "R_binary=\"${R_HOME}/bin/exec${R_ARCH}/R\""

This tells me that the actual executable is /usr/lib/R/bin/exec/R (or /usr/lib64/...). It is taking some inference, but it's a step. This is working for me with R-3.3.2 on ubuntu, so I can only assume it'll be similar on different OSes. If this is uninformative, you can also grep for "/lib" or "/exec" (or just examine the whole script file for other clues).

Once you know the path called by the script (or if it isn't a script to begin with), find the shared library dependencies:

system2("ldd", "/usr/lib/R/bin/exec/R")
#         linux-vdso.so.1 =>  (0x00007ffcfadcd000)
#         libR.so => /usr/lib/R/lib/libR.so (0x00007f746f615000)
#         libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f746f3eb000)
#         libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f746f025000)
#         libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f746eda8000)
#         libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f746eaa2000)
#         libreadline.so.6 => /lib/x86_64-linux-gnu/libreadline.so.6 (0x00007f746e85b000)
#         libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f746e61d000)
#         liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f746e3fb000)
#         libbz2.so.1.0 => /lib/x86_64-linux-gnu/libbz2.so.1.0 (0x00007f746e1ea000)
#         libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f746dfd1000)
#         librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f746ddc9000)
#         libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f746dbc4000)
#         libgomp.so.1 => /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f746d9b5000)
#         /lib64/ld-linux-x86-64.so.2 (0x0000560abd5fa000)
#         libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f746d78b000)

The 5th line of this output suggests that R is using the BLAS library, specifically libblas3.

like image 87
r2evans Avatar answered Nov 06 '22 05:11

r2evans


As far as I am aware there isn’t a specific function (especially not in base R) that will give you the BLAS version. R will most likely use the standard library that comes with your system (unless someone built it from source with a specific BLAS version).

You’d have to do it from the terminal, type

locate libblas.so

It’s probably going to be in /usr/lib/libblas.so or /usr/lib64/libblas.so, and the location is probably a symlink. So follow the links

ls -l /usr/lib/libblas.so

If you want to to change the symbolic links to point to a different BLAS library, you can do update-alternatives --config libblas.so.3gf and select the version you want to use. This post explains it very nicely in more detail: https://www.r-bloggers.com/for-faster-r-use-openblas-instead-better-than-atlas-trivial-to-switch-to-on-ubuntu/

Another way to definitely know what version you are using, would be to install whatever implementation of BLAS you want – for example OpenBLAS, and then replace the standard R BLAS library with a symlink to your new library. Make sure to install R with --with-shared-blas option, as explained in here: https://cran.r-project.org/doc/manuals/r-release/R-admin.html#BLAS Then you just need to:

# navigate to your R directory
cd  …/path-to-R-installation/lib/R/lib
# backup the original 
mv libRblas.so libRblas.backup 
# create simlink
ln –s …/path-to-new-0blas-library/new-lib.so libRblas.so
like image 4
USER_1 Avatar answered Nov 06 '22 06:11

USER_1