Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7731 Discussions

[OpenMP] class member variable in class member functions

class Foo {
    double scaleFactor;
    std::vector<double> a,b;
    void init(){
        scaleFactor = 0.5;
        // init a, b to same size

    void bar(){
        const int len=a.size();
        // The bug is removed if I insert the following line
        // const double scaleFactor=this->scaleFactor;
        #pragma omp parallel for
        for(int i=0;i<len;i++){

int main(){
    Foo foo;
    return 0;

My code is like this (more complicated but can be simplified as this), and has a bug when compiled with icpc 2019 (gcc 7.4 in PATH), linked to iomp5. The scaleFactor in member function bar() has value 0 in the openmp loop. The bug can also be removed if one line is added as the comment described. If I switch to another compiler, like gcc 8.2 (no intel, linked to gomp), the extra line is not needed and the bug does not appear.

My question is:

(1) Can I use member variable in member function? It seems OK according to OpenMP 4.5 specification,

 A variable that is part of another variable (as an array or structure element) cannot appear in a shared clause except if the shared clause is associated with a construct within a class non-static member function and the variable is an accessible data member of the object for which the non-static member function is invoked.

(2) Are there any best-practice recommendations?


0 Kudos
0 Replies