okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 : 31graph_mgr ()= default ; 32graph_mgr (const graph_mgr & )= delete ; 33graph_mgr & operator= (const graph_mgr & )= delete ; 34graph_mgr (graph_mgr && )= delete ; 35graph_mgr & operator= (graph_mgr && )= delete ; 36 37static graph_mgr & instance () { 38static graph_mgr instance ; 39return instance ; 40 } 41 42void begin_recording (sycl::queue * queue_ptr ) { 43// Calling begin_recording on an already recording queue is a no-op in SYCL 44if (queue_graph_map .find (queue_ptr )!= queue_graph_map .end ()) { 45return ; 46 } 47auto graph = new sycl::ext::oneapi::experimental::command_graph < 48 sycl::ext::oneapi::experimental::graph_state::modifiable > ( 49queue_ptr -> get_context (),queue_ptr -> get_device ()); 50auto result = queue_graph_map .insert ({queue_ptr ,graph }); 51if (!result .second ) { 52delete graph ; 53return ; 54 } 55graph -> begin_recording (* queue_ptr ); 56 } 57 58void end_recording (sycl::queue * queue_ptr , 59 dpct::experimental::command_graph_ptr * graph ) { 60auto it = queue_graph_map .find (queue_ptr ); 61if (it == queue_graph_map .end ()) { 62return ; 63 } 64* graph = it -> second ; 65queue_graph_map .erase (it ); 66 (* graph )-> end_recording (); 67 } 68 69void get_nodes (dpct::experimental::command_graph_ptr graph , 70 dpct::experimental::node_ptr * nodesArray , 71 std::size_t * numberOfNodes ) { 72auto nodes = graph -> get_nodes (); 73nodes_map [graph ]= nodes ; 74* numberOfNodes = nodes .size (); 75if (!nodesArray ) { 76return ; 77 } 78for (std::size_t i = 0 ;i < * numberOfNodes ;i ++ ) { 79nodesArray [i ]= & nodes_map [graph ][i ]; 80 } 81 } 82 83void get_root_nodes (dpct::experimental::command_graph_ptr graph , 84 dpct::experimental::node_ptr * nodesArray , 85 std::size_t * numberOfNodes ) { 86auto root_nodes = graph -> get_root_nodes (); 87root_nodes_map [graph ]= root_nodes ; 88* numberOfNodes = root_nodes .size (); 89if (!nodesArray ) { 90return ; 91 } 92for (std::size_t i = 0 ;i < * numberOfNodes ;i ++ ) { 93nodesArray [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 >> 101nodes_map ; 102 std::unordered_map < dpct::experimental::command_graph_ptr , 103 std::vector < sycl::ext::oneapi::experimental::node >> 104root_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 , 138const dpct::experimental::node_ptr * dependenciesArray , 139 std::size_t numberOfDependencies ) { 140if (numberOfDependencies == 0 ) { 141* newNode = new sycl::ext::oneapi::experimental::node (graph -> add ()); 142return ; 143 } 144 std::vector < sycl::ext::oneapi::experimental::node > dependencies ; 145for (std::size_t i = 0 ;i < numberOfDependencies ;i ++ ) { 146dependencies .push_back (* dependenciesArray [i ]); 147 } 148* newNode = 149new sycl::ext::oneapi::experimental::node (graph -> add (sycl::property_list { 150 sycl::ext::oneapi::experimental::property::node::depends_on ( 151dependencies )})); 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 , 163const dpct::experimental::node_ptr * fromNodes , 164const dpct::experimental::node_ptr * toNodes , 165 std::size_t numberOfDependencies ) { 166for (std::size_t i = 0 ;i < numberOfDependencies ;i ++ ) { 167graph -> 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 , 191numberOfNodes ); 192} 193 194}// namespace experimental 195}// namespace dpct