用Cython代码继续PRANGE返回值后
问题描述:
假设我有以下功能:用Cython代码继续PRANGE返回值后
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef bint test(np.int_t [:] values):
cdef Py_ssize_t n_values = len(values)
cdef int i
for i in prange(n_values,nogil=True):
if i ==0:
return 0
print 'test'
我运行它,就像这样:
In [1]: import algos
In [2]: import numpy as np
In [3]: algos.test(np.array([1,2,3,1,4,5]))
test
Out[3]: False
为什么打印功能时,它应该刚刚退出而不进行打印?有没有办法让函数在到达返回时退出?
谢谢。
答
documentation is clear that this is a bit of a minefield since there's no guarantee which iteration finishes first。我认为他们没有说清楚的额外复杂因素是,如果迭代次数足够小(然后提供一个线程),那么您也可以在prange
之后继续,这就是您所看到的。
似乎什么工作对我来说是使用一个循环,只有当它没有完成年初得到执行的else
条款:
for i in prange(n_values,nogil=True):
# stuff ...
else:
with gil:
print "test"
快速浏览一下C代码表明,这是进行适当的检查并且应该可靠。
说实话,这看起来很像一个bug,因为它似乎发生在任何大小的“prange”上,所以它可能值得报告。 https://github.com/cython/cython/issues/ – DavidW