Fortran I/O multiple files

General OpenMP discussion

Fortran I/O multiple files

Postby jah87 » Tue Aug 14, 2012 10:09 am

I am reading in a list of files from a control file:
a1.txt
b1.txt
a2.txt
b2.txt
a3.txt
b3.txt
a4.txt
b4.txt

and then reading from those files across multiple threads:
Code: Select all
Open(5,file='control')
...
Do i=1,nz
  Read(5,"(a80)") file1(i)
  Read(5,"(a80)") file2(i)
EndDo
...
Close(5)
...
!$OMP PARALLEL PRIVATE(mythread) SHARED(nthread)
mythread=1
nthread=1
!$ mythread=omp_get_thread_num()
!$ nthread=omp_get_num_threads()
!$OMP END PARALLEL
...
!$OMP PARALLEL PRIVATE(mythread,file1unit,file2unit,x,y) SHARED(file1,file2)
!$OMP DO
Do i=1,nz
...
  file1unit=50+mythread
  Write(*,*) 'index: ',i,' mythread: ',mythread,' unit: ',file1unit,' file: ',file(i)
  Open(file1unit,file=file1(i))
  Read(file1unit,*) x
  Close(file1unit)
...
  file2unit=150+mythread
  Open(fil2unit,file=file2(i))
  Read(file2unit,*) y
  Close(file2unit)
...
End Do
!$OMP END DO
!$OMP END PARALLEL


Setting environment variable OMP_NUM_THREADS=4
Submitting job with aprun -n1 -d4

I get the error message:
lib-4052 : UNRECOVERABLE library error
The specified file is already connected to another unit.
Encountered during an OPEN of unit 50

Fortran unit 50 is not connected
i: 3 mythread: 3 unit: 53 file: a1.txt
i: 2 mythread: 2 unit: 52 file: a2.txt
i: 4 mythread: 1 unit: 51 file: a3.txt
i: 1 mythread: 0 unit: 50 file: a4.txt


I thought I was doing everything in a threadsafe manner, but something is not working correctly.
Does anyone have any advice, suggestions, help?
Last edited by jah87 on Tue Aug 14, 2012 1:30 pm, edited 1 time in total.
jah87
 
Posts: 3
Joined: Tue Aug 14, 2012 9:29 am

Re: Fortran I/O multiple files

Postby jah87 » Tue Aug 14, 2012 12:38 pm

Okay, I discovered the problem/solution.

Due to the way OpenMP handles memory, mythread was being oversubscribed.

To fix, I just put !$OMP BARRIER after nthread=1:
Code: Select all
!$OMP PARALLEL PRIVATE(mythread) SHARED(nthread)
mythread=1
nthread=1
!$OMP BARRIER
!$ mythread=omp_get_thread_num()
!$ nthread=omp_get_num_threads()
!$OMP END PARALLEL
jah87
 
Posts: 3
Joined: Tue Aug 14, 2012 9:29 am

Re: Fortran I/O multiple files

Postby MarkB » Tue Aug 14, 2012 1:02 pm

Hi there,

I think you might still have a bug: in the second parallel region, mythread is shared (by default): where does its value comes from?
In the first parallel region mythread is private, and the private copies go out of scope at the end of this region....

Mark.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Fortran I/O multiple files

Postby jah87 » Tue Aug 14, 2012 1:30 pm

MarkB wrote:Hi there,

I think you might still have a bug: in the second parallel region, mythread is shared (by default): where does its value comes from?
In the first parallel region mythread is private, and the private copies go out of scope at the end of this region....

Mark.


This is just a typo in my translation from my code to the forum. mythread is globally declared as threadprivate in my code. I fixed the typo in the original post.
jah87
 
Posts: 3
Joined: Tue Aug 14, 2012 9:29 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 10 guests

cron