0

I would love to be able to use C++ in my OpenCL kernels.

However, the OpenCL 3.0 host-side API documentation, Section 5.8.1, says:

5.8.1. Creating Program Objects

To creates a program object for a context and load source code into that object, call the function

cl_program clCreateProgramWithSource(
    cl_context context,
    cl_uint count,
    const char** strings,
    const size_t* lengths,
    cl_int* errcode_ret);

... snip ...

The source code specified by strings is either an OpenCL C program source, header or implementation-defined source for custom devices that support an online compiler. OpenCL C++ is not supported as an online-compiled kernel language through this interface.

So how am I supposed to compile OpenCL C++ kernel code? I can't find another API function that takes that as input.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

tl;dr: That is how you compile OpenCL-C++ code, but it only works under certain conditions.

Details:

Support for JIT compilation of OpenCL-C++ programs is an OpenCL extension, and is not supported/provided for by the core API.

If an OpenCL implementation support the cl_ext_cxx_for_opencl extension, then:

You start by querying your device to determine whether the compiler for that specific device supports OpenCL C++. Use the CL_DEVICE_CXX_FOR_OPENCL_NUMERIC_VERSION_EXT value for the cl_device_info code.

Assuming the device compiler does support OpenCL C++, you then can pass OpenCL code to clCreateProgramWithSource(). But you will have to explicitly have-cl-std=CLC++ as part of the command-line arguments to clCompileProgram() / clBuildProgram(), to actually have the compiler interpret your programs as OpenCL C++.

Within your OpenCL-C++ program, the value of the __OPENCL_CPP_VERSION__ is supposed to agree with the value of the CL_DEVICE_CXX_FOR_OPENCL_NUMERIC_VERSION_EXT device property on the host side.

einpoklum
  • 118,144
  • 57
  • 340
  • 684