Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
prange generates unnecessary python interactions when indexing a 2d view #2987
Indexing a 2d view in a
Here is a reproducing example. LMK if I can help with anything.
#cython: boundscheck=False #cython: language_level=3 from cython.parallel import prange # Indexing a 2d view in a prange loop: generates lots of python interactions. def sum_2d_a(float[:, :] view_2d): cdef: float out = 0 int row_idx = 0 for row_idx in prange(view_2d.shape, nogil=True): out += sum_1d_a(view_2d[row_idx, :]) # lots of python interactions at the end of the loop return out cdef float sum_1d_a(float[:] view_1d) nogil: return 3.4 # irrelevant code # workaround: pass the whole 2d view and the row index. No interactions in this case. def sum_2d_b(float[:, :] view_2d): cdef: float out = 0 int row_idx = 0 for row_idx in prange(view_2d.shape, nogil=True): out += sum_1d_b(view_2d, row_idx) # no python interaction return out cdef float sum_1d_b(float[:, :] view_2d, int row_idx) nogil: return 3.4 # irrelevant code
version : 0.29.6
Creation and (last) deletion of a slice require the GIL (to prevent the underlying memoryview from being deleted before the slice). If not done internally, this happens in the