我可以使用仅主机功能覆盖CUDA主机和设备功能吗? [英] Can I override a CUDA host-and-device function with a host-only function?

查看:281
本文介绍了我可以使用仅主机功能覆盖CUDA主机和设备功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆