Most OpenCL Tutorials give a nice introduction to OpenCL, yet I have not found information on the question of interoperability of the compilation targets. It seems that I have to compile OpenCL code for the target GPU. Now when making a binary distribution of an application using OpenCL, does one have to make several builds for the vendor-platforms?

  • 1
    A binary distribution of what? Of the host program, or of the kernels? The former only contains (hopefully-)portable Kernel source code and calls to the OpenCL API, no device-specific code. – user7043 Aug 28 '13 at 12:59
  • I.e. the kernel is essentially source-distributed? – wirrbel Aug 28 '13 at 13:09
  • Depends entirely on what you're doing. If you call compile_kernel("... some source code ..."), surely you don't expect your C compiler to perform magic and turn that into GPU machine code? – user7043 Aug 28 '13 at 13:17

OpenCL works similarly to other GPU libraries like OpenGL. What you ship is an intermediate representation. The GPU's driver does final compilation for execution on the specific device at run time. Here is an intro to OpenCL slide presentation from Khronos.

Edit: With OpenCL 2 the compilation model changed some to be much more flexible. Now there is an LLVM backend that targets the new SPIR-V intermediate representation, so any language compiler that targets LLVM should be capable of being used to target OpenCL. Other than that the model is pretty much the same but gains a step, source compiles to LLVM IR, LLVM IR compiles to SPIR-V, driver handles final compilation and execution.

  • Unfortunately the linked slides have just 16 pages :-( – qwerty_so Jul 4 '16 at 12:52
  • @ThomasKilian Thanks, looks like they updated them for OpenCL2 and the move to SPIR-V. The model has morphed some, the diagram on new slide 12 is pretty much what you want. It is now Source code -> LLVM -> SPIR -> actual code that gets executed. – stonemetal Jul 5 '16 at 14:39
  • Thanks :-) I guess you should make those edits in your answer. – qwerty_so Jul 5 '16 at 14:43

No, you do not need to have a per-target-platform compiled binary: OpenCL's API supports compiling kernel binaries for several different devices, and obtaining the compiled code with clGetProgramInfo(). You can do this for several target platforms, place all your binaries in your distribution, and ship that.

  • 2
    ...assuming that your users are all using a GPU you happened to pre-compile for. – Mason Wheeler Aug 28 '13 at 13:58
  • @MasonWheeler - Which is the reason you can compile the code during an intialization process and load the binary specifically design for the user. – Ramhound Aug 29 '13 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.