OpenCL-CLHPP icon indicating copy to clipboard operation
OpenCL-CLHPP copied to clipboard

how to handle extension API function pointers

Open bashbaug opened this issue 2 years ago • 0 comments

This issue is slightly related to #146, which also has to do with handling extensions. #146 seems to have more to do with extension defined than extension function pointers though, so I am filing a separate issue instead. If desired, we can combine them.

TL;DR: We should rethink how we handle extension API function pointers in these C++ bindings, to improve functionality, performance, and maintainability.

Currently when we want to call an extension API we query the function pointer, store it in a (sometimes static) variable, and call it. Here is one example:

        static PFN_clEnqueueAcquireD3D10ObjectsKHR pfn_clEnqueueAcquireD3D10ObjectsKHR = NULL;
#if CL_HPP_TARGET_OPENCL_VERSION >= 120
        cl_context context = getInfo<CL_QUEUE_CONTEXT>();
        cl::Device device(getInfo<CL_QUEUE_DEVICE>());
        cl_platform_id platform = device.getInfo<CL_DEVICE_PLATFORM>();
        CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, clEnqueueAcquireD3D10ObjectsKHR);
#endif
#if CL_HPP_TARGET_OPENCL_VERSION >= 110
        CL_HPP_INIT_CL_EXT_FCN_PTR_(clEnqueueAcquireD3D10ObjectsKHR);
#endif

Some issues with this code are:

  • Calling an extension API takes multiple lines of code and is error-prone and inconsistent.
  • Storing in a static variable means the same extension API can only be called for one platform.
  • Storing in a static variable complicates unit testing.
  • Storing in a non-static variable instead requires costly queries per-call, both to query the platform and to query the function pointer itself.

Can we do better? Some non-mutually-exclusive options:

  • Support libraries like the OpenCL Extension Loader when an extra dependency is acceptable.
  • Move extension queries and function definitions to a separate header file, to centralize queries while still providing an all-in-one solution.

bashbaug avatar Mar 10 '23 19:03 bashbaug