如何使用NumPy数组与ctypes?

19

我仍在使用ctypes编写Python接口来调用我的C代码。今天,我用别人使用NumPy编写的Python版本替换了我的文件读取函数。旧的C版本使用byref(p_data)调用,而p_data=PFloat()(见下文)。主函数使用p_data

旧的文件读取:

p_data=POINTER(c_float)
foo.read(filename,byref(p_data))
result=foo.pymain(p_data)

另一方面,Python文件读取函数返回一个NumPy数组。我的问题是:

如何将NumPy数组转换为POINTER(c_float)

我搜索了一下,但只找到了相反的情况:通过ctypes访问的C数组作为NumPy数组和我不理解的东西:C-Types Foreign Function Interface(numpy.ctypeslib)

[更新] 修正了示例代码中的错误。

2个回答

37

你的代码似乎存在一些混淆 -- ctypes.POINTER() 创建的是一个新的 ctypes 指针 ,而不是 ctypes 实例。不管怎样,将 NumPy 数组传递给 ctypes 代码的最简单方法是使用 numpy.ndarrayctypes 属性的 data_as 方法。确保底层数据首先是正确类型的即可。例如:

import ctypes
import numpy
c_float_p = ctypes.POINTER(ctypes.c_float)
data = numpy.array([[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]])
data = data.astype(numpy.float32)
data_p = data.ctypes.data_as(c_float_p)

在执行上述步骤后,NumPy数组是否总是连续的? - grabbag
@grabbag ndarray的底层支持存储器始终是连续的,所以是的,虽然那可能不是你需要知道的确切信息。为了通用地处理数组,您需要解释.ctypes对象的.shape.strides属性,并注意文档中关于.data.data_as()的注意事项。https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ctypes.html - llasram

12

使用np.ndarrays作为ctypes参数

更好的方法是使用ndpointer,如numpy-docs中所述。

这种方法比使用POINTER(c_double)更灵活,因为可以指定多个限制条件,在调用ctypes函数时进行验证。这些条件包括数据类型、维数、形状和标志。如果给定数组不满足指定的限制条件,则会引发TypeError

最小可复现示例

从Python调用memcpy。最终需要调整标准C库libc.so.6的文件名。

import ctypes
import numpy as np

n_bytes_f64 = 8
nrows = 2
ncols = 5

clib = ctypes.cdll.LoadLibrary("libc.so.6")

clib.memcpy.argtypes = [
    np.ctypeslib.ndpointer(dtype=np.float64, ndim=2, flags='C_CONTIGUOUS'),
    np.ctypeslib.ndpointer(dtype=np.float64, ndim=1, flags='C_CONTIGUOUS'),
    ctypes.c_size_t]
clib.memcpy.restype = ctypes.c_void_p

arr_from = np.arange(nrows * ncols).astype(np.float64)
arr_to = np.empty(shape=(nrows, ncols), dtype=np.float64)

print('arr_from:', arr_from)
print('arr_to:', arr_to)

print('\ncalling clib.memcpy ...\n')
clib.memcpy(arr_to, arr_from, nrows * ncols * n_bytes_f64)

print('arr_from:', arr_from)
print('arr_to:', arr_to)

输出

arr_from: [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
arr_to: [[0.0e+000 4.9e-324 9.9e-324 1.5e-323 2.0e-323]
 [2.5e-323 3.0e-323 3.5e-323 4.0e-323 4.4e-323]]

calling clib.memcpy ...

arr_from: [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
arr_to: [[0. 1. 2. 3. 4.]
 [5. 6. 7. 8. 9.]]

如果您修改ndpointerndim=1/2参数,使其与arr_from/arr_to的维度不一致,代码将失败并出现ArgumentError

由于这个问题的标题非常一般化,...

ctypes.c_void_p结果构造np.ndarray

最小可重现示例

在下面的示例中,使用malloc分配了一些内存,并由memset填充为0。然后构造了一个numpy数组来访问这段内存。当然会出现所有权问题,因为python不会释放在c中分配的内存。为了避免内存泄漏,必须通过ctypes再次free已分配的内存。可以使用np.ndarraycopy方法来获得所有权

import ctypes
import numpy as np

n_bytes_int = 4
size = 7

clib = ctypes.cdll.LoadLibrary("libc.so.6")

clib.malloc.argtypes = [ctypes.c_size_t]
clib.malloc.restype = ctypes.c_void_p

clib.memset.argtypes = [
    ctypes.c_void_p,
    ctypes.c_int,
    ctypes.c_size_t]
clib.memset.restype = np.ctypeslib.ndpointer(
    dtype=np.int32, ndim=1, flags='C_CONTIGUOUS')

clib.free.argtypes = [ctypes.c_void_p]
clib.free.restype = ctypes.c_void_p


pntr = clib.malloc(size * n_bytes_int)
ndpntr = clib.memset(pntr, 0, size * n_bytes_int)
print(type(ndpntr))
ctypes_pntr = ctypes.cast(ndpntr, ctypes.POINTER(ctypes.c_int))
print(type(ctypes_pntr))
print()
arr_noowner = np.ctypeslib.as_array(ctypes_pntr, shape=(size,))
arr_owner = np.ctypeslib.as_array(ctypes_pntr, shape=(size,)).copy()
# arr_owner = arr_noowner.copy()


print('arr_noowner (at {:}): {:}'.format(arr_noowner.ctypes.data, arr_noowner))
print('arr_owner (at {:}): {:}'.format(arr_owner.ctypes.data, arr_owner))

print('\nfree allocated memory again ...\n')
_ = clib.free(pntr)

print('arr_noowner (at {:}): {:}'.format(arr_noowner.ctypes.data, arr_noowner))
print('arr_owner (at {:}): {:}'.format(arr_owner.ctypes.data, arr_owner))

print('\njust for fun: free some python-memory ...\n')
_ = clib.free(arr_owner.ctypes.data_as(ctypes.c_void_p))

print('arr_noowner (at {:}): {:}'.format(arr_noowner.ctypes.data, arr_noowner))
print('arr_owner (at {:}): {:}'.format(arr_owner.ctypes.data, arr_owner))

输出

<class 'numpy.ctypeslib.ndpointer_<i4_1d_C_CONTIGUOUS'>
<class '__main__.LP_c_int'>

arr_noowner (at 104719884831376): [0 0 0 0 0 0 0]
arr_owner (at 104719884827744): [0 0 0 0 0 0 0]

free allocated memory again ...

arr_noowner (at 104719884831376): [ -7687536     24381 -28516336     24381         0         0         0]
arr_owner (at 104719884827744): [0 0 0 0 0 0 0]

just for fun: free some python-memory ...

arr_noowner (at 104719884831376): [ -7687536     24381 -28516336     24381         0         0         0]
arr_owner (at 104719884827744): [ -7779696     24381 -28516336     24381         0         0         0]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接