forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_impl.cpp
More file actions
301 lines (254 loc) · 9.41 KB
/
Copy pathevent_impl.cpp
File metadata and controls
301 lines (254 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//==---------------- event_impl.cpp - SYCL event ---------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <CL/sycl/context.hpp>
#include <detail/event_impl.hpp>
#include <detail/event_info.hpp>
#include <detail/plugin.hpp>
#include <detail/queue_impl.hpp>
#include <detail/scheduler/scheduler.hpp>
#include "detail/config.hpp"
#include <chrono>
#ifdef XPTI_ENABLE_INSTRUMENTATION
#include "xpti_trace_framework.hpp"
#include <atomic>
#include <sstream>
#endif
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {
#ifdef XPTI_ENABLE_INSTRUMENTATION
extern xpti::trace_event_data_t *GSYCLGraphEvent;
#endif
// Threat all devices that don't support interoperability as host devices to
// avoid attempts to call method get on such events.
bool event_impl::is_host() const { return MHostEvent || !MOpenCLInterop; }
cl_event event_impl::get() const {
if (MOpenCLInterop) {
getPlugin().call<PiApiKind::piEventRetain>(MEvent);
return pi::cast<cl_event>(MEvent);
}
throw invalid_object_error(
"This instance of event doesn't support OpenCL interoperability.",
PI_INVALID_EVENT);
}
event_impl::~event_impl() {
if (MEvent)
getPlugin().call<PiApiKind::piEventRelease>(MEvent);
}
void event_impl::waitInternal() const {
if (!MHostEvent && MEvent) {
getPlugin().call<PiApiKind::piEventsWait>(1, &MEvent);
return;
}
while (MState != HES_Complete)
;
}
void event_impl::setComplete() {
if (MHostEvent || !MEvent) {
#ifndef NDEBUG
int Expected = HES_NotComplete;
int Desired = HES_Complete;
bool Succeeded = MState.compare_exchange_strong(Expected, Desired);
assert(Succeeded && "Unexpected state of event");
#else
MState.store(static_cast<int>(HES_Complete));
#endif
return;
}
assert(false && "setComplete is not supported for non-host event");
}
const RT::PiEvent &event_impl::getHandleRef() const { return MEvent; }
RT::PiEvent &event_impl::getHandleRef() { return MEvent; }
const ContextImplPtr &event_impl::getContextImpl() { return MContext; }
const plugin &event_impl::getPlugin() const { return MContext->getPlugin(); }
void event_impl::setContextImpl(const ContextImplPtr &Context) {
MHostEvent = Context->is_host();
MOpenCLInterop = !MHostEvent;
MContext = Context;
MState = HES_NotComplete;
}
event_impl::event_impl() : MState(HES_Complete) {}
event_impl::event_impl(RT::PiEvent Event, const context &SyclContext)
: MEvent(Event), MContext(detail::getSyclObjImpl(SyclContext)),
MOpenCLInterop(true), MHostEvent(false), MState(HES_Complete) {
if (MContext->is_host()) {
throw cl::sycl::invalid_parameter_error(
"The syclContext must match the OpenCL context associated with the "
"clEvent.",
PI_INVALID_CONTEXT);
}
RT::PiContext TempContext;
getPlugin().call<PiApiKind::piEventGetInfo>(MEvent, PI_EVENT_INFO_CONTEXT,
sizeof(RT::PiContext),
&TempContext, nullptr);
if (MContext->getHandleRef() != TempContext) {
throw cl::sycl::invalid_parameter_error(
"The syclContext must match the OpenCL context associated with the "
"clEvent.",
PI_INVALID_CONTEXT);
}
getPlugin().call<PiApiKind::piEventRetain>(MEvent);
}
event_impl::event_impl(QueueImplPtr Queue) {
if (Queue->is_host()) {
MState.store(HES_NotComplete);
if (Queue->has_property<property::queue::enable_profiling>()) {
MHostProfilingInfo.reset(new HostProfilingInfo());
if (!MHostProfilingInfo)
throw runtime_error("Out of host memory", PI_OUT_OF_HOST_MEMORY);
}
return;
}
MState.store(HES_Complete);
}
void *event_impl::instrumentationProlog(string_class &Name, int32_t StreamID,
uint64_t &IId) const {
void *TraceEvent = nullptr;
#ifdef XPTI_ENABLE_INSTRUMENTATION
if (!xptiTraceEnabled())
return TraceEvent;
// Use a thread-safe counter to get a unique instance ID for the wait() on the
// event
static std::atomic<uint64_t> InstanceID = {1};
xpti::trace_event_data_t *WaitEvent = nullptr;
// Create a string with the event address so it
// can be associated with other debug data
xpti::utils::StringHelper SH;
Name = SH.nameWithAddress<RT::PiEvent>("event.wait", MEvent);
// We can emit the wait associated with the graph if the
// event does not have a command object or associated with
// the command object, if it exists
if (MCommand) {
Command *Cmd = (Command *)MCommand;
WaitEvent = Cmd->MTraceEvent ? static_cast<xpti_td *>(Cmd->MTraceEvent)
: GSYCLGraphEvent;
} else
WaitEvent = GSYCLGraphEvent;
// Record the current instance ID for use by Epilog
IId = InstanceID++;
xptiNotifySubscribers(StreamID, xpti::trace_wait_begin, nullptr, WaitEvent,
IId, static_cast<const void *>(Name.c_str()));
TraceEvent = (void *)WaitEvent;
#endif
return TraceEvent;
}
void event_impl::instrumentationEpilog(void *TelemetryEvent,
const string_class &Name,
int32_t StreamID, uint64_t IId) const {
#ifdef XPTI_ENABLE_INSTRUMENTATION
if (!(xptiTraceEnabled() && TelemetryEvent))
return;
// Close the wait() scope
xpti::trace_event_data_t *TraceEvent =
(xpti::trace_event_data_t *)TelemetryEvent;
xptiNotifySubscribers(StreamID, xpti::trace_wait_end, nullptr, TraceEvent,
IId, static_cast<const void *>(Name.c_str()));
#endif
}
void event_impl::wait(
std::shared_ptr<cl::sycl::detail::event_impl> Self) const {
#ifdef XPTI_ENABLE_INSTRUMENTATION
void *TelemetryEvent = nullptr;
uint64_t IId;
std::string Name;
int32_t StreamID = xptiRegisterStream(SYCL_STREAM_NAME);
TelemetryEvent = instrumentationProlog(Name, StreamID, IId);
#endif
if (MEvent)
// presence of MEvent means the command has been enqueued, so no need to
// go via the slow path event waiting in the scheduler
waitInternal();
else if (MCommand)
detail::Scheduler::getInstance().waitForEvent(Self);
if (MCommand && !SYCLConfig<SYCL_DISABLE_EXECUTION_GRAPH_CLEANUP>::get())
detail::Scheduler::getInstance().cleanupFinishedCommands(std::move(Self));
#ifdef XPTI_ENABLE_INSTRUMENTATION
instrumentationEpilog(TelemetryEvent, Name, StreamID, IId);
#endif
}
void event_impl::wait_and_throw(
std::shared_ptr<cl::sycl::detail::event_impl> Self) {
wait(Self);
for (auto &EventImpl :
detail::Scheduler::getInstance().getWaitList(std::move(Self))) {
Command *Cmd = (Command *)EventImpl->getCommand();
if (Cmd)
Cmd->getQueue()->throw_asynchronous();
}
Command *Cmd = (Command *)getCommand();
if (Cmd)
Cmd->getQueue()->throw_asynchronous();
}
template <>
cl_ulong
event_impl::get_profiling_info<info::event_profiling::command_submit>() const {
if (!MHostEvent) {
return get_event_profiling_info<info::event_profiling::command_submit>::get(
this->getHandleRef(), this->getPlugin());
}
if (!MHostProfilingInfo)
throw invalid_object_error("Profiling info is not available.",
PI_PROFILING_INFO_NOT_AVAILABLE);
return MHostProfilingInfo->getStartTime();
}
template <>
cl_ulong
event_impl::get_profiling_info<info::event_profiling::command_start>() const {
if (!MHostEvent) {
return get_event_profiling_info<info::event_profiling::command_start>::get(
this->getHandleRef(), this->getPlugin());
}
if (!MHostProfilingInfo)
throw invalid_object_error("Profiling info is not available.",
PI_PROFILING_INFO_NOT_AVAILABLE);
return MHostProfilingInfo->getStartTime();
}
template <>
cl_ulong
event_impl::get_profiling_info<info::event_profiling::command_end>() const {
if (!MHostEvent) {
return get_event_profiling_info<info::event_profiling::command_end>::get(
this->getHandleRef(), this->getPlugin());
}
if (!MHostProfilingInfo)
throw invalid_object_error("Profiling info is not available.",
PI_PROFILING_INFO_NOT_AVAILABLE);
return MHostProfilingInfo->getEndTime();
}
template <> cl_uint event_impl::get_info<info::event::reference_count>() const {
if (!MHostEvent) {
return get_event_info<info::event::reference_count>::get(
this->getHandleRef(), this->getPlugin());
}
return 0;
}
template <>
info::event_command_status
event_impl::get_info<info::event::command_execution_status>() const {
if (!MHostEvent) {
return get_event_info<info::event::command_execution_status>::get(
this->getHandleRef(), this->getPlugin());
}
return info::event_command_status::complete;
}
static uint64_t getTimestamp() {
auto TimeStamp = std::chrono::high_resolution_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::nanoseconds>(TimeStamp)
.count();
}
void HostProfilingInfo::start() { StartTime = getTimestamp(); }
void HostProfilingInfo::end() { EndTime = getTimestamp(); }
pi_native_handle event_impl::getNative() const {
auto Plugin = getPlugin();
pi_native_handle Handle;
Plugin.call<PiApiKind::piextEventGetNativeHandle>(getHandleRef(), &Handle);
return Handle;
}
} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)