OpenCL中不允许使用可变长度数组声明-为什么? [英] variable length array declaration not allowed in OpenCL - why?

查看:900
本文介绍了OpenCL中不允许使用可变长度数组声明-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的OpenCL内核中创建一个本地数组,其大小取决于内核的参数.看来这是不允许的-至少在AMD APP中是这样.

I want to create a local array inside my OpenCL kernel, whose size depends on a parameter of the kernel. It seems that's not allowed - at least with AMD APP.

您的经历与众不同吗?也许只是APP?还是这里有一些理由?

Is your experience different? Perhaps it's just the APP? Or is is there some rationale here?

现在,我建议在CPU端代码中也应允许使用可变长度数组,这是C标准委员会的不幸选择;但问题仍然存在.

I would now suggest variable length arrays should be allowed in CPU-side code too, and it was an unfortunate call by the C standard committee; but the question stands.

推荐答案

您可以动态分配本地块的大小.您需要将其作为内核的参数,并在调用clSetKernelArg时定义其大小.

You can dynamically allocate the size of a local block. You need to take it as a parameter to your kernel, and define its size when you call clSetKernelArg.

定义示例:

__kernel void kernelName(__local float* myLocalFloats, ...)

主机代码:

clSetKernelArg(kernel, 0, myLocalFloatCount * sizeof(float), NULL); // <-- set the size to the correct number of bytes to allocate, but use NULL for the data.

在执行此操作之前,请确保您知道设备上的本地内存限制是多少.调用clGetDeviceInfo,然后轮询"CL_DEVICE_LOCAL_MEM_SIZE"值.

Make sure you know what the limit for local memory is on your device before you do this. Call clGetDeviceInfo, and poll for the 'CL_DEVICE_LOCAL_MEM_SIZE' value.

这篇关于OpenCL中不允许使用可变长度数组声明-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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