okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
7.1 KiB195 linesraw
1//===----- graph.hpp ----------------------------*- C++ -*-----------------===//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <sycl/ext/oneapi/experimental/graph.hpp>
12#include <sycl/sycl.hpp>
13#include <unordered_map>
14
15namespace dpct {
16namespace experimental {
17
18typedef sycl::ext::oneapi::experimental::command_graph<
19    sycl::ext::oneapi::experimental::graph_state::modifiable>
20    *command_graph_ptr;
21
22typedef sycl::ext::oneapi::experimental::command_graph<
23    sycl::ext::oneapi::experimental::graph_state::executable>
24    *command_graph_exec_ptr;
25
26typedef sycl::ext::oneapi::experimental::node *node_ptr;
27
28namespace detail {
29class graph_mgr {
30public:
31  graph_mgr() = default;
32  graph_mgr(const graph_mgr &) = delete;
33  graph_mgr &operator=(const graph_mgr &) = delete;
34  graph_mgr(graph_mgr &&) = delete;
35  graph_mgr &operator=(graph_mgr &&) = delete;
36
37  static graph_mgr &instance() {
38    static graph_mgr instance;
39    return instance;
40  }
41
42  void begin_recording(sycl::queue *queue_ptr) {
43    // Calling begin_recording on an already recording queue is a no-op in SYCL
44    if (queue_graph_map.find(queue_ptr) != queue_graph_map.end()) {
45      return;
46    }
47    auto graph = new sycl::ext::oneapi::experimental::command_graph<
48        sycl::ext::oneapi::experimental::graph_state::modifiable>(
49        queue_ptr->get_context(), queue_ptr->get_device());
50    auto result = queue_graph_map.insert({queue_ptr, graph});
51    if (!result.second) {
52      delete graph;
53      return;
54    }
55    graph->begin_recording(*queue_ptr);
56  }
57
58  void end_recording(sycl::queue *queue_ptr,
59                     dpct::experimental::command_graph_ptr *graph) {
60    auto it = queue_graph_map.find(queue_ptr);
61    if (it == queue_graph_map.end()) {
62      return;
63    }
64    *graph = it->second;
65    queue_graph_map.erase(it);
66    (*graph)->end_recording();
67  }
68
69  void get_nodes(dpct::experimental::command_graph_ptr graph,
70                 dpct::experimental::node_ptr *nodesArray,
71                 std::size_t *numberOfNodes) {
72    auto nodes = graph->get_nodes();
73    nodes_map[graph] = nodes;
74    *numberOfNodes = nodes.size();
75    if (!nodesArray) {
76      return;
77    }
78    for (std::size_t i = 0; i < *numberOfNodes; i++) {
79      nodesArray[i] = &nodes_map[graph][i];
80    }
81  }
82
83  void get_root_nodes(dpct::experimental::command_graph_ptr graph,
84                      dpct::experimental::node_ptr *nodesArray,
85                      std::size_t *numberOfNodes) {
86    auto root_nodes = graph->get_root_nodes();
87    root_nodes_map[graph] = root_nodes;
88    *numberOfNodes = root_nodes.size();
89    if (!nodesArray) {
90      return;
91    }
92    for (std::size_t i = 0; i < *numberOfNodes; i++) {
93      nodesArray[i] = &root_nodes_map[graph][i];
94    }
95  }
96
97private:
98  std::unordered_map<sycl::queue *, command_graph_ptr> queue_graph_map;
99  std::unordered_map<dpct::experimental::command_graph_ptr,
100                     std::vector<sycl::ext::oneapi::experimental::node>>
101      nodes_map;
102  std::unordered_map<dpct::experimental::command_graph_ptr,
103                     std::vector<sycl::ext::oneapi::experimental::node>>
104      root_nodes_map;
105};
106} // namespace detail
107
108/// Begins recording commands into a command graph for a specific SYCL queue.
109/// \param [in] queue_ptr A pointer to the SYCL queue on which the commands
110/// will be recorded.
111static inline void begin_recording(sycl::queue *queue_ptr) {
112  detail::graph_mgr::instance().begin_recording(queue_ptr);
113}
114
115/// Ends the recording of commands into a command graph for a specific SYCL
116/// queue.
117/// \param [in] queue_ptr A pointer to the SYCL queue on which the commands
118/// were recorded.
119/// \param [out] graph A pointer to a command_graph_ptr pointer where the
120/// command graph will be assigned.
121static inline void end_recording(sycl::queue *queue_ptr,
122                                 dpct::experimental::command_graph_ptr *graph) {
123  detail::graph_mgr::instance().end_recording(queue_ptr, graph);
124}
125
126/// Adds an empty node to the command graph with optional
127/// dependencies.
128/// \param [out] newNode A pointer to the node_ptr that will be
129/// added to the graph.
130/// \param [in] graph A pointer to the command graph.
131/// \param [in] dependenciesArray An array of node pointers
132/// representing the dependencies of the new node.
133/// \param [in] numberOfDependencies The number of dependencies in
134/// the dependenciesArray.
135static void
136add_empty_node(dpct::experimental::node_ptr *newNode,
137               dpct::experimental::command_graph_ptr graph,
138               const dpct::experimental::node_ptr *dependenciesArray,
139               std::size_t numberOfDependencies) {
140  if (numberOfDependencies == 0) {
141    *newNode = new sycl::ext::oneapi::experimental::node(graph->add());
142    return;
143  }
144  std::vector<sycl::ext::oneapi::experimental::node> dependencies;
145  for (std::size_t i = 0; i < numberOfDependencies; i++) {
146    dependencies.push_back(*dependenciesArray[i]);
147  }
148  *newNode =
149      new sycl::ext::oneapi::experimental::node(graph->add(sycl::property_list{
150          sycl::ext::oneapi::experimental::property::node::depends_on(
151              dependencies)}));
152}
153
154/// Adds dependencies between nodes in the command graph.
155/// \param [in] graph A pointer to the command graph.
156/// \param [in] fromNodes An array of node pointers representing
157/// the source nodes.
158/// \param [in] toNodes An array of node pointers representing
159/// the destination nodes.
160/// \param [in] numberOfDependencies The number of dependencies
161/// to be added.
162static void add_dependencies(dpct::experimental::command_graph_ptr graph,
163                             const dpct::experimental::node_ptr *fromNodes,
164                             const dpct::experimental::node_ptr *toNodes,
165                             std::size_t numberOfDependencies) {
166  for (std::size_t i = 0; i < numberOfDependencies; i++) {
167    graph->make_edge(*fromNodes[i], *toNodes[i]);
168  }
169}
170
171/// Gets the nodes in the command graph.
172/// \param [in] graph A pointer to the command graph.
173/// \param [out] nodesArray An array of node pointers where the
174/// nodes will be assigned.
175/// \param [out] numberOfNodes The number of nodes in the graph.
176static void get_nodes(dpct::experimental::command_graph_ptr graph,
177                      dpct::experimental::node_ptr *nodesArray,
178                      std::size_t *numberOfNodes) {
179  detail::graph_mgr::instance().get_nodes(graph, nodesArray, numberOfNodes);
180}
181
182/// Gets the root nodes in the command graph.
183/// \param [in] graph A pointer to the command graph.
184/// \param [out] nodesArray An array of node pointers where the
185/// root nodes will be assigned.
186/// \param [out] numberOfNodes The number of root nodes in the graph.
187static void get_root_nodes(dpct::experimental::command_graph_ptr graph,
188                           dpct::experimental::node_ptr *nodesArray,
189                           std::size_t *numberOfNodes) {
190  detail::graph_mgr::instance().get_root_nodes(graph, nodesArray,
191                                               numberOfNodes);
192}
193
194} // namespace experimental
195} // namespace dpct