CUDA - 在内核中创建对象并在主机上使用它们 [英] CUDA - Creating objects in kernel and using them at host

查看:126
本文介绍了CUDA - 在内核中创建对象并在主机上使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的内核中使用多态性。唯一的方法是在设备上创建这些对象(在设备上创建一个虚拟的mehod表)。下面是正在创建的对象

I need to use polymorphism in my kernels. The only way of doing this is to create those objects on the device (to make a virtual mehod table available at the device). Here's the object being created

class Production {
    Vertex * boundVertex;
}


class Vertex {
    Vertex * leftChild;
    Vertex * rightChild;
}

然后在主机上执行:

Production* dProd;
cudaMalloc(&dProd, sizeof(Production *));
createProduction<<<1,1>>>(dProd);

其中

__global__ void createProduction(Production * prod) {
    prod = new Production();
    prod->leftChild = new Vertex();
    prod->rightChild = new Vertex();
}


$ b <在设备上回主机?我知道在类中使用指针非常难以处理,但没有其他方法来创建这样的树结构。

The question is how do I get both left and right vertices of the production created on the device back on the host? I know using pointers in classes makes them very hard to handle but... no other way of creating such tree structure.

推荐答案

你不能这样做。

主机运行时和驱动程序内存管理API不能用于访问运行时堆使用 new malloc 。主机无法从设备复制这些 Vertex 实例。

The host runtime and driver memory management APIs can't be used to access allocations made on the runtime heap using new or malloc. There is no way for the host to copy those Vertexinstances from the device.

这篇关于CUDA - 在内核中创建对象并在主机上使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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