copying a variable from host memory to device memory

I want to copy a variable from host memory to device memory which is not stored in array it is a single variable can you specify how to copy and read it back from the device memory

You can set an integer or a float variable simple as a parameter with setKernelArgs, like you set the pointer to array. However, i noticed, there can be issues if you try to set the byte of char value (1 byte variable), just use 4 byte value instead. You can explicit put the variable into constant memory.
Another thing, you can import a constant value to a Kernel via MACRO .
The openCL support common C- macro syntax. And you can set both string or digital value Macros by creating and building operators, look in your OpenCl documentation for that. In my project, i can even customyze a type of variables, for example


_kernel blablabla (TYPE* inputBuffer)

In Host you can set a Macro (“TYPE”,“uint”), and that will work fine.

Of course, you can export a variable as an array with one element, but that is kinda silly…

Since you want to copy the result back to host memory, you will have to store the value in a cl_mem object. You will have to allocate an array of one element and an OpenCL buffer with enough bytes for one element. Sadly, this is the only way.

Can’t we do this as we do in C language pass the address of the variable and then what changes we do inside the kernel; they will automatically change the value of the variable and we can get the new value in the host function

You can use the COPY_HOST_PTR Memory Object initiailizer to pass the pointer of yor variable to the device. This will copy your already allocated memory to the device. I’m not shure if this works, but hopefully mapping that buffer again will give you the same adress. Or you initialize a buffer with ALLOC_HOST_PTR and use that Pointer. It should always stay the same for each mapping.
But no matter what, you will always have to mat/unmap that memory, to have a correct sync.

Google for pinned memory for some more informations about the alloc example.