问题描述
这个问题是相当直截了当的,但让我概述我的框架。我有一个抽象类 AbstractScheme
表示一种计算类型(一种方程的离散化,但这不重要)。
每个实现必须提供一个方法来返回方案的名称,并且必须实现一个受保护的函数,它是CUDA内核。基本抽象类提供了一个调用CUDA内核的公共方法,并返回内核完成所需的时间。
The question is quite straighforward, but let me give an overview of my framework. I have an abstract class AbstractScheme
representing a type of computation (a kind of discretization for an equation, but this is not important).Each implementation has to provide a method to return the name of the scheme and has to implement a protected function which is the CUDA kernel. The base abstract class provides a public method which calls the CUDA kernel and returns how long it took for the kernel to be completed.
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
在 AbstractScheme
(上面列表中的最后一行):
I also have a couple of implementations of this base class. But when I try to compile with nvcc 7.0, I get this error message referring to the line where I define the function kernel
in AbstractScheme
(the last line in the above listing):
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?
:我所提出的问题的一部分是错误的。请查看注释以了解原因。
the part of the question which I striked out is wrong. Please look at the comments to understand why.
推荐答案
Kernels不能是CUDA对象模型中的类的成员,或不。这是编译错误的原因,即使它不是由编译器发出的错误消息特别明显。
Kernels can't be members of classes in the CUDA object model, whether virtual or not. That is the cause of the compile error, even if it isn't particularly obvious from the error message emitted by the compiler.
这篇关于CUDA内核可以是虚函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!