PROGRAM Send_Recv
!-----------------------------------------------------------------------!
! Basic  primitives to send and receive                                 !
!-----------------------------------------------------------------------!
! REMARK: a message is received by the destination process
!   if it originates from the expected source and has the expected "tag"

IMPLICIT NONE
INCLUDE "mpif.h"
INTEGER:: err, myid, nproc
! "status" is array to hold the communication status
! Note: the array size is predefined in "mpif.h"
INTEGER,DIMENSION(MPI_STATUS_SIZE):: status
REAL,DIMENSION(2):: a

CALL MPI_INIT (err)
CALL MPI_COMM_SIZE (MPI_COMM_WORLD, nproc, err)
CALL MPI_COMM_RANK (MPI_COMM_WORLD, myid, err)

IF (myid==0) THEN
  a = (/ 1, 2 /)	                ! Assign array values in 0 only
  CALL MPI_SEND (a, 2, MPI_REAL, &	! Message content:
                                        ! source buffer
                                        ! buffer size
                                        ! MPI type
             1, 9, MPI_COMM_WORLD, err) ! Envelope:
                                        ! destination process
                                        ! tag, ...
ELSE IF (myid==1)
  CALL MPI_RECV (a, 2, MPI_REAL, &	! Message content:
                                        ! destination buffer, ...
     0, 9, MPI_COMM_WORLD, status, err) ! Envelope:
                                        ! source process,
                                        ! tag,...
  WRITE(*,*) "Process", myid, "received", a
END IF

CALL MPI_FINALIZE (err)
END PROGRAM Send_Recv

