Why did matrix multiplication using python's n

2019-03-31 00:36发布

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!

2条回答
淡お忘
2楼-- · 2019-03-31 01:11

Are you installing numpy through package manager?

If so, I recommend to go through pip instead so you can clearly see in the build process what is being successfully linked during setup.

  1. Remove the apt version (sudo apt-get purge python-numpy)
  2. Install build-deps headers and static libraries (sudo apt-get install libblas-dev liblapack-dev gfortran), maybe there are some others but these are the ones I remember.
  3. pip install numpy
查看更多
Evening l夕情丶
3楼-- · 2019-03-31 01:21

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.

查看更多
登录 后发表回答