OpenMP任务FirstPrivate
我有一个有关OpenMP任务Pragma的问题,如果我们假设以下代码:
#pragma omp parallel
{
x = omp_get_thread_num();
#pragma omp task firstprivate(x)
//do something with x
}
据我了解,无法保证,线程执行任务。 所以我的问题是,任务中的“ x”现在是线程生成任务的ID还是执行任务的一个?
例如,如果线程0遇到任务,并且线程3执行它:X应该为0,那么,对吗?
I have a question regarding the OpenMP task pragma, if we suppose the following code:
#pragma omp parallel
{
x = omp_get_thread_num();
#pragma omp task firstprivate(x)
//do something with x
}
as far as I understood tasking, it is not guaranteed, which thread executes the task.
So my question is, is "x" in the task now the id of thread generated the task or the one who executes it?
e.g. if thread 0 comes across the task, and thread 3 executes it: x should be 0 then, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于,如果并行默认数据共享属性是共享(默认情况下通常是):
这可以用以下代码显示:
因此,带有
id = 1
的线程创建任务,但是,'x'的值与'1'的值不同,并且与当前线程的值不同。执行任务。这是因为当带有id = 1
的线程在睡眠(5);
期间等待时,团队中的其余线程可以更新'x'的值。通常,此类用例中的规范形式是使用
单个
pragma围绕任务创建的包裹如下:在这种情况下,如@michael Klemm所述:
。 。,x将包含创建任务的线程的ID。因此,是的,如果线程0创建了任务,即使选择了线程3来执行任务
。 > x = op_get_thread_num(); 发生。
因此,如果您运行上面的代码,则应始终获得
我是任务创建者
,其值与x = 的值相同,但是您可以在<<
中获得不同的值代码> ID线程执行。例如:这是按照OpenMP标准中指定的行为的一致:即:
It depends, if the parallel default data-sharing attribute is shared (which by default typically it is) then:
This can be show-cased with the following code:
So the thread with
ID=1
creates the task, however, 'x' can have different values than '1' and also different values than the thread currently executing the task. This is because while the thread withID=1
, is waiting duringsleep(5);
, the remaining threads in the team can update the value of 'x'.Typically, the canonical form in such use-cases would be to use a
single
pragma wrapping around the task creation as follows:And in this case as @Michael Klemm mentioned on the comments:
..., x will contain the ID of the thread that created the task. So, yes, if thread 0 created the task, x will be zero even though thread 3 is picked to execute the task.
This also applies in the cases that variable 'x' is private by the time the statement
x = omp_get_thread_num();
happens.Therefore, if you run the code above you should always get
I am the task creator
with the same value asValue of x =
, but you can get a different value inID Thread executing
. For example:This is in accordance to the behaviour specified in the OpenMP standard, namely: