C Specification

To allocate a shared virtual memory buffer (referred to as a SVM buffer) that can be shared by the host and all devices in an OpenCL context that support shared virtual memory, call the function

void* clSVMAlloc(
    cl_context context,
    cl_svm_mem_flags flags,
    size_t size,
    cl_uint alignment);

Parameters

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

  • flags is a bit-field that is used to specify allocation and usage information. The SVM Memory Flags table describes the possible values for flags.

  • size is the size in bytes of the SVM buffer to be allocated.

  • alignment is the minimum alignment in bytes that is required for the newly created buffers memory region. It must be a power of two up to the largest data type supported by the OpenCL device. For the full profile, the largest data type is long16. For the embedded profile, it is long16 if the device supports 64-bit integers; otherwise it is int16. If alignment is 0, a default alignment will be used that is equal to the size of largest data type supported by the OpenCL implementation.

Description

Table 1. List of supported SVM memory flag values
cl_svm_mem_flags Description

CL_​MEM_​READ_​WRITE

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

CL_​MEM_​WRITE_​ONLY

This flag specifies that the SVM buffer will be written but not read by a kernel.

Reading from a SVM buffer 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 SVM buffer object is a read-only memory object when used inside a kernel.

Writing to a SVM buffer 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_​SVM_​FINE_​GRAIN_​BUFFER

Missing before version 2.0.

This specifies that the application wants the OpenCL implementation to do a fine-grained allocation.

CL_​MEM_​SVM_​ATOMICS

Missing before version 2.0.

This flag is valid only if CL_​MEM_​SVM_​FINE_​GRAIN_​BUFFER is specified in flags. It is used to indicate that SVM atomic operations can control visibility of memory accesses in this SVM buffer.

If CL_​MEM_​SVM_​FINE_​GRAIN_​BUFFER is not specified, the buffer can be created as a coarse grained SVM allocation. Similarly, if CL_​MEM_​SVM_​ATOMICS is not specified, the buffer can be created without support for SVM atomic operations (refer to an OpenCL kernel language specifications).

Calling clSVMAlloc does not itself provide consistency for the shared memory region. When the host cannot use the SVM atomic operations, it must rely on OpenCLs guaranteed memory consistency at synchronization points.

For SVM to be used efficiently, the host and any devices sharing a buffer containing virtual memory pointers should have the same endianness. If the context passed to clSVMAlloc has devices with mixed endianness and the OpenCL implementation is unable to implement SVM because of that mixed endianness, clSVMAlloc will fail and return NULL.

Although SVM is generally not supported for image objects, clCreateImage may create an image from a buffer (a 1D image from a buffer or a 2D image from buffer) if the buffer specified in its image description parameter is a SVM buffer. Such images have a linear memory representation so their memory can be shared using SVM. However, fine grained sharing and atomics are not supported for image reads and writes in a kernel.

clSVMAlloc returns a valid non-NULL shared virtual memory address if the SVM buffer is successfully allocated. Otherwise, like malloc, it returns a NULL pointer value. clSVMAlloc will fail if

  • context is not a valid context.

  • flags does not contain CL_​MEM_​SVM_​FINE_​GRAIN_​BUFFER but does contain CL_​MEM_​SVM_​ATOMICS.

  • Values specified in flags do not follow rules described for supported values in the SVM Memory Flags table.

  • CL_​MEM_​SVM_​FINE_​GRAIN_​BUFFER or CL_​MEM_​SVM_​ATOMICS is specified in flags and these are not supported by at least one device in context.

  • The values specified in flags are not valid, i.e. don’t match those defined in the SVM Memory Flags table.

  • size is 0 or > CL_​DEVICE_​MAX_​MEM_​ALLOC_​SIZE value for any device in context.

  • alignment is not a power of two or the OpenCL implementation cannot support the specified alignment for at least one device in context.

  • There was a failure to allocate resources.

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.