这个问题非常简单,但让我概述一下我的框架。我有一个抽象类AbstractScheme
表示一种计算类型(方程的一种离散化,但这并不重要)。
每个实现都必须提供一个方法来返回方案名称,并且必须实现一个受保护的函数,即 CUDA 内核。基本抽象类提供了一个公共方法,该方法调用 CUDA 内核并返回内核完成所需的时间。
class AbstractScheme
{
public:
/**
* @return The name of the scheme is returned
*/
virtual std::string name() const =0;
/**
* Copies the input to the device,
* computes the number of blocks and threads,
* launches the kernel,
* copies the output to the host,
* and measures the time to do all of this.
*
* @return The number of milliseconds to perform the whole operation
* is returned
*/
double doComputation(const float* input, float* output, int nElements)
{
// Does a lot of things and calls this->kernel().
}
protected:
/**
* CUDA kernel which does the computation.
* Must be implemented.
*/
virtual __global__ void kernel(const float*, float*, int) =0;
};
我还有这个基类的几个实现。但是当我尝试使用 nvcc 7.0 进行编译时,我收到此错误消息,指的是我定义函数的行kernel
in AbstractScheme
(上面清单中的最后一行):
myfile.cu(60): error: illegal combination of memory qualifiers
I could not find any resource saying the kernels cannot be virtual functions, but I have the feeling this is the problem. Can you explain the rationale behind this? I clearly understand how and why __device__
functions cannot be virtual functions (virtual functions are pointers to actual [host] functions stored in the object, and you cannot call such a function from within device code), but I'm not sure about __global__
functions.
编辑:我删除的问题部分是错误的。请查看评论以了解原因。