用于OpenCL的C++

文章介绍了C++forOpenCL,一种社区开发的内核语言,它扩展了OpenCL的功能,使得开发者能在保持OpenCL结构的同时使用C++特性。C++forOpenCL支持C++17,并提供离线编译选项,兼容不同OpenCL版本和驱动。
摘要由CSDN通过智能技术生成

C++ for OpenCL

用于OpenCL的C++

The OpenCL working group has transitioned from the original OpenCL C++ kernel language first defined in OpenCL 2.2 to the community developed C++ for OpenCL kernel language that provides improved features and compatibility with OpenCL C.

​OpenCL工作组已经从最初在OpenCL 2.2中定义的OpenCL C++内核语言过渡到为OpenCL内核语言开发的社区C++,该语言提供了改进的功能和与OpenCL C的兼容性。

C++ for OpenCL enables developers to use most C++ features in kernel code while keeping familiar OpenCL constructs, syntax, and semantics from OpenCL C. This facilitates a smooth transition to new C++ features in existing OpenCL applications and does not require changing familiar development flows or tools. The main design goal of C++ for OpenCL is to reapply OpenCL-specific concepts to C++ in the same way as OpenCL C applies them to C. Aside from minor exceptions OpenCL C is a valid subset of C++ for OpenCL. Overall, kernel code written in C++ for OpenCL looks just like code written in OpenCL C with some extra C++ features available for convenience. C++ for OpenCL supports features from C++17.

C++for OpenCL使开发人员能够在内核代码中使用大多数C++功能,同时保留OpenCL C中熟悉的OpenCL构造、语法和语义。这有助于在现有的OpenCL应用程序中顺利过渡到新的C++功能,并且不需要更改熟悉的开发流程或工具。C++for OpenCL的主要设计目标是将特定于OpenCL的概念重新应用于C++,就像OpenCL C将它们应用于C一样。除了一些小的例外,OpenCL C是C++for OpenCL的有效子集。总的来说,用C++为OpenCL编写的内核代码看起来就像用OpenCLC编写的代码,并提供了一些额外的C++功能。用于OpenCL的C++支持C++17中的功能。



C++ for OpenCL Brings Together the Capabilities of OpenCL and C++17

用于OpenCL的C++将OpenCL和C++17的功能结合在一起

Experimental support for C++ for OpenCL was added in Clang 9 with bug fixes and improvements in Clang 10.

​Clang 9中增加了对OpenCL C++的实验性支持,Clang 10中进行了错误修复和改进。

You can check out C++ for OpenCL in Compiler Explorer.

​可以在编译器资源管理器中检出C++for OpenCL。

Documentation

文档

The language documentation can be found in releases of OpenCL-Docs with the first official version 1.0 (see also the latest WIP version html or pdf).

​语言文档可以在第一个官方版本1.0的OpenCL文档中找到(另请参阅最新的WIP版本html或pdf)。

This documentation provides details about the language semantics as well as differences to OpenCL C and C++.

本文档提供了有关语言语义的详细信息,以及与OpenCLC和C++的区别。

Example

范例

The following code snippet illustrates how to implement kernels with complex number arithmetic using C++ features.

下面的代码片段演示了如何使用C++特性实现具有复数运算的内核。

// This example demonstrates a convenient way to implement
// kernel code with complex number arithmetic using various
// C++ features.

// Define a class Complex, that can perform complex number
// computations with various precision when different
// types for T are used - double, float, half...
template<typename T>
class complex_t {
T m_re; // Real component.
T m_im; // Imaginary component.

public:
complex_t(T re, T im): m_re{re}, m_im{im} {};
complex_t operator*(const complex_t &other) const
{
  return {m_re * other.m_re - m_im * other.m_im,
           m_re * other.m_im + m_im * other.m_re};
}
int get_re() const { return m_re; }
int get_im() const { return m_im; }
};

// A helper function to compute multiplication over
// complex numbers read from the input buffer and
// to store the computed result into the output buffer.
template<typename T>
void compute_helper(global T *in, global T *out) {
  auto idx = get_global_id(0);	
  // Every work-item uses 4 consecutive items from the input
  // buffer - two for each complex number.
  auto offset = idx * 4;
  auto num1 = complex_t{in[offset], in[offset + 1]};
  auto num2 = complex_t{in[offset + 2], in[offset + 3]};
  // Perform complex number multiplication.
  auto res = num1 * num2;
  // Every work-item writes 2 consecutive items to the output
  // buffer.
  out[idx * 2] = res.get_re();
  out[idx * 2 + 1] = res.get_im();
}

// This kernel can be used for complex number multiplication
// in single precision.
kernel void compute_sp(global float *in, global float *out) {
  compute_helper(in, out);
}

// This kernel can be used for complex number multiplication
// in half precision.
#pragma OPENCL EXTENSION cl_khr_fp16: enable
kernel void compute_hp(global half *in, global half *out) {
  compute_helper(in, out); 
}
 

Developing kernels with C++ for OpenCL

用C++为OpenCL开发内核

C++ for OpenCL sources can be developed and compiled just like OpenCL C sources. However due to the increased growth of application complexity especially those that benefit most from a rich variety of C++ features and high-level abstractions, it is expected that the majority of C++ for OpenCL kernels will be compiled offline. Offline compilation also provides the ability to take advantage of various newest features in open source tools and frameworks without waiting for their integration into the vendor toolchains.

用于OpenCL源代码的C++可以像OpenCL C源代码一样进行开发和编译。然而,由于应用程序复杂性的增长,特别是那些从丰富的C++特性和高级抽象中获益最多的应用程序,预计大多数用于OpenCL内核的C++都将离线编译。离线编译还提供了利用开源工具和框架中各种最新功能的能力,而无需等待它们集成到供应商工具链中。

Offline compilation

离线编译

Clang provides support for C++ for OpenCL using the same interface as for OpenCL C.

Clang使用与OpenCL C相同的接口为OpenCL提供C++支持。

clang -cl-std=CLC++ test.cl
clang test.clcpp

More details can be found in its UsersManual. In the majority of cases the generated binary can be used in existing drivers. C++ for OpenCL version 1.0 is developed against OpenCL 2.0. Depending on the features used, drivers from other versions (e.g. OpenCL 3.0) might be able to load the binaries produced with C++ for OpenCL v1.0 too. Use of global objects and static function objects with non-trivial constructors is not supported in a portable way, refer to the following clang documentation for details.

​更多详细信息可在其用户手册中找到。在大多数情况下,生成的二进制文件可以在现有的驱动程序中使用。用于OpenCL 1.0版的C++是针对OpenCL 2.0开发的。根据所使用的功能,其他版本(例如OpenCL 3.0)的驱动程序也可以加载用C++为OpenCL v1.0生成的二进制文件。不支持以可移植的方式使用具有非平凡构造函数的全局对象和静态函数对象,有关详细信息,请参阅以下clang文档。

Clang only supports a limited number of vendors and therefore to allow executing the binaries from C++ for OpenCL on more devices it is recommended to generate portable executable formats. C++ for OpenCL kernel sources can be compiled into SPIR-V using open source tools and then loaded into drivers supporting SPIR-V. C++ for OpenCL 1.0 mainly requires SPIR-V 1.0 plus SPIR-V 1.2 for some features.

​Clang只支持有限数量的供应商,因此为了允许在更多设备上执行C++for OpenCL的二进制文件,建议生成可移植的可执行格式。用于OpenCL的C++内核源代码可以使用开源工具编译成SPIR-V,然后加载到支持SPIR-V的驱动程序中。OpenCL 1.0的C++主要需要SPIR-V 1.0和SPIR-V 1.2来实现某些功能。

The bugs and implementation of new features in clang can be tracked via the OpenCL Support Page.

​clang中的bug和新功能的实现可以通过OpenCL支持页面进行跟踪。

Online compilation

在线编译

Kernels written in C++ for OpenCL can be compiled online on devices that support the cl_ext_cxx_for_opencl extension.

​用C++为OpenCL编写的内核可以在支持cl_ext_cxx_for_OpenCL扩展的设备上在线编译。

Libraries

All OpenCL C builtin library functions are available with C++ for OpenCL.

所有的OpenCL C内置库函数都可以使用C++for OpenCL。

There is work ongoing to provide C++ based libraries extended from the C++ implementation: see the experimental libraries in clang and libclcxx projects for more details.

​目前正在努力提供从C++实现扩展而来的基于C++的库:有关更多详细信息,请参阅clang和libclcxx项目中的实验库。

Extensions

扩展

All extensions from OpenCL C are available with C++ for OpenCL.

所有来自OpenCL C的扩展都可以与用于OpenCL的C++一起使用。

Contributions

贡献

C++ for OpenCL is a community driven open language and contributions are welcome from anyone interested to improve the language compilation in clang or documentation of the language hosted in OpenCL-Docs. Refer to git log or git blame to find relevant contributors to contact or loop in for reviews.

​C++for OpenCL是一种社区驱动的开放语言,欢迎任何有兴趣改进clang中的语言编译或OpenCL文档中托管的语言文档的人提供意见。请参阅git日志或git指责,查找相关贡献者以联系或循环查看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值