我可以使用仅主机功能覆盖CUDA主机和设备功能吗? [英] Can I override a CUDA host-and-device function with a host-only function?
问题描述
请考虑以下程序:
class A {
__host__ __device__ void foo();
};
class B : A {
__host__ void foo();
};
int main()
{
A a; (void) a;
B b; (void) b;
}
此使用nvcc 10编译(GodBolt).
This compiles (GodBolt) with nvcc 10.
但是,在更复杂的程序中,有时会出现以下错误(为了可读性而换行):
Yet, in more complex programs, I sometimes get the following error (line breaks for readability):
whatever.hpp(88): error: execution space mismatch: overridden entity (function
"C::foo") is a __host__ __device__ function, but overriding entity (function "D::foo")
is a __host__ function
因此,nvcc告诉我不应该在覆盖方法时删除执行空间.我不是在问自己的代码(在这里没有引用),而是在问原理:
So, nvcc is telling me that I'm not supposed to drop an execution space when overriding methods. I'm not asking about my own code (which I haven't cited here), but about the principle:
- 如果仅用
__host__
个函数(我认为是合理的)来覆盖__host__ __device__
个函数是可以接受的-那么nvcc怎么会出现这样的错误? - 或者,如果不允许的话-为什么上面的小程序正在编译?
- If it's acceptable to override
__host__ __device__
functions with just__host__
functions (which I find reasonable) - then how can nvcc even have such an error? - Alternatively, if it's not allowed - why is the small program above compiling?
推荐答案
覆盖(虚拟)方法必须遵守覆盖方法的执行空间选择.
覆盖"仅与虚拟方法相关-因此必须在C::foo()
被标记为virtual
的情况下.实际上,如果我们在示例程序中将foo()
标记为虚拟:
Overriding (virtual) methods must respect execution space choice of the overridden method.
"overriding" is only relevant to virtual methods - so it must be the case that your C::foo()
is marked virtual
. And indeed, if we mark foo()
in the example program as virtual:
class A {
virtual __host__ __device__ void foo();
};
class B : A {
__host__ void foo(); // can say "override" here; but it doesn't matter
};
int main()
{
A a; (void) a;
B b; (void) b;
}
这将 无法编译:
<source>(6): error: member function declared with "override" does not override
a base class member
此限制有意义吗?可以想象一种解释,其中基类方法将应用于__device__
侧调用,而子类方法将应用于__host__
侧调用.但这也有点尴尬-当通过基类ptr对对象进行操作时,我们需要调用 something .
Does this limitation make sense? One could imagine an interpretation in which the base-class method will apply to __device__
-side calls, and the subclass method to __host__
-side calls. But that too is a bit awkward - and we need to call something when acting on an object via a base class ptr.
这篇关于我可以使用仅主机功能覆盖CUDA主机和设备功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!