OpenMP中的并行累积(前缀)总和:在线程之间传递值

时间:2013-09-10 12:41:09

标签: dependencies sum openmp

假设我有一个函数f(i),它取决于索引i(以及其他无法预先计算的值)。 我想填充数组a,以便a[n] = sum(f(i)) from i=0 to n-1

编辑:在Hristo Iliev的评论之后,我意识到我在做的是cumulative/prefix sum

这可以用代码写成

float sum = 0;
for(int i=0; i<N; i++) {
    sum += f(i);
    a[i] = sum;
}

现在我想使用OpenMP并行执行此操作。我使用OpenMP执行此操作的一种方法是并行写出f(i)的值,然后处理串行中的依赖项。如果f(i)是一个缓慢的函数,那么这可以很好地工作,因为非并行循环很简单。

#pragma omp parallel for
for(int i=0; i<N; i++) {
    a[i] = f(i);
}
for(int i=1; i<N; i++) {
    a[i] += a[i-1];
}

但是没有OpenMP的非并行循环可以做到这一点。然而,我提出的解决方案很复杂,也许是hackish。所以我的问题是,如果使用OpenMP有一个更简单,更简单的方法吗?

下面的代码基本上运行我为每个线程列出的第一个代码。结果是给定线程中a的值是正确的,直到常量。我将每个线程的总和保存到具有suma元素的数组nthreads+1。这允许我在线程之间进行通信并确定每个线程的常量偏移量。然后我用偏移校正a[i]的值。

float *suma;
#pragma omp parallel
{
    const int ithread = omp_get_thread_num();
    const int nthreads = omp_get_num_threads();
    const int start = ithread*N/nthreads;
    const int finish = (ithread+1)*N/nthreads;
    #pragma omp single
    {
        suma = new float[nthreads+1];
        suma[0] = 0;
    }
    float sum = 0;
    for (int i=start; i<finish; i++) {
        sum += f(i);
        a[i] = sum;
    }
    suma[ithread+1] = sum;
    #pragma omp barrier
    float offset = 0;
    for(int i=0; i<(ithread+1); i++) {
        offset += suma[i];
    }
    for(int i=start; i<finish; i++) {
        a[i] += offset;
    }
}
delete[] suma;

一个简单的测试就是设置f(i) = i。然后解决方案是a[i] = i*(i+1)/2(并且在无穷大时它是-1/12)。

2 个答案:

答案 0 :(得分:3)

您可以将策略扩展到任意数量的子区域,并使用任务递归地减少它们:

#include<vector>
#include<iostream>

using namespace std;

const int n          = 10000;
const int baseLength = 100;

int f(int ii) {
  return ii;
}

int recursiveSumBody(int * begin, int * end){

  size_t length  = end - begin;
  size_t mid     = length/2;
  int    sum     = 0;


  if ( length < baseLength ) {
    for(size_t ii = 1; ii < length; ii++ ){
        begin[ii] += begin[ii-1];
    }
  } else {
#pragma omp task shared(sum)
    {
      sum = recursiveSumBody(begin    ,begin+mid);
    }
#pragma omp task
    {
      recursiveSumBody(begin+mid,end      );
    }
#pragma omp taskwait

#pragma omp parallel for
    for(size_t ii = mid; ii < length; ii++) {
      begin[ii] += sum;
    }

  }
  return begin[length-1];
}

void recursiveSum(int * begin, int * end){

#pragma omp single
  {
    recursiveSumBody(begin,end);
  }    
}


int main() {

  vector<int> a(n,0);

#pragma omp parallel
  {
    #pragma omp for
    for(int ii=0; ii < n; ii++) {          
      a[ii] = f(ii);
    }  

    recursiveSum(&a[0],&a[n]);

  }
  cout << n*(n-1)/2 << endl;
  cout << a[n-1] << endl;

  return 0;
}

答案 1 :(得分:0)

为了完整起见,我在考虑到 Hristo 的评论时添加了 OP 的 MWE 代码:

#include <iostream>
#include <omp.h>
using std::cout;
using std::endl;

const int N = 10;
const int Nthr = 4;
float f(int i) {return (float)i;}

int main(void) {
    omp_set_num_threads(Nthr);
    float* a = new float[N];
    float *suma = new float[Nthr+1];
    suma[0] = 0.0;
    float sum = 0.0;
#pragma omp parallel for schedule(static) firstprivate(sum)
    for (int i=0; i<N; i++) {
        sum += f(i);
        a[i] = sum;
        suma[omp_get_thread_num()+1] = sum;
    }

    // this for-loop is also a commulative sum, but it has only Nthr iterations
    for (int i=1; i<Nthr;i++)
        suma[i] += suma[i-1];

#pragma omp parallel for schedule(static)
    for(int i=0; i< N; i++) {
        a[i] += suma[omp_get_thread_num()];
    }

    for (int i=0; i<N; i++) {
        cout << a[i] << endl;
    }

    delete[] suma;
    int n = 95;
    cout << a[n] << endl << n*(n+1)/2 << endl;
    delete[] a;
    return 0;
}