Callbacks in sycl

Can SYCL use callbacks like OpenCL?

The purpose is to:

  1. enqueue kernel operation
  2. async the runtime calls the callback function when the kernel finishes
  3. callback function: do some management and enqueue read operation
  4. async the runtime calls the second callback function when the read op finishes
  5. second callback function: do some management, continue in 1 or do other stuff.

How can be done this with SYCL? What could be the most performant way? I need to do such decisions and management as soon as the op finishes, and dynamically do new ops.

In SYCL 1.2.1 it’s not possible to use callbacks but you can do it with the underlying OpenCL objects. It should be possible to do it without relying on callbacks. Perhaps something like dedicating a thread to it which blocks on memory reads (i.e. a host accessor) or maybe just waits on the kernel finishing.

In SYCL 2020 you can enqueue functions to run on the host using a host task, though the caveat to this is the function is enqueued onto the DAG so is subject to data dependencies and therefore isn’t guaranteed not to be reordered by the runtime, though generally if you enqueue the host task immediately after enqueueing the kernel it will run right after the kernel completes, if you want to guarantee no reordering then you could use an in-order queue (also in SYCL 2020). This does mean waiting for a SYCL implementation with the host task implementation.