OpenMP Tutorial: https://computing.llnl.gov/tutorials/openMP/#Clauses
(1) summary:
In OpenMP commands, there are a series of clauses, most of which are related to data attributes and are also an important part of commands. It is called "data Scope attribute clauses", or "data-sharing attribute clauses". Understanding how to use data attributes is an important part of OpenMP learning and understanding. Since OpenMP is based on Memory Sharing, many data is shared by default. The clauses of these data sharing attributes are usually used in specific commands, such as parallel, do/for, and sections.
(2) Private clause:
private (list)
The private clause specifies that the variables in the list are private to each thread.
Note: features of the private clause,
For each thread in the team, a new object of the same type is declared;
All references to the original object are replaced by references of the new object;
Variables declared as private are considered not initialized for each thread.
The following is a comparison between private and threadprivate:
Private |
Threadprivate |
Data item |
C/C ++: Variable FORTRAN: variable or common block |
C/C ++: Variable FORTRAN: Common Block |
Where declared |
At start of region or work-sharing Group |
In declarations of each routine using block or Global File Scope |
Persistent? |
No |
Yes |
Extent |
Lexical only-unless passed as an argument to subroutine |
Dynamic |
Initialized |
Use firstprivate |
Use copyin |
(3) shared clause
shared (list)
The shared clause declares the variables in the list as shared by all threads in the team.
Note: features of shared clauses,
A shared variable only exists in one memory address, and all threads can read or write its address;
Developers must ensure that the shared variables are accessed correctly (through synchronization, such as critical ).
(4) default clause
default (shared | none)
The default clause specifies the default scope of all variables in the parallel region.
Note: features of the default clause,
You can use other clauses such as shared/private to overwrite the default settings;
The C/C ++ standard does not specify private as default (supported by Fortran). Of course, the compiler can implement it on its own;
If none is specified as the default value, all variables in the parallel block must explicitly define the data range attribute.
In addition, only one default value can be specified for a parallel Parallel Block.
(5) firstprivate clause
firstprivate (list)
The firstprivate clause combines the private clause with the automatic initialization function.
(6) lastprivate clause
lastprivate (list)
The lastprivate clause combines the functions of the private clause and copies the values of the last iteration or section.
(7) copyin clause
copyin (list)
The copyin clause is used to assign a uniform value to the variables of threadprivate commands of all threads of a team.
Note: The Source variable value of copyin is the value of the master thread. When a thread enters a parallel block, it uses its value for initialization.
(8) copyprivate clause
copyprivate (list)
Used for single commands.
(9) limit clause
reduction (operator: list)
The function clause performs the function operation on the specified variable. For multiple threads, each thread will save a copy of The invoke type variable. After the loop iteration or parallel iteration ends, it will perform the operator stacked operation on the replica variables of all threads, get the final result. Operator is a valid operator, for example, +.
Private |
Threadprivate |
Data item |
C/C ++: Variable FORTRAN: variable or common block |
C/C ++: Variable FORTRAN: Common Block |
Where declared |
At start of region or work-sharing Group |
In declarations of each routine using block or Global File Scope |
Persistent? |
No |
Yes |
Extent |
Lexical only-unless passed as an argument to subroutine |
Dynamic |
Initialized |
Use firstprivate |
Use copyin |