https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92836

--- Comment #10 from Andrew Benson <abensonca at gmail dot com> ---
(In reply to Thomas Koenig from comment #8)
> > No. The inquire() is used only to see if the file exists already. If it
> > does, the code branches to read the file, if it does not, the code branches
> > to generate the data for the file and then write it.
> 
> You have to watch out here.
> 
> If you are
> 
> a) using INQUIRE to check if the file exists
> 
> b) using OPEN if it does not
> 
> in a thread, this is classic race condition - separate threads
> may INQUIRE and then try to open, in parallel. This is not atomic,
> and is a classic race condition.
> 
> You should always put such a sequence into OMP CRITICAL.  This has
> the nice side effect that you will also avoid this bug (which should
> be fixed regardless).

It's actually doing a sequence like this:

1) INQUIRE to test for the existence of the file; assuming it does not exist
then:
2) Synchronize access to the file using an OMP CRITICAL section, and a file
lock (the file lock being necessary as this is running with MPI so there are
multiple processes than can be attempting to access the file);
3) INQUIRE again to test the existence of the file (since it's possible that it
just happened to have been created by another thread)
4) Generate data to be written to the file;
5) Open, write, and close the file.
6) Exit the OPENMP CRITICAL section and release the file lock.

This clearly allows multiple threads to INQUIRE in parallel, but I don't think
it allows any race condition on the OPEN. If I'm wrong about that though then I
should fix my code!

> Also, why do you use inquire at all? AFAIK, it is not an error
> to OPEN a file more than one if you don't change anything, so
> a simple
> 
>   open (10,file="foo.dat",status="unknown")
> 
> should, in principle, work even with OpenMP; if it doesn't, this is
> another bug that needs fixing.

I haven't tried this. Are you suggesting this as an alternative to using
INQUIRE? My reasoning for using INQUIRE to check the existence of the file is
that if it doesn't exist I want one thread to proceed to create the data for
the file (and actually that thread will then spawn another set of nested OpenMP
threads to do that work).

Reply via email to