Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] wait_and_throw invokes asynchronous handlers linked to the events that the event depends on. #12202

Open
wants to merge 5 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ void event_impl::wait_and_throw(
std::shared_ptr<sycl::detail::event_impl> Self) {
wait(Self);

auto WaitList = getWaitList();
for (const auto &Event : WaitList) {
Event->getSubmittedQueue()->throw_asynchronous();
}

if (QueueImplPtr SubmittedQueue = MSubmittedQueue.lock())
SubmittedQueue->throw_asynchronous();
}
Expand Down
141 changes: 141 additions & 0 deletions sycl/test-e2e/Basic/dependent_event_async_exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

//==----- dependent_event_async_exception.cpp - Test for event async exceptions
//-----==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <sycl/sycl.hpp>
#include <unordered_set>

struct test_exception {
std::string name;
};

class test_exception_handler {
public:
test_exception_handler()
: queue{[this](sycl::exception_list el) { capture(std::move(el)); }} {}
sycl::queue &get_queue() { return queue; }

bool has(const std::string &name) const {
return captured_exceptions.count(name) != 0;
}

size_t count() const { return captured_exceptions.size(); }

void clear() { captured_exceptions.clear(); }

private:
std::unordered_set<std::string> captured_exceptions;
sycl::queue queue;

void capture(sycl::exception_list el) {
for (auto &e : el) {
try {
std::rethrow_exception(e);
} catch (test_exception &te) {
captured_exceptions.insert(te.name);
}
}
}
};

static sycl::event
make_throwing_host_event(sycl::queue &queue, std::string name,
const std::vector<sycl::event> &dependencies = {}) {
return queue.submit([name, &dependencies](sycl::handler &cgh) {
for (auto &dep : dependencies) {
cgh.depends_on(dep);
}
cgh.host_task([name](auto) { throw test_exception{name}; });
});
}

int main() {
{
test_exception_handler teh1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be worth also having a test for the case that @gmlueck highlighted in the spec PR where there is no dependency chain between the two kernels but they are enqueued to the same queue.

It was decided that in this case we should all errors caught by the queue, so if there is a call to wait on the queue then both exceptions should be thrown.

test_exception_handler teh2;

auto e1 = make_throwing_host_event(teh1.get_queue(), "some-error");
auto e2 = make_throwing_host_event(teh2.get_queue(), "another-error", {e1});

e2.wait_and_throw();

assert(teh2.count() == 1);
assert(teh2.has("another-error"));

assert(teh1.count() == 1);
assert(teh1.has("some-error"));
}
{
int data = 0;
{
sycl::buffer<int, 1> Buf(&data, sycl::range<1>(1));
test_exception_handler teh1;
test_exception_handler teh2;

auto e1 = teh1.get_queue().submit([&](sycl::handler &cgh) {
auto B = sycl::accessor(Buf, cgh, sycl::read_write_host_task);
cgh.host_task([=]() {
B[0] = 10;
throw test_exception{"some-error"};
});
});

auto e2 = teh2.get_queue().submit([&](sycl::handler &cgh) {
auto B = sycl::accessor(Buf, cgh, sycl::read_write_host_task);
cgh.host_task([=]() {
B[0] *= 10;
throw test_exception{"another-error"};
});
});

e2.wait_and_throw();

assert(data == 100);
assert(teh2.count() == 1);
assert(teh2.has("another-error"));

assert(teh1.count() == 1);
assert(teh1.has("some-error"));
}
}
{
int data1 = 0, data2 = 0;
{
sycl::buffer<int, 1> Buf1(&data1, sycl::range<1>(1));
sycl::buffer<int, 1> Buf2(&data2, sycl::range<1>(1));
test_exception_handler teh;

auto e1 = teh.get_queue().submit([&](sycl::handler &cgh) {
auto B = sycl::accessor(Buf1, cgh, sycl::read_write_host_task);
cgh.host_task([=]() {
B[0] = 10;
throw test_exception{"some-error"};
});
});

auto e2 = teh.get_queue().submit([&](sycl::handler &cgh) {
auto B = sycl::accessor(Buf2, cgh, sycl::read_write_host_task);
cgh.host_task([=]() {
B[0] = 20;
throw test_exception{"another-error"};
});
});

e2.wait_and_throw();

assert(data1 == 10);
assert(data2 == 20);
assert(teh.count() == 2);
assert(teh.has("another-error"));
assert(teh.has("some-error"));
}
}
}
steffenlarsen marked this conversation as resolved.
Show resolved Hide resolved
Loading