为什么我应该使用归约变量而不是原子变量? [英] Why should I use a reduction rather than an atomic variable?

查看:90
本文介绍了为什么我应该使用归约变量而不是原子变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们要在OpenMP循环中计数一些东西.比较减少量

  int计数器= 0;#pragma omp进行还原(+:counter)为了 (...) {...计数器++;} 

具有原子增量

  int计数器= 0;#pragma omp for为了 (...) {...#pragma omp atomic计数器++} 

原子访问立即提供结果,而归约仅在循环结束时假定其正确值.例如,减少不允许这样做:

  int t =计数器;如果(t%1000 == 0){printf(%dk次迭代\ n",t/1000);} 

因此提供的功能较少.

为什么我会使用减少而不是原子访问计数器?

解决方案

简短答案:

性能

长答案:

因为原子变量带有价格,所以这个价格是同步的.为了确保没有竞争条件,即两个线程同时修改同一变量,线程必须同步,这实际上意味着您失去了并行性,即线程被序列化.

另一方面,

归约是可以使用并行归约算法并行并行执行的常规操作.阅读

Assume we want to count something in an OpenMP loop. Compare the reduction

int counter = 0;
#pragma omp for reduction( + : counter )
for (...) {
    ...
    counter++;
}

with the atomic increment

int counter = 0;
#pragma omp for
for (...) {
    ...
    #pragma omp atomic
    counter++
}

The atomic access provides the result immediately, while a reduction only assumes its correct value at the end of the loop. For instance, reductions do not allow this:

int t = counter;
if (t % 1000 == 0) {
    printf ("%dk iterations\n", t/1000);
}

thus providing less functionality.

Why would I ever use a reduction instead of atomic access to a counter?

解决方案

Short answer:

Performance

Long Answer:

Because an atomic variable comes with a price, and this price is synchronization. In order to ensure that there is no race conditions i.e. two threads modifying the same variable at the same moment, threads must synchronize which effectively means that you lose parallelism, i.e. threads are serialized.

Reduction on the other hand is a general operation that can be carried out in parallel using parallel reduction algorithms. Read this and this articles for more info about parallel reduction algorithms.


Addendum: Getting a sense of how a parallel reduction work

Imagine a scenario where you have 4 threads and you want to reduce a 8 element array A. What you could do this in 3 steps (check the attached image to get a better sense of what I am talking about):

  • Step 0. Threads with index i<4 take care of the result of summing A[i]=A[i]+A[i+4].
  • Step 1. Threads with index i<2 take care of the result of summing A[i]=A[i]+A[i+4/2].
  • Step 2. Threads with index i<4/4 take care of the result of summing A[i]=A[i]+A[i+4/4]

At the end of this process you will have the result of your reduction in the first element of A i.e. A[0]

这篇关于为什么我应该使用归约变量而不是原子变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆