Pointer in "complex" structure


I have a structure array defined as follow :

[codebox]typedef struct{

float val1;

int val2;

} InnerStruct;

typedef struct{

float v;

InnerStruct * tab;


MyStruct * mystruct;[/codebox]

I would like to know :

  1. how to allocate memory on the device for mystruct

  2. how to access the structure correctly from the kernel

  3. how to copy the data of mystruct from the device to the host

I tried to do it in several ways, but none seemed to work…

I can’t allocate “tab” in a static way, because it can be huge.

Thanks for helping me !

Since you say mem is huge, check cudaMalloc() for errors. Probably your cudaMalloc() is failing…

If you have 768MB of RAM – it does NOT mean everything is available for your disposal for CUDA.

Some portion probably goes to MetaData that descibes the data and so on…

I’ve got a Tesla C1060, so 4GB of RAM.
And I of course checked all cudaMalloc and memcpy I’ve done, there’s no error.
I also tried with small size, to check the results.

So everything succeed, but when I try to check up : “v” is correct, but the values “val1” and “val2” in “tab” are not.
So I think it’s a pointer problem… Probably I don’t allocate/access/copy properly, meaning that pointers don’t point the good memory ?

When you copy mystruct to GPU, you should also copy the pointers inside it… ( U need to cudaMalloc() for those pointers and copy things from CPU to GPU and make sure u update the pointers with GPU pointers etc…)

Using indexes to an array of structures instead of pointers might be less error-prone.
An index works on both the host and on the GPU.