      program pb2

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 row sum of the column products of a certain
c     function of the indices of an array.
      parameter (kases=1)
      integer ns(kases), ms(kases)
      data ns/512/
      data ms/512/

      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     Compute for all cases, the product locally and the sum globally.
c     Distribute the index row over the nodes cyclically.
c      ib = me + 1
c      is = nn
c
      do kase = 1, kases

         kfirst = kxtime()

         n = ns(kase)
         m = ms(kase)
c         sum = 0
c         do i = ib, n, is
c            prod = 1
c            do j = 1, m
c               prod = prod*(1-(0.0001*abs(i-j))/(i+j))
c            enddo
c            sum = sum + prod
c         enddo

c        block distribution
         nlocal=n/nn
c         if(mod(n,nn).ne.0) write(6,*) 'warning: bad load balance!'
         sum=0
         do i=1,nlocal
           prod=1
           i1=me*nlocal+i
           do j=1,m
               prod = prod*(0.1*real(i1-j)/(i1+j)+1.0)
           enddo
           sum=sum+prod
         enddo

         istat = KXCOMB(sum,iadd,4,1,ialnod,0,1)
         klast = kxtime()

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

c        Print the time spent.
         kspent = klast - kfirst
         print *, '# processors:',nn
         call kmulti(6)
         print *, 'time',float(kspent)/1000000.0
         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

