Strange Behavior of "omp for"

Hello.

I’ve been doing some tests on the “omp for” construct and I was quite confuse about its functionality. I understand that the loop needs to be canonical, but if you take a look on the example at Compiler Explorer, I noticed that the GCC compiler doesn’t support a loop where there’s some form of equation present on the induction variable .On the other hand Clang seems to support it but the loop is just wrong. Why not adhere to GCC’s approach and simply disallow the execution of such loops instead of executing them incorrectly?

The other thing that I saw that was also strange, is that the GCC compiler supports the change on the iteration variable inside the body of the loop but this is not true to Clang. Here the link for it Compiler Explorer. I also don’t understand why does it behave like that.

Should this be considered a issue, or I’m missing something about the OpenMP standard?

There are related topics in the standard.

  1. If the loop is associated with a loop-associated directive, the expressions are evaluated before the construct formed from that directive.
    In you case increment is just preevaluated before loop execution to be loop invariant value.
  2. var is the iteration variable of the loop. It must not be modified during the execution of intervening-code or loop-body in the loop.

So, looks like it is not allowed by the standard.

1 Like

I assume this is the right code (it’s what I see at the Compiler Explorer link)
#include <stdio.h>
#include <pthread.h>
int main() {
int x = 0;
#pragma omp for
for (int i = 0; i < 11; i += 2)
x += i;
return x;
}

If it is, then there are a few issues here,

  • There is no OpenMP parallelism in the code (so if it’s not getting the answer you expect, there is a bug in the serial code)
  • If there was parallelism you’d have a race on x, so would need a reduction(+:x) clause.

No, I had sent the wrong link. I’m sorry about that. I found out what the problem was later. I was working with taskloop construct and I found that it was necessary to set the reduction even when I’m using a single region.