内核 CUDA 中的 C++ 代码?
C++ code in kernel CUDA?
据我所知,CUDA 支持 C 和 C++。但是我不能在我的内核中使用 C++。
我试了一个像这样的简单例子
__global__ void simple(){
cout<<"abc";
}
那是错误。但是如果我改成printf("abc");
就对了
你能帮我解释一下吗?非常感谢!
CUDA 没有 link 使用 cout
函数所需的库和头文件。但是,您可以启用 printf()
此回答解释了启用此功能的过程:
printing from cuda kernels
此处引用以便于访问:
To enable use of plain printf() on devices of Compute Capability >= 2.0, it's important to compile for CC of at least CC 2.0 and disable the default, which includes a build for CC 1.0.
Right-click the .cu file in your project, select Properties, select Configuration Properties | CUDA C/C++ | Device. Click on the Code Generation line, click the triangle, select Edit. In the Code Generation dialog box, uncheck Inherit from parent or project defaults, type compute_20,sm_20 in the top window, click OK.
来自 CUDA 7.5 nvidia 幻灯片:
C++11 支持的特性:
- 自动
- lambdas
- std::initializer_list
- 可变参数模板
- static_asserts
- constexpr
- 右值引用
- 基于范围的 for 循环
C++ 不支持的功能
- thread_local
- 标准库:std::*
std::cout
在 C++ 标准库中定义,CUDA 不支持。使用 C printf
从 CUDA 6.5 开始,'compute_11'、'compute_12'、'compute_13'、'sm_11'、'sm_12'、和 'sm_13' 架构已弃用。所以 nvcc
将默认编译为 CC 2.0 启用 printf
支持。
据我所知,CUDA 支持 C 和 C++。但是我不能在我的内核中使用 C++。
我试了一个像这样的简单例子
__global__ void simple(){
cout<<"abc";
}
那是错误。但是如果我改成printf("abc");
就对了
你能帮我解释一下吗?非常感谢!
CUDA 没有 link 使用 cout
函数所需的库和头文件。但是,您可以启用 printf()
此回答解释了启用此功能的过程: printing from cuda kernels 此处引用以便于访问:
To enable use of plain printf() on devices of Compute Capability >= 2.0, it's important to compile for CC of at least CC 2.0 and disable the default, which includes a build for CC 1.0.
Right-click the .cu file in your project, select Properties, select Configuration Properties | CUDA C/C++ | Device. Click on the Code Generation line, click the triangle, select Edit. In the Code Generation dialog box, uncheck Inherit from parent or project defaults, type compute_20,sm_20 in the top window, click OK.
来自 CUDA 7.5 nvidia 幻灯片:
C++11 支持的特性:
- 自动
- lambdas
- std::initializer_list
- 可变参数模板
- static_asserts
- constexpr
- 右值引用
- 基于范围的 for 循环
C++ 不支持的功能
- thread_local
- 标准库:std::*
std::cout
在 C++ 标准库中定义,CUDA 不支持。使用 C printf
从 CUDA 6.5 开始,'compute_11'、'compute_12'、'compute_13'、'sm_11'、'sm_12'、和 'sm_13' 架构已弃用。所以 nvcc
将默认编译为 CC 2.0 启用 printf
支持。