Pointer Arithmetic with OpenCL "cl_mem"

Can we do simple pointer arithmetic with OpenCL “cl_mem” buffer handler? Like:

cl_mem obj1;

...

obj1=obj1+10; // Does not work as it gives following error:

error: invalid use of incomplete type ‘struct _cl_mem’

This is easy doable in CUDA, I guess as device handler is like regular pointer but how can I do it in OpenCL?

[quote name=‘enliten’ date=‘17 January 2011 - 04:35 PM’ timestamp=‘1295278537’ post=‘1178421’]

Can we do simple pointer arithmetic with OpenCL “cl_mem” buffer handler? Like:

cl_mem obj1;

...

obj1=obj1+10; // Does not work as it gives following error:

error: invalid use of incomplete type ‘struct _cl_mem’

Try obj1 = obj1 + (cl_mem)10

“10” is an int. cl_mem could be an struct(in linux) or int in windows. Probably, you need an explicit cast.

In The OpenCL 1.1 specification, i saw a function which is called “clCreateSubBuffer”. I think it makes what you want to do but i have never been able to test it since i wasn’t allowed to install beta Nvidia’s drivers for 1.1.

Explicit casting of cl_mem to any other type is probably a very bad idea. It is inherently not portable and will probably not do what you want anyway.

You are right as I want something more flexible and generic. Using clCreateSubBuffer can be an option but I am having problem with using it. Maybe, I am missing something:

cl_buffer_region *info= new cl_buffer_region();

info->size= (size_t)8;//in bytes for float

info->origin=(size_t)0; // in bytes for float

newBuffer = clCreateSubBuffer (newDevicePointer,CL_MEM_READ_WRITE, CL_BUFFER_CREATE_TYPE_REGION, info, NULL); //Segmentation fault

Last line gives me segmentation fault and I don’t know why and there is no help about it out there. I have ensured that parent buffer is not a subbuffer and other constraints but still, no clue.

Have someone used clCreateSubBuffer or know about this problems?

Usman.

clCreateSubBuffer is an addition to the OpenCL 1.1 specification. I don’t think it will work on NVIDIA’s current toolkit/driver releases.

Err… you do realise in OpenCL you allocate a HANDLE to an opaque struct that contains important stuff for the OpenCL runtime (like what GPU and CL runtime you have loaded). Thus you cannot do any host side pointer arithmetic like you can in CUDA.

You must use the OpenCL APIs to work with cl_mem!