I have a Parent
class and an inherited Child
class:
class Parent {};
class Child : public Parent {};
There are a couple child classes that inherit from Parent
, but for simplicity, I only included one. These inherited classes are necessary for the project I am working on. I also have an object from another class, which I wish to copy onto the device:
class CopyClass {
public:
Parent ** par;
};
Note that the Parent ** par;
is there because I need to have a list of Child
objects, but which child it will be using (and the length of the list) is unknown at compile time. Here is my attempt at copying a CopyClass
object onto the device:
int length = 5;
//Instantiate object on the CPU
CopyClass cpuClass;
cpuClass.par = new Parent*[length];
for(int i = 0; i < length; ++i) cpuClass.par[i] = new Child;
//Copy object onto GPU
CopyClass * gpuClass;
cudaMalloc(&gpuClass,sizeof(CopyClass));
cudaMemcpy(gpuClass,&cpuClass,sizeof(CopyClass),cudaMemcpyHostToDevice);
//Copy dynamically allocated variables to GPU
Parent ** d_par;
d_par = new Parent*[length];
for(int i = 0; i < length; ++i) {
cudaMalloc(&d_par[i],sizeof(Child));
printf("\tCopying data\n");
cudaMemcpy(d_par[i],cpuClass.par[i],sizeof(Child),cudaMemcpyHostToDevice);
}
//SIGSEGV returned during following operation
cudaMemcpy(gpuClass->par,d_par,length*sizeof(void*),cudaMemcpyHostToDevice);
I have seen multiple similar problems to this here, here, here, here, and here, but either I couldnt understand the problem they were having, or it didn't seem to fit in with this particular issue.
I know that the segmentation fault I am getting is because gpuClass->par
is on the device, and cudaMemCpy does not allow device pointers. However, I see no other way to "insert" the pointer into the gpuClass
object.
The ways which I could see a solution is to:
1) Flatten my data structure. However, I don't know how to do this with the inherited class functionality that I want.
2) Instantiate gpuClass
originally on the gpu, which I don't know how to do, or
3) I have seen in one of the solutions that you can use cudaMemCpy to copy the address of your dynamically allocated list into an object, but once again, I don't know how to do that (specifically for copying a device pointer to the location of another device pointer).
Any help would be greatly appreciated.