3

Given the following structure

typedef struct
{
   float3 position;
   float8 position1;
} MyStruct;

I'm creating a buffer to pass it as a pointer to the kernel the buffer will have the previous buffer format.

I understand that I've to add 4 bytes in the buffer after writing the three floats to get the next power of two (16 bytes) but I don't understand why I've to add another 16 bytes extra before writing the bytes of position1. Otherwise I get wrong values in position1.

Can someone explain me why?

Michelle
  • 601
  • 1
  • 8
  • 17

1 Answers1

5

A float8 is a vector of 8 floats, each float being 4 bytes. That makes a size of 32 bytes. As per section 6.1.5 of the OpenCL 1.2 specification, Alignment of Types, types are always aligned to their size; so the float8 must be 32 byte aligned. The same section also tells us that float3 takes 4 words. Also, since the sizeof for a struct is arranged to allow arrays of the struct, it won't shrink from reordering these particular fields. On more complex structs you can save space by keeping the smaller fields together.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • 3
    Hi @Yann, Thank you for your quick response but now that you gave me that explanation I've a doubt: When you say that float8 is 32 bytes alignment it means that in the buffer it must be located in any offset multiple of 32 bytes? – Michelle Jan 24 '12 at 21:44
  • 4
    Yes. And that alignment requirement applies for the struct and buffer also, which is why sizeof(struct MyStruct) is 64, not 48. – Yann Vernier Jan 25 '12 at 07:43