PROGRAM main
    USE double
    USE mpi
    IMPLICIT NONE
    INTEGER, PARAMETER :: procs = 8, size = 3, root = 1
    INTEGER :: nprocs, myrank, newcomm, error
    REAL(KIND=DP), DIMENSION(size,procs) :: source
    REAL(KIND=DP), DIMENSION(size) :: result, summary
    INTEGER, PARAMETER :: colour(0:7) = (/    &
        MPI_UNDEFINED,123,456,MPI_UNDEFINED,123,123,456,MPI_UNDEFINED /)

    source = 0.0_DP
    result = 0.0_DP
    summary = 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/reals.input",ACTION="READ")
        READ (11,*) source
        CLOSE(11)
    END IF

    CALL MPI_Scatter(source,size,MPI_DOUBLE_PRECISION,    &
        result,size,MPI_DOUBLE_PRECISION,root,MPI_COMM_WORLD,error)
    result = exp(result)
    WRITE (*,"('Processor ',I0,' now has data',100(1X,F0.3))") myrank,result
    CALL MPI_Comm_split(MPI_COMM_WORLD,colour(myrank),0,newcomm,error)
    IF (newcomm /= MPI_COMM_NULL) THEN
        CALL MPI_Reduce(result,summary,size,MPI_DOUBLE_PRECISION,    &
            MPI_SUM,root,newcomm,error)
        CALL MPI_Comm_free(newcomm,error)
    END IF
    WRITE (*,"('Processor ',I0,' summary data',100(1X,F0.3))") myrank,summary
    CALL MPI_Finalize(error)
END PROGRAM main
