Skip to content

Commit fed8a1a

Browse files
[SYCL] empty event constructor, more explicit host event handling (#6388)
making host events more explicit. simplifies the logic, makes checking for host (or not) simpler, and reduces potential contention. This is a follow up to #6296 Signed-off-by: Chris Perkins [email protected]
1 parent a430ecb commit fed8a1a

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

sycl/source/detail/event_impl.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ void event_impl::ensureContextInitialized() {
3737
if (MIsContextInitialized)
3838
return;
3939

40-
const device &SyclDevice = default_selector().select_device();
41-
this->setContextImpl(
42-
detail::queue_impl::getDefaultOrNew(detail::getSyclObjImpl(SyclDevice)));
40+
if (MHostEvent) {
41+
QueueImplPtr HostQueue = Scheduler::getInstance().getDefaultHostQueue();
42+
this->setContextImpl(detail::getSyclObjImpl(HostQueue->get_context()));
43+
} else {
44+
const device &SyclDevice = default_selector().select_device();
45+
this->setContextImpl(detail::queue_impl::getDefaultOrNew(
46+
detail::getSyclObjImpl(SyclDevice)));
47+
}
4348
}
4449

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

129-
event_impl::event_impl(HostEventState State)
130-
: MIsInitialized(false), MIsFlushed(true), MState(State) {}
131+
event_impl::event_impl(std::optional<HostEventState> State)
132+
: MIsInitialized(false), MHostEvent(State), MIsFlushed(true),
133+
MState(State.value_or(HES_Complete)) {}
131134

132135
event_impl::event_impl(RT::PiEvent Event, const context &SyclContext)
133136
: MIsContextInitialized(true), MEvent(Event),

sycl/source/detail/event_impl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class event_impl {
4343
/// Constructs a ready SYCL event.
4444
///
4545
/// If the constructed SYCL event is waited on it will complete immediately.
46-
event_impl(HostEventState State = HES_Complete);
46+
/// Normally constructs a host event, use std::nullopt to instead instantiate
47+
/// a device event.
48+
event_impl(std::optional<HostEventState> State = HES_Complete);
49+
4750
/// Constructs an event instance from a plug-in event handle.
4851
///
4952
/// The SyclContext must match the plug-in context associated with the

sycl/source/detail/helpers.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ std::vector<RT::PiEvent> getOrWaitEvents(std::vector<cl::sycl::event> DepEvents,
2323
std::vector<RT::PiEvent> Events;
2424
for (auto SyclEvent : DepEvents) {
2525
auto SyclEventImplPtr = detail::getSyclObjImpl(SyclEvent);
26-
// throwaway events created with default constructor will not have a context
27-
// (which is set lazily) calling is_host(), getContextImpl() would set that
26+
// throwaway events created with empty constructor will not have a context
27+
// (which is set lazily) calling getContextImpl() would set that
2828
// context, which we wish to avoid as it is expensive.
29-
if (SyclEventImplPtr->MIsContextInitialized == false) {
29+
if (SyclEventImplPtr->MIsContextInitialized == false &&
30+
!SyclEventImplPtr->is_host()) {
3031
continue;
3132
}
3233
if (SyclEventImplPtr->is_host() ||

sycl/source/event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
__SYCL_INLINE_NAMESPACE(cl) {
2323
namespace sycl {
2424

25-
event::event() : impl(std::make_shared<detail::event_impl>()) {}
25+
event::event() : impl(std::make_shared<detail::event_impl>(std::nullopt)) {}
2626

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

0 commit comments

Comments
 (0)