Cython调用lapack,错误:“无法获取Python变量的地址”
作者:互联网
我的问题与here所提出的问题非常相似,因此我尝试根据example调用与python捆绑在一起的blas来实现答案中提出的内容.我的代码只是example.pyx的略微修改版本,这是我的相关代码(文件f2pyptr.h不变):
#example.pyx (single precision linear system solver via lapack)
import numpy as np
import scipy.linalg.lapack
import cython
cimport numpy as np
cdef extern from "/(pwd)/f2pyptr.h":
void *f2py_pointer(object) except NULL
ctypedef int sgesv_t(
int *N, int *NRHS,
float *A, int* LDA,
int* IPIV,
float *B,
int *LDB,
int *INFO)
cdef sgesv_t *sgesv = <sgesv_t*>f2py_pointer(scipy.linalg.lapack.sgesv._cpointer)
def myfunc_sgesv():
cdef int N, NRHS, LDA, LDB, INFO
N = 10
NRHS = 1
LDA = 10
LDB = 10
cdef np.ndarray[np.float32_t, ndim=2] A = np.ones((N,N), np.float32, order = "F")
cdef np.ndarray[np.float32_t, ndim=2] B = np.ones((N, NRHS),np.float32, order = "F")
cdef np.ndarray[np.int_t, ndim=1] IPIV = np.empty((N,), dtype = np.int)
sgesv(&N, &NRHS, &A[0,0], &LDA, &IPIV, &B[0,0], &LDB, &INFO)*
def myfunc_sgesv():
cdef int N, NRHS, LDA, LDB, INFO
N = 10
NRHS = 1
LDA = 10
LDB = 10
cdef np.ndarray[np.float32_t, ndim=2] A = np.ones((N,N), np.float32, order = "F")
cdef np.ndarray[np.float32_t, ndim=2] B = np.ones((N, NRHS),np.float32, order = "F")
cdef np.ndarray[np.int_t, ndim=1] IPIV = np.empty((N,), dtype = np.int)
sgesv(&N, &NRHS, &A[0,0], &LDA, &IPIV, &B[0,0], &LDB, &INFO)
使用标准的setup.py文件,我调用
python setup.py build_ext –inplace
导致
Compiling example.pyx because it changed.
Cythonizing example.pyx
Error compiling Cython file:
------------------------------------------------------------
...
cdef np.ndarray[np.float32_t, ndim=2] A = np.ones((N,N), np.float32, order = "F")
cdef np.ndarray[np.float32_t, ndim=2] B = np.ones((N, NRHS),np.float32, order = "F")
cdef np.ndarray[np.int_t, ndim=1] IPIV = np.empty((N,), dtype = np.int)
sgesv(&N, &NRHS, &A[0,0], &LDA, &IPIV, &B[0,0], &LDB, &INFO)
^
------------------------------------------------------------
example.pyx:103:33: Cannot take address of Python variable
Traceback (most recent call last):
File "setup.py", line 5, in <module>
ext_modules = cythonize("example.pyx")
File "/idiap/home/jnewling/.local/lib/python2.7/site-packages/Cython-0.20.1/Cython/Buil/Dependencies.py", line 785, in cythonize
cythonize_one(*args[1:]) File "/idiap/home/jnewling/.local/lib/python2.7/site-packages/Cython-0.20.1/Cython/Buil/Dependencies.py", line 902, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: example.pyx
我的原始example.pyx运行良好,并且具有修改后的单精度版本(即sgemm),因此无法看到为什么我的sgesv实现失败.
任何特别的诊断或替代方法的建议都将受到欢迎.
解决方法:
不知道这个答案是否正确,但是我会告诉您我尝试过的方法,因为它可能会帮助您:
编译代码时,出现错误“无法在其他位置获取Python变量的地址”.我在& IPIV.然后,我注意到它是一个数组,因此将其更改为:
sgesv(& N,& NRHS,& A [0,0],& LDA,& IPIV [0],& B [0,0],& LDB,& INFO).然后,我得到了错误无法将类型’int_t *’分配给’int *’.然后找到以下答案:Passing numpy integer array to c code并将代码更改为:
cdef np.ndarray [int,ndim = 1,mode =’c’] IPIV = np.empty((N,),dtype = ctypes.c_int)
sgesv(& N,& NRHS,& A [0,0],& LDA,& IPIV [0],& B [0,0],& LDB,& INFO)
现在它可以编译而没有任何错误.
标签:lapack,blas,cython,python 来源: https://codeday.me/bug/20191121/2050413.html