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

限于喜欢 提交于 2019-12-10 15:36:35

问题


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?

来源:https://stackoverflow.com/questions/50970079/how-to-pass-an-array-in-a-structure-to-the-kernel

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