openclpyopencl

How do I use the OpenCL C++/C++ for OpenCL kernel languages?


I have only tried compiling kernels using pyopencl, but I can only seem to be able to use OpenCl C. Looking at clinfo, I only see support for CLC listed, heres some truncated output from my pc:

  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

I am using a rocm driver compiled from the AUR, I tried to also install the mesa driver alongside but could not do so (perhaps I need to uninstall the other, but I dread having to recompile it if mesa fails).

My laptop (intel hd graphics) seems to support OpenCL 3.0 but also only lists CLC support. What am I missing, is this not implemented yet? I saw something somewhere about "offline compilation" and maybe using a "clc++" option with clang but can someone elaborate?


Solution

  • C++ for OpenCL can be used in two ways:

    Online compilation

    If OpenCL device supports cl_ext_cxx_for_opencl, it is possible to compile a program written using the C++ for OpenCL kernel language in runtime. Applications may pass -cl-std=CLC++ to clCompileProgram and clBuildProgram for programs created using clCreateProgramFromSource to request the program be built as C++ for OpenCL.

    Offline compilation

    If OpenCL device allows to create the program with SPIR-V, then it is possible to compile C++ for OpenCL source into intermediate LLVM IR:

    clang -c -cl-std=clc++ -target spir64 -O3 -emit-llvm -o test.ll test.clcpp
    

    Next, LLVM IR can be translated into SPIR-V using llvm-spirv:

    llvm-spirv test.ll -o test.spv
    

    Finally, OpenCL program can be created using clCreateProgramWithIL call:

    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);
    

    For PyOpenCL:

    with open('test.spv', 'rb') as il_file:
        il_buffer = bytes(il_file.read())
    
    prg = cl.Program(ctx, il_buffer)
    

    To check that OpenCL device supports SPIR-V modules, you need to use CL_DEVICE_IL_VERSION query in OpenCL 2.1 or newer and the CL_DEVICE_ILS_WITH_VERSION query in OpenCL 3.0 or newer.

    For additional information about offline compilation please see Offline Compilation of OpenCL Kernels into SPIR-V Using Open Source Tooling article.