Fakruddeen
Fakruddeen

Reputation: 175

Structure passing from host to kernel in opencl

I know how to pass the structure from host to code, but the problem is push_back function (which is an inbuilt function for structure) not working in opencl. I have a structure in my host like

struct MyKey
{
   int x;
   float y;
   int scale;
   MyKey(int x, float y, int scale) : x(x), y(y), scale(scale){ }
}

i created an object for this structure in host like

std :: vector<MyKey> obj;

i passed this to my kernel and i placed the structure definition there(in kernel), now when i try to call a push_back function its throwing an error.,

__kernel void test(ui32_t w, ui32_t h, constant struct MyKey* obj, i32_t thrd_size)
 {
         //toatl thread count is w*h
       i32_t i = get_global_id(0);
     if(i<thrd_size)
     {
          for (ui32_t i = 0; i < h; i++)
          {
               for (ui32_t j = 0; j < w; j++) 
                {
                    obj.push_back(MyKey(j,iw,h));
                }
          }
      }
  }

Thanks in advance..

Upvotes: 1

Views: 1789

Answers (2)

Erik Smistad
Erik Smistad

Reputation: 1019

AMD has a Static C++ Kernel Language extension which supports some C++ features such as classes.

You can find more information about it here: http://developer.amd.com/wordpress/media/2012/10/CPP_kernel_language.pdf

From the documentation: Kernel code:

Class Test
{
    setX (int value);
    private:
    int x;
}
__kernel foo (__global Test* InClass, ...)
{
    If (get_global_id(0) == 0)
    InClass->setX(5);
}

Host code:

Class Test
{
    setX (int value);
    private:
    int x;
}
MyFunc ()
{
    tempClass = new(Test);
    ... // Some OpenCL startup code – create context, queue, etc.
    cl_mem classObj = clCreateBuffer(context, CL_USE_HOST_PTR,
    sizeof(Test),&tempClass, event);
    clEnqueueMapBuffer(...,classObj,...);
    tempClass.setX(10);
    clEnqueueUnmapBuffer(...,classObj,...); //class is passed to the Device
    clEnqueueNDRange(..., fooKernel, ...);
    clEnqueueMapBuffer(...,classObj,...); //class is passed back to the Host
}

Upvotes: 2

matthias
matthias

Reputation: 2181

OpenCL's kernel language is based on C99 and thus it's impossible to have any C++ features.

Moreover, your code is wrong on a conceptual level: Even if you declare obj to be a vector of MyKey structures, the obj parameter in the kernel code is still just a pointer to a MyKey structure.

Upvotes: 2

Related Questions