-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbuf.h
218 lines (191 loc) · 6.55 KB
/
jbuf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef _JBUF_H_
#define _JBUF_H_
#include "NvInfer.h"
#include "half.h"
#include "common.h"
#include <cuda_runtime_api.h>
#include <cassert>
#include <iostream>
#include <iterator>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
#include <new>
#include "nvdsinfer.h"
template <typename AllocFunc, typename FreeFunc>
class GenericBuffer
{
public:
//!
//! \brief Construct an empty buffer.
//!
GenericBuffer()
: mByteSize(0)
, mBuffer(nullptr)
{
}
//!
//! \brief Construct a buffer with the specified allocation size in bytes.
//!
GenericBuffer(size_t size)
: mByteSize(size)
{
if (!allocFn(&mBuffer, mByteSize))
throw std::bad_alloc();
}
GenericBuffer(GenericBuffer&& buf)
: mByteSize(buf.mByteSize)
, mBuffer(buf.mBuffer)
{
buf.mByteSize = 0;
buf.mBuffer = nullptr;
}
GenericBuffer& operator=(GenericBuffer&& buf)
{
if (this != &buf)
{
freeFn(mBuffer);
mByteSize = buf.mByteSize;
mBuffer = buf.mBuffer;
buf.mByteSize = 0;
buf.mBuffer = nullptr;
}
return *this;
}
//!
//! \brief Returns pointer to underlying array.
//!
void* data() { return mBuffer; }
//!
//! \brief Returns pointer to underlying array.
//!
const void* data() const { return mBuffer; }
//!
//! \brief Returns the size (in bytes) of the buffer.
//!
size_t size() const { return mByteSize; }
~GenericBuffer()
{
freeFn(mBuffer);
}
private:
size_t mByteSize;
void* mBuffer;
AllocFunc allocFn;
FreeFunc freeFn;
};
class ManagedAllocator
{
public:
bool operator()(void** ptr, size_t size) const { return cudaMallocManaged(ptr, size) == cudaSuccess; }
};
class ManagedFree
{
public:
void operator()(void* ptr) const { cudaFree(ptr); }
};
inline std::string dims_to_str(const nvinfer1::Dims& d) {
std::stringstream s;
for (int32_t i = 0; i < d.nbDims - 1; i++) {
s << d.d[i] << " * ";
}
s << d.d[d.nbDims - 1];
return s.str();
}
using ManagedBuffer = GenericBuffer<ManagedAllocator, ManagedFree>;
class UnifiedBufManager {
public:
static const size_t kINVALID_SIZE_VALUE = ~size_t(0);
//!
//! \brief Create a BufferManager for handling buffer interactions with engine.
//!
UnifiedBufManager(std::shared_ptr<nvinfer1::ICudaEngine> engine, const int& batchSize)
: mEngine(engine)
, mBatchSize(batchSize)
{
for (int i = 0; i < mEngine->getNbBindings(); i++)
{
// Create host and device buffers
auto dims = mEngine->getBindingDimensions(i);
size_t vol = samplesCommon::volume(dims);
auto dataType = mEngine->getBindingDataType(i);
size_t elementSize = samplesCommon::getElementSize(dataType);
size_t allocationSize = static_cast<size_t>(mBatchSize) * vol * elementSize;
std::unique_ptr<ManagedBuffer> manBuf{new ManagedBuffer(allocationSize)};
mDeviceBindings.emplace_back(manBuf->data());
NvDsInferLayerInfo layerInfo;
layerInfo.buffer = manBuf->data();
layerInfo.dataType = NvDsInferDataType(int(dataType));
layerInfo.layerName = mEngine->getBindingName(i);
std::cerr << "binding layer@" << i << ": " << layerInfo.layerName << " " << allocationSize << "[" << mBatchSize << " * " << dims_to_str(dims) << "]" <<endl;
layerInfo.bindingIndex = i;
layerInfo.dims.numDims = dims.nbDims;
layerInfo.dims.numElements = 1;
layerInfo.isInput = 0;
for (int32_t i = 0; i < int32_t(layerInfo.dims.numDims); i++) {
layerInfo.dims.d[i] = dims.d[i];
layerInfo.dims.numElements *= dims.d[i];
}
mManagedBuffers.emplace_back(std::move(manBuf));
mLayerInfos.emplace_back(layerInfo);
}
}
std::vector<void*>& getDeviceBindings() { return mDeviceBindings; }
const std::vector<void*>& getDeviceBindings() const { return mDeviceBindings; }
size_t size(const std::string& tensorName) const
{
int index = mEngine->getBindingIndex(tensorName.c_str());
if (index == -1)
return kINVALID_SIZE_VALUE;
return mManagedBuffers[index]->size();
}
/*void dumpBuffer(std::ostream& os, const std::string& tensorName)
{
int index = mEngine->getBindingIndex(tensorName.c_str());
if (index == -1)
{
os << "Invalid tensor name" << std::endl;
return;
}
void* buf = mManagedBuffers[index]->data();
size_t bufSize = mManagedBuffers[index]->size();
nvinfer1::Dims bufDims = mEngine->getBindingDimensions(index);
size_t rowCount = static_cast<size_t>(bufDims.nbDims >= 1 ? bufDims.d[bufDims.nbDims - 1] : mBatchSize);
os << "[" << mBatchSize;
for (int i = 0; i < bufDims.nbDims; i++)
os << ", " << bufDims.d[i];
os << "]" << std::endl;
switch (mEngine->getBindingDataType(index))
{
case nvinfer1::DataType::kINT32: print<int32_t>(os, buf, bufSize, rowCount); break;
case nvinfer1::DataType::kFLOAT: print<float>(os, buf, bufSize, rowCount); break;
case nvinfer1::DataType::kHALF: print<half_float::half>(os, buf, bufSize, rowCount); break;
case nvinfer1::DataType::kINT8: assert(0 && "Int8 network-level input and output is not supported"); break;
}
}*/
~UnifiedBufManager() = default;
void* getBuffer(const std::string& tensorName) const
{
int index = mEngine->getBindingIndex(tensorName.c_str());
if (index == -1)
return nullptr;
return mManagedBuffers[index]->data();
}
NvDsInferLayerInfo getLayerInfo(const std::string& tensorName) const
{
int index = mEngine->getBindingIndex(tensorName.c_str());
if (index == -1) {
NvDsInferLayerInfo emp;
return emp;
}
return mLayerInfos[index];
}
private:
std::shared_ptr<nvinfer1::ICudaEngine> mEngine; //!< The pointer to the engine
int mBatchSize; //!< The batch size
std::vector<std::unique_ptr<ManagedBuffer>> mManagedBuffers; //!< The vector of pointers to managed buffers
std::vector<void*> mDeviceBindings; //!< The vector of device buffers needed for engine execution
std::vector<NvDsInferLayerInfo> mLayerInfos;
};
#endif