0

I want to pass a structure to opencl kernel, the structure is

struct test
{
    int *x;
    float *y;
    char *z;
};

and the memory allocation and initialization is like

    struct test t;
t.x = (int*)malloc(sizeof(int)*100);
t.y = (float*) malloc (sizeof(float)*50);
t.z = (char*) malloc (sizeof(char) *25);
  for(i = 0;i<100;i++)
  {
      t.x[i] = i;
      if(i<50)
          {
              t.y[i] = i;
          if(i<25)
              t.z[i] = 'a';
          }
  }

can i pass such a structure to opencl kernel

Meluha
  • 1,460
  • 3
  • 14
  • 19
  • Do not do it. Firstly, it does not make any sense, secondly, your structure contains pointers and will be meaningless. Not to mention different alignment and data types widths. If you want to pass structures, you're definitely doing something wrong and it's a time to rethink your architecture. – SK-logic Feb 01 '13 at 12:51

2 Answers2

2

You can pass such a structure, but it will be pointless because x, y and z point to different memory regions. Each of these memory buffers must be transferred on its own.

matthias
  • 2,161
  • 15
  • 22
  • yes you are correct; its not only pointless even its impossible to pass this structure as one unit as they are pointing to different memory location. But what if i have very big structure(means a structure with more than 10-15 parameters) do i still have to pass them one by one or is there any solution. – Meluha Feb 19 '13 at 11:21
  • Unfortunately, each memory buffer that is used by a kernel must be set explicitly via `clSetKernelArg`. But maybe, you could break down your problem a little bit more? – matthias Feb 19 '13 at 16:09
0

Instead of structure, its better to allocate memory at the host side and send them to kernel as kernel parameters.

Megharaj
  • 1,589
  • 2
  • 20
  • 32