Low-hanging R Optimizations on Ubuntu
Posted by The Average Investor on Jul 1, 2011
A friend of mine brought my attention recently to the fact that the default R install is way to generic and thus sub-optimal. While I didn’t go all the way rebuilding everything from scratch, I did find a few cheap steps one can do to help things a little.
Simply install the libatlas3gf-base package. That’s all, but it boosts the performance on some R calculations many fold. This package is an optimized BLAS library, which R can use out of the box.
My next step was to enable some SSE instructions when packages are compiled. To do that one needs to overwrite some compiler settings. First, I needed to find the R home path on my system: R.home() returned /usr/lib64/R on my Ubuntu 11.04. The I created a file called /usr/lib64/R/etc/Makevars.site with the following content:
CFLAGS = -std=gnu99 -O3 -pipe -msse4.2 CXXFLAGS = -O3 -pipe -msse4.2 FCFLAGS = -O3 -pipe -msse4.2 FFLAGS = -O3 -pipe -msse4.2
How did I figured out what to add? Well, I looked up the defaults for these settings in /usr/lib64/R/etc/Makeconf and combined them with what I had in mind (adding SSE4.2 by default). I also removed the default -g. Now, when a new package is installed and compiled, you should see the options. For existing packages, uninstall them using remove.packages and then install them back using install.packages. I start R as root (sudo R) for these operations.
Does your CPU support SSE and what version? Run grep -i sse /proc/cpuinfo.
Last I noticed these two variables in Makeconf:
LAPACK_LIBS = -llapack BLAS_LIBS = -lblas
The next thing I may try when my current simulations finish is to install optimized development libraries for BLAS and LAPACK (libatlas-dev for instance) and then change the above definitions …