numpy.dot很慢但是安装了blas和lapack,如何解决?

时间:2014-11-29 07:05:16

标签: python numpy install lapack blas

我在ArchLinux上运行,我的python版本为2.7.8,并安装了BLASLAPACK

% 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

通过sudo pip2 install numpy安装了Numpy,它确认它同时看到了BLASLAPACK

>>> 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

然而我对np.dot操作的速度测试超过30秒,当我知道在类似的机器上它在10秒内运行良好。如何解决速度问题?在使用BLASLAPACK支持安装numpy时,我是否遗漏了一些内容?

2 个答案:

答案 0 :(得分:6)

好的,这是整个故事。首先,初始设置很慢,因为BLAS参考实现,其设计不是很快。我再说一遍,截至今天,ArchLinux Extra存储库中的包blas是参考实现。有关详细信息,请参阅Presentation部分here

其次,有BLAS的优化版本(实际上有很多版本:ATLAS,OpenBlas,Goto BLAS,MKL等等,毫无疑问)。它们安装起来相当棘手。我最终安装了OpenBlas,这是在ArchLinux上逐步完成的概述:

  1. 安装openblas-lapack package from the AUR
  2. 安装python2-numpy-openblas package from the AUR据我所知,它与python2-numpy配置文件的普通site.cfg包不同,后者指示numpy为搜索我们在步骤1中安装的openblas库。
  3. 这些动作为我解决了问题,速度现在好多了 - 我在问题中提到的测试时间不到1秒。 numpy显示它已经用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
    

    我认为为openblas设置numpy导向python3的过程看起来非常相似。

答案 1 :(得分:0)

我的建议是从源代码构建'numpy'并将其与BLAS和LAPACK链接。