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 2, 2024
1 parent 8d99909 commit 7101a17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
28 changes: 21 additions & 7 deletions samples/extensions/khr/externalmemory/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ bool cl_check_external_memory_handle_type(
exit(EXIT_FAILURE);
}

cl_int opencl_version_contains(const char* dev_version,
const char* version_fragment)
{
char* found_version = strstr(dev_version, version_fragment);
return (found_version != NULL);
}

int main(int argc, char* argv[])
{
cl_int error = CL_SUCCESS;
Expand Down Expand Up @@ -343,13 +350,20 @@ 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
char dev_version[64];
OCLERROR_RET(clGetDeviceInfo(cl_device, CL_DEVICE_OPENCL_C_VERSION,
sizeof(dev_version), &dev_version, NULL),
error, end);
char compiler_options[1024] = "";
if (opencl_version_contains(dev_version, "3."))
{
strcat(compiler_options, "-cl-std=CL3.0 ");
}
else
{
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
25 changes: 18 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_contains(const cl::string& dev_version,
const cl::string& version_fragment)
{
return dev_version.find(version_fragment) != cl::string::npos;
}

int main(int argc, char* argv[])
{
try
Expand Down Expand Up @@ -267,13 +273,18 @@ 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
const std::string dev_version =
cl_device.getInfo<CL_DEVICE_OPENCL_C_VERSION>();
if (opencl_version_contains(dev_version, "3."))
{
cl::string{ "-cl-std=CL3.0 " };
}
else
{
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 7101a17

Please sign in to comment.