Nested Arrays cudaMemcpy of nested arrays

Hey all,

In my host code I have an array of structs that themselves contain an array of another type of struct. The last thing that I tried was:


List* list_D;

List* list_H;

int numItems = fillList(list_H);

cutilSafeCall(cudaMalloc((void**) &list_D, sizeof(List)*numItems));

cutilSafeCall(cudaMemcpy(list_D, list_H, sizeof(List)*numItems, cudaMemcpyHostToDevice));

for(int i = 0; i < numItems; i++)


cutilSafeCall(cudaMalloc((void**) &list_D[i].pList, sizeof(Point)*list_H[i].numPoints));

cutilSafeCall(cudaMemcpy(list_D[i].pList, list_H[i].pList, sizeof(Point)*list_H[i].numPoints, cudaMemcpyHostToDevice));



This code works in emulation mode but does not work on the device (I can’t wait for Nexus). I have also tried to copy the whole array in one call by changing the allocation and memcpy sizes but in my mind that shouldn’t work (which it didn’t) because the memory region won’t be contiguous. What is the best way of transfering nested arrays to device memory?


In the parent object, you are copying (wild) pointers to host memory rather than to the soon to be allocated memory on the device.

I think that makes sense. I am still having trouble deciding how to remedy that problem though. Other non array members of the parent object are being transferred to the device properly, why doesn’t the calls to cudaMalloc put a proper pointer in the array. Could you please give more details about what you think the problem is.


Reverse the order between allocation of child/parent objects and use list_H to store the new pointers before copying them to list_D.

cudaMalloc() will not modify the objects on the device.

OK, now I understand. It was really quite obvious after you said it. I got the code to work by switching the order which made the device pointers get passed to the machine properly.

Thanks for your help.