替换#pragma opp关键(C )

replacement for #pragma omp critical (C++)

本文关键字:关键 #pragma opp 替换      更新时间:2023-10-16

我在嵌套环上使用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子句。

,您都不应该