forked from zerollzeng/tiny-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
102 lines (89 loc) · 3.47 KB
/
utils.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
/*
* @Email: [email protected]
* @Author: zerollzeng
* @Date: 2019-11-12 11:53:56
* @LastEditors: zerollzeng
* @LastEditTime: 2019-12-06 17:17:13
*/
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <algorithm>
#include <numeric>
#include "cuda_runtime.h"
#include <NvInfer.h>
#define UNUSED(unusedVariable) (void)(unusedVariable)
// suppress compiler warning: unused parameter
inline int64_t volume(const nvinfer1::Dims& d)
{
return std::accumulate(d.d, d.d + d.nbDims, 1, std::multiplies<int64_t>());
}
inline unsigned int getElementSize(nvinfer1::DataType t)
{
switch (t)
{
case nvinfer1::DataType::kINT32: return 4;
case nvinfer1::DataType::kFLOAT: return 4;
case nvinfer1::DataType::kHALF: return 2;
case nvinfer1::DataType::kINT8: return 1;
default: throw std::runtime_error("Invalid DataType.");
}
}
#ifndef CUDA_CHECK
#define CUDA_CHECK(callstr) \
{ \
cudaError_t error_code = callstr; \
if (error_code != cudaSuccess) { \
std::cerr << "CUDA error " << error_code << " at " << __FILE__ << ":" << __LINE__ << std::endl; \
exit(0); \
} \
}
#endif
inline void* safeCudaMalloc(size_t memSize) {
void* deviceMem;
CUDA_CHECK(cudaMalloc(&deviceMem, memSize));
if (deviceMem == nullptr) {
std::cerr << "Out of memory" << std::endl;
exit(1);
}
return deviceMem;
}
inline void safeCudaFree(void* deviceMem) {
CUDA_CHECK(cudaFree(deviceMem));
}
inline void error(const std::string& message, const int line, const std::string& function, const std::string& file) {
std::cout << message << " at " << line << " in " << function << " in " << file << std::endl;
}
#define COMPILE_TEMPLATE_BASIC_TYPES_CLASS(className) COMPILE_TEMPLATE_BASIC_TYPES(className, class)
#define COMPILE_TEMPLATE_BASIC_TYPES_STRUCT(className) COMPILE_TEMPLATE_BASIC_TYPES(className, struct)
#define COMPILE_TEMPLATE_BASIC_TYPES(className, classType) \
template classType className<char>; \
template classType className<signed char>; \
template classType className<short>; \
template classType className<int>; \
template classType className<long>; \
template classType className<long long>; \
template classType className<unsigned char>; \
template classType className<unsigned short>; \
template classType className<unsigned int>; \
template classType className<unsigned long>; \
template classType className<unsigned long long>; \
template classType className<float>; \
template classType className<double>; \
template classType className<long double>
// const auto CUDA_NUM_THREADS = 512u;
// inline unsigned int getNumberCudaBlocks(const unsigned int totalRequired,
// const unsigned int numberCudaThreads = CUDA_NUM_THREADS)
// {
// return (totalRequired + numberCudaThreads - 1) / numberCudaThreads;
// }
struct YoloKernel;
static constexpr int LOCATIONS = 4;
struct alignas(float) Detection{
//x y w h
float bbox[LOCATIONS];
//float objectness;
int classId;
float prob;
};
#endif