OpenCL构建:字符串内核代码

时间:2017-06-15 15:07:49

标签: opencl

我目前正在分发OpenCL程序(在线编译)。

现在我的内核代码位于*.cl文件中,该文件在内核构建期间被读取。我认为也可以将内核源代码转换为字符串文字,为了相同的目的,可以直接读取而不是*.cl

我的问题是:字符串化内核代码有什么好处?

1 个答案:

答案 0 :(得分:1)

优点:

  • 无需处理IO。如果你必须支持多个文件系统,这可能很麻烦(Windows,Linux等等)。
  • 对用户来说,可执行文件只有1个文件。
  • 如果有人编辑.cl文件,您可能会遇到问题。
  • 易于编译和发货。

示例:

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);
相关问题