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?

  • I assume that you write your code in the usual way (i.e. in an IDE with compile-checking, debugging and all the other goodness that an IDE provides) before you stringify it? Mar 21 '16 at 18:05
  • Yeah. I use eclipse. I then add my python script as a pre-built step. I really like this approach but it does not help if I want to have header files in my ooencl code
    – chasep255
    Mar 21 '16 at 18:10
  • So in order to have header files they need to be separate from the exe? Mar 21 '16 at 18:13
  • Well I think it needs to be a file to use the -I option in the opencl compiler.
    – chasep255
    Mar 21 '16 at 18:28
  • Can you use the pre processor to compile in you .cl files at compile time with macros and #include statements? Mar 22 '16 at 15:34

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).

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.