【问题标题】:How do you read arguments passed to a native kernel?您如何读取传递给本机内核的参数?
【发布时间】:2013-08-24 02:27:40
【问题描述】:

我有一个本地内核设置,但我不知道如何将它的 void* 参数转换成任何有用的东西。在这个 sn-p 的本机内核中,我将如何获得 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] << " ";
}

【问题讨论】:

    标签: opencl


    【解决方案1】:

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

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

    然后在你的程序中创建并填充一个struct myparams,并将其地址传递给内核调用者

    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 myparams *params=(myparams*)args;
    

    注意:在上面的例子中,我传递了一个堆栈指针……你可能不想要那个;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 2015-01-30
      • 2018-12-14
      • 2017-10-26
      • 2013-01-29
      相关资源
      最近更新 更多