【发布时间】:2021-05-26 15:07:05
【问题描述】:
我正在尝试将 f2py 编译函数用于使用 numba 中的 @njit 装饰器装饰的 python 函数。 f2py封装的函数签名为:
stp,f,g,task = xxx(stp,f,g,ftol,gtol,xtol,task,stpmin,stpmax,isave,dsave)
Wrapper for ``xxx``.
Parameters
----------
stp : input float
f : input float
g : input float
ftol : input float
gtol : input float
xtol : input float
task : input string(len=60)
stpmin : input float
stpmax : input float
isave : in/output rank-1 array('i') with bounds (2)
dsave : in/output rank-1 array('d') with bounds (13)
Returns
-------
stp : float
f : float
g : float
task : string(len=60)
签名文件包括:
subroutine xxx(stp,f,g,ftol,gtol,xtol,task,stpmin,stpmax,isave,dsave)
double precision, intent(in,out) :: stp
double precision, intent(in,out) :: f
double precision, intent(in,out) :: g
double precision, intent(in) :: ftol
double precision, intent(in) :: gtol
double precision, intent(in) :: xtol
character*60, intent(in, out) :: task
double precision, intent(in) :: stpmin
double precision, intent(in) :: stpmax
integer dimension(2), intent(inout) :: isave
double precision dimension(13), intent(inout) :: dsave
end subroutine xxx
因为在'nopython'模式下numba必须知道外部函数的签名,我目前的做法是直接用ctypes加载f2py生成的xxx.so文件,而不是标准的'import'方法。然后,我通过以下方式将函数的输入和输出参数注册到 ctypes:
lib = ctypes.cdll.LoadLibrary('xxx.so')
lib.dcsrch_.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_double,
ctypes.c_double, ctypes.c_double, ctypes.c_double,
ctypes.c_char_p(???), ctypes.c_double, ctypes.c_double,
np.ctypeslib.ndpointer(dtype=np.intc, ndim=1, shape=2),
np.ctypeslib.ndpointer(dtype=np.double, ndim=1, shape=13)]
lib.dcsrch_.restype = ctypes.py_object(???).
这里的 (???) 表示我特别不确定签名。当我尝试使用该功能时,我总是得到 seg。故障错误。同时,以标准方式导入函数时,即from xxx import xxx,一切顺利。
我真的很感谢这里的一些帮助。谢谢!
【问题讨论】: