2D Array作为OpenCL内核参数(2D Array as OpenCL kernel argument)

编程入门 行业动态 更新时间:2024-10-07 06:39:30
2D Array作为OpenCL内核参数(2D Array as OpenCL kernel argument)

非常简单的问题:如何将2D数组用作OpenCL内核参数?

常识建议使用

__kernel void main(__global <datatype> **<name>) ,

然而编译器似乎并没有被这个想法感到相当愉快:

kernel parameter cannot be declared as a pointer to a pointer 。

我是否在监督显而易见的事情,或者究竟是什么,我在这里做错了?

编辑:

主机(c ++)数据结构如下所示:

vector<vector<Element>> ,

其中Element是一个包含同一数组内子节点索引的结构体。 基本指针。

Pretty straight forward question: How would one use a 2D array as an OpenCL kernel argument?

Common sense suggests using

__kernel void main(__global <datatype> **<name>),

however the compiler doesn't seem to be quite amused by this idea:

kernel parameter cannot be declared as a pointer to a pointer.

Am I overseeing the obvious, or what exactly is it, I am doing wrong here?

Edit:

The hosts (c++) datastructure looks like this:

vector<vector<Element>>,

where Element is a struct containing the indexes of the child nodes inside the very same array. Basicly pointers.

最满意答案

您需要将2D阵列缩小为一维阵列。

主办:

int array[50][50]; int * ptr_to_array_data = array[0]; int width = 50, height = 50; cl_mem device_array = clCreateBuffer(/*context*/, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 50 * 50 * sizeof(int), ptr_to_array_data, /*&err*/); clSetKernelArg(/*kernel*/, 0, sizeof(cl_mem), &device_array); clSetKernelArg(/*kernel*/, 1, sizeof(cl_int), &width); clSetKernelArg(/*kernel*/, 2, sizeof(cl_int), &height);

设备:

kernel function(global int * array, int width, int height) { int id = get_global_id(0); int our_value = array[id]; int x = id % width; //This will depend on how the memory is laid out in the 2d array. int y = id / width; //If it's not row-major, then you'll need to flip these two statements. /*...*/ }

如果你的二维数组没有像我的例子所暗示的那样连续地存储在内存中,你需要推出你自己的函数来确保整个内存连续存储在一个堆分配对象中。

You need to reduce the 2D array down into a 1D array.

Host:

int array[50][50]; int * ptr_to_array_data = array[0]; int width = 50, height = 50; cl_mem device_array = clCreateBuffer(/*context*/, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 50 * 50 * sizeof(int), ptr_to_array_data, /*&err*/); clSetKernelArg(/*kernel*/, 0, sizeof(cl_mem), &device_array); clSetKernelArg(/*kernel*/, 1, sizeof(cl_int), &width); clSetKernelArg(/*kernel*/, 2, sizeof(cl_int), &height);

Device:

kernel function(global int * array, int width, int height) { int id = get_global_id(0); int our_value = array[id]; int x = id % width; //This will depend on how the memory is laid out in the 2d array. int y = id / width; //If it's not row-major, then you'll need to flip these two statements. /*...*/ }

If your 2D array is not stored contiguously in memory like my example implies, you'll need to roll your own function to make sure that the entire memory is stored contiguously in a single heap-allocated object.

更多推荐

本文发布于:2023-07-09 10:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内核   参数   OpenCL   Array   kernel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!