编程OpenCL内核

本文探讨了OpenCL应用程序的编程结构,区分了在线和离线编译内核的方法,重点介绍了SPIR-V在跨平台兼容性和语言生态中的作用,以及如何增强OpenCL语言的灵活性,如C++forOpenCL和SYCL。
摘要由CSDN通过智能技术生成

Programming OpenCL Kernels

编程OpenCL内核

An OpenCL application is split into host code and device kernel code. Host code is typically written using a general programming language such as C or C++ and compiled by a conventional compiler for execution on the host CPU. OpenCL bindings for other languages are also available, such as Python.

OpenCL应用程序分为主机代码和设备内核代码。主机代码通常使用诸如C或C++之类的通用编程语言编写,并由传统编译器编译以在主机CPU上执行。其他语言的OpenCL绑定也可用,例如Python。

Device kernels that are written in OpenCL C, which is based on C99, can be ingested and compiled by the OpenCL driver during execution of an application using runtime OpenCL API calls. This is called online compilation and is supported by all OpenCL drivers. OpenCL C is a subset of ISO C99 with language extensions for parallelism, well-defined numerical accuracy (IEEE 754 rounding with specified max error) and a rich set of built-in functions including cross, dot, sin, cos, pow, log etc.

在基于C99的OpenCL C中编写的设备内核可以在使用运行时OpenCL API调用执行应用程序期间由OpenCL驱动程序获取和编译。这被称为在线编译,所有OpenCL驱动程序都支持它。OpenCL C是ISO C99的一个子集,具有并行性的语言扩展、定义明确的数值精度(带指定最大误差的IEEE 754舍入)和丰富的内置函数集,包括cross、dot、sin、cos、pow、log等。



Traditional Versus OpenCL Programming Using OpenCL C Kernels

使用OpenCL C内核的传统编程与OpenCL编程

The OpenCL specification also enables optional offline compilation where the kernel program is pre-compiled into a binary format that a particular driver can ingest. Offline compilation can add significant value to developers by:

OpenCL规范还支持可选的离线编译,其中内核程序被预编译为特定驱动程序可以接受的二进制格式。离线编译可以通过以下方式为开发人员增加重要价值:

  • Speeding OpenCL application execution by eliminating or minimizing kernel code compilation time.
  • 通过消除或最小化内核代码编译时间来加速OpenCL应用程序的执行。
  • Leveraging alternative kernel languages and tools to produce executable binaries.
  • 利用替代内核语言和工具生成可执行二进制文件。

There are two offline compilation approaches:

有两种离线编译方法:

  • Kernels that are compiled online by the driver can be retrieved by the application using the clGetProgramInfo call. Those cached, device-specific, kernels can then be later reloaded for execution on the same device instead of re-compiling those kernels from the source code.
  • 应用程序可以使用clGetProgramInfo调用来检索驱动程序在线编译的内核。这些缓存的、特定于设备的内核稍后可以重新加载,以便在同一设备上执行,而不是从源代码中重新编译这些内核。
  • Offline compilers can be invoked independently before the OpenCL application executes to generate binaries to load and run on the device during application execution.
  • 离线编译器可以在OpenCL应用程序执行之前独立调用,以生成二进制文件,以便在应用程序执行期间加载并在设备上运行。



Online Versus Offline Compilation of Kernels

内核的在线与离线编译

Early OpenCL implementations primarily used proprietary binary formats and caching of driver-compiled binaries to achieve offline compilation. However, the binaries created by the compiler in a specific device driver are not portable to other devices, and so applications using cached binaries lost the portbility to any device that is possible through online compilation of OpenCL C. To solve this portability problem, and to enable a richer language and compiler ecosystem, Khronos has defined a cross-vendor, portable intermediate program representation called SPIR-V. An increasing number of OpenCL implementations are supporting ingestion of offline-compiled kernel programs in the SPIR-V format.

​早期的OpenCL实现主要使用专有的二进制格式和缓存驱动程序编译的二进制文件来实现离线编译。然而,编译器在特定设备驱动程序中创建的二进制文件不可移植到其他设备,因此使用缓存二进制文件的应用程序失去了通过在线编译OpenCL C可以移植到任何设备的能力。为了解决这个可移植性问题,并实现更丰富的语言和编译器生态系统,Khronos定义了一个跨供应商,称为SPIR-V的可移植中间程序表示。越来越多的OpenCL实现支持接收SPIR-V格式的离线编译内核程序。

SPIR-V enables independent innovation by the compiler and silicon communities. Compiler front ends that generate SPIR-V kernels that can be ingested and executed by any OpenCL driver that understands the SPIR-V format. For example the C++ for OpenCL open source front-end and compilers for SYCL can generate SPIR-V code. Both languages bring C++ functionality to programming OpenCL. While C++ for OpenCL allows using C++ features in the traditional OpenCL kernel code, SYCL provides single-source C++ solution both for the host code and the kernel code. There is also ongoing work on providing SPIR-V support in non C/C++-based languages e.g. Julia.

​SPIR-V通过编译器和硅社区实现了自主创新。编译器前端,生成SPIR-V内核,任何理解SPIR-V格式的OpenCL驱动程序都可以获取和执行这些内核。例如,OpenCL的C++开源前端和SYCL的编译器可以生成SPIR-V代码。这两种语言都为编写OpenCL提供了C++功能。虽然面向OpenCL的C++允许在传统的OpenCL内核代码中使用C++功能,但SYCL为主机代码和内核代码提供了单一源C++解决方案。在非基于C/C++的语言(例如Julia)中提供SPIR-V支持的工作也在进行中。

SPIR-V also enables OpenCL kernels written in OpenCL C and C++ for OpenCL to be executed by runtimes other than OpenCL, providing more deployment flexibility for developers that have invested in OpenCL kernel programming. For example, the Google clspv open source compiler can generate Vulkan SPIR-V shaders from OpenCL C kernel source code, Microsoft is working on a compiler chain that can ingest OpenCL SPIR_V kernels into DX12, and there is early work on using SPIR-V tools such as SPIRV-Cross to bring OpenCL SPIR-V kernels into Metal for execution on Apple platforms.

​SPIR-V还允许用OpenCL C和C++为OpenCL编写的OpenCL内核由OpenCL以外的运行时执行,为投资于OpenCL内核编程的开发人员提供了更多的部署灵活性。例如,Google clspv开源编译器可以从OpenCL C内核源代码生成Vulkan SPIR-V着色器,Microsoft正在开发一个可以将OpenCL SPIR_V内核引入DX12的编译器链,并且早期已经在使用SPIR-V工具(如SPIRV Cross)将OpenCL SPIR-V内核引入Metal以在苹果平台上执行。

This diverse OpenCL language ecosystem in turn gives a rich choice to domain specific languages, such as the Halide image processing framework, that can compile to OpenCL C kernels either using source-to-source translations or generating SPIR-V.

这种多样化的OpenCL语言生态系统反过来为特定领域的语言提供了丰富的选择,例如Halide图像处理框架,该框架可以使用源到源翻译或生成SPIR-V编译为OpenCL C内核。



OpenCL Language Ecosystem Enabled With SPIR-V

SPIR-V支持OpenCL语言生态系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值