pgCC & openMP -- miscompilation

I have the following C++ code:

#include <iostream>
#include <omp.h>
using namespace std;

class Test{
  public:
    Test(){
      cout << "constructing\n";
    }
    ~Test(){
      cout << "destructing\n";
    }
};

int main(){
  Test x;

  cout << "begin parallel\n";
  #pragma omp parallel private(x)
    {
    }
  cout << "end parallel\n";

  return 0;
}

Since I work on a four processor cluster, I’d expect the following output:

constructing
begin parallel
constructing
destructing
constructing
destructing
constructing
destructing
constructing
destructing
end parallel
destructing

In fact that’s exactly what I get if I compile it with icc. However, if I compile it using pgCC (ie. pgCC -mp -o omptest omptest.cpp), I get something different:

constructing
begin parallel
end parallel
destructing

Why is it so? Why the constructor is not called when a new copy of variable x is created at the beginning of the parallel area? Is it a bug in pgCC?

Thanks a lot for any suggestion
petr

Hi Petr,

Did you set OMP_NUM_THREADS to 4? By default we only run with 1 thread.

Hongyon

Thank you, you are right, I haven’t done it. However problem remains the same. If I change the code:

#include <iostream> 
#include <omp.h> 
using namespace std; 

class Test{ 
  public: 
    Test(){ 
      cout << "constructing\n"; 
    } 
    ~Test(){ 
      cout << "destructing\n"; 
    } 
}; 

int main(){ 
  Test x; 
  
  int nprocs = omp_get_num_procs();
  omp_set_num_threads(nprocs);

  cout << "begin parallel\n"; 
  #pragma omp parallel private(x) 
    {
      cout << omp_get_thread_num() << "\n";
    } 
  cout << "end parallel\n"; 

  return 0; 
}

then the result is

constructing
begin parallel
0
2
1
3
end parallel
destructing

ie. the constructor (and the descructor) for x is not called…

Hi,

I have replicated you problem and file a TPR#14247. Thanks for reporting the problem.

Hongon

Hi,

The problem is fixed and will be out in 7.2-1 release.

Thank you,
Hongyon