0

我有一个本机内核设置,但我不知道如何将其 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] << " ";
}
4

1 回答 1

1

instead of playing hard with void* i would like to suggest to use struct create your parameter structure like:

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

and then create and fill one struct myparams in your program and pass its address to the kernelcaller

struct myparams params;
params.a=3;

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

and in the nativeKernel just unbox the void* into your parameter struct:

struct myparams *params=(myparams*)args;

beware: in the example above i passed a pointer of the stack...you might not want that ;)

于 2013-08-23T18:27:40.503 回答