I am trying to parallelize a C program using OpenMP.
I would like to know more about:
As far as I know, the difference is the global scope with threadprivate and the preserved value across parallel regions. I found in several examples that when a piece of code contains some global/static variables that must be privatized, these variables are included in a threadprivate list and their initial values are copied into the private copies using copyin.
However, is there any rule that prevents us to use the private clause to deal with global/static variables? perhaps any implementation detail?
I couldn't find any explanation in the OpenMP3.0 specification.
A program in which a thread references another thread's copy of a threadprivate variable is non-conforming. The content of a threadprivate variable can change across a task scheduling point if the executing thread switches to another task that modifies the variable.
A variable in an OpenMP parallel region can be either shared or private. If a variable is shared, then there exists one instance of this variable which is shared among all threads. If a variable is private, then each thread in a team of threads has its own local copy of the private variable.
The THREADPRIVATE directive allows you to specify named common blocks and named variables as private to a thread but global within that thread. Once you declare a common block or variable THREADPRIVATE, each thread in the team maintains a separate copy of that common block or variable.
The difference between threadprivate and private is the global scope associated with threadprivate and the preserved value across parallel regions. copyin: similar to firstprivate for private variables, threadprivate variables are not initialized, unless using copyin to pass the value from the corresponding global variables.
OpenMP Directives: THREADPRIVATE Directive 1 Purpose:. The THREADPRIVATE directive is used to make global file scope variables (C/C++) or common blocks (Fortran)... 2 Format:. 3 Notes:. The directive must appear after the declaration of listed variables/common blocks. Each thread then gets its own... 4 Example:. More ...
The most important differences you have to memorize: A private variable is local to a region and will most of the time be placed on the stack. A threadprivate variable on the other hand will be most likely placed in the heap or in the thread local storage (that can be seen as a global memory local to a thread).
Since OpenMP is a shared memory programming model, most variables in OpenMP code are visible to all threads by default.
The most important differences you have to memorize:
A private
variable is local to a region and will most of the time be placed on the stack. The lifetime of the variable's privacy is the duration defined of the data scoping clause. Every thread (including the master thread) makes a private copy of the original variable (the new variable is no longer storage-associated with the original variable).
A threadprivate
variable on the other hand will be most likely placed in the heap or in the thread local storage (that can be seen as a global memory local to a thread). A threadprivate
variable persist across regions (depending on some restrictions). The master thread uses the original variable, all other threads make a private copy of the original variable (the master variable is still storage-associated with the original variable).
There are also more tricky differences:
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; the initial status of a private
pointer is undefine.
But data in the threadprivate
common blocks should be assumed to be undefined on entry to the first parallel region unless a copyin
clause is specified. When a common block appears in a threadprivate
directive, each thread copy is initialized once prior to its first use.
The OpenMP Specifications (section 2.14.2) actually give a very good description (and also more detailled) of the threadprivate
directive:
Each copy of a
threadprivate
variable is initialized once, in the manner specified by the program, but at an unspecified point in the program prior to the first reference to that copy. The storage of all copies of athreadprivate
variable is freed according to how static variables are handled in the base language, but at an unspecified point in the program.A program in which a thread references another thread’s copy of a
threadprivate
variable is non-conforming.The content of a
threadprivate
variable can change across a task scheduling point if the executing thread switches to another task that modifies the variable. For more details on task scheduling, see Section 1.3 on page 14 and Section 2.11 on page 113.In
parallel
regions, references by the master thread will be to the copy of the variable in the thread that encountered theparallel
region.During a sequential part references will be to the initial thread’s copy of the variable. The values of data in the initial thread’s copy of a
threadprivate
variable are guaranteed to persist between any two consecutive references to the variable in the program.The values of data in the
threadprivate
variables of non-initial threads are guaranteed to persist between two consecutive activeparallel
regions only if all the following conditions hold:
Neither
parallel
region is nested inside another explicitparallel
region.The number of threads used to execute both
parallel
regions is the same.The thread affinity policies used to execute both
parallel
regions are the same.The value of the dyn-var internal control variable in the enclosing task region is false at entry to both
parallel
regions.If these conditions all hold, and if a
threadprivate
variable is referenced in both regions, then threads with the same thread number in their respective regions will reference the same copy of that variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With