Why is the != operator not allowed with OpenMP?

前端 未结 5 1925
清歌不尽
清歌不尽 2020-12-01 01:05

I was trying to compiled the following code:

#pragma omp parallel shared (j)
{
   #pragma omp for schedule(dynamic)
   for(i = 0; i != j; i++)
   {
    // do         


        
5条回答
  •  执念已碎
    2020-12-01 01:59

    .

    I sent an email to OpenMP developers about this subject, the answer I got:

    For signed int, the wrap around behavior is undefined. If we allow !=, programmers may get unexpected tripcount. The problem is whether the compiler can generate code to compute a trip count for the loop.

    For a simple loop, like:

    for( i = 0; i < n; ++i )
    

    the compiler can determine that there are 'n' iterations, if n>=0, and zero iterations if n < 0.

    For a loop like:

    for( i = 0; i != n; ++i ) 
    

    again, a compiler should be able to determine that there are 'n' iterations, if n >= 0; if n < 0, we don't know how many iterations it has.

    For a loop like:

    for( i = 0; i < n; i += 2 )
    

    the compiler can generate code to compute the trip count (loop iteration count) as floor((n+1)/2) if n >= 0, and 0 if n < 0.

    For a loop like:

    for( i = 0; i != n; i += 2 )
    

    the compiler can't determine whether 'i' will ever hit 'n'. What if 'n' is an odd number?

    For a loop like:

    for( i = 0; i < n; i += k )
    

    the compiler can generate code to compute the trip count as floor((n+k-1)/k) if n >= 0, and 0 if n < 0, because the compiler knows that the loop must count up; in this case, if k < 0, it's not a legal OpenMP program.

    For a loop like:

    for( i = 0; i != n; i += k )
    

    the compiler doesn't even know if i is counting up or down. It doesn't know if 'i' will ever hit 'n'. It may be an infinite loop.

    Credits: OpenMP ARB

提交回复
热议问题