作为 CUDA __global__ 函数的 C++ 对象的成员函数 [英] Member function of a C++ object as a CUDA __global__ function

查看:44
本文介绍了作为 CUDA __global__ 函数的 C++ 对象的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类:

  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.

这篇关于作为 CUDA __global__ 函数的 C++ 对象的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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