OpenMP threadprivate和private之间的区别 [英] Difference between OpenMP threadprivate and private
问题描述
我正在尝试使用OpenMP并行化C程序.
I am trying to parallelize a C program using OpenMP.
我想进一步了解:
- threadprivate 指令与 private 子句和 之间的区别
- 在这种情况下,我们必须使用其中任何一个.
- The differences between the threadprivate directive and the private clause and
- In which cases we must use any of them.
据我所知,区别在于具有 threadprivate 的全局范围和跨并行区域的保留值.我在几个示例中发现,当一段代码包含一些必须私有化的全局/静态变量时,这些变量将包含在 threadprivate 列表中,并且使用将它们的初始值复制到私有副本中>复制.
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.
但是,是否有任何规则可以阻止我们使用 private 子句来处理全局/静态变量?也许有任何实现细节?
However, is there any rule that prevents us to use the private clause to deal with global/static variables? perhaps any implementation detail?
我在OpenMP3.0规范中找不到任何解释.
I couldn't find any explanation in the OpenMP3.0 specification.
推荐答案
您必须记住的最重要的区别:
The most important differences you have to memorize:
-
private
变量在区域内是局部变量,大部分时间将放置在堆栈上.变量隐私的生存期是数据作用域定义的持续时间.每个线程(包括主线程)都对原始变量进行私有复制(新变量不再与原始变量在存储上相关联).
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:
-
在进入构造时,未为每个线程定义为
private
的变量,而在退出并行构造时,未定义相应的共享变量;private
指针的初始状态是不确定的.
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 aprivate
pointer is undefine.
但是,除非指定了copyin
子句,否则在进入第一个并行区域时,应该假定未定义threadprivate
公共块中的数据.当一个公共块出现在threadprivate
指令中时,每个线程副本在首次使用之前都会初始化一次.
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.
OpenMP规范(第2.14.2节)实际上对threadprivate
指令给出了很好的描述(并且也有更详细的描述):
The OpenMP Specifications (section 2.14.2) actually give a very good description (and also more detailled) of the threadprivate
directive:
threadprivate
变量的每个副本都按照程序指定的方式初始化一次,但是在第一次引用该副本之前,在程序中的未指定点进行了初始化.threadprivate
变量的所有副本的存储根据基本语言中静态变量的处理方式而释放,但在程序中未指定的位置.
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.
其中一个线程引用另一个线程的threadprivate
变量副本的程序是不合格的.
A program in which a thread references another thread’s copy of a threadprivate
variable is non-conforming.
如果执行线程切换到另一个修改变量的任务,则threadprivate
变量的内容可以在任务调度点之间变化.有关任务计划的更多详细信息,请参阅第14页的1.3节和第113页的2.11节.
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.
在parallel
区域中,主线程将引用遇到parallel
区域的线程中变量的副本.
In parallel
regions, references by the master thread will be to the copy of the variable in the thread that encountered the parallel
region.
在顺序零件期间,引用将指向初始线程的变量副本.保证threadprivate
变量的初始线程副本中的数据值可以保留在程序中对该变量的任何两个连续引用之间.
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.
只有在满足以下所有条件的情况下,才能保证非初始线程的threadprivate
变量中的数据值在两个连续的活动parallel
区域之间持久存在:
The values of data in the threadprivate
variables of non-initial threads are guaranteed to persist between two consecutive active parallel
regions only if all the following conditions hold:
-
parallel
区域均未嵌套在另一个显式的parallel
区域内.
Neither
parallel
region is nested inside another explicitparallel
region.
用于执行两个parallel
区域的线程数是相同的.
The number of threads used to execute both parallel
regions is the same.
用于执行两个parallel
区域的线程相似性策略是相同的.
The thread affinity policies used to execute both parallel
regions are the same.
封闭任务区域中的 dyn-var 内部控制变量的值在同时进入两个parallel
区域时为 false .
The value of the dyn-var internal control variable in the enclosing task region is false at entry to both parallel
regions.
如果这些条件都成立,并且在两个区域中均引用了threadprivate
变量,则在各自区域中具有相同线程号的线程将引用该变量的相同副本.
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.
这篇关于OpenMP threadprivate和private之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!