CLSETKERNELARG(3clc)
π CLSETKERNELARG π
okay so bestie, welcome to the OpenCL Manual page for clSetKernelArg β literally the function that tells a kernel "hey girl, this is your argument" πΈ it's giving backstage pass energy, you're handing values to a kernel one slot at a time. π
β¨ NAME β¨
clSetKernelArg β used to set the argument value for a specific argument of a kernel. π
cl_int clSetKernelArg(cl_kernel kernel, cl_uint arg_index,
size_t arg_size, const void *arg_value);
π«Ά PARAMETERS π«Ά
kernelβ a valid kernel object. like, it has to actually be a real one, no cap. πarg_indexβ the argument index. arguments to the kernel get referred to by indices that run from0for the leftmost argument all the way up ton - 1, wherenis the total number of arguments declared by a kernel. so yeah, zero-indexed, we stay consistent around here. π―arg_valueβ a pointer to the data that should be used as the argument value for the argument specified byarg_index. the argument data pointed to byarg_valuegets copied, so thearg_valuepointer can be reused by the application afterclSetKernelArgreturns β you're free, bestie, go wild. π¦ the argument value you specify is the value used by all API calls that enqueue the kernel (clEnqueueNDRangeKernel(3clc)andclEnqueueTask(3clc)) until the argument value is changed by a call toclSetKernelArgfor thatkernel.
If the argument is a memory object (buffer, image or image array), the arg_value entry will be a pointer to the appropriate buffer, image or image array object. the memory object must be created with the context associated with the kernel object β they have to match, it's a whole vibe check. β¨
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(3clc) or constant(3clc) memory in the kernel.
If the argument is declared with the local(3clc) qualifier, the arg_value entry must be NULL. (yes, must. don't fight it. π)
If the argument is of type sampler_t, the arg_value entry must be a pointer to the sampler 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). and if the argument is declared with the constant(3clc) qualifier, the size in bytes of the memory object cannot exceed CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, and the number of arguments declared as pointers to constant(3clc) memory cannot exceed CL_DEVICE_MAX_CONSTANT_ARGS. those are the rules, periodt. π«
now for the image girlies, listen up, the memory object you pass as argument value has to match the declared type exactly:
- declared
image2d_tβ the memory object must be a 2D image object - declared
image3d_tβ the memory object must be a 3D image object - declared
image1d_tβ the memory object must be a 1D image object - declared
image1d_buffer_tβ the memory object must be a 1D image buffer object - declared
image1d_array_tβ the memory object must be a 1D image array object - declared
image2d_array_tβ the memory object must be a 2D image array object
For all other kernel arguments, the arg_value entry must be a pointer to the actual data to be used as argument value. π
arg_sizeβ specifies the size of the argument value. if the argument is a memory object, the size is the size of the buffer or image object type. for arguments declared with thelocal(3clc)qualifier, the size specified will be the size in bytes of the buffer that must be allocated for thelocal(3clc)argument. if the argument is of typesampler_t, thearg_sizevalue must be equal tosizeof(cl_sampler). for all other arguments, the size will be the size of argument type. π
π§ NOTES π§
A kernel object does not update the reference count for objects such as memory, 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. like, the kernel is not babysitting your objects, bestie. π
Implementations shall not allow cl_kernel objects to hold reference counts to cl_kernel arguments, because no mechanism is provided for the user to tell the kernel to release that ownership right. if the kernel holds ownership rights on kernel args, that would make it impossible for the user to tell with certainty when he may safely release user allocated resources associated with OpenCL objects such as the cl_mem backing store used with CL_MEM_USE_HOST_PTR. so yeah, that's a whole thing, and it's why it works the way it works. π
An OpenCL API call is considered to be thread-safe if the internal state as managed by OpenCL remains consistent when called simultaneously by multiple host threads. OpenCL API calls that are thread-safe allow an application to call these functions in multiple host threads without having to implement mutual exclusion across these host threads i.e. they are also re-entrant-safe. π
All OpenCL API calls are thread-safe except clSetKernelArg. π³ clSetKernelArg is safe to call from any host thread, and is safe to call re-entrantly so long as concurrent calls operate on different cl_kernel objects. However, the behavior of the cl_kernel object is undefined if clSetKernelArg is called from multiple host threads on the same cl_kernel object at the same time. π¨ Please note that there are additional limitations as to which OpenCL APIs may be called from OpenCL callback functions β please see section 5.9.
There is an inherent race condition in the design of OpenCL that occurs between setting a kernel argument and using the kernel with clEnqueueNDRangeKernel(3clc) or clEnqueueTask(3clc). Another host thread might change the kernel arguments between when a host thread sets the kernel arguments and then enqueues the kernel, causing the wrong kernel arguments to be enqueued. π rather than attempt to share cl_kernel objects among multiple host threads, applications are strongly encouraged to make additional cl_kernel objects for kernel functions for each host thread. one kernel per thread, that's the move. π
π₯ ERRORS π₯
clSetKernelArg returns CL_SUCCESS if the function is executed successfully. otherwise, it returns one of the following errors:
CL_INVALID_KERNELifkernelis not a valid kernel object.CL_INVALID_ARG_INDEXifarg_indexis not a valid argument index.CL_INVALID_ARG_VALUEifarg_valuespecified is not a valid value.CL_INVALID_MEM_OBJECTfor an argument declared to be a memory object when the specifiedarg_valueis not a valid memory object.CL_INVALID_SAMPLERfor an argument declared to be of typesampler_twhen the specifiedarg_valueis not a valid sampler object.CL_INVALID_ARG_SIZEifarg_sizedoes 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 andarg_size != sizeof(cl_mem)or ifarg_sizeis zero and the argument is declared with thelocal(3clc)qualifier or if the argument is a sampler andarg_size != sizeof(cl_sampler).CL_INVALID_ARG_VALUEif the argument is an image declared with theread_onlyqualifier andarg_valuerefers to an image object created withcl_mem_flagsofCL_MEM_WRITEor if the image argument is declared with thewrite_onlyqualifier andarg_valuerefers to an image object created withcl_mem_flagsofCL_MEM_READ. πCL_OUT_OF_RESOURCESif there is a failure to allocate resources required by the OpenCL implementation on the device.CL_OUT_OF_HOST_MEMORYif there is a failure to allocate resources required by the OpenCL implementation on the host.
πΈ Example πΈ
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. see? zero-indexed, left to right, main character energy all the way through. π
β SPECIFICATION β
OpenCL Specification[1]
π SEE ALSO π
clCreateKernel(3clc), clCreateKernelsInProgram(3clc), clReleaseKernel(3clc), clRetainKernel(3clc), clGetKernelInfo(3clc), clGetKernelWorkGroupInfo(3clc)
π AUTHORS π
The Khronos Group
π COPYRIGHT π
Copyright Β© 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.
π NOTES π
- OpenCL Specification page 160, section 5.7.2 - Setting Kernel Arguments
okay that's the whole page, you ate that π«Ά this gem was brought to you by The Khronos Group, version dated 01/14/2021, filed under CLSETKERNELARG(3clc) β go set some kernel args bestie, you got this πβ¨
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms