Shared and Private Variables in a Parallel Environment (C Only)

Variables can have either shared or private context in a parallel environment.

The default context of a variable is determined by the following rules:

The following code segments show examples of these default rules:

int E1;                        /* shared static     */
 void main (argvc,...) {        /* argvc is shared   */
   int i;                       /* shared automatic  */
   void *p = malloc(...);       /* memory allocated by malloc   */
                                /* is accessible by all threads */
                                /* and cannot be privatized     */
   #pragma omp parallel firstprivate (p)
   {
     int b;                     /* private automatic  */
     static int s;              /* shared static      */

     #pragma omp for
     for (i =0;...) {
       = b;                     /* b is still private here !    */
       foo (i);                 /* i is private here because it */
                                /* is an iteration variable     */
     } 
     #pragma omp parallel
     {
       = b                      /* b is shared here because it  */
                                /* is another parallel region   */
     }
   }
 }
int E2;                        /*shared static */ 
 void foo (int x) {             /* x is private for the parallel */
                                /* region it was called from     */
   int c;                       /* the same */
 ... }

The compiler can privatize some shared variables if it is possible to do so without changing the semantics of the program. For example, if each loop iteration uses a unique value of a shared variable, that variable can be privatized. Privatized shared variables are reported by the -qinfo=private option. Use critical sections to synchronize access to all shared variables not listed in this report.

Some OpenMP preprocessor directives let you specify visibility context for selected data variables. For more information, see OpenMP directive descriptions or the OpenMP C and C++ Application Program Interface specification.



Program Parallelization
Countable Loops
Reduction Operations in Parallelized Loops


Control Parallel Processing with Pragmas


#pragma Preprocessor Directives for Parallel Processing
#pragma ibm critical Preprocessor Directive
info Compiler Option
OpenMP Specifications