kokkos-execution 0.0.1
Loading...
Searching...
No Matches
events.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_GRAPH_EVENTS_HPP
2#define KOKKOS_EXECUTION_GRAPH_EVENTS_HPP
3
4#include "Kokkos_Core.hpp"
5#include "Kokkos_Graph.hpp"
6
7#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
9#endif
10
12
15 void* graph = nullptr;
16 uint32_t dev_id = 0;
17 uint64_t event_id = 0;
18
19 constexpr auto operator<=>(const GraphCreateEvent&) const = default;
20
21 friend std::ostream& operator<<(std::ostream& out, const GraphCreateEvent& event) {
22 return out << "GraphCreateEvent: {graph = " << event.graph << ", dev_id = " << event.dev_id
23 << ", event_id = " << event.event_id << '}';
24 }
25};
26
34 void* graph = nullptr;
35 void* predecessor = nullptr;
36 void* node = nullptr;
37 uint32_t dev_id = 0;
38
39 constexpr auto operator<=>(const GraphAddNodeEvent&) const = default;
40
41 friend std::ostream& operator<<(std::ostream& out, const GraphAddNodeEvent& event) {
42 return out << "GraphAddNodeEvent: {graph = " << event.graph << ", predecessor = " << event.predecessor
43 << ", node = " << event.node << ", dev_id = " << event.dev_id << '}';
44 }
45};
46
49 void* graph = nullptr;
50
51 constexpr auto operator<=>(const GraphInstantiateEvent&) const = default;
52
53 friend std::ostream& operator<<(std::ostream& out, const GraphInstantiateEvent& event) {
54 return out << "GraphInstantiateEvent: {graph = " << event.graph << '}';
55 }
56};
57
60 void* graph = nullptr;
61 uint32_t dev_id = 0;
62
63 constexpr auto operator<=>(const GraphSubmitEvent&) const = default;
64
65 friend std::ostream& operator<<(std::ostream& out, const GraphSubmitEvent& event) {
66 return out << "GraphSubmitEvent: {graph = " << event.graph << ", dev_id = " << event.dev_id << '}';
67 }
68};
69
71template <typename T>
72concept NodeRef = Kokkos::Impl::is_specialization_of_v<T, Kokkos::Experimental::GraphNodeRef>;
73
75template <NodeRef NodeType>
76auto* get_graph_impl_ptr(const NodeType& node) noexcept {
77 return Kokkos::Impl::GraphAccess::get_graph_weak_ptr(node).lock().get();
78}
79
81template <NodeRef NodeType>
82auto* get_node_ptr(const NodeType& node) noexcept {
83 return Kokkos::Impl::GraphAccess::get_node_ptr(node).get();
84}
85
87template <Kokkos::ExecutionSpace Exec>
88void graph_create_event(const Kokkos::Experimental::Graph<Exec>& graph) {
89#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
92 .graph = get_graph_impl_ptr(graph.root_node()),
93 .dev_id = Kokkos::Tools::Experimental::device_id(graph.get_device_handle().m_exec),
95#endif
96}
97
99template <Kokkos::ExecutionSpace Exec, typename... Args>
100auto create_graph(const Kokkos::Impl::DeviceHandle<Exec>& device_handle, Args&&... args) {
101 Kokkos::Experimental::Graph<Exec> graph{device_handle, std::forward<Args>(args)...};
102 graph_create_event(graph);
103 return graph;
104}
105
111template <NodeRef Predecessor, NodeRef NodeType>
112void graph_add_node_event(const Predecessor& predecessor, const NodeType& node) {
113#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
114 auto* const node_ptr = get_node_ptr(node);
115 auto* const pred_ptr = get_node_ptr(predecessor);
116 auto* const graph_ptr = get_graph_impl_ptr(predecessor);
117
118 // NOLINTBEGIN(bugprone-branch-clone)
119 constexpr bool is_root = []() {
120 using aggregate_t = decltype(graph_ptr->create_aggregate_ptr());
121
122 if constexpr (requires { typename std::remove_cvref_t<decltype(*pred_ptr)>::kernel_type; }) {
123 using kernel_t = typename std::remove_cvref_t<decltype(*pred_ptr)>::kernel_type;
124 if constexpr (Kokkos::Impl::is_graph_kernel_v<kernel_t>) {
125 return false;
126 } else if constexpr (Kokkos::Impl::is_graph_capture_v<kernel_t>) {
127 return false;
128 } else if constexpr (Kokkos::Impl::is_graph_then_host_v<kernel_t>) {
129 return false;
130 }
131 } else if constexpr (std::same_as<Predecessor, aggregate_t>) {
132 return false;
133 }
134 return true;
135 }();
136 // NOLINTEND(bugprone-branch-clone)
137
140 .graph = graph_ptr,
141 .predecessor = is_root ? nullptr : pred_ptr,
142 .node = node_ptr,
143 .dev_id = Kokkos::Tools::Experimental::device_id(node_ptr->get_device_handle().m_exec)});
144#endif
145}
146
148template <Kokkos::ExecutionSpace Exec>
149void graph_instantiate_event(const Kokkos::Experimental::Graph<Exec>& graph) {
150#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
152#endif
153}
154
155
157template <Kokkos::ExecutionSpace Exec>
158void graph_submit_event(const Kokkos::Experimental::Graph<Exec>& graph, const Exec& exec) {
159#if defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
162 .graph = get_graph_impl_ptr(graph.root_node()), .dev_id = Kokkos::Tools::Experimental::device_id(exec)});
163#endif
164}
165
167template <Kokkos::ExecutionSpace Exec>
168void submit_graph(const Kokkos::Experimental::Graph<Exec>& graph, const Exec& exec) {
169 graph_submit_event(graph, exec);
170 graph.submit(exec);
171}
172
173} // namespace Kokkos::Execution::GraphImpl
174
175#endif // KOKKOS_EXECUTION_GRAPH_EVENTS_HPP
Constrain a type that is a specialization of Kokkos::Experimental::GraphNodeRef.
Definition events.hpp:72
void graph_create_event(const Kokkos::Experimental::Graph< Exec > &graph)
Record a GraphCreateEvent event.
Definition events.hpp:88
void graph_add_node_event(const Predecessor &predecessor, const NodeType &node)
Record an event for a node added after predecessor.
Definition events.hpp:112
auto * get_node_ptr(const NodeType &node) noexcept
Retrieve the raw node pointer.
Definition events.hpp:82
auto create_graph(const Kokkos::Impl::DeviceHandle< Exec > &device_handle, Args &&... args)
Create a graph and record the associated event with graph_create_event.
Definition events.hpp:100
void graph_instantiate_event(const Kokkos::Experimental::Graph< Exec > &graph)
Record a GraphInstantiateEvent event.
Definition events.hpp:149
void graph_submit_event(const Kokkos::Experimental::Graph< Exec > &graph, const Exec &exec)
Record a GraphSubmitEvent event.
Definition events.hpp:158
auto * get_graph_impl_ptr(const NodeType &node) noexcept
Retrieve the raw graph pointer from a node.
Definition events.hpp:76
void submit_graph(const Kokkos::Experimental::Graph< Exec > &graph, const Exec &exec)
Submit a graph and record the associated event with graph_submit_event.
Definition events.hpp:168
auto get_next_event_id() noexcept
void dispatch(const EventType &event)
Event to be sent to Kokkos::utils::callbacks::dispatch when a Kokkos graph node is added.
Definition events.hpp:33
constexpr auto operator<=>(const GraphAddNodeEvent &) const =default
friend std::ostream & operator<<(std::ostream &out, const GraphAddNodeEvent &event)
Definition events.hpp:41
Event to be sent to Kokkos::utils::callbacks::dispatch when a Kokkos graph is created.
Definition events.hpp:14
constexpr auto operator<=>(const GraphCreateEvent &) const =default
friend std::ostream & operator<<(std::ostream &out, const GraphCreateEvent &event)
Definition events.hpp:21
Event to be sent to Kokkos::utils::callbacks::dispatch when a Kokkos graph is instantiated.
Definition events.hpp:48
friend std::ostream & operator<<(std::ostream &out, const GraphInstantiateEvent &event)
Definition events.hpp:53
constexpr auto operator<=>(const GraphInstantiateEvent &) const =default
Event to be sent to Kokkos::utils::callbacks::dispatch when a Kokkos graph is submitted.
Definition events.hpp:59
constexpr auto operator<=>(const GraphSubmitEvent &) const =default
friend std::ostream & operator<<(std::ostream &out, const GraphSubmitEvent &event)
Definition events.hpp:65