CLCREATECONTEXT(3clc)
โจ CLCREATECONTEXT(3clc) โจ
okay so this is the page for clCreateContext, straight out of the OpenCL Manual. it's giving let's build a whole entire universe for our GPU to live in and honestly? iconic. buckle up bestie ๐
๐ธ NAME ๐ธ
clCreateContext โ creates an OpenCL context. that's it. that's the whole vibe. โจ
๐ฆ SYNOPSIS ๐ฆ
cl_context clCreateContext(const cl_context_properties *properties,
cl_uint num_devices,
const cl_device_id *devices,
(void CL_CALLBACK *pfn_notify) (const char *errinfo,
const void *private_info,
size_t cb,
void *user_data),
void *user_data, cl_int *errcode_ret);
look at that signature. she's long. she's powerful. she knows things. ๐
๐ DESCRIPTION ๐
so an OpenCL context gets created with one or more devices. periodt.
and what even is a context, you ask? it's the thing the OpenCL runtime uses to manage all your objects โ think command-queues, memory, program and kernel objects โ and it's also how you actually execute kernels on one or more of the devices you picked out. it's the whole group chat, basically, and every device you listed is in it. ๐ซถ
๐ PARAMETERS ๐
propertiesโ this one's the โจmain characterโจ of the parameter list, so let's talk.
it specifies a list of context property names and their corresponding values. each property name is immediately followed by the corresponding desired value. and the list is terminated with 0.
properties can be NULL โ in which case the platform that gets selected is implementation-defined. (iykyk, that means you don't get to pick, bestie, the implementation does ๐ญ)
the list of supported properties is described in the table below. ๐
if the extension cl_khr_dx9_media_sharing(3clc) is enabled, then properties specifies a list of context property names and their corresponding values. each property is followed immediately by the corresponding desired value, and the list is terminated with zero. if a property is not specified in properties, then its default value (listed in the table below) is used โ this is what they call being specified implicitly, fancy right? and if properties is NULL or empty (meaning it points to a list whose first value is zero), then all attributes take on their default values. ๐
if the extension cl_khr_d3d10_sharing(3clc) is enabled, then properties specifies a list of context property names and their corresponding values. each property is followed immediately by the corresponding desired value, and the list is terminated with zero. if a property is not specified in properties, then its default value is used (again โ specified implicitly, you got this). and if properties is NULL or empty (points to a list whose first value is zero), all attributes take on their default value. โจ
reminder bestie: that 0 at the end is not decorative. it's the terminator. skip it and the runtime just keeps reading forever and that is not the slay you think it is. ๐
and that's where part 1 wraps up โ the rest is still coming, so don't go anywhere ๐
If the extension cl_khr_gl_sharing(3clc) is on, then properties is pointing at an attribute list โ think of it as an array of ordered <attribute name, value> pairs, and the whole thing is terminated by zero. ๐
If some attribute isn't in properties, bestie, it just gets its default value (we say it's specified implicitly). And if properties is NULL or empty (i.e. it points to a list whose very first value is zero), then literally every attribute takes on its default. Periodt.
โจ the properties glow-up โจ
CL_CONTEXT_PLATFORM(cl_platform_id) โ says which platform to use. ๐CL_CONTEXT_INTEROP_USER_SYNC(cl_bool) โ tells you whether the user is the one responsible for sync between OpenCL and other APIs. For the restrictions on using this flag, pls refer to the specific sections in the OpenCL 1.2 extension specification that describe sharing with other APIs. IfCL_CONTEXT_INTEROP_USER_SYNCis not specified, a default ofCL_FALSEis assumed. ๐CL_CONTEXT_D3D10_DEVICE_KHR(ID3D10Device*) โ if thecl_khr_d3d10_sharing(3clc)extension is enabled, this specifies theID3D10Device*to use for Direct3D 10 interoperability. The default value isNULL. ๐ฎCL_GL_CONTEXT_KHR(0, OpenGL context handle) โ the OpenGL context to associate the OpenCL context with (available if thecl_khr_gl_sharing(3clc)extension is enabled). ๐CL_EGL_DISPLAY_KHR(EGL_NO_DISPLAY, EGLDisplay handle) โ theEGLDisplayan OpenGL context was created with respect to (available if thecl_khr_gl_sharing(3clc)extension is enabled). โจCL_GLX_DISPLAY_KHR(None, X handle) โ the X Display an OpenGL context was created with respect to (available if thecl_khr_gl_sharing(3clc)extension is enabled). ๐ฆCL_CGL_SHAREGROUP_KHR(0, CGL share group handle) โ the CGL share group to associate the OpenCL context with (available if thecl_khr_gl_sharing(3clc)extension is enabled). ๐CL_WGL_HDC_KHR(0, HDC handle) โ the HDC an OpenGL context was created with respect to (available if thecl_khr_gl_sharing(3clc)extension is enabled). ๐CL_CONTEXT_ADAPTER_D3D9_KHR(IDirect3DDevice9 *) โ specifies anIDirect3DDevice9to use for D3D9 interop (if thecl_khr_dx9_media_sharing(3clc)extension is supported). ๐ฅCL_CONTEXT_ADAPTER_D3D9EX_KHR(IDirect3DDeviceEx*) โ specifies anIDirect3DDevice9Exto use for D3D9 interop (if thecl_khr_dx9_media_sharing(3clc)extension is supported). ๐CL_CONTEXT_ADAPTER_DXVA_KHR(IDXVAHD_Device *) โ specifies anIDXVAHD_Deviceto use for DXVA interop (if thecl_khr_dx9_media_sharing(3clc)CL_CONTEXT_D3D11_DEVICE_KHRโ type isID3D11Device *. Specifies theID3D11Device *to use for Direct3D 11 interoperability. The default value isNULL. (โฆ extension is supported). ๐
num_devices โ the number of devices you're listing in the devices argument. Just a count, bestie, that's the whole job. โจ
devices โ a pointer to a list of unique devices, the ones you got back from clGetDeviceIDs(3clc) or sub-devices you made with clCreateSubDevices(3clc) for a platform. ๐ฆ
pfn_notify โ a callback function you, the application, can register. The OpenCL implementation uses it to report info about errors during context creation and errors that happen at runtime in this context. Heads up though: this callback may be called asynchronously by the OpenCL implementation, so it's your job (the application's responsibility, no cap) to make sure the callback function is thread-safe. If pfn_notify is NULL, no callback gets registered. The parameters of this callback are:
errinfoโ a pointer to an error string. ๐private_infoandcbโ together these represent a pointer to binary data returned by the OpenCL implementation, stuff you can use to log extra info that helps with debugging the error. ๐user_dataโ a pointer to user-supplied data.
NOTE: there are a bunch of cases where error notifications need to be delivered because of an error that happens outside a context. Those notifications might not make it through the pfn_notify callback at all. Where they go is implementation-defined. ๐
user_data โ passed as the user_data argument whenever pfn_notify gets called. user_data can be NULL. ๐
errcode_ret โ returns an appropriate error code. If errcode_ret is NULL, no error code comes back. Simple as that.
๐ NOTES ๐
Okay so clCreateContext and clCreateContextFromType(3clc) do an implicit retain. This is very helpful for 3rd party libraries, which typically get a context handed to them by the application. But plot twist: the application might delete that context without telling the library anything ๐ญ. Letting functions attach to (i.e. retain) and release a context solves the whole "a library is holding a context that's no longer valid" problem. slay. ๐
๐ ERRORS ๐
clCreateContext returns a valid non-zero context and errcode_ret is set to CL_SUCCESS if the context is created successfully. Otherwise it returns a NULL value, and these error values come back in errcode_ret:
CL_INVALID_PLATFORMifpropertiesisNULLand no platform could be selected, or if the platform value specified inpropertiesis not a valid platform. (If the extensioncl_khr_gl_sharing(3clc)is enabled, then this error is replaced withCL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; see below.)CL_INVALID_PROPERTYif a context property name inpropertiesis not a supported property name, if the value specified for a supported property name is not valid, or if the same property name is specified more than once. However if the extensioncl_khr_gl_sharing(3clc)is enabled, thenCL_INVALID_PROPERTYis returned if an attribute name other than those listed in the table forpropertiesabove, or ifCL_CONTEXT_INTEROP_USER_SYNCis specified inproperties.CL_INVALID_PROPERTYif an attribute name other than those specified in table 4.5, or ifCL_CONTEXT_INTEROP_USER_SYNCis specified inproperties.CL_INVALID_VALUEifdevicesisNULL; ifnum_devicesis equal to zero; or ifpfn_notifyisNULLbutuser_datais notNULL.CL_INVALID_DEVICEifdevicescontains an invalid device.CL_INVALID_OPERATIONif Direct3D 10 interoperability is specified by settingCL_INVALID_D3D10_DEVICE_KHRto a non-NULLvalue, and interoperability with another graphics API is also specified (if thecl_khr_d3d10_sharing(3clc)extension is enabled).CL_DEVICE_NOT_AVAILABLEif a device indevicesis currently not available even though the device was returned byclGetDeviceIDs(3clc).CL_OUT_OF_RESOURCESif there's a failure to allocate resources required by the OpenCL implementation on the device.CL_OUT_OF_HOST_MEMORYif there's a failure to allocate resources required by the OpenCL implementation on the host.CL_INVALID_D3D10_DEVICE_KHRif the Direct3D 10 device specified for interoperability is not compatible with the devices against which the context is to be created (if thecl_khr_d3d10_sharing(3clc)extension is enabled).CL_INVALID_D3D10_DEVICE_KHRif the value of the propertyCL_CONTEXT_D3D10_DEVICE_KHRis non-NULLand does not specify a valid Direct3D 10 device with which thecl_device_idsagainst which this context is to be created may interoperate (if thecl_khr_d3d10_sharing(3clc)extension is enabled).CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRwhen an invalid OpenGL context or share group object handle is specified inproperties(only if thecl_khr_gl_sharing(3clc)extension is enabled).CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif no OpenGL or OpenGL ES context or share group is specified in the attribute list given toclCreateContext(3clc)and any of the commands in section 9.7 are called. (if thecl_khr_gl_sharing(3clc)extension is enabled)CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif thecl_khr_gl_sharing(3clc)extension is enabled and if a context was specified by any of the following means:- A context specified for an EGL-based OpenGL ES or OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_EGL_DISPLAY_KHR. - A context was specified for a GLX-based OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_GLX_DISPLAY_KHR. - A context was specified for a WGL-based OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_WGL_HDC_KHR.
- A context specified for an EGL-based OpenGL ES or OpenGL implementation by setting the attributes
and any of the following conditions hold:
- The specified display and context attributes do not identify a valid OpenGL or OpenGL ES context.
- The specified context does not support buffer and renderbuffer objects.
- The specified context is not compatible with the OpenCL context being created (for example, it exists in a physically distinct address space, such as another hardware device, or does not support sharing data with OpenCL due to implementation restrictions).
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif a share group was specified for a CGL-based OpenGL implementation by setting the attributeCL_CGL_SHAREGROUP_KHR, and the specified share group does not identify a valid CGL share group object (if thecl_khr_gl_sharing(3clc)extension is enabled).CL_INVALID_OPERATIONif a context was specified as described above and any of the following conditions hold:- A context or share group object was specified for one of CGL, EGL, GLX, or WGL and the OpenGL implementation does not support that window-system binding API.
- More than one of the attributes
CL_CGL_SHAREGROUP_KHR,CL_EGL_DISPLAY_KHR,CL_GLX_DISPLAY_KHR, andCL_WGL_HDC_KHRis set to a non-default value. - Both of the attributes
CL_CGL_SHAREGROUP_KHRandCL_GL_CONTEXT_KHRare set to non-default values. - Any of the devices specified in the
devicesargument cannot support OpenCL objects which share the data store of an OpenGL object, as described in section 9.7.
CL_INVALID_DX9_MEDIA_ADAPTER_KHRif the media adapter specified for interoperability is not compatible with the devices against which the context is to be created (only if thecl_khr_dx9_media_sharing(3clc)extension is supported).CL_INVALID_ADAPTER_KHRif any of the values of the propertiesCL_CONTEXT_ADAPTER_D3D9_KHR,CL_CONTEXT_ADAPTER_D3D9EX_KHRorCL_CONTEXT_ADAPTER_DXVA_KHRis non-NULLand does not specify a valid media adapter with which thecl_device_idsagainst which this context is to be created may interoperate (only if thecl_khr_dx9_media_sharing(3clc)extension is supported).CL_INVALID_OPERATIONif interoperability is specified by settingCL_CONTEXT_ADAPTER_D3D9_KHR,CL_CONTEXT_ADAPTER_D3D9EX_KHRorCL_CONTEXT_ADAPTER_DXVA_KHRto a non-NULLvalue, and interoperability with another graphics API is also specified (only if thecl_khr_dx9_media_sharing(3clc)extension is supported).CL_INVALID_OPERATIONif Direct3D 11 interoperability is specified by settingCL_INVALID_D3D11_DEVICE_KHRto a non-NULLvalue, and interoperability with another graphics API is also specified (only if thecl_khr_d3d11_sharing(3clc)extension is supported).CL_INVALID_D3D11_DEVICE_KHRif the value of the propertyCL_CONTEXT_D3D11_DEVICE_KHRis non-NULLand does not specify a valid Direct3D 11 device with which thecl_device_idsagainst which this context is to be created may interoperate (only if thecl_khr_d3d11_sharing(3clc)extension is supported).CL_INVALID_D3D11_DEVICE_KHRif the Direct3D 11 device specified for interoperability is not compatible with the devices against which the context is to be created (only if thecl_khr_d3d11_sharing(3clc)extension is supported).
โญ SPECIFICATION โญ
OpenCL Specification[1] ๐
๐ SEE ALSO ๐
clGetDeviceIDs(3clc), clCreateContextFromType(3clc), clRetainContext(3clc), clReleaseContext(3clc), clGetContextInfo(3clc), classDiagram(3clc)
๐ AUTHORS ๐
The Khronos Group ๐
โจ COPYRIGHT โจ
okay bestie, legal era incoming, don't scroll past this one ๐
Copyright ยฉ 2007-2011 The Khronos Group Inc.
and here's the actual permission notice, kept word-for-word because it's the law and we do not freestyle the law:
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.
like. that's it. that's the whole vibe. copy it, ship it, sell it, whatever โ just keep the notice attached to all copies or substantial portions of the Materials. ๐ซถ
๐ NOTES ๐
just one lil footnote babe, one:
- OpenCL Specification page 54, section 4.4 - Contexts
that's your receipts if you wanna go read the source material yourself. go off, scholar ๐
and that's the whole page, bestie!! ๐ you made it to the end of CLCREATECONTEXT(3clc) and honestly? iconic. this one came from The Khronos Group and it's dated 01/14/2021 โ so if you're reading this way later, that's the version you've got in your hands. no cap, go make some contexts โจ๐๐ฆ
๐ฆ 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