我遇到了一个有趣的问题:如果我在我的函数中定义一个数组,它工作正常,但是如果我在函数外部定义相同的数组并将其作为参数传递,则数组的行为会有所不同。
void main()
{
unsigned short arr[8] = {0,1,2,3,4,5,6,7};
fun([...], arr);
}
void fun([...], unsigned short * arr, [...])
{
[...]
unsigned short fun_arr[8] = {0,1,2,3,4,5,6,7};
for(int i = 0; i < 8; i++)
if(arr[i] != fun_arr[i])
printf("Not the same");
//until here it works. the arrays are the same, I get no output, as expected
//this works just fine aswell
float f = some_float_array[fun_arr[0]];
//this works aswell
unsigned short index_from_arr = arr[0];
//this doesnt. The program crashes (-9999, which isnt an actual OpenCL error)
float f = some_float_array[arr[0]];
}
答案 0 :(得分:1)
函数的数组参数只是指针。 OpenCL中的指针始终引用特定的内存空间。不幸的是,默认值为global
,因此您的内部函数fun
正在寻找。您在main中声明并传递到fun()
的数组位于private
地址空间中。出于某种原因,许多(大多数?全部?)实现在这种情况下甚至不会产生警告,所以你必须时刻警惕这个错误。
解决方案是使用private
注释函数参数。根据个人经验,我建议明确说明OpenCL中所有指针的地址空间。
<强>更新强>
请注意,当您使用指向unsigned short
的指针时,除非您的实现明确没有这些限制,否则可能会应用字/字节寻址限制。另请参阅https://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_khr_byte_addressable_store.html
如果有疑问,请切换到unsigned int
并查看是否可以解决问题。