MPI non-blocking communication

 

PROGRAM P_P
      IMPLICIT NONE
      INCLUDE "mpif.h"

      INTEGER ierror,rank,size
      INTEGER status(MPI_STATUS_SIZE)
      INTEGER requests(4)  
      LOGICAL FLAG

      REAL as,bs,cs,ds,es,fs

      CALL MPI_INIT(ierror)
      CALL MPI_COMM_RANK(MPI_COMM_WORLD,rank,ierror)

      IF(rank.eq.0) THEN
      as = 4.2
      bs = 8.4
      write(*,*) rank,' as,bs,cs=',as,bs,cs
       CALL MPI_ISEND(as,1,MPI_REAL,1,101,
     &           MPI_COMM_WORLD,requests(1),ierror)
C
C      Here one can do some computation, which will not update "as"
C      
       es=bs+as
       fs=es*as

C
       CALL MPI_WAIT(requests(1),status,ierror)  

       CALL MPI_IRECV(cs,1,MPI_REAL,1,102,
     &           MPI_COMM_WORLD, requests(2), ierror)
       CALL MPI_WAIT(requests(2),status,ierror)  
       ds = cs* bs
       write(*,*)'I am "0" receiv cs=',cs, ' ds =',ds
       ELSE 
       as = 14.2
       bs = 18.4

       write(*,*) rank,' as,bs,cs=',as,bs,cs
       CALL MPI_IRECV(cs,1,MPI_REAL,0,101,
     &            MPI_COMM_WORLD,requests(3),ierror)
C
C      Here one can do some computation, which will not read/write "cs"
C

       es = as*as
       fs = es+as

       CALL MPI_WAIT(requests(3),status,ierror)
c
       CALL MPI_ISSEND(bs,1,MPI_REAL,0,102,
     &            MPI_COMM_WORLD,requests(4),ierror)
       CALL MPI_TEST(requests(4),FLAG, status,ierror)
        ds = cs *as
        WRITE(*,*) 'I am "1" receiv cs=',cs, ' ds =',ds
       END IF
      CALL MPI_FINALIZE(ierror)

      STOP
      END