      program pb11

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 the Fourier moments of a given set of data.
      parameter (kases=1, NFMOM=4)
      integer N(kases)
      real TFMOM(NFMOM)
      data N/262144/
      parameter (Nmax=262144)
      real DATA(Nmax), COSWT(Nmax)
      data pi/3.1415926/

      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
      NK = N(kase)
      kfirst = kxtime()
      nlocal=NK/nn

c     Initialize the data.

      do i=1,nlocal
         i1=me*nlocal+i
         DATA(i) = -10.0*i1 + 1080.0*sin(1.0+0.0623*i1)
         DATA(i) = amax1(0.0, amin1(1000.0,DATA(i)))
         DATA(i) = alog(1.0 + DATA(i))
      enddo

c     Compute the moments.
      TFMOM(1)=SUM(DATA,nlocal)/NK

      do  j = 2, NFMOM
         do i = 1, nlocal
            i1=me*nlocal+i
            COSWT(i) = DATA(i)*cos(pi*i1*(j-1)/(NK+1))
         enddo
         TFMOM(j) = sum(COSWT,nlocal)/NK
      enddo

         istat = KXCOMB(TFMOM,iadd,4,NFMOM,ialnod,0,1)

         klast = kxtime()

c        Print the results.
         print *, 'N = ', N, ' -> moments ='
         print *, (TFMOM(i), i=1,NFMOM)

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 summing.
      function sum(x,n)
      real x(n)

      s = 0
      do i = 1, n
         s = s+x(i)
      enddo
      sum = s
      return
      end

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

      r1 = r1 + r2
      iadd = 0
      return
      end

