作为CUDA __global__函数的C ++对象的成员函数 [英] Member function of a C++ object as a CUDA __global__ function
问题描述
我有一个基类:
template< class T&
class A {
public:
//一些数据
T数据;
//一些函数像构造等。
...
//一个虚函数
virtual void evaluate()= 0;
}
和派生类:
模板< class T>
class B:public A< T> {
public:
//一些函数类似构造函数等
virtual void evaluate();
__global__ void function2(); // ****错误讯息
}
模板< class T> void
B< T> :: evaluate()
{
dim3 grid(1); dim3 block
void function2<<< grid,block>>>();
}
和
template< class T> __global__ void B< T> :: function2()// ****错误消息
pre>
{
//计算这里
}
基本上我有一个派生类的成员函数,我想在设备上以并行方式执行。
很抱歉,我收到错误:
错误:的线上的内存限定符:
1> __global__ void function2(); // ****错误讯息
2> template< class T> __global__ void B< T> :: function2()// ****错误讯息
新到CUDA。如果有人指向我的错误,这将是非常善良。解决方案您的第一个代码段中的模板类定义是非法的,因为它包含__global__函数(CUDA内核)。根据语言文档,__global__函数不能是静态类成员函数。第二个模板类成员函数由于相同的原因是非法的。
I have a base class:
template <class T> class A{ public: // some data T data; //some functions like constructs etc. ... // one virtual function virtual void evaluate() = 0; }
and a derived class:
template <class T> class B:public A<T>{ public: // some functions like constructors etc. virtual void evaluate(); __global__ void function2(); // **** error message }
Also, I have
template <class T> void B<T>::evaluate() { dim3 grid(1);dim3 block(1); void function2<<<grid,block>>>(); }
and
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
I am new to CUDA. It would be very kind if someone points me to my error. I am developing on Visual Studio 2010.
解决方案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屋!