!************************************************************************
!* mpioExampleF.f:							*
!* This is an example of a FORTRAN code which uses MPIO.		*
!************************************************************************
#ifdef AMPI
  subroutine MPI_main
#else
  program main
#endif
  implicit none

  include 'mpif.h'
  include 'mpiof.h'
  !*    *******************************************************************
  !*    * # of times to repeat write/read cycle				*
  !*    *******************************************************************
  integer NTIMES 
  parameter (NTIMES=5)
  !*    *******************************************************************
  !*    * read/write size in bytes					*
  !*    *******************************************************************
  integer NBYTES 
  parameter (NBYTES=1024*1024*4)

  integer nprocs, myrank, i
  character*1024 str 
  integer buf(NBYTES/4)
  integer fh, status(MPI_STATUS_SIZE)
  integer ierr, argc, iargc
  integer*8 offset

  call MPI_INIT(ierr)
  call MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, ierr)
  call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
  !*    *******************************************************************
  !*    * process 0 takes the input file name as a command-line		*
  !*    * argument and broadcasts it to other processes			*
  !*    *******************************************************************
!  if (myrank .eq. 0) then
!     argc = iargc()
!     i = 0
!     call getarg(i,str)

!     do while ((i .lt. argc) .and. (str .ne. '-fname'))
!        i = i + 1
!        call getarg(i,str)
!     end do

!     i = i + 1
!     call getarg(i,str)
!     call MPI_BCAST(str, 1024, MPI_CHARACTER, 0, MPI_COMM_WORLD,   ierr)
!     print*, 'Access size per process = ', NBYTES,'  bytes, ',' NTIMES= ', NTIMES
!  else
!     call MPI_BCAST(str, 1024, MPI_CHARACTER, 0, MPI_COMM_WORLD,           ierr)
!  end if
  str = '/ptmp/jedwards/trythis.bin'
  offset = myrank*NBYTES

  do i=1, NTIMES

     !*        ***************************************************************
     !*        * open the file, seek, write, and then close			*
     !*        ***************************************************************
       print *,__FILE__,__LINE__,i
     call MPI_FILE_OPEN(MPI_COMM_WORLD, str,MPI_MODE_CREATE+MPI_MODE_RDWR, MPI_INFO_NULL, fh, ierr)
       print *,__FILE__,__LINE__,i
     call MPI_FILE_SEEK(fh, offset, MPI_SEEK_SET, ierr)
       print *,__FILE__,__LINE__,i
     call MPI_FILE_WRITE(fh, buf, NBYTES, MPI_BYTE, status, ierr)
       print *,__FILE__,__LINE__,i
     call MPI_FiLE_CLOSE(fh, ierr)
       print *,__FILE__,__LINE__,i

     !*       ****************************************************************
     !*       *  wait for all nodes to close file before proceeding		*
     !*       ****************************************************************
     call MPI_BARRIER(MPI_COMM_WORLD, ierr)


     !*       ****************************************************************
     !*       *  open the file again, seek, read, and then close		*
     !*       ****************************************************************
     call MPI_FILE_OPEN(MPI_COMM_WORLD, str, MPI_MODE_CREATE+MPI_MODE_RDWR, MPI_INFO_NULL,   fh, ierr)
     call MPI_FILE_SEEK(fh, offset, MPI_SEEK_SET, ierr)
     call MPI_FILE_READ(fh, buf, NBYTES, MPI_BYTE, status, ierr)
     call MPI_FILE_CLOSE(fh, ierr)

     !*       ****************************************************************
     !*       *  wait for all nodes to close file before proceeding		*
     !*       ****************************************************************
     call MPI_BARRIER(MPI_COMM_WORLD, ierr)

  end do

  call MPI_FINALIZE(ierr)

  stop
#ifdef AMPI
  end subroutine
#else
  end program main
#endif
