问题描述
我已经看过正式的定义,但是我仍然很困惑.
I've looked at the official definitions, but I'm still quite confused.
对我来说,这听起来很像私人的.我在寻找示例,但是我似乎不明白它的特殊性或用法.
To me, that sounds a lot like private. I've looked for examples, but I don't seem to understand how it's special or how it can be used.
由于以下示例,我觉得我对这一点了解得更多:
I feel like I understand this one a bit better because of the following example:
#pragma omp parallel
{
#pragma omp for lastprivate(i)
for (i=0; i<n-1; i++)
a[i] = b[i] + b[i+1];
}
a[i]=b[i];
因此,在此示例中,我了解到lastprivate
允许i
作为循环的最后一个值被返回到循环之外.
So, in this example, I understand that lastprivate
allows for i
to be returned outside of the loop as the last value it was.
我今天才开始学习OpenMP.
I just started learning OpenMP today.
推荐答案
private
变量未初始化,即,它们以与任何其他本地自动变量一样的随机值开头(并且通常使用自动变量在堆栈上实现每个线程).以这个简单的程序为例:
private
variables are not initialised, i.e. they start with random values like any other local automatic variable (and they are often implemented using automatic variables on the stack of each thread). Take this simple program as an example:
#include <stdio.h>
#include <omp.h>
int main (void)
{
int i = 10;
#pragma omp parallel private(i)
{
printf("thread %d: i = %d\n", omp_get_thread_num(), i);
i = 1000 + omp_get_thread_num();
}
printf("i = %d\n", i);
return 0;
}
具有四个线程,它输出如下内容:
With four threads it outputs something like:
thread 0: i = 0
thread 3: i = 32717
thread 1: i = 32717
thread 2: i = 1
i = 10
(another run of the same program)
thread 2: i = 1
thread 1: i = 1
thread 0: i = 0
thread 3: i = 32657
i = 10
这清楚地表明了i
的值在并行区域内是随机的(未初始化),并且在并行区域之后对它的任何修改都是不可见的(即,变量保持其值不进入该区域之前).
This clearly demonstrates that the value of i
is random (not initialised) inside the parallel region and that any modifications to it are not visible after the parallel region (i.e. the variable keeps its value from before entering the region).
如果将i
设置为firstprivate
,则使用在并行区域之前的值对其进行初始化:
If i
is made firstprivate
, then it is initialised with the value that it has before the parallel region:
thread 2: i = 10
thread 0: i = 10
thread 3: i = 10
thread 1: i = 10
i = 10
在平行区域内对i
的值进行的修改仍然不可见.
Still modifications to the value of i
inside the parallel region are not visible after it.
您已经了解lastprivate
(并且它不适用于简单的演示程序,因为它缺少工作共享结构).
You already know about lastprivate
(and it is not applicable to the simple demonstration program as it lacks worksharing constructs).
所以是的,firstprivate
和lastprivate
只是private
的特例.第一个结果将值从外部上下文引入并行区域,而第二个结果将值从并行区域传递到外部上下文.这些数据共享类背后的基本原理是,在并行区域内,所有私有变量都在外部上下文中对其进行了阴影处理,即,无法使用赋值操作从并行区域内部修改i
的外部值.
So yes, firstprivate
and lastprivate
are just special cases of private
. The first one results in bringing in values from the outside context into the parallel region while the second one transfers values from the parallel region to the outside context. The rationale behind these data-sharing classes is that inside the parallel region all private variables shadow the ones from the outside context, i.e. it is not possible to use an assignment operation to modify the outside value of i
from inside the parallel region.
这篇关于firstprivate和lastprivate与OpenMP中的private子句有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!