confusing duplication

Hi all,

To create and load a memory buffer I call the following functions:

clInputBuffer := clCreateBuffer(clContext, CL_MEM_READ_WRITE or CL_MEM_USE_HOST_PTR, 4 {sizeof(cl_uint)}720576, src.bits, errNum);
errNum := clEnqueueWriteBuffer(clCommandQueue, clInputBuffer, CL_TRUE, 0, 720576{sizeof(cl_uint)}4, src.bits, 0, NULL, NULL);

why must the host pointer be initialised twice.? It works correctly but the duplication makes me think I am doing something wrong.

Any info on the logic would be helpfull helpfull.


If you are creating the buffer to with CL_MEM_USE_HOST_PTR then it will reference the host pointer and you do not need to write it again. (You do need to be careful about changing/freeing the host pointer, though.)

If you do not use CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR then you need to write in the data.

thanks for that,

I now understand if you use (CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR) you dont have to write the data but in the other case when you create the buffer you specify a pointer to the host data and a pointer to the memory object is returned so why pass the host data address again when you read or write the buffer, opencl already knows it when it was created.

That’s correct. When you use CL_MEM_USE_HOST_PTR OpenCL uses the provided host pointer as a backing store for the cl_mem object. This means that OpenCL will keep that data up to date. You will need to use the map/unmap commands to make sure that the memory is up-to-date before modifying it on the host.

When you do not use either of those modes, OpenCL ignores the pointer you pass in to it, so you need to specify the data to be written explicitly.