Hi.

I am working on a proposal to add some kind of a mechanism for a Python
programmer to specify that a loop should be parallel. Think OpenMP style
parallel for loops. For people not familiar with OpenMP, it provides a very
simple parallel programming constructs which are specified as "pragma"
directives. A compiler is free to skip them if it doesnt implement OpenMP.
In OpenMP, you can specify some loops as parallel loops and the compiler and
runtime take care of spawning threads etc. Compiler automatically spawns
threads at the start of a parallel loop and joins at the end.

The objective is to add such optional annotations which the Python
interpreter will ignore but which can (but is not mandatorily) be used by a
compiler such as Cython or my compiler unPython. The annotations will be
added to only certain predefined type of loops since parallel iteration over
generic objects is not possible.  I am writing a detailed proposal document
and will post for your consideration in 1-2 days. However it would be great
if someone has some ideas about this already or if you can point to similar
work in Python or other languages. I know a "parfor" has been added to
Matlab. I do not know if Cython has such a facility.

My original suggestion :

"pragma parallel for"
for i in xrange(n):x[i] = y[i]

On numpy-discussion list, two better alternatives were suggested.

#pragma:parallel for
for i in xrange(n):x[i] = y[i]

with parallel(i):
   for i in xrange(n):x[i] = y[i]

I was told that IPython already does something similar to "with parallel(i)"
and I am looking into it.
I think we can also do something like :

for i in prange(i): x[i] = y[i]

prange can simply default to xrange when running on the interpreter but the
compiler can think of prange as a special object.
I selfishly want to avoid annotations specified in comments since my
compiler cannot see comments :)

Semantics needs to be defined properly. For example, exceptions cannot be
raised in proper order in parallel loops.
Any ideas/suggestions/flames? OpenMP has many more features such as
reduction variables. OpenMP is a very simple parallel programming model and
introducing something similar in Python can be really helpful.

For an implementation perspectives, it will be a little challenging to
generate code while avoiding the GIL but it can be done in simple cases.

thanks,
rahul
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to