passing array of structs to the kernel in open CL

跟風遠走 提交于 2019-12-06 07:46:53

问题


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 ..

my structure definition is this

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

and my kernel argument is set like this

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

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 :)


回答1:


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.

Your kernel source would look like this:

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

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


来源:https://stackoverflow.com/questions/8532441/passing-array-of-structs-to-the-kernel-in-open-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!