什么是真正的 C++ CUDA 设备代码支持的语言结构?
CUDA 文档 3.2 版本的附录 D 提到了 CUDA 设备代码中的 C++ 支持。
明确提到CUDA支持“计算能力2.x设备的类”。但是,我正在使用计算能力 1.1 和 1.3 的设备,我可以使用此功能!
例如,这段代码可以工作:
// 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();
}
我还可以使用广泛的库,例如 Thrust::random 随机生成类。 我唯一的猜测是,由于 __device__
标记函数的自动内联,我能够做到这一点,但这并不能解释成员变量的处理。
您是否曾经在相同条件下使用过此类功能,或者您能否向我解释一下为什么我的 CUDA 代码会这样?参考指南有问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
官方规定,CUDA 不支持 2.0 之前的设备上的类。
实际上,根据我的经验,只要功能可以在编译时解析,您就可以在所有设备上使用所有 C++ 功能。 2.0之前的设备不支持函数调用(所有函数都是内联的)并且没有程序跳转到变量地址(仅跳转到常量地址)。
这意味着,您可以使用以下 C++ 构造:
否则不能使用以下内容:
实际上,CUDA 编程指南第 D.6 章中的所有示例都可以针对设备 < 进行编译;2.0
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:
You cannot use the following:
Actually, all examples in chapter D.6 of the CUDA Programming Guide can compile for devices <2.0
一些 C++ 类功能可以工作,但是编程指南基本上说它没有得到完全支持,因此并非所有 C++ 类功能都可以工作。如果你能做你想做的事,那么你应该继续!
Some C++ class functionality will work, however the Programming Guide is basically saying that it's not fully supported and therefore not all C++ class functionality will work. If you can do what you're looking to do then you should go ahead!