本文介绍了我需要帮助来了解OpenCL缓冲区中的数据对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下结构

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.

我知道写三个浮点数后必须在缓冲区中添加4个字节才能获得下一个2的幂(16个字节),但是我不明白为什么在写之前必须额外添加16个字节position1的字节.否则,我会在position1中获得错误的值.

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?

推荐答案

float8是8个float的向量,每个float为4个字节.大小为32个字节.根据 OpenCL 1.2规范的第6.1.5节,对齐在类型方面,类型始终与其大小对齐;因此float8必须对齐32个字节.同一部分还告诉我们float3需要4个字.同样,由于结构的sizeof安排为允许结构的数组,因此不会因为对这些特定字段进行重新排序而缩水.在更复杂的结构上,可以通过将较小的字段放在一起来节省空间.

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.

这篇关于我需要帮助来了解OpenCL缓冲区中的数据对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:11
查看更多