Python ctypes中用于C结构体的定义

11

我正尝试调用由Matlab Coder生成的一些C代码。Matlab使用名为emxArray的C结构体表示矩阵(这里有文档:http://www.mathworks.co.uk/help/fixedpoint/ug/c-code-interface-for-unbounded-arrays-and-structure-fields.html)。

struct emxArray_real_T
{
    double *data;
    int *size;
    int allocatedSize;
    int numDimensions;
    boolean_T canFreeData;
};

我对ctypes的经验很少,我正在努力创建一个等效的结构体,以便可以将向量来回传递给在c .so中定义的函数。

这是我在Python中目前的进展...

class EmxArray(ctypes.Structure):
    """ creates a struct to match emxArray_real_T """

    _fields_ = [('data', ctypes.POINTER(ctypes.c_double)),
                ('size', ctypes.POINTER(ctypes.c_int)),
                ('allocatedSize', ctypes.c_int),
                ('numDimensions', ctypes.c_int),
                ('canFreeData', ctypes.c_bool)]    

然而,如果我定义了这个:

data = (1.1, 1.2, 1.3, 1.4)
L = len(data)

x = EmxArray()
x.data = (ctypes.c_double * L)(*data)
x.data = (ctypes.c_int * 1)(L)    

这样就可以工作了

print len(x.data[:L]) 

for v in x.data[:L]: print v

编辑:我已整理并采纳了Roland的建议,可以使用以下方法提取数据

data_out = x.data[:L]

我需要进一步调查,以确定我是否可以成功使用这个结构体来传递和接收来自 C 代码的数据。

解决方案

按照 Roland 建议的实现 ctypes 结构并未奏效——返回的值是垃圾值,我也没搞清楚为什么,因此我转而尝试了 lilbil 答案中基于 Python 的实现。我已经接受了那个答案,因为它最接近……

我会在这里记录我的解决方案,因为这可能会节省其他人浪费和我一样多的时间。

首先,我生成了一个简单的 Matlab 函数,将每个元素乘以自己,并使用编译器将其编译成 c .so 文件。这个文件使用 ctypes 导入到 Python 中。代码如下:

import ctypes

LIBTEST = '..../dll/emx_test/'
EMX = ctypes.cdll.LoadLibrary(LIBTEST + 'emx_test.so')
init = EMX.emx_test_initialize()

# Create a data structure to hold the pointer generated by emxCreateWrapper...
class Opaque(ctypes.Structure):
    pass

# make some random data to pass in
data_in = [1., 2., 4., 8., 16.]
L = len(data_in)
# create an empty array of the same size for the output
data_ou = [0] * L

# put this in a ctypes array
ina = (ctypes.c_double * L)(*data_in)
oua = (ctypes.c_double * L)(*data_ou)
# create a pointer for these arrays & set the rows and columns of the matrix
inp = ctypes.pointer(ina)
oup = ctypes.pointer(oua)

nrows = ctypes.c_int(1)
ncols = ctypes.c_int(L)

# use EMX.emxCreateWrapper_real_T(double *data, int rows, int cols) to generate an emx wrapping the data 
# input arg types are a pointer to the data NOTE its not great to have to resize the ctypes.c_double but cant see another way
EMX.emxCreateWrapper_real_T.argtypes = [ctypes.POINTER(ctypes.c_double * L), ctypes.c_int, ctypes.c_int]
# a pointer to the emxArray is returned and stored in Opaque
EMX.emxCreateWrapper_real_T.restype = ctypes.POINTER(Opaque)
# use emxCreateWrapper
in_emx = EMX.emxCreateWrapper_real_T(inp, nrows, ncols)
ou_emx = EMX.emxCreateWrapper_real_T(oup, nrows, ncols)

# so now we have to emx's created and have pointers to them we can run the emx_test
# emx test looks like this in matlab
#
# function res = emx_test ( in )
#     res = in .* in;
# end
#
# so basically it multiplies each element of the matrix by itself
# 
# therefore [1., 2., 4., 8., 16.] should become [1., 4., 8., 64., 256.]

EMX.emx_test(in_emx, ou_emx)

# and voila...that's what we get
print 'In: ', ina[:L]
print 'Out:', oua[:L]

输出:

In: [1.0, 2.0, 4.0, 8.0, 16.0]
Out:[1.0, 4.0, 16.0, 64.0, 256.0]

感谢大家抽出时间并提供建议。


我觉得你可能不需要 EmxArray 类中的 sz...*sz))' 部分。 - Al.Sal
我这样做的原因是数据将会是一个数组,如果我能允许可变大小的数组,那就最好了。 - Trevor Sweetnam
2个回答

12

只需创建一个指针,然后分配数据即可;

import ctypes

class EmxArray(ctypes.Structure):
    """ creates a struct to match emxArray_real_T """

    _fields_ = [('data', ctypes.POINTER(ctypes.c_double)),
                ('size', ctypes.POINTER(ctypes.c_int)),
                ('allocatedSize', ctypes.c_int),
                ('numDimensions', ctypes.c_int),
                ('canFreeData', ctypes.c_bool)]

data = (1.3, 3.5, 2.7, 4.1)
L = len(data)

e = EmxArray()
e.data = (ctypes.c_double * L)(*data)
e.size = (ctypes.c_int * 1)(L)
# et cetera

3
我对Python-C接口不熟悉,所以我的建议可能并不理想。我的猜测是,崩溃可能是因为->data没有被初始化且指向的内存未被分配。当我在其他语言中使用MATLAB Coder生成的代码与 emxArray 参数进行接口时,采用的方法是手写一个C接口函数,提供更简单的API。这样可以减轻在其他环境(我个人的情况是Android Java)中构造 emxArray 的负担。如果生成的函数foo接收和返回一个2-D double数组,则以下内容可能有效:
void foo(double *x, int *szx, double **y, int *szy);

这个函数将接受输入数据的指针和大小,并提供输出数据的指针和大小。实现大致如下:
void foo(double *x, int *szx, double **y, int *szy) 
{
  emxArray_real_T *pEmx;
  emxArray_real_T *pEmy;

  /* Create input emxArray assuming 2-dimensional input */
  pEmx = emxCreateWrapper_real_T(x, szx[0], szx[1]);

  /* Create output emxArray (assumes that the output is not */
  /* written before allocation occurs) assuming 2-D output  */
  pEmy = emxCreateWrapper_real_T(NULL, 0, 0);

  /* Call generated code (call foobar_initialize/terminate elsewhere) */
  foobar(pEmx, pEmy);

  /* Unpack result - You may want to MALLOC storage in *y and */
  /* MEMCPY there alternatively                               */
  *y = pEmy->data;
  szy[0] = pEmy->size[0];
  szy[1] = pEmy->size[1];

  /* Clean up any memory allocated in the emxArrays (e.g. the size vectors) */
  emxDestroyArray_real_T(pEmx);
  emxDestroyArray_real_T(pEmy);
}

你应该可以更简单地从Python调用此函数,并根据需要传入所需的数据。 我的其他答案中有关于foobar_emxAPI.h文件中找到的emxArray_*函数的更多细节。

感谢您提供这么详细的回答。我认为这可能是正确的方法,所以我今天将探索这个途径。我目前正在编写一个虚拟的Matlab脚本,它只是取一个emx_array平方并将其作为一个emx_array返回。我最终需要使用的库定义了多个函数,因此包装更加繁琐,我想知道是否有可能定义一个通用函数,它接收一个向量并定义一个可以传递给函数和反之的emx_array。 - Trevor Sweetnam
这可能是可能的。库参数的类型和复杂度如何不同?通常,差异可能存在于参数的类型,复杂性或维数数量中。更改这些将需要转换例程的行为更改。您的转换例程是否可以用C ++编写?如果可以,它可以被特殊化为不同类型的模板,知道如何在foo_emxApi.h中调用适当的函数。 - Ryan Livingston
我有一个替代思路,那就是你可以在Python中声明一个指向适当emxArray类型的指针。然后,你可以调用生成的 emxCreate*emxCreateWrapper*函数,从Python代码初始化该指针。这样做应该可以最小化你需要编写的C语言数量,并且这些函数具有带数字参数的简单签名。这也可能让你在Python中编写矢量<--> emxArray转换功能。 - Ryan Livingston

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