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_Macros.hpp"
8#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
9import kokkos.core;
10#else
11# include "Kokkos_Core.hpp"
12#endif
13
14#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
16#endif
17
19
26
28
30template <typename EventType, typename Exec>
31concept event = Kokkos::ExecutionSpace<Exec> && std::default_initializable<EventType> && !std::copyable<EventType>
32 && !std::movable<EventType> && requires(EventType& obj, const Exec& exec) {
33 { obj.record(exec) } -> std::same_as<void>;
34 } && requires(const EventType& obj) {
35 { obj.wait() } -> std::same_as<void>;
36 { obj.m_event_id } -> std::same_as<const uint64_t&>;
37 };
38
40template <Kokkos::ExecutionSpace Exec>
41struct Event;
42
49template <Kokkos::ExecutionSpace Exec>
50struct HasNonBlockingDispatch : std::false_type { };
51
52template <typename Exec>
54
55static constexpr auto invalid_dev_id = Kokkos::Experimental::finite_max_v<uint32_t>;
56static constexpr auto invalid_event_id = Kokkos::Experimental::finite_max_v<uint64_t>;
57
60 uint32_t dev_id = 0;
61 uint64_t event_id = 0;
62
63 constexpr auto operator<=>(const RecordEvent&) const = default;
64
65 friend std::ostream& operator<<(std::ostream& out, const RecordEvent& event) {
66 return out << "RecordEvent: {dev_id = " << event.dev_id << ", event_id = " << event.event_id << '}';
67 }
68};
69
70template <Kokkos::ExecutionSpace Exec>
71void record_event(const Exec& exec, uint64_t& event_id) {
72#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
75 RecordEvent{.dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = event_id});
76#endif
77}
78
80struct WaitEvent {
81 uint32_t dev_id = 0;
82 uint64_t event_id = 0;
83
84 constexpr auto operator<=>(const WaitEvent&) const = default;
85
86 friend std::ostream& operator<<(std::ostream& out, const WaitEvent& event) {
87 return out << "WaitEvent: {dev_id = " << event.dev_id << ", event_id = " << event.event_id << '}';
88 }
89};
90
91template <Kokkos::ExecutionSpace Exec>
93#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
94 Kokkos::utils::callbacks::dispatch(WaitEvent{.dev_id = invalid_dev_id, .event_id = event.m_event_id});
95#endif
96}
97
98template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace ExecFrom>
99void wait_event(const ExecTo& exec, const Event<ExecFrom>& event) {
100#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
102 WaitEvent{.dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = event.m_event_id});
103#endif
104}
105
114template <Kokkos::ExecutionSpace Exec>
115struct Event {
116 static_assert(!has_non_blocking_dispatch<Exec>);
117
118 std::atomic<bool> m_recorded{false};
119
121
122 Event() = default;
123 Event(const Event&) = delete;
124 Event& operator=(const Event&) = delete;
125 Event(Event&&) noexcept = delete;
126 Event& operator=(Event&&) noexcept = delete;
127 ~Event() = default;
128
129 void record(const Exec&) {
130 m_recorded.store(true, std::memory_order_release);
131 }
132
133 void wait() const {
138 while (!m_recorded.load(std::memory_order_acquire)) {
139 }
140 }
141};
142
144template <Kokkos::ExecutionSpace Exec>
145void record(Event<Exec>& event, const Exec& exec) {
146 record_event(exec, event.m_event_id);
147 event.record(exec);
148}
149
150template <Kokkos::ExecutionSpace... Exec>
151void impl_wait(const Event<Exec>&... events) {
152 (events.wait(), ...);
153}
154
156template <Kokkos::ExecutionSpace... Exec>
157void wait(const Event<Exec>&... events) {
158 (wait_event(events), ...);
159 impl_wait(events...);
160}
161
162template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace... ExecFrom>
163void impl_wait(const ExecTo&, const Event<ExecFrom>&... events) {
164 (events.wait(), ...);
165}
166
172template <Kokkos::ExecutionSpace ExecTo, Kokkos::ExecutionSpace... ExecFrom>
173void wait(const ExecTo& exec, const Event<ExecFrom>&... events) {
174 (wait_event(exec, events), ...);
175 impl_wait(exec, events...);
176}
177
178template <Kokkos::ExecutionSpace Exec>
179using event_storage_t = std::optional<Event<Exec>>;
180
182template <Kokkos::ExecutionSpace Exec>
184
185} // namespace Kokkos::Execution::Impl
186
187#if defined(KOKKOS_ENABLE_CUDA)
189#endif
190#if defined(KOKKOS_ENABLE_HIP)
192#endif
193#if defined(KOKKOS_ENABLE_HPX)
195#endif
196#if defined(KOKKOS_ENABLE_SYCL)
198#endif
199
200#endif // KOKKOS_EXECUTION_IMPL_EVENT_HPP
Constrain an EventType type to be a valid event type for Exec execution space type.
Definition event.hpp:31
static constexpr auto invalid_event_id
Definition event.hpp:56
OptionalRef< const Event< Exec > > OptionalConstEventRef
Optionally stores a reference to a const Impl::Event.
Definition event.hpp:183
std::optional< Event< Exec > > event_storage_t
Definition event.hpp:179
static constexpr auto invalid_dev_id
Definition event.hpp:55
void record_event(const Exec &exec, uint64_t &event_id)
Definition event.hpp:71
void record(Event< Exec > &event, const Exec &exec)
Record event on exec.
Definition event.hpp:145
void wait(const Event< Exec > &... events)
Wait for events to complete.
Definition event.hpp:157
void wait_event(const Event< Exec > &event)
Definition event.hpp:92
void impl_wait(const Kokkos::Cuda &exec, const Event< ExecFrom > &... events)
Definition event.hpp:60
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:115
std::atomic< bool > m_recorded
Definition event.hpp:118
Event & operator=(const Event &)=delete
Event(Event &&) noexcept=delete
void record(const Exec &)
Definition event.hpp:129
Event(const Event &)=delete
Determine if the Kokkos backend has non-blocking dispatch.
Definition event.hpp:50
A non-owning, nullable reference to a T.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Definition event.hpp:59
friend std::ostream & operator<<(std::ostream &out, const RecordEvent &event)
Definition event.hpp:65
constexpr auto operator<=>(const RecordEvent &) const =default
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.
Definition event.hpp:80
friend std::ostream & operator<<(std::ostream &out, const WaitEvent &event)
Definition event.hpp:86
constexpr auto operator<=>(const WaitEvent &) const =default