-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilsn.h
64 lines (58 loc) · 1.5 KB
/
utilsn.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
#pragma once
#include<iostream>
#include<vector>
#include<algorithm>
#include<cudnn.h>
#include<assert.h>
#include<NvInfer.h>
using namespace std;
#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__; \
assert(0); \
} \
}
#endif // !CUDA_CHECK
namespace Tn {
class Profiler :public nvinfer1::IProfiler
{
public:
void printLayerTimes(int itrationsTimes) {
float totalTime = 0;
for (size_t i = 0; i < mProfile.size(); i++)
{
printf("%-40.40s %4.3fms\n", mProfile[i].first.c_str(), mProfile[i].second / itrationsTimes);
totalTime += mProfile[i].second;
}
printf("Time over all layers: %4.3f\n", totalTime / itrationsTimes);
}
private:
typedef std::pair<std::string, float> Record;
std::vector<Record> mProfile;
virtual void reportLayerTime(const char* layerName, float ms) {
auto record = std::find_if(mProfile.begin(), mProfile.end(), [&](const Record& r) {return r.first == layerName; });
if (record == mProfile.end())
{
mProfile.push_back(std::make_pair(layerName, ms));
}
else
{
record->second += ms;
}
}
};
template<typename T>
void write(char*& buffer, const T& val) {
*reinterpret_cast<T*>(buffer) = val;
buffer += sizeof(T);
}
template<typename T>
void read(const char*& buffer, T& val) {
val = *reinterpret_cast<const T*>(buffer);
buffer += sizeof(T);
}
};