Does OpenMP open private objects?

I am writing a program that reads a huge file (3x280 GB) and performs the corresponding procedure for the data in the file. This is quite convenient for parallelizing such a program, where it is easy to do using OpenMP.

I do not understand how private variables are accepted in OpenMP. As we all know, fstream objects are not copied intuitively, which prevented me from using it as a private object. Therefore, the file reader was split.

I had some problem later, and I thought about trying to use the philosophies as private ... and guess what? it worked !!! How is this possible? if the object is not copied, how can OpenMP use different copies of the same object for each core?

This is what 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) { ... } 

Thanks.

+4
source share
1 answer

firstprivate variables are copied, not private - for the last, the default constructor is called:

Section 2.9.3.3 - private :

A new list item is initialized or has an initial value of undefined, as if it were declared locally without an initializer. The order in which default constructors are called for different private variables of the class type is not specified. The order in which any C / C ++ Destructors for different private variables of a class type are called unspecified.

Here is a simple demo 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 result, 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 

The funny thing is that the Intel C ++ compiler makes a mistake with an internal error (a failed statement) - it was tested with versions 11.1, 12.0 and 12.1. On the other hand, the GNU C ++ compiler adheres to the standard (output above from g++ ). Both compilers complain when firstprivate used firstprivate , although the Intel C ++ compiler again fails with an internal error.

This may sound silly, but did you verify that you turned on OpenMP support in the particular compiler you are using?

+5
source

All Articles