与 CUDA 协作组合作时,我需要
#include <cooperative_groups.h>
访问 cooperative_groups
命名空间。然而,智能感知无法看到这个命名空间,我最终得到了红色的波浪线。
最小示例代码:
#include <cooperative_groups.h>
namespace cg = cooperative_groups; // Intellisense does not recognise cooperative_groups here
int main() {
return 0;
}
上面的代码编译得很好(任何合作组的例子也是如此),所以这只是智能感知无法找到命名空间的问题。
查看
cooperative_groups.h
头文件后,发现整个命名空间都被包裹在#if defined(__cplusplus) && defined(__CUDACC__)
中。当intellisense尝试解析头文件时,__CUDACC__
未定义(这是由nvcc定义的),因此无法找到名称空间。我的解决方案是将 #include <cooperative_groups.h>
包装在条件 #define
中以实现智能感知:
#ifdef __INTELLISENSE__
#define __CUDACC__
#endif // __INTELLISENSE__
#include <cooperative_groups.h>
#ifdef __INTELLISENSE__
#undef __CUDACC__
#endif // __INTELLISENSE__
现在智能感知可以正确解析头文件,并按照您的预期执行语法突出显示。在 Visual Studio 2022 中进行了测试,使用上面的最小示例和简单合作组示例。