Skip to content

Commit 9542e28

Browse files
authored
[SYCL] Implement discard_events extension (#5026)
Review documentation in #4922
1 parent 18df116 commit 9542e28

18 files changed

+331
-96
lines changed

sycl/include/CL/sycl/detail/memory_manager.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <CL/sycl/detail/cl.h>
1313
#include <CL/sycl/detail/export.hpp>
1414
#include <CL/sycl/detail/sycl_mem_obj_i.hpp>
15+
#include <CL/sycl/id.hpp>
1516
#include <CL/sycl/property_list.hpp>
1617
#include <CL/sycl/range.hpp>
1718

@@ -140,18 +141,43 @@ class __SYCL_EXPORT MemoryManager {
140141
void *MappedPtr, std::vector<RT::PiEvent> DepEvents,
141142
RT::PiEvent &OutEvent);
142143

144+
static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
145+
void *DstMem, std::vector<RT::PiEvent> DepEvents,
146+
RT::PiEvent *OutEvent);
147+
148+
__SYCL_DEPRECATED("copy_usm() accepting PiEvent& is deprecated, use "
149+
"copy_usm() accepting PiEvent* instead")
143150
static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
144151
void *DstMem, std::vector<RT::PiEvent> DepEvents,
145152
RT::PiEvent &OutEvent);
146153

154+
static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
155+
int Pattern, std::vector<RT::PiEvent> DepEvents,
156+
RT::PiEvent *OutEvent);
157+
158+
__SYCL_DEPRECATED("fill_usm() accepting PiEvent& is deprecated, use "
159+
"fill_usm() accepting PiEvent* instead")
147160
static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
148161
int Pattern, std::vector<RT::PiEvent> DepEvents,
149162
RT::PiEvent &OutEvent);
150163

164+
static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
165+
std::vector<RT::PiEvent> DepEvents,
166+
RT::PiEvent *OutEvent);
167+
168+
__SYCL_DEPRECATED("prefetch_usm() accepting PiEvent& is deprecated, use "
169+
"prefetch_usm() accepting PiEvent* instead")
151170
static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
152171
std::vector<RT::PiEvent> DepEvents,
153172
RT::PiEvent &OutEvent);
154173

174+
static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
175+
pi_mem_advice Advice,
176+
std::vector<RT::PiEvent> DepEvents,
177+
RT::PiEvent *OutEvent);
178+
179+
__SYCL_DEPRECATED("advise_usm() accepting PiEvent& is deprecated, use "
180+
"advise_usm() accepting PiEvent* instead")
155181
static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
156182
pi_mem_advice Advice,
157183
std::vector<RT::PiEvent> DepEvents,

sycl/include/CL/sycl/detail/property_helper.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ enum DataLessPropKind {
3333
UsePrimaryContext = 6,
3434
InitializeToIdentity = 7,
3535
UseDefaultStream = 8,
36+
DiscardEvents = 9,
3637
// Indicates the last known dataless property.
37-
LastKnownDataLessPropKind = 8,
38+
LastKnownDataLessPropKind = 9,
3839
// Exceeding 32 may cause ABI breaking change on some of OSes.
3940
DataLessPropKindSize = 32
4041
};

sycl/include/CL/sycl/feature_test.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace sycl {
3535
#define SYCL_EXT_ONEAPI_MATRIX 2
3636
#endif
3737
#define SYCL_EXT_ONEAPI_ASSERT 1
38+
#define SYCL_EXT_ONEAPI_DISCARD_QUEUE_EVENTS 1
3839
#define SYCL_EXT_ONEAPI_ENQUEUE_BARRIER 1
3940
#define SYCL_EXT_ONEAPI_FREE_FUNCTION_QUERIES 1
4041
#define SYCL_EXT_ONEAPI_GROUP_ALGORITHMS 1

sycl/include/CL/sycl/handler.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,18 +1364,12 @@ class __SYCL_EXPORT handler {
13641364
/// Registers event dependencies on this command group.
13651365
///
13661366
/// \param Event is a valid SYCL event to wait on.
1367-
void depends_on(event Event) {
1368-
MEvents.push_back(detail::getSyclObjImpl(Event));
1369-
}
1367+
void depends_on(event Event);
13701368

13711369
/// Registers event dependencies on this command group.
13721370
///
13731371
/// \param Events is a vector of valid SYCL events to wait on.
1374-
void depends_on(const std::vector<event> &Events) {
1375-
for (const event &Event : Events) {
1376-
MEvents.push_back(detail::getSyclObjImpl(Event));
1377-
}
1378-
}
1372+
void depends_on(const std::vector<event> &Events);
13791373

13801374
template <typename T>
13811375
using remove_cv_ref_t =

sycl/include/CL/sycl/info/info_desc.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ enum class event : cl_event_info {
288288
enum class event_command_status : cl_int {
289289
submitted = CL_SUBMITTED,
290290
running = CL_RUNNING,
291-
complete = CL_COMPLETE
291+
complete = CL_COMPLETE,
292+
// Since all BE values are positive, it is safe to use a negative value If you
293+
// add other ext_oneapi values
294+
ext_oneapi_unknown = -1
292295
};
293296

294297
enum class event_profiling : cl_profiling_info {

sycl/include/CL/sycl/properties/queue_properties.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class enable_profiling
2323

2424
namespace ext {
2525
namespace oneapi {
26+
27+
namespace property {
28+
namespace queue {
29+
class discard_events : public ::cl::sycl::detail::DataLessProperty<
30+
::cl::sycl::detail::DiscardEvents> {};
31+
} // namespace queue
32+
} // namespace property
33+
2634
namespace cuda {
2735
namespace property {
2836
namespace queue {
@@ -52,6 +60,9 @@ template <> struct is_property<property::queue::in_order> : std::true_type {};
5260
template <>
5361
struct is_property<property::queue::enable_profiling> : std::true_type {};
5462
template <>
63+
struct is_property<ext::oneapi::property::queue::discard_events>
64+
: std::true_type {};
65+
template <>
5566
struct is_property<property::queue::cuda::use_default_stream> : std::true_type {
5667
};
5768
template <>
@@ -64,6 +75,9 @@ template <>
6475
struct is_property_of<property::queue::enable_profiling, queue>
6576
: std::true_type {};
6677
template <>
78+
struct is_property_of<ext::oneapi::property::queue::discard_events, queue>
79+
: std::true_type {};
80+
template <>
6781
struct is_property_of<property::queue::cuda::use_default_stream, queue>
6882
: std::true_type {};
6983
template <>

sycl/include/CL/sycl/queue.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ class __SYCL_EXPORT queue {
251251
template <typename T> event submit(T CGF _CODELOCPARAM(&CodeLoc)) {
252252
_CODELOCARG(&CodeLoc);
253253

254-
event Event;
255-
256254
#if __SYCL_USE_FALLBACK_ASSERT
257255
if (!is_host()) {
258256
auto PostProcess = [this, &CodeLoc](bool IsKernel, bool KernelUsesAssert,
@@ -268,14 +266,14 @@ class __SYCL_EXPORT queue {
268266
}
269267
};
270268

271-
Event = submit_impl_and_postprocess(CGF, CodeLoc, PostProcess);
269+
auto Event = submit_impl_and_postprocess(CGF, CodeLoc, PostProcess);
270+
return discard_or_return(Event);
272271
} else
273272
#endif // __SYCL_USE_FALLBACK_ASSERT
274273
{
275-
Event = submit_impl(CGF, CodeLoc);
274+
auto Event = submit_impl(CGF, CodeLoc);
275+
return discard_or_return(Event);
276276
}
277-
278-
return Event;
279277
}
280278

281279
/// Submits a command group function object to the queue, in order to be
@@ -293,8 +291,6 @@ class __SYCL_EXPORT queue {
293291
event submit(T CGF, queue &SecondaryQueue _CODELOCPARAM(&CodeLoc)) {
294292
_CODELOCARG(&CodeLoc);
295293

296-
event Event;
297-
298294
#if __SYCL_USE_FALLBACK_ASSERT
299295
if (!is_host()) {
300296
auto PostProcess = [this, &SecondaryQueue, &CodeLoc](
@@ -315,15 +311,15 @@ class __SYCL_EXPORT queue {
315311
}
316312
};
317313

318-
Event = submit_impl_and_postprocess(CGF, SecondaryQueue, CodeLoc,
319-
PostProcess);
314+
auto Event = submit_impl_and_postprocess(CGF, SecondaryQueue, CodeLoc,
315+
PostProcess);
316+
return discard_or_return(Event);
320317
} else
321318
#endif // __SYCL_USE_FALLBACK_ASSERT
322319
{
323-
Event = submit_impl(CGF, SecondaryQueue, CodeLoc);
320+
auto Event = submit_impl(CGF, SecondaryQueue, CodeLoc);
321+
return discard_or_return(Event);
324322
}
325-
326-
return Event;
327323
}
328324

329325
/// Prevents any commands submitted afterward to this queue from executing
@@ -1089,6 +1085,10 @@ class __SYCL_EXPORT queue {
10891085
event submit_impl(std::function<void(handler &)> CGH, queue secondQueue,
10901086
const detail::code_location &CodeLoc);
10911087

1088+
/// Checks if the event needs to be discarded and if so, discards it and
1089+
/// returns a discarded event. Otherwise, it returns input event.
1090+
event discard_or_return(const event &Event);
1091+
10921092
// Function to postprocess submitted command
10931093
// Arguments:
10941094
// bool IsKernel - true if the submitted command was kernel, false otherwise

sycl/source/detail/event_impl.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ void event_impl::waitInternal() const {
5656
return;
5757
}
5858

59+
if (MState == HES_Discarded)
60+
throw sycl::exception(
61+
make_error_code(errc::invalid),
62+
"waitInternal method cannot be used for a discarded event.");
63+
5964
while (MState != HES_Complete)
6065
;
6166
}
@@ -93,7 +98,8 @@ void event_impl::setContextImpl(const ContextImplPtr &Context) {
9398
MState = HES_NotComplete;
9499
}
95100

96-
event_impl::event_impl() : MIsFlushed(true), MState(HES_Complete) {}
101+
event_impl::event_impl(HostEventState State)
102+
: MIsFlushed(true), MState(State) {}
97103

98104
event_impl::event_impl(RT::PiEvent Event, const context &SyclContext)
99105
: MEvent(Event), MContext(detail::getSyclObjImpl(SyclContext)),
@@ -188,6 +194,10 @@ void event_impl::instrumentationEpilog(void *TelemetryEvent,
188194

189195
void event_impl::wait(
190196
std::shared_ptr<cl::sycl::detail::event_impl> Self) const {
197+
if (MState == HES_Discarded)
198+
throw sycl::exception(make_error_code(errc::invalid),
199+
"wait method cannot be used for a discarded event.");
200+
191201
#ifdef XPTI_ENABLE_INSTRUMENTATION
192202
void *TelemetryEvent = nullptr;
193203
uint64_t IId;
@@ -304,6 +314,9 @@ template <> cl_uint event_impl::get_info<info::event::reference_count>() const {
304314
template <>
305315
info::event_command_status
306316
event_impl::get_info<info::event::command_execution_status>() const {
317+
if (MState == HES_Discarded)
318+
return info::event_command_status::ext_oneapi_unknown;
319+
307320
if (!MHostEvent && MEvent) {
308321
return get_event_info<info::event::command_execution_status>::get(
309322
this->getHandleRef(), this->getPlugin());
@@ -333,6 +346,11 @@ pi_native_handle event_impl::getNative() const {
333346
}
334347

335348
std::vector<EventImplPtr> event_impl::getWaitList() {
349+
if (MState == HES_Discarded)
350+
throw sycl::exception(
351+
make_error_code(errc::invalid),
352+
"get_wait_list() cannot be used for a discarded event.");
353+
336354
std::lock_guard<std::mutex> Lock(MMutex);
337355

338356
std::vector<EventImplPtr> Result;

sycl/source/detail/event_impl.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ using EventImplPtr = std::shared_ptr<cl::sycl::detail::event_impl>;
3232

3333
class event_impl {
3434
public:
35+
enum HostEventState : int {
36+
HES_NotComplete = 0,
37+
HES_Complete,
38+
HES_Discarded
39+
};
40+
3541
/// Constructs a ready SYCL event.
3642
///
3743
/// If the constructed SYCL event is waited on it will complete immediately.
38-
event_impl();
44+
event_impl(HostEventState State = HES_Complete);
3945
/// Constructs an event instance from a plug-in event handle.
4046
///
4147
/// The SyclContext must match the plug-in context associated with the
@@ -190,6 +196,11 @@ class event_impl {
190196
/// Cleans dependencies of this event_impl
191197
void cleanupDependencyEvents();
192198

199+
/// Checks if this event is discarded by SYCL implementation.
200+
///
201+
/// \return true if this event is discarded.
202+
bool isDiscarded() const { return MState == HES_Discarded; }
203+
193204
private:
194205
// When instrumentation is enabled emits trace event for event wait begin and
195206
// returns the telemetry event generated for the wait
@@ -215,8 +226,6 @@ class event_impl {
215226
/// the queue to the device.
216227
std::atomic<bool> MIsFlushed = false;
217228

218-
enum HostEventState : int { HES_NotComplete = 0, HES_Complete };
219-
220229
// State of host event. Employed only for host events and event with no
221230
// backend's representation (e.g. alloca). Used values are listed in
222231
// HostEventState enum.

0 commit comments

Comments
 (0)