How to use struct in OpenCL


#1

How can I use my custom struct in OpenCL? Because there are no array of objects in OpenCL, or 2D array beside image.

struct Block {
char item[4][4];
};

I would like to use array of these structs in OpenCL and access its elements by indices like in C/C++. For example

Block *keys = new Block[11];
keys[3].item[2][2];

#2

You should declare this struct in the OpenCL C code as well. There are usually caveats related to struct alignment, but with your simple structure it should just work.

__kernel void k(__global Block* array){}

#3

I read that there can be some problems with bits and bytes with custom data types. Is it better to use array of this custom struct Block or make 1D array of chars with length 16*number of blocks?


#4

It won’t be a problem since you struct is 16-aligned. It could be if you used different data types like char3 and arrays of shorts, but in your case it is unlikely.