forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
379 lines (315 loc) · 14.6 KB
/
queue.cpp
File metadata and controls
379 lines (315 loc) · 14.6 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//==-------------- queue.cpp -----------------------------------------------==//
//
// 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 <detail/backend_impl.hpp>
#include <detail/event_impl.hpp>
#include <detail/queue_impl.hpp>
#include <sycl/detail/common.hpp>
#include <sycl/event.hpp>
#include <sycl/exception_list.hpp>
#include <sycl/ext/codeplay/experimental/fusion_properties.hpp>
#include <sycl/handler.hpp>
#include <sycl/queue.hpp>
#include <algorithm>
namespace sycl {
inline namespace _V1 {
queue::queue(const context &SyclContext, const device_selector &DeviceSelector,
const async_handler &AsyncHandler, const property_list &PropList) {
const std::vector<device> Devs = SyclContext.get_devices();
auto Comp = [&DeviceSelector](const device &d1, const device &d2) {
return DeviceSelector(d1) < DeviceSelector(d2);
};
const device &SyclDevice = *std::max_element(Devs.begin(), Devs.end(), Comp);
impl = std::make_shared<detail::queue_impl>(
detail::getSyclObjImpl(SyclDevice), detail::getSyclObjImpl(SyclContext),
AsyncHandler, PropList);
}
queue::queue(const context &SyclContext, const device &SyclDevice,
const async_handler &AsyncHandler, const property_list &PropList) {
impl = std::make_shared<detail::queue_impl>(
detail::getSyclObjImpl(SyclDevice), detail::getSyclObjImpl(SyclContext),
AsyncHandler, PropList);
}
queue::queue(const device &SyclDevice, const async_handler &AsyncHandler,
const property_list &PropList) {
impl = std::make_shared<detail::queue_impl>(
detail::getSyclObjImpl(SyclDevice), AsyncHandler, PropList);
}
queue::queue(const context &SyclContext, const device_selector &deviceSelector,
const property_list &PropList)
: queue(SyclContext, deviceSelector,
detail::getSyclObjImpl(SyclContext)->get_async_handler(),
PropList) {}
queue::queue(const context &SyclContext, const device &SyclDevice,
const property_list &PropList)
: queue(SyclContext, SyclDevice,
detail::getSyclObjImpl(SyclContext)->get_async_handler(),
PropList) {}
queue::queue(cl_command_queue clQueue, const context &SyclContext,
const async_handler &AsyncHandler) {
const property_list PropList{};
impl = std::make_shared<detail::queue_impl>(
reinterpret_cast<sycl::detail::pi::PiQueue>(clQueue),
detail::getSyclObjImpl(SyclContext), AsyncHandler, PropList);
}
cl_command_queue queue::get() const { return impl->get(); }
context queue::get_context() const { return impl->get_context(); }
device queue::get_device() const { return impl->get_device(); }
ext::oneapi::experimental::queue_state queue::ext_oneapi_get_state() const {
return impl->getCommandGraph()
? ext::oneapi::experimental::queue_state::recording
: ext::oneapi::experimental::queue_state::executing;
}
ext::oneapi::experimental::command_graph<
ext::oneapi::experimental::graph_state::modifiable>
queue::ext_oneapi_get_graph() const {
auto Graph = impl->getCommandGraph();
if (!Graph)
throw sycl::exception(
make_error_code(errc::invalid),
"ext_oneapi_get_graph() can only be called on recording queues.");
return sycl::detail::createSyclObjFromImpl<
ext::oneapi::experimental::command_graph<
ext::oneapi::experimental::graph_state::modifiable>>(Graph);
}
bool queue::is_host() const {
bool IsHost = impl->is_host();
assert(!IsHost && "queue::is_host should not be called in implementation.");
return IsHost;
}
void queue::throw_asynchronous() { impl->throw_asynchronous(); }
event queue::memset(void *Ptr, int Value, size_t Count,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memset(impl, Ptr, Value, Count, {});
}
event queue::memset(void *Ptr, int Value, size_t Count, event DepEvent,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memset(impl, Ptr, Value, Count, {DepEvent});
}
event queue::memset(void *Ptr, int Value, size_t Count,
const std::vector<event> &DepEvents,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memset(impl, Ptr, Value, Count, DepEvents);
}
event queue::memcpy(void *Dest, const void *Src, size_t Count,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memcpy(impl, Dest, Src, Count, {}, CodeLoc);
}
event queue::memcpy(void *Dest, const void *Src, size_t Count, event DepEvent,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memcpy(impl, Dest, Src, Count, {DepEvent}, CodeLoc);
}
event queue::memcpy(void *Dest, const void *Src, size_t Count,
const std::vector<event> &DepEvents,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->memcpy(impl, Dest, Src, Count, DepEvents, CodeLoc);
}
event queue::mem_advise(const void *Ptr, size_t Length, pi_mem_advice Advice,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return mem_advise(Ptr, Length, int(Advice));
}
event queue::mem_advise(const void *Ptr, size_t Length, int Advice,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->mem_advise(impl, Ptr, Length, pi_mem_advice(Advice), {});
}
event queue::mem_advise(const void *Ptr, size_t Length, int Advice,
event DepEvent, const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->mem_advise(impl, Ptr, Length, pi_mem_advice(Advice), {DepEvent});
}
event queue::mem_advise(const void *Ptr, size_t Length, int Advice,
const std::vector<event> &DepEvents,
const detail::code_location &CodeLoc) {
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
return impl->mem_advise(impl, Ptr, Length, pi_mem_advice(Advice), DepEvents);
}
event queue::discard_or_return(const event &Event) {
if (!(impl->MDiscardEvents))
return Event;
using detail::event_impl;
auto Impl = std::make_shared<event_impl>(event_impl::HES_Discarded);
return detail::createSyclObjFromImpl<event>(Impl);
}
event queue::submit_impl(std::function<void(handler &)> CGH,
const detail::code_location &CodeLoc) {
return impl->submit(CGH, impl, CodeLoc);
}
event queue::submit_impl(std::function<void(handler &)> CGH, queue SecondQueue,
const detail::code_location &CodeLoc) {
return impl->submit(CGH, impl, SecondQueue.impl, CodeLoc);
}
event queue::submit_impl_and_postprocess(
std::function<void(handler &)> CGH, const detail::code_location &CodeLoc,
const SubmitPostProcessF &PostProcess) {
return impl->submit(CGH, impl, CodeLoc, &PostProcess);
}
event queue::submit_impl_and_postprocess(
std::function<void(handler &)> CGH, queue SecondQueue,
const detail::code_location &CodeLoc,
const SubmitPostProcessF &PostProcess) {
return impl->submit(CGH, impl, SecondQueue.impl, CodeLoc, &PostProcess);
}
void queue::wait_proxy(const detail::code_location &CodeLoc) {
impl->wait(CodeLoc);
}
void queue::wait_and_throw_proxy(const detail::code_location &CodeLoc) {
impl->wait_and_throw(CodeLoc);
}
static event
getBarrierEventForInorderQueueHelper(const detail::QueueImplPtr QueueImpl) {
// The last command recorded in the graph is not tracked by the queue but by
// the graph itself. We must therefore search for the last node/event in the
// graph.
if (auto Graph = QueueImpl->getCommandGraph()) {
auto LastEvent =
Graph->getEventForNode(Graph->getLastInorderNode(QueueImpl));
return sycl::detail::createSyclObjFromImpl<event>(LastEvent);
}
auto LastEvent = QueueImpl->getLastEvent();
if (QueueImpl->MDiscardEvents) {
std::cout << "Discard event enabled" << std::endl;
return LastEvent;
}
auto LastEventImpl = detail::getSyclObjImpl(LastEvent);
// If last event is default constructed event then we want to associate it
// with the queue and record submission time if profiling is enabled. Such
// event corresponds to NOP and its submit time is same as start time and
// end time.
if (!LastEventImpl->isContextInitialized()) {
LastEventImpl->associateWithQueue(QueueImpl);
LastEventImpl->setSubmissionTime();
}
return detail::createSyclObjFromImpl<event>(LastEventImpl);
}
/// Prevents any commands submitted afterward to this queue from executing
/// until all commands previously submitted to this queue have entered the
/// complete state.
///
/// \param CodeLoc is the code location of the submit call (default argument)
/// \return a SYCL event object, which corresponds to the queue the command
/// group is being enqueued on.
event queue::ext_oneapi_submit_barrier(const detail::code_location &CodeLoc) {
if (is_in_order())
return getBarrierEventForInorderQueueHelper(impl);
return submit([=](handler &CGH) { CGH.ext_oneapi_barrier(); }, CodeLoc);
}
/// Prevents any commands submitted afterward to this queue from executing
/// until all events in WaitList have entered the complete state. If WaitList
/// is empty, then ext_oneapi_submit_barrier has no effect.
///
/// \param WaitList is a vector of valid SYCL events that need to complete
/// before barrier command can be executed.
/// \param CodeLoc is the code location of the submit call (default argument)
/// \return a SYCL event object, which corresponds to the queue the command
/// group is being enqueued on.
event queue::ext_oneapi_submit_barrier(const std::vector<event> &WaitList,
const detail::code_location &CodeLoc) {
bool AllEventsEmptyOrNop = std::all_of(
begin(WaitList), end(WaitList), [&](const event &Event) -> bool {
return !detail::getSyclObjImpl(Event)->isContextInitialized() ||
detail::getSyclObjImpl(Event)->isNOP();
});
if (is_in_order() && AllEventsEmptyOrNop)
return getBarrierEventForInorderQueueHelper(impl);
return submit([=](handler &CGH) { CGH.ext_oneapi_barrier(WaitList); },
CodeLoc);
}
template <typename Param>
typename detail::is_queue_info_desc<Param>::return_type
queue::get_info() const {
return impl->get_info<Param>();
}
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
template __SYCL_EXPORT ReturnT queue::get_info<info::queue::Desc>() const;
#include <sycl/info/queue_traits.def>
#undef __SYCL_PARAM_TRAITS_SPEC
template <typename PropertyT> bool queue::has_property() const noexcept {
return impl->has_property<PropertyT>();
}
template <typename PropertyT> PropertyT queue::get_property() const {
return impl->get_property<PropertyT>();
}
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME) \
template __SYCL_EXPORT bool queue::has_property<NS_QUALIFIER::PROP_NAME>() \
const noexcept; \
template __SYCL_EXPORT NS_QUALIFIER::PROP_NAME \
queue::get_property<NS_QUALIFIER::PROP_NAME>() const;
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
__SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
#include <sycl/properties/queue_properties.def>
bool queue::is_in_order() const {
return impl->has_property<property::queue::in_order>();
}
backend queue::get_backend() const noexcept { return getImplBackend(impl); }
bool queue::ext_oneapi_empty() const { return impl->ext_oneapi_empty(); }
pi_native_handle queue::getNative(int32_t &NativeHandleDesc) const {
return impl->getNative(NativeHandleDesc);
}
event queue::memcpyToDeviceGlobal(void *DeviceGlobalPtr, const void *Src,
bool IsDeviceImageScope, size_t NumBytes,
size_t Offset,
const std::vector<event> &DepEvents) {
return impl->memcpyToDeviceGlobal(impl, DeviceGlobalPtr, Src,
IsDeviceImageScope, NumBytes, Offset,
DepEvents);
}
event queue::memcpyFromDeviceGlobal(void *Dest, const void *DeviceGlobalPtr,
bool IsDeviceImageScope, size_t NumBytes,
size_t Offset,
const std::vector<event> &DepEvents) {
return impl->memcpyFromDeviceGlobal(impl, Dest, DeviceGlobalPtr,
IsDeviceImageScope, NumBytes, Offset,
DepEvents);
}
bool queue::device_has(aspect Aspect) const {
// avoid creating sycl object from impl
return impl->getDeviceImplPtr()->has(Aspect);
}
bool queue::ext_codeplay_supports_fusion() const {
return impl->has_property<
ext::codeplay::experimental::property::queue::enable_fusion>();
}
event queue::ext_oneapi_get_last_event() const {
if (!is_in_order())
throw sycl::exception(
make_error_code(errc::invalid),
"ext_oneapi_get_last_event() can only be called on in-order queues.");
if (impl->MDiscardEvents)
throw sycl::exception(
make_error_code(errc::invalid),
"ext_oneapi_get_last_event() cannot be called on queues with the "
"ext::oneapi::property::queue::discard_events property.");
return impl->getLastEvent();
}
void queue::ext_oneapi_set_external_event(const event &external_event) {
if (!is_in_order())
throw sycl::exception(make_error_code(errc::invalid),
"ext_oneapi_set_external_event() can only be called "
"on in-order queues.");
if (impl->MDiscardEvents)
throw sycl::exception(
make_error_code(errc::invalid),
"ext_oneapi_set_external_event() cannot be called on queues with the "
"ext::oneapi::property::queue::discard_events property.");
return impl->setExternalEvent(external_event);
}
} // namespace _V1
} // namespace sycl
size_t std::hash<sycl::queue>::operator()(const sycl::queue &Q) const {
// Compared to using the impl pointer, the unique ID helps avoid hash
// collisions with previously destroyed queues.
return std::hash<unsigned long long>()(
sycl::detail::getSyclObjImpl(Q)->getQueueID());
}