I want to call a C function from the dynamic library in no python mode, and the input and output of this function are Pointers to structs。
from numba import njit, cfunc, carray
import ctypes
import pathlib
from ctypes import *
#from numba.experiment import jitclass
libname = pathlib.Path().absolute() / "arrayadd.cpython-38-x86_64-linux-gnu.so"
c_lib = ctypes.CDLL(libname)
func = c_lib.add
class Array(Structure):
__fields__ = [("meminfo", POINTER(c_int)),
("parent", POINTER(c_int)),
("nitems", c_int),
("itemsize", c_int),
("data", POINTER(c_int)),
("shape", c_int * 4),
("strides", c_int * 4)]
func.argtypes = [POINTER(Array), POINTER(Array)]
func.restype = POINTER(Array)
a = Array()
a.nitems = 10000
a.itemsize = 4
a.shape = (c_int * 4)(10, 10, 10, 10)
a.strides = (c_int * 4)(1000, 100, 10, 1)
py_values = [i for i in range(a.nitems)]
a.data = (c_int * a.nitems)(*py_values)
b = Array()
b.nitems = 10000
b.itemsize = 4
b.shape = (c_int * 4)(10, 10, 10, 10)
b.strides = (c_int * 4)(1000, 100, 10, 1)
b.data = (c_int * b.nitems)(*py_values)
pointer_a = pointer(a)
pointer_b = pointer(b)
@njit
def test_add():
c = func(pointer_a, pointer_b)
return c
test_add()
The above code will give me an error. How can I change it, or how can I call dynamic library functions in numba?