我正在尝试使用来自BLAS库的dot产品使用Cython,但是当调用编译的模块时,会出现以下回溯“未定义的符号: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))
编译文件如下所示: import 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
)
答案 0 :(得分:0)
您只需要告诉它链接setup.py中的库:
Extension( ... # as before
libraries=[':libcblas.so.3']
)
确切的库可能取决于您安装的内容。首先尝试'cblas'
,如果失败则查找您拥有的libcblas文件。请注意,您需要链接到libcblas而不是libblas。
您也可以查找Scipy BLAS Cython bindings以节省手动创建这些内容的时间(虽然我认为这些是针对BLAS而不是CBLAS)。