2D数组作为OpenCL内核参数 [英] 2D Array as OpenCL kernel argument

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

问题描述

一个简单直接的问题:如何将2D数组用作OpenCL内核参数?

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

常识建议使用

__ kernel void main(__ global< datatype> **< name>)

但是,编译器似乎对此想法并不感到开心:

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

内核参数不能声明为指向指针的指针.

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

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

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

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

vector< vector< Element>>

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

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

推荐答案

您需要将2D数组缩小为1D数组.

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

主机:

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.
    /*...*/
}

如果您的2D数组没有像我的示例所暗示的那样连续地存储在内存中,则需要滚动自己的函数以确保将整个内存连续地存储在单个堆分配的对象中.

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.

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

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