      program example3
      include 'mpif.h'
      integer ierr,myrank,nprocs,ist,ied
      integer status(MPI_STATUS_SIZE)
      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
      itag=1
      if(myrank.ne.0) then
        call MPI_SEND(sum,1,MPI_INTEGER,0,
     &                itag,MPI_COMM_WORLD,ierr)
      else
        call MPI_RECV(sum2,1,MPI_INTEGER,1,
     &                itag,MPI_COMM_WORLD,status,ierr)
        sum=sum+sum2
        call MPI_RECV(sum2,1,MPI_INTEGER,2,
     &                itag,MPI_COMM_WORLD,status,ierr)
        sum=sum+sum2
        call MPI_RECV(sum2,1,MPI_INTEGER,3,
     &                itag,MPI_COMM_WORLD,status,ierr)
        sum=sum+sum2
        write(6,6000) sum
 6000   format("Total Sum = ",i10)
      endif
      call MPI_FINALIZE(ierr)
      stop
      end
