This question already has an answer here:
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;
}
Then on the host I do:
Production* dProd;
cudaMalloc(&dProd, sizeof(Production *));
createProduction<<<1,1>>>(dProd);
where
__global__ void createProduction(Production * prod) {
prod = new Production();
prod->leftChild = new Vertex();
prod->rightChild = new Vertex();
}
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.
You can't do that.
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 Vertex
instances from the device.
来源:https://stackoverflow.com/questions/35116030/cuda-creating-objects-in-kernel-and-using-them-at-host