如何为结构的OpenCL数组设置正确的对齐方式? [英] How to set the right alignment for an OpenCL array of structs?

查看:265
本文介绍了如何为结构的OpenCL数组设置正确的对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下结构:

C ++:

struct ss{
    cl_float3 pos;
    cl_float value;
    cl_bool moved;
    cl_bool nextMoved;
    cl_int movePriority;
    cl_int nextMovePriority;
    cl_float value2;
    cl_float value3;
    cl_int neighbors[6];
    cl_float3 offsets[6];
    cl_float off1[6];
    cl_float off2[6];
};

OpenCL:

typedef struct{
    float3 nextPos;
    float value;
    bool moved;
    bool nextMoved;
    int movePriority;
    int nextMovePriority;
    float value2;
    float value3;
    int neighbors[6];
    float3 offsets[6];
    float off1[6];
    float off2[6];
} ss;

我有一个这样的数组,然后将它们传递给opencl Buffer,但是当我在内核中使用它们时,数据将被破坏.

I have an array of these, and I pass them to an opencl Buffer, but when I operate with them in a kernel, the data gets corrupted.

我相信这是由于一致,我已经阅读了有关它的其他文章

I believe this is because of the alignment, I have read other posts about it

我需要帮助来了解OpenCL的缓冲区

在OpenCL/CUDA中对齐内存访问

但是,我仍然没有完全了解如何正确地将对齐方式设置为我的结构.另外,我也不完全了解属性对齐和打包的限定词.

But still, I don't fully get the idea of how to properly set the alignment to my struct. Also, I dont fully understand the attribute aligned and packed qualifiers.

所以:

Q1.您能告诉我如何调整我的结构以使其正常工作吗?

Q1. Could you show me how to align my struct to work properly?

Q2.您能解释一下还是给我一些链接,以了解所有对齐问题和限定词?

Q2. Could you explain me or give me some link to understand all the alignment problem and the qualifiers?

感谢.

推荐答案

我建议声明您的结构,从最宽的类型到最窄的类型.首先,这避免了由于对齐而浪费未使用的空间.其次,这通常可以避免在不同设备上使用不同对齐方式时会头疼.

I recommend declaring your structure from the widest types first down to the narrowest types. First, this avoids wasted unused spaces due to alignment. Second, this often avoids any headaches with different alignments on different devices.

所以

struct ss{
    cl_float3 pos;
    cl_float3 offsets[6];
    cl_float value;
    cl_float value2;
    cl_float value3;
    cl_float off1[6];
    cl_float off2[6];
    cl_int movePriority;
    cl_int nextMovePriority;
    cl_int neighbors[6];
    cl_bool moved;
    cl_bool nextMoved;
};

还要提防float3类型;它通常是GPU上的float4,如果主机端布局也没有这样做,那么您的对齐方式将不可用.您可以切换到float4以避免这种情况.

Also, beware of the float3 type; it is often a float4 on the GPU and if the host-side layout doesn't also do that, then your alignment will be off. You could switch to float4 to avoid this.

这篇关于如何为结构的OpenCL数组设置正确的对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆