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

      program data_racing
      implicit none
      integer            :: i,nosize
      integer, parameter :: nsize = 10
      integer, dimension(0:nsize-1) :: array_org,array_a

      nosize = nsize
      do i = 0, nosize-1
         array_org(i) = dble(i)
      end do

      array_a = array_org


!$omp parallel do default(none) private(i) shared(nosize,array_a)
      do i = 1, nosize-1
         array_a(i) = array_a(i) + array_a(i-1)
      end do
!$omp end parallel do

      do i = 0, nosize-1
         write(*,'(1x,a,i2,a,i4)')   'array_a( ',i,' ) = ', array_a(i)
      end do


      write(*,*)
      write(*,*)


! restore array_a:
      array_a = array_org


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

!$omp parallel do default(none) private(i) shared(nosize,array_a)
      do i = 1, nosize-1, 2
         array_a(i) = array_a(i) + array_a(i-1)
      end do
!$omp end parallel do

      do i = 0, nosize-1
         write(*,'(1x,a,i2,a,i4)')   'array_a( ',i,' ) = ', array_a(i)
      end do


      end program data_racing

