program main
  use mpi
! Demonstration program to show point to point communications with SEND
! and RECV MPI subroutines.  The root process send an array of 100 elements
! to process 1.  Root prints out the array after changing all values
! and process 1 prints out values it received.
!
! Should be executed with 2 processes.
!
  integer rank, size, to, from, tag, count, i, ierr
  integer src, dest
  integer st_source, st_tag, st_count
  integer status(MPI_STATUS_SIZE)
  double precision data(10)
  
  call MPI_INIT(ierr)
  call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
  call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
  write(6,*) "Process", rank," of ", size, " is alive."
  dest = size - 1
  src = 0
  !
  if (rank == src) then
     to = dest
     count = 10
     tag = 2001
     do i = 1, count
        data(i) = i
     end do
     call MPI_SEND(data, count, MPI_DOUBLE_PRECISION, &
          to, tag, MPI_COMM_WORLD, ierr)
     data = 0
     write(6,*) rank, " (Sender Process), Reset Data=", (data(i),i=1,count)
  else
     tag = MPI_ANY_TAG
     count = 10
     from = MPI_ANY_SOURCE
     call MPI_RECV(data, count, MPI_DOUBLE_PRECISION, &
          from, tag, MPI_COMM_WORLD, status, ierr)
! The following three lines show additional MPI features to deduce
! additional information.
     call MPI_GET_COUNT(status, MPI_DOUBLE_PRECISION, st_count, ierr)
     st_source = status(MPI_SOURCE)
     st_tag = status(MPI_TAG)
     write(6,*) "Status info: source= ", st_source, &
          " tag = ", st_tag, " count= ", st_count
     write(6,*) rank, " received, Original Data=", (data(i),i=1,st_count)
  end if
  call MPI_FINALIZE(ierr)
end program main

