我正在使用prange
來修改數組。我努力以儘可能少的黃線儘可能由用Cython編譯器生成的HTML頁面:cython prange - 處理__Pyx_ErrFetchWithState/__Pyx_ErrRestoreWithState
cython function_prange.pyx -a
但提取的陣列,以修改它的一部分的時候,我總是有這樣的代碼生成:
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
#endif
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_exc_type)
#endif /* _OPENMP */
if (!__pyx_parallel_exc_type) {
__Pyx_ErrFetchWithState(&__pyx_parallel_exc_type, &__pyx_parallel_exc_value, &__pyx_parallel_exc_tb);
__pyx_parallel_filename = __pyx_filename; __pyx_parallel_lineno = __pyx_lineno; __pyx_parallel_clineno = __pyx_clineno;
__Pyx_GOTREF(__pyx_parallel_exc_type);
}
#ifdef WITH_THREAD
PyGILState_Release(__pyx_gilstate_save);
#endif
}
是否有可能避免這些__Pyx_ErrFetchWithState
/__Pyx_ErrRestoreWithState
?真的有關係嗎?
在這裏的是,我使用的代碼:
第一功能添加雙對角線包含在大小爲一陣列的矩陣在function_nogil.pyx
import cython
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
cdef void add_diag(double [:,:,:] a, int a_len, int a_wid, double coeff) nogil:
cdef int x_max = a_len
cdef int x
for x in xrange(x_max):
a[0,x,x] = a[0,x,x] + coeff
及其在頭使用function_nogil.pxd
cdef void add_diag(double [:,:,:] a, int a_len, int a_wid, double coeff) nogil
功能在function_prange.pyx
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def prange_loop_idx(double [:,:,:] a, int a_dim1, int a_dim2, int a_dim3, double coeff,int num_threads):
cdef int i = 0
with nogil,parallel(num_threads=num_threads):
for i in prange(a_dim1):
add_diag(a[i:i+1:,],a_dim2,a_dim3,coeff)