c++ - how to use user defined structure in opencl -
i trying explore way use structure in opencl
i first try struct (defined on host)
typedef struct userstruct { cl_int x; cl_int y; cl_int z; cl_int w; } userstruct;
and structure (defined on device)
typedef struct userstruct { int x; int y; int z; int w; } userstruct;
using defined structure, create 2 buffers (para_input , para_output) , init them different values. kernel function copies value para_input para_output.
the example works fine.
but, when add cl_int16 in struct, copying kernel not work. here modified structure:
typedef struct userstruct { cl_int x; cl_int y; cl_int z; cl_int w; cl_int16 vn16; } userstruct;
and structure (defined on device)
typedef struct userstruct { int x; int y; int z; int w; int16 vn16; } userstruct;
is there requirement align structure on both host , device? or popular way use structure in opencl? thanks.
expanding on comment:
it seems problem caused difference in default structure alignment in c compiler , opencl compiler. namely, c compiler packs structure minimum of 80 bytes, while opencl compiler aligns 128 bytes (which thing performance-wise). can match alignment specifying explicitly: either pack both structures, or align both 128 bytes. see opencl docs , compiler's docs (which, probably, uses same notation) details.
in case, recommend going 128 bytes alignment, unless pressured space. declare structures as:
typedef struct userstruct { cl_int x; cl_int y; cl_int z; cl_int w; cl_int16 vn16; } __attribute__ ((aligned (128))) userstruct;
and analogously host one.
as side note, nothing prevents using same structure both host , device code. cl_int
s aliases native types anyway (although explicit alignment specifier still necessary, because structure potentially processed different compilers).
Comments
Post a Comment