program main implicit none include 'mpif.h' integer i,j,ierr,myid,numprocs,x,status(MPI_STATUS_SIZE) integer destination,source,tag1,tag2 call MPI_INIT(ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD,numprocs,ierr) call MPI_COMM_RANK(MPI_COMM_WORLD,myid,ierr) tag1 = 7 tag2 = 8 do i=1,10 if (mod(i,2) .eq. 0) then x = 0 if (myid .lt. numprocs/2) then destination = numprocs/2 + myid write(*,*) 'Sending from',myid call MPI_SEND(x,1,MPI_INTEGER,destination,tag1, 1 MPI_COMM_WORLD,ierr) else source = numprocs/2 - myid call MPI_RECV(x,1,MPI_INTEGER,source,tag1,MPI_COMM_WORLD, 1 status,ierr) write(*,*) 'Received by',myid endif else x = 1 if (myid .lt. numprocs/2) then source = numprocs/2 + myid call MPI_RECV(x,1,MPI_INTEGER,source,tag2,MPI_COMM_WORLD, 1 status,ierr) else destination = numprocs/2 - myid call MPI_SEND(x,1,MPI_INTEGER,destination,tag2, 1 MPI_COMM_WORLD,ierr) endif endif end do call MPI_FINALIZE(ierr) stop end