2

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?

einpoklum
  • 2,478
  • 1
  • 13
  • 30
wirrbel
  • 3,018
  • 2
  • 21
  • 33
  • 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. –  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? –  Aug 28 '13 at 13:17

2 Answers2

3

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.

stonemetal
  • 3,371
  • 16
  • 17
  • Unfortunately the linked slides have just 16 pages :-( –  Jul 04 '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 05 '16 at 14:39
  • Thanks :-) I guess you should make those edits in your answer. –  Jul 05 '16 at 14:43
0

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.

einpoklum
  • 2,478
  • 1
  • 13
  • 30
  • 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