How can I compile OpenCL kernels written in OpenCL-C++?

huangapple go评论125阅读模式
英文:

How can I compile OpenCL kernels written in OpenCL-C++?

问题

我很想能够在我的OpenCL内核中使用C++。

然而,OpenCL 3.0主机端API文档,第5.8.1节,说道

> 5.8.1. 创建程序对象
>
> 要为上下文创建一个程序对象并将源代码加载到该对象中,请调用以下函数
>
> cl_program clCreateProgramWithSource(
> cl_context context,
> cl_uint count,
> const char** strings,
> const size_t* lengths,
> cl_int* errcode_ret);
>
... 省略部分 ...
>
> 由字符串指定的源代码可以是OpenCL C程序源代码、头文件或支持在线编译器的自定义设备的实现定义源代码。但是,OpenCL C++不受此接口支持作为在线编译的内核语言。

那么,我应该如何编译OpenCL C++内核代码呢?我找不到另一个接受此内容作为输入的API函数。

英文:

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.

答案1

得分: 1

简要概括: 这确实是如何编译OpenCL-C++代码的方式,但只在特定条件下有效。

详细信息:

对于OpenCL-C++程序的即时编译支持是一项OpenCL扩展,不受核心API支持或提供。

如果OpenCL实现支持 cl_ext_cxx_for_opencl 扩展,那么:

您可以通过查询设备来确定特定设备的编译器是否支持OpenCL C++,使用cl_device_info代码的CL_DEVICE_CXX_FOR_OPENCL_NUMERIC_VERSION_EXT值。

假设设备编译器确实支持OpenCL C++,然后您可以将OpenCL代码传递给 clCreateProgramWithSource()。但是,您必须在clCompileProgram() / clBuildProgram()的命令行参数中明确包含-cl-std=CLC++,以使编译器实际解释您的程序为OpenCL C++。

在您的OpenCL-C++程序中,__OPENCL_CPP_VERSION__ 的值应与主机端的CL_DEVICE_CXX_FOR_OPENCL_NUMERIC_VERSION_EXT设备属性的值相符。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月10日 00:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定