      PROGRAM DSENDA ! Bernd Berg Feb 11 2008.
C SEND and RECEIVE (S/R) of double precision real*8 arrays.
C Two MPI processes. Printout to verify correct S/R.
C Open MPI: NDAT=503 works, NDAT>=504 not.
      implicit real*8 (a-h,o-z)
      implicit logical (l)
      include 'mpif.h'
      PARAMETER(IUO=6,ITWO=2,NDAT=15999,ONE=1.D00) ! Works with MPICH.
c     PARAMETER(IUO=6,ITWO=2,NDAT=16000,ONE=1.D00) ! Fails with MPICH.
      DIMENSION SEND(NDAT),RECV(NDAT),PRNT(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,'(/," DSEND: 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 SEND:
        SEND(IDAT)=(IDAT+MY_ID)*ONE
      ENDDO
      PRNT(1)=SEND(1)
      PRNT(2)=SEND(NDAT)
      ITAG=1
      IF(MY_ID.EQ.0) WRITE(IUO,'(/,3X,"MY_ID   PRNT(1)",7X,"PRNT(2)")')
      CALL WRITE_MPI_D04(PRNT,RECV,ITWO,IUO,ITAG) ! Before S/R.
      ISND=MOD(MY_ID+1,N_PROC)
      IRCV=ISND
      ITAG=2
      CALL MPI_SEND(SEND,NDAT,MPI_DOUBLE_PRECISION,ISND,ITAG,
     &  MPI_COMM_WORLD,IERR)         ! Send to other process.
      CALL MPI_RECV(RECV,NDAT,MPI_DOUBLE_PRECISION,IRCV,ITAG,
     &  MPI_COMM_WORLD,ISTATUS,IERR) ! Receive from other process
      IF(MY_ID.EQ.0) WRITE(IUO,'(/," After send/receive:")') 
      PRNT(1)=RECV(1)
      PRNT(2)=RECV(NDAT)
      CALL WRITE_MPI_D04(PRNT,RECV,ITWO,IUO,ITAG) ! After S/R.
      IF(MY_ID.EQ.0) WRITE(IUO,'(/," DSEND: ALL DONE.",/)') 
C
      CALL MPI_FINALIZE(IERR)
      STOP
      END

      include '../../Libs/MPI/write_mpi_d04.f'
