Kokkos Core Kernels Package Version of the Day
Kokkos_Graph.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5// Kokkos v. 3.0
6// Copyright (2020) National Technology & Engineering
7// Solutions of Sandia, LLC (NTESS).
8//
9// Under the terms of Contract DE-NA0003525 with NTESS,
10// the U.S. Government retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38//
39// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40//
41// ************************************************************************
42//@HEADER
43*/
44
45#ifndef KOKKOS_GRAPH_HPP
46#define KOKKOS_GRAPH_HPP
47
48#include <Kokkos_Macros.hpp>
49#include <impl/Kokkos_Error.hpp> // KOKKOS_EXPECTS
50
51#include <Kokkos_Graph_fwd.hpp>
52#include <impl/Kokkos_GraphImpl_fwd.hpp>
53
54// GraphAccess needs to be defined, not just declared
55#include <impl/Kokkos_GraphImpl.hpp>
56
57#include <impl/Kokkos_Utilities.hpp> // fold emulation
58
59#include <functional>
60#include <memory>
61
62namespace Kokkos {
63namespace Experimental {
64
65//==============================================================================
66// <editor-fold desc="Graph"> {{{1
67
68template <class ExecutionSpace>
69struct KOKKOS_ATTRIBUTE_NODISCARD Graph {
70 public:
71 //----------------------------------------------------------------------------
72 // <editor-fold desc="public member types"> {{{2
73
74 using execution_space = ExecutionSpace;
75 using graph = Graph;
76
77 // </editor-fold> end public member types }}}2
78 //----------------------------------------------------------------------------
79
80 private:
81 //----------------------------------------------------------------------------
82 // <editor-fold desc="friends"> {{{2
83
84 friend struct Kokkos::Impl::GraphAccess;
85
86 // </editor-fold> end friends }}}2
87 //----------------------------------------------------------------------------
88
89 //----------------------------------------------------------------------------
90 // <editor-fold desc="private data members"> {{{2
91
92 using impl_t = Kokkos::Impl::GraphImpl<ExecutionSpace>;
93 std::shared_ptr<impl_t> m_impl_ptr = nullptr;
94
95 // </editor-fold> end private data members }}}2
96 //----------------------------------------------------------------------------
97
98 //----------------------------------------------------------------------------
99 // <editor-fold desc="private ctors"> {{{2
100
101 // Note: only create_graph() uses this constructor, but we can't just make
102 // that a friend instead of GraphAccess because of the way that friend
103 // function template injection works.
104 explicit Graph(std::shared_ptr<impl_t> arg_impl_ptr)
105 : m_impl_ptr(std::move(arg_impl_ptr)) {}
106
107 // </editor-fold> end private ctors }}}2
108 //----------------------------------------------------------------------------
109
110 public:
111 ExecutionSpace const& get_execution_space() const {
112 return m_impl_ptr->get_execution_space();
113 }
114
115 void submit() const {
116 KOKKOS_EXPECTS(bool(m_impl_ptr))
117 (*m_impl_ptr).submit();
118 }
119};
120
121// </editor-fold> end Graph }}}1
122//==============================================================================
123
124//==============================================================================
125// <editor-fold desc="when_all"> {{{1
126
127template <class... PredecessorRefs>
128// constraints (not intended for subsumption, though...)
129// ((remove_cvref_t<PredecessorRefs> is a specialization of
130// GraphNodeRef with get_root().get_graph_impl() as its GraphImpl)
131// && ...)
132auto when_all(PredecessorRefs&&... arg_pred_refs) {
133 // TODO @graph @desul-integration check the constraints and preconditions
134 // once we have folded conjunctions from
135 // desul
136 static_assert(sizeof...(PredecessorRefs) > 0,
137 "when_all() needs at least one predecessor.");
138 auto graph_ptr_impl =
139 Kokkos::Impl::GraphAccess::get_graph_weak_ptr(
140 std::get<0>(std::forward_as_tuple(arg_pred_refs...)))
141 .lock();
142 auto node_ptr_impl = graph_ptr_impl->create_aggregate_ptr(arg_pred_refs...);
143 graph_ptr_impl->add_node(node_ptr_impl);
144 KOKKOS_IMPL_FOLD_COMMA_OPERATOR(
145 graph_ptr_impl->add_predecessor(node_ptr_impl, arg_pred_refs) /* ... */);
146 return Kokkos::Impl::GraphAccess::make_graph_node_ref(
147 std::move(graph_ptr_impl), std::move(node_ptr_impl));
148}
149
150// </editor-fold> end when_all }}}1
151//==============================================================================
152
153//==============================================================================
154// <editor-fold desc="create_graph"> {{{1
155
156template <class ExecutionSpace, class Closure>
157Graph<ExecutionSpace> create_graph(ExecutionSpace ex, Closure&& arg_closure) {
158 // Create a shared pointer to the graph:
159 // We need an attorney class here so we have an implementation friend to
160 // create a Graph class without graph having public constructors. We can't
161 // just make `create_graph` itself a friend because of the way that friend
162 // function template injection works.
163 auto rv = Kokkos::Impl::GraphAccess::construct_graph(std::move(ex));
164 // Invoke the user's graph construction closure
165 ((Closure &&) arg_closure)(Kokkos::Impl::GraphAccess::create_root_ref(rv));
166 // and given them back the graph
167 // KOKKOS_ENSURES(rv.m_impl_ptr.use_count() == 1)
168 return rv;
169}
170
171template <
172 class ExecutionSpace = DefaultExecutionSpace,
173 class Closure = Kokkos::Impl::DoNotExplicitlySpecifyThisTemplateParameter>
174Graph<ExecutionSpace> create_graph(Closure&& arg_closure) {
175 return create_graph(ExecutionSpace{}, (Closure &&) arg_closure);
176}
177
178// </editor-fold> end create_graph }}}1
179//==============================================================================
180
181} // end namespace Experimental
182} // namespace Kokkos
183
184// Even though these things are separable, include them here for now so that
185// the user only needs to include Kokkos_Graph.hpp to get the whole facility.
186#include <Kokkos_GraphNode.hpp>
187
188#include <impl/Kokkos_GraphNodeImpl.hpp>
189#include <impl/Kokkos_Default_Graph_Impl.hpp>
190#include <Cuda/Kokkos_Cuda_Graph_Impl.hpp>
191#endif // KOKKOS_GRAPH_HPP