3

I want to compile an OpenCL kernel for a certain AMD GPU - which is not available on my machine -, so that later I'm able to just load and run it when that GPU is present.

I've read this question here on SO:

Offline compilation for AMD and NVIDIA OpenCL Kernels without cards installed

And the answer suggesting I create the OpenCL context with CL_CONTEXT_OFFLINE_DEVICES_AMD. Ok, I can do that. But what then? In AMD's OpenCL Programming User Guide, it says:

A.8.6 cl_amd_offline_devices To generate binary images offline, it is necessary to access the compiler for every device that the runtime supports, even if the device is currently not installed on the system. When, during context creation, CL_CONTEXT_OFFLINE_DEVICES_AMD is passed in the context properties, all supported devices, whether online or offline, are reported and can be used to create OpenCL binary images.

ok, but how exactly? I'm assuming I'd need to call clCompileProgram() or clBuildProgram(), right? How do I set the device list for it to the device I like?

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

2

As you assumed, you start with the usual clCompileProgram() and clBuildProgram().

Next you can use clGetProgramInfo() with CL_PROGRAM_BINARY_SIZES to get the sizes for your buffer allocations, and a second time with CL_PROGRAM_BINARIES to get the actual binary images of the program.

This image can then be used with clCreateProgramWithBinary() instead of clCreateProgramWithSource().

Hope that helps.

noma
  • 1,171
  • 6
  • 15
  • So, I need to go through all the devices to pick the one I want, and then use that index to get the size and the pointer to the compiled binary, and write that to a file? Ok, that sounds doable. I don't suppose you have a link to an example program doing this? – einpoklum Mar 13 '20 at 18:54
  • Sorry, no example at hand, but you can basically use any OpenCL Hello-World, and e.g., quickly modify it into two versions - one that creates the binaries and exits, and one that loads them instead of the source. Good luck. :-) – noma Mar 13 '20 at 19:01