1
votes

I have a Fortran subroutine that is called several times in a main program (which I don't have access to). In my subroutine, I wish to read data from one of several (~10^4) files in every iteration based on an input argument. Each of the files has one line of data; and the format of my data is as follows:

  0.97014199999999995       0.24253600000000000        0.0000000000000000    

I'm using the following lines of code to open and read the files:

      program test_read
      implicit none
      integer  :: i, iopen_status, iread_status
      real :: gb
      CHARACTER(len=25) :: filename
      CHARACTER(*), PARAMETER :: fileplace =
     & "/home/ajax/hexmesh_readn/G3/"
      dimension gb(3)

      i = 5
      WRITE(filename,'(a,I0,a)')'GBn_',i,'.txt'
      open(unit=15,
     & file=fileplace//filename,IOSTAT=iopen_status)
      read (15,*,IOSTAT=iread_status) gb
      print *,"gb",gb(1),gb(2),gb(3)
      close(15)
      end program test_read
  • In the main program, i is a variable, but I have a file for all possible values of i.
  • Now, this code works perfectly well when I run on my local machine. But, when I submit it along with the main program, it behaves somewhat weirdly. Specifically, it reads some of the files, but not others.
  • When I print out the IOSTAT for open and read, I see that the IOSTAT for open is 0 for all the files, whereas that for the read command is 0 for some, -1 for some and 29 for others! I looked up what the error code 29 means and I learned that it might indicate that the file is not found in the path. But the file is most definitely there.
  • Also, I don't see anything different about the files that it isn't able to read. In fact, I have even seen the same file giving an IOSTAT value of 0 and 29!
  • One thing to note is that I'm running the main program on several cores. Could this have anything to do with the error?
1
I guess you are using fixed-form source, so please pay attention to the formatting of the code (you probably just want to indent by four more columns). My advice elsewhere to use an iomsg= specifier (and check iostat values when you use iostat=) applies here. - francescalus
Looks like iomsg is only available in Fortran 2003 onwards, but I'm using Fortran 90. Also, where do you mean I should indent by 4 columns? Before the first column? - Ajax
With iomsg=, just try it. It may not work for your compiler, but there are very few Fortran 90 only compilers currently in popular use (and not many more Fortran 2003 compilers). And it's worth noting that your code in the question isn't Fortran 90. - francescalus
@francescalus is right! Your code isn't even Fortran 2003 compliant. Implied-shape declarations were introduced in Fortran 2008; hence `CHARACTER(*), PARAMETER :: fileplace = & "/home/ajax/hexmesh_readn/G3/" is not valid Fortran 90 but perfectly legal Fortran 2008+. - jlokimlin
@jlokimlin There are no implied shape declarations in the code shown. CHARACTER(*) is an assumed length declaration. That syntax is valid Fortran 90. - IanH

1 Answers

1
votes

Are you running several instances of the same program simultaneously? On some operating systems, different programs can't simultaneously open the same file. Specifying that you want read-only access might allow access by multiple programs. On the Fortran open statement: action='read'.

If you are running a multi-threaded program, then different threads might be doing IO simultaneously on different files ... different unit numbers should be used by each thread to avoid conflicts.