Float4 usage



I have been reading about the float4 usage in opencl and why it is to prefer over the normal float in some cases.

I cant figure out how to generate the float4 input in java and use jocl where the kernel argument is float4 data.

Does anyone know how to generate float4 input in java with jocl?





In the simplest case, a float4 is just 4 floats. So when you have a kernel that receives a float4 as one argument, you can just pass in a 4-element float array:
clSetKernelArg(kernel, a++, Sizeof.cl_float4, Pointer.to(new float[] { 1,2,3,4}));

For arrays of (i.e. pointers to) float4, you can analogously create a cl_mem with the desired size, e.g. count * Sizeof.cl_float4.

Of course, handling a float[n*4] array on Java side is not as convenient as handling an array of cl_float4 in C. But that’s hard to avoid. I once created utlities that, to some extent, allow “emulating” float4 as “structs”, at jocl.org - Utilities . But you should not consider this as a stable, universally applicable solution. There may be options to make the handling of something-like-a-float4 in Java more convenient, but these will depend on the application case.




Ok, that simple :slight_smile:

Thanks for the help, Ill try it when I get home