/* -----------------------------------------------------!
! The current code has the purpose:                     !
! (1) demonstrate data racing issues.                   !
!-------------------------------------------------------!
! Xiaoxu Guan at LSU HPC.                               !
! March 07, 2016                                        !
!----------------------------------------------------- */

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

const int  nsize = 10;

int main () 
{
    int i,nosize;
    int array_org[nsize],array_a[nsize];


    nosize = nsize;
    for (i = 0; i < nosize; i++) 
    {
       array_org[i] = (double) i; 
       array_a[i] = array_org[i];
    }



#pragma omp parallel for default(none) private(i) shared(nosize,array_a)
        for(i = 1; i < nosize; i++) { 
          array_a[i] = array_a[i] + array_a[i-1];
            }


    for(i = 0; i < nosize; i++) {
        printf("array_a[ %d ] = %d\n", i, array_a[i]);
                                }

    printf("\n\n");


/* restore array_a  */
    for (i = 0; i < nosize; i++) { array_a[i] = array_org[i]; }



/* ----------------------------------------------------------!
!---  do we have data dependences in the following loop?  ---!
!---------------------------------------------------------- */

#pragma omp parallel for default(none) private(i) shared(nosize,array_a)
        for(i = 1; i < nosize; i += 2) { 
          array_a[i] = array_a[i] + array_a[i-1];
            }


    for(i = 0; i < nosize; i++) {
        printf("array_a[ %d ] = %d\n", i, array_a[i]);
                                }


}
