How to pass 16-bit floats (cl halfs) to an OpenCL kernel in .cl file? I have in my .cl file:
__kernel void func(__global half* a, __global half* b, __global half* res){
int i = get_global_id(0);
res[i] = a[i] - b[i];
}
I want to pass a and b in C file and treat them as floating point values. I tried using cl_half, but it's an unsigned short, so something like:
cl_half aData[1];
aData[0] = -0.005;
won't really work, because aData[0] gets rounded to 0. How to do it properly?
On the GPU side, in OpenCL C, you treat the numbers as
half, that part is correct. OpenCL C supports thehalfdata type and arithmetic with it. It's better to use thehalf2vector type though, as only then you get the 2x FP16 arithmetic throughput of GPUs.On the CPU side, you treat the numbers as
unsigned short(this is whatcl_halfis under the hood), as they are only 16 bits in size and there is nohalfdata type in C/C++. You can't just write 32-bit FP32 literals like-0.005finto there, as they will get casted to 16-bit integer. You need properfloat<->halfconversion. Then, to write numbers:And to read the data again:
Note that on the CPU, arithmetic with the
ushort/cl_halfis not supported, and you always have to first convert the numbers tofloat.