cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

spectral
Adept II

OpenCL pointer : c++ equivalent ?

Hi,

I have a OpenCL structure like this. But how can I define a C++ structure that correspond to this . First, I don't know the pointer size. Secondly, I don't know how align the structure !

Does someone know ?

Thanks

 

typedef struct { .... } MyInfo; typedef struct { __global MyInfo* info; } Task;

0 Likes
5 Replies
nou
Exemplar

IMHO it is not legal. i mean that only reason why do you want C++ struct is to set it as kernel argument. AFAIK it is not possible to make kernel argument with pointers.

0 Likes

ok it is legal. at least it compile kernel with struct which contain __global pointer.

0 Likes
notzed
Challenger

Originally posted by: viewon01 Hi,

 

I have a OpenCL structure like this. But how can I define a C++ structure that correspond to this . First, I don't know the pointer size. Secondly, I don't know how align the structure !

 

Does someone know ?

 

Can't pass structs with pointers to kernels (well you can, it's just data, but the pointer will be meaningless), so just pass 'info' as a separate argument, or include it's content in Task if it isn't kernel-global.  I'm assuming here Task is a kernel argument.

Should you need to access a lot of objects (e.g. one per thread), accessing arrays of structures is usually inefficient, so you're better off flatting the fields into separate arrays (and/or concatenating them if they are the same type, and/or using the vector types if they are related), each passed as an argument.  This also avoids any questions of alignment.  But it kind of depends on what your'e doing as to whether it's worth it.

 

0 Likes
rick_weber
Adept II

The compiler should handle data alignment for you. As for the structure's size, if you put a pointer in it, it's size will be implementation dependent. That being said, using pointers and expecting them to persist across kernels is dangerous.

On AMD GPUs, memory addresses can map differently across kernel calls which will screw up your data structures. I would recommend using integer offsets instead of pointers, as integers have a predictable size and you can reindex into the buffer they were allocated in at runtime and get the real address.

0 Likes

Thanks,

You simply confirm what I was expecting 🙂

 

0 Likes