      program pb13

c     Reference:
c     Problems to test parallel and vector languages
c     CSD-TR 516, computer science, Purdue University
c     John R. Rice, may 1, 1985
c
c     Revised by John R. Rice and J. Jing, oct. 1, 1990
c
c     *************************************************
c     *      Adapted for FORTRAN D benchmarking       *
c     *    by  T. Haupt  (haupt@sccs.npac.syr.edu)    *
c     *        Assisted by B. S. Balakrishna          *
c     *                                               *
c     *    Northeast Parallel Architectures Center    *
c     *   at Syracuse University, Syracuse, NY, USA   *
c     *************************************************
c
c     Version parallel-1.00  (express fortran77)
c     =================================================

c     To compute a certain expression, given four vectors.
      parameter (kases=1)
      integer ns(kases)
      data ns/65536/
      parameter (nmax=65536)
      real a(nmax), b(nmax), c(nmax), d(nmax)

      integer env(4)
      common/xpenv/ env
      common/xpress/nocare,norder,nonode,ihost,ialnod,ialprc
      equivalence (env(1),me), (env(2),nn)
      external iadd

      call KXINIT
      call KXPARA(env)

      do kase = 1, kases
        n = ns(kase)
        kfirst = kxtime()
        nlocal=n/nn

c     Initialize 

      do ii = 1,nlocal
         i=me*nlocal+ii
         a(ii) = i/10.0 + 1.0/i
         b(ii) = log10(a(ii)) + 0.02
         c(ii) = (a(ii) + b(ii))*sin(a(ii))
         d(ii) = a(ii) + b(ii) - 2.0*c(ii)
      enddo

c     Compute. Result is in e.
      e = 0.0
      do ii = 1,nlocal
         a(ii) = a(ii)**sin(b(ii))
         if (sin(a(ii)).lt.cos(c(ii))) then
            a(ii) = a(ii)+c(ii)
         else
            a(ii) = a(ii)-d(ii)
         endif
         e = e+a(ii)**2
      enddo

c     Now combine the results from all the nodes.
        istat = KXCOMB(e,iadd,4,1,ialnod,0,1)

         klast = kxtime()

c     Print the result.
         print *, 'n = ', n, ' -> e  = ', e

c     Print the time spent.
         kspent = klast - kfirst
         call kmulti(6)
         print *, 'time', float(kspent)/1000000.0,' seconds'
         call kflush(6)
      enddo

      stop
      end

c     Subroutine for adding. For global use.
      integer function iadd(r1,r2,isize)

      r1 = r1 + r2
      iadd = 0
      return
      end





