casting char pointer to struct pointer in cuda Cuda gpu programming

Hi,

while excuting a small program I got the following error:

cudaSafeCall() Runtime API error : invalid argument.

When I worked around I found out that, I am not able to cast char pointer to a struct pointer !!

my struct is

struct data {

unsigned int NDatas : 6;

unsigned int Dummy: 2;

unsigned int p1 : 21;

unsigned int p2 : 1;

unsigned int x1 : 1;

unsigned int x2 : 1;

}

the variable NDatas tells that still NDatas bytes are to be read before jumping to the next struct.

I tried all the following instructions but no way in gpu, even though it works in CPU

globel test(char*prt)

{

while (size< sizeOfframe)

{ h=( data*)(&prt[0]+size);

h=( data*)(prt+size);

h=( data*)(&prt);

//or

h=reinterpret_cast<Data*>(prt+size);

or

memcpy(h,prt,sizeof(Data)+size);

size+=data->NDatas+sizeof(data)

}

}

Iwas just able to read the first struct !!!.

I write a debug program in cpu and it is working fine.

any clues

The problem probably is that the struct would need to be properly aligned, which it probably isn’t. CPUs nowadays carry extra logic to allow for misaligned accesses, but the GPU (so far) doesn’t.

thank for responding me tera, as fare as I know it about alignement, the structure should be multiple of 2bytes or 4bytes, which is my case, because the size of my structure is 4bytes.

i counted 6 unsigned ints which is if i remember correctly 24 bytes

could you tell me how did you count them ?

my struct is
struct data {
unsigned int NDatas : 6;
unsigned int Dummy: 2;
unsigned int p1 : 21;
unsigned int p2 : 1;
unsigned int x1 : 1;
unsigned int x2 : 1;
}
those are not 6 unsigned ints?
Also shouldn’t the : sign be = ?
Am i missing something here?

He’s using bitfields there (the ‘:’ means that the unsigned int needs to only take up a specific number of bits), so there are actually (6 + 2 + 21 + 1 + 1 + 1) = 32 bits = 1 unsigned int.

Still I think that tera is right about the misalignment problem. If ‘size’ the in the example is not 4-byte aligned, this will probably silently fail.