From 94e07fc93af7b02bf9114ba58248608e2dd64dee Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Fri, 3 Sep 2021 21:10:20 -0700 Subject: [PATCH 01/10] remove CL_HPP_USE_CL_DEVICE_FISSION --- include/CL/opencl.hpp | 38 +++++++++----------------------------- tests/CMakeLists.txt | 2 +- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 5abcc5fa..6767ce31 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -33,8 +33,6 @@ * * Optional extension support * - * cl_ext_device_fission - * #define CL_HPP_USE_CL_DEVICE_FISSION * cl_khr_d3d10_sharing * #define CL_HPP_USE_DX_INTEROP * cl_khr_sub_groups @@ -404,10 +402,6 @@ # pragma message("opencl.hpp: USE_DX_INTEROP is deprecated. Define CL_HPP_USE_DX_INTEROP instead") # define CL_HPP_USE_DX_INTEROP #endif -#if !defined(CL_HPP_USE_CL_DEVICE_FISSION) && defined(USE_CL_DEVICE_FISSION) -# pragma message("opencl.hpp: USE_CL_DEVICE_FISSION is deprecated. Define CL_HPP_USE_CL_DEVICE_FISSION instead") -# define CL_HPP_USE_CL_DEVICE_FISSION -#endif #if !defined(CL_HPP_ENABLE_EXCEPTIONS) && defined(__CL_ENABLE_EXCEPTIONS) # pragma message("opencl.hpp: __CL_ENABLE_EXCEPTIONS is deprecated. Define CL_HPP_ENABLE_EXCEPTIONS instead") # define CL_HPP_ENABLE_EXCEPTIONS @@ -527,11 +521,6 @@ #error Visual studio 2013 or another C++11-supporting compiler required #endif -// -#if defined(CL_HPP_USE_CL_DEVICE_FISSION) || defined(CL_HPP_USE_CL_SUB_GROUPS_KHR) -#include -#endif - #if defined(__APPLE__) || defined(__MACOSX) #include #else @@ -1397,8 +1386,8 @@ inline cl_int getInfoHelper(Func f, cl_uint name, T* param, int, typename T::cl_ F(cl_program_info, CL_PROGRAM_SCOPE_GLOBAL_CTORS_PRESENT, cl_bool) \ F(cl_program_info, CL_PROGRAM_SCOPE_GLOBAL_DTORS_PRESENT, cl_bool) -#define CL_HPP_PARAM_NAME_DEVICE_FISSION_(F) \ - F(cl_device_info, CL_DEVICE_PARENT_DEVICE_EXT, cl_device_id) \ +#define CL_HPP_PARAM_NAME_DEVICE_FISSION_EXT_(F) \ + F(cl_device_info, CL_DEVICE_PARENT_DEVICE_EXT, cl::Device) \ F(cl_device_info, CL_DEVICE_PARTITION_TYPES_EXT, cl::vector) \ F(cl_device_info, CL_DEVICE_AFFINITY_DOMAINS_EXT, cl::vector) \ F(cl_device_info, CL_DEVICE_REFERENCE_COUNT_EXT , cl_uint) \ @@ -1504,9 +1493,9 @@ CL_HPP_PARAM_NAME_INFO_1_1_DEPRECATED_IN_2_0_(CL_HPP_DECLARE_PARAM_TRAITS_) CL_HPP_PARAM_NAME_INFO_1_2_DEPRECATED_IN_2_0_(CL_HPP_DECLARE_PARAM_TRAITS_) #endif // CL_HPP_MINIMUM_OPENCL_VERSION < 200 -#if defined(CL_HPP_USE_CL_DEVICE_FISSION) -CL_HPP_PARAM_NAME_DEVICE_FISSION_(CL_HPP_DECLARE_PARAM_TRAITS_); -#endif // CL_HPP_USE_CL_DEVICE_FISSION +#if defined(cl_ext_device_fission) +CL_HPP_PARAM_NAME_DEVICE_FISSION_EXT_(CL_HPP_DECLARE_PARAM_TRAITS_); +#endif // cl_ext_device_fission #if defined(cl_khr_extended_versioning) #if CL_HPP_TARGET_OPENCL_VERSION < 300 @@ -2294,7 +2283,6 @@ class Device : public detail::Wrapper return param; } - #if CL_HPP_TARGET_OPENCL_VERSION >= 210 /** * Return the current value of the host clock as seen by the device. @@ -2341,9 +2329,6 @@ class Device : public detail::Wrapper } #endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 - /** - * CL 1.2 version - */ #if CL_HPP_TARGET_OPENCL_VERSION >= 120 //! \brief Wrapper for clCreateSubDevices(). cl_int createSubDevices( @@ -2378,11 +2363,10 @@ class Device : public detail::Wrapper return CL_SUCCESS; } -#elif defined(CL_HPP_USE_CL_DEVICE_FISSION) +#endif -/** - * CL 1.1 version that uses device fission extension. - */ +#if defined(cl_ext_device_fission) + //! \brief Wrapper for clCreateSubDevices(). cl_int createSubDevices( const cl_device_partition_property_ext * properties, vector* devices) @@ -2424,7 +2408,7 @@ class Device : public detail::Wrapper } return CL_SUCCESS; } -#endif // defined(CL_HPP_USE_CL_DEVICE_FISSION) +#endif // defined(cl_ext_device_fission) }; CL_HPP_DEFINE_STATIC_MEMBER_ std::once_flag Device::default_initialized_; @@ -10327,10 +10311,6 @@ namespace compatibility { #undef CL_HPP_INIT_CL_EXT_FCN_PTR_ #undef CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_ -#if defined(CL_HPP_USE_CL_DEVICE_FISSION) -#undef CL_HPP_PARAM_NAME_DEVICE_FISSION_ -#endif // CL_HPP_USE_CL_DEVICE_FISSION - #undef CL_HPP_NOEXCEPT_ #undef CL_HPP_DEFINE_STATIC_MEMBER_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 560a8bd1..65e3bc6e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,7 +58,7 @@ set(TEST_SOURCES # TODO enable testing for OpenCL 1.0 and 1.1 foreach(VERSION 120 200 210 220 300) - foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_USE_CL_DEVICE_FISSION CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR CL_HPP_USE_CL_SUB_GROUPS_KHR CL_HPP_USE_IL_KHR) + foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_USE_CL_SUB_GROUPS_KHR CL_HPP_USE_IL_KHR) if(OPTION STREQUAL "") # The empty string means we're not setting any special option. set(UNDERSCORE_OPTION "${OPTION}") From 0a10f242ece7e5b7ddd562dcc49e617ce9473a80 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Mon, 6 Sep 2021 13:30:50 -0700 Subject: [PATCH 02/10] remove CL_HPP_USE_CL_SUB_GROUPS_KHR --- include/CL/opencl.hpp | 76 ++++++++++++++------------- tests/CMakeLists.txt | 2 +- tests/test_openclhpp.cpp | 109 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 42 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 6767ce31..57c1584d 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -35,8 +35,6 @@ * * cl_khr_d3d10_sharing * #define CL_HPP_USE_DX_INTEROP - * cl_khr_sub_groups - * #define CL_HPP_USE_CL_SUB_GROUPS_KHR * cl_khr_image2d_from_buffer * #define CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR * @@ -188,22 +186,6 @@ * applies to use of cl::Program construction and other program * build variants. * - * - CL_HPP_USE_CL_DEVICE_FISSION - * - * Enable the cl_ext_device_fission extension. - * - * - CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR - * - * Enable the cl_khr_image2d_from_buffer extension. - * - * - CL_HPP_USE_CL_SUB_GROUPS_KHR - * - * Enable the cl_khr_subgroups extension. - * - * - CL_HPP_USE_DX_INTEROP - * - * Enable the cl_khr_d3d10_sharing extension. - * * - CL_HPP_USE_IL_KHR * * Enable the cl_khr_il_program extension. @@ -817,9 +799,11 @@ static inline cl_int errHandler (cl_int err, const char * errStr = NULL) #if CL_HPP_TARGET_OPENCL_VERSION >= 120 #define __GET_KERNEL_ARG_INFO_ERR CL_HPP_ERR_STR_(clGetKernelArgInfo) #endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 -#if CL_HPP_TARGET_OPENCL_VERSION >= 200 +#if CL_HPP_TARGET_OPENCL_VERSION >= 210 #define __GET_KERNEL_SUB_GROUP_INFO_ERR CL_HPP_ERR_STR_(clGetKernelSubGroupInfo) -#endif // CL_HPP_TARGET_OPENCL_VERSION >= 200 +#else +#define __GET_KERNEL_SUB_GROUP_INFO_ERR CL_HPP_ERR_STR_(clGetKernelSubGroupInfoKHR) +#endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 #define __GET_KERNEL_WORK_GROUP_INFO_ERR CL_HPP_ERR_STR_(clGetKernelWorkGroupInfo) #define __GET_PROGRAM_INFO_ERR CL_HPP_ERR_STR_(clGetProgramInfo) #define __GET_PROGRAM_BUILD_INFO_ERR CL_HPP_ERR_STR_(clGetProgramBuildInfo) @@ -1462,9 +1446,9 @@ CL_HPP_PARAM_NAME_INFO_2_2_(CL_HPP_DECLARE_PARAM_TRAITS_) CL_HPP_PARAM_NAME_INFO_3_0_(CL_HPP_DECLARE_PARAM_TRAITS_) #endif // CL_HPP_TARGET_OPENCL_VERSION >= 300 -#if defined(CL_HPP_USE_CL_SUB_GROUPS_KHR) && CL_HPP_TARGET_OPENCL_VERSION < 210 +#if defined(cl_khr_subgroups) && CL_HPP_TARGET_OPENCL_VERSION < 210 CL_HPP_PARAM_NAME_INFO_SUBGROUP_KHR_(CL_HPP_DECLARE_PARAM_TRAITS_) -#endif // #if defined(CL_HPP_USE_CL_SUB_GROUPS_KHR) && CL_HPP_TARGET_OPENCL_VERSION < 210 +#endif // #if defined(cl_khr_subgroups) && CL_HPP_TARGET_OPENCL_VERSION < 210 #if defined(CL_HPP_USE_IL_KHR) && CL_HPP_TARGET_OPENCL_VERSION < 210 CL_HPP_PARAM_NAME_INFO_IL_KHR_(CL_HPP_DECLARE_PARAM_TRAITS_) @@ -6013,26 +5997,44 @@ class Kernel : public detail::Wrapper return param; } -#if (CL_HPP_TARGET_OPENCL_VERSION >= 200 && defined(CL_HPP_USE_CL_SUB_GROUPS_KHR)) || CL_HPP_TARGET_OPENCL_VERSION >= 210 +#if defined(cl_khr_subgroups) || CL_HPP_TARGET_OPENCL_VERSION >= 210 cl_int getSubGroupInfo(const cl::Device &dev, cl_kernel_sub_group_info name, const cl::NDRange &range, size_type* param) const { -#if CL_HPP_TARGET_OPENCL_VERSION >= 210 + cl_int error = CL_INVALID_OPERATION; - return detail::errHandler( - clGetKernelSubGroupInfo(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr), - __GET_KERNEL_SUB_GROUP_INFO_ERR); - -#else // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 +#if CL_HPP_TARGET_OPENCL_VERSION >= 210 + bool useCore = false; + if (error != CL_SUCCESS) { + // Run-time decision based on the device + cl_uint version = detail::getDevicePlatformVersion(dev()); + if (version >= 0x30000) { + // Optional support in OpenCL 3.0 or newer + cl_uint maxNumSubGroups = 0; + clGetDeviceInfo(dev(), CL_DEVICE_MAX_NUM_SUB_GROUPS, sizeof(maxNumSubGroups), &maxNumSubGroups, nullptr); + useCore = (maxNumSubGroups > 0); + } else { + // Supported in OpenCL 2.1 or newer + useCore = (version >= 0x20001); + } + if (useCore) { + error = clGetKernelSubGroupInfo(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr); + } + } +#endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 - typedef clGetKernelSubGroupInfoKHR_fn PFN_clGetKernelSubGroupInfoKHR; - static PFN_clGetKernelSubGroupInfoKHR pfn_clGetKernelSubGroupInfoKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clGetKernelSubGroupInfoKHR); +#if defined(cl_khr_subgroups) + if (error != CL_SUCCESS) { + typedef clGetKernelSubGroupInfoKHR_fn PFN_clGetKernelSubGroupInfoKHR; + static PFN_clGetKernelSubGroupInfoKHR pfn_clGetKernelSubGroupInfoKHR = NULL; + CL_HPP_INIT_CL_EXT_FCN_PTR_(clGetKernelSubGroupInfoKHR); - return detail::errHandler( - pfn_clGetKernelSubGroupInfoKHR(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr), - __GET_KERNEL_SUB_GROUP_INFO_ERR); + if (pfn_clGetKernelSubGroupInfoKHR) { + error = pfn_clGetKernelSubGroupInfoKHR(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr); + } + } +#endif // defined(cl_khr_subgroups) -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 + return detail::errHandler(error, __GET_KERNEL_SUB_GROUP_INFO_ERR); } template @@ -6045,7 +6047,7 @@ class Kernel : public detail::Wrapper } return param; } -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200 +#endif // defined(cl_khr_subgroups) || CL_HPP_TARGET_OPENCL_VERSION >= 210 #if CL_HPP_TARGET_OPENCL_VERSION >= 200 /*! \brief setArg overload taking a shared_ptr type diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 65e3bc6e..e5587487 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,7 +58,7 @@ set(TEST_SOURCES # TODO enable testing for OpenCL 1.0 and 1.1 foreach(VERSION 120 200 210 220 300) - foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_USE_CL_SUB_GROUPS_KHR CL_HPP_USE_IL_KHR) + foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_USE_IL_KHR) if(OPTION STREQUAL "") # The empty string means we're not setting any special option. set(UNDERSCORE_OPTION "${OPTION}") diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index ea179fa3..8cd48942 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -218,6 +218,24 @@ static cl_int clGetPlatformInfo_version_2_0( param_value_size_ret, "OpenCL 2.0 Mock"); } +/** + * A stub for clGetPlatformInfo that will only support querying + * CL_PLATFORM_VERSION, and will return version 2.1. + */ +static cl_int clGetPlatformInfo_version_2_1( + cl_platform_id id, + cl_platform_info param_name, + size_t param_value_size, + void *param_value, + size_t *param_value_size_ret, + int num_calls) +{ + (void) num_calls; + return clGetPlatformInfo_version( + id, param_name, param_value_size, param_value, + param_value_size_ret, "OpenCL 2.1 Mock"); +} + /** * A stub for clGetPlatformInfo that will only support querying * CL_PLATFORM_VERSION, and will return version 3.0. @@ -2373,12 +2391,69 @@ static cl_int clGetKernelSubGroupInfo_testSubGroups(cl_kernel kernel, } } -void testSubGroups() +void testGetKernelSubGroupInfo21() { -// TODO support testing cl_khr_subgroups on 2.0 #if CL_HPP_TARGET_OPENCL_VERSION >= 210 clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); - clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_1); + clGetKernelSubGroupInfo_StubWithCallback(clGetKernelSubGroupInfo_testSubGroups); + clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); + clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); + + cl::Kernel k(make_kernel(0)); + cl::Device d(make_device_id(0)); + cl_int err; + cl::NDRange ndrange(8, 8); + size_t res1 = k.getSubGroupInfo( + d, ndrange, &err); + size_t res2 = 0; + err = k.getSubGroupInfo( + d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); + + TEST_ASSERT_EQUAL(res1, 32); + TEST_ASSERT_EQUAL(res2, 2); +#endif +} + +static cl_int clGetDeviceInfo_MaxNumSubGroupsNonZero( + cl_device_id id, + cl_device_info param_name, + size_t param_value_size, + void *param_value, + size_t *param_value_size_ret, + int num_calls) +{ + (void)num_calls; + switch (param_name) { + case CL_DEVICE_PLATFORM: + if (param_value != NULL) { + *static_cast(param_value) = make_platform_id(0); + } + if (param_value_size_ret != NULL) { + *param_value_size_ret = sizeof(cl_platform_id); + } + return CL_SUCCESS; + case CL_DEVICE_MAX_NUM_SUB_GROUPS: + TEST_ASSERT_EQUAL(param_value_size, sizeof(cl_uint)); + TEST_ASSERT_NOT_EQUAL(param_value, NULL); + if (param_value != NULL) { + *static_cast(param_value) = 4; + } + if (param_value_size_ret != NULL) { + *param_value_size_ret = sizeof(cl_uint); + } + return CL_SUCCESS; + default: break; + } + TEST_FAIL(); + return CL_INVALID_OPERATION; +} + +void testGetKernelSubGroupInfo30() +{ +#if CL_HPP_TARGET_OPENCL_VERSION >= 300 + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_MaxNumSubGroupsNonZero); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_3_0); clGetKernelSubGroupInfo_StubWithCallback(clGetKernelSubGroupInfo_testSubGroups); clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); @@ -2398,6 +2473,33 @@ void testSubGroups() #endif } +void testGetKernelSubGroupInfoTryKHR() +{ +// Because this test generates an error it does not work with exceptions enabled +#if !defined(CL_HPP_ENABLE_EXCEPTIONS) + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); +#if defined(cl_khr_subgroups) + clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", NULL); + clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", NULL); +#endif + clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); + clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); + + cl::Kernel k(make_kernel(0)); + cl::Device d(make_device_id(0)); + cl_int err; + cl::NDRange ndrange(8, 8); + size_t res1 = k.getSubGroupInfo( + d, ndrange, &err); + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); + size_t res2 = 0; + err = k.getSubGroupInfo( + d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +#endif +} + /** * Stub implementation of clGetDeviceInfo that returns an absense of builtin kernels */ @@ -3035,5 +3137,4 @@ void testDevicePCIBusInfo_KHR() #endif } - } // extern "C" From dd8bcd0ba4863284ee69edf9b622c8cd44fb99fc Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Mon, 6 Sep 2021 14:29:10 -0700 Subject: [PATCH 03/10] remove CL_HPP_USE_IL_KHR --- include/CL/opencl.hpp | 104 +++++++++++++++++---------------- tests/CMakeLists.txt | 2 +- tests/test_openclhpp.cpp | 123 +++++++++++++++++++++------------------ 3 files changed, 119 insertions(+), 110 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 57c1584d..dc598fd1 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -186,10 +186,6 @@ * applies to use of cl::Program construction and other program * build variants. * - * - CL_HPP_USE_IL_KHR - * - * Enable the cl_khr_il_program extension. - * * * \section example Example * @@ -834,12 +830,11 @@ static inline cl_int errHandler (cl_int err, const char * errStr = NULL) #define __CREATE_KERNEL_ERR CL_HPP_ERR_STR_(clCreateKernel) #define __SET_KERNEL_ARGS_ERR CL_HPP_ERR_STR_(clSetKernelArg) #define __CREATE_PROGRAM_WITH_SOURCE_ERR CL_HPP_ERR_STR_(clCreateProgramWithSource) -#if CL_HPP_TARGET_OPENCL_VERSION >= 200 -#define __CREATE_PROGRAM_WITH_IL_ERR CL_HPP_ERR_STR_(clCreateProgramWithIL) -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200 #define __CREATE_PROGRAM_WITH_BINARY_ERR CL_HPP_ERR_STR_(clCreateProgramWithBinary) #if CL_HPP_TARGET_OPENCL_VERSION >= 210 #define __CREATE_PROGRAM_WITH_IL_ERR CL_HPP_ERR_STR_(clCreateProgramWithIL) +#else +#define __CREATE_PROGRAM_WITH_IL_ERR CL_HPP_ERR_STR_(clCreateProgramWithILKHR) #endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 #if CL_HPP_TARGET_OPENCL_VERSION >= 120 #define __CREATE_PROGRAM_WITH_BUILT_IN_KERNELS_ERR CL_HPP_ERR_STR_(clCreateProgramWithBuiltInKernels) @@ -1450,9 +1445,9 @@ CL_HPP_PARAM_NAME_INFO_3_0_(CL_HPP_DECLARE_PARAM_TRAITS_) CL_HPP_PARAM_NAME_INFO_SUBGROUP_KHR_(CL_HPP_DECLARE_PARAM_TRAITS_) #endif // #if defined(cl_khr_subgroups) && CL_HPP_TARGET_OPENCL_VERSION < 210 -#if defined(CL_HPP_USE_IL_KHR) && CL_HPP_TARGET_OPENCL_VERSION < 210 +#if defined(cl_khr_il_program) && CL_HPP_TARGET_OPENCL_VERSION < 210 CL_HPP_PARAM_NAME_INFO_IL_KHR_(CL_HPP_DECLARE_PARAM_TRAITS_) -#endif // #if defined(CL_HPP_USE_IL_KHR) +#endif // #if defined(cl_khr_il_program) && CL_HPP_TARGET_OPENCL_VERSION < 210 // Flags deprecated in OpenCL 2.0 @@ -1761,7 +1756,7 @@ static cl_uint getVersion(const vector &versionInfo) { int highVersion = 0; int lowVersion = 0; - int index = 7; + int index = 7; // 7 characters to skip "OpenCL " while(versionInfo[index] != '.' ) { highVersion *= 10; highVersion += versionInfo[index]-'0'; @@ -6005,18 +6000,9 @@ class Kernel : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 210 bool useCore = false; if (error != CL_SUCCESS) { - // Run-time decision based on the device + // Run-time decision: supported in OpenCL 2.1 or newer cl_uint version = detail::getDevicePlatformVersion(dev()); - if (version >= 0x30000) { - // Optional support in OpenCL 3.0 or newer - cl_uint maxNumSubGroups = 0; - clGetDeviceInfo(dev(), CL_DEVICE_MAX_NUM_SUB_GROUPS, sizeof(maxNumSubGroups), &maxNumSubGroups, nullptr); - useCore = (maxNumSubGroups > 0); - } else { - // Supported in OpenCL 2.1 or newer - useCore = (version >= 0x20001); - } - if (useCore) { + if (version >= 0x20001) { error = clGetKernelSubGroupInfo(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr); } } @@ -6397,7 +6383,7 @@ class Program : public detail::Wrapper } -#if CL_HPP_TARGET_OPENCL_VERSION >= 210 || (CL_HPP_TARGET_OPENCL_VERSION==200 && defined(CL_HPP_USE_IL_KHR)) +#if defined(cl_khr_il_program) || CL_HPP_TARGET_OPENCL_VERSION >= 210 /** * Program constructor to allow construction of program from SPIR-V or another IL. * Valid for either OpenCL >= 2.1 or when CL_HPP_USE_IL_KHR is defined. @@ -6407,25 +6393,33 @@ class Program : public detail::Wrapper bool build = false, cl_int* err = NULL) { - cl_int error; - Context context = Context::getDefault(err); -#if CL_HPP_TARGET_OPENCL_VERSION >= 210 - - object_ = ::clCreateProgramWithIL( - context(), static_cast(IL.data()), IL.size(), &error); - -#else // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 + cl_int error = CL_INVALID_OPERATION; - typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; - static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); +#if CL_HPP_TARGET_OPENCL_VERSION >= 210 + if (error != CL_SUCCESS) { + // Run-time decision: supported in OpenCL 2.1 or newer + cl_uint version = detail::getContextPlatformVersion(context()); + if (version >= 0x20001) { + object_ = ::clCreateProgramWithIL( + context(), static_cast(IL.data()), IL.size(), &error); + } + } +#endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 - object_ = pfn_clCreateProgramWithILKHR( - context(), static_cast(IL.data()), IL.size(), &error); +#if defined(cl_khr_il_program) + if (error != CL_SUCCESS) { + typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; + static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; + CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 + if (pfn_clCreateProgramWithILKHR) { + object_ = pfn_clCreateProgramWithILKHR( + context(), static_cast(IL.data()), IL.size(), &error); + } + } +#endif // defined(cl_khr_il_program) detail::errHandler(error, __CREATE_PROGRAM_WITH_IL_ERR); @@ -6462,23 +6456,31 @@ class Program : public detail::Wrapper bool build = false, cl_int* err = NULL) { - cl_int error; + cl_int error = CL_INVALID_OPERATION; #if CL_HPP_TARGET_OPENCL_VERSION >= 210 + if (error != CL_SUCCESS) { + // Run-time decision: supported in OpenCL 2.1 or newer + cl_uint version = detail::getContextPlatformVersion(context()); + if (version >= 0x20001) { + object_ = ::clCreateProgramWithIL( + context(), static_cast(IL.data()), IL.size(), &error); + } + } +#endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 - object_ = ::clCreateProgramWithIL( - context(), static_cast(IL.data()), IL.size(), &error); - -#else // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 - - typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; - static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); - - object_ = pfn_clCreateProgramWithILKHR( - context(), static_cast(IL.data()), IL.size(), &error); +#if defined(cl_khr_il_program) + if (error != CL_SUCCESS) { + typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; + static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; + CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 + if (pfn_clCreateProgramWithILKHR) { + object_ = pfn_clCreateProgramWithILKHR( + context(), static_cast(IL.data()), IL.size(), &error); + } + } +#endif // defined(cl_khr_il_program) detail::errHandler(error, __CREATE_PROGRAM_WITH_IL_ERR); @@ -6502,7 +6504,8 @@ class Program : public detail::Wrapper *err = error; } } -#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 210 +#endif // #if defined(cl_khr_il_program) || CL_HPP_TARGET_OPENCL_VERSION >= 210 + /** * Construct a program object from a list of devices and a per-device list of binaries. @@ -10248,7 +10251,6 @@ namespace compatibility { #undef __CREATE_KERNEL_ERR #undef __SET_KERNEL_ARGS_ERR #undef __CREATE_PROGRAM_WITH_SOURCE_ERR -#undef __CREATE_PROGRAM_WITH_IL_ERR #undef __CREATE_PROGRAM_WITH_BINARY_ERR #undef __CREATE_PROGRAM_WITH_IL_ERR #undef __CREATE_PROGRAM_WITH_BUILT_IN_KERNELS_ERR diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e5587487..9affc037 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,7 +58,7 @@ set(TEST_SOURCES # TODO enable testing for OpenCL 1.0 and 1.1 foreach(VERSION 120 200 210 220 300) - foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_USE_IL_KHR) + foreach(OPTION "" CL_HPP_ENABLE_EXCEPTIONS CL_HPP_ENABLE_SIZE_T_COMPATIBILITY CL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY CL_HPP_CL_1_2_DEFAULT_BUILD) if(OPTION STREQUAL "") # The empty string means we're not setting any special option. set(UNDERSCORE_OPTION "${OPTION}") diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index 8cd48942..7e79ca19 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -1772,6 +1772,71 @@ void testCopyHostToBuffer() } +/**************************************************************************** +* Tests for creating Programs +****************************************************************************/ + +static cl_program clCreateProgramWithIL_testCreate( + cl_context context, + const void* il, + size_t length, + cl_int* errcode_ret, + int num_calls) +{ + TEST_ASSERT_EQUAL(context, make_context(0)); + TEST_ASSERT_NOT_EQUAL(il, nullptr); + TEST_ASSERT_EQUAL(length, 128); + if (errcode_ret != NULL) { + *errcode_ret = CL_SUCCESS; + } + return make_program(0); +} + +void testCreateProgramWithIL21() +{ +#if CL_HPP_TARGET_OPENCL_VERSION >= 210 + std::vector il(128); + + clGetContextInfo_StubWithCallback(clGetContextInfo_device); + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_1); + clCreateProgramWithIL_StubWithCallback(clCreateProgramWithIL_testCreate); + clReleaseProgram_ExpectAndReturn(make_program(0), CL_SUCCESS); + clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); + + cl::Context context(make_context(0)); + + cl_int err = CL_INVALID_OPERATION; + cl::Program program(context, il, false, &err); + + TEST_ASSERT_EQUAL(err, CL_SUCCESS); +#endif +} + +void testCreateProgramWithILTryKHR() +{ +// Because this test generates an error it does not work with exceptions enabled +#if !defined(CL_HPP_ENABLE_EXCEPTIONS) + std::vector il(128); + + clGetContextInfo_StubWithCallback(clGetContextInfo_device); + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); +#if defined(cl_khr_il_program) + clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", NULL); +#endif + clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); + + cl::Context context(make_context(0)); + + cl_int err = CL_INVALID_OPERATION; + cl::Program program(context, il, false, &err); + + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +#endif +} + + /**************************************************************************** * Tests for building Programs ****************************************************************************/ @@ -2415,64 +2480,6 @@ void testGetKernelSubGroupInfo21() #endif } -static cl_int clGetDeviceInfo_MaxNumSubGroupsNonZero( - cl_device_id id, - cl_device_info param_name, - size_t param_value_size, - void *param_value, - size_t *param_value_size_ret, - int num_calls) -{ - (void)num_calls; - switch (param_name) { - case CL_DEVICE_PLATFORM: - if (param_value != NULL) { - *static_cast(param_value) = make_platform_id(0); - } - if (param_value_size_ret != NULL) { - *param_value_size_ret = sizeof(cl_platform_id); - } - return CL_SUCCESS; - case CL_DEVICE_MAX_NUM_SUB_GROUPS: - TEST_ASSERT_EQUAL(param_value_size, sizeof(cl_uint)); - TEST_ASSERT_NOT_EQUAL(param_value, NULL); - if (param_value != NULL) { - *static_cast(param_value) = 4; - } - if (param_value_size_ret != NULL) { - *param_value_size_ret = sizeof(cl_uint); - } - return CL_SUCCESS; - default: break; - } - TEST_FAIL(); - return CL_INVALID_OPERATION; -} - -void testGetKernelSubGroupInfo30() -{ -#if CL_HPP_TARGET_OPENCL_VERSION >= 300 - clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_MaxNumSubGroupsNonZero); - clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_3_0); - clGetKernelSubGroupInfo_StubWithCallback(clGetKernelSubGroupInfo_testSubGroups); - clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); - clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); - - cl::Kernel k(make_kernel(0)); - cl::Device d(make_device_id(0)); - cl_int err; - cl::NDRange ndrange(8, 8); - size_t res1 = k.getSubGroupInfo( - d, ndrange, &err); - size_t res2 = 0; - err = k.getSubGroupInfo( - d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); - - TEST_ASSERT_EQUAL(res1, 32); - TEST_ASSERT_EQUAL(res2, 2); -#endif -} - void testGetKernelSubGroupInfoTryKHR() { // Because this test generates an error it does not work with exceptions enabled From b9ab04411d9f230b5fba8a505a04c36297d6b91f Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Tue, 7 Sep 2021 13:55:54 -0700 Subject: [PATCH 04/10] add tests that actually use the extension functions This isn't perfect! Because the extension function pointers are stored in static variables these tests have state and need to execute in a specific order. --- tests/test_openclhpp.cpp | 129 +++++++++++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 13 deletions(-) diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index 7e79ca19..e5c595f6 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -1776,12 +1776,11 @@ void testCopyHostToBuffer() * Tests for creating Programs ****************************************************************************/ -static cl_program clCreateProgramWithIL_testCreate( +static cl_program clCreateProgramWithIL_func( cl_context context, const void* il, size_t length, - cl_int* errcode_ret, - int num_calls) + cl_int* errcode_ret) { TEST_ASSERT_EQUAL(context, make_context(0)); TEST_ASSERT_NOT_EQUAL(il, nullptr); @@ -1792,6 +1791,16 @@ static cl_program clCreateProgramWithIL_testCreate( return make_program(0); } +static cl_program clCreateProgramWithIL_testCreate( + cl_context context, + const void* il, + size_t length, + cl_int* errcode_ret, + int num_calls) +{ + return clCreateProgramWithIL_func(context, il, length, errcode_ret); +} + void testCreateProgramWithIL21() { #if CL_HPP_TARGET_OPENCL_VERSION >= 210 @@ -1815,8 +1824,6 @@ void testCreateProgramWithIL21() void testCreateProgramWithILTryKHR() { -// Because this test generates an error it does not work with exceptions enabled -#if !defined(CL_HPP_ENABLE_EXCEPTIONS) std::vector il(128); clGetContextInfo_StubWithCallback(clGetContextInfo_device); @@ -1829,10 +1836,37 @@ void testCreateProgramWithILTryKHR() cl::Context context(make_context(0)); + cl_int err = CL_INVALID_OPERATION; +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + try { +#endif + cl::Program program(context, il, false, &err); +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + } catch (cl::Error& e) { + TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); + } +#endif + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +} + +void testCreateProgramWithILUseKHR() +{ +#if defined(cl_khr_il_program) + std::vector il(128); + + clGetContextInfo_StubWithCallback(clGetContextInfo_device); + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); + clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", clCreateProgramWithIL_func); + clReleaseProgram_ExpectAndReturn(make_program(0), CL_SUCCESS); + clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); + + cl::Context context(make_context(0)); + cl_int err = CL_INVALID_OPERATION; cl::Program program(context, il, false, &err); - TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); + TEST_ASSERT_EQUAL(err, CL_SUCCESS); #endif } @@ -2423,15 +2457,15 @@ void testCreatePipe() #endif } -static cl_int clGetKernelSubGroupInfo_testSubGroups(cl_kernel kernel, +static cl_int clGetKernelSubGroupInfo_func( + cl_kernel kernel, cl_device_id device, cl_kernel_sub_group_info param_name, size_t input_value_size, const void *input_value, size_t param_value_size, void *param_value, - size_t *param_value_size_ret, - int num_calls) + size_t *param_value_size_ret) { TEST_ASSERT_NOT_NULL(input_value); TEST_ASSERT_NOT_NULL(param_value); @@ -2456,6 +2490,28 @@ static cl_int clGetKernelSubGroupInfo_testSubGroups(cl_kernel kernel, } } +static cl_int clGetKernelSubGroupInfo_testSubGroups( + cl_kernel kernel, + cl_device_id device, + cl_kernel_sub_group_info param_name, + size_t input_value_size, + const void *input_value, + size_t param_value_size, + void *param_value, + size_t *param_value_size_ret, + int num_calls) +{ + return clGetKernelSubGroupInfo_func( + kernel, + device, + param_name, + input_value_size, + input_value, + param_value_size, + param_value, + param_value_size_ret); +} + void testGetKernelSubGroupInfo21() { #if CL_HPP_TARGET_OPENCL_VERSION >= 210 @@ -2471,9 +2527,12 @@ void testGetKernelSubGroupInfo21() cl::NDRange ndrange(8, 8); size_t res1 = k.getSubGroupInfo( d, ndrange, &err); + TEST_ASSERT_EQUAL(err, CL_SUCCESS); + size_t res2 = 0; err = k.getSubGroupInfo( d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); + TEST_ASSERT_EQUAL(err, CL_SUCCESS); TEST_ASSERT_EQUAL(res1, 32); TEST_ASSERT_EQUAL(res2, 2); @@ -2482,8 +2541,6 @@ void testGetKernelSubGroupInfo21() void testGetKernelSubGroupInfoTryKHR() { -// Because this test generates an error it does not work with exceptions enabled -#if !defined(CL_HPP_ENABLE_EXCEPTIONS) clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); #if defined(cl_khr_subgroups) @@ -2493,20 +2550,66 @@ void testGetKernelSubGroupInfoTryKHR() clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); + cl::Kernel k(make_kernel(0)); + cl::Device d(make_device_id(0)); + cl::NDRange ndrange(8, 8); + cl_int err; + + size_t res1 = 0; +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + try { +#endif + res1 = k.getSubGroupInfo( + d, ndrange, &err); +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + } catch (cl::Error& e) { + TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); + } +#endif + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); + + size_t res2 = 0; +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + try { +#endif + err = k.getSubGroupInfo( + d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); +#if defined(CL_HPP_ENABLE_EXCEPTIONS) + } catch (cl::Error& e) { + TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); + } +#endif + TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +} + +void testGetKernelSubGroupInfoUseKHR() +{ +#if defined(cl_khr_subgroups) + clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); + clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); + clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", clGetKernelSubGroupInfo_func); + clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); + clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); + cl::Kernel k(make_kernel(0)); cl::Device d(make_device_id(0)); cl_int err; cl::NDRange ndrange(8, 8); size_t res1 = k.getSubGroupInfo( d, ndrange, &err); - TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); + TEST_ASSERT_EQUAL(err, CL_SUCCESS); + size_t res2 = 0; err = k.getSubGroupInfo( d, CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE_KHR, ndrange, &res2); - TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); + TEST_ASSERT_EQUAL(err, CL_SUCCESS); + + TEST_ASSERT_EQUAL(res1, 32); + TEST_ASSERT_EQUAL(res2, 2); #endif } + /** * Stub implementation of clGetDeviceInfo that returns an absense of builtin kernels */ From 4ee6855da80d7562c22c9fd7d54ddc7a2f47b3ba Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Tue, 7 Sep 2021 21:16:50 -0700 Subject: [PATCH 05/10] remove CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR --- include/CL/opencl.hpp | 16 ++++++++++++---- tests/test_openclhpp.cpp | 4 +--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index dc598fd1..3e665e0d 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -35,8 +35,6 @@ * * cl_khr_d3d10_sharing * #define CL_HPP_USE_DX_INTEROP - * cl_khr_image2d_from_buffer - * #define CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR * * Doxygen documentation for this header is available here: * @@ -1497,6 +1495,16 @@ CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_NODE_MASK_KHR, cl_uint) CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_PCI_BUS_INFO_KHR, cl_device_pci_bus_info_khr) #endif +// Note: some headers do not define cl_khr_image2d_from_buffer +#if CL_HPP_TARGET_OPENCL_VERSION < 200 +#if defined(CL_DEVICE_IMAGE_PITCH_ALIGNMENT_KHR) +CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_IMAGE_PITCH_ALIGNMENT_KHR, cl_uint) +#endif +#if defined(CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT_KHR) +CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT_KHR, cl_uint) +#endif +#endif // CL_HPP_TARGET_OPENCL_VERSION < 200 + #if defined(cl_khr_integer_dot_product) CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_INTEGER_DOT_PRODUCT_CAPABILITIES_KHR, cl_device_integer_dot_product_capabilities_khr) #if defined(CL_DEVICE_INTEGER_DOT_PRODUCT_ACCELERATION_PROPERTIES_8BIT_KHR) @@ -4803,10 +4811,11 @@ class Image2D : public Image #endif // CL_HPP_MINIMUM_OPENCL_VERSION < 120 } -#if CL_HPP_TARGET_OPENCL_VERSION >= 200 || defined(CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR) /*! \brief Constructs a 2D Image from a buffer. * \note This will share storage with the underlying buffer. * + * Requires OpenCL 2.0 or the cl_khr_image2d_from_buffer extension. + * * Wraps clCreateImage(). */ Image2D( @@ -4840,7 +4849,6 @@ class Image2D : public Image *err = error; } } -#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 200 || defined(CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR) #if CL_HPP_TARGET_OPENCL_VERSION >= 200 /*! \brief Constructs a 2D Image from an image. diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index e5c595f6..7e0e7c94 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -2095,9 +2095,8 @@ static cl_mem clCreateImage_testCreateImage2DFromBuffer_2_0( return image_desc->buffer; } -void testCreateImage2DFromBuffer_2_0() +void testCreateImage2DFromBuffer() { -#if CL_HPP_TARGET_OPENCL_VERSION >= 200 clGetContextInfo_StubWithCallback(clGetContextInfo_device); clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); @@ -2119,7 +2118,6 @@ void testCreateImage2DFromBuffer_2_0() TEST_ASSERT_EQUAL(CL_SUCCESS, err); buffer() = NULL; -#endif } static cl_mem clCreateImage_testCreateImage2D_2_0( From 46775f81a9a30e23c2c3c0503ed1293ab2ad1b17 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Tue, 7 Sep 2021 21:25:34 -0700 Subject: [PATCH 06/10] fix comments --- include/CL/opencl.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 3e665e0d..9a02f6fa 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -4814,7 +4814,7 @@ class Image2D : public Image /*! \brief Constructs a 2D Image from a buffer. * \note This will share storage with the underlying buffer. * - * Requires OpenCL 2.0 or the cl_khr_image2d_from_buffer extension. + * Requires OpenCL 2.0 or newer or the cl_khr_image2d_from_buffer extension. * * Wraps clCreateImage(). */ @@ -6394,7 +6394,8 @@ class Program : public detail::Wrapper #if defined(cl_khr_il_program) || CL_HPP_TARGET_OPENCL_VERSION >= 210 /** * Program constructor to allow construction of program from SPIR-V or another IL. - * Valid for either OpenCL >= 2.1 or when CL_HPP_USE_IL_KHR is defined. + * + * Requires OpenCL 2.1 or newer or the cl_khr_il_program extension. */ Program( const vector& IL, @@ -6456,7 +6457,8 @@ class Program : public detail::Wrapper /** * Program constructor to allow construction of program from SPIR-V or another IL * for a specific context. - * Valid for either OpenCL >= 2.1 or when CL_HPP_USE_IL_KHR is defined. + * + * Requires OpenCL 2.1 or newer or the cl_khr_il_program extension. */ Program( const Context& context, From 654308e17a12d92152838cbd63c0dfff3378e4d1 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Wed, 8 Sep 2021 07:51:41 -0700 Subject: [PATCH 07/10] fix Linux build --- tests/test_openclhpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index 7e0e7c94..a5e91165 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -1857,7 +1857,7 @@ void testCreateProgramWithILUseKHR() clGetContextInfo_StubWithCallback(clGetContextInfo_device); clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); - clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", clCreateProgramWithIL_func); + clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", (void*)clCreateProgramWithIL_func); clReleaseProgram_ExpectAndReturn(make_program(0), CL_SUCCESS); clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); @@ -2585,7 +2585,7 @@ void testGetKernelSubGroupInfoUseKHR() #if defined(cl_khr_subgroups) clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); - clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", clGetKernelSubGroupInfo_func); + clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", (void*)clGetKernelSubGroupInfo_func); clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); From 333e648f35e7bba83146d8b8ed6d4780eb9309ac Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Wed, 15 Sep 2021 11:14:28 -0700 Subject: [PATCH 08/10] remove unused variable --- include/CL/opencl.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 9a02f6fa..25489a5d 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -6006,7 +6006,6 @@ class Kernel : public detail::Wrapper cl_int error = CL_INVALID_OPERATION; #if CL_HPP_TARGET_OPENCL_VERSION >= 210 - bool useCore = false; if (error != CL_SUCCESS) { // Run-time decision: supported in OpenCL 2.1 or newer cl_uint version = detail::getDevicePlatformVersion(dev()); From b85ea1d566132529936cd96dca23e6a332fd37bb Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Mon, 4 Oct 2021 12:13:33 -0700 Subject: [PATCH 09/10] fix deprecation warnings, still have a few TODOs --- include/CL/opencl.hpp | 313 +++++++++++++++++++++------------------ tests/test_openclhpp.cpp | 24 +-- 2 files changed, 183 insertions(+), 154 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index 25489a5d..a735fa77 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -522,14 +522,14 @@ // Define deprecated prefixes and suffixes to ensure compilation // in case they are not pre-defined #if !defined(CL_API_PREFIX__VERSION_1_1_DEPRECATED) -#define CL_API_PREFIX__VERSION_1_1_DEPRECATED +#define CL_API_PREFIX__VERSION_1_1_DEPRECATED #endif // #if !defined(CL_API_PREFIX__VERSION_1_1_DEPRECATED) #if !defined(CL_API_SUFFIX__VERSION_1_1_DEPRECATED) #define CL_API_SUFFIX__VERSION_1_1_DEPRECATED #endif // #if !defined(CL_API_PREFIX__VERSION_1_1_DEPRECATED) #if !defined(CL_API_PREFIX__VERSION_1_2_DEPRECATED) -#define CL_API_PREFIX__VERSION_1_2_DEPRECATED +#define CL_API_PREFIX__VERSION_1_2_DEPRECATED #endif // #if !defined(CL_API_PREFIX__VERSION_1_2_DEPRECATED) #if !defined(CL_API_SUFFIX__VERSION_1_2_DEPRECATED) #define CL_API_SUFFIX__VERSION_1_2_DEPRECATED @@ -678,21 +678,35 @@ namespace cl { namespace cl { class Memory; -#define CL_HPP_INIT_CL_EXT_FCN_PTR_(name) \ - if (!pfn_##name) { \ - pfn_##name = (PFN_##name) \ - clGetExtensionFunctionAddress(#name); \ - if (!pfn_##name) { \ - } \ +#if CL_HPP_TARGET_OPENCL_VERSION < 120 +// Before OpenCL 1.2, use clGetExtensionFunctionAddress: +#define CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, name) \ + if (!pfn_##name) { \ + pfn_##name = (PFN_##name) \ + clGetExtensionFunctionAddress(#name); \ + } +#elif CL_HPP_MINIMUM_OPENCL_VERSION < 120 +// Before OpenCL 1.2, use clGetExtensionFunctionAddress, otherwise use +// clGetExtensionFunctionAddressForPlatform: +#define CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, name) \ + if (!pfn_##name) { \ + cl_uint version = detail::getPlatformVersion(platform); \ + if (version >= 0x10002) { \ + pfn_##name = (PFN_##name) \ + clGetExtensionFunctionAddressForPlatform(platform, #name);\ + } else { \ + pfn_##name = (PFN_##name) \ + clGetExtensionFunctionAddress(#name); \ + } \ } - -#define CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, name) \ - if (!pfn_##name) { \ - pfn_##name = (PFN_##name) \ - clGetExtensionFunctionAddressForPlatform(platform, #name); \ - if (!pfn_##name) { \ - } \ +#else +// For OpenCL 1.2 or newer use clGetExtensionFunctionAddressForPlatform: +#define CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, name) \ + if (!pfn_##name) { \ + pfn_##name = (PFN_##name) \ + clGetExtensionFunctionAddressForPlatform(platform, #name); \ } +#endif class Program; class Device; @@ -1758,7 +1772,9 @@ struct ReferenceHandler }; -#if CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 +// TODO: What should these ifdefs be? +//#if CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 + // Extracts version number with major in the upper 16 bits, minor in the lower 16 static cl_uint getVersion(const vector &versionInfo) { @@ -1789,26 +1805,29 @@ static cl_uint getPlatformVersion(cl_platform_id platform) return getVersion(versionInfo); } -static cl_uint getDevicePlatformVersion(cl_device_id device) +static cl_platform_id getDevicePlatform(cl_device_id device) { - cl_platform_id platform; + cl_platform_id platform = NULL; clGetDeviceInfo(device, CL_DEVICE_PLATFORM, sizeof(platform), &platform, NULL); - return getPlatformVersion(platform); + return platform; } -static cl_uint getContextPlatformVersion(cl_context context) +static cl_platform_id getContextPlatform(cl_context context) { // The platform cannot be queried directly, so we first have to grab a - // device and obtain its context + // device and the platform from it. size_type size = 0; clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size); - if (size == 0) - return 0; - vector devices(size/sizeof(cl_device_id)); - clGetContextInfo(context, CL_CONTEXT_DEVICES, size, devices.data(), NULL); - return getDevicePlatformVersion(devices[0]); + if (size != 0) { + vector devices(size/sizeof(cl_device_id)); + clGetContextInfo(context, CL_CONTEXT_DEVICES, size, devices.data(), NULL); + return getDevicePlatform(devices[0]); + } + return NULL; } -#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 + +// TODO: what should these ifdefs be? +//#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 template class Wrapper @@ -1920,8 +1939,9 @@ class Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 120 #if CL_HPP_MINIMUM_OPENCL_VERSION < 120 if (device != NULL) { - int version = getDevicePlatformVersion(device); - if(version > ((1 << 16) + 1)) { + cl_platform_id platform = getDevicePlatform(device); + cl_uint version = getPlatformVersion(platform); + if (version >= 0x10002) { retVal = true; } } @@ -2358,6 +2378,9 @@ class Device : public detail::Wrapper const cl_device_partition_property_ext * properties, vector* devices) { + cl_platform_id platform(detail::getDevicePlatform(object_)); + cl_int error = CL_INVALID_OPERATION; + typedef CL_API_ENTRY cl_int ( CL_API_CALL * PFN_clCreateSubDevicesEXT)( cl_device_id /*in_device*/, @@ -2367,33 +2390,37 @@ class Device : public detail::Wrapper cl_uint * /*num_devices*/ ) CL_API_SUFFIX__VERSION_1_1; static PFN_clCreateSubDevicesEXT pfn_clCreateSubDevicesEXT = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateSubDevicesEXT); + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clCreateSubDevicesEXT); - cl_uint n = 0; - cl_int err = pfn_clCreateSubDevicesEXT(object_, properties, 0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES_ERR); - } + if (pfn_clCreateSubDevicesEXT) { + cl_uint n = 0; + error = pfn_clCreateSubDevicesEXT(object_, properties, 0, NULL, &n); + if (error != CL_SUCCESS) { + return detail::errHandler(error, __CREATE_SUB_DEVICES_ERR); + } - vector ids(n); - err = pfn_clCreateSubDevicesEXT(object_, properties, n, ids.data(), NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES_ERR); - } - // Cannot trivially assign because we need to capture intermediates - // with safe construction - if (devices) { - devices->resize(ids.size()); + vector ids(n); + error = pfn_clCreateSubDevicesEXT(object_, properties, n, ids.data(), NULL); + if (error != CL_SUCCESS) { + return detail::errHandler(error, __CREATE_SUB_DEVICES_ERR); + } - // Assign to param, constructing with retain behaviour - // to correctly capture each underlying CL object - for (size_type i = 0; i < ids.size(); i++) { - // We do not need to retain because this device is being created - // by the runtime - (*devices)[i] = Device(ids[i], false); + // Cannot trivially assign because we need to capture intermediates + // with safe construction + if (devices) { + 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++) { + // We do not need to retain because this device is being created + // by the runtime + (*devices)[i] = Device(ids[i], false); + } } } - return CL_SUCCESS; + + return detail::errHandler(error, __CREATE_SUB_DEVICES_ERR); } #endif // defined(cl_ext_device_fission) }; @@ -4103,32 +4130,27 @@ class BufferD3D10 : public Buffer const Context& context, cl_mem_flags flags, ID3D10Buffer* bufobj, - cl_int * err = NULL) : pfn_clCreateFromD3D10BufferKHR(nullptr) + cl_int * err = NULL) { + cl_platform_id platform = detail::getContextPlatform(context()); + cl_int error = CL_INVALID_OPERATION; + typedef CL_API_ENTRY cl_mem (CL_API_CALL *PFN_clCreateFromD3D10BufferKHR)( cl_context context, cl_mem_flags flags, ID3D10Buffer* buffer, cl_int* errcode_ret); - PFN_clCreateFromD3D10BufferKHR pfn_clCreateFromD3D10BufferKHR; -#if CL_HPP_TARGET_OPENCL_VERSION >= 120 - vector props = context.getInfo(); - cl_platform platform = -1; - for( int i = 0; i < props.size(); ++i ) { - if( props[i] == CL_CONTEXT_PLATFORM ) { - platform = props[i+1]; - } - } - CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, clCreateFromD3D10BufferKHR); -#elif CL_HPP_TARGET_OPENCL_VERSION >= 110 - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateFromD3D10BufferKHR); -#endif + static PFN_clCreateFromD3D10BufferKHR pfn_clCreateFromD3D10BufferKHR = NULL; - cl_int error; - object_ = pfn_clCreateFromD3D10BufferKHR( - context(), - flags, - bufobj, - &error); + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clCreateFromD3D10BufferKHR); + if (pfn_clCreateFromD3D10BufferKHR) { + object_ = pfn_clCreateFromD3D10BufferKHR( + context(), + flags, + bufobj, + &error); + } + + // TODO: This should really have a D3D10 rerror code! detail::errHandler(error, __CREATE_GL_BUFFER_ERR); if (err != NULL) { *err = error; @@ -4765,7 +4787,8 @@ class Image2D : public Image #if CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getContextPlatform(context()); + cl_uint version = detail::getPlatformVersion(platform); useCreateImage = (version >= 0x10002); // OpenCL 1.2 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 120 @@ -4811,10 +4834,12 @@ class Image2D : public Image #endif // CL_HPP_MINIMUM_OPENCL_VERSION < 120 } +#if CL_HPP_TARGET_OPENCL_VERSION >= 120 /*! \brief Constructs a 2D Image from a buffer. * \note This will share storage with the underlying buffer. * - * Requires OpenCL 2.0 or newer or the cl_khr_image2d_from_buffer extension. + * Requires OpenCL 2.0 or newer or OpenCL 1.2 and the + * cl_khr_image2d_from_buffer extension. * * Wraps clCreateImage(). */ @@ -4849,6 +4874,7 @@ class Image2D : public Image *err = error; } } +#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 #if CL_HPP_TARGET_OPENCL_VERSION >= 200 /*! \brief Constructs a 2D Image from an image. @@ -5184,7 +5210,8 @@ class Image3D : public Image #if CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getContextPlatform(context()); + cl_uint version = detail::getPlatformVersion(platform); useCreateImage = (version >= 0x10002); // OpenCL 1.2 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 120 @@ -6003,29 +6030,30 @@ class Kernel : public detail::Wrapper #if defined(cl_khr_subgroups) || CL_HPP_TARGET_OPENCL_VERSION >= 210 cl_int getSubGroupInfo(const cl::Device &dev, cl_kernel_sub_group_info name, const cl::NDRange &range, size_type* param) const { + cl_platform_id platform = detail::getDevicePlatform(dev()); cl_int error = CL_INVALID_OPERATION; #if CL_HPP_TARGET_OPENCL_VERSION >= 210 if (error != CL_SUCCESS) { // Run-time decision: supported in OpenCL 2.1 or newer - cl_uint version = detail::getDevicePlatformVersion(dev()); + cl_uint version = detail::getPlatformVersion(platform); if (version >= 0x20001) { error = clGetKernelSubGroupInfo(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr); } } #endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 -#if defined(cl_khr_subgroups) +#if defined(cl_khr_subgroups) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 if (error != CL_SUCCESS) { typedef clGetKernelSubGroupInfoKHR_fn PFN_clGetKernelSubGroupInfoKHR; static PFN_clGetKernelSubGroupInfoKHR pfn_clGetKernelSubGroupInfoKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clGetKernelSubGroupInfoKHR); + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clGetKernelSubGroupInfoKHR); if (pfn_clGetKernelSubGroupInfoKHR) { error = pfn_clGetKernelSubGroupInfoKHR(object_, dev(), name, range.size(), range.get(), sizeof(size_type), param, nullptr); } } -#endif // defined(cl_khr_subgroups) +#endif // defined(cl_khr_subgroups) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 return detail::errHandler(error, __GET_KERNEL_SUB_GROUP_INFO_ERR); } @@ -6403,12 +6431,13 @@ class Program : public detail::Wrapper { Context context = Context::getDefault(err); + cl_platform_id platform = detail::getContextPlatform(context()); cl_int error = CL_INVALID_OPERATION; #if CL_HPP_TARGET_OPENCL_VERSION >= 210 if (error != CL_SUCCESS) { // Run-time decision: supported in OpenCL 2.1 or newer - cl_uint version = detail::getContextPlatformVersion(context()); + cl_uint version = detail::getPlatformVersion(platform); if (version >= 0x20001) { object_ = ::clCreateProgramWithIL( context(), static_cast(IL.data()), IL.size(), &error); @@ -6416,18 +6445,18 @@ class Program : public detail::Wrapper } #endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 -#if defined(cl_khr_il_program) +#if defined(cl_khr_il_program) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 if (error != CL_SUCCESS) { typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clCreateProgramWithILKHR); if (pfn_clCreateProgramWithILKHR) { object_ = pfn_clCreateProgramWithILKHR( context(), static_cast(IL.data()), IL.size(), &error); } } -#endif // defined(cl_khr_il_program) +#endif // defined(cl_khr_il_program) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 detail::errHandler(error, __CREATE_PROGRAM_WITH_IL_ERR); @@ -6465,12 +6494,13 @@ class Program : public detail::Wrapper bool build = false, cl_int* err = NULL) { + cl_platform_id platform = detail::getContextPlatform(context()); cl_int error = CL_INVALID_OPERATION; #if CL_HPP_TARGET_OPENCL_VERSION >= 210 if (error != CL_SUCCESS) { // Run-time decision: supported in OpenCL 2.1 or newer - cl_uint version = detail::getContextPlatformVersion(context()); + cl_uint version = detail::getPlatformVersion(platform); if (version >= 0x20001) { object_ = ::clCreateProgramWithIL( context(), static_cast(IL.data()), IL.size(), &error); @@ -6478,18 +6508,18 @@ class Program : public detail::Wrapper } #endif // CL_HPP_TARGET_OPENCL_VERSION >= 210 -#if defined(cl_khr_il_program) +#if defined(cl_khr_il_program) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 if (error != CL_SUCCESS) { typedef clCreateProgramWithILKHR_fn PFN_clCreateProgramWithILKHR; static PFN_clCreateProgramWithILKHR pfn_clCreateProgramWithILKHR = NULL; - CL_HPP_INIT_CL_EXT_FCN_PTR_(clCreateProgramWithILKHR); + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clCreateProgramWithILKHR); if (pfn_clCreateProgramWithILKHR) { object_ = pfn_clCreateProgramWithILKHR( context(), static_cast(IL.data()), IL.size(), &error); } } -#endif // defined(cl_khr_il_program) +#endif // defined(cl_khr_il_program) && CL_HPP_MINIMUM_OPENCL_VERSION < 210 detail::errHandler(error, __CREATE_PROGRAM_WITH_IL_ERR); @@ -7183,7 +7213,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(device()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -7249,7 +7280,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(device()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -7314,7 +7346,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(devices[0]()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -7381,7 +7414,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(devices[0]()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -7432,7 +7466,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(device()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -7483,7 +7518,8 @@ class CommandQueue : public detail::Wrapper #if CL_HPP_TARGET_OPENCL_VERSION >= 200 && CL_HPP_MINIMUM_OPENCL_VERSION < 200 // Run-time decision based on the actual platform { - cl_uint version = detail::getContextPlatformVersion(context()); + cl_platform_id platform = detail::getDevicePlatform(device()); + cl_uint version = detail::getPlatformVersion(platform); useWithProperties = (version >= 0x20000); // OpenCL 2.0 or above } #elif CL_HPP_TARGET_OPENCL_VERSION >= 200 @@ -8653,7 +8689,7 @@ class CommandQueue : public detail::Wrapper * Deprecated APIs for 1.2 */ #if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - CL_API_PREFIX__VERSION_1_1_DEPRECATED + CL_API_PREFIX__VERSION_1_1_DEPRECATED cl_int enqueueMarker(Event* event = NULL) const CL_API_SUFFIX__VERSION_1_1_DEPRECATED { cl_event tmp; @@ -8726,46 +8762,37 @@ class CommandQueue : public detail::Wrapper } #if defined (CL_HPP_USE_DX_INTEROP) -typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueAcquireD3D10ObjectsKHR)( - cl_command_queue command_queue, cl_uint num_objects, - const cl_mem* mem_objects, cl_uint num_events_in_wait_list, - const cl_event* event_wait_list, cl_event* event); -typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)( - cl_command_queue command_queue, cl_uint num_objects, - const cl_mem* mem_objects, cl_uint num_events_in_wait_list, - const cl_event* event_wait_list, cl_event* event); - cl_int enqueueAcquireD3D10Objects( const vector* mem_objects = NULL, const vector* events = NULL, Event* event = NULL) const { + cl_platform_id platform = detail::getDevicePlatform(getInfo()()); + cl_int error = CL_INVALID_OPERATION; + + typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueAcquireD3D10ObjectsKHR)( + cl_command_queue command_queue, cl_uint num_objects, + const cl_mem* mem_objects, cl_uint num_events_in_wait_list, + const cl_event* event_wait_list, cl_event* event); static PFN_clEnqueueAcquireD3D10ObjectsKHR pfn_clEnqueueAcquireD3D10ObjectsKHR = NULL; -#if CL_HPP_TARGET_OPENCL_VERSION >= 120 - cl_context context = getInfo(); - cl::Device device(getInfo()); - cl_platform_id platform = device.getInfo(); - 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 - - cl_event tmp; - cl_int err = detail::errHandler( - pfn_clEnqueueAcquireD3D10ObjectsKHR( - object_, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - (mem_objects != NULL && mem_objects->size() > 0) ? (const cl_mem *) &mem_objects->front(): NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_ACQUIRE_GL_ERR); - if (event != NULL && err == CL_SUCCESS) - *event = tmp; + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clEnqueueAcquireD3D10ObjectsKHR); - return err; + if (pfn_clEnqueueAcquireD3D10ObjectsKHR) { + cl_event tmp; + error = pfn_clEnqueueAcquireD3D10ObjectsKHR( + object_, + (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, + (mem_objects != NULL && mem_objects->size() > 0) ? (const cl_mem *) &mem_objects->front(): NULL, + (events != NULL) ? (cl_uint) events->size() : 0, + (events != NULL) ? (cl_event*) &events->front() : NULL, + (event != NULL) ? &tmp : NULL); + + if (event != NULL && error == CL_SUCCESS) + *event = tmp; + } + + return detail::errHandler(error, __ENQUEUE_ACQUIRE_GL_ERR); } cl_int enqueueReleaseD3D10Objects( @@ -8773,32 +8800,32 @@ typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)( const vector* events = NULL, Event* event = NULL) const { + cl_platform_id platform = detail::getDevicePlatform(getInfo()()); + cl_int error = CL_INVALID_OPERATION; + + typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)( + cl_command_queue command_queue, cl_uint num_objects, + const cl_mem* mem_objects, cl_uint num_events_in_wait_list, + const cl_event* event_wait_list, cl_event* event); static PFN_clEnqueueReleaseD3D10ObjectsKHR pfn_clEnqueueReleaseD3D10ObjectsKHR = NULL; -#if CL_HPP_TARGET_OPENCL_VERSION >= 120 - cl_context context = getInfo(); - cl::Device device(getInfo()); - cl_platform_id platform = device.getInfo(); - CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, clEnqueueReleaseD3D10ObjectsKHR); -#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 -#if CL_HPP_TARGET_OPENCL_VERSION >= 110 - CL_HPP_INIT_CL_EXT_FCN_PTR_(clEnqueueReleaseD3D10ObjectsKHR); -#endif // CL_HPP_TARGET_OPENCL_VERSION >= 110 - cl_event tmp; - cl_int err = detail::errHandler( - pfn_clEnqueueReleaseD3D10ObjectsKHR( + CL_HPP_INIT_CL_EXT_FCN_PTR_(platform, clEnqueueReleaseD3D10ObjectsKHR); + + if (pfn_clEnqueueReleaseD3D10ObjectsKHR) { + cl_event tmp; + cl_int err = pfn_clEnqueueReleaseD3D10ObjectsKHR( object_, (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, (mem_objects != NULL && mem_objects->size() > 0) ? (const cl_mem *) &mem_objects->front(): NULL, (events != NULL) ? (cl_uint) events->size() : 0, (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_RELEASE_GL_ERR); + (event != NULL) ? &tmp : NULL); - if (event != NULL && err == CL_SUCCESS) - *event = tmp; + if (event != NULL && err == CL_SUCCESS) + *event = tmp; + } - return err; + return detail::errHandler(error, __ENQUEUE_RELEASE_GL_ERR); } #endif @@ -10320,9 +10347,7 @@ namespace compatibility { #endif //CL_HPP_USER_OVERRIDE_ERROR_STRINGS -// Extensions #undef CL_HPP_INIT_CL_EXT_FCN_PTR_ -#undef CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_ #undef CL_HPP_NOEXCEPT_ #undef CL_HPP_DEFINE_STATIC_MEMBER_ diff --git a/tests/test_openclhpp.cpp b/tests/test_openclhpp.cpp index a5e91165..5601ee80 100644 --- a/tests/test_openclhpp.cpp +++ b/tests/test_openclhpp.cpp @@ -1830,13 +1830,13 @@ void testCreateProgramWithILTryKHR() clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); #if defined(cl_khr_il_program) - clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", NULL); + clGetExtensionFunctionAddressForPlatform_ExpectAndReturn(make_platform_id(0), "clCreateProgramWithILKHR", NULL); #endif clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); cl::Context context(make_context(0)); - cl_int err = CL_INVALID_OPERATION; + cl_int err = CL_SUCCESS; #if defined(CL_HPP_ENABLE_EXCEPTIONS) try { #endif @@ -1845,8 +1845,9 @@ void testCreateProgramWithILTryKHR() } catch (cl::Error& e) { TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); } -#endif +#else TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +#endif } void testCreateProgramWithILUseKHR() @@ -1857,7 +1858,7 @@ void testCreateProgramWithILUseKHR() clGetContextInfo_StubWithCallback(clGetContextInfo_device); clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); - clGetExtensionFunctionAddress_ExpectAndReturn("clCreateProgramWithILKHR", (void*)clCreateProgramWithIL_func); + clGetExtensionFunctionAddressForPlatform_ExpectAndReturn(make_platform_id(0), "clCreateProgramWithILKHR", (void*)clCreateProgramWithIL_func); clReleaseProgram_ExpectAndReturn(make_program(0), CL_SUCCESS); clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS); @@ -2542,8 +2543,8 @@ void testGetKernelSubGroupInfoTryKHR() clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); #if defined(cl_khr_subgroups) - clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", NULL); - clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", NULL); + clGetExtensionFunctionAddressForPlatform_ExpectAndReturn(make_platform_id(0), "clGetKernelSubGroupInfoKHR", NULL); + clGetExtensionFunctionAddressForPlatform_ExpectAndReturn(make_platform_id(0), "clGetKernelSubGroupInfoKHR", NULL); #endif clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); @@ -2551,8 +2552,8 @@ void testGetKernelSubGroupInfoTryKHR() cl::Kernel k(make_kernel(0)); cl::Device d(make_device_id(0)); cl::NDRange ndrange(8, 8); - cl_int err; + cl_int err = CL_SUCCESS; size_t res1 = 0; #if defined(CL_HPP_ENABLE_EXCEPTIONS) try { @@ -2563,9 +2564,11 @@ void testGetKernelSubGroupInfoTryKHR() } catch (cl::Error& e) { TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); } -#endif +#else TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +#endif + err = CL_SUCCESS; size_t res2 = 0; #if defined(CL_HPP_ENABLE_EXCEPTIONS) try { @@ -2576,8 +2579,9 @@ void testGetKernelSubGroupInfoTryKHR() } catch (cl::Error& e) { TEST_ASSERT_NOT_EQUAL(e.err(), CL_SUCCESS); } -#endif +#else TEST_ASSERT_NOT_EQUAL(err, CL_SUCCESS); +#endif } void testGetKernelSubGroupInfoUseKHR() @@ -2585,7 +2589,7 @@ void testGetKernelSubGroupInfoUseKHR() #if defined(cl_khr_subgroups) clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform); clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0); - clGetExtensionFunctionAddress_ExpectAndReturn("clGetKernelSubGroupInfoKHR", (void*)clGetKernelSubGroupInfo_func); + clGetExtensionFunctionAddressForPlatform_ExpectAndReturn(make_platform_id(0), "clGetKernelSubGroupInfoKHR", (void*)clGetKernelSubGroupInfo_func); clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS); clReleaseKernel_ExpectAndReturn(make_kernel(0), CL_SUCCESS); From 32ebc8ada580f75c8d817a15e0a65d2494c8aeb8 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Wed, 13 Oct 2021 22:36:45 -0700 Subject: [PATCH 10/10] fix the remaining TODOs The helper functions to get the platform version are used in so many places they cannot be protected by ifdefs for specific target OpenCL versions or minimum OpenCL versions. --- include/CL/opencl.hpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/include/CL/opencl.hpp b/include/CL/opencl.hpp index a735fa77..f9af96ce 100644 --- a/include/CL/opencl.hpp +++ b/include/CL/opencl.hpp @@ -1771,9 +1771,7 @@ struct ReferenceHandler { return ::clReleaseEvent(event); } }; - -// TODO: What should these ifdefs be? -//#if CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 +// Helper functions to get the platform version: // Extracts version number with major in the upper 16 bits, minor in the lower 16 static cl_uint getVersion(const vector &versionInfo) @@ -1826,9 +1824,6 @@ static cl_platform_id getContextPlatform(cl_context context) return NULL; } -// TODO: what should these ifdefs be? -//#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120 && CL_HPP_MINIMUM_OPENCL_VERSION < 120 - template class Wrapper {