Monday, December 3, 2012

R uses different blas and lapack libraries without relink on ubuntu

I was seeking ways to speedup R's linear algebra routines and found several interesting articles about using alternative BLAS and LAPACK libraries.  The official manual (cran.r-project.org/doc/manuals/R-admin.html) provides examples to recompile R with various BLAS/LAPACK libraries.  However, I am too lazy to recompile my own R and upgrade every time when a new version come out.  Then, I found Dirk Eddelbuettel's article "Benchmarking Single- and Multi-Core BLAS Implementations and GPUs for use with R".  Dirk pointed out that under Ubuntu/Debian, R is compiled with dynamic libraries and it is trivial to use another BLAS/LAPACK libraries.  But in that article, no detail was given.  So, I tried a few times and found how to do it.

First, we need to install some packages:
libblas3gf : the original blas from netlib
libopenblas-base: this is continued version of GOblas
revolution-r: this contains revolution's repackaged intel MKL libraries

When revolution-r is installed, R will default to use its library, which is good because intel's MKL libraries are multithreaded and fast.  But in case we would like to try out other libraries, it can be done by renaming files under /usr/lib/R/lib directory.

I am going to leverage Debian/Ubuntu's alternative system in the following paragraphs (just google update-alternative if you don't know).  I can use a system wised liblapack from R by creating a symbolic link in /usr/lib/R/lib as below:

lrwxrwxrwx 1 root root       16 Sep 14  2009 liblapack.so -> liblapack.so.3gf*
lrwxrwxrwx 1 root root       25 Dec  3 20:09 liblapack.so.3gf -> /usr/lib/liblapack.so.3gf*
-rw-r--r-- 1 root root 28398656 Sep 14  2009 liblapack.so.3gf.old

I renamed revoluton-r's liblapack.so.3gf to liblapack.so.3gf.old and created a symbolic link to /usr/lib/liblapack.so.3gf which is the system liblapack.  You can do the same thing to libblas.
It is as simple as that!  You can confirm that R is really using your BLAS/LAPACK libraries using "lsof -p <pid of your R>" this command lists loaded libraries by R.

Intel's MKL is fast, but how about even faster with GPU computing power?  culatools.com provides yet another lapack implementation which harvests nvidia's GPU power.  We can ask R to use cula's library!

You need to get cula library first from its website.  Install it and setup appropriate environments following cula's installation manual.  I installed it under /usr/local/cula
Then, create an entry in alternatives using command: sudo update-alternatives --install /usr/lib/liblapack.so.3gf liblapack.so.3gf /usr/local/cula/lib64/libcula_lapack_link.so 20
select cula's lapack as the system's lapack library using command sudo update-alternatives --config liblapack.so.3gf
If you would like to use MKL's lapack when cula decides that it is better to use CPU instead of GPU, just create an environment variable:  export CULA_CPU_LIB=/usr/lib/R/lib/liblapack.so.3gf.old

And now, R can use GPU to do linear algebra computation and fall back to intel MKL if appropriate!

No comments:

Post a Comment