What are the real C++ language constructs supported by CUDA device code?(CUDA 设备代码支持哪些真正的 C++ 语言结构?)
问题描述
Appendix D of the 3.2 version of the CUDA documentation refers to C++ support in CUDA device code.
It is clearly mentioned that CUDA supports "Classes for devices of compute capability 2.x". However, I'm working with devices of compute capability 1.1 and 1.3 and I can use this feature!
For instance, this code works:
// class definition voluntary simplified
class Foo {
private:
int x_;
public:
__device__ Foo() { x_ = 42; }
__device__ void bar() { return x_; }
};
//kernel using the previous class
__global__ void testKernel(uint32_t* ddata) {
Foo f;
ddata[threadIdx.x] = f.bar();
}
I'm also able to use widespread libraries such as Thrust::random random generation classes.
My only guess is that I'm able to do so thanks to the automatic inlining of __device__
marked function, but this does not explain the handling of member variables withal.
Have you ever used such features in the same conditions, or can you explain to me why my CUDA code behaves this way? Is there something wrong in the reference guide?
Oficially, CUDA has no support for classes on devices prior to 2.0.
Practically, from my experience, you can use all C++ features on all devices as long as the functionality can be resolved at compile-time. Devices prior to 2.0 do not support function calls (all functions are inlined) and no program jumps to a variable address (only jumps at constant address).
This means, you can use the following C++ constructs:
- Visibility (public/protected/private)
- non-virtual inheritance
- whole template programming and metaprogramming (until you stuble on nvcc bugs; there are quite a few of them as of version 3.2)
- constructors (except when object is declared in __ shared __ memory)
- namespaces
You cannot use the following:
- new & delete operators (I believe devices >=2.0 can do that)
- virtual methods (requires jumps at variable address)
- function recursion (requires function calls)
- exceptions
Actually, all examples in chapter D.6 of the CUDA Programming Guide can compile for devices <2.0
这篇关于CUDA 设备代码支持哪些真正的 C++ 语言结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CUDA 设备代码支持哪些真正的 C++ 语言结构?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01