C Specification
To set the argument value for a specific argument of a kernel, call the function
// Provided by CL_VERSION_1_0
cl_int clSetKernelArg(
cl_kernel kernel,
cl_uint arg_index,
size_t arg_size,
const void* arg_value);
Parameters
-
kernel is a valid kernel object.
-
arg_index is the kernel argument index. Kernel arguments are referred to by indices that go from zero to n - 1, where n is the total number of arguments declared by the kernel.
-
arg_size specifies the size of the kernel argument value.
-
arg_value is a pointer to the data for the kernel argument. The argument data pointed to by arg_value is copied and the arg_value pointer can therefore be reused by the application after clSetKernelArg returns. The argument data is used by all API calls that enqueue the kernel until the argument is changed by another call to clSetKernelArg for the kernel.
Description
If the kernel argument being set is a pointer to the global or constant
address space, then arg_value must point to a buffer memory object or NULL,
or arg_value must be NULL.
If arg_value is NULL or points to NULL, then the kernel argument will be
set to NULL.
If the kernel argument being set is a pointer to the local address space, then
arg_value must be NULL, and arg_size specifies the amount of local memory
in bytes that are allocated for the kernel argument.
Prior to OpenCL 3.1, arg_size must be greater than zero.
For OpenCL 3.1 and newer, arg_size may be zero, indicating that no local
memory is required for the kernel argument. [1]
If the kernel argument being set is an image object, then arg_value must point to an image memory object. Additionally:
-
If the kernel argument is a 1D image, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE1D -
If the kernel argument is a 2D image, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE2D -
If the kernel argument is a 3D image, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE3D -
If the kernel argument is a 1D image buffer, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE1D_ BUFFER -
If the kernel argument is a 1D image array, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE1D_ ARRAY -
If the kernel argument is a 2D image array, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE2D_ ARRAY -
If the kernel argument is a 2D depth image, then the image memory object must be of image type
CL_MEM_and image channel orderOBJECT_ IMAGE2D CL_DEPTH. -
If the kernel argument is a 2D depth image array, then the image memory object must be of image type
CL_MEM_and image channel orderOBJECT_ IMAGE2D_ ARRAY CL_DEPTH. -
If the kernel argument is a 2D MSAA image, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE2D -
If the kernel argument is a 2D MSAA image array, then the image memory object must be of image type
CL_MEM_.OBJECT_ IMAGE2D_ ARRAY -
If the kernel argument is a 2D MSAA depth image, then the image memory object must be of image type
CL_MEM_and image channel orderOBJECT_ IMAGE2D CL_DEPTH. -
If the kernel argument is a 2D MSAA depth image array, then the image memory object must be of image type
CL_MEM_and image channel orderOBJECT_ IMAGE2D_ ARRAY CL_DEPTH.
Behavior is undefined if the same image memory object is passed as both a
read_only image and a write_only image, or as a read_write image and
either a read_only image or a write_only image.
If the kernel argument being set is a sampler, then arg_value must point to a sampler object.
If the kernel argument being set is a device queue, then arg_value must point to a device queue object.
If the kernel argument being set is a pipe, then arg_value must point to a pipe memory object.
For all other kernel arguments, arg_value points to the data that is used as the kernel argument value.
All OpenCL objects set as kernel arguments must be created from the same context as the kernel object.
|
A kernel object does not update the reference count for objects such as memory or sampler objects specified as argument values by clSetKernelArg. Users may not rely on a kernel object to retain objects specified as argument values to the kernel. Implementations shall not allow |
clSetKernelArg returns CL_SUCCESS if the function is executed successfully.
Otherwise, it returns one of the following errors:
-
CL_INVALID_KERNEL -
if kernel is not a valid kernel
-
-
CL_INVALID_ARG_ INDEX -
if arg_index is not a valid argument index
-
-
CL_INVALID_CONTEXT -
if the context associated with kernel and an OpenCL object pointed to by arg_value are not the same
-
-
CL_INVALID_MEM_ OBJECT -
if arg_value is
NULLand it must point to a valid memory object -
if arg_value points to
NULLand it must point to a valid memory object -
if arg_value is not
NULL, and does not point toNULL, and does not point to a valid memory object -
if arg_value points to a valid memory object, but the memory object is not valid for the kernel argument specified by arg_index
-
-
CL_INVALID_SAMPLER -
if the kernel argument is a sampler, but arg_value does not point to a valid sampler object
-
-
CL_INVALID_DEVICE_ QUEUE -
if the kernel argument is a device queue, but arg_value does not point to a valid device queue object. This error code is missing before version 2.0.
-
-
CL_INVALID_ARG_ SIZE -
if arg_value points to a memory object and arg_size is not equal to
sizeof(cl_mem) -
if arg_value points to a sampler object and arg_size is not equal to
sizeof(cl_sampler) -
if arg_value points to a device queue object and arg_size is not equal to
sizeof(cl_command_)queue -
if the kernel argument is a pointer to the
localaddress space and arg_size is zero. This error condition does not apply if the platform associated with kernel is OpenCL 3.1 or newer. -
if arg_value points to the data to be used as the kernel argument value and arg_size does not match the size of the data type for the argument
-
-
CL_INVALID_ARG_ VALUE -
if the argument is an image declared with the
read_onlyqualifier and arg_value points to an image object created with the memory flagCL_MEM_WRITE_ ONLY -
if the argument is an image declared with the
write_onlyqualifier and arg_value points to an image object created with the memory flagCL_MEM_orREAD_ ONLY CL_MEM_IMMUTABLE_ EXT
-
-
CL_MAX_SIZE_ RESTRICTION_ EXCEEDED -
if the size in bytes of the memory object (if the argument is a memory object) or arg_size (if the argument is declared with
localqualifier) exceeds a language-specified maximum size restriction for this argument, such as the MaxByteOffset SPIR-V decoration. This error code is missing before version 2.2.
-
-
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
-
When clSetKernelArg returns an error code different from CL_SUCCESS, the
internal state of kernel may only be modified when that error code is
CL_OUT_ or CL_OUT_. When the internal state
of kernel is modified, it is implementation-defined whether:
-
The argument value that was previously set is kept so that it can be used in further kernel enqueues.
-
The argument value is unset such that a subsequent kernel enqueue fails with
CL_INVALID_. [2]KERNEL_ ARGS
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.