PROGRAM posix_io

  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER :: i, myid, nproc, bufsize, ierr
  INTEGER, PARAMETER :: nx = 32
  INTEGER, ALLOCATABLE :: buf(:,:)
  INTEGER :: fp, intsize

! Variables for cartesian topology
  INTEGER, DIMENSION(2) :: mpi_dims, coords
  LOGICAL, DIMENSION(2) :: period = (/.FALSE., .FALSE./)
  INTEGER :: cart_comm

! Variables for domain decomposition and subarray view 
  INTEGER, DIMENSION(2) :: dimsf = (/nx, nx/)
  INTEGER, DIMENSION(2) :: count, offset

  INTEGER(KIND=MPI_OFFSET_KIND) :: disp
  INTEGER :: filetype


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

  mpi_dims(1) = INT(SQRT(REAL(nproc)))
  mpi_dims(2) = mpi_dims(1)
  IF (mpi_dims(1) * mpi_dims(2) /= nproc ) STOP 'Wrong number of processes' 
  CALL MPI_CART_CREATE(MPI_COMM_WORLD, 2, mpi_dims, period, 0, cart_comm, ierr)
  CALL MPI_COMM_RANK(cart_comm, myid, ierr)
  CALL MPI_CART_COORDS(cart_comm, myid, 2, coords, ierr)

  DO i=1,2
    count(i) = nx / mpi_dims(i)
    offset(i) = coords(i) * count(i)
  END DO
  bufsize = count(1) * count(2)

  ALLOCATE(buf(count(1), count(2)))

  buf(:,:) = myid

! Begin TODO
! Open a file, create a proper filetype and write buf to a file 

! End TODO

  CALL MPI_FINALIZE(ierr)

END PROGRAM
