|
| 1 | +// RUN: %{build} -o %t.out |
| 2 | +// RUN: %{run} %t.out |
| 3 | + |
| 4 | +//==----- dependent_event_async_exception.cpp - Test for event async exceptions |
| 5 | +//-----==// |
| 6 | +// |
| 7 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 8 | +// See https://llvm.org/LICENSE.txt for license information. |
| 9 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include <sycl/sycl.hpp> |
| 14 | +#include <unordered_set> |
| 15 | + |
| 16 | +struct test_exception { |
| 17 | + std::string name; |
| 18 | +}; |
| 19 | + |
| 20 | +class test_exception_handler { |
| 21 | +public: |
| 22 | + test_exception_handler() |
| 23 | + : queue{[this](sycl::exception_list el) { capture(std::move(el)); }} {} |
| 24 | + sycl::queue &get_queue() { return queue; } |
| 25 | + |
| 26 | + bool has(const std::string &name) const { |
| 27 | + return captured_exceptions.count(name) != 0; |
| 28 | + } |
| 29 | + |
| 30 | + size_t count() const { return captured_exceptions.size(); } |
| 31 | + |
| 32 | + void clear() { captured_exceptions.clear(); } |
| 33 | + |
| 34 | +private: |
| 35 | + std::unordered_set<std::string> captured_exceptions; |
| 36 | + sycl::queue queue; |
| 37 | + |
| 38 | + void capture(sycl::exception_list el) { |
| 39 | + for (auto &e : el) { |
| 40 | + try { |
| 41 | + std::rethrow_exception(e); |
| 42 | + } catch (test_exception &te) { |
| 43 | + captured_exceptions.insert(te.name); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +static sycl::event |
| 50 | +make_throwing_host_event(sycl::queue &queue, std::string name, |
| 51 | + const std::vector<sycl::event> &dependencies = {}) { |
| 52 | + return queue.submit([name, &dependencies](sycl::handler &cgh) { |
| 53 | + for (auto &dep : dependencies) { |
| 54 | + cgh.depends_on(dep); |
| 55 | + } |
| 56 | + cgh.host_task([name](auto) { throw test_exception{name}; }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +int main() { |
| 61 | + test_exception_handler teh1; |
| 62 | + test_exception_handler teh2; |
| 63 | + |
| 64 | + auto e1 = make_throwing_host_event(teh1.get_queue(), "some-error"); |
| 65 | + auto e2 = make_throwing_host_event(teh2.get_queue(), "another-error", {e1}); |
| 66 | + |
| 67 | + e2.wait_and_throw(); |
| 68 | + |
| 69 | + assert(teh2.count() == 1); |
| 70 | + assert(teh2.has("another-error")); |
| 71 | + |
| 72 | + assert(teh1.count() == 1); |
| 73 | + assert(teh1.has("some-error")); |
| 74 | +} |
0 commit comments