Reputation: 719
I ran across an interesting problem: If I define an array inside my function, it works fine, however if I define the same array outside the function and pass it as a parameter, the array behaves differently.
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]];
}
Upvotes: 1
Views: 76
Reputation: 23438
Array arguments to functions are just pointers. And pointers in OpenCL always refer to a specific memory space. Unfortunately, there's a default, which is global
so that's where your inner function fun
is looking. The array you've declared in main and are passing to fun()
is in the private
address space. For some reason, many (most? all?) implementations don't even generate warnings in this case, so you have to constantly be on guard for this mistake.
The solution is to annotate your function argument with private
. From personal experience, I recommend stating the address space of all pointers in OpenCL explicitly.
Update:
Please note also that as you're using pointers to unsigned short
, word/byte addressing limitations may apply unless your implementation explicitly does not have those limitations. See also https://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_khr_byte_addressable_store.html
If in doubt, switch to unsigned int
and see if that fixes it.
Upvotes: 1