Samuel
Samuel

Reputation: 6247

how does clEnqueueMapBuffer work

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.

Upvotes: 11

Views: 11720

Answers (2)

kiranputtur
kiranputtur

Reputation: 338

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:

Upvotes: 6

Phil Wright
Phil Wright

Reputation: 180

No, the map functions don't allocate memory. You'd do that in your call to clCreateBuffer. If you allocate memory on the CPU and then try to use it, it will need to be copied to GPU accessible memory. To get memory accessible by both it's best to use CL_MEM_ALLOC_HOST_PTR

clCreateBuffer(context, flags, size, host_ptr, &error);

context - Context for the device you're using.

flags - CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE

size - Size of the buffer in bytes, usually N * sizeof(data type)

host_ptr - Can be NULL or 0 meaning we have no existing data. You could add CL_MEM_COPY_HOST_PTR to flags and pass in a pointer to the values you want copied to the buffer. This would save you having to copy via the mapped pointer. Beneficial if the values won't change.

Upvotes: -1

Related Questions