Compute Graph Framework SDK Reference  5.12
SyncPortHelper.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_SYNCPORTHELPER_HPP_
32#define DW_FRAMEWORK_SYNCPORTHELPER_HPP_
33
34#include <dwshared/dwfoundation/dw/core/container/HashContainer.hpp>
36
37namespace dw
38{
39namespace framework
40{
41
42using CycleCountFetcher = dw::core::Function<uint32_t(void)>;
43
44// These classes are used to parse and handle indexed packets.
45// TODO (ajayawardane) Move this logic into a separate port and change
46// the port type for each pipelined node.
48{
49public:
51 : m_dataSynced(false)
52 , m_syncCount(0U)
53 , m_dataOffset(0U)
54 , m_syncCountRetriever(nullptr)
55 {
56 }
57 void setSyncCount(uint32_t index);
58 virtual void parseDataSynced(const ChannelParams& params);
60 dwStatus setSyncRetriever(const CycleCountFetcher& func);
61
62protected:
64 void stampSyncCount(uint32_t& syncCountOut) const;
65
67 uint32_t m_syncCount;
68 uint32_t m_dataOffset;
70};
71
72// There are no specific requirements on the template type
73// coverity[autosar_cpp14_a14_1_1_violation]
74template <typename T>
76{
77public:
80 {
81 }
82
83protected:
85 {
86 MetadataPayload* metadataPacket{genericData.template getData<MetadataPayload>()};
87
88 if (!metadataPacket)
89 {
90 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperOutput extractInternalPacket: packet type mismatch");
91 }
92
93 T* packet{metadataPacket->data.template getData<T>()};
94 if (!packet)
95 {
96 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperOutput extractInternalPacket: failed to extract underlying data");
97 }
98
99 m_metadataPayloadBuf[packet] = metadataPacket;
100 return packet;
101 }
102
104 {
105 MetadataPayload* metadataPacket{m_metadataPayloadBuf[frame]};
106 if (!metadataPacket)
107 {
108 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperOutput getmetadataPacket: sync packet not found in packet buffer");
109 }
110
111 return metadataPacket;
112 }
113
114 void parseDataSynced(const ChannelParams& params) override
115 {
117 m_metadataPayloadBuf = HeapHashMap<T*, MetadataPayload*>(params.getPoolCapacity());
118 }
119
120private:
121 HeapHashMap<T*, MetadataPayload*> m_metadataPayloadBuf;
122};
123
124// There are no specific requirements on the template type
125// coverity[autosar_cpp14_a14_1_1_violation]
126template <typename T>
128{
129public:
132 , m_bufferedPacket()
133 , m_dataBuffered(false)
134 {
135 }
136
137protected:
139 {
140 if (m_dataBuffered)
141 {
142 return true;
143 }
144 return false;
145 }
146
148 {
149 if (!isPacketBuffered())
150 {
151 return false;
152 }
153
154 MetadataPayload* packet{m_bufferedPacket.template getData<MetadataPayload>()};
155
156 if (!packet)
157 {
158 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperInput isValidPacketBuffered: packet type mistmatch");
159 }
160 return validatePacket(*packet);
161 }
162
164 {
165 m_dataBuffered = false;
166 return m_bufferedPacket;
167 }
168
170 {
171 MetadataPayload* metadataPacket{genericData.template getData<MetadataPayload>()};
172
173 if (!metadataPacket)
174 {
175 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperInput extractSyncPacket: packet type mistmatch");
176 }
177
178 if (validatePacket(*metadataPacket))
179 {
180 T* packet{metadataPacket->data.template getData<T>()};
181 m_metadataPayloadBuf[packet] = metadataPacket;
182 return packet;
183 }
184 else
185 {
186 m_bufferedPacket = genericData;
187 m_dataBuffered = true;
188 return nullptr;
189 }
190 }
191
193 {
194 MetadataPayload* metadataPacket{genericData.template getData<MetadataPayload>()};
195
196 if (!metadataPacket)
197 {
198 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperInput extractInternalPacket: packet type mistmatch");
199 }
200
201 T* packet{metadataPacket->data.template getData<T>()};
202 m_metadataPayloadBuf[packet] = metadataPacket;
203 return packet;
204 }
205
207 {
208 MetadataPayload* metadataPacket = m_metadataPayloadBuf[frame];
209 if (!metadataPacket)
210 {
211 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "SyncPortHelperInput getmetadataPacket: sync packet not found in packet buffer");
212 }
213
214 return metadataPacket;
215 }
216
217 void parseDataSynced(const ChannelParams& params) override
218 {
220 m_metadataPayloadBuf = HeapHashMap<T*, MetadataPayload*>(params.getPoolCapacity());
221 }
222
223private:
224 bool validatePacket(MetadataPayload& pkt)
225 {
226 // If a producer - consumer pair are across pipeline boundaries, they will
227 // have non-zero data offsets; however, connections from that producer to
228 // consumers not across the pipeline boundary must also have sync ports
229 // (since if a producer is sync, all consumers must be sync). This check
230 // is in place for cases where producer -> consumer pairs are in the same
231 // pipeline boundary, and is basically a no-op for synchronization.
232 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
233 if (m_dataOffset == 0U)
234 {
235 return true;
236 }
237
238 uint32_t syncCount{(m_syncCountRetriever.has_function()) ? m_syncCountRetriever() : m_syncCount};
239
240 // Check if the packet is valid for consumption. The packet sync count represents
241 // when the packet was produced and the m_syncCount is the current sync count.
242 // The data offset is the offset between when it was produced and when it is
243 // available for consumption.
244 // coverity[autosar_cpp14_a4_7_1_violation]
245 // coverity[cert_int30_c_violation]
246 // coverity[cert_int31_c_violation]
247 int validOffset{static_cast<int>(syncCount - pkt.header.iterationCount - m_dataOffset)};
248
249 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
250 if (validOffset >= 0)
251 {
252 return true;
253 }
254
255 return false;
256 }
257
258 HeapHashMap<T*, MetadataPayload*> m_metadataPayloadBuf;
259 GenericData m_bufferedPacket;
260 bool m_dataBuffered;
261};
262
263} // namespace framework
264} // namespace dw
265
266#endif // DW_FRAMEWORK_SYNCPORTHELPER_HPP_
T * extractInternalPacket(GenericData genericData)
T * extractSyncPacket(GenericData genericData)
MetadataPayload * getMetadataPacket(T *frame)
void parseDataSynced(const ChannelParams &params) override
T * extractInternalPacket(GenericData genericData)
void parseDataSynced(const ChannelParams &params) override
MetadataPayload * getMetadataPacket(T *frame)
uint32_t ChannelPacketTypeID
dw::core::Function< uint32_t(void)> CycleCountFetcher
Definition: Buffer.hpp:40
dwStatus setSyncRetriever(const CycleCountFetcher &func)
ChannelPacketTypeID getNewPacketID(ChannelPacketTypeID packetTypeID)
CycleCountFetcher m_syncCountRetriever
void setSyncCount(uint32_t index)
virtual void parseDataSynced(const ChannelParams &params)
void stampSyncCount(uint32_t &syncCountOut) const