PROGRAM posix_io

  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER :: i, myid, nproc, bufsize, ierr
  INTEGER, ALLOCATABLE :: buf(:), writebuf(:)
  INTEGER :: fp, intsize
  INTEGER(KIND=MPI_OFFSET_KIND) :: disp

  CALL MPI_INIT(ierr)
  CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr);
  CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr);

  bufsize = 100 / nproc;
  IF (bufsize *nproc /= 100 ) STOP 'Wrong number of processes' 

  ALLOCATE(buf(bufsize))

  CALL MPI_FILE_OPEN(MPI_COMM_WORLD, "test.dat", &
       MPI_MODE_RDONLY, &
       MPI_INFO_NULL, fp, ierr);
  
  CALL MPI_TYPE_SIZE(MPI_INTEGER, intsize, ierr)
  disp = myid*bufsize*intsize
  CALL MPI_FILE_SEEK(fp, disp, MPI_SEEK_SET, ierr)
  CALL MPI_FILE_READ(fp, buf, bufsize, &
                   MPI_INTEGER, MPI_STATUS_IGNORE, ierr)

  CALL MPI_FILE_CLOSE(fp, ierr)
  WRITE (*,*) "Rank", myid, "first and last elements", buf(1), buf(bufsize)

  CALL MPI_FINALIZE(ierr)


END PROGRAM
