Clarification for 7.3 private Clause

Discuss the OpenMP Examples document, updated for OpenMP 4.5
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help:
Posts: 2
Joined: Mon Aug 20, 2018 6:57 am

Clarification for 7.3 private Clause

Post by bsteinb »


I find it difficult to understand what is going on in section 7.3 regarding the private clause in the Examples document. Specifically, the explanatory comment in private.2.c which I will reproduce here (and private.2.f which is very similar) seem to be at odds with my understanding of the scoping rules of the base programming languages.

Code: Select all

int a;

void g(int k) {
  a = k; /* Accessed in the region but outside of the construct;
          * therefore unspecified whether original or private list
          * item is modified. */

void f(int n) {
  int a = 0;

  #pragma omp parallel for private(a)
   for (int i=1; i<n; i++) {
       a = i;
       g(a*2);     /* Private copy of "a" */
As I understand it, in this example, there are two variables that go by the name "a". One file-scope variable a, declared on the first line of the listing and one block-scope variable which I will call f::a declared in the first line of the body of function f. According to the scoping rules in C, this f::a should shadow the file-scope a in the body of f and the OpenMP Application Program Interface in section 2.15.3 on Data-Sharing Attribute Clauses states (in the second sentence):
Data-sharing attribute clauses apply only to variables for which the names are visible in the construct on which the clause appears.
Thus the private clause on the parallel for construct in f should affect only the block-scope f::a.

Now, since f::a is not in scope in the body of g, the access to a in the body of g should affect the file-scope a. However, the comment in g suggests that the access is actually to f::a (since it mentions private list items and only f::a is declared private).

I would appreciate any clarification on whether (and how) OpenMP modifies the base language scoping rules in this example (and the Fortran example as well).

Best regards