Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Graph.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
17 #ifndef KOKKOS_GRAPH_HPP
18 #define KOKKOS_GRAPH_HPP
19 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE
21 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_GRAPH
22 #endif
23 
24 #include <Kokkos_Macros.hpp>
25 #include <impl/Kokkos_Error.hpp> // KOKKOS_EXPECTS
26 
27 #include <Kokkos_Graph_fwd.hpp>
28 #include <impl/Kokkos_GraphImpl_fwd.hpp>
29 
30 // GraphAccess needs to be defined, not just declared
31 #include <impl/Kokkos_GraphImpl.hpp>
32 
33 #include <functional>
34 #include <memory>
35 
36 namespace Kokkos {
37 namespace Experimental {
38 
39 //==============================================================================
40 // <editor-fold desc="Graph"> {{{1
41 
42 template <class ExecutionSpace>
43 struct [[nodiscard]] Graph {
44  public:
45  //----------------------------------------------------------------------------
46  // <editor-fold desc="public member types"> {{{2
47 
48  using execution_space = ExecutionSpace;
49  using graph = Graph;
50 
51  // </editor-fold> end public member types }}}2
52  //----------------------------------------------------------------------------
53 
54  private:
55  //----------------------------------------------------------------------------
56  // <editor-fold desc="friends"> {{{2
57 
58  friend struct Kokkos::Impl::GraphAccess;
59 
60  // </editor-fold> end friends }}}2
61  //----------------------------------------------------------------------------
62 
63  //----------------------------------------------------------------------------
64  // <editor-fold desc="private data members"> {{{2
65 
66  using impl_t = Kokkos::Impl::GraphImpl<ExecutionSpace>;
67  std::shared_ptr<impl_t> m_impl_ptr = nullptr;
68 
69  // </editor-fold> end private data members }}}2
70  //----------------------------------------------------------------------------
71 
72  //----------------------------------------------------------------------------
73  // <editor-fold desc="private ctors"> {{{2
74 
75  // Note: only create_graph() uses this constructor, but we can't just make
76  // that a friend instead of GraphAccess because of the way that friend
77  // function template injection works.
78  explicit Graph(std::shared_ptr<impl_t> arg_impl_ptr)
79  : m_impl_ptr(std::move(arg_impl_ptr)) {}
80 
81  // </editor-fold> end private ctors }}}2
82  //----------------------------------------------------------------------------
83 
84  public:
85  ExecutionSpace const& get_execution_space() const {
86  return m_impl_ptr->get_execution_space();
87  }
88 
89  void submit() const {
90  KOKKOS_EXPECTS(bool(m_impl_ptr))
91  (*m_impl_ptr).submit();
92  }
93 };
94 
95 // </editor-fold> end Graph }}}1
96 //==============================================================================
97 
98 //==============================================================================
99 // <editor-fold desc="when_all"> {{{1
100 
101 template <class... PredecessorRefs>
102 // constraints (not intended for subsumption, though...)
103 // ((remove_cvref_t<PredecessorRefs> is a specialization of
104 // GraphNodeRef with get_root().get_graph_impl() as its GraphImpl)
105 // && ...)
106 auto when_all(PredecessorRefs&&... arg_pred_refs) {
107  // TODO @graph @desul-integration check the constraints and preconditions
108  // once we have folded conjunctions from
109  // desul
110  static_assert(sizeof...(PredecessorRefs) > 0,
111  "when_all() needs at least one predecessor.");
112  auto graph_ptr_impl =
113  Kokkos::Impl::GraphAccess::get_graph_weak_ptr(
114  std::get<0>(std::forward_as_tuple(arg_pred_refs...)))
115  .lock();
116  auto node_ptr_impl = graph_ptr_impl->create_aggregate_ptr(arg_pred_refs...);
117  graph_ptr_impl->add_node(node_ptr_impl);
118  (graph_ptr_impl->add_predecessor(node_ptr_impl, arg_pred_refs), ...);
119  return Kokkos::Impl::GraphAccess::make_graph_node_ref(
120  std::move(graph_ptr_impl), std::move(node_ptr_impl));
121 }
122 
123 // </editor-fold> end when_all }}}1
124 //==============================================================================
125 
126 //==============================================================================
127 // <editor-fold desc="create_graph"> {{{1
128 
129 template <class ExecutionSpace, class Closure>
130 Graph<ExecutionSpace> create_graph(ExecutionSpace ex, Closure&& arg_closure) {
131  // Create a shared pointer to the graph:
132  // We need an attorney class here so we have an implementation friend to
133  // create a Graph class without graph having public constructors. We can't
134  // just make `create_graph` itself a friend because of the way that friend
135  // function template injection works.
136  auto rv = Kokkos::Impl::GraphAccess::construct_graph(std::move(ex));
137  // Invoke the user's graph construction closure
138  ((Closure &&) arg_closure)(Kokkos::Impl::GraphAccess::create_root_ref(rv));
139  // and given them back the graph
140  // KOKKOS_ENSURES(rv.m_impl_ptr.use_count() == 1)
141  return rv;
142 }
143 
144 template <
145  class ExecutionSpace = DefaultExecutionSpace,
146  class Closure = Kokkos::Impl::DoNotExplicitlySpecifyThisTemplateParameter>
147 Graph<ExecutionSpace> create_graph(Closure&& arg_closure) {
148  return create_graph(ExecutionSpace{}, (Closure &&) arg_closure);
149 }
150 
151 // </editor-fold> end create_graph }}}1
152 //==============================================================================
153 
154 } // end namespace Experimental
155 } // namespace Kokkos
156 
157 // Even though these things are separable, include them here for now so that
158 // the user only needs to include Kokkos_Graph.hpp to get the whole facility.
159 #include <Kokkos_GraphNode.hpp>
160 
161 #include <impl/Kokkos_GraphNodeImpl.hpp>
162 #include <impl/Kokkos_Default_Graph_Impl.hpp>
163 #include <Cuda/Kokkos_Cuda_Graph_Impl.hpp>
164 #if defined(KOKKOS_ENABLE_HIP)
165 // The implementation of hipGraph in ROCm 5.2 is bugged, so we cannot use it.
166 #if !((HIP_VERSION_MAJOR == 5) && (HIP_VERSION_MINOR == 2))
167 #include <HIP/Kokkos_HIP_Graph_Impl.hpp>
168 #endif
169 #endif
170 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_GRAPH
171 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
172 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_GRAPH
173 #endif
174 #endif // KOKKOS_GRAPH_HPP