OPEN MP + FILE I/O

General OpenMP discussion

OPEN MP + FILE I/O

Postby digimat » Fri May 02, 2008 7:55 am

Hello OpenMP Experts,
I would like to know what precautions need to be taken, while writting and reading a txt file in the parallel context. I have a third party code which is written in FORTRAN who decides the number of threads etc. I have written a C++ function which is called by this FORTRAN code. where I open a existing file and read them and initialize variables. I write to an another file some log message. As there is single input and log file I would like to have some advice and precautions to be taken for parallel codes.
Thank you,
digimat
digimat
 
Posts: 9
Joined: Fri Apr 18, 2008 2:23 am

Re: OPEN MP + FILE I/O

Postby ejd » Fri May 02, 2008 1:41 pm

As long as multiple threads are not trying to do I/O to a single file at the same time, you should be okay. It is similar to the problem you have if you were trying to do I/O to the same file from Fortran and C++. Each language or thread could have it's own "view" (i.e., set of control blocks for I/O) of the file. They can get in each others way if you are not careful. As long as you open the file and the information is shared and you don't try to do something like two writes to the same file at the same time, it should work fine. Generally most people read from or write to separate files so they can get some I/O speedup as well when using parallel.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: OPEN MP + FILE I/O

Postby digimat » Mon May 05, 2008 7:04 am

Hello,
Thank you very much for the reply. Still something is not clear to me in your explanation. I am writing to a single log file. If each thread is going to write to the same log file then I should see repeated log output, provided I open the files in append mode.
However if two threads write to a single file at the same time how will the things be handled ?? I do not see a way to avoid that two threads begin the write operation at the same time.
Thank you,
digimat
digimat
 
Posts: 9
Joined: Fri Apr 18, 2008 2:23 am

Re: OPEN MP + FILE I/O

Postby ejd » Mon May 05, 2008 2:37 pm

Unfortunately, what you want to do might not work. There are some file systems that can handle parallel I/O. However, the standard I/O provided by C, C++, and Fortran do have some limitations and may not handled this correctly. The OpenMP spec does have a statement about Fortran saying "unsynchronized use of Fortran I/O statements by multiple threads on the same unit has unspecified behavior". The spec says that the base language must be thread-safe and that all library, intrinsic and built-in routines provided by the base language must be threadsafe in a compliant implementation. However, if you do something like write to the same file from two different units (Fortran) or the same file from stdout and stderr, then there is no guarantee that the output will be correct. So there are some limitations that you have to be careful of.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests