OpenMP: atomic construct problem with gcc

Discussion in 'Mac Programming' started by spooner1887, Aug 5, 2011.

  1. spooner1887 macrumors newbie

    Oct 11, 2010
    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.

    #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
    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
    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.
  2. cartoonist macrumors newbie

    Dec 31, 2011
    OpenMP specification versions

    Dear spooner1887,

    According to OpenMP specification version 3 (2008):
    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.
  3. gnasher729 macrumors P6


    Nov 25, 2005
    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.
  4. cartoonist macrumors newbie

    Dec 31, 2011
    He said that he fixed that bug. I checked and this error:
    because of the following expression immediately after atom construct:
    according to OpenMP specification independent of for structure. You can check it! ;)

Share This Page