      program main
      include 'mpif.h'
      parameter(n=100)
      integer i,j,istart,iend,istart2,iend2,dst,src
      double precision x(n)
      integer nprocs,myrank,istat(MPI_STATUS_SIZE),ierr
      call mpi_init(ierr)
      call mpi_comm_size(MPI_COMM_WORLD,nprocs,ierr)
      call mpi_comm_rank(MPI_COMM_WORLD,myrank,ierr)
      istart=n*myrank/nprocs+1
      iend=n*(myrank+1)/nprocs
      do i=istart, iend
         x(i)=dble(i)
      end do
      do j=1, nprocs-1
         dst=mod(myrank+j,nprocs)
         src=mod(myrank-j+nprocs,nprocs)
         istart2=n*src/nprocs+1
         iend2=n*(src+1)/nprocs
         call mpi_sendrecv(x(istart),iend-istart+1,
     *                     MPI_DOUBLE_PRECISION,dst,100,
     *                     x(istart2),iend2-istart2+1,
     *                     MPI_DOUBLE_PRECISION,src,100,
     *                     MPI_COMM_WORLD,istat,ierr)
      end do
      if (myrank.eq.0) then
         do i=1, n
            write(6,*) i, x(i)
         end do
      end if
      call mpi_finalize(ierr)
      stop
      end
