1

I try to work with OpenMP in C, but found some problem with the != operator and parallel for. For the next code:

#include <stdio.h>

int main () 
{
    int array[] = {1,2,3,4,5, 6, 7};
    int *p;
    int *stop = array + 7;

    #pragma omp parallel for
    for (p = array; p != stop; p++)
        printf("a: %d\n", *p);

    return 0;   
}

I get this error:

omp.c:10:18: error: invalid controlling predicate
    for (p = array; p != stop; p++)
                    ^

But when I replace the != with <, it compiles and works great. Is there any alternative? I want to paralleling a code with "stop" pointer, and have to use the != operator for that.

Compiling command: gcc -o omp.o -fopenmp example.c

Thanks!

Aviv Abramovich
  • 135
  • 2
  • 9
  • 2
    *Why* do you have to use `!=`? You can still have a "stop pointer" while using `<`. In fact it might even be better from a parallel point of view, since then there is no chance of a thread possibly using a pointer *beyond* `stop`. – Some programmer dude Mar 05 '18 at 09:28
  • @Someprogrammerdude This is just an example, I have a program that use `!=` and the pointer is not necessarily smaller. I want to keep the program as close to the original code, so I prefer keep it logic and stop conditions – Aviv Abramovich Mar 05 '18 at 09:31

1 Answers1

4

OpenMP specification requires that the loop be in the so-called canonical form. Canonical form admits only four types of relational operations in the test clause of the for loop. Namely <, <=, >, or >=.

The full specification of the canonical form can be read directly from OpenMP specification:

for ( init-expr ; test-expr ; incr-expr ) structured-block

where test-expr is one of the following:

var relational-op b
b relational-op var

with relational-op being <, <=, >, or >=. It is Chapter 2.6 in the OpenMP specification version 4.5 (chapter numbers may change in future versions of the specification).

For the case in your example

for (p = array; p != stop; p++)

is exactly equivalent to

for (p = array; p < stop; p++)

For other cases you might have to modify your code to bring the loops into a canonical form.

Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76