* The program sends an integer to a slave process. The slave
* increases the value by one and then sends it back.
* The program should be used with two processes.
*
      program simple
      implicit    none
      include    "mpif.h"
      integer     message, length, source, dest, tag
      integer     my_rank, err
      integer     n_procs    ! number of processes 
      integer     status(MPI_STATUS_SIZE)


      call MPI_Init(err)  ! Start up MPI

*     Find out the number of n_processes and my rank 
      call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, err)
      call MPI_Comm_size(MPI_COMM_WORLD, n_procs, err)
 
      tag    = 1
      length = 1   ! Length of message 
      
      if ( my_rank .eq. 0 ) then ! I'm the master process 
        print*, "Number of processes = ", n_procs
        dest    = 1  ! Send to the other process 
        message = 1  ! Just send one integer
      
*       Send message to slave 
        call MPI_Send(message, length, MPI_INTEGER, dest, tag,
     +                MPI_COMM_WORLD, err)
        print*, "After MPI_Send"
      
        source = 1
*       Receive message from slave 
        call MPI_Recv(message, length, MPI_INTEGER, source, tag,
     +                MPI_COMM_WORLD, status, err)
      
        print*, "After MPI_Recv, message = ", message
      
      else  ! I'm the slave process 
        source = 0
*       Receive message from master
        call MPI_Recv(message, length, MPI_INTEGER, source, tag,
     +                MPI_COMM_WORLD, status, err)

        dest    = 0            ! Send to the other process 
        message = message + 1  ! Increase message 
*       Send message to master
        call MPI_Send(message, length, MPI_INTEGER, dest, tag,
     +                MPI_COMM_WORLD, err)
      end if
      
      call MPI_Finalize(err)  ! Shut down MPI 

      end