numpy.dot is slow yet blas and lapack are installed, how to fix?

blas, installation, lapack, numpy, python

Solution

Alright, here is the whole story. First, the initial setup was slow because `BLAS` is a reference implementation which is not designed to be fast. I repeat, as of today, the package `blas` in the ArchLinux Extra repository is the reference implementation. For details, see the `Presentation` section here.

Secondly, there are optimized versions of `BLAS` (quite a few, actually: ATLAS, OpenBlas, Goto BLAS, MKL and many more, no doubt). They are rather tricky to install. I ended up installing OpenBlas, here is a step-by-step overview of doing that on ArchLinux:

- Install the `openblas-lapack` package from the AUR

- Install the `python2-numpy-openblas` package from the AUR As I understand it, it differs from the ordinary `python2-numpy` package by the `site.cfg` configuration file, which instructs `numpy` to be searching for the `openblas` libraries we've installed on step 1.

These actions solved the problem for me, the speed is now much better -- under 1 second for the test I mentioned in the question. Also numpy shows it has been compiled with openblas:

>>> np.show_config()
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/lib']
    language = f77
blas_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/lib']
    language = f77
openblas_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/lib']
    language = f77
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/lib']
    language = f77
blas_mkl_info:
  NOT AVAILABLE

I believe that the process of setting up `openblas` oriented `numpy` for `python3` looks very similar.

Problem

I am running on `ArchLinux`, my python version is `2.7.8` and both `BLAS` and `LAPACK` are installed: ``` % pacman -Qs blas; pacman -Qs lapack local/blas 3.5.0-1 Basic Linear Algebra Subprograms local/lapack 3.5.0-1 Linear Algebra PACKage ``` Numpy has been installed through `sudo pip2 install numpy` and it confirms that it sees both `BLAS` and `LAPACK`: ``` >>> numpy.show_config() blas_info: libraries = ['blas'] library_dirs = ['/usr/lib64'] language = f77 lapack_info: libraries = ['lapack'] library_dirs = ['/usr/lib64'] language = f77 atlas_threads_info: NOT AVAILABLE blas_opt_info: libraries = ['blas'] library_dirs = ['/usr/lib64'] language = f77 define_macros = [('NO_ATLAS_INFO', 1)] atlas_blas_threads_info: NOT AVAILABLE openblas_info: NOT AVAILABLE lapack_opt_info: libraries = ['lapack', 'blas'] library_dirs = ['/usr/lib64'] language = f77 define_macros = [('NO_ATLAS_INFO', 1)] openblas_lapack_info: NOT AVAILABLE atlas_info: NOT AVAILABLE lapack_mkl_info: NOT AVAILABLE blas_mkl_info: NOT AVAILABLE atlas_blas_info: NOT AVAILABLE mkl_info: NOT AVAILABLE ``` Yet my speed test for the `np.dot` operation is well over 30 seconds when I know that on a similar machine it runs well under 10 seconds. How to fix the speed issue? Have I missed something when installing numpy with `BLAS` and `LAPACK` support?

Original source