Skip to content

Commit e345169

Browse files
Alexander Batashevbader
authored andcommitted
[SYCL] Refactor event to improve ABI stability (#907)
This patch is a part of effort to decouple SYCL Runtime library interface from its actual implementation. The goal is to improve SYCL ABI/API compatibility between different versions of library. The following modifications were applied to platform and platform_impl classes: 1. Removed include of event_impl header from event.hpp and replaced it with forward declaration. 2. std::shared_ptr was replaced with shared_ptr_class for platform class 3. Documentation was improved for both eventand event_impl 4. Both classes now try to follow LLVM code style 5. event_impl now uses PI interface. Applying aforementioned changes requires modifying other header files. While some of those may seem as a regression, affected classes will be covered by future patches. Signed-off-by: Alexander Batashev <[email protected]>
1 parent eb49c87 commit e345169

File tree

9 files changed

+228
-103
lines changed

9 files changed

+228
-103
lines changed

sycl/include/CL/sycl/detail/event_impl.hpp

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,75 +24,148 @@ using ContextImplPtr = std::shared_ptr<cl::sycl::detail::context_impl>;
2424
class queue_impl;
2525
using QueueImplPtr = std::shared_ptr<cl::sycl::detail::queue_impl>;
2626

27-
// Profiling info for the host execution.
27+
/// Profiling info for the host execution.
2828
class HostProfilingInfo {
2929
cl_ulong StartTime = 0;
3030
cl_ulong EndTime = 0;
3131

3232
public:
33+
/// Returns event's start time.
34+
///
35+
/// @return event's start time in nanoseconds.
3336
cl_ulong getStartTime() const { return StartTime; }
37+
/// Returns event's end time.
38+
///
39+
/// @return event's end time in nanoseconds.
3440
cl_ulong getEndTime() const { return EndTime; }
3541

42+
/// Measures event's start time.
3643
void start();
44+
/// Measures event's end time.
3745
void end();
3846
};
3947

4048
class event_impl {
4149
public:
50+
/// Constructs a ready SYCL event.
51+
///
52+
/// If the constructed SYCL event is waited on it will complete immediately.
4253
event_impl() = default;
43-
event_impl(cl_event CLEvent, const context &SyclContext);
54+
/// Constructs an event instance from a plug-in event handle.
55+
///
56+
/// The SyclContext must match the plug-in context associated with the ClEvent.
57+
///
58+
/// @param Event is a valid instance of plug-in event.
59+
/// @param SyclContext is an instance of SYCL context.
60+
event_impl(RT::PiEvent Event, const context &SyclContext);
4461
event_impl(QueueImplPtr Queue);
4562

46-
// Threat all devices that don't support interoperability as host devices to
47-
// avoid attempts to call method get on such events.
63+
/// Checks if this event is a SYCL host event.
64+
///
65+
/// All devices that do not support OpenCL interoperability are treated as
66+
/// host device to avoid attempts to call method get on such events.
67+
//
68+
/// @return true if this event is a SYCL host event.
4869
bool is_host() const;
4970

71+
/// Returns a valid OpenCL event interoperability handle.
72+
///
73+
/// @return a valid instance of OpenCL cl_event.
5074
cl_event get() const;
5175

52-
// Self is needed in order to pass shared_ptr to Scheduler.
76+
/// Waits for the event.
77+
///
78+
/// Self is needed in order to pass shared_ptr to Scheduler.
79+
///
80+
/// @param Self is a pointer to this event.
5381
void wait(std::shared_ptr<cl::sycl::detail::event_impl> Self) const;
5482

83+
/// Waits for the event.
84+
///
85+
/// If any uncaught asynchronous errors occurred on the context that the event
86+
/// is waiting on executions from, then call that context's asynchronous error
87+
/// handler with those errors.
88+
/// Self is needed in order to pass shared_ptr to Scheduler.
89+
///
90+
/// @param Self is a pointer to this event.
5591
void wait_and_throw(std::shared_ptr<cl::sycl::detail::event_impl> Self);
5692

93+
/// Queries this event for profiling information.
94+
///
95+
/// If the requested info is not available when this member function is called
96+
/// due to incompletion of command groups associated with the event, then the
97+
/// call to this member function will block until the requested info is
98+
/// available. If the queue which submitted the command group this event is
99+
/// associated with was not constructed with the
100+
/// property::queue::enable_profiling property, an invalid_object_error SYCL
101+
/// exception is thrown.
102+
///
103+
/// @return depends on template parameter.
57104
template <info::event_profiling param>
58105
typename info::param_traits<info::event_profiling, param>::return_type
59106
get_profiling_info() const;
60107

108+
/// Queries this SYCL event for information.
109+
///
110+
/// @return depends on the information being requested.
61111
template <info::event param>
62112
typename info::param_traits<info::event, param>::return_type get_info() const;
63113

64114
~event_impl();
65115

116+
/// Waits for the event with respect to device type.
66117
void waitInternal() const;
67118

119+
/// Marks this event as completed.
68120
void setComplete();
69121

70-
// Warning. Returned reference will be invalid if event_impl was destroyed.
122+
/// Returns raw interoperability event handle. Returned reference will be]
123+
/// invalid if event_impl was destroyed.
124+
///
125+
/// @return a reference to an instance of plug-in event handle.
71126
RT::PiEvent &getHandleRef();
127+
/// Returns raw interoperability event handle. Returned reference will be]
128+
/// invalid if event_impl was destroyed.
129+
///
130+
/// @return a const reference to an instance of plug-in event handle.
72131
const RT::PiEvent &getHandleRef() const;
73132

133+
/// Returns context that is associated with this event.
134+
///
135+
/// @return a shared pointer to a valid context_impl.
74136
const ContextImplPtr &getContextImpl();
75137

76-
// Warning. Provided cl_context inside ContextImplPtr must be associated
77-
// with the cl_event object stored in this class
138+
/// Associate event with the context.
139+
///
140+
/// Provided PiContext inside ContextImplPtr must be associated
141+
/// with the PiEvent object stored in this class
142+
///
143+
/// @param Context is a shared pointer to an instance of valid context_impl.
78144
void setContextImpl(const ContextImplPtr &Context);
79145

80-
void *getCommand() { return m_Command; }
146+
/// Returns command that is associated with the event.
147+
///
148+
/// @return a generic pointer to Command object instance.
149+
void *getCommand() { return MCommand; }
81150

82-
void setCommand(void *Command) { m_Command = Command; }
151+
/// Associates this event with the command.
152+
///
153+
/// @param Command is a generic pointer to Command object instance.
154+
void setCommand(void *Command) { MCommand = Command; }
83155

84-
HostProfilingInfo *getHostProfilingInfo() {
85-
return m_HostProfilingInfo.get();
86-
}
156+
/// Returns host profiling information.
157+
///
158+
/// @return a pointer to HostProfilingInfo instance.
159+
HostProfilingInfo *getHostProfilingInfo() { return MHostProfilingInfo.get(); }
87160

88161
private:
89-
RT::PiEvent m_Event = nullptr;
90-
ContextImplPtr m_Context;
91-
QueueImplPtr m_Queue;
92-
bool m_OpenCLInterop = false;
93-
bool m_HostEvent = true;
94-
std::unique_ptr<HostProfilingInfo> m_HostProfilingInfo;
95-
void *m_Command = nullptr;
162+
RT::PiEvent MEvent = nullptr;
163+
ContextImplPtr MContext;
164+
QueueImplPtr MQueue;
165+
bool MOpenCLInterop = false;
166+
bool MHostEvent = true;
167+
std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;
168+
void *MCommand = nullptr;
96169
};
97170

98171
} // namespace detail

sycl/include/CL/sycl/detail/queue_impl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <CL/sycl/context.hpp>
1212
#include <CL/sycl/detail/context_impl.hpp>
1313
#include <CL/sycl/detail/device_impl.hpp>
14+
#include <CL/sycl/detail/event_impl.hpp>
1415
#include <CL/sycl/detail/scheduler/scheduler.hpp>
1516
#include <CL/sycl/device.hpp>
1617
#include <CL/sycl/event.hpp>

sycl/include/CL/sycl/event.hpp

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#pragma once
1010

1111
#include <CL/sycl/detail/common.hpp>
12-
#include <CL/sycl/detail/event_impl.hpp>
1312
#include <CL/sycl/stl.hpp>
1413

1514
#include <memory>
@@ -18,11 +17,24 @@ namespace cl {
1817
namespace sycl {
1918
// Forward declaration
2019
class context;
20+
namespace detail {
21+
class event_impl;
22+
}
23+
2124
class event {
2225
public:
26+
/// Constructs a ready SYCL event.
27+
///
28+
/// If the constructed SYCL event is waited on it will complete immediately.
2329
event();
2430

25-
event(cl_event clEvent, const context &syclContext);
31+
/// Constructs a SYCL event instance from an OpenCL cl_event.
32+
///
33+
/// The SyclContext must match the OpenCL context associated with the ClEvent.
34+
///
35+
/// @param ClEvent is a valid instance of OpenCL cl_event.
36+
/// @param SyclContext is an instance of SYCL context.
37+
event(cl_event ClEvent, const context &SyclContext);
2638

2739
event(const event &rhs) = default;
2840

@@ -36,31 +48,73 @@ class event {
3648

3749
bool operator!=(const event &rhs) const;
3850

51+
/// Returns a valid OpenCL event interoperability handle.
52+
///
53+
/// @return a valid instance of OpenCL cl_event.
3954
cl_event get();
4055

56+
/// Checks if this event is a SYCL host event.
57+
///
58+
/// @return true if this event is a SYCL host event.
4159
bool is_host() const;
4260

61+
/// Return the list of events that this event waits for.
62+
///
63+
/// Only direct dependencies are returned. Already completed events are not
64+
/// included in the returned vector.
65+
///
66+
/// @return a vector of SYCL events.
4367
vector_class<event> get_wait_list();
4468

69+
/// Wait for the event.
4570
void wait();
4671

72+
/// Synchronously wait on a list of events.
73+
///
74+
/// @param EventList is a vector of SYCL events.
4775
static void wait(const vector_class<event> &EventList);
4876

77+
/// Wait for the event.
78+
///
79+
/// If any uncaught asynchronous errors occurred on the context that the event
80+
/// is waiting on executions from, then call that context's asynchronous error
81+
/// handler with those errors.
4982
void wait_and_throw();
5083

84+
/// Synchronously wait on a list of events.
85+
///
86+
/// If any uncaught asynchronous errors occurred on the context that the
87+
/// events are waiting on executions from, then call those contexts'
88+
/// asynchronous error handlers with those errors.
89+
///
90+
/// @param EventList is a vector of SYCL events.
5191
static void wait_and_throw(const vector_class<event> &EventList);
5292

93+
/// Queries this SYCL event for information.
94+
///
95+
/// @return depends on the information being requested.
5396
template <info::event param>
5497
typename info::param_traits<info::event, param>::return_type get_info() const;
5598

99+
/// Queries this SYCL event for profiling information.
100+
///
101+
/// If the requested info is not available when this member function is called
102+
/// due to incompletion of command groups associated with the event, then the
103+
/// call to this member function will block until the requested info is
104+
/// available. If the queue which submitted the command group this event is
105+
/// associated with was not constructed with the
106+
/// property::queue::enable_profiling property, an invalid_object_error SYCL
107+
/// exception is thrown.
108+
///
109+
/// @return depends on template parameter.
56110
template <info::event_profiling param>
57111
typename info::param_traits<info::event_profiling, param>::return_type
58112
get_profiling_info() const;
59113

60114
private:
61-
event(std::shared_ptr<detail::event_impl> event_impl);
115+
event(shared_ptr_class<detail::event_impl> EventImpl);
62116

63-
std::shared_ptr<detail::event_impl> impl;
117+
shared_ptr_class<detail::event_impl> impl;
64118

65119
template <class Obj>
66120
friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
@@ -75,7 +129,7 @@ class event {
75129
namespace std {
76130
template <> struct hash<cl::sycl::event> {
77131
size_t operator()(const cl::sycl::event &e) const {
78-
return hash<std::shared_ptr<cl::sycl::detail::event_impl>>()(
132+
return hash<cl::sycl::shared_ptr_class<cl::sycl::detail::event_impl>>()(
79133
cl::sycl::detail::getSyclObjImpl(e));
80134
}
81135
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PARAM_TRAITS_SPEC(event_profiling, command_submit, cl_ulong)
2+
PARAM_TRAITS_SPEC(event_profiling, command_start, cl_ulong)
3+
PARAM_TRAITS_SPEC(event_profiling, command_end, cl_ulong)
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PARAM_TRAITS_SPEC(event, command_execution_status, info::event_command_status)
2+
PARAM_TRAITS_SPEC(event, reference_count, cl_uint)
3+

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,9 @@ template <typename T, T param> class param_traits {};
258258

259259
#include <CL/sycl/info/context_traits.def>
260260

261-
PARAM_TRAITS_SPEC(event, command_execution_status, event_command_status)
262-
PARAM_TRAITS_SPEC(event, reference_count, cl_uint)
261+
#include <CL/sycl/info/event_traits.def>
263262

264-
PARAM_TRAITS_SPEC(event_profiling, command_submit, cl_ulong)
265-
PARAM_TRAITS_SPEC(event_profiling, command_start, cl_ulong)
266-
PARAM_TRAITS_SPEC(event_profiling, command_end, cl_ulong)
263+
#include <CL/sycl/info/event_profiling_traits.def>
267264

268265
#include <CL/sycl/info/kernel_sub_group_traits.def>
269266
#include <CL/sycl/info/kernel_traits.def>

0 commit comments

Comments
 (0)