关于Xcode的OpenCL错误:全局变量必须有一个常量的地址空间限定符const char * KernelSource = \ n“\

时间:2013-10-30 23:26:40

标签: opencl

我试图在Xcode中编译这段代码,但是我得到了上面的错误

const char *KernelSource =   "\n"\
"__kernel void pi(           \n"\
"__global float* out, \n"\
"uint cntSteps        \n"\
")                                                          \n"\
"{                                                          \n"\
" const uint idThread = get_local_id(0);                  \n"\
" uint numprocs = get_global_size(0);                     \n"\
" const float local_num = (float)cntSteps / numprocs;     \n"\
"float step = 1.0 / cntSteps;                            \n"\
"float sum = 0.0;                                        \n"\
"float x;                                                \n"\
"int localmax = (idThread + 1) * local_num;              \n"\
"for(uint i = idThread * local_num; i < localmax; i++)   \n"\
"{                                                          \n"\
"x = step * (i + 0.5);                                \n"\
"sum = sum + 4.0 / (1.0 + x * x);                     \n"\
"}                                                       \n"\
"out[idThread] = sum * step;                             \n"\
"}                                                          \n";

知道这个内核有什么问题!,

1 个答案:

答案 0 :(得分:0)

以下是我解决它的方法:

  1. 创建一个内核文件,例如calc_pi.cl
  2. // calc_pi.cl

    kernel void pi(global float* out, uint cntSteps) {
       const uint idThread = get_local_id(0);
       uint numprocs = get_global_size(0);
       const float local_num = (float)cntSteps / numprocs;
       float step = 1.0 / cntSteps;
       float sum = 0.0;
       float x;
       int localmax = (idThread + 1) * local_num;
       for(uint i = idThread * local_num; i < localmax; i++) {
          x = step * (i + 0.5);
          sum = sum + 4.0 / (1.0 + x * x);
       }
       out[idThread] = sum * step;
    }
    
    2. in main.cpp 
    // include all necessary headers and the above kernel e.g 
    #include "calc_pi.cl.h"
    //.. more includes 
    
    1. 然后使用你的内核
    2. 我希望这有助于某些人开始在Xcode中使用OpenCL。