abdimuna
abdimuna

Reputation: 785

OpenCL on Xcode error: global variables must have a constant address space qualifier const char *KernelSource = \n"\

I tried to compile this code in Xcode, but I get the above error

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";

Any idea what is wrong with this kernel !,

Upvotes: 0

Views: 851

Answers (1)

abdimuna
abdimuna

Reputation: 785

Here is how I solved it:

  1. create a kernel file e.g 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. then work with your kernel

I hope this helps someone starting to work with OpenCL in Xcode.

Upvotes: 1

Related Questions