I have a class such as this:
class CudaArray
{
CudaArray() : Ptr(new double[5]) {}
double* Ptr;
int Dimension;
}
and then another class such as this:
class Container
{
short a;
CudaArray* ArrayPtr;
int b;
int c;
}
Right now I m creating the Array on device in this way:
CudaArray H_Array;
CudaArray* D_Array;
Check(cudaMalloc(&D_Array, sizeof(CudaArray)));
Check(cudaMemcpy(D_Array, &H_Array, sizeof(CudaArray), cudaMemcpyHostToDevice));
double* Tmp;
Check(cudaMalloc(&Tmp, sizeof(double) * 5));
Check(cudaMemcpy(Tmp, H_Array.Ptr, sizeof(double) * 5, cudaMemcpyHostToDevice));
Check(cudaMemcpy(&(D_Array->Ptr), &Tmp, sizeof(double*), cudaMemcpyHostToDevice));
I want to be able to use an object of type Container
on device code, and I m having trouble initializing the CudaArray
member from an existing array. So far i tried:
Container* Cont = nullptr;
Check(cudaMalloc(&Cont , sizeof(Container)));
Check(cudaMemcpy(&(Cont->ArrayPtr), &D_Array, sizeof(CudaArray*), cudaMemcpyDeviceToDevice));
But I get GPUassert: invalid argument
on the last cudaMemcpy.
How can I initialize a device class that contains a pointer to an existing object(class) in device memory?
And also, is there a simpler or more elegant way to copy complex objects between host and device?