Why am I getting undefined behavior when using OpenMP firstprivate with std :: vector in an Intel compiler?

I have a problem when using OpenMP in combination with firstprivate and std :: vector on an Intel C ++ compiler. Take the following three functions:

#include <omp.h> void pass_vector_by_value(std::vector<double> p) { #pragma omp parallel { //do sth } } void pass_vector_by_value_and_use_firstprivate(std::vector<double> p) { #pragma omp parallel firstprivate(p) { //do sth } } void create_vector_locally_and_use_firstprivate() { std::vector<double> p(3, 7); #pragma omp parallel firstprivate(p) { //do sth } } 

The code compiles without warning:

 icc filename.cpp -openmp -Wall -pedantic 

(icc version 14.0.1 (compatible with gcc version 4.7.0))

or

 g++ filename.cpp -fopenmp -Wall -pedantic 

(gcc version 4.7.2 20130108 [gcc-4_7-branch version 195012] (SUSE Linux))

but after compiling with icc I get runtime errors such as:

 *** Error in `./a.out': munmap_chunk(): invalid pointer: 0x00007fff31bcc980 *** 

when calling the second function (pass_vector_by_value_and_use_firstprivate)

Thus, an error occurs only when using the firstprivate clause (which should call the copy constructor), and the vector is passed by the value of the function (which should also refer to the copy constructor). If you don’t either skip the vector, but create it locally in a function or not use firstprivate, no error! On gcc, I get no errors.

I am wondering how the code creates undefined behavior or if it is an error in icc?

+3
c ++ gcc vector openmp icc
May 21 '14 at 7:26
source share
1 answer

I have the same problem with ICC, but not with GCC. Looks like a mistake. Here is a workaround

 void pass_vector_by_value2(std::vector<double> p) { #pragma omp parallel { std::vector<double> p_private = p; //do sth with p_private } } 

On the other hand, in the general case, I do not pass non-POD by value for functions in any case. I would use the link, but if you do, you will get an error

 error: 'p' has reference type for 'firstprivate' 

The solution to this is the code that I posted above. Pass it by value or by reference, and then define a private copy inside the parallel area, as it was in the above code.

+1
May 21 '14 at 11:59
source share



All Articles