C Specification

A buffer object is created using the following function

cl_mem clCreateBuffer(
    cl_context context,
    cl_mem_flags flags,
    size_t size,
    void* host_ptr,
    cl_int* errcode_ret);

Parameters

  • context is a valid OpenCL context used to create the buffer object.

  • flags is a bit-field that is used to specify allocation and usage information such as the memory arena that should be used to allocate the buffer object and how it will be used. The Memory Flags table describes the possible values for flags. If the value specified for flags is 0, the default is used which is CL_​MEM_​READ_​WRITE.

  • 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 ≥ size bytes.

Description

Table 1. List of supported memory flag values
cl_mem_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 call define the initial contents of the buffer object.

OpenCL implementations are allowed to cache the buffer 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, clCreateImage, 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.

The user is responsible for ensuring that data passed into and out of OpenCL images are natively aligned relative to the start of the buffer as per kernel language or IL requirements. OpenCL buffers created with CL_​MEM_​USE_​HOST_​PTR need to provide an appropriately aligned host memory pointer that is aligned to the data types used to access these buffers in a kernel(s).

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

If clCreateBuffer 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 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 will succeed and return a valid non-zero buffer object as long as the size argument to clCreateBuffer 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 returns a valid non-zero buffer object and errcode_ret is set to CL_​SUCCESS if the buffer object is created successfully. Otherwise, it returns 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_​VALUE if values specified in flags are not valid as defined in the Memory Flags table.

  • CL_​INVALID_​BUFFER_​SIZE if size is 04.

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

    4

    Implementations may return CL_​INVALID_​BUFFER_​SIZE if size is greater than CL_​DEVICE_​MAX_​MEM_​ALLOC_​SIZE value specified in the Device Queries table for all devices in context.

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.