CUDA和类 [英] CUDA and Classes

查看:817
本文介绍了CUDA和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一些洞察如何确切地使用类与CUDA,虽然有一个普遍的共识,它可以完成,显然是由人做的,我已经很难找到如何实际做。

I've searched all over for some insight on how exactly to use classes with CUDA, and while there is a general consensus that it can be done and apparently is being done by people, I've had a hard time finding out how to actually do it.

我有一个类,实现一个基本的bitset与操作符重载等。我需要能够在主机和设备上实例化这个类的对象,在两者之间复制等。我在.cu中定义这个类吗?如果是这样,我如何在我的主机端C ++代码中使用它?类的函数不需要访问特殊的CUDA变量,如threadId;它只需要能够使用主机和设备端。

I have a class which implements a basic bitset with operator overloading and the like. I need to be able to instantiate objects of this class on both the host and the device, copy between the two, etc. Do I define this class in a .cu? If so, how do I use it in my host-side C++ code? The functions of the class do not need to access special CUDA variables like threadId; it just needs to be able to be used host and device side.

感谢任何帮助,如果我接近这完全错误的方式,我会喜欢听到替代品。

Thanks for any help, and if I'm approaching this in completely the wrong way, I'd love to hear alternatives.

推荐答案

在#include的标题中定义类,就像在C ++中一样。

Define the class in a header that you #include, just like in C++.

任何必须从设备代码调用的方法都应该用 __ device __ __host __ declspecs,包括构造函数和析构函数,如果你计划使用 new / delete 设备(注意 new / delete 需要CUDA 4.0和计算能力2.0或更高的GPU)。

Any method that must be called from device code should be defined with both __device__ and __host__ declspecs, including the constructor and destructor if you plan to use new/delete on the device (note new/delete require CUDA 4.0 and a compute capability 2.0 or higher GPU).

您可能想要定义一个宏

#ifdef __CUDACC__
#define CUDA_CALLABLE_MEMBER __host__ __device__
#else
#define CUDA_CALLABLE_MEMBER
#endif 

然后在成员函数上使用此宏

Then use this macro on your member functions

class Foo {
public:
    CUDA_CALLABLE_MEMBER Foo() {}
    CUDA_CALLABLE_MEMBER ~Foo() {}
    CUDA_CALLABLE_MEMBER void aMethod() {}
};

这样做的原因是只有CUDA编译器知道 __ device __ __ host __ - 您的主机C ++编译器将引发错误。

The reason for this is that only the CUDA compiler knows __device__ and __host__ -- your host C++ compiler will raise an error.

编辑:
注意 __ CUDACC __ 由NVCC在编译CUDA文件时定义。这可以是在使用NVCC编译.cu文件时或使用命令行选项 -x cu 编译任何文件时。

Note __CUDACC__ is defined by NVCC when it is compiling CUDA files. This can be either when compiling a .cu file with NVCC or when compiling any file with the command line option -x cu.

这篇关于CUDA和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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