[Cython] Nested prange loops - (was: [cython-users] Converting to Python objects with nogil (inside prange for loop))

Lisandro Dalcin dalcinl at gmail.com
Mon Jul 18 06:58:10 EDT 2022


On Fri, 15 Jul 2022 at 11:03, Stefan Behnel <stefan_ml at behnel.de> wrote:


> nested prange loops seem to be a common gotcha for users. I can't say if
> there is ever a reason to do this, but at least I can't think of any.


Unless you make nested prange() emit the following C code:

#pragma omp parallel for collapse(2)
for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
        // do something
    }
}

This is a very valid use case, as users should not care whether which of M
or N (or both) are large enough to split across available threads, thus
leading to thread subutilization.
And the nice thing about it is that the Cython implementation is probably
trivial, just adding a `collapse(N)` clause in the outermost `#pragma omp
for`.

But  of course, there would be nasty details to address, like what to do if
the start of the inner "j" index depends on the outer "i" index (i.e a
triangular loop), or `break` or `continue` statements are used.

For reference, here you have a summary of the rules:
https://www.ibm.com/docs/en/xl-fortran-linux/16.1.1?topic=clauses-collapse

For
> me, this sounds like we should turn it into a compile time error – unless
> someone can think of a use case?


Well, I just gave you one, but if you want to add checks for bad usage and
corner cases, the burden put on Cython development may be too high.

I would just blindly transform nested prange() calls to a `collapse(N)`
clause in the outermost `#pragma omp for` loop, and either let the C
compiler complain about the user code breaking the rules, or let users pay
for the consequences of breaking the Open MP rules with undefined behavior.


-- 
Lisandro Dalcin
============
Senior Research Scientist
Extreme Computing Research Center (ECRC)
King Abdullah University of Science and Technology (KAUST)
http://ecrc.kaust.edu.sa/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/cython-devel/attachments/20220718/ae46e7c2/attachment.html>


More information about the cython-devel mailing list