C Specification

To enqueue a command to map a region of the buffer object given by buffer into the host address space and returns a pointer to this mapped region, call the function

void* clEnqueueMapBuffer(
    cl_command_queue command_queue,
    cl_mem buffer,
    cl_bool blocking_map,
    cl_map_flags map_flags,
    size_t offset,
    size_t size,
    cl_uint num_events_in_wait_list,
    const cl_event* event_wait_list,
    cl_event* event,
    cl_int* errcode_ret);

Parameters

  • command_queue must be a valid host command-queue.

  • blocking_map indicates if the map operation is blocking or non-blocking.

Description

If blocking_map is CL_​TRUE, clEnqueueMapBuffer does not return until the specified region in buffer is mapped into the host address space and the application can access the contents of the mapped region using the pointer returned by clEnqueueMapBuffer.

If blocking_map is CL_​FALSE i.e. map operation is non-blocking, the pointer to the mapped region returned by clEnqueueMapBuffer cannot be used until the map command has completed. The event argument returns an event object which can be used to query the execution status of the map command. When the map command is completed, the application can access the contents of the mapped region using the pointer returned by clEnqueueMapBuffer.

  • map_flags is a bit-field and is described in the Memory Map Flags table.

  • buffer is a valid buffer object. The OpenCL context associated with command_queue and buffer must be the same.

  • offset and size are the offset in bytes and the size of the region in the buffer object that is being mapped.

  • event_wait_list and num_events_in_wait_list specify events that need to complete before this particular command can be executed. If event_wait_list is NULL, then this particular command does not wait on any event to complete. If event_wait_list is NULL, num_events_in_wait_list must be 0. If event_wait_list is not NULL, the list of events pointed to by event_wait_list must be valid and num_events_in_wait_list must be greater than 0. The events specified in event_wait_list act as synchronization points. The context associated with events in event_wait_list and command_queue must be the same. The memory associated with event_wait_list can be reused or freed after the function returns.

  • event returns an event object that identifies this particular command and can be used to query or queue a wait for this particular command to complete. event can be NULL in which case it will not be possible for the application to query the status of this command or queue a wait for this command to complete. If the event_wait_list and the event arguments are not NULL, the event argument should not refer to an element of the event_wait_list array.

  • errcode_ret will return an appropriate error code. If errcode_ret is NULL, no error code is returned.

clEnqueueMapBuffer will return a pointer to the mapped region. The errcode_ret is set to CL_​SUCCESS.

A NULL pointer is returned otherwise with one of the following error values returned in errcode_ret:

  • CL_​INVALID_​COMMAND_​QUEUE if command_queue is not a valid host command-queue.

  • CL_​INVALID_​CONTEXT if context associated with command_queue and buffer are not the same or if the context associated with command_queue and events in event_wait_list are not the same.

  • CL_​INVALID_​MEM_​OBJECT if buffer is not a valid buffer object.

  • CL_​INVALID_​VALUE if region being mapped given by (offset, size) is out of bounds or if size is 0 or if values specified in map_flags are not valid.

  • CL_​INVALID_​EVENT_​WAIT_​LIST if event_wait_list is NULL and num_events_in_wait_list > 0, or event_wait_list is not NULL and num_events_in_wait_list is 0, or if event objects in event_wait_list are not valid events.

  • CL_​MISALIGNED_​SUB_​BUFFER_​OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_​DEVICE_​MEM_​BASE_​ADDR_​ALIGN value for the device associated with queue. This error code is missing before version 1.1.

  • CL_​MAP_​FAILURE if there is a failure to map the requested region into the host address space. This error cannot occur for buffer objects created with CL_​MEM_​USE_​HOST_​PTR or CL_​MEM_​ALLOC_​HOST_​PTR.

  • CL_​EXEC_​STATUS_​ERROR_​FOR_​EVENTS_​IN_​WAIT_​LIST if the map operation is blocking and the execution status of any of the events in event_wait_list is a negative integer value. This error code is missing before version 1.1.

  • CL_​MEM_​OBJECT_​ALLOCATION_​FAILURE if there is a failure to allocate memory for data store associated with buffer.

  • CL_​INVALID_​OPERATION if buffer has been created with CL_​MEM_​HOST_​WRITE_​ONLY or CL_​MEM_​HOST_​NO_​ACCESS and CL_​MAP_​READ is set in map_flags or if buffer has been created with CL_​MEM_​HOST_​READ_​ONLY or CL_​MEM_​HOST_​NO_​ACCESS and CL_​MAP_​WRITE or CL_​MAP_​WRITE_​INVALIDATE_​REGION is set in map_flags.

  • CL_​OUT_​OF_​RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.

  • CL_​OUT_​OF_​HOST_​MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.

  • CL_​INVALID_​OPERATION if mapping would lead to overlapping regions being mapped for writing.

The pointer returned maps a region starting at offset and is at least size bytes in size. The result of a memory access outside this region is undefined.

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 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.

Mapped buffer objects are unmapped using clEnqueueUnmapMemObject. This is described in Unmapping Mapped Memory Objects.

Table 1. List of supported map flag values
cl_map_flags Description

CL_​MAP_​READ

This flag specifies that the region being mapped in the memory object is being mapped for reading.

The pointer returned by clEnqueueMapBuffer (clEnqueueMapImage) is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer (clEnqueueMapImage) command has completed.

CL_​MAP_​WRITE

This flag specifies that the region being mapped in the memory object is being mapped for writing.

The pointer returned by clEnqueueMapBuffer (clEnqueueMapImage) is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer (clEnqueueMapImage) command has completed

CL_​MAP_​WRITE_​INVALIDATE_​REGION

Missing before version 1.2.

This flag specifies that the region being mapped in the memory object is being mapped for writing.

The contents of the region being mapped are to be discarded. This is typically the case when the region being mapped is overwritten by the host. This flag allows the implementation to no longer guarantee that the pointer returned by clEnqueueMapBuffer (clEnqueueMapImage) contains the latest bits in the region being mapped which can be a significant performance enhancement.

CL_​MAP_​READ or CL_​MAP_​WRITE and CL_​MAP_​WRITE_​INVALIDATE_​REGION are mutually exclusive.

See Also

Document Notes

For more information, see the OpenCL Specification

This page is extracted from the OpenCL Specification. Fixes and changes should be made to the Specification, not directly.

Copyright (c) 2014-2020 Khronos Group. This work is licensed under a Creative Commons Attribution 4.0 International License.