! adder.f90

! © Copyright 2003-4 Dauger Research, Inc. 
!    
! Our lawyers made us say this: 
! DISCLAIMER: We provide the following on an "AS IS" basis. Use it at your own risk.  */
      
      module adderlib
      contains 
! A routine performing elemental work.
! This can be replaced with a much larger routine. 
      function kernelroutine(input) 
      implicit none
      real :: kernelroutine, input
      
      kernelroutine = (input)*(input)
      
      end function

      subroutine computeloop(theSum, myArray, HighestIndex)
      implicit none
      real :: theSum, mySum 
! local copies of the data to be output 
      real, dimension(:) :: myArray
      integer :: loopEnd, index, HighestIndex
      
      mySum = 0
! limit of the loop 
      loopEnd = HighestIndex 
      
      if (loopEnd.gt.0) then
		
! loop over indicies 
        do index = 1,loopEnd

! call the kernel routine for each index, and save into the array 
          myArray(index) = kernelroutine(real(index)) 
! sum as we go 
          mySum = mySum + myArray(index)
			
        end do
        
      end if
	
! return the sum and the array 
      theSum = mySum
      
      end subroutine

      end module adderlib
	
      program adder
      use adderlib
      implicit none
! HighestIndex specifies how highest index to sample 
      integer, parameter :: HighestIndex =   10000

! main copies of the sum and the array 
      real :: theSum
      real, dimension(HighestIndex) :: theArray 
      integer :: ios
	
      print *,"Beginning computation..." 
	
      call computeloop(theSum, theArray, HighestIndex)
	  
! error checking 
      if (theSum.gt.0) then
		
! save the array into a data file 
        open(unit = 2, file = "output", status = "replace", iostat = ios, form = "unformatted") 
        if (ios .eq. 0) then
          print *, "writing array...\n"
	
          write(unit = 2) theArray
			
          endfile 2 
        end if
		
        print *,"the sum is ", theSum 
		
      else 
		print *,"memory allocation failure" 
      end if
	
      stop 
      end program
     