Memory managment with classes Best practice to allocate/deallocate object

Hi everybody,

If we take a simple class as follows:

class ClassTest


     int size;

     int *tab;


With Cuda, I have not found another way to allocate an object of type ClassTest like this:

// consider an object in the host memory contains data

      ClassTest *h_object;

// temporary pointer on ClassTest object

      ClassTest *tmp_test = new ClassTest();

      tmp_test->size = h_object->size;

      cudaMalloc((void **)&tmp_test->tab, sizeof(int) * tmp_test->size);

      cudaMemcpy(tmp_test->tab, h_object->tab, sizeof(int) * tmp_test->size , cudaMemcpyHostToDevice);

// Pointer on real object cuda

      ClassTest *d_test;

      cudaMalloc((void **)&d_test, sizeof(ClassTest));

      cudaMemcpy(d_test, tmp_test, sizeof(ClassTest), cudaMemcpyHostToDevice);

And this code for deallocate device object :


      delete tmp_test;


So, I wondered if there was a better way to do this?


Yet, nobody as never copy object from host to device memory ?

I generally avoid data structures on the device that contain pointers for this reason. There isn’t an easier way to do this.

Ok, Thanks

And is that the performance is lower with the use of classes like this instead of simple array ?