在开放式CL中将结构数组传递给内核 [英] passing array of structs to the kernel in open CL

查看:99
本文介绍了在开放式CL中将结构数组传递给内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在开放式CL中实现距离矢量程序.

hi i'm trying to implement the distance vector program in open CL ..

基本上,我在将结构数组作为参数传递给内核时遇到了问题.

basically i'm having problems with passing an array of structures into the kernel as an argument ..

我的结构定义是这个

    typedef struct 
    {
    int a[nodes][4];
    }node;
    node * srcA;

为此分配内存后..我已使用此代码将其捆绑到缓冲区对象中

after allocating the memory for this .. i have bundled it into a buffer object using this code

         // allocate the buffer memory objects
    memobjs1 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,           
         sizeof(node) * n, srcA, NULL);

if (memobjs1 == (cl_mem)0)
{
    printf("ERROR: Failed to create Buffer...mem[0]\n");
    clReleaseCommandQueue(cmd_queue);
    clReleaseContext(context);
    return -1;
}

我的内核参数设置如下

    err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &memobjs1);

现在我想将此结构数组(即srcA指向)传递到内核中 我已经做到了..

now i want to pass this array of structs ( i.e. pointed by srcA ) into the kernel i have done this ..

    const char *ocl_test_programs[] = {\
              "__kernel void disvec (__global node *x,__global int *p)"\
          "{"\
         "int i=1,r=1,n;"\
          "r=p[1]; "\
          "n=p[0];"\

          //"for(i=1;i<=n;i++) "\

         "{"\

          "if(x[r].a[i][2]!=999 && x[r].a[i][2]!=0)"\

         "{"\

      "int j = get_global_id(0); "\

        /*  "int k=x[r].a[i][2] + x[i].a[j][2];"\
            "if(x[r].a[j][2]>k)"\
        "{ "\

            "   x[r].a[j][2] = k;"\
            "x[r].a[j][3] = i; } "\   */
        //" } "\  

     " } "\
    " } "\
    " } "
    };

当我运行此程序时,它说未定义节点类型...我是否必须记住要传递的其他一些参数?我应该做出什么改变..?如果有人可以给我一个简单的代码,用一个简单的例子来说明将结构传递到内核中,那将不胜感激..非常感谢:)

when i run this program it says node type not defined ... do i have to keep in mind some other parameters to pass ?? what are the changes i shud make .. ?? if someone can atleast give me a simple code to illustrate struct passing into the kernel with a simple example it will be much appreciated .. thanks a lot :)

推荐答案

您还需要在内核中提供结构定义.编译内核的编译器并不神奇地知道C代码中定义的类型.当您将内核保存在一个单独的文件中,而不是将其作为一个巨大的字符串保存在主"程序中时,这一点更加明显.

You need to provide the structure definition in the kernel as well. The compiler that compiles your kernel doesn't magically know about types defined in your C code. This is more obvious when you keep your kernel in a separate file, rather than keep it as a giant string in your "main" program.

您的内核源代码如下:

typedef struct {
    int a[nodes][4];
} node;

kernel void disvec (global node *x, global int *p) {
    /* you kernel code here */
};

这篇关于在开放式CL中将结构数组传递给内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆