Reputation: 53
My problem arises from a combination of a few particular things.
default(none)
(because I'm paranoid). Essentially, I have something of the form
#pragma omp parallel \
default(none) \
shared(...) \
private(...)
{
#pragma omp for
for (i = 0; i < num_i; ++i) {
compute_stuff;
#if FLAG_1
compute_more_stuff;
#endif
}
}
Suppose for clarity that variable x
is only required if FLAG_1
is true
. I could wrap the declaration for x
and its usage within #if FLAG1 ... #endif
statements, but I still need to list x
within the variable list for the #pragma omp parallel
, and, as far as I'm aware, I can't nest a #if FLAG1 ... #endif
within the #pragma omp parallel
statement (it's several lines long - there are a lot of variables). So, I either I get errors about non-existent variables listing in the pragma omp, or warnings about an unused variable.
In this case the removed variables are all omp-private, and I acknowledge up front that simply replacing the default(none)
with default(private)
would resolve the problem. That said, I do like the coding practice of default(none)
, and would like to keep it if possible.
Another option would be to simply break up the omp-parallel into something like the following, but compute_stuff
and compute_more_stuff
have some shared computations / memory access that I'd like the avoid duplicating.
#pragma omp parallel \
default(none) \
shared(...) \
private(...)
{
#pragma omp for
for (i = 0; i < num_i; ++i) {
compute_stuff;
}
}
#if FLAG_1
#pragma omp parallel \
default(none) \
shared(...) \
private(...)
{
#pragma omp for
for (i = 0; i < num_i; ++i) {
compute_more_stuff;
}
}
#endif
Any thoughts on how to maintain good coding practices while keep readable and efficient code would be greatly appreciated!
Upvotes: 0
Views: 296
Reputation: 4050
If you are using C++17, What's about the [[maybe_unused]]
attribute?:
#pragma omp parallel \
default(none) \
shared(...) \
private(...)
[[maybe_unused]] variable_potencially_not_used;
{
#pragma omp for
for (i = 0; i < num_i; ++i) {
compute_stuff;
#if FLAG_1
variable_potencially_not_used = 1;
#endif
}
}
If not, and alternative is to implement something similat to the Q_UNUSED
macro. You can declare your own:
#define MAYBE_UNUSED(X) (void)X
#pragma omp parallel \
default(none) \
shared(...) \
private(...)
MAYBE_UNUSED(variable_potencially_not_used);
{
#pragma omp for
for (i = 0; i < num_i; ++i) {
compute_stuff;
#if FLAG_1
variable_potencially_not_used = 1;
#endif
}
}```
Upvotes: 2