Skip to content

Commit f3a0970

Browse files
[SYCL] Optimize host event wait (#6245)
1 parent 1927d21 commit f3a0970

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

sycl/source/detail/event_impl.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ event_impl::~event_impl() {
5050
getPlugin().call<PiApiKind::piEventRelease>(MEvent);
5151
}
5252

53-
void event_impl::waitInternal() const {
53+
void event_impl::waitInternal() {
5454
if (!MHostEvent && MEvent) {
5555
getPlugin().call<PiApiKind::piEventsWait>(1, &MEvent);
5656
return;
@@ -61,8 +61,11 @@ void event_impl::waitInternal() const {
6161
make_error_code(errc::invalid),
6262
"waitInternal method cannot be used for a discarded event.");
6363

64-
while (MState != HES_Complete)
65-
;
64+
if (MState == HES_Complete)
65+
return;
66+
67+
std::unique_lock lock(MMutex);
68+
cv.wait(lock, [this] { return MState == HES_Complete; });
6669
}
6770

6871
void event_impl::setComplete() {
@@ -77,6 +80,7 @@ void event_impl::setComplete() {
7780
#else
7881
MState.store(static_cast<int>(HES_Complete));
7982
#endif
83+
cv.notify_all();
8084
return;
8185
}
8286

@@ -190,8 +194,7 @@ void event_impl::instrumentationEpilog(void *TelemetryEvent,
190194
#endif
191195
}
192196

193-
void event_impl::wait(
194-
std::shared_ptr<cl::sycl::detail::event_impl> Self) const {
197+
void event_impl::wait(std::shared_ptr<cl::sycl::detail::event_impl> Self) {
195198
if (MState == HES_Discarded)
196199
throw sycl::exception(make_error_code(errc::invalid),
197200
"wait method cannot be used for a discarded event.");

sycl/source/detail/event_impl.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <atomic>
1919
#include <cassert>
20+
#include <condition_variable>
2021

2122
__SYCL_INLINE_NAMESPACE(cl) {
2223
namespace sycl {
@@ -70,7 +71,7 @@ class event_impl {
7071
/// Self is needed in order to pass shared_ptr to Scheduler.
7172
///
7273
/// \param Self is a pointer to this event.
73-
void wait(std::shared_ptr<cl::sycl::detail::event_impl> Self) const;
74+
void wait(std::shared_ptr<cl::sycl::detail::event_impl> Self);
7475

7576
/// Waits for the event.
7677
///
@@ -112,7 +113,7 @@ class event_impl {
112113
~event_impl();
113114

114115
/// Waits for the event with respect to device type.
115-
void waitInternal() const;
116+
void waitInternal();
116117

117118
/// Marks this event as completed.
118119
void setComplete();
@@ -248,6 +249,7 @@ class event_impl {
248249
bool MNeedsCleanupAfterWait = false;
249250

250251
std::mutex MMutex;
252+
std::condition_variable cv;
251253
};
252254

253255
} // namespace detail

0 commit comments

Comments
 (0)