在OpenCL中是否有使用clEnqueueNativeKernel的示例?通过这种方式,可以用c或c ++语言编写内核。其他命令保持不变吗?
答案 0 :(得分:4)
Native C ++“kernels”本质上只是您希望在命令队列中执行以保留命令顺序的函数。
AFAIK在GPU上不支持它们。如果要在所有设备上执行C ++函数,则应考虑使用cl_event回调(当status == CL_COMPLETE时)。
假设您有一个要从设备读取的缓冲区对象并传递给C ++函数。你也想传递一些整数值 (我使用C ++ OpenCL包装器):
// First of all, we should define a struct which describes our arguments list.
struct Arguments
{
int integer;
void* buffer_host;
};
// Define C/C++ function you want to call.
void CFunction(void *args)
{
Arguments args = reinterpret_cast<Arguments*>(args);
// Do something with args->integer and args->buffer_host.
}
// ...
Arguments args = {.integer = 0, .buffer_host = NULL};
// First, we should define Buffer objects in arguments.
std::vector<cl::Memory> buffers_dev;
buffers_dev.push_back(a_buffer);
// Then we should define pointers to *pointer in args* which will be set
// when OpenCL read data from buffers_dev to the host memory.
std::vector<const void*> buffers_host;
buffers_host.push_back(&args.buffer_host);
// Finally, set integer
args.integer = 10;
queue.enqueueNativeKernel(CFunction,
std::make_pair(&args, siezof(Arguments)),
&buffers_dev,
&buffers_host);
// At this point args were copied by OpenCL and you may reuse or delete it.