Skip to content

Commit 3e42e0e

Browse files
[SYCL] Fix USM event clearing when calling queue::wait()
Clear the USM events from the queue upon calls to wait() so that they are not waited for multiple times. Signed-off-by: Sergey Semenov <[email protected]>
1 parent 24726df commit 3e42e0e

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

sycl/source/detail/queue_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
188188
for (event &Event : MUSMEvents) {
189189
Event.wait();
190190
}
191+
191192
MEvents.clear();
193+
MUSMEvents.clear();
192194

193195
#ifdef XPTI_ENABLE_INSTRUMENTATION
194196
instrumentationEpilog(TelemetryEvent, Name, StreamID, IId);

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ endfunction()
5757
add_subdirectory(misc)
5858
add_subdirectory(pi)
5959
add_subdirectory(program)
60+
add_subdirectory(queue)
6061
add_subdirectory(scheduler)
6162
add_subdirectory(thread_safety)

sycl/unittests/queue/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_sycl_unittest(QueueTests OBJECT
2+
wait.cpp
3+
)

sycl/unittests/queue/wait.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <CL/sycl.hpp>
2+
#include <detail/context_impl.hpp>
3+
#include <gtest/gtest.h>
4+
#include <helpers/PiMock.hpp>
5+
6+
using namespace cl::sycl;
7+
8+
struct TestCtx {
9+
TestCtx(context &Ctx) : Ctx{Ctx} {};
10+
11+
context &Ctx;
12+
int NEventsWaitedFor = 0;
13+
};
14+
15+
std::unique_ptr<TestCtx> TestContext;
16+
17+
pi_result redefinedUSMEnqueueMemset(pi_queue queue, void *ptr, pi_int32 value,
18+
size_t count,
19+
pi_uint32 num_events_in_waitlist,
20+
const pi_event *events_waitlist,
21+
pi_event *event) {
22+
// Provide a dummy non-nullptr value
23+
*event = reinterpret_cast<pi_event>(1);
24+
return PI_SUCCESS;
25+
}
26+
27+
pi_result redefinedEventsWait(pi_uint32 num_events,
28+
const pi_event *event_list) {
29+
++TestContext->NEventsWaitedFor;
30+
return PI_SUCCESS;
31+
}
32+
33+
pi_result redefinedEventGetInfo(pi_event event, pi_event_info param_name,
34+
size_t param_value_size, void *param_value,
35+
size_t *param_value_size_ret) {
36+
EXPECT_EQ(param_name, PI_EVENT_INFO_CONTEXT)
37+
<< "Unexpected event info requested";
38+
auto *Result = reinterpret_cast<RT::PiContext *>(param_value);
39+
RT::PiContext PiCtx =
40+
detail::getSyclObjImpl(TestContext->Ctx)->getHandleRef();
41+
*Result = PiCtx;
42+
return PI_SUCCESS;
43+
}
44+
45+
pi_result redefinedEventRetain(pi_event event) { return PI_SUCCESS; }
46+
47+
pi_result redefinedEventRelease(pi_event event) { return PI_SUCCESS; }
48+
49+
// Check that the USM events are cleared from the queue upon call to wait(),
50+
// so that they are not waited for multiple times.
51+
TEST(QueueWaitTest, USMEventClear) {
52+
platform Plt{default_selector()};
53+
if (Plt.is_host()) {
54+
std::cout << "Not run on host - no PI events created in that case"
55+
<< std::endl;
56+
return;
57+
}
58+
59+
unittest::PiMock Mock{Plt};
60+
Mock.redefine<detail::PiApiKind::piextUSMEnqueueMemset>(
61+
redefinedUSMEnqueueMemset);
62+
Mock.redefine<detail::PiApiKind::piEventsWait>(redefinedEventsWait);
63+
Mock.redefine<detail::PiApiKind::piEventGetInfo>(redefinedEventGetInfo);
64+
Mock.redefine<detail::PiApiKind::piEventRetain>(redefinedEventRetain);
65+
Mock.redefine<detail::PiApiKind::piEventRelease>(redefinedEventRelease);
66+
67+
context Ctx{Plt};
68+
TestContext.reset(new TestCtx(Ctx));
69+
queue Q{Ctx, default_selector()};
70+
71+
unsigned char *HostAlloc = (unsigned char *)malloc_host(1, Ctx);
72+
Q.memset(HostAlloc, 42, 1);
73+
Q.wait();
74+
ASSERT_EQ(TestContext->NEventsWaitedFor, 1);
75+
Q.wait();
76+
ASSERT_EQ(TestContext->NEventsWaitedFor, 1);
77+
}

0 commit comments

Comments
 (0)