I need help understanding data alignment in OpenCL's buffers

风流意气都作罢 提交于 2019-12-06 07:10:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8994219/i-need-help-understanding-data-alignment-in-opencls-buffers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!