nvcc
禁用特定的编译器警告,具体来说是禁用以下警告:warning: NULL reference is not allowed我正在处理的代码中使用了
NULL
引用作为SFINAE的一部分,因此无法避免使用它们。理想的解决方案是在我们要禁用警告的源文件中使用#pragma
,但如果存在可以关闭仅针对此警告的编译器标志,则也可以接受。nvcc
禁用特定的编译器警告,具体来说是禁用以下警告:warning: NULL reference is not allowed我正在处理的代码中使用了
NULL
引用作为SFINAE的一部分,因此无法避免使用它们。理想的解决方案是在我们要禁用警告的源文件中使用#pragma
,但如果存在可以关闭仅针对此警告的编译器标志,则也可以接受。关于-xcudafe的问题,补充一下之前的回答(由于声望不够,无法在评论中留言):
重要修改:
cudaFE显然是Nvidia从Edison Design Group获得的C++前端的定制版本。您可以在此处找到其文档:http://www.edg.com/docs/edg_cpp.pdf。我目前正在参考2019年7月v5.1手册的页码。
@einpoklum指出,就像我在最初的帖子中最初陈述的那样,仅进行push/pop并不起作用,特别是#pragma push会生成一个警告,表示它被忽略了。我无法复制这个警告,但确实在下面的测试程序中,CUDA 10.1和CUDA 9.2都没有实际执行push/pop操作(请注意,第20行和第22行都没有生成警告)。
但是,在该手册的第75页中,他们介绍了如何在不使用push/pop的情况下进行局部诊断严重性控制:
以下示例抑制了对类A声明中“无意义的友元声明”的警告:
#pragma diag_default 将警告返回到默认状态。另一个示例是:
#pragma diag_suppress 522 class A { friend class A; }; #pragma diag_default 522 class B { friend class B; };
#pragma diag_suppress = code_is_unreachable ... #pragma diag_default = code_is_unreachable
等号符是可选的。测试表明这样做可以正确地本地化严重性控制。此外,测试还显示以这种方式添加诊断抑制会增加先前的诊断抑制,而不是替换它们。值得注意的是,在CUDA 10.1中,无法访问的代码没有生成警告,而在CUDA 9.2中则有。最后,手册第77页提到了一种新的推入/弹出语法:
#pragma push_macro(“identifier”) #pragma pop_macro(“identifier”)
但是我无法让它在下面的程序中工作。
以上所有内容都经过以下程序测试,使用
nvcc -std=c++14 test_warning_suppression.cu -o test_warning_suppression
编译:
#include <cuda_runtime.h> __host__ __device__ int return1(){ int j = -1; //warning given for both CUDA 9.2 and 10.1 return 1; if(false){ return 0; } //warning given here for CUDA 9.2 } #pragma push #pragma diag_suppress = code_is_unreachable #pragma diag_suppress = declared_but_not_referenced __host__ __device__ int return2(){ int j = -1; return 2; if(false){ return 0; } } #pragma pop __host__ __device__ int return3(){ int j = -1; //no warning given here return 3; if(false){ return 0; } //no warning here even in CUDA 9.2 } //push/pop did not localize warning suppression, so reset to default #pragma diag_default = declared_but_not_referenced #pragma diag_default = code_is_unreachable //warning suppression localized to lines above by diag_default! __host__ __device__ int return4(){ int j = -1; //warning given for both CUDA 9.2 and 10.1 return 4; if(false){ return 0; } //warning given here for CUDA 9.2 } /* below does not work as of CUDA 10.1 #pragma push_macro(“identifier”) #pragma diag_suppress = code_is_unreachable __device__ int return5(){ return 5; if(false){ return 0; } } #pragma pop_macro(“identifier”) __device__ int return6(){ return 6; if(false){ return 0; } } */ int main(){ return 0; }
补充user2333829的回答:如果您知道警告名称,可以像这样禁用它:
-Xcudafe "--diag_suppress=boolean_controlling_expr_is_constant"
如果您不知道名称,请通过编译获取警告编号:
-Xcudafe --display_error_number
-Xcudafe --diag_suppress=<warning_number>
-Xcudafe --display_error_number -Xcudafe --diag_suppress=<warning_number>
,要么是-Xcudafe "--display_error_number --diag_suppress=<warning_number>"
。 - Jakub Klinkovský#20208-D
,那么你该使用什么来进行抑制? - einpoklum#pragma diag_suppress
进行抑制呢? - einpoklum您可以使用w标志来抑制警告
nvcc -w
-Xcudafe
,所以这里有另一种方法。CL.exe
传递编译器标志来禁用特定警告。例如,要禁用有关未检查迭代器的警告,可以传递 /wd4996
。warning C4996: 'std::_Copy_impl': Function call with parameters that may be
unsafe - this call relies on the caller to check that the passed values are
correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See
documentation on how to use Visual C++ 'Checked Iterators'
nvcc
,因此您需要通过 CUDA C/C++
对话框添加它。我在使用Ubuntu g++编译器的情况下,使用nvcc进行编译,我的情况下是openmpi mpic++。对于g++编译器的"-Wunused-result",相应的消息抑制是"-Wno-unused-result"。因此,在nvcc中传递像"-Xcompiler“-Wno-unused-result”这样的参数对我很有效。
nvcc --help
命令,我可以看到有一个--disable-warnings
选项或者等价的-w
选项。 - Soroosh Bateni.cu
文件之外。CUDA C接近但不完全兼容C++。当尝试使用更晦涩的C++特性时,你可能会遇到其他问题。.cu
文件编译速度也可能相对较慢,因此从长远来看,只在其中保留设备代码可能是一个优势。 - Roger Dahl