0

我被一个看起来微不足道的问题所困扰(也许现在还为时过早)。从一个omp parallel区域内调用的函数中,我想返回一个值,该值 (1) 在每个线程上必须相同,但 (2) 在omp single块中计算。我怎样才能做到这一点?这是一个小代码片段:

// must work correctly when called from within omp parallel region
// this version is flawed. please suggest corrections
int func(vector<int>&args)
{
  int x;
#pragma omp single
  x = foo(args);    // foo() must be called in single block
                    // would need to broadcast x to all threads here
  return x;         // error: x only set for thread which executed foo()
}

// possible usage (elsewhere, you're not allowed to change this code)
#pragma omp parallel
{
   /* ... */
   int y = func(args);
   /* ... */
}

一个相当不优雅的解决方案是将single块转换为parallel forwith reduction

int func(types args)
{
  int x=0;
#pragma omp for reduction(+:x)
  for(int i=0; i<1; ++i)
    x = foo(args);
  return x;
}

但可以肯定的是,必须有更好的解决方案。

4

1 回答 1

2

解决方案非常简单 - 只需附加copyprivate(x)子句,它就是这样做的 - 它将构造中使用的列出的私有变量的实例的值广播single到其他线程:

int func(vector<int>&args)
{
  int x;
#pragma omp single copyprivate(x)
  x = foo(args);    // foo() must be called in single block
                    // would need to broadcast x to all threads here
  return x;
}
于 2013-03-08T12:56:38.787 回答