Skip to content

[SYCL] Optimize host event wait #6245

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
Jun 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
13 changes: 8 additions & 5 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ event_impl::~event_impl() {
getPlugin().call<PiApiKind::piEventRelease>(MEvent);
}

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

while (MState != HES_Complete)
;
if (MState == HES_Complete)
return;

std::unique_lock lock(MMutex);
cv.wait(lock, [this] { return MState == HES_Complete; });
}

void event_impl::setComplete() {
Expand All @@ -77,6 +80,7 @@ void event_impl::setComplete() {
#else
MState.store(static_cast<int>(HES_Complete));
#endif
cv.notify_all();
return;
}

Expand Down Expand Up @@ -190,8 +194,7 @@ void event_impl::instrumentationEpilog(void *TelemetryEvent,
#endif
}

void event_impl::wait(
std::shared_ptr<cl::sycl::detail::event_impl> Self) const {
void event_impl::wait(std::shared_ptr<cl::sycl::detail::event_impl> Self) {
if (MState == HES_Discarded)
throw sycl::exception(make_error_code(errc::invalid),
"wait method cannot be used for a discarded event.");
Expand Down
6 changes: 4 additions & 2 deletions sycl/source/detail/event_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <atomic>
#include <cassert>
#include <condition_variable>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
Expand Down Expand Up @@ -70,7 +71,7 @@ class event_impl {
/// Self is needed in order to pass shared_ptr to Scheduler.
///
/// \param Self is a pointer to this event.
void wait(std::shared_ptr<cl::sycl::detail::event_impl> Self) const;
void wait(std::shared_ptr<cl::sycl::detail::event_impl> Self);

/// Waits for the event.
///
Expand Down Expand Up @@ -112,7 +113,7 @@ class event_impl {
~event_impl();

/// Waits for the event with respect to device type.
void waitInternal() const;
void waitInternal();

/// Marks this event as completed.
void setComplete();
Expand Down Expand Up @@ -248,6 +249,7 @@ class event_impl {
bool MNeedsCleanupAfterWait = false;

std::mutex MMutex;
std::condition_variable cv;
};

} // namespace detail
Expand Down