使用 ctypes 从 numba 调用 fortran

问题描述 投票:0回答:2

考虑这个 fortran 模块,在文件中

test.f90

module mymod
  use iso_c_binding, only: c_double
  implicit none
contains
  subroutine addstuff(a,b,c) bind(c,name='addstuff_wrap')
    real(c_double), intent(in) :: a, b
    real(c_double), intent(out) :: c
    c = a + b
  end subroutine
end module

可以使用

gfortran test.f90 -shared -fPIC -o test.so
进行编译。我可以从 python 中调用它

import ctypes as ct
mylib = ct.CDLL('test.so')
addstuff.argtypes = [ct.POINTER(ct.c_double), ct.POINTER(ct.c_double), ct.POINTER(ct.c_double)]
addstuff.restype = None

a = ct.c_double(1.0)
b = ct.c_double(2.0)
c = ct.c_double()

addstuff(ct.byref(a),ct.byref(b),ct.byref(c))
print(c.value)

这会返回正确的答案,

3.0
。但是,我想从
numba
jitted 函数

调用它
from numba import njit
@njit
def test(a, b):
    c = ct.c_double()
    addstuff(ct.byref(ct.c_double(a)), \
             ct.byref(ct.c_double(b)), \
             ct.byref(c))
    return c.value
test(1.0, 2.0)

但这行不通。它返回错误

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'c_double' of type Module(<module 'ctypes' from ...)

File "<ipython-input-14-f8fb94981395>", line 3:
def test(a, b):
    c = ct.c_double()
    ^

有人知道解决方法吗?这很烦人,因为

numba
确实声称它支持c_double类型。

python fortran ctypes numba
2个回答
2
投票

解决方案如下:

import ctypes as ct
from numba import njit

mylib = ct.CDLL('test.so')
addstuff.argtypes = [ct.c_void_p, ct.c_void_p, ct.c_void_p]
addstuff.restype = None

@njit
def test(a, b):
    aa = np.array(a,np.float64)
    bb = np.array(b,np.float64)
    c = np.array(0.0,np.float64)
    addstuff(aa.ctypes.data, \
             bb.ctypes.data, \
             c.ctypes.data)
    return c.item()

0
投票

以上对我不起作用: 我收到此错误:

  • 参数 0:无法确定
  • 的 Numba 类型

我正在传递: a = ct.c_double(1.0) b = ct.c_double(2.0)

打印(测试(a,b))

© www.soinside.com 2019 - 2024. All rights reserved.