Skip to content

Commit

Permalink
Fix opencl version check
Browse files Browse the repository at this point in the history
  • Loading branch information
Beanavil committed Oct 3, 2024
1 parent 8d99909 commit 56cdc27
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
38 changes: 31 additions & 7 deletions samples/extensions/khr/externalmemory/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 22 additions & 7 deletions samples/extensions/khr/externalmemory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<cl_name_version> dev_versions =
cl_device.getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
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());

Expand Down

0 comments on commit 56cdc27

Please sign in to comment.