program main !-- Process 0 sends an array of 5 real variables: x(1:5), to process 1. ! Process 1 prints the values of the received array: y(1:5). use mpi implicit none !--Include the mpi header file integer i,ierr,numprocs integer status(MPI_STATUS_SIZE) integer irc integer count,dest,tag integer my_rank,datatype,comm,source real x(5),y(5) !--Initialize MPI call MPI_INIT( ierr ) !--Who am I? --- get my rank=my_rank call MPI_COMM_RANK( MPI_COMM_WORLD, my_rank, ierr ) !--How many processes in the global group? call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr ) print *, "Process ", my_rank, " of ", numprocs, " is alive" ! --the common calling arguments count =5 datatype=MPI_REAL tag = 2 comm = MPI_COMM_WORLD !---process 0 send data to process 1 (dest=1) if (my_rank == 0) then do i=1,5 x(i)=float(i) end do dest = 1 call MPI_SEND(x(1),count,datatype, dest,tag,comm, ierr) dest = 2 call MPI_SEND(x(1),count,datatype, dest,tag,comm, ierr) end if !---process 1 receives data from process 0 (source=0) if (my_rank == 1) then y=0. source=0 call MPI_RECV(y(1),count,datatype, source,tag,comm,status, ierr) print *,'At process ',my_rank,' y=',y, 'source=',source,' count=',count dest=2 x=2*y call MPI_SEND(x(1),count,datatype, dest,tag,comm, ierr) end if if (my_rank == 2) then y=0. source=0 call MPI_RECV(y(1),count,datatype, source,tag,comm,status, ierr) print *,'At process ',my_rank,' y=',y, 'source=',source,' count=',count source=1 call MPI_RECV(y(1),count,datatype, source,tag,comm,status, ierr) print *,'At process ',my_rank,' y=',y, 'source=',source,' count=',count end if !--Finilize MPI call MPI_FINALIZE(irc) stop end