I am trying to integrate some numerical code written in C into the Python library using numpy and ctypes. I already have the actual calculations, but now I would like to report on the progress of the intermediate steps of my callback function algorithm in my Python code. Although I can call the callback function successfully, I cannot get the data in the array xpassed to the callback. In a callback x, it is an object ndpointerthat cannot look dereferenced.
Current code
Consider this minimal example:
test.h:
typedef void (*callback_t)(
double *x,
int n
);
void callback_test(double* x, int n, callback_t callback);
test.c:
#include "test.h"
void callback_test(double* x, int n, callback_t callback) {
for(int i = 1; i <= 5; i++) {
for(int j = 0; j < n; j++) {
x[j] = x[j] / i;
}
callback(x, n);
}
}
test.py:
import numpy as np
import numpy.ctypeslib as npct
import ctypes
import os.path
array_1d_double = npct.ndpointer(dtype=np.double, ndim=1, flags='CONTIGUOUS')
callback_func = ctypes.CFUNCTYPE(
None,
array_1d_double,
ctypes.c_int
)
libtest = npct.load_library('libtest', os.path.dirname(__file__))
libtest.callback_test.restype = None
libtest.callback_test.argtypes = [array_1d_double, ctypes.c_int, callback_func]
@callback_func
def callback(x, n):
print("x: {0}, n: {1}".format(x, n))
if __name__ == '__main__':
x = np.array([20, 13, 8, 100, 1, 3], dtype=np.double)
libtest.callback_test(x, x.shape[0], callback)
Current output
After compiling and running the script, I get the following output:
x: <ndpointer_<f8_1d_CONTIGUOUS object at 0x7f9b55faba70>, n: 6
x: <ndpointer_<f8_1d_CONTIGUOUS object at 0x7f9b55faba70>, n: 6
x: <ndpointer_<f8_1d_CONTIGUOUS object at 0x7f9b55faba70>, n: 6
x: <ndpointer_<f8_1d_CONTIGUOUS object at 0x7f9b55faba70>, n: 6
x: <ndpointer_<f8_1d_CONTIGUOUS object at 0x7f9b55faba70>, n: 6
x[0:n] (TypeError: 'ndpointer_x.value( ).
callback_func:
callback_func = ctypes.CFUNCTYPE(
None,
ctypes.POINTER(ctypes.c_double),
ctypes.c_int
)
:
@callback_func
def callback(x, n):
print("x: {0}, n: {1}".format(x[:n], n))
:
x: [20.0, 13.0, 8.0, 100.0, 1.0, 3.0], n: 6
x: [10.0, 6.5, 4.0, 50.0, 0.5, 1.5], n: 6
x: [3.3333333333333335, 2.1666666666666665, 1.3333333333333333, 16.666666666666668, 0.16666666666666666, 0.5], n: 6
x: [0.8333333333333334, 0.5416666666666666, 0.3333333333333333, 4.166666666666667, 0.041666666666666664, 0.125], n: 6
x: [0.16666666666666669, 0.10833333333333332, 0.06666666666666667, 0.8333333333333334, 0.008333333333333333, 0.025], n: 6
numpy-ish x ? , numpy.array, , ndpointer, x ( )
gist -, .