title: Basic programming sample of OpenCL from Apple fails to run on GPU
body: <p>I started learning some basics about OpenCL a while ago and decided to give the <a href="https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/ExampleHelloWorld/Example_HelloWorld.html#//apple_ref/doc/uid/TP40008312-CH112-SW2" rel="nofollow">"Basic programming sample"</a> from Apple a go. I runs OK on CPU, but when I select GPU as the target device I get err = -45 from</p>
  
  <p><code>err = gclExecKernelAPPLE(k, ndrange, &amp;kargs);</code></p>
  
  <p>This error code translates to <code>CL_INVALID_PROGRAM_EXECUTABLE</code>. Any idea how can I correct the sample code?</p>
  
  <p>Automatically generated <code>kernel.cl.c</code> code looks like this (+ includes on top):</p>
  
  <pre><code>static void initBlocks(void);
  
  // Initialize static data structures
  static block_kernel_pair pair_map[1] = {
      { NULL, NULL }
  };
  
  static block_kernel_map bmap = { 0, 1, initBlocks, pair_map };
  
  // Block function
  void (^square_kernel)(const cl_ndrange *ndrange, cl_float* input, cl_float* output) =
  ^(const cl_ndrange *ndrange, cl_float* input, cl_float* output) {
      int err = 0;
      cl_kernel k = bmap.map[0].kernel;
      if (!k) {
          initBlocks();
          k = bmap.map[0].kernel;
      }
      if (!k)
          gcl_log_fatal("kernel square does not exist for device");
      kargs_struct kargs;
      gclCreateArgsAPPLE(k, &amp;kargs);
      err |= gclSetKernelArgMemAPPLE(k, 0, input, &amp;kargs);
      err |= gclSetKernelArgMemAPPLE(k, 1, output, &amp;kargs);
      gcl_log_cl_fatal(err, "setting argument for square failed");
  
      err = gclExecKernelAPPLE(k, ndrange, &amp;kargs);
  
      gcl_log_cl_fatal(err, "Executing square failed");
      gclDeleteArgsAPPLE(k, &amp;kargs);
  };
  
  // Initialization functions
  static void initBlocks(void) {
      const char* build_opts = " -cl-std=CL1.1";
      static dispatch_once_t once;
      dispatch_once(&amp;once,
      ^{ int err = gclBuildProgramBinaryAPPLE("OpenCL/kernel.cl", "", &amp;bmap, build_opts);
          if (!err) {
              assert(bmap.map[0].block_ptr == square_kernel &amp;&amp; "mismatch block");
              bmap.map[0].kernel = clCreateKernel(bmap.program, "square", &amp;err);
          }
      });
  }
  
  __attribute__((constructor))
  static void RegisterMap(void) {
      gclRegisterBlockKernelMap(&amp;bmap);
      bmap.map[0].block_ptr = square_kernel;
  }
  </code></pre>