CUDA内核可以是虚函数吗? [英] Can CUDA kernels be virtual functions?

查看:495
本文介绍了CUDA内核可以是虚函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是相当直截了当的,但让我概述我的框架。我有一个抽象类 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

我找不到任何资源说内核不能是虚函数,是问题。你能解释一下背后的理由吗? 我清楚地了解如何以及为什么 __ device __ 函数不能是虚函数(虚函数是指向存储在对象中的实际[host]函数的指针,并且不能调用这样的函数函数从设备代码),但我不确定 __全局__ 函数。

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.

:我所提出的问题的一部分是错误的。请查看注释以了解原因。

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内核可以是虚函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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