简体   繁体   English

OpenCL C++ 绑定:如何为 enqueueWriteBuffer 竞争实现回调

[英]OpenCL C++ Bindings: How to implement a callback for enqueueWriteBuffer competition

I'm just getting started with OpenCL 1.2 and the C++ Bindings.我刚刚开始使用 OpenCL 1.2 和 C++ 绑定。 I want to enqueue a write buffer asynchronous and get a callback once the operation has been completed.我想将写入缓冲区异步排队并在操作完成后获得回调。 Here is a stripped down version of the relevant lines of code:这是相关代码行的精简版本:

cl::Event enqueuingBufferReady;
auto error = enqueuingBufferReady.setCallback (CL_COMPLETE, [] (cl_event, cl_int, void*) { std::cout << "Enqueueing complete\n"; });
std::cout << "SetCallback return value: " << MyOpenCLHelpers::getErrorString (error) << std::endl;

// source is a std::vector<int>, buffer is a cl::Buffer of a matching size
commandQueue.enqueueWriteBuffer (buffer, CL_FALSE, 0, sizeof (int) * source.size(), source.data(), NULL, &enqueuingBufferReady);

//... execute the kernel - works successfully!

cl_int info;
enqueuingBufferAReady.getInfo (CL_EVENT_COMMAND_EXECUTION_STATUS, &info);
std::cout << "State of enqueuing " << MyOpenCLHelpers::getEventCommandExecutionStatusString (info) << std::endl;

What works:什么工作:

The kernel is executed successfully and produces the right results.内核成功执行并产生正确的结果。 Enqueuing of the buffer should have worked.缓冲区的入队应该有效。 The program terminates with a print程序以打印结束

State of enqueuing CL_COMPLETE

What does not work:什么不起作用:

The setCallback call returns setCallback 调用返回

SetCallback return value: CL_INVALID_EVENT

The callback is never called.永远不会调用回调。

So what's wrong with this piece of code and how could it be changed to work as intended?那么这段代码有什么问题,如何将其更改为按预期工作?

In the meanwhile I found it out by myself.与此同时,我自己发现了它。 My fault was to set the callback before enqueueing the write buffer.我的错误是在将写入缓冲区入队之前设置回调。 The right order is:正确的顺序是:

cl::Event enqueuingBufferReady;

// source is a std::vector<int>, buffer is a cl::Buffer of a matching size
commandQueue.enqueueWriteBuffer (buffer, CL_FALSE, 0, sizeof (int) * source.size(), source.data(), NULL, &enqueuingBufferReady);

auto error = enqueuingBufferReady.setCallback (CL_COMPLETE, [] (cl_event, cl_int, void*) { std::cout << "Enqueueing complete\n"; });
std::cout << "SetCallback return value: " << MyOpenCLHelpers::getErrorString (error) << std::endl;

Only after the call to enqueueWriteBuffer the passed in cl::Event becomes valid and the subsequent setCallback call works.只有在调用enqueueWriteBuffer之后传入的cl::Event有效并且随后的setCallback调用起作用。 I was a bit confused on this because I wasn't sure how it was guaranteed that enqueueing the buffer won't have finished before the callback was set, however my test showed that this doesn't matter as the callback is even called if it is set long after the operation was already completed.我对此有点困惑,因为我不确定如何保证在设置回调之前不会完成缓冲区的排队,但是我的测试表明这并不重要,因为如果回调甚至会被调用在操作完成后很久才设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM