2

I'm working on some OpenCL code within a larger project. The code only gets compiled at run-time - but I don't want to deploy a version and start it up just for that. Is there some way for me to have the syntax of those kernels checked (even without consider), or even compile them, at least under some restrictions, to make it easier to catch errors earlier?

I will be targeting AMD and/or NVIDIA GPUs.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Depending on your environment, you could compile the kernel ahead of time to SPIR-V, or if you know what cards you'll be using, compile it as part of your build process and download the binary. Could you add more details about what brand hardware you're using? – apetranzilla Feb 06 '20 at 15:19
  • "You could compile" - How? "Download the binary" - which binary? From where? – einpoklum Feb 06 '20 at 15:36
  • 1
    As I said, it depends on your environment. With AMD cards, you can use the `cl_amd_offline_devices` platform extension to compile the program for any supported AMD GPU, including ones that you don't have physically installed, which can then be saved with `clGetProgramInfo(CL_PROGRAM_BINARIES)`. I'm not aware of an equivalent for Nvidia devices, but if they support SPIR-V, you may be able to use a SPIR-V compiler. – apetranzilla Feb 06 '20 at 18:44
  • @apetranzilla: Ah, now you're talking. I should look into that platform extension, at least for AMD cards. – einpoklum Feb 06 '20 at 20:52
  • I was able to find [this guide](https://developer.amd.com/wordpress/media/2013/12/AMD_OpenCL_Programming_User_Guide2.pdf) which describes how to use the `cl_amd_offline_devices` extension (Appendix A, section 8.6). If your headers don't include the `CL_CONTEXT_OFFLINE_DEVICES_AMD` macro it mentions, I found it defined as `0x403F` in my system, so defining it yourself with that value should work fine. – apetranzilla Feb 06 '20 at 21:58
  • @apetranzilla: Wait, doesn't the value depend on which devices you want the "offline" builds to target? – einpoklum Feb 09 '20 at 13:54
  • I'm not familiar with how it works, but I'd assume it creates a context with all available "offline" devices automatically - similar to how you can create a context from a device type – apetranzilla Feb 09 '20 at 18:01

1 Answers1

1

The type of program you are looking for is an "offline compiler" for OpenCL kernels - knowing this will hopefully help with your search. They exist for many OpenCL implementations, you should check availability for the specific implementation you are using; otherwise, a quick web search suggests there are some generic open source ones which may or may not fit the bill for you.

If your build machine is also your deployment machine (i.e. your target OpenCL implementation is available on your build machine), you can of course also put together a very basic offline compiler yourself by simply wrapping clBuildProgram() and friends in a basic command line utility.

pmdj
  • 22,018
  • 3
  • 52
  • 103