API
 
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1#include "/opt/MagAOX/vendor/TensorRT-10.0.0.6/include/NvInfer.h"
2#include <algorithm>
3#include <cassert>
4#include <chrono>
5#include <cmath>
6#include <cstring>
7#include <cuda_runtime_api.h>
8#include <fstream>
9#include <functional>
10#include <iomanip>
11#include <iostream>
12#include <iterator>
13#include <map>
14#include <memory>
15#include <new>
16#include <numeric>
17#include <ratio>
18#include <sstream>
19#include <string>
20#include <utility>
21#include <vector>
22#include <chrono>
23
24// Utility Timer
25template <typename Clock = std::chrono::high_resolution_clock> class Stopwatch {
26 typename Clock::time_point start_point;
27
28public:
29 Stopwatch() : start_point(Clock::now()) {}
30
31 // Returns elapsed time
32 template <typename Rep = typename Clock::duration::rep, typename Units = typename Clock::duration> Rep elapsedTime() const {
33 std::atomic_thread_fence(std::memory_order_relaxed);
34 auto counted_time = std::chrono::duration_cast<Units>(Clock::now() - start_point).count();
35 std::atomic_thread_fence(std::memory_order_relaxed);
36 return static_cast<Rep>(counted_time);
37 }
38};
39
40
41
42class Logger : public nvinfer1::ILogger
43{
44 void log(Severity severity, const char* msg) noexcept override
45 {
46 // Only log messages of type INFO and above
47 if (severity <= Severity::kINFO)
48 {
49 std::cout << msg << std::endl;
50 }
51 }
52};
53
54inline bool doesFileExist(const std::string &filepath) {
55 std::ifstream f(filepath.c_str());
56 return f.good();
57}
58
59inline void parseCSV(std::string filepath, float * fileData)
60{
61 // Open the CSV file
62 std::ifstream file(filepath);
63 if (!file.is_open()) {
64 std::cout << "Error opening file!" << std::endl;
65 }
66
67 std::string line;
68 int i =0;
69 int arrayLength = sizeof(&fileData) / sizeof(float);
70 while (i<arrayLength)
71 {
72 getline(file, line);
73 std::stringstream ss(line);
74 std::string cell;
75 while (getline(ss, cell, ',')) {
76 fileData[i] = std::stof(cell);
77 i++;
78 }
79 }
80 file.close();
81}
82
83//! Locate path to file, given its filename or filepath suffix and possible dirs it might lie in.
84//! Function will also walk back MAX_DEPTH dirs from CWD to check for such a file path.
85inline std::string locateFile(
86 const std::string& filepathSuffix, const std::vector<std::string>& directories, bool reportError = true)
87{
88 const int MAX_DEPTH{10};
89 bool found{false};
90 std::string filepath;
91
92 for (auto& dir : directories)
93 {
94 if (!dir.empty() && dir.back() != '/')
95 {
96#ifdef _MSC_VER
97 filepath = dir + "\\" + filepathSuffix;
98#else
99 filepath = dir + "/" + filepathSuffix;
100#endif
101 }
102 else
103 {
104 filepath = dir + filepathSuffix;
105 }
106
107 for (int i = 0; i < MAX_DEPTH && !found; i++)
108 {
109 const std::ifstream checkFile(filepath);
110 found = checkFile.is_open();
111 if (found)
112 {
113 break;
114 }
115
116 filepath = "../" + filepath; // Try again in parent dir
117 }
118
119 if (found)
120 {
121 break;
122 }
123
124 filepath.clear();
125 }
126
127 // Could not find the file
128 if (filepath.empty())
129 {
130 const std::string dirList = std::accumulate(directories.begin() + 1, directories.end(), directories.front(),
131 [](const std::string& a, const std::string& b) { return a + "\n\t" + b; });
132 std::cout << "Could not find " << filepathSuffix << " in data directories:\n\t" << dirList << std::endl;
133
134 if (reportError)
135 {
136 std::cout << "&&&& FAILED" << std::endl;
137 exit(EXIT_FAILURE);
138 }
139 }
140
141 return filepath;
142}
143
144
145inline void enableDLA(
146 nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, int useDLACore, bool allowGPUFallback = true)
147{
148 if (useDLACore >= 0)
149 {
150 if (builder->getNbDLACores() == 0)
151 {
152 std::cerr << "Trying to use DLA core " << useDLACore << " on a platform that doesn't have any DLA cores"
153 << std::endl;
154 assert("Error: use DLA core on a platfrom that doesn't have any DLA cores" && false);
155 }
156 if (allowGPUFallback)
157 {
158 config->setFlag(nvinfer1::BuilderFlag::kGPU_FALLBACK);
159 }
160 if (!config->getFlag(nvinfer1::BuilderFlag::kINT8))
161 {
162 // User has not requested INT8 Mode.
163 // By default run in FP16 mode. FP32 mode is not permitted.
164 config->setFlag(nvinfer1::BuilderFlag::kFP16);
165 }
166 config->setDefaultDeviceType(nvinfer1::DeviceType::kDLA);
167 config->setDLACore(useDLACore);
168 }
169}
170
172{
173 template <typename T>
174 void operator()(T* obj) const
175 {
176 delete obj;
177 }
178};
179
180static auto StreamDeleter = [](cudaStream_t* pStream) {
181 if (pStream)
182 {
183 static_cast<void>(cudaStreamDestroy(*pStream));
184 delete pStream;
185 }
186};
187
188std::unique_ptr<cudaStream_t, decltype(StreamDeleter)> makeCudaStream()
189{
190 std::unique_ptr<cudaStream_t, decltype(StreamDeleter)> pStream(new cudaStream_t, StreamDeleter);
191 if (cudaStreamCreateWithFlags(pStream.get(), cudaStreamNonBlocking) != cudaSuccess)
192 {
193 pStream.reset(nullptr);
194 }
195
196 return pStream;
197}
void log(Severity severity, const char *msg) noexcept override
Definition util.hpp:44
Clock::time_point start_point
Definition util.hpp:26
Stopwatch()
Definition util.hpp:29
Rep elapsedTime() const
Definition util.hpp:32
void operator()(T *obj) const
Definition util.hpp:174
std::string locateFile(const std::string &filepathSuffix, const std::vector< std::string > &directories, bool reportError=true)
Definition util.hpp:85
bool doesFileExist(const std::string &filepath)
Definition util.hpp:54
static auto StreamDeleter
Definition util.hpp:180
void parseCSV(std::string filepath, float *fileData)
Definition util.hpp:59
void enableDLA(nvinfer1::IBuilder *builder, nvinfer1::IBuilderConfig *config, int useDLACore, bool allowGPUFallback=true)
Definition util.hpp:145
std::unique_ptr< cudaStream_t, decltype(StreamDeleter)> makeCudaStream()
Definition util.hpp:188