Skip to content

Commit 55cf399

Browse files
author
iclsrc
committed
Merge from 'sycl' to 'sycl-web'
2 parents e8cdd2a + 461fa02 commit 55cf399

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

sycl/source/detail/queue_impl.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ void queue_impl::addEvent(const event &Event) {
115115
/// addSharedEvent will have the queue track the events via a shared pointer.
116116
void queue_impl::addSharedEvent(const event &Event) {
117117
std::lock_guard<mutex_class> Lock(MMutex);
118+
// Events stored in MEventsShared are not released anywhere else aside from
119+
// calls to queue::wait/wait_and_throw, which a user application might not
120+
// make, and ~queue_impl(). If the number of events grows large enough,
121+
// there's a good chance that most of them are already completed and ownership
122+
// of them can be released.
123+
const size_t EventThreshold = 128;
124+
if (MEventsShared.size() >= EventThreshold) {
125+
MEventsShared.erase(
126+
std::remove_if(
127+
MEventsShared.begin(), MEventsShared.end(),
128+
[](const event &E) {
129+
return E.get_info<info::event::command_execution_status>() ==
130+
info::event_command_status::complete;
131+
}),
132+
MEventsShared.end());
133+
}
118134
MEventsShared.push_back(Event);
119135
}
120136

sycl/unittests/queue/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_sycl_unittest(QueueTests OBJECT
2-
wait.cpp
2+
EventClear.cpp
33
)

sycl/unittests/queue/wait.cpp renamed to sycl/unittests/queue/EventClear.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//==----------------- wait.cpp --- queue wait unit test --------------------==//
1+
//==------------------ EventClear.cpp --- queue unit tests -----------------==//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -30,7 +30,6 @@ pi_result redefinedUSMEnqueueMemset(pi_queue queue, void *ptr, pi_int32 value,
3030
pi_event *event) {
3131
// Provide a dummy non-nullptr value
3232
*event = reinterpret_cast<pi_event>(1);
33-
TestContext->EventReferenceCount = 1;
3433
return PI_SUCCESS;
3534
}
3635

@@ -43,12 +42,12 @@ pi_result redefinedEventsWait(pi_uint32 num_events,
4342
pi_result redefinedEventGetInfo(pi_event event, pi_event_info param_name,
4443
size_t param_value_size, void *param_value,
4544
size_t *param_value_size_ret) {
46-
EXPECT_EQ(param_name, PI_EVENT_INFO_CONTEXT)
45+
EXPECT_EQ(param_name, PI_EVENT_INFO_COMMAND_EXECUTION_STATUS)
4746
<< "Unexpected event info requested";
48-
auto *Result = reinterpret_cast<RT::PiContext *>(param_value);
49-
RT::PiContext PiCtx =
50-
detail::getSyclObjImpl(TestContext->Ctx)->getHandleRef();
51-
*Result = PiCtx;
47+
// Report half of events as complete
48+
static int Counter = 0;
49+
auto *Result = reinterpret_cast<pi_event_status *>(param_value);
50+
*Result = (++Counter % 2 == 0) ? PI_EVENT_COMPLETE : PI_EVENT_RUNNING;
5251
return PI_SUCCESS;
5352
}
5453

@@ -62,21 +61,17 @@ pi_result redefinedEventRelease(pi_event event) {
6261
return PI_SUCCESS;
6362
}
6463

65-
// Check that the USM events are cleared from the queue upon call to wait(),
66-
// so that they are not waited for multiple times.
67-
TEST(QueueWaitTest, USMEventClear) {
68-
platform Plt{default_selector()};
64+
bool preparePiMock(platform &Plt) {
6965
if (Plt.is_host()) {
7066
std::cout << "Not run on host - no PI events created in that case"
7167
<< std::endl;
72-
return;
68+
return false;
7369
}
74-
75-
// TODO: Skip test for CUDA temporarily
70+
// TODO: Skip tests for CUDA temporarily
7671
if (detail::getSyclObjImpl(Plt)->getPlugin().getBackend() == backend::cuda) {
7772
std::cout << "Not run on CUDA - usm is not supported for CUDA backend yet"
7873
<< std::endl;
79-
return;
74+
return false;
8075
}
8176

8277
unittest::PiMock Mock{Plt};
@@ -86,16 +81,48 @@ TEST(QueueWaitTest, USMEventClear) {
8681
Mock.redefine<detail::PiApiKind::piEventGetInfo>(redefinedEventGetInfo);
8782
Mock.redefine<detail::PiApiKind::piEventRetain>(redefinedEventRetain);
8883
Mock.redefine<detail::PiApiKind::piEventRelease>(redefinedEventRelease);
84+
return true;
85+
}
86+
87+
// Check that the USM events are cleared from the queue upon call to wait(),
88+
// so that they are not waited for multiple times.
89+
TEST(QueueEventClear, ClearOnQueueWait) {
90+
platform Plt{default_selector()};
91+
if (!preparePiMock(Plt))
92+
return;
8993

9094
context Ctx{Plt};
9195
TestContext.reset(new TestCtx(Ctx));
9296
queue Q{Ctx, default_selector()};
9397

9498
unsigned char *HostAlloc = (unsigned char *)malloc_host(1, Ctx);
99+
TestContext->EventReferenceCount = 1;
95100
Q.memset(HostAlloc, 42, 1);
96101
Q.wait();
97102
ASSERT_EQ(TestContext->NEventsWaitedFor, 1);
98103
ASSERT_EQ(TestContext->EventReferenceCount, 0);
99104
Q.wait();
100105
ASSERT_EQ(TestContext->NEventsWaitedFor, 1);
101106
}
107+
108+
// Check that shared events are cleaned up from the queue once their number
109+
// exceeds a threshold.
110+
TEST(QueueEventClear, CleanupOnThreshold) {
111+
platform Plt{default_selector()};
112+
if (!preparePiMock(Plt))
113+
return;
114+
115+
context Ctx{Plt};
116+
TestContext.reset(new TestCtx(Ctx));
117+
queue Q{Ctx, default_selector()};
118+
119+
unsigned char *HostAlloc = (unsigned char *)malloc_host(1, Ctx);
120+
const int ExpectedEventThreshold = 128;
121+
TestContext->EventReferenceCount = ExpectedEventThreshold;
122+
for (size_t I = 0; I < ExpectedEventThreshold; ++I) {
123+
Q.memset(HostAlloc, 42, 1).wait();
124+
}
125+
// Half of the events (those reported as completed) should be released.
126+
Q.memset(HostAlloc, 42, 1);
127+
ASSERT_EQ(TestContext->EventReferenceCount, ExpectedEventThreshold / 2);
128+
}

0 commit comments

Comments
 (0)