From a52e4d6cf4848711245da19f19db83981e833648 Mon Sep 17 00:00:00 2001 From: Richard Kellnberger Date: Tue, 30 Sep 2025 19:34:30 +0200 Subject: [PATCH 1/2] pass vectors by reference in getters closes #331 --- include/CL/opencl.hpp | 139 +++++++++++++++++++++++++++++------------- 1 file changed, 98 insertions(+), 41 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index ba4edafb..fd2da7b3 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -2882,12 +2882,9 @@ class Platform : public detail::Wrapper */ cl_int getDevices( cl_device_type type, - vector* devices) const + vector& devices) const { cl_uint n = 0; - if( devices == nullptr ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); - } cl_int err = CL_(clGetDeviceIDs)(object_, type, 0, nullptr, &n); if (err != CL_SUCCESS && err != CL_DEVICE_NOT_FOUND) { return detail::errHandler(err, __GET_DEVICE_IDS_ERR); @@ -2905,14 +2902,34 @@ class Platform : public detail::Wrapper // with safe construction // We must retain things we obtain from the API to avoid releasing // API-owned objects. - if (devices) { - devices->resize(ids.size()); + devices.resize(ids.size()); - // Assign to param, constructing with retain behaviour - // to correctly capture each underlying CL object - for (size_type i = 0; i < ids.size(); i++) { - (*devices)[i] = Device(ids[i], true); - } + // Assign to param, constructing with retain behaviour + // to correctly capture each underlying CL object + for (size_type i = 0; i < ids.size(); i++) { + devices[i] = Device(ids[i], true); + } + return CL_SUCCESS; + } + + /*! \brief Gets a list of devices for this platform. + * + * Pointer overload for backwards compatibility. + */ + cl_int getDevices( + cl_device_type type, + vector* devices) const + { + if( devices == nullptr ) { + return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); + } + + // Cannot trivially assign because we need to capture intermediates + // with safe construction + // We must retain things we obtain from the API to avoid releasing + // API-owned objects. + if (devices) { + return getDevices(type, *devices); } return CL_SUCCESS; } @@ -2945,7 +2962,7 @@ class Platform : public detail::Wrapper cl_d3d10_device_source_khr d3d_device_source, void * d3d_object, cl_d3d10_device_set_khr d3d_device_set, - vector* devices) const + vector& devices) const { typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clGetDeviceIDsFromD3D10KHR)( cl_platform_id platform, @@ -2956,10 +2973,6 @@ class Platform : public detail::Wrapper cl_device_id * devices, cl_uint* num_devices); - if( devices == nullptr ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); - } - static PFN_clGetDeviceIDsFromD3D10KHR pfn_clGetDeviceIDsFromD3D10KHR = nullptr; #if CL_HPP_TARGET_OPENCL_VERSION >= 120 CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(object_, clGetDeviceIDsFromD3D10KHR); @@ -2998,14 +3011,36 @@ class Platform : public detail::Wrapper // with safe construction // We must retain things we obtain from the API to avoid releasing // API-owned objects. - if (devices) { - devices->resize(ids.size()); + devices.resize(ids.size()); - // Assign to param, constructing with retain behaviour - // to correctly capture each underlying CL object - for (size_type i = 0; i < ids.size(); i++) { - (*devices)[i] = Device(ids[i], true); - } + // Assign to param, constructing with retain behaviour + // to correctly capture each underlying CL object + for (size_type i = 0; i < ids.size(); i++) { + devices[i] = Device(ids[i], true); + } + return CL_SUCCESS; + } + + /*! \brief Get the list of available D3D10 devices. + * + * Pointer overload for backwards compatibility. + */ + cl_int getDevices( + cl_d3d10_device_source_khr d3d_device_source, + void * d3d_object, + cl_d3d10_device_set_khr d3d_device_set, + vector* devices) const + { + if( devices == nullptr ) { + return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); + } + + // Cannot trivially assign because we need to capture intermediates + // with safe construction + // We must retain things we obtain from the API to avoid releasing + // API-owned objects. + if (devices) { + return getDevices(d3d_device_source, d3d_object, d3d_device_set, *devices); } return CL_SUCCESS; } @@ -3016,14 +3051,10 @@ class Platform : public detail::Wrapper * Wraps clGetPlatformIDs(). */ static cl_int get( - vector* platforms) + vector& platforms) { cl_uint n = 0; - if( platforms == nullptr ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_PLATFORM_IDS_ERR); - } - cl_int err = CL_(clGetPlatformIDs)(0, nullptr, &n); if (err != CL_SUCCESS) { return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); @@ -3035,13 +3066,28 @@ class Platform : public detail::Wrapper return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); } - if (platforms) { - platforms->resize(ids.size()); + platforms.resize(ids.size()); - // Platforms don't reference count - for (size_type i = 0; i < ids.size(); i++) { - (*platforms)[i] = Platform(ids[i]); - } + // Platforms don't reference count + for (size_type i = 0; i < ids.size(); i++) { + platforms[i] = Platform(ids[i]); + } + return CL_SUCCESS; + } + + /*! \brief Gets a list of available platforms. + * + * Pointer overload for backwards compatibility. + */ + static cl_int get( + vector* platforms) + { + if( platforms == nullptr ) { + return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_PLATFORM_IDS_ERR); + } + + if (platforms) { + return get(*platforms); } return CL_SUCCESS; } @@ -3572,13 +3618,9 @@ class Context cl_int getSupportedImageFormats( cl_mem_flags flags, cl_mem_object_type type, - vector* formats) const + vector& formats) const { cl_uint numEntries; - - if (!formats) { - return CL_SUCCESS; - } cl_int err = CL_(clGetSupportedImageFormats)( object_, @@ -3604,16 +3646,31 @@ class Context return detail::errHandler(err, __GET_SUPPORTED_IMAGE_FORMATS_ERR); } - formats->assign(value.begin(), value.end()); + formats.assign(value.begin(), value.end()); } else { // If no values are being returned, ensure an empty vector comes back - formats->clear(); + formats.clear(); } return CL_SUCCESS; } + /*! \brief Gets a list of supported image formats. + * + * Pointer overload for backwards compatibility. + */ + cl_int getSupportedImageFormats( + cl_mem_flags flags, + cl_mem_object_type type, + vector* formats) const + { + if (!formats) { + return CL_SUCCESS; + } + return getSupportedImageFormats(flags, type, *formats); + } + #if defined(cl_ext_image_requirements_info) template cl_int getImageRequirementsInfoExt(cl_image_requirements_info_ext name, From 8dab0db783bce49781e55dd2500a9f1711b6719a Mon Sep 17 00:00:00 2001 From: Richard Kellnberger Date: Wed, 1 Oct 2025 19:55:54 +0200 Subject: [PATCH 2/2] remove redundant comments and checks --- include/CL/opencl.hpp | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index fd2da7b3..3179afac 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -2924,14 +2924,7 @@ class Platform : public detail::Wrapper return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); } - // Cannot trivially assign because we need to capture intermediates - // with safe construction - // We must retain things we obtain from the API to avoid releasing - // API-owned objects. - if (devices) { - return getDevices(type, *devices); - } - return CL_SUCCESS; + return getDevices(type, *devices); } #if defined(CL_HPP_USE_DX_INTEROP) @@ -3035,14 +3028,7 @@ class Platform : public detail::Wrapper return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); } - // Cannot trivially assign because we need to capture intermediates - // with safe construction - // We must retain things we obtain from the API to avoid releasing - // API-owned objects. - if (devices) { - return getDevices(d3d_device_source, d3d_object, d3d_device_set, *devices); - } - return CL_SUCCESS; + return getDevices(d3d_device_source, d3d_object, d3d_device_set, *devices); } #endif @@ -3086,10 +3072,7 @@ class Platform : public detail::Wrapper return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_PLATFORM_IDS_ERR); } - if (platforms) { - return get(*platforms); - } - return CL_SUCCESS; + return get(*platforms); } /*! \brief Gets the first available platform.