Skip to content

Commit 32163b9

Browse files
committed
Fix opencl version check
1 parent c1bfcd9 commit 32163b9

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

samples/extensions/khr/externalmemory/main.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ bool cl_check_external_memory_handle_type(
217217
exit(EXIT_FAILURE);
218218
}
219219

220+
cl_int opencl_version_is_major(cl_name_version* dev_name_version, cl_uint major)
221+
{
222+
return CL_VERSION_MAJOR(dev_name_version->version) == major;
223+
}
224+
220225
int main(int argc, char* argv[])
221226
{
222227
cl_int error = CL_SUCCESS;
@@ -343,13 +348,32 @@ int main(int argc, char* argv[])
343348
error, ker);
344349

345350
// The Khronos extension showcased requires OpenCL 3.0 version.
346-
char compiler_options[1023] = "";
347-
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
348-
strcat(compiler_options, "-cl-std=CL3.0 ");
349-
#else
350-
fprintf(stderr, "\nError: OpenCL version must be at least 3.0\n");
351-
exit(EXIT_FAILURE);
352-
#endif
351+
// Get number of versions supported.
352+
size_t versions_size = 0;
353+
OCLERROR_RET(clGetDeviceInfo(cl_device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, 0,
354+
NULL, &versions_size),
355+
error, end);
356+
size_t versions_count = versions_size / sizeof(cl_name_version);
357+
358+
// Get and check versions.
359+
cl_name_version* dev_versions = (cl_name_version*)malloc(versions_size);
360+
OCLERROR_RET(clGetDeviceInfo(cl_device, CL_DEVICE_OPENCL_C_ALL_VERSIONS,
361+
versions_size, dev_versions, NULL),
362+
error, end);
363+
char compiler_options[1024] = "";
364+
for (cl_uint i = 0; i < versions_count; ++i)
365+
{
366+
if (opencl_version_is_major(&dev_versions[i], 3))
367+
{
368+
strcat(compiler_options, "-cl-std=CL3.0 ");
369+
}
370+
}
371+
372+
if (compiler_options[0] == '\0')
373+
{
374+
fprintf(stderr, "\nError: OpenCL version must be at least 3.0\n");
375+
exit(EXIT_FAILURE);
376+
}
353377

354378
OCLERROR_RET(cl_util_build_program(program, cl_device, compiler_options),
355379
error, prg);

samples/extensions/khr/externalmemory/main.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ bool cl_check_external_memory_handle_type(
138138
return it != supported_handle_types.end();
139139
}
140140

141+
bool opencl_version_is_major(const cl_name_version& dev_name_version,
142+
const cl_uint& major)
143+
{
144+
return CL_VERSION_MAJOR(dev_name_version.version) == major;
145+
}
146+
141147
int main(int argc, char* argv[])
142148
{
143149
try
@@ -267,13 +273,22 @@ int main(int argc, char* argv[])
267273

268274
// The Khronos extension showcased requires OpenCL 3.0 version.
269275
cl::string compiler_options = "";
270-
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
271-
compiler_options += cl::string{ "-cl-std=CL3.0 " };
272-
#else
273-
sdt::cerr << "\nError: OpenCL version must be at least 3.0"
274-
<< std::endl;
275-
exit(EXIT_FAILURE);
276-
#endif
276+
std::vector<cl_name_version> dev_versions =
277+
cl_device.getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
278+
for (cl_name_version dev_name_version : dev_versions)
279+
{
280+
if (opencl_version_is_major(dev_name_version, 3))
281+
{
282+
compiler_options += cl::string{ "-cl-std=CL3.0 " };
283+
}
284+
}
285+
286+
if (compiler_options.empty())
287+
{
288+
std::cerr << "\nError: OpenCL version must be at least 3.0"
289+
<< std::endl;
290+
exit(EXIT_FAILURE);
291+
}
277292

278293
cl_program.build(cl_device, compiler_options.c_str());
279294

0 commit comments

Comments
 (0)