0

With recent CUDA versions - 12.1 for sure, probably 12.0 as well - I get a warning message when compiling against the OpenCL headers:

#pragma message("cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 300 (OpenCL 3.0)")

Indeed, I haven't defined CL_TARGET_OPENCL_VERSION. But - why should I? I've worked with OpenCL for quite a few years, and the custom/standard has been that the headers make implicit assumptions about default versions, and if I want something else then I actively say so.

Why is it now required/expected for me to define this explicitly?


Note: This question is related.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

The Stack Overflow page you mention does state that:

  • the clinfo command reports the OpenCL version supported by your GPU.
  • the OpenCL version in the SDK is the maximum version supported by the SDK.

If your program is using a higher version of OpenCL than what your GPU supports (e.g., using OpenCL 2.0 when your GPU supports OpenCL 1.2), you might encounter errors or undefined behavior.

To avoid this, you can explicitly set the target OpenCL version in your program by defining CL_TARGET_OPENCL_VERSION before including the OpenCL headers.
For example, if you want to target OpenCL 1.2, you would define it as follows:

#define CL_TARGET_OPENCL_VERSION 120
#include <CL/cl.h>

This ensures that your program does not inadvertently use features from a version of OpenCL that is not supported by your GPU.

This is illustrated for instance in open-mpi/hwloc issue 319, which did define CL_TARGET_OPENCL_VERSION before CL/cl_ext.h, with the following comment:

opencl: hide OpenCL warning about unspecified target API

Latest OpenCL warn unless CL_TARGET_OPENCL_VERSION defines the API revision that we want.

Set it to 220 (current default, and first revision that looks at this).

  • Older versions would enable some deprecated functions.
  • Later versions may not be supported by all installations that look at CL_TARGET_OPENCL_VERSION.

We only use basic functions that have existed for ever anyway.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • "you might encounter errors or undefined behavior." <- But that's not what the warning tells me... – einpoklum Jun 27 '23 at 18:40
  • @einsupportsModeratorStrike I agree. I am just trying to answer "Why is it now required/expected for me to define this explicitly?". – VonC Jun 27 '23 at 19:08