[英]Copying classes with pointers in CUDA
I'm trying to implement something on CUDA that involves classes which have pointers to another classes inside. 我试图在CUDA上实现一些涉及类的类,这些类具有指向另一个类的指针。 I found out that I need to copy memory for all of those pointers separately and here's my approach
我发现我需要分别为所有这些指针复制内存,这是我的方法
__host__ Production * Production::allocateToDevice() {
Production * productionOnDevice;
size_t productionSize = sizeof(Production);
cudaMalloc((void **) &productionOnDevice, productionSize);
cudaMemcpy(productionOnDevice, this, productionSize, cudaMemcpyHostToDevice);
Vertex * boundRootOnDevice;
cudaMalloc((void **) &boundRootOnDevice, boundRoot->getMemsize());
cudaMemcpy(boundRootOnDevice, boundRoot, boundRoot->getMemsize(), cudaMemcpyHostToDevice);
size_t boundRootPointerSize = sizeof(Vertex *);
cudaMemcpy(&(productionOnDevice->boundRoot), boundRootOnDevice, boundRootPointerSize, cudaMemcpyHostToDevice);
return productionOnDevice;
}
where 哪里
class Production {
Vertex * boundRoot;
public:
Production(Vertex * root);
__host__ Production * allocateToDevice();
__host__ __device__ Vertex * getBoundNode();
__host__ __device__ void execute();
__host__ __device__ size_t getSize();
};
As you can see first I copy the Production object itself, then it's only member Vector and at the end I'm copying the pointer. 如您所见,首先我复制Production对象本身,然后它是成员Vector,最后复制指针。 Yet I'm getting access violation at penultimate line.
但是我在倒数第二行遇到访问冲突。 What am I doing wrong?
我究竟做错了什么?
This: 这个:
cudaMemcpy(&(productionOnDevice->boundRoot), boundRootOnDevice,
boundRootPointerSize, cudaMemcpyHostToDevice);
is illegal because you have passed boundRootOnDevice
(a device pointer) as the source pointer in a host-to-device transfer. 这是非法的,因为您已将
boundRootOnDevice
(设备指针)作为主机到设备传输中的源指针。 This is result in the host attempting to perform pointer indirection on a device address, resulting in segfault. 这导致主机尝试对设备地址执行指针间接访问,从而导致段错误。
Presumably you want to perform a device to device copy in this case. 在这种情况下,大概是要执行设备到设备的复制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.