Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CUDA:“;非void函数末尾缺少return语句“;在constexpr if函数中_C++_Cuda - Fatal编程技术网

C++ CUDA:“;非void函数末尾缺少return语句“;在constexpr if函数中

C++ CUDA:“;非void函数末尾缺少return语句“;在constexpr if函数中,c++,cuda,C++,Cuda,当我编译以下测试代码时,我得到以下警告: test.cu(49): warning: missing return statement at end of non-void function "AllocateSize<T,D>(size_t) noexcept [with T=int, D=Device::GPU]" detected during instantiation of "Pointer<T, D> Allo

当我编译以下测试代码时,我得到以下警告:

test.cu(49): warning: missing return statement at end of non-void function "AllocateSize<T,D>(size_t) noexcept [with T=int, D=Device::GPU]"
          detected during instantiation of "Pointer<T, D> AllocateSize<T,D>(size_t) noexcept [with T=int, D=Device::GPU]" 
(61): here
  • CUDA 11.1版
  • 基于Ubuntu的Linux发行版

我们的编译团队研究了这个问题。这个警告是假的。我们希望在未来的CUDA版本中解决这个问题。我将无法回答关于何时可能发生的问题。代码正在正确生成

如果希望使警告静音,一种可能的方法是在相关函数的末尾添加一个额外的return语句。在这种情况下,这应该没有效果,因为无法访问:

template <typename T, Device D>
__host__ inline Pointer<T, D> AllocateSize(const size_t size) noexcept {
    if constexpr (D == Device::CPU) {
        return CPU_Ptr<T>(reinterpret_cast<T*>(std::malloc(size)));
    } else {
        T* p;
        cudaMalloc(reinterpret_cast<void**>(&p), size);
        return GPU_Ptr<T>(p);
    }
    return Pointer<T, D>(NULL);
}
模板
__主机\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu{
if constexpr(D==设备::CPU){
返回CPU_Ptr(重新解释转换(标准::malloc(大小));
}否则{
T*p;
Cudamaloc(重新解释铸件和p),尺寸;
返回GPUptr(p);
}
返回指针(NULL);
}

如果函数的返回类型是
auto
,则由于[另一个虚假的]“冲突类型错误”,在函数末尾添加一个免费的返回语句(@RobertCrovella的答案)不起作用

对我来说,有效的方法是将此选项传递到前端

-Xcudafe "--diag_suppress=implicit_return_from_non_void_function"

(我花了一段时间才找到正确的选择)


更正,这不会抑制警告,它似乎与作为模板的函数有关,并且在实例化模板之前应用了pragma

**更新:更具体的解决方法** 正如@RobertCrovella所评论的,全局抑制警告会使其忽略警告有意义的合法案例。因此,本地化替代方案更可取:

#if defined(__NVCC__)
#pragma push
#pragma diag_suppress 0117 //"implicit_return_from_non_void_function"
#endif
template <typename T, Device D>
__host__ inline Pointer<T, D> AllocateSize(const size_t size) noexcept {
    if constexpr (D == Device::CPU) { ... } else { ... }
}
#if defined(__NVCC__)
#pragma pop
#endif
#如果已定义(uuu NVCC_uu)
#布拉格推
#pragma diag\u suppress 0117/“从非无效函数隐式返回”
#恩迪夫
模板
__主机\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu{
如果constexpr(D==Device::CPU){…}否则{…}
}
#如果已定义(\uuuu NVCC\uuuuu)
#布拉格流行音乐
#恩迪夫
我在此处找到错误代码:。但不幸的是,关于这些NVCC编译器选项的信息很难找到。

我认为这是意料之中的。我的建议是在developer.nvidia.com上提交一个bug谢谢您的快速响应@RobertCrovella,你能确认另一个答案是否是推荐的解决方法吗?是的,应该可以接受。明确地说,可以安全地忽略此特定问题的警告。另一个答案(…diag_suppress…)就是明确忽略这种警告。我们必须承认,如果您在同一类别中有另一个非虚假警告,您也有忽略该警告的风险。@RobertCrovella,是的,暂时禁用该警告的本地化pragma会更好。我不知道pragma是否可以与模板代码一起使用。我还没试过。你知道怎么做吗?“英特尔编译器”也有同样的问题(可能有相同的前端)。@RobertCrovella,看来pragmas不会抑制此警告。谢谢,这似乎是一个临时解决办法。感谢您抽出时间来研究我的问题。@PolarToCartesian,谢谢,我认为这比更改代码要好,可能还要更改代码的含义。它还强调这是编译器的缺陷,而不是您的代码。@PolarToCartesian,我更新了我的答案。
#if defined(__NVCC__)
#pragma push
#pragma diag_suppress 0117 //"implicit_return_from_non_void_function"
#endif
template <typename T, Device D>
__host__ inline Pointer<T, D> AllocateSize(const size_t size) noexcept {
    if constexpr (D == Device::CPU) { ... } else { ... }
}
#if defined(__NVCC__)
#pragma pop
#endif