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

Python 如何访问作为numpy数组传递给ctypes回调的数组?,python,numpy,ctypes,Python,Numpy,Ctypes,我正在尝试使用numpy和ctypes将一些用C编写的数字代码集成到Python库中。我已经完成了实际的计算,但现在想向Python代码中的回调函数报告算法中间步骤的进度。虽然我可以成功调用回调函数,但无法检索传递给回调函数的x数组中的数据。在回调中,x是一个我似乎无法取消引用的ndpointer对象 现行代码 考虑这个最小的例子: test.h: typedef void (*callback_t)( double *x, int n ); void callback_tes

我正在尝试使用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);
#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)
test.c:

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)
电流输出 编译并运行脚本后,我得到以下输出:

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
以及以下可选回调函数:

@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
?与其订阅然后转换回
numpy.array
,我更愿意访问ndpointer指向的数据,因为我想限制
x
的副本数量(并且为了优雅的代码)


如果您想在我的代码上进行实验,我已经找到了整个小示例的一部分。

我找到了一个使用
ctypes.POINTER(ctypes.c_double)
和-根据numpy.ctypeslib文档,这将与数组共享内存:

callback_func = ctypes.CFUNCTYPE(
    None,            # return
    ctypes.POINTER(ctypes.c_double), # x
    ctypes.c_int     # n
)
[……]


任何有更优雅解决方案的人,可能使用
ndpointer
对象?

ctypeslib.as\u array
可以在创建NumPy数组时指定
形状。代价是它必须为每个指针实例准备数组接口;但是,它会将其作为ctypes数组类型上的
属性进行缓存。
ndpointer
创建
\ndptr
的子类,该子类是
c\u void\u p
的子类,具有NumPY
\u数组\u接口。它使用_param
中的ctypes
对函数指针参数进行类型验证,然后将其推送到
obj.ctypes
。它使用ctypes
\u check\u retval\u
重新键入创建一个数组;这将返回一个NumPy数组副本(您可以将其修补为使用
copy=False
),并且需要一个定义
形状
元组的数组接口。如果已经定义了
shape
,则可以在回调中使用
x=np.array(x,copy=False)