如何让 NumPy 在 Ubuntu 中使用 OpenBlas?

How can I make NumPy use OpenBlas in Ubuntu?

我同时安装了 BLAS 和 OpenBLAS:

$ dpkg -l \*blas\* | grep ^i
ii  libblas-dev                                           1.2.20110419-7                                      amd64        Basic Linear Algebra Subroutines 3, static library
ii  libblas3                                              1.2.20110419-7                                      amd64        Basic Linear Algebra Reference implementations, shared library
ii  libopenblas-base                                      0.2.8-6ubuntu1                                      amd64        Optimized BLAS (linear algebra) library based on GotoBLAS2
ii  libopenblas-dev                                       0.2.8-6ubuntu1                                      amd64        Optimized BLAS (linear algebra) library based on GotoBLAS2

但是,NumPy 仍然说 OpenBLAS 不可用:

>> np.__config__.show()
blas_info:
    libraries = ['blas']
    library_dirs = ['/usr/lib']
    language = f77
lapack_info:
    libraries = ['lapack']
    library_dirs = ['/usr/lib']
    language = f77
atlas_threads_info:
  NOT AVAILABLE
blas_opt_info:
    libraries = ['blas']
    library_dirs = ['/usr/lib']
    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/lib']
    language = f77
    define_macros = [('NO_ATLAS_INFO', 1)]
atlas_info:
  NOT AVAILABLE
lapack_mkl_info:
  NOT AVAILABLE
blas_mkl_info:
  NOT AVAILABLE
atlas_blas_info:
  NOT AVAILABLE
mkl_info:
  NOT AVAILABLE

我该如何解决这个问题?

我不认为我可以卸载 libblas3,因为很多东西都依赖它,包括 libblas-dev,甚至 libopenblas-dev 也依赖它。

我试过了

$ sudo apt-get install --reinstall python-numpy

但这没有帮助。

这特别令人惊讶,因为 numpy link 中的所有 *.so 文件到 OpenBLAS:

$ ldd `find /usr/lib/python2.7/dist-packages/numpy -name \*\.so` | grep libblas
        libblas.so.3 => /usr/lib/libblas.so.3 (0x00007fba2ac96000)
        libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f04f7f54000)
        libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f9a941a9000)

$ ls -l /usr/lib/libblas.so.3 /etc/alternatives/libblas.so.3
lrwxrwxrwx 1 root root 35 Oct 22  2014 /etc/alternatives/libblas.so.3 -> /usr/lib/openblas-base/libblas.so.3
lrwxrwxrwx 1 root root 30 Oct  6  2014 /usr/lib/libblas.so.3 -> /etc/alternatives/libblas.so.3

根据 ldd 输出,NumPy 必须已经与 OpenBLAS 链接。它只是不知道,因为它是通过 /usr/lib/libblas* 链接的,它认为这是通用的 BLAS。

在 Ubuntu 16.10 你可以

$ apt install libopenblas-base

并使用

激活您首选的 BLAS 实现
$ update-alternatives --config libblas.so.3

我做到了 运行

import numpy as np
a1 = np.random.rand(10000, 10000)
a2 = np.random.rand(10000, 10000)
np.dot(a1, a2)

使用 libblas(2m38s,仅单核负载)和 libopenblas(0m18s,多核负载)

编辑:这是通过 Python 和 numpy 通过 Ubuntu 的官方存储库安装的,而不是 pip。