Threadprivate Directive

The OpenMP threadprivate directive may appear anywhere in a program relative to other OpenMP constructs, but there are restrictions about where it can appear for any particular variable (see below). The directive begins with the OpenMP sentinel followed by threadprivate and a list of global variables. In C, the variables in the list may have file-scope, namespace-scope, or be static block-scope. In Fortran the list of variables may include variables and named common blocks whose names are delimited by slashes.

Example:

    !$OMP THREADPRIVATE A,B,C,/MYCOMMON/

These variables will be replicated in all OpenMP threads. There is no guarantee of just when each will be initialized in its thread, except that it will be initialized before it is first referenced. Outside of parallel regions, references will be to the initial thread's copy of each variable. Inside parallel regions, references in the primary thread will be to the copy in the thread that initiated the parallel region.

Values in threadprivate objects will persist between parallel regions under certain circumstances. Values in the initial thread's copy are guaranteed to persist between two consecutive references to the object. Values in threadprivate objects of threads other than the primary thread will persist between two consecutive parallel regions only if all the following conditions are met:

  1. neither parallel region is nested in another parallel region
  2. the number of threads in both parallel regions is the same
  3. the dynamic internal variable is false at entry to the first parallel region and remains false until entering the second region
  4. the nthreads internal variable is the same at entry to both parallel regions.

If a threadprivate variable that would otherwise be persistent is subject to a copyin clause on the second parallel region, the results are undefined.

Fortran common blocks and module variables that are listed in threadprivate directives implicitly have the SAVE attribute.

There are restrictions on threadprivate directives. Threadprivate objects may not appear in a clause of another directive except copyin, copyprivate, schedule, num_threads, or if clauses. The location of the threadprivate declaration for any object must be carefully chosen. It must be after the object has been declared, but before it has been referenced. You cannot wait until you are in a nested scope to declare the variable threadprivate. If a variable is declared threadprivate in one translation unit, it must be so declared in all translation units in which it is declared. This means, among other things, that threadprivate common blocks must be so declared in all program units where they appear. A blank common block cannot appear in a threadprivate directive. Fortran variables that are threadprivate cannot be listed in common blocks or EQUIVALENCE statements.

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