how does clEnqueueMapBuffer work
clEnqueueMapBuffer
/clEnqueueMapImage
OpenCL mechanism for accessing memory objects instead of using clEnqueueRead
/Write
. we can map a memory object on a device to a memory region on host. Once we have mapped the object we can read/write or modify anyway we like.
One more difference between Read
/Write
buffer and clEnqueueMapBuffer
is the map_flags
argument. If map_flags
is set to CL_MAP_READ
, the mapped memory will be read only, and if it is set as CL_MAP_WRITE
the mapped memory will be write only, if you want both read + write then make the flag CL_MAP_READ | CL_MAP_WRITE
.
Compared to read/write fns, memory mapping requires three step process>
- Map the memory using
clEnqueueMapBuffer
. - transfer the memory from device to/from host via
memcpy
. - Unmap using
clEnqueueUnmapObject
.
It is common consensus that memory mapping gives significant improvement in performance compared to regular read/write, see here: what's faster - AMD devgurus forum link
If you want to copy a image or rectangular region of image then you can make use of clEnqueueMapImage
call as well.
References:
Samuel
Updated on September 16, 2022Comments
-
Samuel over 1 year
Could anybody talk about the function
clEnqueueMapBuffer
work mechanism. Actually I mainly concern what benefits on speed I can get from this function overclEnqueueRead/WriteBuffer
.PS: Does
clEnqueueMapBuffer/clEnqueueMapImage
also alloc a buffer from the CPU automatically? If yes.
I want to manage my CPU buffer. I mean I malloc a big buffer first. Then if I need buffer. I can allocate it from the big buffer which I allocate first. How to make theclEnqueueMapBuffer/clEnqueueMapImage
allocate buffer from the big buffer. -
Samuel over 11 years@ockuser : Does
clEnqueueMapBuffer/clEnqueueMapImage
also alloc a buffer from the CPU automatically. If yes. I want to manage my CPU buffer. I mean I alloc a big buffer first. Then if I need buffer. I can alloc it from the big buffer which I alloc first. How to make theclEnqueueMapBuffer/clEnqueueMapImage
alloc buffer from the big buffer -
Dithermaster almost 10 yearsIf you didn't use CL_MEM_ALLOC_HOST_PTR in a buffer that you subsequently map then host memory is allocated.