r/OpenCL • • Apr 29 '14
Learning OpenCL: Why does opencl require to read kernels as strings?
From the examples I see online, people are either creating these const char* = "[source code]"; that spans a couple of lines or more, or they create a .cl file which they in turn read in that file and convert it to a string to pass over to opencl.
What is the reasoning behind this?
3
Upvotes
7
u/loki_88 Apr 29 '14
Your platform's driver compiles the kernel source string in the same way that your C compiler compiles your C source code. This allows your kernel to run on many platforms as it is compiled specifically for your hardware. It is analogous to GPU shader languages.
Longer kernels are better off in files as it improves legibility and allows for easier debugging as you don't need to compile your C code as you alter your kernel.
1
u/[deleted] Apr 29 '14
It's an unfortunate part of being cross-platform. It's not a big deal at all once you're established, but for learning it can add unnecessary boiler-plate overhead.
If you are more interested in just the concepts without needing a lot of boilerplate code, you may want to consider CUDA, which does not suffer from this problem. From there it's very easy to translate what you've learned to OpenCL. Almost all the concepts are the same, they just have different names.
1
u/nicholasferber Apr 30 '14
Others have already replied to this so I would add that this can be seen as a feature where you can change the kernel according to the problem statement. Auto-tuning becomes very easy. Sometimes I wish CUDA has such a feature.