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
// Provided by CL_VERSION_1_0
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 isNULL, num_events_in_wait_list must be 0. If event_wait_list is notNULL, 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 command and can be used to query or queue a wait for this command to complete. If event is
NULLor the enqueue is unsuccessful, no event will be created and therefore it will not be possible to query the status of this command or to wait for this command to complete. If event_wait_list and event are notNULL, event must 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_if command_queue is not a valid host command-queue.COMMAND_ QUEUE -
CL_INVALID_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.CONTEXT -
CL_INVALID_if buffer is not a valid buffer object.MEM_ OBJECT -
CL_INVALID_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.VALUE -
CL_INVALID_if event_wait_list isEVENT_ WAIT_ LIST NULLand num_events_in_wait_list > 0, or event_wait_list is notNULLand num_events_in_wait_list is 0, or if event objects in event_wait_list are not valid events. -
CL_MISALIGNED_if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned toSUB_ BUFFER_ OFFSET CL_DEVICE_value for the device associated with queue. This error code is missing before version 1.1.MEM_ BASE_ ADDR_ ALIGN -
CL_MAP_if there is a failure to map the requested region into the host address space. This error cannot occur for buffer objects created withFAILURE CL_MEM_orUSE_ HOST_ PTR CL_MEM_.ALLOC_ HOST_ PTR -
CL_EXEC_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.STATUS_ ERROR_ FOR_ EVENTS_ IN_ WAIT_ LIST -
CL_MEM_if there is a failure to allocate memory for data store associated with buffer.OBJECT_ ALLOCATION_ FAILURE -
CL_INVALID_if buffer has been created withOPERATION CL_MEM_orHOST_ WRITE_ ONLY CL_MEM_andHOST_ NO_ ACCESS CL_MAP_is set in map_flags or if buffer has been created withREAD CL_MEM_orHOST_ READ_ ONLY CL_MEM_andHOST_ NO_ ACCESS CL_MAP_orWRITE CL_MAP_is set in map_flags.WRITE_ INVALIDATE_ REGION -
CL_OUT_if there is a failure to allocate resources required by the OpenCL implementation on the device.OF_ RESOURCES -
CL_OUT_if there is a failure to allocate resources required by the OpenCL implementation on the host.OF_ HOST_ MEMORY -
CL_INVALID_if mapping would lead to overlapping regions being mapped for writing.OPERATION -
CL_INVALID_if buffer was created withOPERATION CL_MEM_in flags andIMMUTABLE_ EXT CL_MAP_orWRITE CL_MAP_is set in map_flags.WRITE_ INVALIDATE_ REGION
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_ set in mem_flags,
the following will be true:
-
The host_ptr specified in clCreateBuffer or clCreateBufferWithProperties will 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.
| Map Flags | Description |
|---|---|
|
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. |
|
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 |
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. |
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.