C **********************************************************************
C
C               Example 6 : Omp_Sumof_Elements_ReductionOp.f
C
C   Objective : Sum of 'n' elements of one-dimensional real array.
C               This example demonstrates the use of
C               PARALLEL DO directive and REDUCTION operation.
C
C   Input     : Size of an array
C
C   Output    : Sum of 'n' elements
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 SumOfElements
       integer SIZE
       parameter (SIZE=100)
       real Sum, CheckSum
       real Array(SIZE)

       integer OMP_GET_THREAD_NUM, ThreadID
       integer ArraySize, ValidOutput

       print*,"Input the Size of Array"
       read(*,*) ArraySize

       if((ArraySize .le. 0) .or. (ArraySize .gt. SIZE)) then 
          print*,"Input is Wrong." 
          print*,"Please try with another number less than",SIZE+1
          print*,"Or greater than 0"
          goto 100
       endif

       Sum = 0.0
       do j = 1, ArraySize
         Array(j) = float(j)
       enddo

C$OMP PARALLEL DO REDUCTION(+:Sum)
       do j = 1, ArraySize
          Sum = Sum+Array(j)
       enddo

       print *, 'Sum:  ', Sum

       CheckSum = 0.0
       do j = 1, ArraySize
          CheckSum = CheckSum + Array(j)
       enddo

       if(CheckSum .ne. Sum) then
         ValidOutput = 0
       else
         ValidOutput = 1
       endif

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

100    end

