简体   繁体   English

在openmp并行块内使用预编译器语句避免未使用的变量警告

[英]Avoiding unused variable warnings with pre-compiler statements inside openmp parallel blocks

Background 背景

My problem arises from a combination of a few particular things. 我的问题来自于一些特殊的东西。

  1. I am using pre-processor statements to determine what kind of calculations to include in the produced executable 我正在使用预处理程序语句来确定要在生成的可执行文件中包括哪种计算
  2. I am using openmp parallel for blocks with default(none) (because I'm paranoid). 我将openmp parallel用于具有default(none)块(因为我很偏执)。
  3. The code compiles and runs properly, but can spew out unused variable warnings depending on the pre-processor flags. 该代码可以编译并正常运行,但是可以根据预处理器标志发出未使用的变量警告。 Not technically an error, but I would like to remove those warnings (and no, I don't just mean disabling the compiler warnings, but actually removing the cause ie the unused variables). 从技术上讲不是错误,但是我想删除这些警告(不,我不只是意味着禁用编译器警告,而是实际上删除了原因,即未使用的变量)。

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
    }
  }

Main issue 主要问题

Suppose for clarity that variable x is only required if FLAG_1 is true . 为了清楚起见,假设变量x仅在FLAG_1true才是必需的。 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. 因此,我要么收到有关编译指示中列出的不存在变量的错误,要么收到有关未使用变量的警告。

Possible (but unsatisfying) solutions 可能(但不令人满意)的解决方案

  1. 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)的编码实践,并且希望尽可能保留它。

  2. 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_stuffcompute_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM