gpt4 book ai didi

python - 如何访问作为 numpy 数组传递给 ctypes 回调的数组?

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:08 24 4
gpt4 key购买 nike

我正在尝试使用 numpy 和 ctypes 将一些用 C 编写的数字代码集成到 Python 库中。我已经进行了实际的计算,但现在想将算法中间步骤的进度报告给 Python 代码中的回调函数。虽然我可以成功调用回调函数,但我无法检索传递给回调的 x 数组中的数据。在回调中,x 是一个我似乎无法取消引用的 ndpointer 对象。

当前代码

考虑这个最小的例子:

测试.h:

typedef void (*callback_t)(
double *x,
int n
);

void callback_test(double* x, int n, callback_t callback);

测试.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);
}
}

测试.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](类型错误:'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

我的问题

是否有更 NumPy 的方式来访问回调中的 x?与其下标然后转换回 numpy.array,我更愿意访问 ndpointer 指向的数据,因为我想限制 x 的副本数量>(并且为了优雅的代码)

我已经 uploaded a gist如果您想对我的代码进行试验,请查看整个小示例。

最佳答案

我找到了使用 ctypes.POINTER(ctypes.c_double)numpy.ctypeslib.as_array 的解决方案- 根据 numpy.ctypeslib 文档,这将与数组共享内存:

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 对象?

关于python - 如何访问作为 numpy 数组传递给 ctypes 回调的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19702372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com