C******************************************************************************
C
C               Example 7 : Omp_Recursion.f
C
C   Objective : simple OpenMP program for recursive computation
C               This example demonstrates the use of
C               PARALLEL DO directive, and CRITICAL section
C               
C   Input     : Size of an array.  
C
C   Output    : Status of computation
C  
C   Necessary Condition : Number of threads should be less than or 
C                         equal to number of processors of a SMP node.
C
C******************************************************************************

      program RecursiveComp

      integer  DATA_SIZE
      parameter (DATA_SIZE=1000)
      integer i, j, ValidOutput, ArraySize
      real Array(DATA_SIZE)
      real CheckArray(DATA_SIZE)

      print *,'Input Size of Array'
		read(*,*) ArraySize

      if((ArraySize .le. 0) .or. (ArraySize .gt. DATA_SIZE)) then
        print *,'Invalid ArraySize.'
        print *,'Please try with another number less than', DATA_SIZE+1
        goto 100
	   endif

C     Initializations
        do i = 1, ArraySize
          Array(i) = i * 1.0
          CheckArray(i) = i * 1.0
        enddo

      print *, 'Values of Array'
      do i = 1, ArraySize
         print*,Array(i)
      enddo
      print *, ' '

C$OMP PARALLEL DO 
         do i = 2, ArraySize
C$OMP CRITICAL
            Array(i) = (Array(i-1) + Array(i)) / 2
C$OMP END CRITICAL
         enddo


      do i = 2, ArraySize
          CheckArray(i) = (CheckArray(i-1) + CheckArray(i)) / 2
      enddo

      do i = 1, ArraySize
         if(Array(i) .ne. CheckArray(i)) then
            ValidOutput = 0
         else
            ValidOutput = 1
         endif
      enddo

      if(ValidOutput .eq. 1) then
        print*,"------ Correct Result -----"
      else
        print*,"------ Wrong Result -----"
      endif

100   stop
      end
