How to pass an array in a structure to the kernel?

2019-06-25 10:24发布

问题:

I need to output to Kernel an array of structures in which there will be an array. But in the end, the data is a little correct, but in some way the error. I have this code on Host

struct myStruct
{
    int a;
    double b;
    double c[5];
};

myStruct *result = new myStruct[countOptim];

for (int i = 0; i < countOptim; i++)
{
    result[i].a = 5;
    result[i].b = 11.5;
    for (int j = 0; j < 5; j++)
    {
        result[i].c[j] = j;
    }
}

// Make kernel
Kernel kernel(program, "vector_add");

Buffer bufferResult = Buffer(context, CL_MEM_READ_WRITE, countOptim * sizeof(myStruct));

queue.enqueueWriteBuffer(bufferResult, CL_TRUE, 0, countOptim * sizeof(myStruct), result);


kernel.setArg(0, bufferResult);


// Run the kernel on specific ND range
NDRange global(countOptim);
queue.enqueueNDRangeKernel(kernel, NULL, global);

And this code on Kernel

struct myStruct
{
    int a;
    double b;
    double c[];
};
typedef struct myStruct myStruct;

__kernel void vector_add(__global myStruct *result) 
{


   // Get the index of the current element to be processed
   int id = get_global_id(0);

   printf("a - %d, b - %f, c[0] - %f",result[id].a, result[id].b, result[id].c[0]);
}

In the console displays this

Perhaps the problem is in memory

Okey, this is fixed by specifying the size of the array in the Kernel. But is it possible to do without set size of the array in kernel?

标签: c++ opencl