OpenMP: atomic construct problem with gcc

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

  1. spooner1887 macrumors newbie

    Joined:
    Oct 11, 2010
    #1
    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.
     
  2. cartoonist macrumors newbie

    Joined:
    Dec 31, 2011
    #2
    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

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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

    Joined:
    Dec 31, 2011
    #4
    He said that he fixed that bug. I checked and this error:
    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! ;)
     

Share This Page