PROGRAM main
    USE double
    USE mpi
    IMPLICIT NONE
    INTEGER, PARAMETER :: root = 1
    INTEGER :: nprocs, myrank, sizes(4), total, mysize, offsets(4), error, i
    REAL(KIND=DP), DIMENSION(:), ALLOCATABLE :: source, result

    CALL MPI_Init(error)
    CALL MPI_Comm_size(MPI_COMM_WORLD,nprocs,error)
    IF (nprocs /= 4) 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/mixed.input",ACTION="READ")
        READ (11,*) sizes
        total = 0
        DO i = 1,4
            offsets(i) = total
            total = total+sizes(i)
        END DO
        ALLOCATE(source(total))
        READ (11,*) source
        CLOSE(11)
        WRITE (*,"('Processor ',I0,' initial data',100(1X,F0.3))") myrank,source
    ELSE
        ALLOCATE(source(1))
    END IF

    CALL MPI_Scatter(sizes,1,MPI_INTEGER,    &
        mysize,1,MPI_INTEGER,root,MPI_COMM_WORLD,error)
    ALLOCATE(result(mysize))
    CALL MPI_Scatterv(source,sizes,offsets,MPI_DOUBLE_PRECISION,    &
        result,mysize,MPI_DOUBLE_PRECISION,root,MPI_COMM_WORLD,error)
    WRITE (*,"('Processor ',I0,' now has data',100(1X,F0.3))") myrank,result
    CALL MPI_Finalize(error)
END PROGRAM main
