OpenCL内核参数 [英] OpenCL kernel arguments

查看:240
本文介绍了OpenCL内核参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始摆弄OpenCL,遇到一个问题:我不知道如何将复杂的数据结构作为参数传递.我正在使用LWJGL的OpenCL绑定,以及Wiki http://lwjgl中提供的示例. org/wiki/index.php?title = Sum_Example .在该示例中,创建了2个浮点缓冲区并将其作为参数传递(LWGJL在名为 BufferUtils的类中提供方法创建这些缓冲区).

I've just started fiddling around with OpenCL and I've come across a problem: I do not know how to pass complex data structures as arguments. I'm using LWJGL's OpenCL binding, and the example provided in the wiki http://lwjgl.org/wiki/index.php?title=Sum_Example. In that example 2 float buffers are created and passed as arguments (LWGJL provides methods in a class named BufferUtils for creating these buffers).

现在,我一般如何创建点,typedef struct {int x, int y} tpoint或结构的缓冲区? Java中没有结构.此外,没有BufferUtils.createStructBuffer方法.

Now, how would I create a buffer of points, typedef struct {int x, int y} tpoint, or structs in general? There are no structs in Java. Moreover there is no BufferUtils.createStructBuffer method.

推荐答案

在主机上读写OpenCL结构可能很简单.它们的内存布局可能取决于OpenCL设备的体系结构,并且字节序不一定是主机的.

Reading and writing OpenCL structs on the host can be non-trivial. Their in-memory layout can depend on the OpenCL device's architecture, and their endianness is not necessarily the host's.

您可以使用packed属性控制布局.如果使用packed,这将精确指定结构成员的布局,而不是默认设置,后者将根据OpenCL设备的体系结构对齐成员.但是请注意,使用packed可能会导致性能下降.

You can control the layout by using the packed attribute. If you use packed, this specifies exactly the layout of the struct's members, as opposed to the default which will align the members depending on the OpenCL device's architecture. Note, however, that with packed you may lose performance.

或者,您也可以使用小内核为结构布局询问" OpenCL设备:

Alternatively, you can also 'ask' the OpenCL device for the struct layout with a small kernel:

kernel void struct_layout(global unsigned long* totalSize, global unsigned long* fieldOffsets)
{
    tpoint theStruct;
    totalSize[0] = sizeof(theStruct);
    unsigned long baseOffset = (unsigned long)&theStruct;
    fieldOffsets[0] = (unsigned long)&theStruct.x - baseOffset;
    fieldOffsets[1] = (unsigned long)&theStruct.y - baseOffset;
}

对于您的特殊情况,如果您打包了tpoint结构,则可以从Java中使用字节缓冲区并对其进行读/写int,然后在每个结构值的xy成员之间交替.

For your particular case, If you packed the tpoint struct then from Java you could just use a byte buffer and read/write ints to it and alternate between the x and y members of each struct value.

ByteBuffer buf = ...;

int x1, y1 = ...;
int x2, y2 = ...;

buf.putInt(x1).putInt(y1);
buf.putInt(x2).putInt(y2);

这篇关于OpenCL内核参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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