如何广播OpenMP单块的结果
How to broadcast result from OpenMP single block
我被一个看起来微不足道的问题困住了(也许现在还为时过早)。从在omp parallel
区域内调用的函数中,我想返回一个值(1)必须在每个线程上相同,但(2)在omp single
块中计算。我该怎么做呢?下面是一小段代码:
// must work correctly when called from within omp parallel region
// this version is flawed. please suggest corrections
int func(vector<int>&args)
{
int x;
#pragma omp single
x = foo(args); // foo() must be called in single block
// would need to broadcast x to all threads here
return x; // error: x only set for thread which executed foo()
}
// possible usage (elsewhere, you're not allowed to change this code)
#pragma omp parallel
{
/* ... */
int y = func(args);
/* ... */
}
一个相当不优雅的解决方案是用reduction
:
single
块转换为parallel for
块。int func(types args)
{
int x=0;
#pragma omp for reduction(+:x)
for(int i=0; i<1; ++i)
x = foo(args);
return x;
}
但肯定有更好的解决办法。
解决方案非常简单—只需附加copyprivate(x)
子句,它确实做到了这一点—它将single
构造中列出的私有变量的实例的值广播给其他线程:
int func(vector<int>&args)
{
int x;
#pragma omp single copyprivate(x)
x = foo(args); // foo() must be called in single block
// would need to broadcast x to all threads here
return x;
}
相关文章:
- C++中高效的大型稀疏块压缩线性方程
- 孤立代码块在结构中引发异常
- OpenMP阵列性能较差
- QTreeView幻灯片多选后无法使用单击选择
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何使用宏中宏后面的代码块
- OpenMP:并行更新数组总是需要减少数组吗
- llvm构建器向基本块添加终止符
- 并行块(线程清理器)之外的 OpenMP 中的争用条件;误报?
- openmp c++ 中并行块内 lambda 函数的奇怪行为
- _CrtIsValidHeapPointer(块)错误在应用单例-帕特恩后退出主窗口时引发
- 在 OpenMP 中使用并行块时没有看到任何显着改进C++
- 避免在 openmp 并行块中使用预编译器语句出现未使用的变量警告
- OpenMP程序挂在块的末尾
- 代码::块调试:不要单步执行头文件
- C++ OpenMP:将 for 循环拆分为静态偶数块,并在最后连接数据
- 可以是OpenMP中的静态调度循环块
- 如何广播OpenMP单块的结果
- 你能用 c 或 c++ 分配一个非常大的单块内存(> 4GB)吗?