GPU struct allocation

Hi,

How to allocate memory for device and copy it from host to device for the following structure?

typedef struct {
  long int *data;
  int length;
  long int total;
} entity;

I just do it in the following way:

entity *gpuEntitys;
int entitySize    = sizeof(entity);
int totalEntitySize = N*entitySize;
cudaMalloc((void**)&gpuEntitys, totalEntitySize);
cudaMemcpy(gpuEntitys, entityHost, totalEntitySize, cudaMemcpyHostToDevice);

When I try to write to the property data, I get an error:
CUDA error at code=77(cudaErrorIllegalAddress)

__global__ void kernel(entity *entitys) {
    entity ent = entitys[index];
    ent.data[1] = 1;
}

Could anyone give a tip?

thanks!

data is a pointer.

when you copy that pointer value from host to device (in the structure), the pointer value that was pointing to a location on the host no longer points to anything meaningful on the device.

You have to do a “deep copy” or else re-allocate the pointer on the device.

There are plenty of worked examples for “deep copy” or copying a pointer embedded in a structure if you look around.

Here is one example:

http://stackoverflow.com/questions/14284964/cuda-how-to-allocate-memory-for-data-member-of-a-class