c
c FNAME - filename for test program
c
#define BASE_NAME 'shared.file'
#ifdef  HPIODIR
#  define FNAME   HPIODIR//BASE_NAME
#else
#  define FNAME   BASE_NAME
#endif
#define SYNC  synch(77)

      program sftest
c
      implicit none
#include "sf.fh"
c
      integer chunk,bchunk
      parameter (bchunk=32768)
      parameter (chunk=bchunk/8)
      double  precision buf(chunk)
      double  precision offset
      integer fsize
      integer id
c
      integer nodeid, nnodes
      integer me,nproc,handle,i,id, rc
      double precision tcgtime, tt0, ttr,ttw
      fsize = 5000*chunk
c
      call pbeginf()
c
      me = nodeid()
      nproc = nnodes()
c
      rc = sf_create(FNAME,
     $     dble(8*fsize),dble(8*fsize),dble(bchunk),handle)
      if (rc.ne.0) call parerr('create failed',rc)
c
      do i = 1, chunk
         buf(i) = dble(me)
      enddo
c
      call SYNC
      tt0 = tcgtime()
      do i = me, fsize/chunk -1, nproc 
            rc = sf_write(handle,  dble(8*i*chunk), 
     &           dble(bchunk), buf, id) 
            if (rc.ne.0) call parerr('write failed',rc)
            rc = sf_wait(id)
            if (rc.ne.0) call parerr('wait(w) failed',rc)
      enddo
      ttw = tcgtime() -tt0
c     call dgop(88,ttw,1,'max')
      call SYNC
c
      tt0 = tcgtime()
      do i = Mod(me+1,nproc), fsize/chunk -1, nproc
            rc = sf_read(handle,  dble(8*i*chunk),
     &           dble(bchunk), buf, id)
            if (rc.ne.0) call parerr('read failed',rc)
            rc = sf_wait(id)
            if (rc.ne.0) call parerr('wait(r) failed',rc)
      enddo
      ttr = tcgtime() -tt0
c     call dgop(88,ttr,1,'max')
      call SYNC
c
      rc = sf_destroy(handle)
      if(me.eq.0)then
        write(*,*)' '
        write(*,*)'Shared File size= ',1d-6*fsize*8,' MB/s P=',nproc
        write(*,*) 1d-6*fsize*8/ttw,' MB/s write rate'
        write(*,*) 1d-6*fsize*8/ttr,' MB/s read rate'
        write(*,*)' '
      endif
c
      call SYNC
      call pend()
      end
