从 Cython 调用 Blas ddot

Calling blas ddot from Cython

我正在尝试使用 Cython 使用 BLAS 库中的点积,但是当调用编译的模块时出现以下回溯 "undefined symbol: cblas_ddot"。执行 np.config.show() 查看链接库:

lapack_info:
       libraries = ['lapack', 'lapack']
       library_dirs = ['/usr/lib64']
       language = f77

lapack_info:
    libraries = ['lapack', 'lapack']
    library_dirs = ['/usr/lib64']
    language = f77

openblas_lapack_info:
  NOT AVAILABLE

blas_info:
    libraries = ['cblas', 'blas']
    library_dirs = ['/usr/lib64']
    define_macros = [('HAVE_CBLAS', None)]
    language = c

atlas_3_10_blas_threads_info:
  NOT AVAILABLE

atlas_threads_info:
  NOT AVAILABLE

atlas_3_10_threads_info:
  NOT AVAILABLE

atlas_blas_info:
  NOT AVAILABLE

atlas_3_10_blas_info:
  NOT AVAILABLE

atlas_blas_threads_info:
  NOT AVAILABLE

openblas_info:
  NOT AVAILABLE

blas_mkl_info:
  NOT AVAILABLE

blas_opt_info:
    libraries = ['cblas', 'blas']
    library_dirs = ['/usr/lib64']
    language = c
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]

blis_info:
  NOT AVAILABLE

atlas_info:
  NOT AVAILABLE
atlas_3_10_info:
  NOT AVAILABLE

lapack_mkl_info:
  NOT AVAILABLE

和 ldd /usr/lib/python2.7/site-packages/numpy/core/multiarray.so 和 readlink -e /usr/lib/libblas.so.3 显示: /usr/lib/libblas.so.3.7.0

显然 BLAS 库已链接,但未找到 cblas_ddot。 pyx 文件:

import numpy as np
cimport numpy as np

cdef extern from "cblas.h":
    double ddot "cblas_ddot"(int N,
                             double *X, int incX,
                             double *Y, int incY)

ctypedef np.float64_t dtype_t
def matmul(np.ndarray[dtype_t, ndim=2] A,
           np.ndarray[dtype_t, ndim=2] B):
    cdef Py_ssize_t i, j
    cdef np.ndarray[dtype_t,ndim=2] out = np.zeros((A.shape[0],B.shape[1]))
    cdef np.ndarray[dtype_t, ndim=1] A_row, B_col
    for i in range(A.shape[0]):
        A_row = A[i,:]
        for j in range(B.shape[1]):
            B_col = B[:, j]
            out[i,j] = ddot(
                A_row.shape[0],
                <dtype_t*>A_row.data,
                A_row.strides[0] // sizeof(dtype_t),
                <dtype_t*>B_col.data,
                B_col.strides[0] // sizeof(dtype_t))

编译文件如下所示: 导入 numpy

if __name__ == '__main__':

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext


    # The Cython modules to setup
    ext_modules = [
        Extension('matmul', ['matmul.pyx'], include_dirs=
        [numpy.get_include()])
    ]

    # Run the setup command
    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = ext_modules
    )

你只需要告诉 link setup.py 中的图书馆:

Extension( ... # as before
   libraries=[':libcblas.so.3']
  )

确切的库可能在一定程度上取决于您安装的内容。首先尝试 'cblas',如果失败,请查找您拥有的 libcblas 文件。请注意,您需要 link 到 libcblas 而不是 libblas。

您也可以查看 Scipy BLAS Cython bindings 以节省手动创建这些东西的时间(尽管我认为这些是针对 BLAS 而不是 CBLAS)。