Compute Graph Framework SDK Reference  5.12
PortDescriptor.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) 2021-2023 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_PORTDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_PORTDESCRIPTOR_HPP_
33
34#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
35#include <dwcgf/port/Port.hpp>
36#include <dwshared/dwfoundation/dw/core/language/cxx20.hpp>
37#include <dwshared/dwfoundation/dw/core/language/Tuple.hpp>
38
39#include <functional>
40#include <tuple>
41#include <type_traits>
42#include <utility>
43
44namespace dw
45{
46namespace framework
47{
48
49// API needed to declare the ports of a node.
50
51template <typename... Args>
52// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
53constexpr auto describePortCollection(Args&&... args) -> dw::core::Tuple<Args...>
54{
55 return dw::core::make_tuple<Args...>(std::forward<Args>(args)...);
56}
57
58// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
59// coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
60static constexpr const size_t PORT_TYPE_NAME{0U};
61// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
62// coverity[autosar_cpp14_m0_1_4_violation] RFD Pending: TID-2055
63static constexpr const size_t PORT_NAME{1U};
64// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
65// coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
66static constexpr const size_t PORT_TYPE{2U};
67// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
68// coverity[autosar_cpp14_m0_1_4_violation] RFD Pending: TID-2055
69static constexpr const size_t PORT_ARRAY_SIZE{3U};
70// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
71// coverity[autosar_cpp14_m0_1_4_violation] RFD Pending: TID-2055
72static constexpr const size_t PORT_BINDING{4U};
73// coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
74// coverity[autosar_cpp14_m0_1_4_violation] RFD Pending: TID-2055
75static constexpr const size_t PORT_COMMENT{5U};
76
77enum class PortBinding : uint8_t
78{
79 OPTIONAL = 0,
80 REQUIRED = 1
81};
82
83#define DW_PORT_TYPE_NAME_STRING_VIEW(TYPE_NAME_STR) TYPE_NAME_STR##_sv
84#define DW_DESCRIBE_PORT(TYPE_NAME, args...) dw::framework::describePort<TYPE_NAME>(DW_PORT_TYPE_NAME_STRING_VIEW(#TYPE_NAME), ##args)
85
86template <typename PortType>
87// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
88constexpr auto describePort(
89 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> std::tuple<dw::core::StringView, dw::core::StringView, PortType*, size_t, PortBinding, dw::core::StringView>
90{
91 return std::make_tuple(
92 std::move(typeName),
93 std::move(name),
94 static_cast<PortType*>(nullptr),
95 static_cast<size_t>(0),
96 std::move(binding),
97 std::move(comment));
98}
99
100template <typename PortType>
101// Overloaded functions are provided for ease of use
102// coverity[autosar_cpp14_a2_10_5_violation]
103constexpr auto describePort(
104 dw::core::StringView typeName, dw::core::StringView name, dw::core::StringView comment)
105{
106 return describePort<PortType>(
107 std::move(typeName),
108 std::move(name),
109 std::move(PortBinding::OPTIONAL),
110 std::move(comment));
111}
112
113#define DW_DESCRIBE_PORT_ARRAY(TYPE_NAME, ARRAYSIZE, args...) dw::framework::describePortArray<TYPE_NAME, ARRAYSIZE>(DW_PORT_TYPE_NAME_STRING_VIEW(#TYPE_NAME), ##args)
114
115template <
116 typename PortType,
117 size_t ArraySize,
118 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
119// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
120constexpr auto describePortArray(
121 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> std::tuple<dw::core::StringView, dw::core::StringView, PortType*, size_t, PortBinding, dw::core::StringView>
122{
123 return std::make_tuple(
124 std::move(typeName),
125 std::move(name),
126 static_cast<PortType*>(nullptr),
127 ArraySize,
128 std::move(binding),
129 std::move(comment));
130}
131
132template <
133 typename PortType,
134 size_t ArraySize,
135 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
136// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
137constexpr auto describePortArray(
138 dw::core::StringView typeName, dw::core::StringView name, dw::core::StringView comment)
139{
140 return describePortArray<PortType, ArraySize>(
141 std::move(typeName),
142 std::move(name),
143 std::move(PortBinding::OPTIONAL),
144 std::move(comment));
145}
146
147// API to access declared ports of a node.
148
149template <typename Node>
150// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
151// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
152constexpr auto describeInputPorts()
153{
155}
156
157template <typename Node>
158// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
159// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
160constexpr auto describeOutputPorts()
161{
163}
164
165template <
166 typename Node,
167 PortDirection Direction,
168 typename std::enable_if_t<Direction == PortDirection::INPUT, void>* = nullptr>
169// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
170// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
171constexpr auto describePorts()
172{
173 return describeInputPorts<Node>();
174}
175
176template <
177 typename Node,
178 PortDirection Direction,
179 typename std::enable_if_t<Direction == PortDirection::OUTPUT, void>* = nullptr>
180// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
181// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
182constexpr auto describePorts()
183{
184 return describeOutputPorts<Node>();
185}
186
187// API to query information about declared ports of a node.
188
189// Number of input or output port descriptors
190template <typename Node, PortDirection Direction>
191// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
192constexpr std::size_t portDescriptorSize()
193{
194 return dw::core::tuple_size<decltype(describePorts<Node, Direction>())>::value;
195}
196
197// The flag if the port described by a specific descriptor is an array
198template <typename Node, PortDirection Direction, size_t DescriptorIndex>
199constexpr bool descriptorPortArray()
200{
201 constexpr size_t array_length = std::get<dw::framework::PORT_ARRAY_SIZE>(
202 dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()));
203 return array_length > 0;
204}
205
206// The number of input or output ports described by a specific descriptor
207// 1 for non-array descriptors, ARRAY_SIZE for array descriptors
208template <typename Node, PortDirection Direction, size_t DescriptorIndex>
209// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
210constexpr size_t descriptorPortSize()
211{
212 constexpr size_t array_length{std::get<dw::framework::PORT_ARRAY_SIZE>(
213 dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()))};
214 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
215 if (array_length == 0U)
216 {
217 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
218 return 1U;
219 }
220 return array_length;
221}
222
223// The binding of input or output ports described by a specific descriptor
224template <typename Node, PortDirection Direction, size_t DescriptorIndex>
226{
227 constexpr PortBinding port_binding = std::get<dw::framework::PORT_BINDING>(
228 dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()));
229 return port_binding;
230}
231
232// The comment of input or output ports described by a specific descriptor
233template <typename Node, PortDirection Direction, size_t DescriptorIndex>
234constexpr dw::core::StringView descriptorPortComment()
235{
236 constexpr dw::core::StringView port_comment = std::get<dw::framework::PORT_COMMENT>(
237 dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()));
238 return port_comment;
239}
240
241// Return type is the type of the descriptor, to be used with decltype()
242template <typename Node, PortDirection Direction, size_t DescriptorIndex>
243// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
244// coverity[autosar_cpp14_a7_1_5_violation]
245constexpr auto portDescriptorType()
246{
247 // since the PortDescriptor contains a T* to avoid storing an actual
248 // instance of T the pointer needs to be removed here
249 return std::remove_pointer_t<
250 typename std::tuple_element_t<
252 typename dw::core::tuple_element_t<
253 DescriptorIndex,
254 decltype(describePorts<Node, Direction>())>>>();
255}
256
257// Number of ports for a specific direction (sum across all descriptors)
258namespace detail
259{
260
261template <
262 typename Node, PortDirection Direction, size_t DescriptorIndex,
263 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
264// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
265constexpr std::size_t portSize_()
266{
267 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
268 return 0U;
269}
270
271template <
272 typename Node, PortDirection Direction, size_t DescriptorIndex,
273 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
274 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
275 constexpr std::size_t portSize_()
276{
277 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portSize_<Node, Direction, DescriptorIndex + 1>();
278}
279
280} // namespace detail
281
282template <typename Node, PortDirection Direction>
283// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
284constexpr std::size_t portSize()
285{
286 return detail::portSize_<Node, Direction, 0>();
287}
288
289// Descriptor index from port index
290namespace detail
291{
292
293template <
294 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
295 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
296// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
297constexpr std::size_t descriptorIndex_()
298{
299 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
300 return 0U;
301}
302
303template <
304 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
305 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
306 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
307 constexpr std::size_t descriptorIndex_()
308{
309 // coverity[autosar_cpp14_a5_1_1_violation]
310 if (RemainingPortIndex < descriptorPortSize<Node, Direction, DescriptorIndex>())
311 {
312 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
313 return 0U;
314 }
315 // coverity[autosar_cpp14_a0_1_1_violation]
316 // coverity[autosar_cpp14_a5_1_1_violation]
317 // coverity[autosar_cpp14_m0_1_4_violation]
318 constexpr size_t remainingPortIndex{RemainingPortIndex - descriptorPortSize<Node, Direction, DescriptorIndex>()};
319 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
320 return 1U + descriptorIndex_<Node, Direction, DescriptorIndex + 1, remainingPortIndex>();
321}
322
323} // namespace detail
324
325template <typename Node, PortDirection Direction, size_t PortIndex>
326// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
327constexpr size_t descriptorIndex()
328{
329 if (Direction == PortDirection::OUTPUT)
330 {
331 return detail::descriptorIndex_<Node, Direction, 0, PortIndex - portSize<Node, PortDirection::INPUT>()>();
332 }
333 return detail::descriptorIndex_<Node, Direction, 0, PortIndex>();
334}
335
336// Return type is the type of the port, to be used with decltype()
337template <typename Node, PortDirection Direction, size_t PortIndex>
338// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
339// coverity[autosar_cpp14_a7_1_5_violation]
340constexpr auto portType()
341{
342 // coverity[autosar_cpp14_a0_1_1_violation]
343 // coverity[autosar_cpp14_m0_1_4_violation]
344 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
345 return portDescriptorType<Node, Direction, index>();
346}
347
348// Check if port index is valid
349template <typename Node, PortDirection Direction>
350constexpr bool isValidPortIndex(std::size_t portID)
351{
352 // only temporarily for backward compatibility with enum value
353 // output port indices are offset by the number of input ports
354 if (Direction == PortDirection::OUTPUT)
355 {
356 return portID >= portSize<Node, PortDirection::INPUT>() && portID < portSize<Node, PortDirection::INPUT>() + portSize<Node, Direction>();
357 }
358 return portID < portSize<Node, Direction>();
359}
360
361// Array size for an array port name, 0 for non-array ports
362namespace detail
363{
364
365template <
366 typename Node, PortDirection Direction, size_t DescriptorIndex,
367 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
368constexpr std::size_t portArraySize_(StringView identifier)
369{
370 (void)identifier;
371 return 0;
372}
373
374template <
375 typename Node, PortDirection Direction, size_t DescriptorIndex,
376 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
377 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
378 constexpr std::size_t portArraySize_(StringView identifier)
379{
380 constexpr auto descriptorName = std::get<dw::framework::PORT_NAME>(dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()));
381 if (descriptorName == identifier)
382 {
383 return descriptorPortSize<Node, Direction, DescriptorIndex>();
384 }
385 return portArraySize_<Node, Direction, DescriptorIndex + 1>(identifier);
386}
387
388} // namespace detail
389
390template <typename Node, PortDirection Direction>
391constexpr size_t portArraySize(StringView identifier)
392{
393 return detail::portArraySize_<Node, Direction, 0>(identifier);
394}
395
396// Get the port index for a give port name
397namespace detail
398{
399
400template <
401 typename Node, PortDirection Direction, size_t DescriptorIndex,
402 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
403// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
404constexpr std::size_t portIndex_(StringView identifier)
405{
406 static_cast<void>(identifier);
407 // since output port indices follow input port indices
408 // this must add the number of output port for invalid input port identifier
409 // to avoid that for an invalid input port identifier the index of the first output port is returned
410 if (Direction == PortDirection::INPUT)
411 {
412 return dw::framework::portSize<Node, PortDirection::OUTPUT>();
413 }
414 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
415 return 0U;
416}
417
418template <
419 typename Node, PortDirection Direction, size_t DescriptorIndex,
420 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
421 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
422 constexpr std::size_t portIndex_(StringView identifier)
423{
424 constexpr StringView descriptorName{std::get<dw::framework::PORT_NAME>(dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()))};
425 if (descriptorName == identifier)
426 {
427 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
428 return 0U;
429 }
430 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
431}
432
433} // namespace detail
434
435template <typename Node, PortDirection Direction>
436// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
437constexpr size_t portIndex(StringView identifier)
438{
439 // only temporarily for backward compatibility with enum value
440 // output port indices are offset by the number of input ports
441 if (Direction == PortDirection::OUTPUT)
442 {
443 return portSize<Node, PortDirection::INPUT>() + detail::portIndex_<Node, Direction, 0>(identifier);
444 }
445 return detail::portIndex_<Node, Direction, 0>(identifier);
446}
447
448// Check if given string is a valid port name
449template <typename Node, PortDirection Direction>
450constexpr bool isValidPortIdentifier(StringView identifier)
451{
452 constexpr size_t index = portIndex<Node, Direction>(identifier);
453 return isValidPortIndex<Node, Direction>(index);
454}
455
456// Get the port index for a give port name
457namespace detail
458{
459
460template <
461 typename Node, PortDirection Direction, size_t DescriptorIndex,
462 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
463constexpr std::size_t portDescriptorIndex_(StringView identifier)
464{
465 (void)identifier;
466 return 0;
467}
468
469template <
470 typename Node, PortDirection Direction, size_t DescriptorIndex,
471 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
472 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
473 constexpr std::size_t portDescriptorIndex_(StringView identifier)
474{
475 constexpr auto descriptorName = std::get<dw::framework::PORT_NAME>(dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()));
476 if (descriptorName == identifier)
477 {
478 return 0;
479 }
480 return 1 + portDescriptorIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
481}
482
483} // namespace detail
484
485template <typename Node, PortDirection Direction>
486constexpr size_t portDescriptorIndex(StringView identifier)
487{
488 return detail::portDescriptorIndex_<Node, Direction, 0>(identifier);
489}
490
491} // namespace framework
492} // namespace dw
493
494#endif // DW_FRAMEWORK_PORTDESCRIPTOR_HPP_
constexpr auto describePorts()
constexpr bool descriptorPortArray()
constexpr size_t portIndex(StringView identifier)
constexpr size_t descriptorPortSize()
constexpr auto describePortArray(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> std::tuple< dw::core::StringView, dw::core::StringView, PortType *, size_t, PortBinding, dw::core::StringView >
constexpr size_t portDescriptorIndex(StringView identifier)
static constexpr const size_t PORT_TYPE
static constexpr const size_t PORT_COMMENT
constexpr auto portDescriptorType()
constexpr size_t portArraySize(StringView identifier)
constexpr std::size_t portDescriptorSize()
constexpr auto describeOutputPorts()
constexpr auto describePort(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> std::tuple< dw::core::StringView, dw::core::StringView, PortType *, size_t, PortBinding, dw::core::StringView >
constexpr auto describePortCollection(Args &&... args) -> dw::core::Tuple< Args... >
constexpr bool isValidPortIndex(std::size_t portID)
constexpr std::size_t portSize()
constexpr PortBinding descriptorPortBinding()
constexpr auto describeInputPorts()
static constexpr const size_t PORT_ARRAY_SIZE
static constexpr const size_t PORT_BINDING
constexpr bool isValidPortIdentifier(StringView identifier)
constexpr dw::core::StringView descriptorPortComment()
constexpr auto portType()
constexpr size_t descriptorIndex()
static constexpr const size_t PORT_TYPE_NAME
static constexpr const size_t PORT_NAME
Definition: Buffer.hpp:40