      program pb4

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 sum of reciprocals of a set of numbers.
      parameter (kases=1)
      integer ns(kases)
      data ns/65536/
      parameter (nmax=65536)
      real y(nmax), z(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)

c     Group the numbers into nn sets and distribute among the nodes.
      do kase = 1, kases

         kfirst = kxtime()

      n = ns(kase)
      ib = 1 + me*(n/nn)
      ie = ib + (n/nn) - 1


      do i = ib, ie
         y(i) = real(100-i)/1000.0
      enddo
      
      do i = ib, ie
         if(abs(y(i)).gt.0.00001) then
            z(i) = 1.0/y(i)
         else
            z(i) = 0
         endif
      enddo

      sum = 0
      do i = ib, ie
         sum = sum + z(i)
      enddo

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


         klast = kxtime()

         print *, 'n = ', n, ' -> sum = ', sum

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

      stop
      end

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

      r1 = r1 + r2
      iadd = 0
      return
      end

