简体   繁体   English

对 CUDA 代码使用 C++ 头结构

[英]Using C++ header structure for CUDA code

I know that this question has probably been asked, but I can't find an answer that particularly answers my question so here it goes...我知道可能有人问过这个问题,但我找不到特别能回答我的问题的答案,所以就这样吧……

The question is pretty simple, I am trying to use a C++ style header for CUDA (.cu/.cuh) code.问题很简单,我正在尝试为 CUDA (.cu/.cuh) 代码使用 C++ 风格的头文件。

//MyClass.cuh
#ifndef MY_CLASS
#define MY_CLASS
#include ...cuda.h, etc.
class MyClass
{
   public:
      void myFunction();   
   private:
      __global__ void myKernel();
}
#endif

//MyClass.cu
#include "MyClass.cuh"
void MyClass::myFunction()
{
   //myFunction definition...
}

__global__ void MyClass::myKernel()
{
   //myKernel definition...
}

Will this work?这会起作用吗?

No, this will not work:不,这行不通:

class MyClass
{
    ...
      __global__ void myKernel();

the compiler will not let you define a class member function that is a __global__ function (try it).编译器不会让你定义一个__global__函数的类成员函数(试试看)。 Even if you could, such a function running on the device would not have the usual class access to other class data or function members.即使可以,在设备上运行的此类函数也不具有对其他类数据或函数成员的通常类访问权限。 So the usual advice is to declare your kernel definition outside the scope of the class, and have a wrapper member function in the class that calls the kernel.所以通常的建议是在类的范围之外声明你的内核定义,并在调用内核的类中有一个包装成员函数。

Some additional discussion is here and here一些额外的讨论在这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM