Memory Alignment - Xorgon/DEMOranges GitHub Wiki

The host and the device both have copies of any data that you send (as an example, this function copies an array of particles to the device). The problem is that the compiled code on the host and the compiled code on the device may not have the same default system for dealing with memory. So the host may decide to arrange the memory differently to the device. But when the host then passes memory to the device it's passed in whatever format the host decided on and if the device doesn't do it the same way then it can't correctly deal with that data.

In OpenCL the way to ensure that it's all correct is to put the biggest types at the beginning of the struct declaration and then go in descending order (e.g. in particle it goes float3 > ulong > float), and then align it to memory boundaries. Less specific stuff may work, but this is the only way I found to do it repeatably. (Here's my source on the size order thing: https://stackoverflow.com/a/17374931/5270376)

The way I test if the alignment is correct is in test_alignment. The code in there essentially just creates the struct to be tested, fills it with specific data, passes it to the device, the device checks that the data is correct, fills it with different specific data, passes it back, and the host checks that the data is correct. If you add to a struct you'd need to add data for that to the relevant test code. The particle struct alignment tests are here, for example: https://github.com/Xorgon/DEMOranges/blob/master/tests/test_alignment/test_alignment.c#L9 https://github.com/Xorgon/DEMOranges/blob/master/tests/test_alignment/alignment_test_kernels.cl#L1