[Cython] prange CEP updated

Dag Sverre Seljebotn d.s.seljebotn at astro.uio.no
Wed May 4 14:10:06 CEST 2011


On 05/04/2011 01:59 PM, mark florisson wrote:
> On 4 May 2011 13:54, Dag Sverre Seljebotn<d.s.seljebotn at astro.uio.no>  wrote:
>> On 05/04/2011 01:48 PM, mark florisson wrote:
>>>
>>> On 4 May 2011 13:47, mark florisson<markflorisson88 at gmail.com>    wrote:
>>>>
>>>> On 4 May 2011 13:45, Dag Sverre Seljebotn<d.s.seljebotn at astro.uio.no>
>>>>   wrote:
>>
>>>>> Look.
>>>>>
>>>>> i = 42
>>>>> for i in prange(n):
>>>>>     f(i)
>>>>> print i # want 42 whenever n == 0
>>>>>
>>>>> Now, translate this to:
>>>>>
>>>>> i = 42;
>>>>> #pragma omp parallel for firstprivate(i) lastprivate(i)
>>>>> for (temp = 0; ...; ...) {
>>>>>     i = ...
>>>>> }
>>>>> #pragma omp parallel end
>>>>> /* At this point, i == 42 if n == 0 */
>>>>>
>>>>> Am I missing something?
>>>>
>>>> Yes, 'i' may be uninitialized with nsteps>    0 (this should be valid
>>>> code). So if nsteps>    0, we need to initialize 'i' to something to get
>>>> correct behaviour with firstprivate.
>>
>> This I don't see. I think I need to be spoon-fed on this one.
>
> So assume this code
>
> cdef int i
>
> for i in prange(10): ...
>
> Now if we transform this without the guard we get
>
> int i;
>
> #pragma omp parallel for firstprivate(i) lastprivate(i)
> for (...) { ...}
>
> This is invalid C code, but valid Cython code. So we need to
> initialize 'i', but then we get our "leave it unaffected for 0
> iterations" paradox. So we need a guard.

You mean C code won't compile if i is firstprivate and not initialized? 
(Sorry, I'm not aware of such things.)

My first instinct is to initialize i to 0xbadabada. After all, its value 
is not specified -- we're not violating any Cython specs by initializing 
it to garbage ourselves.

OTOH, I see that your approach with an if-test is more 
Valgrind-friendly, so I'm OK with that.

Would it work to do

if (nsteps > 0) {
     #pragma omp parallel
     i = 0;
     #pragma omp for lastprivate(i)
     for (temp = 0; ...) ...
     ...
}

instead, to get rid of the warning without using a firstprivate? Not 
sure if there's an efficiency difference here, I suppose a good C 
compiler could compile them to the same thing.

Dag Sverre


More information about the cython-devel mailing list