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?
source share