program main !-- All processes send a message to process 0 implicit none !--Include the mpi header file include 'mpif.h' integer i,ierr,myid,numprocs,itag integer message,mess_received,iroot_pe,itag,icount,ierror integer status(MPI_STATUS_SIZE) integer irc !--Initialize MPI call MPI_INIT( ierr ) !--Who am I? --- get my rank=myid call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr ) !--How many processes in the global group? call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr ) print *, "Process ", myid, " of ", numprocs, " is alive" !--Compose the message message=myid !--Send a message to process 0 icount=1 iroot_pe=0 if(myid /= 0) then itag=myid call mpi_send(message,icount,MPI_INTEGER,iroot_pe,itag, & MPI_COMM_WORLD,ierror) else do i=1,numprocs-1 itag=i call mpi_recv(mess_received,icount,MPI_INTEGER,i, & itag,MPI_COMM_WORLD, & status,ierror) print *,'received message from process',mess_received end do end if !--Finilize MPI call MPI_FINALIZE(irc) stop end