NVIDIA DeepStream SDK API Reference

6.2 Release
infer_batch_buffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
19 #ifndef __NVDSINFERSERVER_BATCH_BUFFER_H__
20 #define __NVDSINFERSERVER_BATCH_BUFFER_H__
21 
22 #include <algorithm>
23 #include <set>
24 #include <unordered_map>
25 #include "nvbufsurftransform.h"
26 #include "infer_common.h"
27 #include "infer_datatypes.h"
28 
29 namespace nvdsinferserver {
30 
35 protected:
36  explicit BaseBatchBuffer(uint32_t batchSize) : m_BatchSize(batchSize) {}
37 
38 public:
39  ~BaseBatchBuffer() override = default;
40  const InferBufferDescription& getBufDesc() const final { return m_Desc; }
41  uint32_t getBatchSize() const final { return m_BatchSize; }
42  uint64_t getTotalBytes() const override
43  {
44  assert(m_Desc.dataType != InferDataType::kNone);
45  assert(m_Desc.elementSize >= 0);
46  uint32_t b = getBatchSize();
47  return m_Desc.elementSize * m_Desc.dims.numElements * (b ? b : 1);
48  }
49  void setBufDesc(const InferBufferDescription& desc) { m_Desc = desc; }
50  InferBufferDescription& mutableBufDesc() { return m_Desc; }
51  virtual void setBatchSize(uint32_t size) { m_BatchSize = size; }
52  const SharedCuEvent& cuEvent() const { return m_CuEvent; }
53  void setCuEvent(SharedCuEvent e) { m_CuEvent = std::move(e); }
54  void setSyncObj(NvBufSurfTransformSyncObj_t SyncObj) { assert(m_SyncObj==nullptr); m_SyncObj = SyncObj; }
55  NvBufSurfTransformSyncObj_t& getSyncObj() { return m_SyncObj; }
57  {
58  if (m_SyncObj)
59  {
60  NvBufSurfTransformSyncObjWait (m_SyncObj, -1);
62  m_SyncObj = nullptr;
63  }
64  }
65 
66  // attached buffers
68  {
69  m_Attaches.emplace_back(std::move(buf));
70  assert(!hasAttachLoop());
71  }
72  void detach() { m_Attaches.clear(); }
73  bool hasAttachedBufs() const { return !m_Attaches.empty();}
74  const std::vector<SharedBatchBuf>& attachedBufs() const
75  {
76  return m_Attaches;
77  }
78  std::vector<SharedBatchBuf>& mutableAttachedBufs() { return m_Attaches; }
79  bool hasAttachLoop() const
80  {
81  std::set<BaseBatchBuffer*> allAttached;
82  for (auto const& buf : m_Attaches) {
83  assert(buf);
84  if (allAttached.count(buf.get())) {
85  return true;
86  }
87  allAttached.emplace(buf.get());
88  }
89  return false;
90  }
91 
92  void setBufId(uint64_t id) { m_BufId = id; }
93  uint64_t bufId() const { return m_BufId; }
94 
95 private:
98  uint32_t m_BatchSize = 0;
99  SharedCuEvent m_CuEvent;
100  std::vector<SharedBatchBuf> m_Attaches;
101  uint64_t m_BufId = UINT64_C(0); // for debug and track
104  NvBufSurfTransformSyncObj_t m_SyncObj = nullptr;
105 };
106 
110 class BaseBatchArray : public IBatchArray {
111 public:
112  ~BaseBatchArray() override = default;
113  uint32_t getSize() const final { return m_Bufs.size(); }
114  const IBatchBuffer* getBuffer(uint32_t arrayIdx) const final
115  {
116  assert(arrayIdx < (uint32_t)m_Bufs.size());
117  assert(m_Bufs.at(arrayIdx).get());
118  return m_Bufs.at(arrayIdx).get();
119  }
120  SharedIBatchBuffer getSafeBuf(uint32_t arrayIdx) const final
121  {
122  assert(arrayIdx < (uint32_t)m_Bufs.size());
123  assert(m_Bufs.at(arrayIdx).get());
124  return buf(arrayIdx);
125  }
127  {
128  SharedBatchBuf castBuf =
129  std::dynamic_pointer_cast<BaseBatchBuffer>(buf);
130  assert(castBuf);
131  addBuf(castBuf);
132  }
133  const IOptions* getOptions() const final { return m_Options.get(); }
134  SharedIOptions getSafeOptions() const { return m_Options; }
135  void setIOptions(SharedIOptions o) final { setOptions(o); }
136 
137  const std::vector<SharedBatchBuf>& bufs() const { return m_Bufs; }
138  std::vector<SharedBatchBuf>& mutableBufs() { return m_Bufs; }
139  void addBuf(SharedBatchBuf buf) { assert(buf); m_Bufs.emplace_back(std::move(buf)); }
140  const SharedBatchBuf& buf(uint32_t idx) const
141  {
142  assert(idx < (uint32_t)m_Bufs.size());
143  return m_Bufs.at(idx);
144  }
145  SharedBatchBuf& buf(uint32_t idx)
146  {
147  assert(idx < (uint32_t)m_Bufs.size());
148  return m_Bufs.at(idx);
149  }
150  const SharedCuEvent& cuEvent() const { return m_CuEvent; }
151  void setCuEvent(SharedCuEvent e) { m_CuEvent = std::move(e); }
152  int findFirstGpuId() const {
153  int gpuId = -1;
154  auto iter = std::find_if(m_Bufs.cbegin(), m_Bufs.cend(),
155  [&gpuId](const SharedBatchBuf& buf) {
156  assert(buf);
157  const InferBufferDescription& desc = buf->getBufDesc();
158  if (desc.memType == InferMemType::kGpuCuda) {
159  gpuId = desc.devId;
160  return true;
161  }
162  return false;
163  });
164  (void) iter;
165 
166  assert((iter != m_Bufs.cend() && gpuId >= 0) ||
167  (iter == m_Bufs.cend() && gpuId == -1));
168  return gpuId;
169  }
170 
171  void setBufId(uint64_t id) { m_BufId = id; }
172  uint64_t bufId() const { return m_BufId; }
173 
174  // set options
175  void setOptions(SharedIOptions o) { m_Options = std::move(o); }
176 
177 private:
178  std::vector<SharedBatchBuf> m_Bufs;
179  SharedCuEvent m_CuEvent;
180  uint64_t m_BufId = UINT64_C(0); // for debug and track
181 
182  SharedOptions m_Options;
183 };
184 
185 extern void normalizeDims(InferDims& dims);
186 
191 public:
193  void* bufBase, size_t bufBytes, const InferBufferDescription& desc,
194  uint32_t batchSize)
195  : BaseBatchBuffer(batchSize), m_BufBase(bufBase), m_BufBytes(bufBytes)
196  {
197  setBufDesc(desc);
198  normalizeDims(mutableBufDesc().dims);
199  }
200  void* getBufPtr(uint32_t batchIdx) const override {
201  assert(batchIdx == 0 || batchIdx < getBatchSize());
202  const InferBufferDescription& desc = getBufDesc();
203  assert(batchIdx <= 0 || desc.dataType != InferDataType::kString);
204  batchIdx = std::max(batchIdx, 0U);
205  return (void*)((uint8_t*)m_BufBase +
206  desc.elementSize * desc.dims.numElements * batchIdx);
207  }
208  uint64_t getTotalBytes() const final { return m_BufBytes; }
209  void* basePtr() { return m_BufBase; }
210 
211 private:
212  mutable void* m_BufBase = nullptr;
213  size_t m_BufBytes = 0;
214 };
215 
217 public:
218  template <typename BufPtr>
219  WrapCBatchBuffer(BufPtr buf) : m_Impl(std::move(buf)) {
220  assert(m_Impl);
221  }
222 
223  ~WrapCBatchBuffer() final = default;
224  const InferBufferDescription& getBufDesc() const final {
225  return m_Impl->getBufDesc();
226  }
227  void* getBufPtr(uint32_t batchIdx) const final {
228  return m_Impl->getBufPtr(batchIdx);
229  }
230  uint32_t getBatchSize() const final { return m_Impl->getBatchSize(); }
231  uint64_t getTotalBytes() const final { return m_Impl->getTotalBytes(); }
232 
233 private:
234  SharedBatchBuf m_Impl;
235 };
236 
237 } // namespace nvdsinferserver
238 
239 #endif
nvdsinferserver::BaseBatchArray::buf
const SharedBatchBuf & buf(uint32_t idx) const
Definition: infer_batch_buffer.h:140
nvdsinferserver
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: infer_custom_process.h:23
nvdsinferserver::BaseBatchBuffer::getBatchSize
uint32_t getBatchSize() const final
Definition: infer_batch_buffer.h:41
nvdsinferserver::BaseBatchArray::getSize
uint32_t getSize() const final
Definition: infer_batch_buffer.h:113
nvdsinferserver::BaseBatchArray::~BaseBatchArray
~BaseBatchArray() override=default
nvdsinferserver::BaseBatchBuffer::setBufId
void setBufId(uint64_t id)
Definition: infer_batch_buffer.h:92
nvdsinferserver::SharedBatchBuf
std::shared_ptr< BaseBatchBuffer > SharedBatchBuf
Common buffer interfaces (internal).
Definition: infer_common.h:71
nvdsinferserver::BaseBatchArray::getOptions
const IOptions * getOptions() const final
Definition: infer_batch_buffer.h:133
NvBufSurfTransformSyncObj_t
struct NvBufSurfTransformSyncObj * NvBufSurfTransformSyncObj_t
Holds the information about synchronization objects for asynchronous transform/composite APIs.
Definition: nvbufsurftransform.h:254
nvdsinferserver::SharedIBatchBuffer
std::shared_ptr< IBatchBuffer > SharedIBatchBuffer
Definition: infer_datatypes.h:204
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::BaseBatchArray::setBufId
void setBufId(uint64_t id)
Definition: infer_batch_buffer.h:171
NvBufSurfTransformSyncObjDestroy
NvBufSurfTransform_Error NvBufSurfTransformSyncObjDestroy(NvBufSurfTransformSyncObj_t *sync_obj)
Destroy the synchroization object.
nvdsinferserver::RefBatchBuffer
A batch buffer with allocated memory.
Definition: infer_batch_buffer.h:190
nvdsinferserver::BaseBatchBuffer::cuEvent
const SharedCuEvent & cuEvent() const
Definition: infer_batch_buffer.h:52
nvdsinferserver::RefBatchBuffer::RefBatchBuffer
RefBatchBuffer(void *bufBase, size_t bufBytes, const InferBufferDescription &desc, uint32_t batchSize)
Definition: infer_batch_buffer.h:192
nvdsinferserver::BaseBatchBuffer::getSyncObj
NvBufSurfTransformSyncObj_t & getSyncObj()
Definition: infer_batch_buffer.h:55
nvdsinferserver::normalizeDims
void normalizeDims(InferDims &dims)
nvdsinferserver::BaseBatchArray::setIOptions
void setIOptions(SharedIOptions o) final
Definition: infer_batch_buffer.h:135
nvdsinferserver::RefBatchBuffer::basePtr
void * basePtr()
Definition: infer_batch_buffer.h:209
nvdsinferserver::BaseBatchArray::getBuffer
const IBatchBuffer * getBuffer(uint32_t arrayIdx) const final
Definition: infer_batch_buffer.h:114
nvdsinferserver::BaseBatchBuffer::attachedBufs
const std::vector< SharedBatchBuf > & attachedBufs() const
Definition: infer_batch_buffer.h:74
nvdsinferserver::RefBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const final
Definition: infer_batch_buffer.h:208
nvdsinferserver::BaseBatchBuffer::detach
void detach()
Definition: infer_batch_buffer.h:72
nvdsinferserver::IBatchBuffer
Interface class for a batch buffer.
Definition: infer_datatypes.h:211
nvdsinferserver::BaseBatchBuffer::waitForSyncObj
void waitForSyncObj()
Definition: infer_batch_buffer.h:56
nvdsinferserver::BaseBatchArray::setCuEvent
void setCuEvent(SharedCuEvent e)
Definition: infer_batch_buffer.h:151
nvdsinferserver::BaseBatchBuffer::setBatchSize
virtual void setBatchSize(uint32_t size)
Definition: infer_batch_buffer.h:51
nvdsinferserver::BaseBatchArray::cuEvent
const SharedCuEvent & cuEvent() const
Definition: infer_batch_buffer.h:150
nvdsinferserver::BaseBatchArray::addBuf
void addBuf(SharedBatchBuf buf)
Definition: infer_batch_buffer.h:139
nvdsinferserver::BaseBatchBuffer::bufId
uint64_t bufId() const
Definition: infer_batch_buffer.h:93
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::BaseBatchArray::mutableBufs
std::vector< SharedBatchBuf > & mutableBufs()
Definition: infer_batch_buffer.h:138
nvdsinferserver::BaseBatchArray::getSafeBuf
SharedIBatchBuffer getSafeBuf(uint32_t arrayIdx) const final
Definition: infer_batch_buffer.h:120
nvdsinferserver::BaseBatchArray::setOptions
void setOptions(SharedIOptions o)
Definition: infer_batch_buffer.h:175
nvdsinferserver::BaseBatchBuffer::getBufDesc
const InferBufferDescription & getBufDesc() const final
Definition: infer_batch_buffer.h:40
nvdsinferserver::WrapCBatchBuffer
Definition: infer_batch_buffer.h:216
nvdsinferserver::WrapCBatchBuffer::getBatchSize
uint32_t getBatchSize() const final
Definition: infer_batch_buffer.h:230
nvdsinferserver::BaseBatchArray::bufId
uint64_t bufId() const
Definition: infer_batch_buffer.h:172
nvdsinferserver::SharedOptions
std::shared_ptr< IOptions > SharedOptions
Definition: infer_common.h:73
nvdsinferserver::BaseBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const override
Definition: infer_batch_buffer.h:42
nvdsinferserver::WrapCBatchBuffer::WrapCBatchBuffer
WrapCBatchBuffer(BufPtr buf)
Definition: infer_batch_buffer.h:219
nvdsinferserver::WrapCBatchBuffer::getBufPtr
void * getBufPtr(uint32_t batchIdx) const final
Definition: infer_batch_buffer.h:227
NvBufSurfTransformSyncObjWait
NvBufSurfTransform_Error NvBufSurfTransformSyncObjWait(NvBufSurfTransformSyncObj_t sync_obj, uint32_t time_out)
Wait on the synchroization object.
nvdsinferserver::WrapCBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const final
Definition: infer_batch_buffer.h:231
nvdsinferserver::BaseBatchBuffer::setSyncObj
void setSyncObj(NvBufSurfTransformSyncObj_t SyncObj)
Definition: infer_batch_buffer.h:54
nvdsinferserver::BaseBatchBuffer::mutableAttachedBufs
std::vector< SharedBatchBuf > & mutableAttachedBufs()
Definition: infer_batch_buffer.h:78
nvdsinferserver::InferBufferDescription::dataType
InferDataType dataType
Datatype associated with the buffer.
Definition: infer_datatypes.h:180
nvdsinferserver::BaseBatchBuffer::BaseBatchBuffer
BaseBatchBuffer(uint32_t batchSize)
Definition: infer_batch_buffer.h:36
nvdsinferserver::BaseBatchBuffer::hasAttachedBufs
bool hasAttachedBufs() const
Definition: infer_batch_buffer.h:73
INFER_EXPORT_API
Definition: infer_utils.h:33
nvdsinferserver::RefBatchBuffer::getBufPtr
void * getBufPtr(uint32_t batchIdx) const override
Definition: infer_batch_buffer.h:200
nvdsinferserver::IOptions
Definition: infer_ioptions.h:53
nvdsinferserver::BaseBatchArray::appendIBatchBuf
void appendIBatchBuf(SharedIBatchBuffer buf) final
Definition: infer_batch_buffer.h:126
nvdsinferserver::BaseBatchBuffer::setBufDesc
void setBufDesc(const InferBufferDescription &desc)
Definition: infer_batch_buffer.h:49
nvdsinferserver::SharedIOptions
std::shared_ptr< IOptions > SharedIOptions
Definition: infer_datatypes.h:206
nvdsinferserver::BaseBatchArray
The base class for array of batch buffers.
Definition: infer_batch_buffer.h:110
nvdsinferserver::InferDataType::kNone
@ kNone
nvdsinferserver::BaseBatchBuffer::setCuEvent
void setCuEvent(SharedCuEvent e)
Definition: infer_batch_buffer.h:53
nvdsinferserver::InferDims::numElements
unsigned int numElements
Number of elements in the layer including all dimensions.
Definition: infer_datatypes.h:153
nvdsinferserver::BaseBatchArray::buf
SharedBatchBuf & buf(uint32_t idx)
Definition: infer_batch_buffer.h:145
nvdsinferserver::InferBufferDescription::dims
InferDims dims
Dimensions of the tensor.
Definition: infer_datatypes.h:184
nvdsinferserver::BaseBatchArray::getSafeOptions
SharedIOptions getSafeOptions() const
Definition: infer_batch_buffer.h:134
nvdsinferserver::SharedCuEvent
std::shared_ptr< CudaEvent > SharedCuEvent
Definition: infer_common.h:86
nvdsinferserver::BaseBatchBuffer
The base class for batch buffers.
Definition: infer_batch_buffer.h:34
nvdsinferserver::InferBufferDescription::elementSize
uint32_t elementSize
Per element bytes, except kString (with elementSize is 0)
Definition: infer_datatypes.h:188
nvdsinferserver::BaseBatchBuffer::attach
void attach(SharedBatchBuf buf)
Definition: infer_batch_buffer.h:67
nvdsinferserver::BaseBatchArray::findFirstGpuId
int findFirstGpuId() const
Definition: infer_batch_buffer.h:152
nvdsinferserver::IBatchArray
Interface class for an array of batch buffers.
Definition: infer_datatypes.h:227
nvbufsurftransform.h
nvdsinferserver::BaseBatchBuffer::mutableBufDesc
InferBufferDescription & mutableBufDesc()
Definition: infer_batch_buffer.h:50
nvdsinferserver::BaseBatchBuffer::hasAttachLoop
bool hasAttachLoop() const
Definition: infer_batch_buffer.h:79
nvdsinferserver::InferMemType::kNone
@ kNone
nvdsinferserver::BaseBatchArray::bufs
const std::vector< SharedBatchBuf > & bufs() const
Definition: infer_batch_buffer.h:137
nvdsinferserver::InferBufferDescription
Holds the information about a inference buffer.
Definition: infer_datatypes.h:168