I’m writing a program that reads huge file (3×280 GB) and does a fitting procedure to the data in the file. It’s pretty convenient to parallelise such a program, where this is easily done with OpenMP.
The thing I don’t understand is how private variables are taken in OpenMP. As we all know, fstream’s obejcts are a non-copyable, and intiuitively, that prevented me from using it as a private object. So the reader of the file was shared.
I got some problem later, and I thought of trying have fstreams as private, … and guess what? it worked!!! How could this be possible?! if the object is non-copyable, how could OpenMP use different copies of the same object for each kernel?
This is how my program looks like:
fstream dataReaderX(Dirs[0].c_str(), ios::in | ios::binary);
fstream dataReaderY(Dirs[1].c_str(), ios::in | ios::binary);
fstream dataReaderZ(Dirs[2].c_str(), ios::in | ios::binary);
#pragma omp parallel num_threads(cpus_num) shared(...) private(...,dataReaderX,dataReaderY,dataReaderZ)
{
...
}
Thank you.
firstprivatevariables are copied, notprivate– for the latter the default constructor is called:Section 2.9.3.3 –
privateclause:Here is a simple demonstration code:
And here is the output as expected:
One funny thing is that Intel C++ Compiler errs with internal error (a failed assertion) – tested with versions 11.1, 12.0 and 12.1. On the other hand GNU C++ compiler adheres to the standard (the output above is from
g++). Both compilers complain whenfirstprivateis used instead although Intel C++ Compiler again errs with internal error.It may sound stupid but did you check that you have enabled OpenMP support in the particular compiler that your are using?