问题
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;
};
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? 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.
EDIT: the part of the question which I striked out is wrong. Please look at the comments to understand why.
回答1:
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.
来源:https://stackoverflow.com/questions/31120692/can-cuda-kernels-be-virtual-functions