Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

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

04 October 2012

rEFIt, broken GRUB repair following updates

If you mess up your GRUB configuration on an Intel Mac dual booting via rEFIt (or rEFInd), there are several things you can do.

There's all sorts hints and tips that may or may not work on your setup and are all very particular to your partitioning scheme, but I found a few to be quite informative, especially in understanding  the underlying technical problem (ie why GRUB is such a pain to get working right in these setups).

https://bbs.archlinux.org/viewtopic.php?id=99097
http://mac.linux.be/content/problems-refit-and-grub-after-installation
http://ubuntuforums.org/showthread.php?t=1704357

In the last one, the author of an important EFI / GPT-MBR hybrid utility chips in some advice and information. I would try to avoid doing a hard recreation of your hybrid MBR though. One thing you will definitely need is some type of live boot disk.

After trying several bits and pieces of the above advice, and apparently failing to properly reinstall GRUB, I went from a working GRUB that just couldn't find my OSes after selecting to them to a GRUB that never got past displaying "loading".  If you get that far, then you may have to force an install.

Based on this post, I wrote this script for my set up. And that fixed things.

02 October 2012

sudo fu

So apparently, you have to use
sudo su someuser

to do something as another user on Linux Mint Debian Edition. Oh, and the wheel group is actually the sudo group.