Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

spooner1887

macrumors newbie
Original poster
Oct 11, 2010
12
0
Hello, I've been learning OpenMP by reading Using OpenMP: Portable Shared Memory Parallel Programming by Chapman et. al.

I've been trying to get the following example in the book to work, using the atomic construct. Here is an example from page 92 of said book.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(){

int ic, i, n;
ic = 0;
n = 10;

#pragma omp parallel shared(n, ic) private(i)
     for(i=0; i<n; i++)
     {
        #pragma omp atomic
            ic = ic +1;
      }

printf("Counter = %d\n",ic);
 
return 0;
}

I fixed a mistake in the text. They give
Code:
for(i=0; i++, i<n)

if I try compiling it with gcc -fopenmp file.c, I get the following error:
file.c:16: error: invalid operator for '#pragma omp atomic' before '=' token

However if I change
Code:
ic=ic+1;
to
Code:
ic++;
it compiles and runs just fine. I've tried this with gcc-mp-4.4 and gcc-mp-4.5 (which are versions 4.4.6 and 4.5.3 respectively) and I get the same problems. These compilers were downloaded from macports. Am I missing something or is the text I'm using incorrect in its usage of the atomic construct. Perhaps the compilers I'm using do not meet the OpenMP standard. Any insight would be appreciated.
 
OpenMP specification versions

Dear spooner1887,

According to OpenMP specification version 3 (2008):
The syntax of the atomic construct is as follows:
Code:
#pragma omp atomic [I]new-line[/I]
[I]expression-stmt[/I]
where expression-stmt is an expression statement with one of the following forms:
Code:
x binop= expr
x++
++x
x--
--x

The atomic construct definition has changed in OpenMP version 3.1 (2011). Based on the new definition, your code is correct and can be compiled without any error. Maybe, the current version of gcc uses OpenMP v3.0.
 
That's an obvious syntax error, independent of OpenMP. Once you have that kind of error, there's no guarantee what the compiler will do.

He said that he fixed that bug. I checked and this error:
file.c:16: error: invalid operator for '#pragma omp atomic' before '=' token
because of the following expression immediately after atom construct:
Code:
ic=ic+1;
according to OpenMP specification independent of for structure. You can check it! ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.