作为 CUDA __global__ 函数的 C++ 对象的成员函数

2022-01-10 00:00:00 cuda c++ visual-studio-2010

我有一个基类:

  template <class T> 
  class A{
         public:
           // some data
           T data;
           //some functions like constructs etc.
            ...
           // one virtual function
           virtual void evaluate() = 0;

   }

还有一个派生类:

 template <class T> 
 class B:public A<T>{
          public:
          // some functions like constructors etc.
          virtual void evaluate();
          __global__ void function2();   // **** error message

 } 

还有,我有

 template <class T> void
 B<T>::evaluate()
 { 
    dim3 grid(1);dim3 block(1);  
    void function2<<<grid,block>>>();
 }

template <class T>  __global__ void B<T>::function2() // **** error message 
{
   // computation here
}

所以本质上我有一个派生类的成员函数,我想在设备上以并行方式执行它.

so essentially I have a member function of a derived class which I would like to execute in a parallel fashion on the device.

不幸的是,我得到了错误:

Unfortunately, I get the error:

error : illegal combination of memory qualifiers on the lines :

1> __global__ void function2();   // **** error message

2> template <class T>  __global__ void B<T>::function2() // **** error message

我是 CUDA 的新手.如果有人指出我的错误,那就太好了.我正在 Visual Studio 2010 上开发.

I am new to CUDA. It would be very kind if someone points me to my error. I am developing on Visual Studio 2010.

推荐答案

第一个代码片段中的模板类定义是非法的,因为它包含一个 __global__ 函数(CUDA 内核).根据 语言文档,__global__ 函数不能是静态类成员函数.出于同样的原因,第二个模板类成员函数是非法的.

The template class definition in your first code snippet is illegal because it contains a __global__ function (CUDA kernel). As per the language documentation, __global__ functions cannot be static class member functions. The second templated class member function is illegal for the same reason.

相关文章