[英]Avoiding unused variable warnings with pre-compiler statements inside openmp parallel blocks
My problem arises from a combination of a few particular things. 我的问题来自于一些特殊的东西。
default(none)
(because I'm paranoid). 我将openmp parallel用于具有default(none)
块(因为我很偏执)。 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
. 为了清楚起见,假设变量x
仅在FLAG_1
为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). 我可以将x
的声明及其用法包装在#if FLAG1 ... #endif
语句中,但是我仍然需要在#pragma omp parallel
的变量列表中列出x
,据我所知,我不能在#pragma omp parallel
语句内嵌套#if FLAG1 ... #endif
(它长几行-变量很多)。 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. 在这种情况下,删除的变量都是omp-private,我default(none)
承认,只需将default(none)
替换为default(private)
解决问题。 That said, I do like the coding practice of default(none)
, and would like to keep it if possible. 就是说,我喜欢default(none)
的编码实践,并且希望尽可能保留它。
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. 另一个选择是简单地将omp-parallel分解为如下所示的内容,但是compute_stuff
和compute_more_stuff
具有一些共享的计算/内存访问,我希望避免重复。
#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! 在保持可读性和高效代码的同时,如何保持良好编码习惯的任何想法将不胜感激!
If you are using C++17, What's about the [[maybe_unused]]
attribute?: 如果您使用的是C ++ 17, [[maybe_unused]]
属性是什么?
#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. 如果不是,则替代方法是实现与Q_UNUSED
宏类似的Q_UNUSED
。 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
}
}```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.