! FILE: pingpong.f90 from www.nersc.gov ! ! This is an example program intended to demonstrate ! the use of simple send and receive commands. Each ! of 2 CPUs sends its ID number to the other one and ! at the end they both print the values of what they ! sent as well as what they received. ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! include "mpiSim.f90" program pingpong include "useSim.f90" IMPLICIT NONE ! INCLUDE "mpif.h" INTEGER :: me, sbuf, rbuf, ierr, status(MPI_STATUS_SIZE) INTEGER :: count, dest, source, tag ! initialize MPI CALL MPI_INIT(ierr) ! find the current task CALL MPI_COMM_RANK( MPI_COMM_WORLD, me, ierr ) ! set an ID value for the message tag tag=99 ! have the first task [task zero] send then receive IF (me.EQ.0) THEN write(*,*) "Task 0" ! set values for the send command ! dest is where the message is going ! count is the number of items being sent dest=1 count=1 ! set values for the recv command ! source is where to expect a message from source=1 ! send a message containing the task number to task 1 ! place the task ID in the variable sbuf sbuf = me CALL MPI_SEND(sbuf, count, MPI_INTEGER, dest, tag, & MPI_COMM_WORLD, ierr) ! receive the message from task 1 into rbuf CALL MPI_RECV(rbuf, count, MPI_INTEGER, source, tag, & MPI_COMM_WORLD, status, ierr) END IF ! have the second task [task one] receive then send. This constitutes ! "blocking" message passing. IF (me.EQ.1) THEN write(*,*) "Task 1" ! set values for the send command ! dest is where the message is going ! count is the number of items being sent dest=0 count=1 ! set values for the recv command ! source is where to expect a message from source=0 ! Receive the message from task 0 into rbuf CALL MPI_RECV(rbuf, count, MPI_INTEGER, source, tag, & MPI_COMM_WORLD, status, ierr) ! Send a message containing the task number to task 0 ! place the task ID in the variable sbuf sbuf = me CALL MPI_SEND(sbuf, count, MPI_INTEGER, dest, tag, & MPI_COMM_WORLD, ierr) END IF ! have each task print both what it sent and received PRINT*, "TASK #", me, " sent ", sbuf PRINT*, "TASK #", me, " received ", rbuf ! output with 2 processors: !Task 0 !Task 1 !TASK # 0 sent 0 !TASK # 0 received 1 !TASK # 1 sent 1 !TASK # 1 received 0 ! close out MPI CALL MPI_FINALIZE(ierr) END 1 !TASK # 1 received 0 ! close out MPI CALL MPI_FINALIZE(ierr) END