将 Numpy 数组传递给 C 函数以进行输入和输出 [英] Passing Numpy arrays to a C function for input and output

查看:26
本文介绍了将 Numpy 数组传递给 C 函数以进行输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哦,我是个傻瓜.我只是在调用函数时省略了第二个和第三个参数.像个傻瓜.因为这就是我.原来的愚蠢问题如下:

这个好像应该是很常见的事情了,但是找不到相关教程,对Numpyctypes太无知了自己想办法.

我在文件 ctest.c 中有一个 C 函数.

#include void cfun(const void * indatav, int rowcount, int colcount, void * outdatav) {//void cfun(const double * indata, int rowcount, int colcount, double * outdata) {const double * indata = (double *) indatav;double * outdata = (double *) outdatav;国际我;puts("我们开始吧!");for (i = 0; i < rowcount * colcount; ++i) {出数据[i] = 入数据[i] * 2;}puts("完成!");}

(正如您可能猜到的,我最初将参数设为 double * 而不是 void *,但无法弄清楚在 Python 方面该做什么.我当然很想将它们改回来,但我不挑剔,只要有效.)

我用它制作了一个共享库.gcc -fPIC -shared -o ctest.so ctest.c

然后在 Python 中,我有几个 numpy 数组,我想将它们传递给 C 函数,一个作为输入,一个作为输出.

indata = numpy.ones((5,6), dtype=numpy.double)outdata = numpy.zeros((5,6), dtype=numpy.double)lib = ctypes.cdll.LoadLibrary('./ctest.so')乐趣 = lib.cfun# 傻瓜部分来了.乐趣(ctypes.c_void_p(indata.ctypes.data),ctypes.c_void_p(outdata.ctypes.data))打印 'indata: %s' % indata打印输出数据:%s"% 输出数据

这不会报告任何错误,而是打印出来

<预><代码>>>>开始了!完毕!数据:[[ 1. 1. 1. 1. 1. 1.][ 1. 1. 1. 1. 1. 1. ][ 1. 1. 1. 1. 1. 1. ][ 1. 1. 1. 1. 1. 1. ][ 1. 1. 1. 1. 1. 1.]]输出数据:[[ 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.]]

未修改 outdata 数组.事实上,如果我再次调用该函数,则会出现段错误.这并不让我感到惊讶——我真的不知道我在这里做什么.有人能指出我正确的方向吗?

解决方案

只需将所有四个参数传递给 C 函数.从以下位置更改您的 Python 代码:

fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_void_p(outdata.ctypes.data))

致:

fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_int(5), ctypes.c_int(6),ctypes.c_void_p(outdata.ctypes.data))

Oh my word I'm a fool. I was simply omitting the second and third arguments when calling the function. Like a fool. Because that's what I am. Original silly question follows:

This seems like it must be a very common thing to do, but I can't find a relevant tutorial, and I'm too ignorant about Numpy and ctypes to figure it out myself.

I have a C function in file ctest.c.

#include <stdio.h>

void cfun(const void * indatav, int rowcount, int colcount, void * outdatav) {
    //void cfun(const double * indata, int rowcount, int colcount, double * outdata) {
    const double * indata = (double *) indatav;
    double * outdata = (double *) outdatav;
    int i;
    puts("Here we go!");
    for (i = 0; i < rowcount * colcount; ++i) {
        outdata[i] = indata[i] * 2;
    }
    puts("Done!");
}

(As you may guess, I originally had the arguments as double * rather than void *, but couldn't figure out what to do on the Python side. I'd certainly love to change them back, but I'm not picky as long as it works.)

I make a shared library out of it. gcc -fPIC -shared -o ctest.so ctest.c

Then in Python, I have a couple numpy arrays, and I'd like to pass them to the C function, one as input and one as output.

indata = numpy.ones((5,6), dtype=numpy.double)
outdata = numpy.zeros((5,6), dtype=numpy.double)
lib = ctypes.cdll.LoadLibrary('./ctest.so')
fun = lib.cfun
# Here comes the fool part.
fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_void_p(outdata.ctypes.data))

print 'indata: %s' % indata
print 'outdata: %s' % outdata

This doesn't report any errors, but prints out

>>> Here we go!
Done!
indata: [[ 1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.]]
outdata: [[ 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.]]

The outdata array is not modified. And in fact if I call the function again I get a segfault. Which doesn't surprise me -- I really don't know what I'm doing here. Can anyone point me in the right direction?

解决方案

Just pass all four arguments to the C function. Change your Python code from:

fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_void_p(outdata.ctypes.data))

To:

fun(ctypes.c_void_p(indata.ctypes.data), ctypes.c_int(5), ctypes.c_int(6),
    ctypes.c_void_p(outdata.ctypes.data))

这篇关于将 Numpy 数组传递给 C 函数以进行输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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