c/c++语言开发共享OpenMP中的嵌套循环

我需要运行一个短的外循环和一个长的内循环。 我想将后者并行化,而不是前者。 原因是在内部循环运行后有一个更新的数组。 我正在使用的代码如下

#pragma omp parallel{ for(j=0;j<3;j++){ s=0; #pragma omp for reduction(+:s) for(i=0;i<10000;i++) s+=1; A[j]=s; } } 

这实际上是挂起的。 以下工作正常,但我宁愿避免开始一个新的并行区域的开销,因为这之前是另一个。

 for(j=0;j<3;j++){ s=0; #pragma omp parallel for reduction(+:s) for(i=0;i<10000;i++) s+=1; A[j]=s; } 

这样做的正确(和最快)方式是什么?

    以下示例应按预期工作:

     #include using namespace std; int main(){ int s; int A[3]; #pragma omp parallel { // Note that I moved the curly bracket for(int j = 0; j < 3; j++) { #pragma omp single s = 0; #pragma omp for reduction(+:s) for(int i=0;i<10000;i++) { s+=1; } // Implicit barrier here #pragma omp single A[j]=s; // This statement needs synchronization } // End of the outer for loop } // End of the parallel region for (int jj = 0; jj < 3; jj++) cout << A[jj] << endl; return 0; } 

    编译和执行的一个例子是:

     > g++ --version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. > g++ -fopenmp -Wall main.cpp > export OMP_NUM_THREADS=169 > ./a.out 10000 10000 10000 

      以上就是c/c++开发分享OpenMP中的嵌套循环相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519502.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐