PROGRAM main
    USE double
    USE mpi
    IMPLICIT NONE
    INTEGER, PARAMETER :: procs = 4, root = 3
    INTEGER :: nprocs, myrank, error
    INTEGER, DIMENSION(procs,procs) :: source
    INTEGER, DIMENSION(procs) :: one, two

    source = 0.0_DP
    one = 0.0_DP
    two = 0.0_DP
    CALL MPI_Init(error)
    CALL MPI_Comm_size(MPI_COMM_WORLD,nprocs,error)
    IF (nprocs /= procs) THEN
        PRINT *, 'Invalid number of processes'
        CALL MPI_Abort(MPI_COMM_WORLD,1,error)
    END IF

    CALL MPI_Comm_rank(MPI_COMM_WORLD,myrank,error)
    IF (myrank == root) THEN
        OPEN (11,FILE="Programs/integers.input",ACTION="READ")
        READ (11,*) source
        CLOSE(11)
    END IF

    CALL MPI_Scatter(source,procs,MPI_INTEGER,    &
        one,procs,MPI_INTEGER,root,MPI_COMM_WORLD,error)
    WRITE (*,"('Processor ',I0,' first data',100(1X,I0))") myrank,one
    CALL MPI_Alltoall(one,1,MPI_INTEGER,    &
        two,1,MPI_INTEGER,MPI_COMM_WORLD,error)
    WRITE (*,"('Processor ',I0,' second data',100(1X,I0))") myrank,two
    CALL MPI_Finalize(error)
END PROGRAM main
