Reputation: 8692
My understanding is that if I set up a cl_mem
object using clCreateBuffer
using the flag CL_MEM_USE_HOST_PTR
, that block of memory is now in the control of the device.
If I wish to alter that memory on the host in some way, I should first map the memory using clEnqueueMapBuffer
, which returns a pointer.
Is the pointer that is returned guaranteed in this case to be the same pointer that was used to create the buffer (i.e. that I passed to clCreateBuffer
initially), albeit with type void *
?
By extension, if I now wish to return control of the memory to the device, can I call clEnqueueUnmapMemObject
and use the original pointer used to create the buffer for the mapped_pointer
argument?
Edit: Further investigation would suggest that the pointer would clearly not be the same if a non-zero offset is used when calling clEnqueueMapBuffer
. Therefore, the questions above are in the case when I do set a zero offset.
Upvotes: 4
Views: 982
Reputation: 2565
I can't answer your question with a straight yes or no, but I can give some pointers (no pun intended).
I didn't find in the standard anything that guaranty that the two pointers should actually be the same. However, in the section 5.2.4 of the standard there is a interesting note stating:
If the buffer object is created with CL_MEM_USE_HOST_PTR set in mem_flags, the following will be true:
- The host_ptr specified in clCreateBuffer is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer command has completed.
- The pointer value returned by clEnqueueMapBuffer will be derived from the host_ptr specified when the buffer object is created.
I don't know why you asked that question but at least with the first bullet point you know that you can use the 'original' pointer while the region is mapped.
I found the second bullet point quite unclear but maybe it's because I'm not a native. You might have a better understanding of it than me. Still it seems to me that it might suggest a yes to your question...
Finally I found out in the chapter 10 of "Heterogeneous Computing with OpenCL" a footnote (page 200) stating:
Note that CL_MEM_ALLOC_HOST_PTR guarantees that when map is called on the buffer, the passed pointer becomes the host pointer returned by map. Data is still only valid at the host pointer between calls to map and unmap.
Of course this note is about CL_MEM_ALLOC_HOST_PTR and not CL_MEM_USE_HOST_PTR, and it doesn't indicate the relevant standard section. Still, it's very tempting to assume it's the case for CL_MEM_USE_HOST_PTR too.
Upvotes: 6