0

我尝试在 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";

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

4

1 回答 1

0

这是我解决它的方法:

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

// 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. 然后使用您的内核

我希望这对开始在 Xcode 中使用 OpenCL 的人有所帮助。

于 2013-10-31T09:36:29.890 回答