      program example4
      include 'mpif.h'
      integer ierr,myrank,nprocs,ist,ied
      parameter(n=1000)
      integer sum,sum2
      call MPI_INIT(ierr)
      call MPI_COMM_SIZE(MPI_COMM_WORLD,nprocs,ierr)
      call MPI_COMM_RANK(MPI_COMM_WORLD,myrank,ierr)
      ist=((n-1)/nprocs+1)*myrank+1
      ied=((n-1)/nprocs+1)*(myrank+1)
      sum=0
      do i=ist,ied
        sum=sum+i
      enddo
      call MPI_REDUCE(sum,sum2,1,MPI_INTEGER,MPI_SUM,0,
     &                MPI_COMM_WORLD,ierr)
      if(myrank.eq.0) write(6,6000) sum2
 6000 format("Total Sum = ",i10)
      call MPI_FINALIZE(ierr)
      stop
      end
