Struct with jcuda

I want to create an Array of structures with different data types like:

struct vehicle
{
     int id;
     bool forward;
     float speed;
};

How can I create and fill it with Java and copy it to the cuda memory so that i can access it from the kernel.

Hello

Structs are difficult. On the one hand, because there is no real support for structs on Java side, and on the other hand, because there are some specific alignment requirements for structs in CUDA, and it’s hard to get this right even in plain C. I did some experiments to see if and how structs might be supported in JOCL, but not yet in JCuda. Theoretically, it is possible to allocate a large chunk of memory using ByteBuffer#allocateDirect, and fill this byte buffer by alternatingly interpreting it as an IntBuffer and a FloatBuffer and setting the individual enties manually, considering possible paddings that are necessary to meet the alignment requirements. (Even that would be particularly difficult in this case, since the struct involves a ‘bool’, and you can only guess how many bytes a bool occupies on native side…). In any case, it would probably be tedious and error-prone.

As far as I know, by far the most simple solution is to not use an “Array Of Structs”, but a “Struct Of Arrays”, that is, to pass the 'id’s, the ‘forward’ flags and the ‘speed’ values to the kernel as three arrays of ints, ints and floats, respectively.

Sorry, if someone knows an easier way to achieve that, I’d also be glad to heat about it.

bye
Marco