-2

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.

//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?

WaffleMan0310
  • 75
  • 1
  • 7

1 Answers1

1

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). 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

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257