如何将C数组的指针转换为python数组 [英] How to convert pointer to c array to python array

查看:479
本文介绍了如何将C数组的指针转换为python数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++回调函数,可使用ctypes调用Python。此函数的参数是一个指向double和元素数量数组的指针。

I have a C++ callback function that calls into Python using ctypes. This function's parameters are a pointer to an array of double and the number of elements.

元素很多,大约2,000,000。我需要将其发送到scipy函数中。

There are a lot of elements, approximately 2,000,000. I need to send this into scipy functions.

C ++原型为:

bool (*ptsetDataSource)(double*, long long);

这是以下python代码:

which is the following python code:

CPF_setDataSource = CFUNCTYPE(c_bool, POINTER(c_double),c_longlong)
CPF_setSelection= CFUNCTYPE(c_bool,c_char_p, c_longlong,c_longlong)
CPF_ResetSequence = CFUNCTYPE(c_bool)

def setDataSource(Data, DataLength):
    Datalist=[0.0]*100
    for i in range(0,100):
        Datalist[i]=Data[i]

    print Datalist
    return True

问题是打印数据列表返回:

The problem is that print datalist returns:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

这是不正确的(在c ++端进行检查时,数据填充了很多其他数字。

which is not correct(data is filled with a lot of other numbers when checked on the c++ side.

此外,如果我使用此代码将数据转换为python列表,则会在分配步骤锁定计算机。

Also, if I use this code to convert the data to a python list, it locks up the computer at the allocate step.

是否有从C ++数组加载数据,然后将其转换为适合scipy的数组?

Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

推荐答案

如果 Data (c_double * DataLength.value)数组,则您可以:

If Data were (c_double*DataLength.value) array then you could:

a = np.frombuffer(Data) # no copy. Changes in `a` are reflected in `Data`

如果 Data POINTER(c_double),您可以使用 numpy.fromiter() 。与您的问题相同,但循环更快:

If Data is a POINTER(c_double) you could get numpy array using numpy.fromiter(). It is the same loop as in your question but faster:

a = np.fromiter(Data, dtype=np.float, count=DataLength.value) # copy

要从 POINTER创建一个numpy数组(c_double)实例而不进行复制,则可以使用 .from_address () 方法:

To create a numpy array from POINTER(c_double) instance without copying you could use .from_address() method:

ArrayType = ctypes.c_double*DataLength.value
addr = ctypes.addressof(Data.contents)
a = np.frombuffer(ArrayType.from_address(addr))

array_pointer = ctypes.cast(Data, ctypes.POINTER(ArrayType))
a = np.frombuffer(array_pointer.contents)

两种方法都转换 POINTER( c_double)实例传递到(c_double * DataLength)实例,然后再将其传递给 numpy.frombuffer()

Both methods convert POINTER(c_double) instance to (c_double*DataLength) before passing it to numpy.frombuffer().


是否仍然可以从C ++数组加载数据,然后将其转换为适合scipy的数组?

Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

以下是Python的C扩展模块(用Cython编写),提供了C API转换功能:

Here's C extension module for Python (written in Cython) that provide as C API the conversion function:

cimport numpy as np
np.import_array() # initialize C API to call PyArray_SimpleNewFromData

cdef public api tonumpyarray(double* data, long long size) with gil:
    if not (data and size >= 0): raise ValueError
    cdef np.npy_intp dims = size
    #NOTE: it doesn't take ownership of `data`. You must free `data` yourself
    return np.PyArray_SimpleNewFromData(1, &dims, np.NPY_DOUBLE, <void*>data)

它可以与 ctypes 一起使用,如下所示:

It could be used with ctypes as follows:

from ctypes import (PYFUNCTYPE, py_object, POINTER, c_double, c_longlong,
                    pydll, CFUNCTYPE, c_bool, cdll)

import pointer2ndarray
tonumpyarray = PYFUNCTYPE(py_object, POINTER(c_double), c_longlong)(
    ("tonumpyarray", pydll.LoadLibrary(pointer2ndarray.__file__)))

@CFUNCTYPE(c_bool, POINTER(c_double), c_longlong)
def callback(data, size):
    a = tonumpyarray(data, size)
    # call scipy functions on the `a` array here
    return True

cpplib = cdll.LoadLibrary("call_callback.so") # your C++ lib goes here
cpplib.call_callback(callback)

其中 call_callback 是: void call_callback(bool(*)(double *,long long))

这篇关于如何将C数组的指针转换为python数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆