      program simvec

c     This code is used to demonstrate the performance gains that can
c     be realized by using the vector version of intrinsic functions.
c     For simple cases, the Sun f90 compiler recognizes opportunities
c     to replace calls to scalar functions with calls to the vector
c     equivalents. 
c
c     If the accompanying makefile is not used, code should be 
c     compiled using one of the following line
c
c     f90 -fast -xnolibmopt -Xlist -o simvec simvec.f
c     f90 -fast -xnolibmopt -xvector -Xlist -o simvec simvec.f
c     f90 -fast -Xlist -o simvec simvec.f
c     f90 -fast -xvector -Xlist -o simvec simvec.f
c
c     Author: Bob Sinkovits, SDSC 2/16/00
c
c     NOTE: the exponent used in the real-to-integer power function
c     test is read in at run time rather than being hard-coded. This
c     prevents the compiler from attempting to optimize the operation
c     by replacing with a repeated multiplication
c

      implicit none

      integer, parameter :: n = 10000000
      real*8, dimension(n) :: x,y,z

      integer iexp
      real*8 tstart,tend,second
      real*8 tsqrt
      real*8 trsqrt
      real*8 texp
      real*8 tlog
      real*8 tsin
      real*8 tcos
      real*8 ttan
      real*8 tr2r
      real*8 tr2i

      real time(2)

      open(unit=10,file='iexp.dat',status='old')
      read(10,*) iexp
      close(10)

      call etime(time)
      tstart = time(1)
      call random_number(x)
      call random_number(y)
      call etime(time)
      tend = time(1)
      if(tend .lt. tstart) write(6,*) tend - tstart
      
      
c     Testing performance of vector intrinsic functions
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = sqrt(y(i))
      enddo
      call etime(time)
      tend = time(1)
      tsqrt = tend - tstart
      if (z(n).eq.-100.0) write(10,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = 1.0/sqrt(y(i))
      enddo
      call etime(time)
      tend = time(1)
      trsqrt = tend - tstart
      if (z(n).eq.-100.0) write(10,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = exp(x(i))
      enddo
      call etime(time)
      tend = time(1)
      texp = tend - tstart
      if (z(n).eq.-100.0) write(10,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = log(x(i))
      enddo
      call etime(time)
      tend = time(1)
      tlog = tend - tstart
      if (z(n).eq.-100.0) write(10,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = sin(x(i))
      enddo
      call etime(time)
      tend = time(1)
      tsin = tend - tstart
      if (z(n).eq.-100.0) write(10,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = cos(x(i))
      enddo
      call etime(time)
      tend = time(1)
      tcos = tend - tstart
      if (z(n).eq.-100.0) write(20,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = tan(x(i))
      enddo
      call etime(time)
      tend = time(1)
      ttan = tend - tstart
      if (z(n).eq.-100.0) write(20,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = x(i)**y(i)
      enddo
      call etime(time)
      tend = time(1)
      tr2r = tend - tstart
      if (z(n).eq.-100.0) write(20,*) z
      
      call etime(time)
      tstart = time(1)
      do i=1,n
         z(i) = x(i)**iexp
      enddo
      call etime(time)
      tend = time(1)
      tr2i = tend - tstart
      if (z(n).eq.-100.0) write(20,*) z
      
      
c     Outputting results
      
      write(6,100)tsqrt,
     1     trsqrt,
     2     texp,
     3     tlog,
     4     tsin,
     5     tcos,
     6     ttan,
     7     tr2r,
     8     tr2i
      
 100  format('function     t',
     1     /,'SQRT            ',f11.5,
     2     /,'RSQRT           ',f11.5,
     3     /,'EXP             ',f11.5,
     4     /,'LOG             ',f11.5,
     5     /,'SIN             ',f11.5,
     6     /,'COS             ',f11.5,
     7     /,'TAN             ',f11.5,
     8     /,'REAL-TO-REAL    ',f11.5,
     9     /,'REAL-TO-INT     ',f11.5)
      
      
      end
