2017-04-16 40 views
-1

假設我有以下功能:用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 

爲什麼打印功能時,它應該剛剛退出而不進行打印?有沒有辦法讓函數在到達返回時退出?

謝謝。

回答

1

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代碼表明,這是進行適當的檢查並且應該可靠。

+0

說實話,這看起來很像一個bug,因爲它似乎發生在任何大小的「prange」上,所以它可能值得報告。 https://github.com/cython/cython/issues/ – DavidW