PROGRAM Broadcast
!-----------------------------------------------------------
! One-to-many communication:
! sending the same data to all processes
!-----------------------------------------------------------

IMPLICIT NONE
INCLUDE "mpif.h"

INTEGER:: err, nproc, myid
INTEGER:: root, a(2)

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

root = 0
IF (myid==root) a = (/ 1, 2 /)  ! Define the data in root

CALL MPI_BCAST (a, 2,     MPI_INTEGER, root,   MPI_COMM_WORLD, err)
!       s/d array, count, type,        source, communicator
!     REMARK: same name for source and destination arays!

WRITE(*,*) myid, " received:", a

CALL MPI_FINALIZE(err)
END PROGRAM Broadcast
