I've been searching a solution for this in the past days, and it seems I'm not able to solve it. I have a C function that loops over some arrays. The function is called from python, and the arrays are contiguous numpy arrays, and all the arrays are allocated from the python side, the C function just uses the arrays and write in one output array. The interface that I have written works just fine, but right now I need to @njit the python function, which is called from other python functions that will be jitted too (I'm aware that jitting a python function that only almost calls a C function won't have a speedup, but the more general python functions of the project will).
I can't make the interface with the C code work when I use @njit. I post here a very simple MWE with 2 numpy arrays. The C function is:
#include <stddef.h>
#include <sys/types.h>
void loopTestC(const size_t n, const double values[], double output[])
{
size_t i;
for (i=0; i<n; i++)
{
output[i] = values[i];
}
}
which I compile with
gcc -O0 -Wall -Wextra -pedantic -Warray-bounds -fPIC -shared -o simpleLoopC.so simpleLoopC.c
while the pyhon code, which works without @njit is:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import ctypes
from numba import jit, njit
lib = ctypes.cdll.LoadLibrary('./simpleLoopC.so')
loop=lib.loopTestC
loop.restype = None
loop.argtypes = [ctypes.c_size_t,
np.ctypeslib.ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
np.ctypeslib.ndpointer(ctypes.c_double, flags="C_CONTIGUOUS")]
# @njit(parallel=False)
def testC():
a = np.arange(100, dtype=np.float64)
b = np.zeros_like(a)
print(a, b)
n = a.size
loop(n, a, b)
print(a, b)
testC()
When I activate numba, I get the error >Unsupported ctypes type: <class 'numpy.ctypeslib.ndpointer_<f8_C_CONTIGUOUS'>. I tried with ctypes, and it looks like numba requires loop.argtypes to be defined, but I can't make it work. Can someone help me?
Thanks.