Search code examples
gpuopenclblockingnonblockingread-write

Difference between using blocking write and event.wait() with non-blocking write on OpenCL


This may end up being silly, but I stumbled into this question and I am not being able to answer it myself.

What is the practical difference between using the following two pieces of code when writing to a buffer in OpenCL?

  1. Blocking write:
queue->enqueueWriteBuffer(d_vec, CL_TRUE, 0, sizeof(int) * vec.size(), vec.data());
  1. Non-blocking write with event.wait():
cl::Event event;
queue->enqueueWriteBuffer(d_vec, CL_FALSE, 0, sizeof(int) * vec.size(), vec.data(), nullptr, &event);
event.wait();

It seems to me that both codes would behave the same in the end. Can anyone explain the difference?

Thank you!


Solution

  • Yes, they are effectively the same if that's all you're doing. However, if you're additionally enqueueing kernels or other non-blocking operations to a concurrent queue, or to other queues, then the buffer write or read can potentially run in parallel to those other operations. You can also skip waiting for the write event to complete by using the event as an input (dependency) for a subsequent enqueued task.

    The blocking write is just a shortcut for when you don't need any of those abilities.