forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.cc
More file actions
135 lines (109 loc) · 4.1 KB
/
tool.cc
File metadata and controls
135 lines (109 loc) · 4.1 KB
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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#if defined(_WIN32)
#define NOMINMAX
#endif
#include "gpu_inst_count.hpp"
#include "utils.h"
using namespace gtpin;
using namespace gtpin_prof;
class GpuInstCountTxtWriter : public StreamGTPinDataWriter {
public:
using StreamGTPinDataWriter::StreamGTPinDataWriter;
bool Init() final {
if (sh == nullptr) {
sh = new StreamHolder(std::cout);
}
if (sh == nullptr) return false;
return true;
}
void Write(const std::shared_ptr<ProfilerData> res) {
auto gpuInstCountProfRes = std::dynamic_pointer_cast<GpuInstCountProfilerData>(res);
for (auto kerData : res->kernels) {
auto gpuInstCountKernRes = std::dynamic_pointer_cast<GpuInstCountKernelData>(kerData.second);
if (gpuInstCountKernRes->invocations.size() == 0) {
continue;
}
GetStream() << "=== " << gpuInstCountKernRes->kernelName << "(runs "
<< gpuInstCountKernRes->invocations.size() << " times) ===\n";
std::map<InstructionOffset, size_t> instCount;
size_t maxCount = 0;
for (auto invoc : gpuInstCountKernRes->invocations) {
for (auto data : invoc.second->data) {
auto gpuInstCountResult = std::dynamic_pointer_cast<GpuInstCountResultData>(data);
if (instCount.find(gpuInstCountResult->instructionOffset) == instCount.end()) {
instCount[gpuInstCountResult->instructionOffset] = 0;
}
instCount[gpuInstCountResult->instructionOffset] += gpuInstCountResult->count;
maxCount = std::max(maxCount, instCount[gpuInstCountResult->instructionOffset]);
}
}
std::string maxCountStr = std::to_string(maxCount);
for (auto asmLine : gpuInstCountKernRes->origAsm) {
auto it = instCount.lower_bound(asmLine.instructionOffset + 1);
if (it != instCount.begin()) it--;
GetStream() << "[" << std::dec << std::setw(maxCountStr.size() + 1) << it->second << "] 0x"
<< std::setw(6) << std::hex << std::setfill('0') << asmLine.instructionOffset
<< std::setfill(' ') << " : " << asmLine.asmLineOrig << "\n";
}
GetStream() << "\n";
}
}
};
GpuInstCountProfiler* toolHandle = nullptr;
std::shared_ptr<GpuInstCountTxtWriter> txtWriter =
std::make_shared<GpuInstCountTxtWriter>(std::cerr);
std::shared_ptr<DefaultGTPinFilter> filter = std::make_shared<DefaultGTPinFilter>();
// External Tool Interface ////////////////////////////////////////////////////
extern "C" PTI_EXPORT void Usage() {
std::cout << "Usage: ./gpu_inst_count[.exe] <application> <args>" << std::endl;
}
extern "C" PTI_EXPORT int ParseArgs(int argc, char* argv[]) { return 1; }
extern "C" PTI_EXPORT void SetToolEnv() {
utils::SetEnv("ZE_ENABLE_TRACING_LAYER", "1");
utils::SetEnv("ZET_ENABLE_PROGRAM_INSTRUMENTATION", "1");
}
// Internal Tool Functionality ////////////////////////////////////////////////
static void PrintResults() {
PTI_ASSERT(toolHandle != nullptr);
toolHandle->Stop();
delete toolHandle;
toolHandle = nullptr;
std::cerr << std::endl;
}
// Internal Tool Interface ////////////////////////////////////////////////////
void EnableProfiling() {
std::cerr << std::endl;
PTI_ASSERT(toolHandle == nullptr);
toolHandle = new GpuInstCountProfiler(txtWriter, filter);
auto status = toolHandle->Start();
if (PROF_STATUS_SUCCESS != status) {
std::cout << GTPIN_LAST_ERROR_STR << std::endl;
}
}
void DisableProfiling() {
std::cerr << std::endl;
if (toolHandle != nullptr) {
PrintResults();
delete toolHandle;
}
}
void OnFini() {
if (toolHandle != nullptr) {
toolHandle->Stop();
delete toolHandle;
toolHandle = nullptr;
}
}
EXPORT_C_FUNC void GTPin_Entry(int argc, const char* argv[]) {
ConfigureGTPin(argc, argv);
atexit(OnFini);
toolHandle = new GpuInstCountProfiler(txtWriter);
auto status = toolHandle->Start();
if (PROF_STATUS_SUCCESS != status) {
std::cout << GTPIN_LAST_ERROR_STR << std::endl;
}
}