This is a small example of an EAF program which uses asynchronous I/O to open, write to, then read back from a file.


#include "chemio.h"
#include "eaf.fh"

        integer fh              ! File Handle
        integer id1, id2        ! asynchronous ID handles
        integer stat            ! Return status
        integer pend            ! Pending status
        integer iter            ! Iterations counter
        integer buf(100), x     ! Data

        iter = 0

        fh = EAF_OpenScratch('/piofs/mogill/test.out', CHEMIO_RW)

        stat = EAF_AWrite(fh, 0,  buf, 100*EAF_SZ_INT, id1)
        if(stat .ne. 0) write(0,*) 'Error doing 1st asynch write.  stat=', stat

        stat = EAF_AWrite(fh, 100*EAF_SZ_INT,  x, 1*EAF_SZ_INT, id2)
        if(stat .ne. 0) write(0,*) 'Error doing 2nd asynch write.  stat=', stat

100     stat = EAF_Probe(id1, pend)
        iter = iter + 1
        write(0,*) 'Waiting', iter
        if(iter .lt. 100  .and.  pend .eq. CHEMIO_PENDING) goto 100
        EAF_Wait(id1)

        stat = EAF_ARead(fh, 0, buf, 100*EAF_SZ_INT, id1)
        if(stat .ne. 0) write(0,*) 'Error doing 1st asynch read.  stat=', stat

        EAF_Wait(id2)
        stat = EAF_AWrite(fh, 100*EAF_SZ_INT,  x, 1*EAF_SZ_INT, id2)
        if(stat .ne. 0) write(0,*) 'Error doing 2nd asynch write.  stat=', stat
        EAF_Wait(id2)
        EAF_Wait(id1)

        stat = EAF_Close(fh)
        end


This example demonstrates use of asynchronous reading and writing. The entire buffer 'buf' is written to offset 0, the beginning of the file. The file is simultaniously written to from the scalar x in the position following the buffer. The positions in the file are determined by abosulte offset argument as with the synchronous write.

The first write, id1, is repeatedly probed for completion for 100 tries or until completion, whichever comes first. The two possible pending statuses are CHEMIO_DONE and CHEMIO_PENDING.

When a completed asynchronous operation is detected with EAF_Wait or EAF_Probe, the id is invalidated with CHEMIO_DONE. The following EAF_Wait(id1) blocks until id1 completes. Using EAF_Probe or EAF_Wait with an invalidated ID has no effect.

Once id1 is freed, it is reused in the first asynchronous read statement. The following EAF_Wait blocks for completion and invalidation of id2, which is then used to asynchronouslyread the scalar X.

The EAF_Close deletes the file because it was opened as a scratch (EAF_OpenScratch) file.