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];

Let me explane. I am working on implementing AES-128 ECB in OpenCL. Here is AES description. These structs(blocks) I used for dividing plaintext into blocks 4x4 bytes. This array of 11 blocks is 11 keys for each round. I did same thing with plaintext. For example, plaintext of 67bytes is divided into 5 blocks. In C this is working very well in sequential execution (key scheduling, subbytes, shift rows, mixcolumns, addround) encryption and decryption. But problem now is that is not simple like that in OpenCL. How can I use array of these blocks in OpenCL? Or do I need to transform everything into 1D array of char (for example)?

boki_bo
  • 80
  • 9
  • Could this question be a duplicate of https://stackoverflow.com/questions/33119233/opencl-using-struct-as-kernel-argument ? – JohnDuck Mar 01 '18 at 21:50
  • It is a bit similar, just here I have array of structs. – boki_bo Mar 01 '18 at 23:42
  • The algorithm you are implementing does not seem relevant to this question. Seems like you are asking several different questions, first of all "How do I pass an array of structs to an OpenCL kernel?". Which I already explained. If that part doesn't make sense then go look at an OpenCL tutorial. Your other questions seem to be how to determine the memory layout of a struct & the endianness of your host and device. Is that right? – JohnDuck Mar 02 '18 at 01:32
  • Yes you are right. Now when I know how to pass an array of structs to device, my question is. How to manipulate with these structs using global and local size on device? How to divide this work on work groups and work items? Can I make one struct as one work group, and 16 work items inside this group? – boki_bo Mar 02 '18 at 08:46
  • About the work size, I don't know. That's a fundamental question that depends on what you're trying to do, the specifics of the compute device, and benchmarking to get good results. – JohnDuck Mar 02 '18 at 18:51

1 Answers1

2

In OpenCL C and OpenCL C++ you can't dynamically allocate memory in a kernel -- no malloc, only placement new, etc. You can indeed make 2D arrays like char item[4][4] and declare structs like Block in your kernels. But since you can't allocate memory if you must have a dynamically sized array then you can do the following things:

  1. Declare an array (of whatever dimension) of automatic storage duration that is sufficiently large for your use. For example declare char item[100][100] if you know you won't need more than a 100x100 array.

  2. Create a buffer on the host with clCreateBuffer and pass it in as a kernel argument.

If you want to build an array of structs on your host and then pass it in to your kernel as a buffer, you can do that too! But you must declare the structs separately in your host source and kernel source, and pay attention to size and alignment characteristics, and byte order. It's on you to make sure bits you pass in from the host are interpreted correctly on your device.

Edit:

To understand the layout of a struct take a look at this question: C struct memory layout?

OpenCL C is based on C, so you can expect the layout of structs to follow the same rules. The sizes of primitive types may differ on your host and device, but OpenCL defines several typedefs like cl_int that you should probably use when declaring a struct on your host to make sure it has the same size as on your device. For example, the size of cl_int on your host will be the same as the size of int on your device.

You can determine the endianness of your device with clGetDeviceInfo using the param_name CL_DEVICE_ENDIAN_LITTLE.

JohnDuck
  • 140
  • 8
  • I forgot to mention that this code is on my host, sorry. I read that I must declare structs on host and device. How to make sure that I passed bits correctly from host to device? – boki_bo Mar 01 '18 at 21:08
  • Please edit your question to illustrate exactly what you are asking, and what you have tried already. – JohnDuck Mar 01 '18 at 21:42