Use the PRIVATE clause on the PARALLEL, DO, SECTIONS, SINGLE, PARALLEL DO, and PARALLEL SECTIONS directives to declare variables to be private to each thread in the team.
The behavior of variables declared PRIVATE is as follows:
A new object of the same type and size is declared once for each thread in the team, and 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 on entering the construct, and the corresponding shared variable is undefined on exit from a parallel construct.
Contents, allocation state, and association status of variables defined as PRIVATE are undefined when they are referenced outside the lexical extent, but inside the dynamic extent, of the construct unless they are passed as actual arguments to called routines.
In the following example, the values of I and J are undefined on exit from the parallel region:
!$OMP PARALLEL PRIVATE(I) FIRSTPRIVATE(J)
J =J+ 2
!$OMP END PARALLEL
PRINT *, I, J
Use the FIRSTPRIVATE clause on the PARALLEL , DO, SECTIONS, SINGLE, PARALLEL DO, and PARALLEL SECTIONS directives to provide a superset of the PRIVATE clause functionality.
In addition to the PRIVATE clause functionality, private copies of the variables are initialized from the original object existing before the parallel construct.
Use the LASTPRIVATE clause on the DO, SECTIONS, PARALLEL DO, and PARALLEL SECTIONS directives to provide a superset of the PRIVATE clause functionality.
When the LASTPRIVATE clause appears on a DO or PARALLEL DO directive, the thread that executes the sequentially last iteration updates the version of the object it had before the construct.
When the LASTPRIVATE clause appears on a SECTIONS or PARALLEL SECTIONS directive, the thread that executes the lexically last section updates the version of the object it had before the construct.
Subobjects that are not assigned a value by the last iteration of the DO loop or the lexically last SECTION directive are undefined after the construct.
Correct execution sometimes depends on the value that the last iteration of a loop assigns to a variable. You must list all such variables as arguments to a LASTPRIVATE clause so that the values of the variables are the same as when the loop is executed sequentially. As shown in the following example, the value of I at the end of the parallel region is equal to N+1, as it would be with sequential execution.
!$OMP DO LASTPRIVATE(I)
A(I) = B(I) + C(I)
!$OMP END PARALLEL