Packing structs for OpenCL

gpuarrays

#1

I’m trying to use GPUArrays to broadcast on types that pack differently in Jullia and OpenCL, such as arrays of structs/tuples or structs containing tuples.

Is this a bug in CLArrays that it doesn’t ‘just work’, or intended behaviour? Can I manually pack the struct in Julia to match OpenCL requirements? or do I need to build the struct in C? It would best if the conversion could be abstracted and automated within GPUArrays.

I’m trying to write generic kernels for OpenCL, CUDA and vanilla Julia as much as possible.

ERROR:     Julia and OpenCL type don't match at kernel argument 5: Found Tuple{GPUArrays.BInfo{Array,2},GPUArrays.BInfo{Any,0},GPUArrays.BInfo{Array,2},GPUArrays.BInfo{Any,0}}. 
    Please make sure to define OpenCL structs correctly!
    You should be generally fine by using `__attribute__((packed))`, but sometimes the alignment of fields is different from Julia.
    Consider the following example:
        ```
        //packed
        // Tuple{NTuple{3, Float32}, Void, Float32}
        struct __attribute__((packed)) Test{
            float3 f1;
            int f2; // empty type gets replaced with Int32 (no empty types allowed in OpenCL)
            // you might need to define the alignement of fields to match julia's layout
            float f3; // for the types used here the alignement matches though!
        };
        // this is a case where Julia and OpenCL packed alignment would differ, so we need to specify it explicitely
        // Tuple{Int64, Int32}
        struct __attribute__((packed)) Test2{
        };
        // this is a case where Julia and OpenCL packed alignment would differ, so we need to specify it explicitely
        // Tuple{Int64, Int32}
        struct __attribute__((packed)) Test2{
            long f1;
            int __attribute__((aligned (8))) f2; // opencl would align this to 4 in packed layout, while Julia uses 8! 
        };
        ```
    You can use `c.datatype_align(T)` to figure out the alignment of a Julia type!

#2

Issue is discussed at: https://github.com/JuliaGPU/CLArrays.jl/issues/38#issuecomment-407058050