C Specification

A buffer object may be created using the function

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

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 be NULL. OpenCL 3.0 does not define any optional properties for buffers.

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

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, or if the same property name is specified more than once.

  • CL_INVALID_VALUE if values specified in flags are not valid as defined in the Memory Flags table.

  • CL_INVALID_BUFFER_SIZE if size is 0, or if size is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE for all devices in context, or if CL_MEM_USE_HOST_PTR is set in flags and host_ptr is a pointer returned by clSVMAlloc and size is greater than the size passed to clSVMAlloc.

  • CL_INVALID_HOST_PTR if host_ptr is NULL and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags or if host_ptr is not NULL but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags.

  • CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for 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.

Table 1. List of supported memory flag values
Memory Flags Description

CL_MEM_READ_WRITE

This flag specifies that the memory object will be read and written by a kernel. This is the default.

CL_MEM_WRITE_ONLY

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 CL_MEM_WRITE_ONLY inside a kernel is undefined.

CL_MEM_READ_WRITE and CL_MEM_WRITE_ONLY are mutually exclusive.

CL_MEM_READ_ONLY

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 CL_MEM_READ_ONLY inside a kernel is undefined.

CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY and CL_MEM_READ_ONLY are mutually exclusive.

CL_MEM_USE_HOST_PTR

This flag is valid only if host_ptr is not NULL. If specified, it indicates that the application wants the OpenCL implementation to use memory referenced by host_ptr as the storage bits for the memory object.

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.

CL_MEM_ALLOC_HOST_PTR

This flag specifies that the application wants the OpenCL implementation to allocate memory from host accessible memory.

CL_MEM_ALLOC_HOST_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive.

CL_MEM_COPY_HOST_PTR

This flag is valid only if host_ptr is not NULL. If specified, it indicates that the application wants the OpenCL implementation to allocate memory for the memory object and copy the data from memory referenced by host_ptr. The implementation will copy the memory immediately and host_ptr is available for reuse by the application when the clCreateBuffer, clCreateBufferWithProperties, clCreateImage, clCreateImageWithProperties, clCreateImage2D, or clCreateImage3D operation returns.

CL_MEM_COPY_HOST_PTR and CL_MEM_USE_HOST_PTR are mutually exclusive.

CL_MEM_COPY_HOST_PTR can be used with CL_MEM_ALLOC_HOST_PTR to initialize the contents of the cl_mem object allocated using host-accessible (e.g. PCIe) memory.

CL_MEM_HOST_WRITE_ONLY

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

CL_MEM_HOST_READ_ONLY

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

CL_MEM_HOST_WRITE_ONLY and CL_MEM_HOST_READ_ONLY are mutually exclusive.

CL_MEM_HOST_NO_ACCESS

missing before version 1.2.

This flag specifies that the host will not read or write the memory object.

CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_READ_ONLY and CL_MEM_HOST_NO_ACCESS are mutually exclusive.

CL_MEM_KERNEL_READ_AND_WRITE

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

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 2014-2023 The Khronos Group Inc.

SPDX-License-Identifier: CC-BY-4.0