Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did matrix multiplication using python's numpy become so slow after upgrading ubuntu from 12.04 to 14.04?

I used to have Ubuntu 12.04 and recently did a fresh installation of Ubuntu 14.04. The stuff I'm working on involves multiplications of big matrices (~2000 X 2000), for which I'm using numpy. The problem I'm having is that now the calculations are taking 10-15 times longer.

Going from Ubuntu 12.04 to 14.04 implied going from Python 2.7.3 to 2.7.6 and from numpy 1.6.1 to 1.8.1. However, I think that the issue might have to do with the linear algebra libraries that numpy is linked to. Instead of libblas.so.3gf and liblapack.so.3gf, I can only find libblas.so.3 and liblapack.so.3.

I also installed libopenblas and libatlas:

$ sudo apt-get install libopenblas-base libatlas3-base

and tried them, but the slowdown doesn't change. So, my questions are:

  1. What's the difference between the packages with and without the "gf"?
  2. Is this possibly causing the slowdown in the matrix multiplications?
  3. If so, how can I go back to libblas.so.3gf and liblapack.so.3gf? They seem to be discontinued in Ubuntu 14.04.

Thanks much!

like image 342
Mauricio Gutierrez Avatar asked Mar 18 '23 17:03

Mauricio Gutierrez


1 Answers

wim is correct, in that the problem is probably caused by numpy linking to a slower BLAS library (e.g. the reference CBLAS library rather than ATLAS).

You can check which BLAS library is being linked at runtime by calling the ldd utility on one of numpy's compiled shared libraries.

For example, if you installed numpy in the standard location using apt-get:

~$ ldd /usr/lib/python2.7/dist-packages/numpy/core/_dotblas.so
        ...
        libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f01f0188000)
        ...

This output tells me that numpy is linked against /usr/lib/libblas.so.3. This is usually a symlink to the reference CBLAS library, which is pretty slow.

You could, as wim suggests, remove the version of numpy installed via apt-get and build it yourself, either using pip or by downloading the source directly. However, I would strongly discourage you from using sudo pip install ... to install Python modules system-wide. This is a bad habit to get into, since you run the risk of breaking dependencies in your system-wide Python environment.

It is much safer to either install into your ~/.local/ directory using pip install --user ... or even better, to install into a completely self-contained virtualenv.

Another option would be to use update-alternatives to force your system-wide numpy to link against a different BLAS library. I've written a previous answer here that shows how to do this.

like image 175
ali_m Avatar answered Apr 25 '23 08:04

ali_m