Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

PRIVATE Clause

Parallel Directive Clause: Declares one or more variables to be private to each thread in a team.

Syntax

PRIVATE (list)

list

Is the name of one or more variables or common blocks that are accessible to the scoping unit. Subobjects cannot be specified. Each name must be separated by a comma, and a named common block must appear between slashes (/ /).

This clause allows each thread to have its own copy of the variable and makes it a local variable to the thread.

The following occurs when variables are declared in a PRIVATE clause:

  • A new object of the same type is declared once for each thread in the team (or for each implicit task in a region) and it is used by each thread (or task) inside the scope of the directive construct instead of the original variable. The new object is no longer storage associated with the original object.

  • All references to the original object in the lexical extent of the directive construct are replaced with references to the private object.

  • Variables defined as PRIVATE are undefined for each thread upon entering the construct and the corresponding shared variable is undefined when the parallel construct is exited. Within a parallel, worksharing, or task region, the initial status of a private pointer is undefined.

The value and allocation status of the original variable will change only in the following cases:

  • If it is accessed and modified by means of a pointer

  • If it is accessed in the region but outside of the construct

  • As a side effect of directives or clauses

  • If accessed and modified via construct association.

If a variable has the ALLOCATABLE attribute, the following rules apply:

  • If the variable is not currently allocated, the new list item will have an initial state of "unallocated".

  • If the variable is allocated, the new list item will have an initial state of allocated with the same array bounds.

A variable that appears in a PRIVATE clause may be storage-associated with other variables when the PRIVATE clause is encountered by constructs such as EQUIVALENCE or COMMON. If A is a variable appearing in a PRIVATE clause and B is a variable that is storage-associated with A, then the following applies:

  • The contents, allocation, and association status of B are undefined on entry to the parallel or task region.

  • Any definition of A, or any definition of its allocation or association status, causes the contents, allocation, and association status of B to become undefined.

  • Any definition of B, or any definition of its allocation or association status, causes the contents, allocation, and association status of A to become undefined.

  • A list item that appears in a private clause can be a selector in an ASSOCIATE construct. If the ASSOCIATE construct association is established before entry to a parallel region, the association between the associate name and the original PRIVATE list item will be retained in the parallel region.

The following are restrictions for the PRIVATE clause:

  • A variable that is part of another variable (as an array or structure element) must not appear in a PRIVATE clause.

  • A variable that appears in a PRIVATE clause must either be definable or it must be an allocatable array. This restriction does not apply to the FIRSTPRIVATE clause.

  • Assumed-size arrays must not appear in a PRIVATE clause.

  • A dummy argument that is a pointer with the INTENT (IN) attribute must not appear in a PRIVATE clause. This restriction does not apply to the FIRSTPRIVATE clause.

  • Variables that appear in NAMELIST statements, in variable format expressions, and in expressions for statement function definitions, must not appear in a PRIVATE clause.

Variables in a list can appear in other clauses as follows:

  • Variables that appear in a PRIVATE, FIRSTPRIVATE, or REDUCTION clause in a parallel construct can also appear in a PRIVATE clause in an enclosed parallel, task, or worksharing construct.

  • Variables that appear in a PRIVATE or FIRSTPRIVATE clause in a task construct can also appear in a PRIVATE clause in an enclosed parallel or task construct.

  • Variables that appear in a PRIVATE, FIRSTPRIVATE, LASTPRIVATE, or REDUCTION clause in a worksharing construct can also appear in a PRIVATE clause in an enclosed parallel or task construct.

NOTE:

Variables that are used as counters for explicit or implicit DO loops or FORALL commands, or common blocks that are specified to be THREADPRIVATE become automatically private to each thread, even though they are not explicitly included inside a PRIVATE clause at the beginning of the scope of the parallel region.

Example

Consider the following:

!$OMP PARALLEL PRIVATE(A, B)

In this simple case, each thread will have its own copy of variables A and B. The variables can have different values in each thread because the variables are local to the thread.