3

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

3
  • Don't do it. You're not supposed to pass structures. Use plain arrays instead. Commented Dec 16, 2011 at 11:25
  • @SK-logic i tried using a 3d array for the data structure instead but it said stack alignment problem or something like that .. hence i'm unable to do it .. coud u suggest an alternate data structure i cud use to store the nodes as i have defined in the struct node definition ?? Commented Dec 16, 2011 at 15:20
  • 1
    Try using a flat array, recalculating the indexes accordingly. Commented Dec 16, 2011 at 15:41

1 Answer 1

10

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 */
};
Sign up to request clarification or add additional context in comments.

2 Comments

what if i pass in an array of x. how do i index x
+1 for suggestion to keep kernel source in a separate file. The string in the C source practice is just sick unless for really short wrappers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.