PROGRAM All_to_all
!----------------------------------------------------------------------
! Exchange data among all processors
!-----------------------------------------------------------------------

IMPLICIT NONE
INCLUDE "mpif.h"
INTEGER:: err, myid, nproc
INTEGER,DIMENSION(MPI_STATUS_SIZE):: status

INTEGER,PARAMETER:: N=4
INTEGER:: i, m
REAL,DIMENSION(N):: a

CALL MPI_INIT (err)
CALL MPI_COMM_SIZE (MPI_COMM_WORLD, nproc, err)
CALL MPI_COMM_RANK (MPI_COMM_WORLD, myid, err)

a = (/ (N*myid+i, i=1,N) /)
WRITE(*,*) "process", myid, " has:", a

m = N/nproc
CALL MPI_ALLTOALL (a, m, MPI_REAL, &	! Sender
                   a, m, MPI_REAL, &    ! Receiver
!		buff, count, type
		   MPI_COMM_WORLD, err)
! REMARK: count is the number of elements sent from one
!   process to another

WRITE(*,*) "Process", myid, "received:", a

CALL MPI_FINALIZE (err)
END PROGRAM All_to_all

