用Cython C code语言编译失败,类型化memoryviews [英] cython C code compilation fails with typed memoryviews

查看:268
本文介绍了用Cython C code语言编译失败,类型化memoryviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code编译没有在用Cython问题:

The following code compiles without issues in cython:

cdef class Double:
    cdef double v
    def __init__(self, double v):
        self.v = v
    cdef double incr(self) nogil:
        self.v += 1
        return self.v

cdef int f(Double[:] xs):
    cdef int i, N
    N = xs.shape[0]
    for i in range(N):
        xs[i].incr()
    return 0

然而,当我尝试编译生成的C code,gcc编译器将停止与一个错误:

However, when I try to compile the generated C code, the gcc compiler stops with an error:

test.c: In function '__pyx_f_4test_f':
test.c:1491:55: error: 'PyObject {aka struct _object}' has no member named '__pyx_vtab'
     ((struct __pyx_vtabstruct_4test_Double *)__pyx_t_3->__pyx_vtab)->incr(__pyx_t_3);

在哪里的错误,我在做什么?需要注意的是,当我使用标准memorytypes(例如,使用同一code没有任何问题,双重[:] XS 参数在˚F功能)。

此外,有没有办法用来释放里面的的吉˚F函数双[:] ?如果我尝试了,我得到一个错误信息

Additionally, is there a way to release the gil inside the f function with Double[:]? If I try that, I get an error message

test.pyx:13:10: Cannot access buffer with object dtype without gil

纵观产生code,我看到了几个电话到 __ Pyx_INCREF __ Pyx_DECREF ,其中然而,似乎并不需要我。

Looking at the generated code, I see a couple of calls to __Pyx_INCREF and __Pyx_DECREF, which however seems not needed to me.

修改

经过一些讨论,我现在可以找到变通方法,并能够编译C code:例如使用对象[:] XS 在函数定义中,然后抹上(小于双>(XS [I]))INCR()在for循环。然而,这些解决方法引入更蟒蛇互动,这是真的,我想什么来避免。

Following some discussion, I can now find workarounds and be able to compile the C code: for example use object[:] xs in the function definition, and then cast (<Double>(xs[i])).incr() in the for loop. However, these workaround introduce even more python interaction, which is really what I would like to avoid.

在更一般的问题是:有在用Cython的方式来处理的CDEF类同质列表(类型化的缓冲区或类似)所以,没有蟒开销和GIL可以释放?

In more general terms, the question is: is there a way in cython to deal with a homogeneous list of cdef class (a typed buffer or similar) so that there is no python overhead and the GIL can be released?

推荐答案

请检查这个话题,你的情况可能是类似于它:
<一href=\"http://stackoverflow.com/questions/31119510/cython-have-sequence-of-extension-types-as-attribute-of-another-extension-type\">Cython:有扩展类型为能够访问CDEF方法的另一个扩展类型属性的序列

Please check this topic, your case is probably similar to it: Cython: have sequence of Extension Types as attribute of another Extension Type with access to cdef methods

请尝试也为谷歌搜索 __ pyx_vtab - 搜索结果显示了谁也有类似的问题,我查了第一个简单,看起来有些解决方案是有太多的问题的人民:
https://groups.google.com/forum/#​​!topic/cython -Users / 3tUDIc11Xak

Please also try Googling for __pyx_vtab - search results shows questions from peoples who has similar problem, I checked first one briefly and looks that some solution is there too: https://groups.google.com/forum/#!topic/cython-users/3tUDIc11Xak

这篇关于用Cython C code语言编译失败,类型化memoryviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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