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

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

问题描述

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

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:

这似乎肯定是很常见的事情,但是我找不到相关的教程,而且我对Numpyctypes太不了解,无法自己解决.

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.

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

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!");
}

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

(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.)

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

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

然后在Python中,我有几个numpy数组,我想将它们传递给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.]]

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

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?

推荐答案

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

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))

收件人:

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天全站免登陆