OpenMP is intended for shared memory programming, so as you might expect, variables that are declared prior to a parallel region are generally shared by all the threads. Shared means that each thread accesses a given variable's value at the exact same memory location. When this is not what is desired, specified variables can be made private to individual threads via OpenMP private clauses or other clauses at the entry to the region. There are also special cases in which variables cannot be shared; for example, the loop counter in a parallel for construct will necessarily be private to each thread.

Note that in C/C++, variables that are declared inside a parallel region are private to each thread; that is, each thread keeps its own copy of these variables. And other language-based exceptions can arise in odd situations: What if the variable only appears within a function that is called from the region? What if the variable is a pointer to memory allocated from the heap? The rest of this page and the next deal with these exceptional situations.

When a function is called in a parallel region, the status and visibility of its variables depends on whether this call takes place in a worksharing construct (e.g., for in C or do in Fortran) or outside of it. In cases where the worksharing construct and the parallel region have the same extent (parallel for or parallel do, for example), there is no parallel region outside the worksharing construct, so the discussion below does not apply.

The terminology between C and Fortran is different enough that the sharing rules need to be discussed separately for each, though the basic sharing philosophy for both languages is the same.

In C code, variables that are declared inside a called routine are shared only if they are static (i.e., saved between calls), or if they have the const qualifier and have no mutable members (i.e., are read-only). Also, file-scope and namespace-scope variables in called routines are shared unless they appear in a threadprivate list. Outside of called routines and outside of workshare constructs, but inside a parallel region, variables with heap-allocated storage and static data members are shared. Arguments to called functions that are passed by reference inherit the property of the actual argument. Other variables declared in called routines are private.

In Fortran code, local variables declared in called routines that have the save attribute, or are data-initialized, or appear in common blocks, or are declared in modules, are shared unless they appear in a threadprivate list. Dummy arguments inherit the sharing attribute of the actual argument. Implied-do and forall indices are private. Other local variables in called routines are private.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement