31 May 2013

Speed up R (on Linux)

You can drastically speed up R in many cases by using a better/more tuned BLAS (linear algebra library) implementation. ATLAS (Automatically Tuned Linear Algebra System) is one such option, but the basic compiled version distributed with Debian and Ubuntu won’t help you out much — you get the most benefit when you compile it yourself so that it’s optimally tuned for your system. (This is also why Debian stopped distributing semi-optimized builds as binary packages.)

No worries though, installing ATLAS from source isn’t hard, and Debian and Ubuntu even maintain a source package.

On Ubuntu and Debian, installing ATLAS goes likes this:

user@localhost:~$ sudo apt-get build-dep atlas
user@localhost:~$ sudo apt-get install  build-essential dpkg-dev cdbs devscripts gfortran liblapack-dev liblapack-pic 
user@localhost:~$ sudo apt-get source atlas
user@localhost:~$ cd atlas*
user@localhost:~$ sudo fakeroot debian/rules custom

If you get a warning about cpufreq not being set to performance mode, then you’ll have to change that for each CPU (numbered from 0). This will turn off frequency-scaling, which offers a speed boost in and of itself, but perhaps isn’t the best option for laptops as it increases power consumption.

Set n equal to the number of cpus (including virtual HT cpus) minus one:

user@localhost:~$ for i in {0..n}; do sudo cpufreq-set -g performance -c$i; done 

After you set your CPU to performance mode, you can try again:

user@localhost:~$ sudo fakeroot debian/rules custom
user@localhost:~$ sudo apt-get install libatlas-base-dev libatlas-base
user@localhost:~$ sudo dpkg -i libatlas3gf-*.deb 

I recommend using tab completion instead of wildcards, but it should now be installed. You can switch back and forth between different BLAS implementations (and see which one is active) with:

user@localhost:~$ sudo update-alternatives --config libblas.so.3

There are 2 choices for the alternative libblas.so.3 (providing /usr/lib/libblas.so.3).
Selection    Path                                    Priority   Status
------------------------------------------------------------
* 0            /usr/lib/atlas-base/atlas/libblas.so.3   35        auto mode
  1            /usr/lib/atlas-base/atlas/libblas.so.3   35        manual mode
  2            /usr/lib/libblas/libblas.so.3            10        manual mode

Press enter to keep the current choice[*], or type selection number: 

I was happy with auto-selection favoring ATLAS, so I just hit enter.

Now for the benchmarks using large matrix multiplication in R.

Before:

> a = matrix(rnorm(5000*5000), 5000, 5000) 
> b = matrix(rnorm(5000*5000), 5000, 5000) 
> system.time( a%*%b )
   user  system elapsed 
191.668   0.108 191.828 

After:

> a = matrix(rnorm(5000*5000), 5000, 5000) 
> b = matrix(rnorm(5000*5000), 5000, 5000) 
> system.time(a%*%b)
   user  system elapsed 
 34.726   0.200  17.687

That’s more than a 10x speedup in elapsed time!

Sources

  • http://packages.debian.org/unstable/libatlas3-base
  • http://anonscm.debian.org/viewvc/debian-science/packages/atlas/tags/3.8.4-2/README.Debian?revision=38541&view=markup
  • http://wiki.debian.org/DebianScience/LinearAlgebraLibraries
  • https://gist.github.com/palday/5685150

No comments:

Post a Comment