7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include < CL/sycl/context.hpp>
10
+ #include < CL/sycl/device_selector.hpp>
10
11
#include < detail/event_impl.hpp>
11
12
#include < detail/event_info.hpp>
12
13
#include < detail/plugin.hpp>
@@ -31,11 +32,26 @@ namespace detail {
31
32
extern xpti::trace_event_data_t *GSYCLGraphEvent;
32
33
#endif
33
34
34
- // Threat all devices that don't support interoperability as host devices to
35
- // avoid attempts to call method get on such events.
36
- bool event_impl::is_host () const { return MHostEvent || !MOpenCLInterop; }
35
+ // If we do not yet have a context, use the default one.
36
+ void event_impl::ensureContextInitialized () {
37
+ if (MIsContextInitialized)
38
+ return ;
39
+
40
+ const device &SyclDevice = default_selector ().select_device ();
41
+ this ->setContextImpl (
42
+ detail::queue_impl::getDefaultOrNew (detail::getSyclObjImpl (SyclDevice)));
43
+ }
37
44
38
- cl_event event_impl::get () const {
45
+ bool event_impl::is_host () {
46
+ // We'll need a context before we can answer is_host question.
47
+ // setting it may adjust the values of MHostEvent and MOpenCLInterop
48
+ ensureContextInitialized ();
49
+ // Treat all devices that don't support interoperability as host devices to
50
+ // avoid attempts to call method get on such events.
51
+ return MHostEvent || !MOpenCLInterop;
52
+ }
53
+
54
+ cl_event event_impl::get () {
39
55
if (!MOpenCLInterop) {
40
56
throw invalid_object_error (
41
57
" This instance of event doesn't support OpenCL interoperability." ,
@@ -91,25 +107,32 @@ void event_impl::setComplete() {
91
107
const RT::PiEvent &event_impl::getHandleRef () const { return MEvent; }
92
108
RT::PiEvent &event_impl::getHandleRef () { return MEvent; }
93
109
94
- const ContextImplPtr &event_impl::getContextImpl () { return MContext; }
110
+ const ContextImplPtr &event_impl::getContextImpl () {
111
+ ensureContextInitialized ();
112
+ return MContext;
113
+ }
114
+
115
+ const plugin &event_impl::getPlugin () {
116
+ ensureContextInitialized ();
117
+ return MContext->getPlugin ();
118
+ }
95
119
96
- const plugin & event_impl::getPlugin () const { return MContext-> getPlugin () ; }
120
+ void event_impl::setStateIncomplete () { MState = HES_NotComplete ; }
97
121
98
122
void event_impl::setContextImpl (const ContextImplPtr &Context) {
99
123
MHostEvent = Context->is_host ();
100
124
MOpenCLInterop = !MHostEvent;
101
125
MContext = Context;
102
-
103
- MState = HES_NotComplete;
126
+ MIsContextInitialized = true ;
104
127
}
105
128
106
129
event_impl::event_impl (HostEventState State)
107
130
: MIsInitialized(false ), MIsFlushed(true ), MState(State) {}
108
131
109
132
event_impl::event_impl (RT::PiEvent Event, const context &SyclContext)
110
- : MEvent(Event ), MContext(detail::getSyclObjImpl(SyclContext) ),
111
- MOpenCLInterop ( true ), MHostEvent( false ), MIsFlushed (true ),
112
- MState(HES_Complete) {
133
+ : MIsContextInitialized( true ), MEvent(Event ),
134
+ MContext (detail::getSyclObjImpl(SyclContext)), MOpenCLInterop (true ),
135
+ MHostEvent( false ), MIsFlushed( true ), MState(HES_Complete) {
113
136
114
137
if (MContext->is_host ()) {
115
138
throw cl::sycl::invalid_parameter_error (
@@ -133,6 +156,8 @@ event_impl::event_impl(RT::PiEvent Event, const context &SyclContext)
133
156
event_impl::event_impl (const QueueImplPtr &Queue)
134
157
: MQueue{Queue}, MIsProfilingEnabled{Queue->is_host () ||
135
158
Queue->MIsProfilingEnabled } {
159
+ this ->setContextImpl (Queue->getContextImplPtr ());
160
+
136
161
if (Queue->is_host ()) {
137
162
MState.store (HES_NotComplete);
138
163
@@ -262,7 +287,7 @@ void event_impl::checkProfilingPreconditions() const {
262
287
263
288
template <>
264
289
uint64_t
265
- event_impl::get_profiling_info<info::event_profiling::command_submit>() const {
290
+ event_impl::get_profiling_info<info::event_profiling::command_submit>() {
266
291
checkProfilingPreconditions ();
267
292
if (!MHostEvent) {
268
293
if (MEvent)
@@ -279,7 +304,7 @@ event_impl::get_profiling_info<info::event_profiling::command_submit>() const {
279
304
280
305
template <>
281
306
uint64_t
282
- event_impl::get_profiling_info<info::event_profiling::command_start>() const {
307
+ event_impl::get_profiling_info<info::event_profiling::command_start>() {
283
308
checkProfilingPreconditions ();
284
309
if (!MHostEvent) {
285
310
if (MEvent)
@@ -295,8 +320,7 @@ event_impl::get_profiling_info<info::event_profiling::command_start>() const {
295
320
}
296
321
297
322
template <>
298
- uint64_t
299
- event_impl::get_profiling_info<info::event_profiling::command_end>() const {
323
+ uint64_t event_impl::get_profiling_info<info::event_profiling::command_end>() {
300
324
checkProfilingPreconditions ();
301
325
if (!MHostEvent) {
302
326
if (MEvent)
@@ -310,8 +334,7 @@ event_impl::get_profiling_info<info::event_profiling::command_end>() const {
310
334
return MHostProfilingInfo->getEndTime ();
311
335
}
312
336
313
- template <>
314
- uint32_t event_impl::get_info<info::event::reference_count>() const {
337
+ template <> uint32_t event_impl::get_info<info::event::reference_count>() {
315
338
if (!MHostEvent && MEvent) {
316
339
return get_event_info<info::event::reference_count>::get (
317
340
this ->getHandleRef (), this ->getPlugin ());
@@ -321,7 +344,7 @@ uint32_t event_impl::get_info<info::event::reference_count>() const {
321
344
322
345
template <>
323
346
info::event_command_status
324
- event_impl::get_info<info::event::command_execution_status>() const {
347
+ event_impl::get_info<info::event::command_execution_status>() {
325
348
if (MState == HES_Discarded)
326
349
return info::event_command_status::ext_oneapi_unknown;
327
350
@@ -344,13 +367,9 @@ void HostProfilingInfo::start() { StartTime = getTimestamp(); }
344
367
345
368
void HostProfilingInfo::end () { EndTime = getTimestamp (); }
346
369
347
- pi_native_handle event_impl::getNative () const {
348
- if (!MContext) {
349
- static context SyclContext;
350
- MContext = getSyclObjImpl (SyclContext);
351
- MHostEvent = MContext->is_host ();
352
- MOpenCLInterop = !MHostEvent;
353
- }
370
+ pi_native_handle event_impl::getNative () {
371
+ ensureContextInitialized ();
372
+
354
373
auto Plugin = getPlugin ();
355
374
if (!MIsInitialized) {
356
375
MIsInitialized = true ;
0 commit comments