2

OpenCL Best Practices Guide ( https://www.cs.cmu.edu/afs/cs/academic/class/15668-s11/www/cuda-doc/OpenCL_Best_Practices_Guide.pdf ) suggests in the section 3.1.3 to use clFlush to ensure that commands happen in the right order, e.g. processing doesn't happen before data transfer:

  1. Transfer the data for queue0
  2. clFlush for queue0
  3. Run the kernel for queue0, transfer the data for queue1
  4. clFlush for queue0 and queue1
  5. Run the kernel for queue1 and retrieve the data for queue0
  6. clFlush for them both
  7. Retrieve the data for queue1

The reply here https://stackoverflow.com/a/12389713/4634819 suggests to use events to achieve, as it seems, the same.

My question is: Did I get it right, and do both clFlush and events serve the same purpose (avoiding simultaneous execution) in this case? Does it matter which of them to use?

lawful_neutral
  • 633
  • 8
  • 29

1 Answers1

5

clFlush only ensures the enqueue function enqueues the data transfer or the kernel execution, but it does not ensure the function you call is finished. There are multiple cases where you need to use events:

1 - If you are using non-blocking calls to the data transfers, you need to use events to make sure you have finished transferring all of it before you can start executing your kernel and when you are copying back to the host, you need to wait for read event to finish.

2 - If you have dependencies between kernels you are executing in both queues, then again you have to use event to order the kernels in the right way.

So your question depends on what kind of dependencies you have between kernel executions and whether you are using non-blocking calls to transfer data. If you do not have dependencies and you are using blocking calls for data transfer, clFlush will do the job. Otherwise, you need events.

parallel highway
  • 354
  • 2
  • 12