我只尝试过使用pyopincl编译内核,但我似乎只能使用OpenCl C。在clinfo中,我只看到了对CLC的支持,下面是我电脑的一些截断输出:
Platform Name AMD Accelerated Parallel Processing
Platform Vendor Advanced Micro Devices, Inc.
Platform Version OpenCL 2.1 AMD-APP (3423.0)
Platform Profile FULL_PROFILE
Platform Extensions cl_khr_icd cl_amd_event_callback
Platform Extensions function suffix AMD
Platform Host timer resolution 1ns
Platform Name AMD Accelerated Parallel Processing
Number of devices 1
Device Name gfx1031
Device Vendor Advanced Micro Devices, Inc.
Device Vendor ID 0x1002
Device Version OpenCL 2.0
Driver Version 3423.0 (HSA1.1,LC)
Device OpenCL C Version OpenCL C 2.0
Device Type GPU
Device Board Name (AMD) AMD Radeon RX 6700 XT
Device PCI-e ID (AMD) 0x73df
Device Topology (AMD) PCI-E, 0000:2f:00.0
Device Profile FULL_PROFILE
Device Available Yes
Compiler Available Yes
Linker Available Yes
Max compute units 20
我使用的是从AUR编译的rocm驱动程序,我试图同时安装mesa驱动程序,但无法做到(也许我需要卸载另一个,但如果mesa失败,我担心必须重新编译它(。
我的笔记本电脑(英特尔高清显卡(似乎支持OpenCL3.0,但也只列出了CLC支持。我缺少什么,这还没有实施吗?我在某个地方看到了";离线编译";并且可能使用";clc++";clang的选项,但有人能详细说明吗?
OpenCL的C++可以通过两种方式使用:
在线编译
如果OpenCL设备支持cl_ext_cxx_for_opencl
,则可以在运行时编译使用C++for OpenCL内核语言编写的程序。应用程序可以为使用clCreateProgramFromSource
创建的程序传递-cl-std=CLC++
到clCompileProgram
和clBuildProgram
,以请求将该程序构建为OpenCL的C++。
离线编译
如果OpenCL设备允许使用SPIR-V创建程序,则可以将OpenCL源代码的C++编译为中间LLVM IR:
clang -c -cl-std=clc++ -target spir64 -O3 -emit-llvm -o test.ll test.clcpp
接下来,LLVM IR可以使用llvm-spirv
:转换为SPIR-V
llvm-spirv test.ll -o test.spv
最后,可以使用clCreateProgramWithIL
调用创建OpenCL程序:
std::ifstream il_file("test.spv", std::ios::binary);
std::vector<char> il_buffer;
std::copy(std::istreambuf_iterator<char>(il_file),
std::istreambuf_iterator<char>(),
std::back_inserter(il_buffer));
cl_program program =
clCreateProgramWithIL(context, il_buffer.data(), il_buffer.size(), &ret);
对于PyOpenCL:
with open('test.spv', 'rb') as il_file:
il_buffer = bytes(il_file.read())
prg = cl.Program(ctx, il_buffer)
要检查OpenCL设备是否支持SPIR-V模块,您需要在OpenCL 2.1或更新版本中使用CL_DEVICE_IL_VERSION
查询,在OpenCL 3.0或更新版本中将使用CL_DEVICE_ILS_WITH_VERSION
查询。
有关脱机编译的更多信息,请参阅"使用开源工具将OpenCL内核脱机编译为SPIR-V"一文。