0

There is a similar question here for parallel sections instead of tasks: OpenMP and C++: this pointer, but I don't see anything in the standard (currently 5.2) which would confirm that 'this' should be shared.

Here is a member function:

long long kdnode::searchkdtree(kdnode *q, int p,
               long long cut, long long cut2,
               int depth, int depthmax)
{
 long long xij, yij, zij, wij, r2, count, countL, countH;

  // ...
#pragma omp task shared(countH) if(depth < depthmax) \
   firstprivate(q, p, cut, cut2, depth, depthmax)
 {
   countH = hi->searchkdtree(q, p+1, cut, cut2, depth+1, depthmax);
  }
  // ...
}

"hi" is a member of kdnode, will be reached by using 'this': this->hi->.... The only thing I can find in standard (5.1.1, p.99):

• In an orphaned task generating construct, if no default clause is present, formal arguments passed by reference are firstprivate.

Technically, 'this' pointer will be passed to the member function as a hidden parameter, by value - but I strongly suspect that the desired effect is 'shared' 'this'. Can somebody point me to a place in the standard please?

Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • Task data is shared if it was shared in the parallel region that created the task, firstprivate if it was private. So here it depends on the environment that calls your routine. I think. – Victor Eijkhout Feb 16 '22 at 04:47
  • @Victor I did a test, a class member variable is shared if it is accessed from a task created by a member function, but I do not know which rule applies here. The code can be found [here](https://godbolt.org/z/cbYdjnosa). – Laci Feb 16 '22 at 08:24
  • Confirmed on a couple of compilers (godbolt doesn't compile this with Intel). I'm asking someone who understands more about OMP than I because reading the standard I can not figure this one out. – Victor Eijkhout Feb 16 '22 at 22:23
  • I thought about this more. 'this' itself (pointer) can't be modified (if I am not wrong), so it doesn't really matter if it'll be shared or firstprivate. By standard I think is should be firstprivate (last rule in 5.1.1: In a task generating construct, if no default clause is present, a variable for which the data-sharing attribute is not determined by the rules above is firstprivate.). We care about the object here, and it will be shared: Objects with dynamic storage duration are shared – Natalia Glagoleva Feb 16 '22 at 22:53
  • @NataliaGlagoleva In the example I showed you the class has automatic storage duration, but still member variables are shared. I think the only reasonable explanation is that `this` pointer is not considered as a variable, so `this->hi` behaves like it would be shared. I think this is the explanation for the fact that compiler does not complain about class member variables if `default(none)` is used. – Laci Feb 17 '22 at 06:11
  • Check this: https://stackoverflow.com/questions/4610656/why-is-class-member-variable-x-not-allowed-to-be-sharedx-in-openmp – Laci Feb 17 '22 at 06:55

0 Answers0