PROGRAM main
    USE double
    USE mpi
    IMPLICIT NONE
    INTEGER, PARAMETER :: procs = 4, size = 3, root = 1
    INTEGER :: nprocs, myrank, error
    REAL(KIND=DP), DIMENSION(size,procs) :: source
    REAL(KIND=DP), DIMENSION(size) :: result
    REAL(KIND=DP), DIMENSION(2) :: inpair, outpair

    source = 0.0_DP
    result = 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
    inpair(1) = MAXVAL(result)
    inpair(2) = myrank
    CALL MPI_Reduce(inpair,outpair,1,MPI_2DOUBLE_PRECISION,    &
        MPI_MAXLOC,root,MPI_COMM_WORLD,error)
    IF (myrank == root)    &
        WRITE (*,"('Maximum value ',F0.3,' on processor ',I0)")    &
            outpair(1),INT(outpair(2))
    CALL MPI_Finalize(error)
END PROGRAM main
