C******************************************************************************
C
C               Example 9 : Omp_Recurrence.f
C
C   Objective : simple OpenMP program for parallelization of a loop nest
C               containing a recurrence.
C               This example demonstrates the use of
C               PARALLEL DO directive, and SHARED clause
C               
C   Input     : Size of matrix.  
C
C   Output    : 
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 Recurrence

      integer  DATA_SIZE
      parameter (DATA_SIZE=1000)
      integer i, j, ValidOutput
      real Matrix_A(DATA_SIZE,DATA_SIZE)
      real Check_C(DATA_SIZE, DATA_SIZE)

      print *,'Input Size of Matrix'
		read(*,*) MatrixSize

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

C     Initializations
      do i = 1, MatrixSize
        do j = 1, MatrixSize
          Matrix_A(i,j) = i * 1.0
          Check_C(i,j) = i * 1.0
        enddo
      enddo

      print *, 'Values of matrix'
      do i = 1, MatrixSize
         print*,(Matrix_A(i,j), j = 1, MatrixSize)
      enddo
      print *, ' '

      do j = 1, MatrixSize
C$OMP PARALLEL DO SHARED(Matrix_A)
         do i = 1, MatrixSize
            Matrix_A(i,j) = Matrix_A(i,j) + Matrix_A(i, j-1)
         enddo
      enddo


      do j = 1, MatrixSize
        do i = 1, MatrixSize
          Check_C(i,j) = Check_C(i,j) + Check_C(i,j-1)
        enddo
      enddo

      do j = 1, MatrixSize
         do i = 1, MatrixSize
            if(Matrix_A(i,j) .ne. Check_C(i,j)) then
               ValidOutput = 0
            else
               ValidOutput = 1
            endif
         enddo
      enddo

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

100   stop
      end
