OpenMP中单个指令和section指令的区别
Difference between single and sections directive in OpenMP
我的理解是我可以使用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);
}
}
}
谁能给我一些例子使用sections
和single
指令在不同的场景?
首先,single
和sections
指令在阅读代码时具有明显不同的语义目的,使用一个指令来模仿另一个指令可能会产生很大的误导。
关于技术细节,single
是唯一支持copyprivate
子句的工作共享指令,它:
…提供使用私有变量的机制将一个隐式任务的数据环境中的值广播给的其他隐式任务的数据环境并行区域。
另一方面,sections
工作共享结构支持lastprivate
和reduction
子句,而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
构造保持其隐式屏障。
在某些情况下,single nowait
结构可能看起来与sections
结构的行为方式相同。然而,OpenMP规范只要求只有一个线程执行single
构造。它不要求空闲线程承担其他后续构造。您不能简单地依赖于所有OpenMP实现的这种行为。大多数会做你所期望的,但没有保证。
另一件值得一提的事情是,大多数实现使用票务系统将single
区域分配给线程。sections
通常的代码转换是通过将sections构造转换为for
循环并为section
构造使用switch
语句来映射到for
工作共享构造。因此,对于执行有更多的保证。
欢呼,迈克尔。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- int(c) 和 c-'0' 之间的区别。C++
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- C++:对不存在的命名空间使用命名空间指令
- C++ - "!pointer"和"pointer == nullptr"的区别?
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 返回递归调用和仅递归调用的区别
- 函数名是c中该函数的第一条指令的地址吗
- C++:包含指令和头文件有什么区别?
- 来自 C 的指令 #include 和来自 C++ 的指令之间有什么区别吗?
- C++中include指令和forward声明之间的区别
- OpenMP中单个指令和section指令的区别