[Cython] CEP: prange for parallel loops

Stefan Behnel stefan_ml at behnel.de
Mon Apr 4 13:23:50 CEST 2011


Dag Sverre Seljebotn, 04.04.2011 12:17:
> CEP up at http://wiki.cython.org/enhancements/prange

"""
Variable handling

Rather than explicit declaration of shared/private variables we rely on 
conventions:

     * Thread-shared: Variables that are only read and not written in the 
loop body are shared across threads. Variables that are only used in the 
else block are considered shared as well.

     * Thread-private: Variables that are assigned to in the loop body are 
thread-private. Obviously, the iteration counter is thread-private as well.

     * Reduction: Variables that only used on the LHS of an inplace 
operator, such as s above, are marked as targets for reduction. If the 
variable is also used in other ways (LHS of assignment or in an expression) 
it does instead turn into a thread-private variable. Note: This means that 
if one, e.g., inserts printf(... s) above, s is turned into a thread-local 
variable. OTOH, there is simply no way to correctly emulate the effect 
printf(... s) would have in a sequential loop, so such code must be 
discouraged anyway.
"""

What about simply (ab-)using Python semantics and creating a new inner 
scope for the prange loop body? That would basically make the loop behave 
like a closure function, but with the looping header at the 'right' place 
rather than after the closure.

Also, in the example, the local variable declaration of "tmp" outside of 
the loop looks somewhat misplaced, although it's precedented by 
comprehensions (which also have their own local scope in Cython).

Stefan


More information about the cython-devel mailing list