5

I am having a hard time deciding how I should manage my OpenCL source code so that my program can compile it at runtime. There seem to be two many strategies. You can create them in you C source as strings (I really don't like this approach since this seems like a horrible way to write code). The second approach seems to be reading the .cl files at runtime using stdio and then compiling them. This is what I have been doing. The only issue I have with this is that it adds a dependency to my program (I would prefer it to be contained in a single executable). Also I seem to need to run my program in the same directory as the .cl files so they can be found. I can think of a few solutions to this but I am just wondering how you guys do it?

Finally, I just tried writing a python script which is run before compilation and generates a header file containing strings corresponding to each .cl file. This way the cl source code can we written separately and included. The issue I have with this is I am not sure how to handle includes within the opencl source. How do you guys go about managing your opencl source code?

chasep255
  • 151
  • 3

1 Answers1

4

You could also embed the OpenCL source inside your executable. Just make a tiny script converting (perhaps with the help of hexdump(1)...) that file into a huge C file like

 const char myopenclsource[] = { 0x2f, 0x2f, 0x20, 0x4d, 0x75, 
   /// etc
 };

(of course the OpenCL source could easily be found from the binary executable, e.g. using strings(1) on Linux)

Then, you'll need to change your build procedure, e.g. add a few lines to your Makefile.

Some OpenCL implementations might also accept SPIR bytecode (which you might perhaps embed likewise; however, it is less easy to decode for humans, and it probably is slightly faster to load into the GPU by implementations).

Basile Starynkevitch
  • 32,434
  • 6
  • 84
  • 125