GPU memory pointers in struct type kernel argument


I would like to pass a struct (with GPU device memory pointer) to the OpenCL kernel as an argument.

The struct would be like,

struct MyStruct {

  int size;

  __global float *array;


__kernel void MyKernel(MyStruct input) {

 int s = input.size;


 float value = input.array[idx];



MyStruct has two fields (one for the device memory pointer, and one for the size of the array),

and the kernel has one argument of type MyStruct.

Is it possible to pass this struct from the host?

The problem is that the type of device memory allocated is cl_mem instead of the raw pointers,

so I’m not sure how to store it into MyStruct on the host side.

If this is not possible,

one workaround would be to pass those two fields separately to the kernel,

but I would like to avoid that because the struct could have many fields.

Are they any other solutions for this?

Thanks in advance.

As far as I understand, OpenCL’s concept of memory objects does not approve to store device memory pointers between kernel calls, which also allows that memory objects can be shared between different devices (e.g. CPUs and a GPU) in a single context. So in principle a memory object could be moved around in memory space if the OpenCL runtime decides to do so, e.g. for memory compaction or to swap that object out and in. So for OpenCL 1.0 and 1.1, you will have to put every memory object a kernel relies on into its argument list.

I see. Thanks for the replay.