How to pass/return 2-dimensional arrays in a kernel?

Hi All,

I seem to understand I cannot pass down/return 2-dimensional arrays in OpenCL (declared smt like float** for example in the kernel signature).

Is there a way to work around this? Can I for example pass-down/return an array of float4 vectors?

Just to give a bit of background, the issue here is that I cannot flatten this data out into separate buffers because it’s dynamically determined by one of the other input parameters.

Any help/suggestions appreciated (examples welcome too)!

I seem to understand I cannot pass down/return 2-dimensional arrays in OpenCL (declared smt like float** for example in the kernel signature).

Right. It is disallowed in section 6.8.

Can I for example pass-down/return an array of float4 vectors?

Sure you can. You can also pass arrays of structs.

Just to give a bit of background, the issue here is that I cannot flatten this data out into separate buffers because it’s dynamically determined by one of the other input parameters.

I don’t understand this part. Data can always be flattened. In the very worst case you would need to pass two arguments: one that contains flattened data and another one that is an array of indices into that flattened data.

Hi David, first of all thanks for your prompt reply.

Sure you can. You can also pass arrays of structs.

you mean I can define a struct and pass it down as a kernel parameter? Sweet, I didn’t know (can you point me to any example?).

Data can always be flattened. In the very worst case you would need to pass two arguments: one that contains flattened data and another one that is an array of indices into that flattened data.

I see your point, I didn’t make myself clear - I meant I cannot flatten it as separate buffers, I would like to avoid having to do that kind of “manual” flattening if I can get away with a float4 array (and in my case I can), but definitely doable.

Thanks for your help!

you mean I can define a struct and pass it down as a kernel parameter? Sweet, I didn’t know (can you point me to any example?).

Yes, it’s just like passing any other pointer to a kernel. Be careful with alignment as it may be different on the host and the device.