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);
clSVMAlloc is missing before version 2.0.

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
SVM Memory 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 OpenCL’s 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 and clCreateImageWithProperties 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, or no devices in context support SVM.

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

SPDX-License-Identifier: CC-BY-4.0