ENFS Fortran


In order to facilitate use of the 'enfs:' prefix with the full path to the file in Fortran applications, Aidan Thompson has generously contributed the code samples and explanation below.


In FORTRAN, instead of:
      open(unit=10,file='outfile',status='unknown')
    
you need to use:
      open(unit=10,file='enfs:/enfs/tmp/athomps/test/outfile',status='unknown')
    

Of course, hard-wiring filepaths into your code is not a good idea, and providing the path in an input script can also be troublesome, particularly for FORTRAN uses, as it has such lousy support for character string operations.

My solution has been to use the non-standard but widely available intrinsic function getcwd, which provides the path of the current working directory. Most importantly, it works on Cplant.

Below is a FORTRAN routine getpath which reliably returns the current working directory using getcwd. You can use it to convert simple FORTRAN file opens to parallel I/O file opens, as follows:


      Old code:

     
	  :
	  open(unit=10,file='outfile',status='unknown')
	  :
	
    
      New code:   
    
      
	  character getpath*128
	  external getpath
	  :
	  
	  open(unit=10,file='enfs:'//trim(getpath())//'/outfile',status='unknown')
	  :
	
    
A more extensive demo is given in the test program at the end of the file.
      
c----------------------------------------------------------------
c     This provides a safe version of getcwd,
c     that works with a variety of compilers.
c
c	Note: len, len_trim, trim are standard on f90, but not
c	on older compilers.
c
c     Authored by Aidan P. Thompson 8/31/01
c
      character*(*) function getpath()
      implicit none
      integer istatus,getcwd,ind0,ind1
c
c     This routine uses getcwd, which is not a fortran standard
c     It assumes the returned value of path is in one of two forms.
c
c     a) path followed by all blanks
c
c     b) path followed by a char(0) and then garbage, possibly blanks.
c
c     To avoid overflow, we need len(getpath) >= length of path + 1
c
      do ind0 = 1,len(getpath)
         getpath(ind0:ind0) = char(0)
      end do

      istatus = getcwd(getpath)
      ind1=max(index(getpath,char(0)),index(getpath,' '))

      do ind0 = ind1,len(getpath)
         getpath(ind0:ind0) = ' '
      end do

      return
      end


c----------------------------------------------------------------
c     This demonstrates how to use getpath 
c     to open a file using enfs: or else standard I/O
c     Authored by Aidan P. Thompson 8/31/01
c
c     You can compile it for CPlant machines 
c     and run it using yod -sz 1,
c     or you can compile it for serial machines 
c     (to test portability of getpath).
c
      program enfs_test
      implicit none
      integer maxpathlen
      parameter (maxpathlen = 129)
      character dumpfile*32
      character mypath*maxpathlen
      character getpath*maxpathlen
      logical Lenfs

      write(6,*) 'Do you want to use enfs:? (T/F)'
      read(5,*) Lenfs
      dumpfile = 'ldump_file'
      mypath = getpath()
      if (len_trim(mypath).eq.0) then
         write(6,*) 'getcwd failed'
         stop
      elseif(len_trim(mypath).eq.len(mypath)) then
         write(6,*) 'path too long'
         stop
      else
         if (Lenfs) then
            if (mypath(1:9).eq.'/enfs/tmp') then
               open (11,file='enfs:'//trim(mypath)//'/'//dumpfile,
     $              status='unknown')
               write(11,*) 'Hello enfs world'
            else
               write(6,*) 'enfs: only works on enfs filesystem'
               stop
            endif
         else
            open (11,file=trim(mypath)//'/'//dumpfile,
     $           status='unknown')
            write(11,*) 'Hello no-enfs world'
         endif
      endif
 
      stop
      end
      
    

Ruth Klundt
Last modified: Fri Sep 28 13:34:20 MDT 2001