1

有没有在 OpenCL 中使用 clEnqueueNativeKernel 的例子?这样就可以用 ac 或 c++ 语言编写内核。其他命令是否保持不变?

4

1 回答 1

4

本机 C++“内核”本质上只是您希望在命令队列中执行以保持命令顺序的函数。

AFAIK 它们在 GPU 上不受支持。如果您想在所有设备上执行 C++ 函数,您应该考虑使用 cl_event 回调(当状态 == 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.
于 2012-04-23T10:53:49.907 回答