在opencl中从主机传递到内核的结构

时间:2013-01-17 05:38:47

标签: kernel structure opencl

我知道如何将结构从主机传递给代码,但问题是push_back函数(这是结构的内置函数)不能在opencl中工作。 我的主机中有一个结构,如

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

我在主机中为这个结构创建了一个对象,如

std :: vector<MyKey> obj;

我把它传递给了我的内核,我将结构定义放在那里(在内核中),现在当我尝试调用push_back函数时抛出错误。,

__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));
                }
          }
      }
  }

提前致谢..

2 个答案:

答案 0 :(得分:2)

OpenCL的内核语言基于C99,因此不可能有任何C ++功能。

此外,您的代码在概念层面上是错误的:即使您将obj声明为MyKey结构的向量,内核代码中的obj参数仍然只是一个指向MyKey结构的指针。

答案 1 :(得分:2)

AMD有一个静态C ++内核语言扩展,它支持一些C ++功能,例如类。

您可以在此处找到有关它的更多信息:http://developer.amd.com/wordpress/media/2012/10/CPP_kernel_language.pdf

从文档中: 内核代码:

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

主机代码:

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
}
相关问题