From 32163b9703149de2a97f2cb56b957ebbb6f2223f Mon Sep 17 00:00:00 2001 From: Beatriz Navidad Vilches Date: Wed, 2 Oct 2024 10:41:34 +0200 Subject: [PATCH] Fix opencl version check --- samples/extensions/khr/externalmemory/main.c | 38 +++++++++++++++---- .../extensions/khr/externalmemory/main.cpp | 29 ++++++++++---- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/samples/extensions/khr/externalmemory/main.c b/samples/extensions/khr/externalmemory/main.c index d0d3bd28..1c466a25 100644 --- a/samples/extensions/khr/externalmemory/main.c +++ b/samples/extensions/khr/externalmemory/main.c @@ -217,6 +217,11 @@ bool cl_check_external_memory_handle_type( exit(EXIT_FAILURE); } +cl_int opencl_version_is_major(cl_name_version* dev_name_version, cl_uint major) +{ + return CL_VERSION_MAJOR(dev_name_version->version) == major; +} + int main(int argc, char* argv[]) { cl_int error = CL_SUCCESS; @@ -343,13 +348,32 @@ int main(int argc, char* argv[]) error, ker); // The Khronos extension showcased requires OpenCL 3.0 version. - char compiler_options[1023] = ""; -#if CL_HPP_TARGET_OPENCL_VERSION >= 300 - strcat(compiler_options, "-cl-std=CL3.0 "); -#else - fprintf(stderr, "\nError: OpenCL version must be at least 3.0\n"); - exit(EXIT_FAILURE); -#endif + // Get number of versions supported. + size_t versions_size = 0; + OCLERROR_RET(clGetDeviceInfo(cl_device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, 0, + NULL, &versions_size), + error, end); + size_t versions_count = versions_size / sizeof(cl_name_version); + + // Get and check versions. + cl_name_version* dev_versions = (cl_name_version*)malloc(versions_size); + OCLERROR_RET(clGetDeviceInfo(cl_device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, + versions_size, dev_versions, NULL), + error, end); + char compiler_options[1024] = ""; + for (cl_uint i = 0; i < versions_count; ++i) + { + if (opencl_version_is_major(&dev_versions[i], 3)) + { + strcat(compiler_options, "-cl-std=CL3.0 "); + } + } + + if (compiler_options[0] == '\0') + { + fprintf(stderr, "\nError: OpenCL version must be at least 3.0\n"); + exit(EXIT_FAILURE); + } OCLERROR_RET(cl_util_build_program(program, cl_device, compiler_options), error, prg); diff --git a/samples/extensions/khr/externalmemory/main.cpp b/samples/extensions/khr/externalmemory/main.cpp index 97b99a29..9cbaf433 100644 --- a/samples/extensions/khr/externalmemory/main.cpp +++ b/samples/extensions/khr/externalmemory/main.cpp @@ -138,6 +138,12 @@ bool cl_check_external_memory_handle_type( return it != supported_handle_types.end(); } +bool opencl_version_is_major(const cl_name_version& dev_name_version, + const cl_uint& major) +{ + return CL_VERSION_MAJOR(dev_name_version.version) == major; +} + int main(int argc, char* argv[]) { try @@ -267,13 +273,22 @@ int main(int argc, char* argv[]) // The Khronos extension showcased requires OpenCL 3.0 version. cl::string compiler_options = ""; -#if CL_HPP_TARGET_OPENCL_VERSION >= 300 - compiler_options += cl::string{ "-cl-std=CL3.0 " }; -#else - sdt::cerr << "\nError: OpenCL version must be at least 3.0" - << std::endl; - exit(EXIT_FAILURE); -#endif + std::vector dev_versions = + cl_device.getInfo(); + for (cl_name_version dev_name_version : dev_versions) + { + if (opencl_version_is_major(dev_name_version, 3)) + { + compiler_options += cl::string{ "-cl-std=CL3.0 " }; + } + } + + if (compiler_options.empty()) + { + std::cerr << "\nError: OpenCL version must be at least 3.0" + << std::endl; + exit(EXIT_FAILURE); + } cl_program.build(cl_device, compiler_options.c_str());