非常简单的问题:如何将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.
更多推荐
发布评论