问题
I am trying to parallelize a C program using OpenMP.
I would like to know more about:
- The differences between the threadprivate directive and the private clause and
- In which cases we must use any of them.
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.
回答1:
The most important differences you have to memorize:
A
privatevariable 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
threadprivatevariable 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). Athreadprivatevariable 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
privateare 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 aprivatepointer is undefine.But data in the
threadprivatecommon blocks should be assumed to be undefined on entry to the first parallel region unless acopyinclause is specified. When a common block appears in athreadprivatedirective, 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
threadprivatedirective:Each copy of a
threadprivatevariable 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 athreadprivatevariable 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
threadprivatevariable is non-conforming.The content of a
threadprivatevariable 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
parallelregions, references by the master thread will be to the copy of the variable in the thread that encountered theparallelregion.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
threadprivatevariable are guaranteed to persist between any two consecutive references to the variable in the program.The values of data in the
threadprivatevariables of non-initial threads are guaranteed to persist between two consecutive activeparallelregions only if all the following conditions hold:Neither
parallelregion is nested inside another explicitparallelregion.The number of threads used to execute both
parallelregions is the same.The thread affinity policies used to execute both
parallelregions are the same.The value of the dyn-var internal control variable in the enclosing task region is false at entry to both
parallelregions.
If these conditions all hold, and if a
threadprivatevariable is referenced in both regions, then threads with the same thread number in their respective regions will reference the same copy of that variable.
来源:https://stackoverflow.com/questions/18022133/difference-between-openmp-threadprivate-and-private