Skip to main content

Exception in SYCL

Errors, what we hate but can't avoid. If you ever tried to debug an OpenCL program, you know how painful it is. However, SYCL offers C++ exception handling for host code, which is a bit of a blessing.

Parallel computation device don't have exception mechanism because it impedes the parallelism.

Synchronous Exceptions

Synchronous exceptions are identical to the standard C++ exception, just of type sycl::exception.

Asynchronous Exceptions

Asynchronous Exceptions are exceptions thrown within the host task in the action graph.

You need a handler to deal with that- and that's it.

#include <sycl/sycl.hpp>
using namespace sycl;

// Our example asynchronous handler function
auto handle_async_error = [](exception_list elist) {
for (auto &e : elist) {
try {
std::rethrow_exception(e);
} catch (...) {
std::cout << "Caught SYCL ASYNC exception!!\n";
}
}
};

void say_device(const queue &Q) {
std::cout << "Device : "
<< Q.get_device().get_info<info::device::name>()
<< "\n";
}

class something_went_wrong {}; // Example exception type

int main() {
queue q{cpu_selector_v, handle_async_error};
say_device(q);

q.submit([&](handler &h) {
h.host_task([&]() { throw something_went_wrong{}; });
}).wait();

return 0;
}