Copy pointer-based objects from host to device

I am trying to copy a data structure from host to device that has pointers to objects that themselves have pointers to other objects. (At the end of the chain of pointers is an array that the kernel on the device would operate on.) However, I am not sure whether this is possible as I encounter segmentation fault when performing the second cudaMalloc (3rd statement below).

// Allocate and copy top object
Param_t dparam;
*)&dparam, sizeof(Param_t)));
CUDA_SAFE_CALL(cudaMemcpy(dparam, param, sizeof(Param_t), cudaMemcpyHostToDevice));
// Now allocate and copy member object
CUDA_SAFE_CALL(cudaMalloc((void**)&dparam->Field1, sizeof(Field_t)));
CUDA_SAFE_CALL(cudaMemcpy(dparam->Field, param->Field1, sizeof(Field_t), cudaMemcpyHostToDevice));

It seg faults at the second cudaMalloc. Is it possible to dereference dparam->Field1 on the host when dparam is allocated for the device with cudaMalloc?

If it cannot be done, then pointer manipulation on the host side like below would not be possible, right?

dparam->Field3 = dparam->Field1 + offset;


You could do it. It’s tricky. Remember you’ve got two types of addresses, device and host. You CANNOT dereference a pointer that’s holding a device address. Which is what you’re doing with “param->Field1”. What you’d do is allocate your structure-of-pointers on the host. Then allocate the pointed-to object on the device. Then write the device addresses into the host memory. Then allocate the structure-of-pointers again, this time on the device. And memcpy your filled-out structure. You can extend this for any number of layers, always using things like “->” operator on host pointers!

Some discussion in this thread: