kokkos-execution 0.0.1
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_IMPL_EVENT_HPP
2#define KOKKOS_EXECUTION_IMPL_EVENT_HPP
3
4#include <concepts>
5#include <optional>
6
7#include "Kokkos_Core.hpp"
8
9#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
11#endif
12
14
21
23
25template <typename EventType, typename Exec>
26concept event = Kokkos::ExecutionSpace<Exec> && std::default_initializable<EventType> && !std::copyable<EventType>
27 && !std::movable<EventType> && requires(EventType& obj, const Exec& exec) {
28 { obj.record(exec) } -> std::same_as<void>;
29 } && requires(const EventType& obj) {
30 { obj.wait() } -> std::same_as<void>;
31 { obj.m_event_id } -> std::same_as<const uint64_t&>;
32 };
33
35template <Kokkos::ExecutionSpace Exec>
36struct Event;
37
44template <Kokkos::ExecutionSpace Exec>
45struct HasNonBlockingDispatch : std::false_type { };
46
47template <typename Exec>
49
50static constexpr auto invalid_dev_id = Kokkos::Experimental::finite_max_v<uint32_t>;
51static constexpr auto invalid_event_id = Kokkos::Experimental::finite_max_v<uint64_t>;
52
55 uint32_t dev_id = 0;
56 uint64_t event_id = 0;
57
58 constexpr auto operator<=>(const RecordEvent&) const = default;
59
60 friend std::ostream& operator<<(std::ostream& out, const RecordEvent& event) {
61 return out << "RecordEvent: {dev_id = " << event.dev_id << ", event_id = " << event.event_id << '}';
62 }
63};
64
65template <Kokkos::ExecutionSpace Exec>
66void record_event(const Exec& exec, uint64_t& event_id) {
67#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
70 RecordEvent{.dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = event_id});
71#endif
72}
73
75struct WaitEvent {
76 uint32_t dev_id = 0;
77 uint64_t event_id = 0;
78
79 constexpr auto operator<=>(const WaitEvent&) const = default;
80
81 friend std::ostream& operator<<(std::ostream& out, const WaitEvent& event) {
82 return out << "WaitEvent: {dev_id = " << event.dev_id << ", event_id = " << event.event_id << '}';
83 }
84};
85
86template <Kokkos::ExecutionSpace Exec>
88#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
89 Kokkos::utils::callbacks::dispatch(WaitEvent{.dev_id = invalid_dev_id, .event_id = event.m_event_id});
90#endif
91}
92
93template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace ExecFrom>
94void wait_event(const ExecTo& exec, const Event<ExecFrom>& event) {
95#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
97 WaitEvent{.dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = event.m_event_id});
98#endif
99}
100
109template <Kokkos::ExecutionSpace Exec>
110struct Event {
111 static_assert(!has_non_blocking_dispatch<Exec>);
112
113 std::atomic<bool> m_recorded{false};
114
116
117 Event() = default;
118 Event(const Event&) = delete;
119 Event& operator=(const Event&) = delete;
120 Event(Event&&) noexcept = delete;
121 Event& operator=(Event&&) noexcept = delete;
122 ~Event() = default;
123
124 void record(const Exec&) {
125 m_recorded.store(true, std::memory_order_release);
126 }
127
128 void wait() const {
133 while (!m_recorded.load(std::memory_order_acquire)) {
134 }
135 }
136};
137
139template <Kokkos::ExecutionSpace Exec>
140void record(Event<Exec>& event, const Exec& exec) {
141 record_event(exec, event.m_event_id);
142 event.record(exec);
143}
144
145template <Kokkos::ExecutionSpace... Exec>
146void impl_wait(const Event<Exec>&... events) {
147 (events.wait(), ...);
148}
149
151template <Kokkos::ExecutionSpace... Exec>
152void wait(const Event<Exec>&... events) {
153 (wait_event(events), ...);
154 impl_wait(events...);
155}
156
157template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace... ExecFrom>
158void impl_wait(const ExecTo&, const Event<ExecFrom>&... events) {
159 (events.wait(), ...);
160}
161
167template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace... ExecFrom>
168void wait(const ExecTo& exec, const Event<ExecFrom>&... events) {
169 (wait_event(exec, events), ...);
170 impl_wait(exec, events...);
171}
172
173template <Kokkos::ExecutionSpace Exec>
174using event_storage_t = std::optional<Event<Exec>>;
175
177template <Kokkos::ExecutionSpace Exec>
179
180} // namespace Kokkos::Execution::Impl
181
182#if defined(KOKKOS_ENABLE_CUDA)
184#endif
185#if defined(KOKKOS_ENABLE_HIP)
187#endif
188#if defined(KOKKOS_ENABLE_HPX)
190#endif
191#if defined(KOKKOS_ENABLE_SYCL)
193#endif
194
195#endif // KOKKOS_EXECUTION_IMPL_EVENT_HPP
Constrain an EventType type to be a valid event type for Exec execution space type.
Definition event.hpp:26
static constexpr auto invalid_event_id
Definition event.hpp:51
OptionalRef< const Event< Exec > > OptionalConstEventRef
Optionally stores a reference to a const Impl::Event.
Definition event.hpp:178
std::optional< Event< Exec > > event_storage_t
Definition event.hpp:174
static constexpr auto invalid_dev_id
Definition event.hpp:50
void record_event(const Exec &exec, uint64_t &event_id)
Definition event.hpp:66
void record(Event< Exec > &event, const Exec &exec)
Record event on exec.
Definition event.hpp:140
void wait(const Event< Exec > &... events)
Wait for events to complete.
Definition event.hpp:152
void wait_event(const Event< Exec > &event)
Definition event.hpp:87
void impl_wait(const Kokkos::Cuda &exec, const Event< ExecFrom > &... events)
Definition event.hpp:54
auto get_next_event_id() noexcept
void dispatch(const EventType &event)
An event that can be recorded on an execution space instance.
Definition event.hpp:110
std::atomic< bool > m_recorded
Definition event.hpp:113
Event & operator=(const Event &)=delete
Event(Event &&) noexcept=delete
void record(const Exec &)
Definition event.hpp:124
Event(const Event &)=delete
Determine if the Kokkos backend has non-blocking dispatch.
Definition event.hpp:45
A non-owning, nullable reference to a T.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Definition event.hpp:54
friend std::ostream & operator<<(std::ostream &out, const RecordEvent &event)
Definition event.hpp:60
constexpr auto operator<=>(const RecordEvent &) const =default
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.
Definition event.hpp:75
friend std::ostream & operator<<(std::ostream &out, const WaitEvent &event)
Definition event.hpp:81
constexpr auto operator<=>(const WaitEvent &) const =default