0

如果我编写以下 CUDA 代码:

#include <stdio.h>

template <unsigned N>
__global__ void foo()
{
    printf("In kernel foo() with N = %u\n", N);
    if (N < 10) { return; }
    printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}

int main() {
    foo<5><<<1,1>>>();
    foo<20><<<1,1>>>();
    return 0;
}

我收到编译器警告:

a.cu(8): warning: statement is unreachable
          detected during instantiation of "void foo<N>() [with N=5U]" 
(12): here

我“感觉”我不应该收到此警告,因为无法访问的代码仅对于模板参数的某些值是无法访问的。如果我写“CPU等价物”,可以这么说:

#include <cstdio>

template <unsigned N>
void foo()
{
    std::printf("In kernel foo() with N = %u\n", N);
    if (N < 10) { return; }
    std::printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}

int main() {
    foo<5>();
    foo<20>();
    return 0;
}

并使用 gcc (5.4.0) 构建它 - 即使我使用-Wall.

现在,我可以通过写来规避这个

if (not (N < 10)) { 
    printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}

但我宁愿避免不得不颠倒我的逻辑来跳过nvcc的“箍”。我也可以写

if (not (N < 10)) { 
    return;
}
else {
    printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}

但是 - 我不想缩进所有代码(同样的问题可能会再次发生,需要在 else 块内进行更多的缩进。

有什么我可以做的吗?另外,这不是一个“错误”,还是我应该将其报告为错误的错误功能?

4

1 回答 1

1

关于什么:

template<unsigned N, bool>
struct FooImpl
{
  static void foo()
  {
    std::printf("In kernel foo() with N = %u\n", N);
  }
};

template<unsigned N>
struct FooImpl<N, false>
{
  static void foo()
  {
    std::printf("In kernel foo() with N = %u\n", N);
    std::printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
  }
};

template <unsigned N>
__global__ void foo()
{
  FooImpl<N, N < 10>::foo();
}
于 2017-04-01T18:49:39.447 回答