我试图使用Cython使用BLAS库中的点产品,但是当调用编译模块时,会出现以下“未定义符号: 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
)发布于 2017-06-01 18:26:04
您只需告诉它将库链接到setup.py中:
Extension( ... # as before
libraries=[':libcblas.so.3']
)确切的库可能在一定程度上取决于您已经安装了什么。首先尝试'cblas',如果失败,请查找您拥有的libcblas文件。请注意,您需要链接到libcblas而不是libblas。
您还可以查找Scipy绑定以节省手工创建这些东西的时间(尽管我认为这些是针对BLAS而不是CBLAS的)。
https://stackoverflow.com/questions/44269610
复制相似问题