如何将向量参数传递给 C 中的 OpenCL 内核? [英] How to pass vector parameter to OpenCL kernel in C?

查看:99
本文介绍了如何将向量参数传递给 C 中的 OpenCL 内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将向量类型 (uint8) 参数从 C 中的主机代码传递给 OpenCL 内核函数.

I'm having trouble passing a vector type (uint8) parameter to an OpenCL kernel function from the host code in C.

在主机中,我将数据放在一个数组中:

In the host I've got the data in an array:

cl_uint dataArr[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };

(我的真实数据不仅仅是[1, 8];这只是为了便于解释.)

(My real data is more than just [1, 8]; this is just for ease of explanation.)

然后我将数据传输到缓冲区以传递给内核:

I then transfer the data over to a buffer to be passed to the kernel:

cl_mem kernelInputData = clCreateBuffer(context,
    CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_uint)*8, dataArr, NULL);

接下来,我将此缓冲区传递给内核:

Next, I pass this buffer into the kernel:

clSetKernelArg(kernel, 0, sizeof(cl_mem), &kernelInputData);

内核函数的签名看起来像这样:

And the kernel function's signature looks something like this:

kernel void kernelFunction(constant uint8 *vectorPtr)

然而,内核似乎没有从指向 kernelInputData 的指针获取正确的输入数据.当我从内核内部传回值时,我看到 vectorPtr 指向具有以下结构的内容: ( 1, 2, 3, 4, 5, ?, ?, ? ) 其中问号通常 4293848814 但有时0.无论哪种方式,都不是它们应有的样子.

However, the kernel doesn't seem to be obtaining the correct input data from the pointer to kernelInputData. When I pass values back from within the kernel, I see that vectorPtr points to something with this structure: ( 1, 2, 3, 4, 5, ?, ?, ? ) where the question marks are usually 4293848814 but sometimes 0. Either way, not what they're supposed to be.

我做错了什么?

我已经在主机端从使用数组切换到 cl_uint8.我现在有:

I've switched from using an array to cl_uint8 on the host side. I now have:

cl_uint8 dataVector = { 1, 2, 3, 4, 5, 6, 7, 8 };

我像这样将这个向量传递给内核:

And I pass this vector to the kernel like so:

clSetKernelArg(kernel, 0, sizeof(cl_uint8), &dataVector);

内核函数的签名看起来像这样:

And the kernel function's signature looks something like this:

kernel void kernelFunction(constant uint8 *vectorPtr)

但是,运行此代码会在 clSetKernelArg() 上给我一个 CL_INVALID_ARG_SIZE 错误.如果我将 ARG_SIZE 参数切换为 sizeof(cl_uint8 *),此错误就会消失,但随后在 __dynamic_cast<中出现 EXC_BAD_ACCESS 错误/code> 在 clSetKernelArg() 中.

However, running this code gives me a CL_INVALID_ARG_SIZE error on clSetKernelArg(). This error goes away if I switch the ARG_SIZE paramater to sizeof(cl_uint8 *) but then I get an EXC_BAD_ACCESS error in __dynamic_cast within clSetKernelArg().

我的设备是:

Apple Macbook Pro(2009 年中)
OSX 10.8 山狮
NVIDIA GeForce 9400M
OpenCL 1.0
CLH 1.0

Apple Macbook Pro (mid-2009)
OSX 10.8 Mountain Lion
NVIDIA GeForce 9400M
OpenCL 1.0
CLH 1.0

推荐答案

您正在定义一个大小为 8 的 cl_uint 数组.cl_mem 的创建和内核参数的设置是正确的.但是您的内核参数不正确:您尝试读取 cl_uint8 而不是 cl_uint 的数组.

You are defining an array of cl_uint of size 8. The creation of the cl_mem and the setting of kernel argument are right. But your kernel argument isn't correct: you try to read an array of cl_uint8 instead of cl_uint.

如果要使用向量数据类型,必须声明:cl_uint8 dataArr,大小为1.或者,如果您想使用大小为 8 的数组:kernel void kernelFunction(constant uint *vectorPtr, uint size):

If you want to use a vector data type, you must declare: cl_uint8 dataArr of size 1. Or if you want to use an array of size 8: kernel void kernelFunction(constant uint *vectorPtr, uint size):

cl_uint8 dataVector 的内核参数不是指针.所以,正确的代码是:

The kernel parameter for cl_uint8 dataVector is not a pointer. So, the correct code is:

cl_uint8 dataVector = { 1, 2, 3, 4, 5, 6, 7, 8 };
clSetKernelArg(kernel, 0, sizeof(cl_uint8), &dataVector);

kernel void kernelFunction(constant uint8 vectorPtr)

这篇关于如何将向量参数传递给 C 中的 OpenCL 内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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