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 argument index. Arguments to the kernel are referred by indices that go from 0 for the leftmost argument to n - 1, where n is the total number of arguments declared by a kernel (see below).
-
arg_size specifies the size of the argument value. If the argument is a memory object, the arg_size value must be equal to
sizeof(. For arguments declared with thecl_mem)localqualifier, the size specified will be the size in bytes of the buffer that must be allocated for thelocalargument. If the argument is of type sampler_t, the arg_size value must be equal tosizeof(. If the argument is of type queue_t, the arg_size value must be equal tocl_sampler)sizeof(. For all other arguments, the size will be the size of argument type.cl_command_)queue -
arg_value is a pointer to data that should be used as the argument value for argument specified by arg_index. 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 value specified is the value used by all API calls that enqueue kernel (clEnqueueNDRangeKernel and clEnqueueTask) until the argument value is changed by a call to clSetKernelArg for kernel.
Description
For example, consider the following kernel:
kernel void image_filter (int n,
int m,
constant float *filter_weights,
read_only image2d_t src_image,
write_only image2d_t dst_image)
{
...
}
Argument index values for image_filter will be 0 for n, 1 for m, 2 for
filter_weights, 3 for src_image and 4 for dst_image.
If the argument is a memory object (buffer, pipe, image or image array), the
arg_value entry will be a pointer to the appropriate buffer, pipe, image
or image array object.
The memory object must be created with the context associated with the
kernel object.
If the argument is a buffer object, the arg_value pointer can be NULL or
point to a NULL value in which case a NULL value will be used as the
value for the argument declared as a pointer to global or constant
memory in the kernel.
If the argument is declared with the local qualifier, the arg_value
entry must be NULL.
If the argument is of type sampler_t, the arg_value entry must be a
pointer to the sampler object.
If the argument is of type queue_t, the arg_value entry must be a
pointer to the device queue object.
If the cl_khr_ extension is supported, then:
If the argument is a multi-sample 2D image, the arg_value entry must be a
pointer to a multi-sample image object.
If the argument is a multi-sample 2D depth image, the arg_value entry must
be a pointer to a multisample depth image object.
If the argument is a multi-sample 2D image array, the arg_value entry must
be a pointer to a multi-sample image array object.
If the argument is a multi-sample 2D depth image array, the arg_value
entry must be a pointer to a multi-sample depth image array object.
If the argument is declared to be a pointer of a built-in scalar or vector
type, or a user defined structure type in the global or constant address
space, the memory object specified as argument value must be a buffer object
(or NULL).
If the argument is declared with the constant qualifier, the size in bytes
of the memory object cannot exceed CL_DEVICE_ and
the number of arguments declared as pointers to constant memory cannot
exceed CL_DEVICE_.
The memory object specified as argument value must be a pipe object if the argument is declared with the pipe qualifier.
The memory object specified as argument value must be a 2D image object if
the argument is declared to be of type image2d_t.
The memory object specified as argument value must be a 2D image object with
image channel order = CL_DEPTH if the argument is declared to be of type
image2d_depth_t.
The memory object specified as argument value must be a 3D image object if
argument is declared to be of type image3d_t.
The memory object specified as argument value must be a 1D image object if
the argument is declared to be of type image1d_t.
The memory object specified as argument value must be a 1D image buffer
object if the argument is declared to be of type image1d_buffer_t.
The memory object specified as argument value must be a 1D image array
object if argument is declared to be of type image1d_array_t.
The memory object specified as argument value must be a 2D image array
object if argument is declared to be of type image2d_array_t.
The memory object specified as argument value must be a 2D image array
object with image channel order = CL_DEPTH if argument is declared to be of
type image2d_array_depth_t.
Behavior is undefined if the same 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.
For all other kernel arguments, the arg_value entry must be a pointer to the actual data to be used as argument value.
|
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 was executed
successfully.
Otherwise, it returns one of the following errors:
-
CL_INVALID_if kernel is not a valid kernel object.KERNEL -
CL_INVALID_if arg_index is not a valid argument index.ARG_ INDEX -
CL_INVALID_if arg_value specified is not a valid value.ARG_ VALUE -
CL_INVALID_for an argument declared to be a memory object when the specified arg_value is not a valid memory object.MEM_ OBJECT -
CL_INVALID_for an argument declared to be a depth image, depth image array, multi-sample image, multi-sample image array, multi-sample depth image, or a multi-sample depth image array when the specified arg_value does not follow the rules described above for a depth memory object or memory array object argument.MEM_ OBJECT -
CL_INVALID_for an argument declared to be of type sampler_t when the specified arg_value is not a valid sampler object.SAMPLER -
CL_INVALID_for an argument declared to be of type queue_t when the specified arg_value is not a valid device queue object. This error code is missing before version 2.0.DEVICE_ QUEUE -
CL_INVALID_ARG_ SIZE -
if arg_size does not match the size of the data type for an argument that is not a memory object, or
-
if the argument is a memory object and arg_size !=
sizeof(, orcl_mem) -
if arg_size is zero and the argument is declared with the
localqualifier, or -
if the argument is a sampler and arg_size !=
sizeof(.cl_sampler)
-
-
CL_MAX_if the size in bytes of the memory object (if the argument is a memory object) or arg_size (if the argument is declared withSIZE_ RESTRICTION_ EXCEEDED 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_INVALID_ARG_ VALUE -
if the argument is an image declared with the
read_onlyqualifier and arg_value refers to an image object created with cl_mem_flags ofCL_MEM_, orWRITE_ ONLY -
if the image argument is declared with the
write_onlyqualifier and arg_value refers to an image object created with cl_mem_flags ofCL_MEM_.READ_ ONLY
-
-
CL_OUT_if there is a failure to allocate resources required by the OpenCL implementation on the device.OF_ RESOURCES -
CL_OUT_if there is a failure to allocate resources required by the OpenCL implementation on the host.OF_ HOST_ MEMORY
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_. [1]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.