你如何读取传递给本机内核的参数?

时间:2013-08-23 18:13:52

标签: opencl

我有一个本机内核设置但我不知道如何将其void *参数转换为有用的东西。在这个代码片段的本机内核中,我如何获得int(7)或int [](16个int设置为0)?

void __stdcall nativeKernel(void * args) 
{
    int a1 = (*(int*)args);
    cout << "a1-->: "<< a1 << endl; // gibberish
}


void kernelCaller() 
{
    const int dim1Size = 16;
    int dim1[dim1Size] = {};

    cl_int status = 0;

    cl_mem mem_d1 = clCreateBuffer(*context, 0, sizeof(int)*dim1Size, NULL, &status);
    clEnqueueWriteBuffer(*queue, mem_d1, CL_TRUE, 0, sizeof(int)*dim1Size, dim1, 0, NULL, NULL);

    const void* args[2] = {(void*)7, NULL}; 
    cl_mem mem_list[1] = {mem_d1};
    const void* args_mem_loc[1] = {&args[1]};

    cl_event run;
    status = clEnqueueNativeKernel(*queue, nativeKernel, args, 2, 1, mem_list, args_mem_loc, 0, NULL, &run);
    status = clEnqueueReadBuffer(*queue, mem_d1, CL_TRUE, 0, sizeof(int)*dim1Size, dim1, 1, &run, NULL);

    for(auto i = 0; i != dim1Size; i++)
        cout << dim1[i] << " ";
}

1 个答案:

答案 0 :(得分:1)

而不是用void*努力玩我想建议使用struct 创建您的参数结构,如:

struct myparams{
  int a
  int a[3];
};

然后在程序中创建并填充一个struct myparams并将其地址传递给kernelcaller

struct myparams params;
params.a=3;

status = clEnqueueNativeKernel(*queue, nativeKernel, (void*)&params, 2, 1, mem_list, args_mem_loc, 0, NULL, &run);

并在nativeKernel只需将void*取消装入参数struct:

struct myparams *params=(myparams*)args;

要注意:在上面的示例中,我传递了堆栈的指针......您可能不希望这样;)