kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

Memory alignment of custom structs #42

Closed RobbinMarcus closed 7 years ago

RobbinMarcus commented 7 years ago

I was wondering if there is a general accepted solution to create a memory layout that works in C++ CUDA and in C# / ManagedCuda. I'm having trouble with the alignment of structures that contain float4 values. Since the float4 is aligned to 16 bytes in CUDA/C++, the structures usually add padding when there are members smaller than 16 bytes. This padding is not added when Marshalled from C#, thus the sizes of the structs do not match.

You can get more information from this StackOverflow description.

I also made a small project that shows the problem: https://github.com/RobbinMarcus/ManagedCudaCustomStruct

kunzmi commented 7 years ago

I posted an answer on StackOverflow, but the short answer is, that this will be tough to get running automatically.

I had the same troubles with some of the structures of the CUDA API. To manipulate the layout, you'd use the StructLayout options. But still, in some rare cases, I also had to use the explicit layout to get things to work.