Reputation: 4809
I've come across an unusual problem in trying to make my C/C++ project support both gcc-8 (with OpenMP 4.5) and gcc-9 (with OpenMP 5.0). It is caused by variables defined as const
which are to be shared between threads.
For example, here's some code compatible with OpenMP 5, but fails with OpenMP 4.5 (with error 'x' is predetermined 'shared' for 'shared'
):
const x = 10;
int i;
# pragma omp parallel \
default (none) \
shared (x) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}
The above turns out also to be compatible with clang-10
, though not clang-3.7
.
Here's the same code (just excluding x
from shared
) compatible with OpenMP 4.5, but fails with OpenMP 5 (with error error: 'numTasks' not specified in enclosing 'parallel'
):
const x = 10;
int i;
# pragma omp parallel \
default (none) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}
It seems like OpenMP 5 no longer intelligently assumes that const
variables are shared. The only code which can compile on both is one which needlessly puts x
in firstprivate
:
const x = 10;
int i;
# pragma omp parallel \
default (none) \
firstprivate (x) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}
This seems a poor solution, since now I'm paying to copy x
into every thread when it's never going to be modified! Note also I cannot make x
non-const
, since it's actually a const
argument to a calling function.
What's the deal, and what's the correct/efficient way to make agnostic code?
Upvotes: 3
Views: 472
Reputation: 4809
Porting to gcc-9
is addressed here. To maintain compatability with both versions of OpenMP, I'll have to either:
default (none)
const
Upvotes: 3