/***** GCL Generated File *********************/
/* Automatically generated file, do not edit! */
/**********************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <dispatch/dispatch.h>
#include <OpenCL/opencl.h>
#include <OpenCL/gcl_priv.h>
#include "apply_stack_cl_source.h"

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 (^apply_stack_kernel)(const cl_ndrange *ndrange, cl_int step_i, cl_int step_j, cl_int w_ind_size, cl_int h_ind_size, cl_uint* offsets, cl_float* stacks, cl_float* weights, cl_int width, cl_int height, cl_float2* numerator_denominator) =
^(const cl_ndrange *ndrange, cl_int step_i, cl_int step_j, cl_int w_ind_size, cl_int h_ind_size, cl_uint* offsets, cl_float* stacks, cl_float* weights, cl_int width, cl_int height, cl_float2* numerator_denominator) {
  int err = 0;
  cl_kernel k = bmap.map[0].kernel;
  if (!k) {
    initBlocks();
    k = bmap.map[0].kernel;
  }
  if (!k)
    gcl_log_fatal("kernel apply_stack does not exist for device");
  kargs_struct kargs;
  gclCreateArgsAPPLE(k, &kargs);
  err |= gclSetKernelArgAPPLE(k, 0, sizeof(step_i), &step_i, &kargs);
  err |= gclSetKernelArgAPPLE(k, 1, sizeof(step_j), &step_j, &kargs);
  err |= gclSetKernelArgAPPLE(k, 2, sizeof(w_ind_size), &w_ind_size, &kargs);
  err |= gclSetKernelArgAPPLE(k, 3, sizeof(h_ind_size), &h_ind_size, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 4, offsets, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 5, stacks, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 6, weights, &kargs);
  err |= gclSetKernelArgAPPLE(k, 7, sizeof(width), &width, &kargs);
  err |= gclSetKernelArgAPPLE(k, 8, sizeof(height), &height, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 9, numerator_denominator, &kargs);
  gcl_log_cl_fatal(err, "setting argument for apply_stack failed");
  err = gclExecKernelAPPLE(k, ndrange, &kargs);
  gcl_log_cl_fatal(err, "Executing apply_stack failed");
  gclDeleteArgsAPPLE(k, &kargs);
};

// Initialization functions
static void initBlocks(void) {
  const char* build_opts = "";
  static dispatch_once_t once;
  dispatch_once(&once,
    ^{ int err = gclBuildProgramBinaryAPPLE("OpenCL/apply_stack_cl_source", "com.macphun.noiselessPro", &bmap, build_opts);
       if (!err) {
          assert(bmap.map[0].block_ptr == apply_stack_kernel && "mismatch block");
          bmap.map[0].kernel = clCreateKernel(bmap.program, "apply_stack", &err);
       }
     });
}

__attribute__((constructor))
static void RegisterMap(void) {
  gclRegisterBlockKernelMap(&bmap);
  bmap.map[0].block_ptr = apply_stack_kernel;
}

