C Specification
A buffer object may be created using the function
// Provided by CL_VERSION_1_0
cl_mem clCreateBuffer(
cl_context context,
cl_mem_flags flags,
size_t size,
void* host_ptr,
cl_int* errcode_ret);
A buffer object may also be created with additional properties using the function
// Provided by CL_VERSION_3_0
cl_mem clCreateBufferWithProperties(
cl_context context,
const cl_mem_properties* properties,
cl_mem_flags flags,
size_t size,
void* host_ptr,
cl_int* errcode_ret);
| clCreateBufferWithProperties is missing before version 3.0. |
Parameters
-
context is a valid OpenCL context used to create the buffer object.
-
properties is an optional list of properties for the buffer object and their corresponding values. The list is terminated with the special property
0. If no properties are required, properties may beNULL. OpenCL 3.0 does not define any optional properties for buffers, but extensions may define properties as described in the https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#external-buffer-memory-properties-table. -
flags is a bit-field that is used to specify allocation and usage information about the image memory object being created and is described in the supported memory flag values table.
-
size is the size in bytes of the buffer memory object to be allocated.
-
host_ptr is a pointer to the buffer data that may already be allocated by the application. The size of the buffer that host_ptr points to must be greater than or equal to size bytes.
-
errcode_ret may return an appropriate error code. If errcode_ret is
NULL, no error code is returned.
Description
The alignment requirements for data stored in buffer objects are described in Alignment of Application Data Types.
If clCreateBuffer or clCreateBufferWithProperties is called with
CL_MEM_ set in its flags argument, the contents of the
memory pointed to by host_ptr at the time of the clCreateBuffer call
define the initial contents of the buffer object.
If clCreateBuffer or clCreateBufferWithProperties is called with a
pointer returned by clSVMAlloc as its host_ptr argument, and
CL_MEM_ is set in its flags argument, clCreateBuffer or
clCreateBufferWithProperties will succeed and return a valid non-zero
buffer object as long as the size argument is no larger than the
size argument passed in the original clSVMAlloc call.
The new buffer object returned has the shared memory as the underlying
storage.
Locations in the buffers underlying shared memory can be operated on using
atomic operations to the devices level of support as defined in the memory
model.
| Property | Property Value | Description |
|---|---|---|
provided by the |
|
Specifies the list of OpenCL devices (terminated with
|
provided by the |
|
When set to Each device in the context can have their own (fixed) device-side address and a copy of the created buffer which are synchronized implicitly by the runtime. The flag might imply that the buffer will be "pinned" permanently to a device’s memory, but might not be necessarily so, as long as the address range of the buffer remains constant. The device addresses of sub-buffers derived from If the device supports SVM and clCreateBufferWithProperties is called with a pointer
returned by clSVMAlloc as its host_ptr argument, and |
If CL_MEM_ is not specified as part of properties,
the memory object created by clCreateBufferWithProperties or
clCreateImageWithProperties is by default associated with all devices in the
context.
The properties used to create a buffer from an external memory handle are
described for the corresponding
extensions.
When a buffer is created from an external memory handle, the
flags used to specify usage information for the buffer must not
include CL_MEM_, CL_MEM_, or
CL_MEM_, and the host_ptr argument must be NULL.
If CL_MEM_ is set in flags, the buffer created is
an immutable memory object that cannot be modified by the implementation
after creation, and one and only one of the following conditions must be met:
-
Either
CL_MEM_orCOPY_ HOST_ PTR CL_MEM_must be set in flags.USE_ HOST_ PTR -
An external memory handle must be included in properties.
clCreateBuffer and clCreateBufferWithProperties returns a valid non-zero
buffer object and errcode_ret is set to CL_SUCCESS if the buffer object
is created successfully.
Otherwise, they return a NULL value with one of the following error values
returned in errcode_ret:
-
CL_INVALID_CONTEXT -
if context is not a valid context
-
-
CL_INVALID_PROPERTY -
if a property name in properties is not a supported property name
-
if the value specified for a supported property name is not valid
-
if the same property name is specified more than once
-
if properties does not include a supported external memory handle and
CL_MEM_is specified as part of propertiesDEVICE_ HANDLE_ LIST_ KHR -
if properties includes more than one external memory handle
-
-
CL_INVALID_DEVICE -
if a device identified by the property
CL_MEM_is not a valid device or is not associated with contextDEVICE_ HANDLE_ LIST_ KHR -
if a device identified by property
CL_MEM_cannot import the requested external memory object typeDEVICE_ HANDLE_ LIST_ KHR -
if
CL_MEM_is not specified as part of properties and one or more devices in context cannot import the requested external memory object typeDEVICE_ HANDLE_ LIST_ KHR
-
-
CL_INVALID_VALUE -
if values specified in flags are not valid as defined in the Memory Flags table
-
if properties includes a supported external memory handle and flags includes
CL_MEM_,USE_ HOST_ PTR CL_MEM_, orALLOC_ HOST_ PTR CL_MEM_COPY_ HOST_ PTR -
if
CL_MEM_is set in flags andIMMUTABLE_ EXT CL_MEM_,READ_ WRITE CL_MEM_, orWRITE_ ONLY CL_MEM_is set in flagsHOST_ WRITE_ ONLY -
if
CL_MEM_is set in flags and none of the following conditions are met:IMMUTABLE_ EXT -
CL_MEM_orCOPY_ HOST_ PTR CL_MEM_is set in flagsUSE_ HOST_ PTR -
properties includes an external memory handle
-
-
-
CL_INVALID_BUFFER_ SIZE -
if size is zero and properties does not include an AHardwareBuffer external memory handle
-
if size is non-zero and properties includes an AHardwareBuffer external memory handle
-
if size is greater than
CL_DEVICE_for all devices in contextMAX_ MEM_ ALLOC_ SIZE -
if
CL_MEM_orUSE_ HOST_ PTR CL_MEM_is set in flags, and host_ptr is a pointer returned by clSVMAlloc, and size is greater than the size passed to clSVMAllocCOPY_ HOST_ PTR
-
-
CL_INVALID_HOST_ PTR -
if host_ptr is
NULL, andCL_MEM_orUSE_ HOST_ PTR CL_MEM_are set in flagsCOPY_ HOST_ PTR -
if host_ptr is not
NULLbutCL_MEM_orCOPY_ HOST_ PTR CL_MEM_are not set in flagsUSE_ HOST_ PTR -
if properties includes a supported external memory handle and host_ptr is not
NULL
-
-
CL_INVALID_OPERATION -
if properties includes an AHardwareBuffer external memory handle and the AHardwareBuffer format is not
AHARDWAREBUFFER_FORMAT_BLOB -
if properties includes
CL_MEM_and there are no devices in the context that support theDEVICE_ PRIVATE_ ADDRESS_ EXT cl_ext_extensionbuffer_ device_ address
-
-
CL_MEM_OBJECT_ ALLOCATION_ FAILURE -
if there is a failure to allocate memory for the buffer object
-
-
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
-
| Memory Flags | Description |
|---|---|
|
This flag specifies that the memory object will be read and written by a kernel. This is the default. |
|
This flag specifies that the memory object will be written but not read by a kernel. Reading from a buffer or image object created with |
|
This flag specifies that the memory object is a readonly memory object when used inside a kernel. Writing to a buffer or image object created with |
|
This flag is valid only if host_ptr is not The contents of the memory pointed to by host_ptr at the time of the clCreateBuffer, clCreateBufferWithProperties, clCreateImage, clCreateImageWithProperties, clCreateImage2D, or clCreateImage3D call define the initial contents of the memory object. OpenCL implementations are allowed to cache the contents pointed to by host_ptr in device memory. This cached copy can be used when kernels are executed on a device. The result of OpenCL commands that operate on multiple buffer objects created with the same host_ptr or from overlapping host or SVM regions is considered to be undefined. |
|
This flag specifies that the application wants the OpenCL implementation to allocate memory from host accessible memory. |
|
This flag is valid only if host_ptr is not |
missing before version 1.2. |
This flag specifies that the host will only write to the memory object (using OpenCL APIs that enqueue a write or a map for write). This can be used to optimize write access from the host (e.g. enable write-combined allocations for memory objects for devices that communicate with the host over a system bus such as PCIe). |
missing before version 1.2. |
This flag specifies that the host will only read the memory object (using OpenCL APIs that enqueue a read or a map for read). |
missing before version 1.2. |
This flag specifies that the host will not read or write the memory object. |
missing before version 2.0. |
This flag is only used by clGetSupportedImageFormats to query image
formats that may be both read from and written to by the same kernel
instance.
To create a memory object that may be read from and written to use
|
provided by the |
This flag specifies that the contents of the memory object cannot be altered by the implementation after creation. |
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.