I'm writing a program that reads huge file (3x280 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.
firstprivate
variables are copied, not private
- for the latter the default constructor is called:
Section 2.9.3.3 - private
clause:
The new list item is initialized, or has an undefined initial value, as if it had been locally declared without an initializer. The order in which any default constructors for different private variables of class type are called is unspecified. The order in which any C/C++ destructors for different private variables of class type are called is unspecified.
Here is a simple demonstration code:
#include <fstream>
#include <stdio.h>
#include <omp.h>
int main (void)
{
std::fstream reader("test.txt", std::ios::in);
printf("Main thread: reader.is_open() = %d\n", reader.is_open());
#pragma omp parallel private(reader)
{
printf("Thread %d: reader.is_open() = %d\n",
omp_get_thread_num(), reader.is_open());
}
return 0;
}
And here is the output as expected:
Main thread: reader.is_open() = 1
Thread 1: reader.is_open() = 0
Thread 0: reader.is_open() = 0
Thread 3: reader.is_open() = 0
Thread 2: reader.is_open() = 0
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 when firstprivate
is 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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With