program switch use mpi implicit none integer :: rank, nb_mpi_processes, ierror, tag, statu(MPI_STATUS_SIZE), n, val, val0 integer :: niter = 4 ! Switch program tag = 7777 call MPI_INIT( ierror ) call MPI_COMM_SIZE( MPI_COMM_WORLD , nb_mpi_processes , ierror ) call MPI_COMM_RANK( MPI_COMM_WORLD , rank , ierror ) if(nb_mpi_processes /= 2) stop 'This program is design to be run with 2 processes only' if(rank==0) val = 74 if(rank==1) val = 32 ! check print*, 'process',rank,'I have the value',val,'at 2' ! First switch if(rank==0) then call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 1 , tag , val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , statu , ierror ) val = val0 end if if(rank==1) then call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 0 , tag , val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , statu , ierror ) val = val0 end if ! check print*, 'process',rank,'I have the value',val,'at 1' ! Second switch if(rank==0) then call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 1 , tag , val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , statu , ierror ) val = val0 end if if(rank==1) then call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 0 , tag , val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , statu , ierror ) val = val0 end if ! check print*, 'process',rank,'I have the value',val,'at 2' call MPI_FINALIZE ( ierror ) ! Close MPI end program switch