Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prange generates unnecessary python interactions when indexing a 2d view #2987

Open
NicolasHug opened this issue Jun 7, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@NicolasHug
Copy link

commented Jun 7, 2019

Indexing a 2d view in a prange will generate some unnecessary python interactions related to the GIL.

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[0], 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[0], 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

@robertwb

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2019

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 __PYX_{INC,DEC}_MEMVIEW utilities. Even if we had stronger analysis, we'd have to know that sum_1d_b doesn't cache it away somewhere, so it's hard to see how we'd do better here.

@NicolasHug

This comment has been minimized.

Copy link
Author

commented Jun 14, 2019

Thanks for your feedback.

I'm a bit confused about memory views needing the GIL, because changing prange to range will remove the python interactions. Even in a with nogil: context manager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.