c
c	iowait.f 1.5 92/07/01 15:26:36
c
c  Copyright 1991 Intel Corporation, Inc.
c  See the "Copyright" file for complete rights and liability information.
c
c
c  Requires at least a two-node cube.  Node 0 creates and writes to the file;
c  other nodes read from it.
c
c  Note:  To ensure that the cfs file "/cfs/mydata" exists in a
c         known state, load the program in the file "myfile.f" first.

      include 'fcube.h'

      integer   iam, mode, msgid, size
      parameter (size=10000)
      integer   sbuf(size), rbuf(size)

c  Open existing file.

      open(unit = 12, file = '/cfs/mydata',
     1     status = 'old', form = 'unformatted')
      call setiomode(12, 0)

c  Identify self.

      iam = mynode()

c  If node 0, then ...

      if(iam .eq. 0) then
         print *,'Starting ...'

c    Verify I/O mode.

         mode = iomode(12)
         write(*, 100) mode
100      format('I/O mode set to: ', i2, '.')

c    Create the list.

         do 1 i = 1, size
            sbuf(i) = i
1        continue

c    Write to the file.

         msgid = iwrite(12, sbuf, 4*size)

c    Wait until the write completes.

         call iowait(msgid)

         write(*, 200) iam
200      format('Node ',i3, ' finished writing.')

c    Force other node(s) to wait for the write to complete.

         call gsync()

c  If not node 0, then ...

      else

c    Wait for the write to complete.

         call gsync()

c    Read the file.

         msgid = iread(12, rbuf, 4*size)

c    Loop until the read completes.

         do while (iodone(msgid) .eq. 0)
            write(*, 300) iam
300         format('Node ', i3, ' looping ....')
         end do

c    Display the data.

         write(*, 400) iam
400      format('Node ',i3, ' finished reading.')

      endif

      end
