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
// Provided by CL_VERSION_2_0
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
| SVM Memory Flags | Description |
|---|---|
|
This flag specifies that the SVM buffer will be read and written by a kernel. This is the default. |
|
This flag specifies that the SVM buffer will be written but not read by a kernel. Reading from a SVM buffer created with |
|
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 |
missing before version 2.0. |
This specifies that the application wants the OpenCL implementation to do a fine-grained allocation. |
missing before version 2.0. |
This flag is valid only if |
If CL_MEM_ is not specified, the buffer can be created
as a coarse grained SVM allocation.
Similarly, if CL_MEM_ 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_but does containSVM_ FINE_ GRAIN_ BUFFER CL_MEM_.SVM_ ATOMICS -
flags contains
CL_MEM_.IMMUTABLE_ EXT -
Values specified in flags do not follow rules described for supported values in the SVM Memory Flags table.
-
CL_MEM_orSVM_ FINE_ GRAIN_ BUFFER CL_MEM_is specified in flags and these are not supported by at least one device in context.SVM_ ATOMICS -
The values specified in flags are not valid, i.e. do not match those defined in the SVM Memory Flags table.
-
size is 0 or >
CL_DEVICE_value for any device in context.MAX_ MEM_ ALLOC_ SIZE -
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.
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.