title: Passing Class to a Kernel in Intel Opencl
body: <p>I have been working on an c/c++ OpenCL solution for the past few weeks now. For my solution, I need to pass a class from my CPU(Host) to GPU(Device). When I try to pass the class as an argument it gives an error "Unknown Type-Identifier Class". My doubt whether OpenCL on Intel Platform does it allow us to pass a class to kernel or any work around is available for it. In CUDA I have seen some examples and it works perfectly fine for the platform. However, with respect to OpenCL I am not able to find any references and No examples related to this query. I would be really thankful to any help with regards to this issue. I have posted the same question on Intel website but to no avail. If someone would be kind enough to help me understand where I am going wrong or how I should proceed with this I would be really thankful to you.</p>
  
  <p>//HOST SIDE CODE</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;iostream&gt;
  #include"CL/cl.h"
  
  
  class test
  {
  public:
      cl_int a;
      cl_char b;
  };
  
  int main()
  {
      test *tempstruct = new test;
  
      cl_platform_id platfrom_id;
      cl_device_id device_id; // compute device id 
      cl_context context; // compute context
      cl_command_queue commands; // compute command queue
      cl_program program; // compute program
      cl_kernel kernel; // compute kernel
  
      int err;
  
      err = clGetPlatformIDs(1, &amp;platfrom_id, NULL);
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to create a platfrom group!\n");
          return -1;
      }
  
      err = clGetDeviceIDs(platfrom_id, CL_DEVICE_TYPE_GPU, 1, &amp;device_id, NULL);
  
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to create a device group!\n");
          return -1;
      }
  
      context = clCreateContext(0, 1, &amp;device_id, NULL, NULL, NULL);
  
      if (!context)
      {
          printf("Error: Failed to create a compute context!\n");
          return -1;
      }
  
      commands = clCreateCommandQueue(context, device_id, 0, NULL);
  
      if (!commands)
      {
          printf("Error: Failed to create a command commands!\n");
          return -1;
      }
  
      #define MAX_SOURCE_SIZE (0x100000) 
      FILE *fp, *fp1;
      char filename[] = "Template.cl";
      fp = fopen(filename, "r");
      if (fp == NULL)
      {
          printf("\n file  not found \n");
          return -1;
      }
      char * source_str = (char*)malloc(MAX_SOURCE_SIZE);
      size_t size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
      fclose(fp);
  
      cl_mem classobj = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, sizeof(tempstruct), &amp;tempstruct, &amp;err);
  
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to allocate device memory!\n");
          return -1;
      }
  
      program = clCreateProgramWithSource(context, 1, (const char **)&amp; source_str, (const size_t *)&amp;size, &amp;err);
  
      if (!program)
      {
          printf("Error: Failed to create program with source!\n");
          return -1;
      }
  
      err = clBuildProgram(program, 1, &amp;device_id, NULL, NULL, NULL);
  
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to build program executable!\n");
          return -1;
      }
  
  
      test  *resptr = (test *)clEnqueueMapBuffer(commands, classobj, CL_TRUE, CL_MAP_WRITE, NULL, sizeof(test), NULL, NULL, NULL, &amp;err);
      // INITIALISATION OF CLASS
  
      tempstruct-&gt;a = 10;
      if (!resptr)
      {
          printf("Error: Failed to create enqueuemapbuffer!\n");
          return -1;
      }
  
      err = clEnqueueUnmapMemObject(commands, classobj, resptr, 0, NULL, NULL);
  
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to write to source array!\n");
          return -1;
      }
  
      kernel = clCreateKernel(program, "CLASS", &amp;err);
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to create compute kernel!\n");
          return -1;
      }
  
      err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &amp;classobj);
  
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to set kernel arguments! %d\n", err);
          return -1;
      }
  
      size_t globalsize = 1;
      size_t local = 1;
  
      err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &amp;globalsize, &amp;local, 0, NULL, NULL);
      if (err)
      {
          printf("Error: Failed to execute nd range!\n");
          return -1;
      }
  
      test  *resptr1 = (test *)clEnqueueMapBuffer(commands, classobj, CL_TRUE, CL_MAP_READ, NULL, sizeof(test), NULL, NULL, NULL, &amp;err);
  
      err = clEnqueueUnmapMemObject(commands, classobj, resptr1, 0, NULL, NULL);
      if (err != CL_SUCCESS)
      {
          printf("Error: Failed to read output array! %d\n", err);
          return -1;
      }
  
      // again i am printing the class value
  
      printf("\n in cpu side = %d\n", tempstruct-&gt;a);
  
  }
  
  
  //HOST END
  
  
  //DEVICE SIDE(KERNEL CODE)
  
   // filename :  Template.cl
  
  class test
  {
  public:
      cl_int a;
      cl_char b;
  };
  
  __kernel void CLASS(__global test *inclass )
  {
     inclass-&gt;a = 10;
      printf("\n in kernel side = %d \n",inclass-&gt;a);
  }
  </code></pre>
  
  <p>//KERNEL END</p>
  
  <p>ERRORS:</p>
  
  <p>I am facing these all errors at kernel side only </p>
  
  <p>1) Error    Tempalte.CL     unknown type name 'test'<br>
  2) Error    Tempalte.CL     expected ';' after top level declarator<br>
  3) Error    Tempalte.CL     program scope variables are required to be declared in constant address space<br>
  4) Error    Tempalte.CL     unknown type name 'class'</p>
  
  <p>QUERY:</p>
  
  <p>Q)My main Question is How to Pass a CLASS to kernel in Intel architecture<br>
  * I have successfully passed class to kernel in AMD. whenever I tried with same code in Intel side it shows the above four errors.<br>
  * Is there any alternative method to pass class to kernel in Intel or is it possible to pass class to kernel in Intel architecture ? </p>