0
votes

I am currently in the process of distributing an OpenCL program (online compilation).

Now my kernel code is in a *.cl file, which is read during kernel building. I think that is also possible to convert the kernel source code into string literals, that can be read directly instead of the *.cl for the same purpose.

My question is: what would be the advantage of stringfying the kernel code?

1
there could be parts like @replace_me@ and one can replace them with dynamically changed values and re-compile same code with different parameters such as local array size (local array size must be constant so you can re-compile whenever you need a different size for local arrays). __local float data[@replace_me_@]; - huseyin tugrul buyukisik
I recommend you put the code in a string in the code. It is much easier to use than opening a file. - DarkZeros
@huseyintugrulbuyukisik: wouldn't it be better to use compiler defines for transfer those constant primitives (-DSIZE=NUM)?. - L30nardo SV.
@DarkZeros: could you please explain more why using a string is much easier than opening a file? - L30nardo SV.

1 Answers

1
votes

Advantages:

  • No need to deal with IO. If you have to support multiple file systems, this can be a pain (Windows, Linux, etc...).
  • Easier for the user, the executable is only 1 file.
  • You may have problems if somebody edits the .cl file.
  • Easier to compile and ship.

Example:

const char *KernelSource = "\n" \
"__kernel void square(                     \n" \
"   __global float* input,                 \n" \
"   __global float* output,                \n" \
"   const unsigned int count)              \n" \
"{                                         \n" \
"   int i = get_global_id(0);              \n" \
"   if(i < count)                          \n" \
"       output[i] = input[i] * input[i];   \n" \
"}                                         \n";

program = clCreateProgramWithSource(context, 1, (const char **) &KernelSource, NULL, &err);