Compute Graph Framework SDK Reference
5.4.5418 Release
For Test and Development only

IChannelPacket.hpp
Go to the documentation of this file.
1 //
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) 2020-2022 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_ICHANNEL_PACKET_HPP_
32 #define DW_FRAMEWORK_ICHANNEL_PACKET_HPP_
33 
34 #include <typeinfo>
35 #include <cstddef>
36 #include <dwcgf/Types.hpp>
39 #include <nvscibuf.h>
40 #include <nvscisync.h>
41 #include <dw/core/language/Function.hpp>
42 
43 namespace dw
44 {
45 namespace framework
46 {
47 
48 // Forward-declare
49 class ChannelLocalShmemRegistry;
50 
51 // Virtual functions for the channel to interact with packets opaquely
53 {
54 public:
55  // Enable ownership via this interface
56  virtual ~IChannelPacket() = default;
57  // Deserialize metadata at provided pointer
58  virtual GenericData getGenericData() = 0;
59 
60  // Callbacks needed for socket channel
62  {
63  public:
64  // Get a pointer to the payload
65  virtual uint8_t* getBuffer() = 0;
66  // Get size of serialized data buffer
67  virtual size_t getBufferSize() = 0;
68  // Serialize the packet to internal buffer
69  virtual size_t serialize() = 0;
70  // Deserialize the backet from internal buffer
71  virtual void deserialize(size_t) = 0;
72  };
73 
74  // Callbacks needed for nvsci channel
76  {
77  public:
78  // Init time callbacks
79 
80  // Get size of the metadata
81  virtual uint32_t getNumBuffers() const = 0;
82  // Get size of serialized data buffer
83  virtual void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList& attrList) const = 0;
84  // Initialize the packet from the given NvSciBufObjs
85  virtual void initializeFromNvSciBufObjs(dw::core::span<NvSciBufObj> bufs) = 0;
86 
87  // Runtime callbacks
88 
89  // Pack the API data type into the NvSciBufObjs
90  virtual void pack() = 0;
91  // Unpack the API data type from the NvSciBufObjs
92  virtual void unpack() = 0;
93  };
94 
95  // Get interface for channel socket, throws if not enabled or supported
97  {
98  if (auto* ptr = dynamic_cast<SocketCallbacks*>(this))
99  {
100  return *ptr;
101  }
102  else
103  {
104  throw Exception(DW_NOT_SUPPORTED, "This packet interface does not implement socket callbacks");
105  }
106  }
107 
109  {
110  if (auto* ptr = dynamic_cast<NvSciCallbacks*>(this))
111  {
112  return *ptr;
113  }
114  else
115  {
116  throw Exception(DW_NOT_SUPPORTED, "This packet interface does not implement nvsci callbacks");
117  }
118  }
119 };
120 
121 // TODO(chale): this should be made private to the framework once external entities no longer create
122 // Channel packets
124 {
125 public:
126  ChannelPacketDefaultBase(size_t typeSize)
127  : m_typeSize(typeSize)
128  , m_buffer(new uint8_t[m_typeSize]())
129  , m_data{m_buffer.get(), m_typeSize}
130  {
131  }
132 
134  {
135  return m_data;
136  }
137 
138 protected:
139  size_t m_typeSize;
140  std::unique_ptr<uint8_t[]> m_buffer;
142 };
143 
145 {
146 public:
147  ChannelPacketDefault(size_t typeSize)
148  : ChannelPacketDefaultBase(typeSize)
149  {
150  }
151 
152  uint8_t* getBuffer() final
153  {
154  return m_buffer.get();
155  }
156 
157  size_t getBufferSize() final
158  {
159  return m_typeSize;
160  }
161 
162  size_t serialize() final
163  {
164  return m_typeSize;
165  }
166 
167  void deserialize(size_t) final
168  {
169  }
170 };
171 
173 {
174  static constexpr char LOG_TAG[] = "ChannelNvSciPacketDefault";
175 
176 public:
177  ChannelNvSciPacketDefault(size_t typeSize)
178  : m_typeSize{typeSize}
179  {
180  }
181 
182  uint32_t getNumBuffers() const final
183  {
184  return 1U;
185  }
186 
187  void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList& attrList) const final
188  {
189  if (bufferIndex != 0U)
190  {
191  throw Exception(DW_INVALID_ARGUMENT, "ChannelNvSciPacketDefault: invalid buffer index");
192  }
193 
194  fillCpuPacketDataAttributes(attrList);
195  }
196 
197  void initializeFromNvSciBufObjs(dw::core::span<NvSciBufObj> bufs)
198  {
199  if (bufs.size() != getNumBuffers())
200  {
201  throw Exception(DW_INVALID_ARGUMENT, "ChannelNvSciPacketDefault: wrong number of buffers passed");
202  }
203 
204  FRWK_CHECK_NVSCI_ERROR(NvSciBufObjGetCpuPtr(bufs[0], &m_data));
205  }
206 
207  void pack() final
208  {
209  // noop
210  }
211 
212  void unpack() final
213  {
214  // noop
215  }
216 
218  {
219  return GenericData(m_data, m_typeSize);
220  }
221 
222 private:
223  void fillCpuPacketDataAttributes(NvSciBufAttrList& output) const
224  {
225  const NvSciBufType bufferType = NvSciBufType_RawBuffer;
226  const bool cpuAccessFlag = true;
227  const uint64_t rawAlign = 4;
228  const NvSciBufAttrValAccessPerm permissions = NvSciBufAccessPerm_ReadWrite;
229 
230  dw::core::Array<NvSciBufAttrKeyValuePair, 5> rawBufferAttributes =
231  {{{NvSciBufGeneralAttrKey_Types, &bufferType, sizeof(bufferType)},
232  {NvSciBufGeneralAttrKey_NeedCpuAccess, &cpuAccessFlag, sizeof(cpuAccessFlag)},
233  {NvSciBufGeneralAttrKey_RequiredPerm, &permissions, sizeof(permissions)},
234  {NvSciBufRawBufferAttrKey_Align, &rawAlign, sizeof(rawAlign)},
235  {NvSciBufRawBufferAttrKey_Size, &m_typeSize, sizeof(m_typeSize)}}};
236 
237  FRWK_CHECK_NVSCI_ERROR(NvSciBufAttrListSetAttrs(output,
238  rawBufferAttributes.data(),
239  rawBufferAttributes.size()));
240  }
241 
242  size_t m_typeSize{};
243  void* m_data{};
244 };
245 
247 {
248 public:
249  using OnDataReady = dw::core::Function<void()>;
250 
251  virtual std::unique_ptr<IChannelPacket> makePacket(ChannelPacketTypeID id, ChannelType channelType, GenericData ref) = 0;
252 
253  virtual std::shared_ptr<ChannelLocalShmemRegistry> getChannelLocalShmemRegistry() = 0;
254 
255  virtual NvSciBufModule getNvSciBufModule() = 0;
256 
257  virtual NvSciSyncModule getNvSciSyncModule() = 0;
258 
259  virtual void dispatchDataReady(void* opaque, OnDataReady onDataReady) = 0;
260 };
261 using ChannelPacketFactoryPtr = std::shared_ptr<IChannelPacketFactory>;
262 
263 } // namespace framework
264 } // namespace dw
265 
266 #endif // DW_FRAMEWORK_ICHANNEL_PACKET_HPP_
uint32_t ChannelPacketTypeID
std::shared_ptr< IChannelPacketFactory > ChannelPacketFactoryPtr
virtual ~IChannelPacket()=default
#define FRWK_CHECK_NVSCI_ERROR(e)
Definition: Exception.hpp:354
enum ChannelType :uint8_t { DW_CHANNEL_TYPE_SHMEM_LOCAL=0, DW_CHANNEL_TYPE_SHMEM_REMOTE=1, DW_CHANNEL_TYPE_EGLSTREAM=2, DW_CHANNEL_TYPE_SOCKET=3, DW_CHANNEL_TYPE_DDS=4, DW_CHANNEL_TYPE_NVSCI=5, } ChannelType
void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList &attrList) const final
NvSciCallbacks & getNvSciCallbacks()
virtual GenericData getGenericData()=0
std::unique_ptr< uint8_t[]> m_buffer
Definition: Exception.hpp:46
dw::core::Function< void()> OnDataReady
void initializeFromNvSciBufObjs(dw::core::span< NvSciBufObj > bufs)
SocketCallbacks & getSocketCallbacks()