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

陌路散爱 提交于 2019-12-03 17:30:54
ali_m

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!