! ring_ex.f90 ! Example from Rutgers include "mpiSim.f90" program ring include "useSim.f90" ! ! This example program uses MPI blocking and nonblocking point-to-point ! communication calls to send and receive message in the ring where node ! with rank i sends a message to the node with rank i+1 and receives a ! message from the node with rank i-1 !************************************************************** implicit none !include 'mpif.h' integer ntasks, taskid, right, left, inmsg, outmsg, mtype, & msgid, rbytes, sbytes, i integer ierr,status(MPI_STATUS_SIZE),tag,requests(2) outmsg = 0 i = 1 ! $$ Error: tag has not been defined before calling MPI_Send. ! ! learn number of tasks in partition and task ID call mpi_init(ierr) call mpi_comm_size(MPI_COMM_WORLD,ntasks,ierr) call mpi_comm_rank(MPI_COMM_WORLD,taskid,ierr) ! compute source and destination for messages if (taskid.eq.0) then left = ntasks - 1 else left = taskid - 1 endif if (taskid.eq.ntasks-1) then right = 0 else right = taskid + 1 endif do while (outmsg .ne. -1) mtype = i !node 0 queries user for message, sends it to the right, ! then waits for its return if (taskid .eq. 0) then write (*,*) 'Enter integer value to be passed along ring' write (*,*) 'A value of -1 ends the program' read (*,*) outmsg !$$ tag has never been initialized; for example tag = MPI_Any_tag. call mpi_send(outmsg,1,MPI_INTEGER,right,tag, & MPI_COMM_WORLD,ierr) call mpi_irecv(inmsg,1,MPI_INTEGER,left,tag, & MPI_COMM_WORLD,requests(1:1),ierr) call mpi_wait(requests(1),status,ierr) write (*,*) 'node 0 received message content is ', inmsg ! the rest of the nodes in the group read the message and pass it on else call mpi_recv (inmsg,1,MPI_INTEGER,left,tag, & MPI_COMM_WORLD, status,ierr) outmsg = inmsg call mpi_isend (outmsg,1,MPI_INTEGER,right,tag, & MPI_COMM_WORLD, requests(2),ierr) call mpi_wait (requests(2),status,ierr) write (*,*) taskid, 'processed message ', mtype, & 'content is ', outmsg end if i = i + 1 end do end