替换#pragma opp关键(C )
replacement for #pragma omp critical (C++)
我在嵌套环上使用openMP,该循环的工作原理
#pragma omp parallel shared(vector1) private(i,j)
{
#pragma omp for schedule(dynamic)
for (i = 0; i < vector1.size(); ++i){
//some code here
for (j = 0; j < vector1.size(); ++j){
//some other code goes here
#pragma omp critical
A+=B;
}
C +=A;
}
}
这里的问题是我的代码在代码的A+=B
部分中进行了许多计算。因此,通过使其批评,我无法实现我想要的加速。(实际上,似乎有一些开销,因为我的程序需要更长的执行时间,然后被依次编写)。
我尝试使用
#pragma omp reduction private(B) reduction(+:A)
A+=B
这加快了执行时间的速度,但是似乎没有照顾像critical
条款这样的种族条件,因为我没有得到A。
我可以尝试吗?
,除非您想经历使Vector3
类线程安全或重写操作的麻烦,供与std::atomic<Vector3>
一起使用,这两个仍然会遭受性能缺陷(尽管不像认真地使用关键部分),您实际上可以模仿OpenMP降低的行为:
#pragma omp parallel // no need to declare variables declared outside/inside as shared/private
{
Vector3 A{}, LocalC{}; // both thread-private
#pragma omp for schedule(dynamic)
for (i = 0; i < vector1.size(); ++i){
//some code here
for (j = 0; j < vector1.size(); ++j){
//some other code goes here
A += B; // does not need a barrier
}
LocalC += A; // does not need a barrier
}
#pragma omp critical
C += LocalC;
}
nb认为您不访问"某些代码"注释中阅读A
,但是如果您想过使用reduction
子句。
相关文章:
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- #pragma 包(1)会导致分段错误
- OpenMP 与有序和关键指令并行
- 禁止显示有关包含文件中 #pragma 包的警告
- 基本 在C++生成器中输入关键功能
- 编译时检查 #pragma 包的使用情况
- OpenMP:for 循环避免数据竞争,而无需使用关键
- OpenMP #pragma omp for v/s #pragma omp parallel for 之间的区别?
- #pragma(*诊断)当将Clang分析器与GCC编译器混合时
- #pragma 警告不适用于 catch 语句
- 如何在 DLL 内的成员函数中使用互斥/关键节
- 如何在OpenGL中使用关键事件来转换对象
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 如何检查特定流程是否为关键流程
- 并发问题:如何只有一个线程通过关键部分
- TBB spin_mutex parallel_for内部以阻止关键部分
- 在安全关键系统中测试 C++17
- "std::unordered_map",无需复制关键数据
- 为什么"#pragma omp 关键"部分不能在同一行上具有左大括号?
- 替换#pragma opp关键(C )