我被一个看起来微不足道的问题所困扰(也许现在还为时过早)。从一个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 for
with 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;
}
但可以肯定的是,必须有更好的解决方案。