OpenMP® Forum

Discussion on the OpenMP specification run by the OpenMP ARB. OpenMP and the OpenMP logo are registered trademarks of the OpenMP Architecture Review Board in the United States and other countries. All rights reserved.

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

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.

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

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.