/* -----------------------------------------------------!
! The current code has a purpose:                       !
! (1) how to use atomic (critical) directive.           !
!-------------------------------------------------------!
! Xiaoxu Guan at LSU HPC.                               !
! March 07, 2016                                        !
!----------------------------------------------------- */

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define nsize 1000

int main () 
{
    int i;
    double x = 0.0, answer;

    #pragma omp parallel private(i) shared(x)
    {
        #pragma omp for
          for (i = 0; i < nsize; ++i) {
        #pragma omp atomic
    /*      #pragma omp critical   */
          x += (double) i;
            }
    }

    answer = (double) 0.5*(nsize-1)*nsize;
    printf("parallel sum is %.2f\n", x);
    printf("correct answer is %.2f\n", answer);
}

