如何访问作为numpy数组传递给ctypes回调的数组? [英] How to access arrays passed to ctypes callbacks as numpy arrays?
问题描述
我正在尝试使用numpy和ctypes将一些用C语言编写的数字代码集成到Python库中.我已经进行了实际的计算,但是现在想向我的Python代码中的回调函数报告算法的中间步骤的进度.虽然我可以成功调用回调函数,但无法在传递给回调的x
数组中检索数据.在回调中,x
是我似乎无法取消引用的ndpointer
对象.
当前代码
考虑以下最小示例:
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:
#!/usr/bin/env python
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, # return
array_1d_double, # x
ctypes.c_int # n
)
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)
电流输出
编译并运行脚本后,得到以下输出:
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, # return
ctypes.POINTER(ctypes.c_double), # x
ctypes.c_int # n
)
以及以下替代回调函数:
@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
我的问题
在回调中是否还有更多访问x
的方式?我宁愿访问ndpointer指向的数据,也不想下标然后转换回numpy.array
,因为我想限制x
的副本数量(并且为了简洁的代码)>
如果您想尝试使用上传整个迷你示例的要旨,我的代码.
我已经找到了使用 有什么更优雅的解决方案的人,也许使用 I'm trying to integrate some numerical code written in C into a Python library using numpy and ctypes. I've already got the actual computations working but would now like to report the progress of the intermediate steps of my algorithm to a callback function in my Python code. While I can call the callback function successfully, I'm not able to retrieve the data in the Consider this minimal example: test.h: test.c: test.py:
After compiling and running the script, I get the following output: I've also tried the subsetting operator If I use the following alternative definition of and the following alternative callback function: I get the desired results:
Is there a more more numpy-ish way of accessing I've uploaded a gist of the whole mini-example if you want to experiment on my code. I've found a solution using [...] Anyone with a more elegant solution, perhaps using the 这篇关于如何访问作为numpy数组传递给ctypes回调的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!ctypes.POINTER(ctypes.c_double)
和ndpointer
对象?x
array passed to the callback. In the callback, x
is an ndpointer
object that I can't seem to dereference.Current Code
typedef void (*callback_t)(
double *x,
int n
);
void callback_test(double* x, int n, callback_t callback);
#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);
}
}
#!/usr/bin/env python
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, # return
array_1d_double, # x
ctypes.c_int # n
)
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
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 (returns the pointer as a number).Hackish Solution
callback_func
:callback_func = ctypes.CFUNCTYPE(
None, # return
ctypes.POINTER(ctypes.c_double), # x
ctypes.c_int # n
)
@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
My Question
x
in the callback? Rather than subscripting and then converting back to a numpy.array
, I would prefer to access the data pointed to by the ndpointer since I would like to limit the amount of copies of x
(and for the sake of elegant code)ctypes.POINTER(ctypes.c_double)
and numpy.ctypeslib.as_array - according to the numpy.ctypeslib documentation, this will share the memory with the array:callback_func = ctypes.CFUNCTYPE(
None, # return
ctypes.POINTER(ctypes.c_double), # x
ctypes.c_int # n
)
@callback_func
def callback(x, n):
x = npct.as_array(x, (n,))
print("x: {0}, n: {1}".format(x, n))
ndpointer
objects?