1
- // ==----------------- wait .cpp --- queue wait unit test --- -----------------==//
1
+ // ==------------------ EventClear .cpp --- queue unit tests -----------------==//
2
2
//
3
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
4
// See https://llvm.org/LICENSE.txt for license information.
@@ -30,7 +30,6 @@ pi_result redefinedUSMEnqueueMemset(pi_queue queue, void *ptr, pi_int32 value,
30
30
pi_event *event) {
31
31
// Provide a dummy non-nullptr value
32
32
*event = reinterpret_cast <pi_event>(1 );
33
- TestContext->EventReferenceCount = 1 ;
34
33
return PI_SUCCESS;
35
34
}
36
35
@@ -43,12 +42,12 @@ pi_result redefinedEventsWait(pi_uint32 num_events,
43
42
pi_result redefinedEventGetInfo (pi_event event, pi_event_info param_name,
44
43
size_t param_value_size, void *param_value,
45
44
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 )
47
46
<< " 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 ;
52
51
return PI_SUCCESS;
53
52
}
54
53
@@ -62,21 +61,17 @@ pi_result redefinedEventRelease(pi_event event) {
62
61
return PI_SUCCESS;
63
62
}
64
63
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) {
69
65
if (Plt.is_host ()) {
70
66
std::cout << " Not run on host - no PI events created in that case"
71
67
<< std::endl;
72
- return ;
68
+ return false ;
73
69
}
74
-
75
- // TODO: Skip test for CUDA temporarily
70
+ // TODO: Skip tests for CUDA temporarily
76
71
if (detail::getSyclObjImpl (Plt)->getPlugin ().getBackend () == backend::cuda) {
77
72
std::cout << " Not run on CUDA - usm is not supported for CUDA backend yet"
78
73
<< std::endl;
79
- return ;
74
+ return false ;
80
75
}
81
76
82
77
unittest::PiMock Mock{Plt};
@@ -86,16 +81,48 @@ TEST(QueueWaitTest, USMEventClear) {
86
81
Mock.redefine <detail::PiApiKind::piEventGetInfo>(redefinedEventGetInfo);
87
82
Mock.redefine <detail::PiApiKind::piEventRetain>(redefinedEventRetain);
88
83
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 ;
89
93
90
94
context Ctx{Plt};
91
95
TestContext.reset (new TestCtx (Ctx));
92
96
queue Q{Ctx, default_selector ()};
93
97
94
98
unsigned char *HostAlloc = (unsigned char *)malloc_host (1 , Ctx);
99
+ TestContext->EventReferenceCount = 1 ;
95
100
Q.memset (HostAlloc, 42 , 1 );
96
101
Q.wait ();
97
102
ASSERT_EQ (TestContext->NEventsWaitedFor , 1 );
98
103
ASSERT_EQ (TestContext->EventReferenceCount , 0 );
99
104
Q.wait ();
100
105
ASSERT_EQ (TestContext->NEventsWaitedFor , 1 );
101
106
}
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