使用OpenMP减少 [英] Reduction with OpenMP
问题描述
我正在尝试使用openmp计算2d矩阵的均值.这个二维矩阵实际上是一幅图像.
I am trying to compute mean of a 2d matrix using openmp. This 2d matrix is actually an image.
我正在对数据进行线程分割.例如,如果我有N
个线程,则用thread0
处理Rows/N
行数,依此类推.
I am doing the thread-wise division of data. For example, if I have N
threads than I process Rows/N
number of rows with thread0
, and so on.
我的问题是:我可以在"#pragma omp parallel
"中使用openmp减少子句吗?
My question is: Can I use the openmp reduction clause with "#pragma omp parallel
"?
#pragma omp parallel reduction( + : sum )
{
if( thread == 0 )
bla bla code
sum = sum + val;
else if( thread == 1 )
bla bla code
sum = sum + val;
}
推荐答案
是的,您可以-reduce子句适用于整个并行区域以及单个for
工作共享结构.这允许例如减少在不同并行部分中完成的计算(重组代码的首选方法):
Yes, you can - the reduction clause is applicable to the whole parallel region as well as to individual for
worksharing constructs. This allows for e.g. reduction over computations done in different parallel sections (the preferred way to restructure the code):
#pragma omp parallel sections private(val) reduction(+:sum)
{
#pragma omp section
{
bla bla code
sum += val;
}
#pragma omp section
{
bla bla code
sum += val;
}
}
您还可以使用OpenMP for
工作共享结构自动在团队中的线程之间分配循环迭代,而无需使用以下部分重新实现它:
You can also use the OpenMP for
worksharing construct to automatically distribute the loop iterations among the threads in the team instead of reimplementing it using sections:
#pragma omp parallel for private(val) reduction(+:sum)
for (row = 0; row < Rows; row++)
{
bla bla code
sum += val;
}
请注意,归约变量是私有变量,它们的中间值(即在parallel
区域结尾处归约前它们所拥有的值)只是部分的,不是很有用.例如,以下串行循环无法(通过简化操作)转换为并行循环:
Note that reduction variables are private and their intermediate values (i.e. the value they hold before the reduction at the end of the parallel
region) are only partial and not very useful. For example the following serial loop cannot be (easily?) transformed to a parallel one with reduction operation:
for (row = 0; row < Rows; row++)
{
bla bla code
sum += val;
if (sum > threshold)
yada yada code
}
在这里,一旦sum
的累加值超过threshold
的值,就应在每次迭代中执行yada yada code
.当循环并行运行时,sum
的私有值可能永远不会达到threshold
,即使它们的总和也是如此.
Here the yada yada code
should be executed in each iteration once the accumulated value of sum
has passed the value of threshold
. When the loop is run in parallel, the private values of sum
might never reach threshold
, even if their sum does.
这篇关于使用OpenMP减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!