我正在尝试使用OpenMP并行化一个C程序。
我想知道更多关于以下方面的情况:
据我所知,区别在于threadprivate的全局范围和跨并行区域的保留值。我在几个例子中发现,当一段代码包含一些必须私有化的全局/静态变量时,这些变量包含在一个threadprivate列表中,并且它们的初始值被使用版权复制到私有副本中。
但是,是否有任何规则阻止我们使用私有子句来处理全局/静态变量?可能有任何实现细节吗?
我在OpenMP3.0规范中找不到任何解释。
发布于 2013-08-04 16:46:23
你必须记住的最重要的区别是:
private变量是区域的本地变量,大多数情况下将放在堆栈上。变量隐私的生存期是数据作用域子句定义的持续时间。每个线程(包括主线程)都生成原始变量的私有副本(新变量不再是与原始变量相关联的存储)。threadprivate变量很可能放在堆或线程局部存储器中(可以看作是线程本地的全局内存)。一个threadprivate变量在各个区域中持续存在(取决于某些限制)。主线程使用原始变量,所有其他线程都生成原始变量的私有副本(主变量仍然是存储的--与原始变量关联)。- 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.
threadprivate指令进行了非常好的描述(也更不夸张):
threadprivate变量的每个副本都以程序指定的方式初始化一次,但在程序中第一次引用该副本之前的一个未指定的点进行初始化。threadprivate变量的所有副本的存储根据静态变量在基本语言中的处理方式而被释放,但是在程序中的一个未指定的点。
线程引用另一个线程的threadprivate变量副本的程序是不符合的.
如果正在执行的线程切换到另一个修改变量的任务,则threadprivate变量的内容可以跨任务调度点进行更改。有关任务调度的详细信息,请参阅第14页的1.3节和第113页的2.11节。
在parallel区域中,主线程的引用将指向遇到parallel区域的线程中变量的副本。
在连续的部分中,引用将指向变量的初始线程副本。threadprivate变量的初始线程副本中的数据值保证在程序中对变量的任意两个连续引用之间持久化。
只有在以下所有条件都保持不变的情况下,才能保证非初始线程的threadprivate变量中的数据值在两个连续的active parallel区域之间保持不变:- Neither `parallel` region is nested inside another explicit `parallel` 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.如果这些条件都成立,并且如果两个区域都引用了一个threadprivate变量,那么在各自区域中具有相同线程号的线程将引用该变量的相同副本。
https://stackoverflow.com/questions/18022133
复制相似问题