how does clEnqueueMapBuffer work

10,208

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>

  1. Map the memory using clEnqueueMapBuffer.
  2. transfer the memory from device to/from host via memcpy.
  3. 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:

Share:
10,208
Samuel
Author by

Samuel

Updated on September 16, 2022

Comments

  • Samuel
    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 over clEnqueueRead/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 the clEnqueueMapBuffer/clEnqueueMapImage allocate buffer from the big buffer.

  • Samuel
    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 the clEnqueueMapBuffer/clEnqueueMapImage alloc buffer from the big buffer
  • Dithermaster
    Dithermaster almost 10 years
    If you didn't use CL_MEM_ALLOC_HOST_PTR in a buffer that you subsequently map then host memory is allocated.