4

I would like to open 10,000 files with file names starting from abc25000 until abc35000 and copy some information into each file. The code I have written is as below:

PROGRAM puppy
IMPLICIT NONE

integer :: i
CHARACTER(len=3) :: n1
CHARACTER(len=5) :: cnum
CHARACTER(len=8) :: n2

loop1: do i = 25000 ,35000  !in one frame

  n1='abc'
  write(cnum,'(i5)') i
  n2=n1//cnum
  print*, n2
  open(unit=i ,file=n2)

enddo loop1

end

This code is supposed to generate files starting from abc24000 until abc35000 but it stops about half way saying that

At line 17 of file test-openFile.f90 (unit = 26021, file = '')

Fortran runtime error: Too many open files

What do I need to do to fix the above code?

Community
  • 1
  • 1
Vijay
  • 965
  • 5
  • 13
  • 27

3 Answers3

6

This limit is set by your OS. If you're using a Unix/Linux variant, you can check the limit using from the command line using ulimit -n, and raise it using ulimit -n 16384. You'll need to set a limit greater than 10000 to allow for all the other files that the shell will have open. You may also need admin privileges to do this.

I regularly bump the limit up to 2048 to run Fortran programs, but never as high as 10000. However, I echo the other answers that, if possible, it's better to restructure your program to close each file before opening the next.

Deditos
  • 1,385
  • 1
  • 13
  • 23
  • @Rook Regularly is prob an exaggeration. It's usually for a one-off task of converting satellite data from one file per image to one file per pixel time series. It's not code I'm proud of. – Deditos Sep 30 '11 at 20:12
  • Thought as much; only the sentence was put in a way that I just couldn't resist :))) – Rook Sep 30 '11 at 21:35
  • I am using linux ubuntu 10.10 64 bit and memory 10GB. I have small doubt. If I raise the ulimit to the value 16348, would there be any negative implication to the OS? Another question is why specifically have to the number 16348? I am eager to know this. Thanks in advance. – Vijay Oct 01 '11 at 00:43
  • Your OS kernel will impose a hard limit and won't allow you to raise this soft limit above that: see [this question](http://stackoverflow.com/questions/34588). The default values are usually powers of 2, so I was just sticking to that: 16384=2^14. It's not necessary though. – Deditos Oct 01 '11 at 11:50
3

Operating systems tend to have limits on resources. Typically on, for instance, Linux, there is by default a limit of 1024 file descriptors per process. The error message you're getting is just the Fortran runtime library passing information upwards that it was unable to open yet another file due to an OS error.

janneb
  • 36,249
  • 2
  • 81
  • 97
3

You need to work on the files one at a time (or in small groups that do not exceed the limitation imposed by the operating system).

for each file:
  open file
  write
  close file
William Pursell
  • 204,365
  • 48
  • 270
  • 300