CLCREATECONTEXTFROMT(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

πŸ’– CLCREATECONTEXTFROMT πŸ’–

okay so bestie, welcome to the clCreateContextFromType(3clc) page β€” it lives in the OpenCL Manual and it's literally your girl for spinning up a context from a device type instead of hand-picking the devices yourself. πŸ¦‹


✨ NAME ✨

clCreateContextFromType β€” create an OpenCL context from a device type that identifies the specific device(s) to use. πŸ’…

like, you say "gimme all the GPUs" and it goes and finds them for you. no cap.

cl_context
                                   clCreateContextFromType(const cl_context_properties *properties,
                                   cl_device_type device_type,
                                   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);

πŸŽ€ PARAMETERS πŸŽ€

  • properties β€” this one's a whole list of context property names plus their matching values. the way it works: every property name gets immediately followed by the value you want for it. πŸ“‹ The list of supported properties is described in the table below. πŸ‘€
heads up bestie β€” properties can also be NULL, and in that case the platform that gets selected is implementation-defined. so like, you're not choosing. the implementation is. ✨ iykyk.

and that's just part one, don't go anywhere β€” more of this page is coming. 🫢

If the cl_khr_gl_sharing(3clc) extension is supported, properties points to an attribute list whose format and valid contents are identical to the properties argument of clCreateContext(3clc) β€” and that argument is literally this: properties specifies a list of context property names and their corresponding values. Each property name is immediately followed by the corresponding desired value. The list is terminated with 0. The list of supported properties is described in the table below. properties can be NULL, in which case the platform that is selected is implementation-defined. ✨

Okay bestie, the table has three columns: the cl_context_properties enum, the property value, and the description. We’re serving it as a list because terminal tables are not the vibe. Same exact info, no cap. πŸ’…

  • CL_CONTEXT_PLATFORM β€” property value: cl_platform_id. Description: Specifies the platform to use. 🌈
  • CL_CONTEXT_INTEROP_USER_SYNC β€” property value: cl_bool. Description: Specifies whether the user is responsible for synchronization between OpenCL and other APIs. Please refer to the specific sections in the OpenCL 1.2 extension specification that describe sharing with other APIs for restrictions on using this flag. If CL_CONTEXT_INTEROP_USER_SYNC is not specified, a default of CL_FALSE is assumed. 🫢
  • CL_CONTEXT_D3D10_DEVICE_KHR β€” property value: ID3D10Device *. Description: Specifies the ID3D10Device * to use for Direct3D 10 interoperability. The default value is NULL (applies if the extension cl_khr_d3d10_sharing(3clc) is supported). πŸŽ€
  • CL_CONTEXT_ADAPTER_D3D9_KHR β€” property value: IDirect3DDevice9 *. Description: Specifies an IDirect3DDevice9 to use for D3D9 intero (applies if the cl_khr_dx9_media_sharing(3clc) extension is supported). πŸ’€
  • CL_CONTEXT_ADAPTER_D3D9EX_KHR β€” property value: IDirect3DDeviceEx*. Description: Specifies an IDirect3DDevice9Ex to use for D3D9 interop (applies if the cl_khr_dx9_media_sharing(3clc) extension is supported). ✨
  • CL_CONTEXT_ADAPTER_DXVA_KHR β€” property value: IDXVAHD_Device *. Description: Specifies an IDXVAHD_Device to use for DXVA interop (applies if the cl_khr_dx9_media_sharing(3clc) extension is supported). 🌸
  • CL_GL_CONTEXT_KHR β€” property value: 0, OpenGL context handle. Description: OpenGL context to associated the OpenCL context with (available if the cl_khr_gl_sharing(3clc) extension is enabled). πŸ¦‹
  • CL_CGL_SHAREGROUP_KHR β€” property value: 0, CGL share group handle. Description: CGL share group to associate the OpenCL context with (available if the cl_khr_gl_sharing(3clc) extension is enabled). πŸ“
  • CL_EGL_DISPLAY_KHR β€” property value: EGL_NO_DISPLAY, EGLDisplay handle. Description: CGL share group to associate the OpenCL context with (available if the cl_khr_gl_sharing(3clc) extension is enabled). 😭
  • CL_GLX_DISPLAY_KHR β€” property value: None, X handle. Description: X Display an OpenGL context was created with respect to (available if the cl_khr_gl_sharing(3clc) extension is enabled). πŸ’œ
  • CL_WGL_HDC_KHR β€” property value: 0, HDC handle. Description: HDC an OpenGL context was created with respect to (available if the cl_khr_gl_sharing(3clc) extension is enabled). 🧚
  • CL_CONTEXT_D3D11_DEVICE_KHR β€” property value: ID3D11Device *. Description: Specifies the ID3D11Device * to use for Direct3D 11

okay bestie we're mid-table, mid-glow-up, so let's just keep going πŸ’…

the tail end of that properties table is gossiping about interoperability. The default value is NULL* (applies if the cl_khr_d3d11_sharing(3clc) extension is supported). ✨


πŸŽ€ device_type πŸŽ€

A bit-field that identifies the type of device and is described in the table below. (and yes, we're doing the table as a list because tables are not the vibe here, but every single row is intact, no cap πŸ’–)

  • CL_DEVICE_TYPE_CPU β€” An OpenCL device that is the host processor. The host processor runs the OpenCL implementations and is a single or multi-core CPU.
  • CL_DEVICE_TYPE_GPU β€” An OpenCL device that is a GPU. By this we mean that the device can also be used to accelerate a 3D API such as OpenGL or DirectX.
  • CL_DEVICE_TYPE_ACCELERATOR β€” Dedicated OpenCL accelerators (for example the IBM CELL Blade). These devices communicate with the host processor using a peripheral interconnect such as PCIe.
  • CL_DEVICE_TYPE_CUSTOM β€” Dedicated accelerators that do not support programs written in OpenCL C.
  • CL_DEVICE_TYPE_DEFAULT β€” The default OpenCL device in the system. The default device cannot be a CL_DEVICE_TYPE_CUSTOM device.
  • CL_DEVICE_TYPE_ALL β€” All OpenCL devices available in the system except CL_DEVICE_TYPE_CUSTOM devices.

πŸ¦‹ pfn_notify πŸ¦‹

A callback function that can be registered by the application. This callback function will be used by the OpenCL implementation to report information on errors that occur in this context. This callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe. If pfn_notify is NULL, no callback function is registered. The parameters to this callback function are:

  • errinfo is a pointer to an error string.
  • private_info and cb represent a pointer to binary data that is returned by the OpenCL implementation that can be used to log additional information helpful in debugging the error.
  • user_data is a pointer to user supplied data.

There are a number of cases where error notifications need to be delivered due to an error that occurs outside a context. Such notifications may not be delivered through the pfn_notify callback. Where these notifications go is implementation-defined. πŸ™ˆ


🌸 user_data 🌸

Passed as the user_data argument when pfn_notify is called. user_data can be NULL.

πŸ“ errcode_ret πŸ“

Return an appropriate error code. If errcode_ret is NULL, no error code is returned.


✨ DESCRIPTION ✨

Only devices that are returned by clGetDeviceIDs(3clc) for device_type are used to create the context. The context does not reference any sub-devices that may have been created from these devices. periodt.


πŸ’œ NOTES πŸ’œ

clCreateContextFromType may return all or a subset of the actual physical devices present in the platform and that match device_type. (yes bestie, subset, it's giving mysterious 😭)

clCreateContextFromType and clCreateContext(3clc) perform an implicit retain. This is very helpful for 3rd party libraries, which typically get a context passed to them by the application. However, it is possible that the application may delete the context without informing the library. Allowing functions to attach to (i.e. retain) and release a context solves the problem of a context being used by a library no longer being valid. 🫢


and we're still going, don't touch that dial πŸ’–


πŸ’€ ERRORS πŸ’€

Okay bestie, here's the tea on what goes wrong. If the context gets created all good, clCreateContextFromType hands you back a valid non-zero context and pops CL_SUCCESS into errcode_ret. But if it doesn't slay? You get a NULL back and one of these little heartbreaks sitting in errcode_ret:

  • CL_INVALID_PLATFORM β€” happens when properties is NULL and no platform could be selected, or when the platform value you put in properties isn't a valid platform. Plot twist: if the cl_khr_gl_sharing(3clc) extension is supported, this error is replaced (or not) by CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR and possibly CL_INVALID_OPERATION β€” see below, and section 9.6.4 of the spec for the full clarification. πŸ™ˆ
  • CL_INVALID_PROPERTY β€” if a context property name in properties isn't a supported property name, or the value you gave for a supported property name isn't valid, or you specified the same property name more than once. Not you being extra twice. 😭
  • CL_INVALID_VALUE β€” if pfn_notify is NULL but user_data is not NULL. They're a package deal, bestie.
  • CL_INVALID_DEVICE_TYPE β€” if device_type is not a valid value.
  • CL_DEVICE_NOT_AVAILABLE β€” if no devices matching device_type and the property values in properties are currently available. Like, they exist, but not right now. 🫠
  • CL_DEVICE_NOT_FOUND β€” if no devices matching device_type and the property values in properties were found at all.
  • CL_OUT_OF_RESOURCES β€” if there's a failure to allocate resources required by the OpenCL implementation on the device.
  • CL_OUT_OF_HOST_MEMORY β€” if there's a failure to allocate resources required by the OpenCL implementation on the host.
  • CL_INVALID_D3D10_DEVICE_KHR β€” if the Direct3D 10 device specified for interoperability isn't compatible with the devices against which the context is to be created (only when the cl_khr_d3d10_sharing(3clc) extension is enabled).
  • CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR β€” when an invalid OpenGL context or share group object handle is specified in properties, if the cl_khr_gl_sharing(3clc) extension is enabled.
  • CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR β€” if no OpenGL or OpenGL ES context or share group is specified in the attribute list, then memory objects may not be shared, and calling any of the commands in section 9.7 will result in this error (if the cl_khr_gl_sharing(3clc) extension is enabled). No sharing, no vibes.
  • CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR β€” if the cl_khr_gl_sharing(3clc) extension is enabled and a context was specified by any of the following means:
    • context specified for an EGL-based OpenGL ES or OpenGL implementation by setting the attributes CL_GL_CONTEXT_KHR and CL_EGL_DISPLAY_KHR.
    • context was specified for a GLX-based OpenGL implementation by setting the attributes CL_GL_CONTEXT_KHR and CL_GLX_DISPLAY_KHR.
    • context was specified for a WGL-based OpenGL implementation by setting the attributes CL_GL_CONTEXT_KHR and CL_WGL_HDC_KHR.

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 it doesn't support sharing data with OpenCL due to implementation restrictions.
Like, they're in different universes. It's giving long-distance and it's not working out. πŸ’”
  • CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR β€” if a share group was specified for a CGL-based OpenGL implementation by setting the attribute CL_CGL_SHAREGROUP_KHR, and the specified share group doesn't identify a valid CGL share group object (only if the cl_khr_gl_sharing(3clc) extension is enabled).
  • CL_INVALID_DX9_MEDIA_ADAPTER_KHR β€” if the media adapter specified for interoperability isn't compatible with the devices against which the context is to be created (only if the cl_khr_dx9_media_sharing(3clc) extension is supported).
  • CL_INVALID_ADAPTER_KHR β€” if any of the values of the properties CL_CONTEXT_ADAPTER_D3D9_KHR, CL_CONTEXT_ADAPTER_D3D9EX_KHR or CL_CONTEXT_ADAPTER_DXVA_KHR is non-NULL and doesn't specify a valid media adapter with which the cl_device_ids against which this context is to be created may interoperate. (Only if the cl_khr_dx9_media_sharing(3clc) extension is supported.)
  • CL_INVALID_OPERATION β€” if interoperability is specified by setting CL_CONTEXT_ADAPTER_D3D9_KHR, CL_CONTEXT_ADAPTER_D3D9EX_KHR or CL_CONTEXT_ADAPTER_DXVA_KHR to a non-NULL value, and interoperability with another graphics API is also specified. (Only if the cl_khr_dx9_media_sharing(3clc) extension is supported.) Two graphics APIs at once? Pick a lane. 🚫
  • CL_INVALID_D3D11_DEVICE_KHR β€” if the Direct3D 11 device specified for interoperability isn't compatible with the devices against which the context is to be created (only if the cl_khr_d3d11_sharing(3clc) extension is supported).
  • CL_INVALID_OPERATION β€” if Direct3D 11 interoperability is specified by setting CL_INVALID_D3D11_DEVICE_KHR to a non-NULL value, and interoperability with another graphics API is also specified. (Only if the cl_khr_d3d11_sharing(3clc) extension is supported.)
  • CL_INVALID_D3D11_DEVICE_KHR β€” if the value of the property CL_CONTEXT_D3D11_DEVICE_KHR is non-NULL and doesn't specify a valid Direct3D 11 device with which the cl_device_ids against which this context is to be created may interoperate. (Only if the cl_khr_d3d11_sharing(3clc) extension is supported.)

πŸ“œ SPECIFICATION πŸ“œ

OpenCL Specification[1]

1. OpenCL Specification β€” page 56, section 4.4 - Contexts ✨

πŸ‘€ SEE ALSO πŸ‘€

(Yes, clGetContextInfo(3clc) is in there twice. It said it twice because it meant it twice. Iconic behaviour, no notes. πŸ‘‘)


🫢 AUTHORS 🫢

The Khronos Group


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.


And that's the whole page, bestie!! πŸŽ€βœ¨ This little masterpiece was served up by The Khronos Group, dated 01/14/2021, and it lives in your man pages as CLCREATECONTEXTFROMT(3clc). Go forth and create contexts responsibly. πŸ’…πŸ¦‹πŸ’–

πŸ“¦ where this page comes from