Compute Graph Framework SDK Reference  5.8
ComputeGraphImpl.hpp
Go to the documentation of this file.
1
2//
3// Notice
4// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
5// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
6// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
7// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8//
9// NVIDIA CORPORATION & AFFILIATES assumes no responsibility for the consequences of use of such
10// information or for any infringement of patents or other rights of third parties that may
11// result from its use. No license is granted by implication or otherwise under any patent
12// or patent rights of NVIDIA CORPORATION & AFFILIATES. No third party distribution is allowed unless
13// expressly authorized by NVIDIA. Details are subject to change without notice.
14// This code supersedes and replaces all information previously supplied.
15// NVIDIA CORPORATION & AFFILIATES products are not authorized for use as critical
16// components in life support devices or systems without express written approval of
17// NVIDIA CORPORATION & AFFILIATES.
18//
19// SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
21//
22// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
23// property and proprietary rights in and to this material, related
24// documentation and any modifications thereto. Any use, reproduction,
25// disclosure or distribution of this material and related documentation
26// without an express license agreement from NVIDIA CORPORATION or
27// its affiliates is strictly prohibited.
28//
30
31#ifndef DW_FRAMEWORK_COMPUTEGRAPH_IMPL_HPP_
32#define DW_FRAMEWORK_COMPUTEGRAPH_IMPL_HPP_
33
34#include <dw/core/base/Types.h>
35
36#include <dwcgf/Types.hpp>
38#include <dwcgf/node/Node.hpp>
39#include <dwcgf/pass/Pass.hpp>
41
42#include <dw/core/container/Span.hpp>
43#include <dw/core/container/VectorFixed.hpp>
44#include <dw/core/container/HashContainer.hpp>
45
46#include <stack>
47
48#include <vector>
49#include <algorithm>
50#include <iterator>
51
52namespace dw
53{
54namespace core
55{
56template <typename Type, size_t CapacityAtCompileTime_ = 0>
57class StackFixed : public VectorFixed<Type, CapacityAtCompileTime_>
58{
59public:
61 : VectorFixed<Type, CapacityAtCompileTime_>()
62 {
63 }
64
65 explicit StackFixed(size_t size_)
66 : VectorFixed<Type, CapacityAtCompileTime_>(size_)
67 {
68 }
69
70 bool push(const Type& value)
71 {
72 return this->push_back(value);
73 }
74
75 bool pop()
76 {
77 if (this->empty())
78 {
79 return false;
80 }
81 this->pop_back();
82 return true;
83 }
84
85 const Type& top()
86 {
87 if (this->empty())
88 {
89 return this->at(0);
90 }
91 return this->back();
92 }
93
94 bool contains(const Type& value) const
95 {
96 for (const auto& each : *this)
97 {
98 if (each == value)
99 {
100 return true;
101 }
102 }
103 return false;
104 }
105};
106
107template <typename Type, size_t CapacityAtCompileTime_ = 0>
108class QueueFixed : public VectorFixed<Type, CapacityAtCompileTime_>
109{
110public:
112 : VectorFixed<Type, CapacityAtCompileTime_>()
113 {
114 }
115
116 explicit QueueFixed(size_t size_)
117 : VectorFixed<Type, CapacityAtCompileTime_>(size_)
118 {
119 }
120
121 bool push(const Type& value)
122 {
123 return this->push_back(value);
124 }
125
126 bool empty()
127 {
128 return this->size() == 0;
129 }
130
131 bool pop()
132 {
133 if (this->empty())
134 {
135 return false;
136 }
137 m_index++;
138 return true;
139 }
140
141 const Type& top()
142 {
143 if (this->empty())
144 {
145 return this->at(0);
146 }
147 return this->at(m_index);
148 }
149
150 size_t size()
151 {
152 return VectorFixed<Type, CapacityAtCompileTime_>::size() - m_index;
153 }
154
155 void clear()
156 {
157 VectorFixed<Type, CapacityAtCompileTime_>::clear();
158 m_index = 0;
159 }
160
161 bool contains(const Type& value) const
162 {
163 for (const auto& each : *this)
164 {
165 if (each == value)
166 {
167 return true;
168 }
169 }
170 return false;
171 }
172
173private:
174 size_t m_index = 0;
175};
176}
177}
178
179// TODO(eklein): clang-tidy complains about this "using" here. They suggest instead we use "using-declarations".
180// See https://en.cppreference.com/w/cpp/language/using_declaration.
181//
182// That's a fair bit of work, so for now I'm leading this as a TODO, but we ought to at least look into it.
183//
184// NOLINTNEXTLINE(google-build-using-namespace)
185//using namespace dw::core;
186
187namespace dw
188{
189namespace framework
190{
191
192/***
193 * @brief A collection of different execution types
194 */
196{
199};
200
202{
203public:
204 static constexpr char LOG_TAG[] = "ComputeGraphImpl";
205
207
208 virtual ~ComputeGraphImpl() = default;
209
210 void setChannels(span<ChannelObject*> channels);
211
212 void setNodes(span<Node*> nodes);
213
214 void setConnections(span<const Connection> connections);
215
217
218 void build();
219
220 void getPass(Pass*& pass, const char* key);
221
222 void getPasses(VectorFixed<Pass*>& passList, uint64_t nodeIndex);
223
224 void getPasses(VectorFixed<Pass*>& passList, ComputeGraphTraversalOrder order);
225
226 void getPasses(VectorFixed<Pass*>& passList, dwProcessorType processorType, dwProcessType processType, ComputeGraphTraversalOrder order);
227
229
231
232private:
233 static constexpr uint64_t MAX_ADJACENCY_MATRIX_DIM = 1000;
234 static constexpr uint64_t MAX_ADJACENCY_MATRIX_CELL_DIM = 100;
235
236 void connectNode(const Connection& connection);
237
238 bool hasChannels() const;
239 bool hasNodes() const;
240 bool hasConnections() const;
241 bool hasValidInputs() const;
242 bool connectNodeInputsAreValid(const Connection& connection) const;
243
244 using CellEntry_t = struct CellEntry_t
245 {
246 uint64_t inputNodeId;
247 uint64_t channelId;
248 };
249
250 using AdjacencyMatrixCell_t = VectorFixed<CellEntry_t, MAX_ADJACENCY_MATRIX_CELL_DIM>;
251 using AdjacencyMatrixRow_t = VectorFixed<AdjacencyMatrixCell_t, MAX_ADJACENCY_MATRIX_DIM>;
252 using AdjacenyMatrix_t = VectorFixed<AdjacencyMatrixRow_t, MAX_ADJACENCY_MATRIX_DIM>;
253 using Queue_t = QueueFixed<uint64_t, MAX_ADJACENCY_MATRIX_DIM>;
254 using Stack_t = StackFixed<uint64_t, MAX_ADJACENCY_MATRIX_DIM>;
255 using Vector_t = VectorFixed<uint64_t, MAX_ADJACENCY_MATRIX_DIM>;
256 using VectorB_t = VectorFixed<bool, MAX_ADJACENCY_MATRIX_DIM>;
257
258 static void labelDisconnectedInputs(AdjacenyMatrix_t& adjacencyMatrix);
259
260 static bool graphHasImproperChannelUse(Vector_t& usedChannels,
261 const AdjacenyMatrix_t& adjacencyMatrix,
262 uint64_t numChannels);
263 static bool graphIsCyclic(uint64_t rowIndex,
264 VectorB_t& visited,
265 VectorB_t& nodesToExplore,
266 const AdjacenyMatrix_t& adjacencyMatrix);
267 static bool graphIsIncorrect(Stack_t& stack,
268 VectorB_t& visited,
269 VectorB_t& nodesToExplore,
270 const Queue_t& rootNodes,
271 const AdjacenyMatrix_t& adjacencyMatrix);
272
273 template <typename ContainterType>
274 static void findRootNodes(ContainterType& rootNodes,
275 const AdjacenyMatrix_t& adjacencyMatrix);
276
277 template <typename ContainerType, typename VisitFunction>
278 static void traverse(ContainerType& nodesToVisit,
279 const Queue_t& rootNodes,
280 const AdjacenyMatrix_t& adjacencyMatrix,
281 VisitFunction visit);
282
283 static void prettyPrint(const AdjacenyMatrix_t& adjacencyMatrix);
284
285private:
286 span<ChannelObject*> m_channels;
287 span<Node*> m_nodes;
288 span<const Connection> m_connections;
289
290 bool m_validated;
291
313
314 AdjacenyMatrix_t m_adjacencyMatrix;
315
316 Vector_t m_usedChannels;
317 VectorB_t m_nodesToExplore;
318 VectorB_t m_visitedNodes;
319 Queue_t m_rootNodes;
320 Queue_t m_queue;
321 Stack_t m_stack;
322
323 VectorFixed<Pass*> m_passList;
324 using PassTableString = FixedString<256>;
325 HashMap<PassTableString, Pass*> m_passTable;
326};
327}
328}
329
330#endif // DW_FRAMEWORK_COMPUTEGRAPH_IMPL_HPP_
bool push(const Type &value)
bool contains(const Type &value) const
bool push(const Type &value)
bool contains(const Type &value) const
void getPasses(VectorFixed< Pass * > &passList, ComputeGraphTraversalOrder order)
void setChannels(span< ChannelObject * > channels)
void getPass(Pass *&pass, const char *key)
void getPasses(VectorFixed< Pass * > &passList, dwProcessorType processorType, dwProcessType processType, ComputeGraphTraversalOrder order)
void run(ComputeGraphTraversalOrder order)
void setConnections(span< const Connection > connections)
void setNodes(span< Node * > nodes)
static constexpr char LOG_TAG[]
void getPasses(VectorFixed< Pass * > &passList, uint64_t nodeIndex)
virtual ~ComputeGraphImpl()=default
Pass is a runnable describes the metadata of a pass.
Definition: Pass.hpp:49
Connection { uint64_t srcNodeId Connection
Definition: Connection.hpp:45
uint64_t channelId
Definition: Connection.hpp:49
Definition: Exception.hpp:47