gpt4 book ai didi

c++ - OpenMP 中单个指令和部分指令之间的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:45:01 25 4
gpt4 key购买 nike

据我了解,我可以使用 single 指令完成与使用 sections 相同的工作,只需添加 nowait 标志

section 指令相比,以下代码对我来说没有什么不同:

    void main(){
#pragma omp parallel
{
int tid = omp_get_thread_num();
#pragma omp single nowait
{
printf("Thread %d in #1 single construct.\n", tid);
}
#pragma omp single nowait
{
printf("Thread %d in #2 single construct.\n", tid);
}
#pragma omp single nowait
{
printf("Thread %d in #3 single construct.\n", tid);
}
}
}

任何人都可以给我一些在不同场景中使用 sectionssingle 指令的例子吗?

最佳答案

首先,singlesections 指令在阅读代码时具有明显不同的语义目的,使用一个来模仿另一个可能会产生很大的误导。

关于技术细节,single 是唯一支持 copyprivate 子句的工作共享指令,它:

... provides a mechanism to use a private variable to broadcast a value from the data environment of one implicit task to the data environments of the other implicit tasks belonging to the parallel region.

另一方面,sections 工作共享结构支持 lastprivatereduction 子句,single 不支持。

最后,请注意您的代码段:

#pragma omp single nowait
{
printf("Thread %d in #1 single construct.\n", tid);
}
#pragma omp single nowait
{
printf("Thread %d in #2 single construct.\n", tid);
}
#pragma omp single nowait
{
printf("Thread %d in #3 single construct.\n", tid);
} // No barrier here

不模仿sections,而是模仿sections nowait。要模拟 sections,您必须记住让最后一个 single 构造维护其隐式屏障。

关于c++ - OpenMP 中单个指令和部分指令之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com