Help with creating structured datatypes with CUDA vectors

34 views
Skip to first unread message

r. keith morley

unread,
Jul 28, 2022, 4:44:09 PM7/28/22
to CuPy User Group

Hello,

I am working on Python bindings for NVIDIA's OptiX ray-tracing API.  One of the necessities of this projects is the ability populate arbitrary CUDA-C structs from the Python side.  A typical workflow is to 
  • create Numpy structured dtypes to represent the array element types
  • create an array of these types,
  • use CUPY to copy arrays of these structs to the GPU or from the GPU

It is a reasonable constraint for these structs to only contain POD members, with one very important exception.  It is very common for these structs to have CUDA vector types as members:

struct Foo
{
    float  A;
    float2 B
}; 

The problem is that the cuda vector types have specific alignment requirements.  They are declared similar to this:

struct __attribute__((aligned(8))) float2 
    float x; 
    float y; 
};

The float2 type requires 8 byte alignment.

The problem arises when using Numpy structured datatypes to create a representation of float2.  Numpy has support for creating aligned structured dtypes with appropriate padding that can be passed to C programs, but it assumes default alignment, meaning that if I create a dtype to represent a float2, it will have alignment of 4 bytes.  As far as I can tell, there is no way to request the alignment for a structured array dtype to be non-default.

I am hoping that other CUPY users have run into this issue and found a workaround.  The only workarounds I have found so far is to manage offsets within a structured dtype manually, but this is not feasible since matching padding and alignment requirements for structs with many members, for various compilers would by highly onerous.  

Any suggestions would be very much appreciated.


Thanks,
keith

Reply all
Reply to author
Forward
0 new messages