Skip to content

[SYCL] empty event constructor, more explicit host event handling #6388

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

Merged
merged 2 commits into from
Jul 6, 2022
Merged
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
19 changes: 11 additions & 8 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ void event_impl::ensureContextInitialized() {
if (MIsContextInitialized)
return;

const device &SyclDevice = default_selector().select_device();
this->setContextImpl(
detail::queue_impl::getDefaultOrNew(detail::getSyclObjImpl(SyclDevice)));
if (MHostEvent) {
QueueImplPtr HostQueue = Scheduler::getInstance().getDefaultHostQueue();
this->setContextImpl(detail::getSyclObjImpl(HostQueue->get_context()));
} else {
const device &SyclDevice = default_selector().select_device();
this->setContextImpl(detail::queue_impl::getDefaultOrNew(
detail::getSyclObjImpl(SyclDevice)));
}
}

bool event_impl::is_host() {
// We'll need a context before we can answer is_host question.
// setting it may adjust the values of MHostEvent and MOpenCLInterop
ensureContextInitialized();
// Treat all devices that don't support interoperability as host devices to
// avoid attempts to call method get on such events.
return MHostEvent || !MOpenCLInterop;
Expand Down Expand Up @@ -126,8 +128,9 @@ void event_impl::setContextImpl(const ContextImplPtr &Context) {
MIsContextInitialized = true;
}

event_impl::event_impl(HostEventState State)
: MIsInitialized(false), MIsFlushed(true), MState(State) {}
event_impl::event_impl(std::optional<HostEventState> State)
: MIsInitialized(false), MHostEvent(State), MIsFlushed(true),
MState(State.value_or(HES_Complete)) {}

event_impl::event_impl(RT::PiEvent Event, const context &SyclContext)
: MIsContextInitialized(true), MEvent(Event),
Expand Down
5 changes: 4 additions & 1 deletion sycl/source/detail/event_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class event_impl {
/// Constructs a ready SYCL event.
///
/// If the constructed SYCL event is waited on it will complete immediately.
event_impl(HostEventState State = HES_Complete);
/// Normally constructs a host event, use std::nullopt to instead instantiate
/// a device event.
event_impl(std::optional<HostEventState> State = HES_Complete);

/// Constructs an event instance from a plug-in event handle.
///
/// The SyclContext must match the plug-in context associated with the
Expand Down
7 changes: 4 additions & 3 deletions sycl/source/detail/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ std::vector<RT::PiEvent> getOrWaitEvents(std::vector<cl::sycl::event> DepEvents,
std::vector<RT::PiEvent> Events;
for (auto SyclEvent : DepEvents) {
auto SyclEventImplPtr = detail::getSyclObjImpl(SyclEvent);
// throwaway events created with default constructor will not have a context
// (which is set lazily) calling is_host(), getContextImpl() would set that
// throwaway events created with empty constructor will not have a context
// (which is set lazily) calling getContextImpl() would set that
// context, which we wish to avoid as it is expensive.
if (SyclEventImplPtr->MIsContextInitialized == false) {
if (SyclEventImplPtr->MIsContextInitialized == false &&
!SyclEventImplPtr->is_host()) {
continue;
}
if (SyclEventImplPtr->is_host() ||
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

event::event() : impl(std::make_shared<detail::event_impl>()) {}
event::event() : impl(std::make_shared<detail::event_impl>(std::nullopt)) {}

event::event(cl_event ClEvent, const context &SyclContext)
: impl(std::make_shared<detail::event_impl>(
Expand Down