什么是CUDA设备代码支持的真正的C ++语言结构? [英] What are the real C++ language constructs supported by CUDA device code?

查看:136
本文介绍了什么是CUDA设备代码支持的真正的C ++语言结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CUDA文档的3.2版本的附录D指的是CUDA设备代码中的C ++支持。

清楚地提到CUDA支持用于计算能力2.x的设备的类。但是,我正在使用计算能力1.1和1.3的设备,我可以使用此功能!

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(); 
}



我还可以使用广泛的库,如Thrust :: random random生成类。
我的唯一猜测是,我能够这样做感谢自动内联的 __ device __ 标记的函数,但这并不解释成员变量的处理。

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.

您是否在相同的条件下使用过这些功能,或者您能向我解释为什么我的CUDA代码会这样做?参考指南中有错误吗?

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?

推荐答案

理论上,CUDA不支持2.0之前的设备上的类。

Oficially, CUDA has no support for classes on devices prior to 2.0.

实际上,根据我的经验,您可以在所有设备上使用所有C ++功能,只要该功能可以在编译期解决。 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).

可以使用以下C ++结构:

This means, you can use the following C++ constructs:


  • 可见性(public / protected / private)

  • 虚拟继承

  • 整个模板编程和元编程(直到你对nvcc错误感到困惑;从3.2版开始有很多错误)

  • < (除非在__共享__内存中声明对象)
  • 命名空间

  • 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

以下内容:


  • new&删除运算符(我相信设备> = 2.0可以做到)

  • 虚方法(需要在变量地址处跳转)

  • 函数递归)

  • 例外

  • new & delete operators (I believe devices >=2.0 can do that)
  • virtual methods (requires jumps at variable address)
  • function recursion (requires function calls)
  • exceptions

实际上,CUDA编程指南可以为设备< 2.0

Actually, all examples in chapter D.6 of the CUDA Programming Guide can compile for devices <2.0

这篇关于什么是CUDA设备代码支持的真正的C ++语言结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆