      PROGRAM ISENDB ! Bernd Berg Feb 10 2009 (B for Buffer).
C Buffered Send and Receive (S/R) of integer arrays. 
C Two MPI processes. Printout to verify correct S/R.
C OPEN MPI: All array sizes tested worked.
      implicit real*8 (a-h,o-z)
      implicit logical (l)
      include 'mpif.h'
      PARAMETER(IUO=6,ITWO=2,NDAT=10**6) ! Set array size.
      DIMENSION IBUF(NDAT),NSEND(NDAT),NRECV(NDAT),NPRNT(ITWO)
      COMMON /FOR_MPI/ ISTATUS(MPI_STATUS_SIZE)
C
      CALL MPI_INIT(IERR)
      CALL MPI_COMM_RANK(MPI_COMM_WORLD,MY_ID,IERR)
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD,N_PROC,IERR)
      IF(MY_ID.EQ.0) WRITE(IUO,'(/," IDEMO: N_PROC,NDAT =",2I9)') 
     &                                      N_PROC,NDAT
      IF(N_PROC.NE.2) THEN
        IF(MY_ID.EQ.0) WRITE(IUO,'(/,"N_PROC=2 REQUIRED!")')
        CALL MPI_FINALIZE(IERR)
      STOP
      ENDIF
C
      DO IDAT=1,NDAT ! Integers put into NSEND:
        NSEND(IDAT)=IDAT+MY_ID
      ENDDO
      NPRNT(1)=NSEND(1)
      NPRNT(2)=NSEND(NDAT)
      ITAG=1
      IF(MY_ID.EQ.0) WRITE(IUO,'(/,4X,"MY_ID  NPRNT(1) NPRNT(2)")')
      CALL WRITE_MPI_I(NPRNT,NRECV,ITWO,IUO,ITAG) ! Before S/R.
      ISND=MOD(MY_ID+1,N_PROC)
      IRCV=ISND
      ITAG=2
      CALL MPI_BUFFER_ATTACH(IBUF,NDAT,IERR)
      CALL MPI_BSEND(NSEND(1),NDAT,MPI_INTEGER,ISND,ITAG,
     &  MPI_COMM_WORLD,IERR)         ! Send to other process.
      CALL MPI_RECV(NRECV(1),NDAT,MPI_INTEGER,IRCV,ITAG,
     &  MPI_COMM_WORLD,ISTATUS,IERR) ! Receive from other process
      IF(MY_ID.EQ.0) WRITE(IUO,'(/," After send/receive:")') 
      NPRNT(1)=NRECV(1)
      NPRNT(2)=NRECV(NDAT)
      CALL WRITE_MPI_I(NPRNT,NRECV,ITWO,IUO,ITAG) ! After S/R.
      IF(MY_ID.EQ.0) WRITE(IUO,'(/," IDEMO: ALL DONE.",/)') 
C
      CALL MPI_FINALIZE(IERR)
      STOP
      END

      include '../../Libs/MPI/write_mpi_i.f'
