The Packings of struct between nvcc and g++ Are Different
One of my CUDA programme, which was successfully built by Visual Studio and ran on Windows without any problem, failed to produce the correct output on Mac and Linux. The error lay in the difference on the packing of struct between nvcc and g++. Let me give an example. Suppose that we have three files, which are Entry.h, FillEntry.cu and Main.cpp:
Let's build and launch the executable FillAndPrintEntry by the following commands:
We will find that the fourth line of the output, i.e. the d field, does not print the expected value 0.83. This problem can be solved by using the #pragma pack directive on the definition of Entry to make its packing consistent between nvcc and g++: