-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_reader.cpp
More file actions
203 lines (192 loc) · 8.66 KB
/
Copy pathtrace_reader.cpp
File metadata and controls
203 lines (192 loc) · 8.66 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
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
#include "trace_reader.h"
#include "kernel.h"
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
void trace_reader::gen_block_mem_trace(const std::string &trace_path, int kernel_id) {
std::string mem_file = trace_path + "/kernel-" + std::to_string(kernel_id) + ".mem";
std::ifstream file(mem_file);
std::string line;
while (std::getline(file, line)) {
unsigned pos_LDG = line.find("LDG");
unsigned pos_STG = line.find("STG");
unsigned pos_ATOM = line.find("ATOM");
if (pos_LDG != std::string::npos || pos_STG != std::string::npos || pos_ATOM != std::string::npos) {
unsigned pos_split = line.find_first_of(' ');
std::string tmp = line.substr(0, pos_split);
int block_id = std::stoi(tmp);
std::ofstream wf(
trace_path + "/kernel-" + std::to_string(kernel_id) + "-block-" + std::to_string(block_id) +
".mem", std::ios::app);
line = line.substr(pos_split + 1, line.size() - 2);
wf << line << "\n";
wf.close();
}
}
file.close();
}
void trace_reader::read_sass(const std::string &benchmark_path, kernel_info &kernelInfo,
std::vector<trace_inst> &trace_insts) const {
std::string file_s = benchmark_path + "kernel-" + std::to_string(m_kernel_id) + ".sass";
std::ifstream file(file_s);
if(!file.is_open()){
printf("%s does not exist!\n", file_s.c_str());
exit(1);
}
std::string line;
std::map<int, std::map<long long, int> > block_pc_num;
while (std::getline(file, line)) {
if (line == "Flexsim version 3 sass trace" || line == "Flexsim kernel info end")
continue;
else if (line.find("kernel_id = ") != std::string::npos) {
std::string str = "kernel_id = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_kernel_id = std::stoi(line);
} else if (line.find("grid_size = ") != std::string::npos) {
std::string str = "grid_size = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_grid_size = std::stoi(line);
} else if (line.find("block_size = ") != std::string::npos) {
std::string str = "block_size = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_block_size = std::stoi(line);
} else if (line.find("shared_mem_bytes = ") != std::string::npos) {
std::string str = "shared_mem_bytes = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_shared_mem_bytes = std::stoi(line);
} else if (line.find("num_registers = ") != std::string::npos) {
std::string str = "num_registers = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_num_registers = std::stoi(line);
} else {
std::istringstream is(line);
std::string str;
std::queue<std::string> tmp_str;
while (is >> str) {
tmp_str.push(str);
}
int warp_id = std::stoi(tmp_str.front());
tmp_str.pop();
char *stop;
long long pc = std::strtoll(tmp_str.front().c_str(), &stop, 16);
tmp_str.pop();
std::string active_mask = tmp_str.front();
tmp_str.pop();
int dest_num = std::stoi(tmp_str.front());
tmp_str.pop();
std::vector<std::string> dest_regs;
for (int i = 0; i < dest_num; i++) {
dest_regs.push_back(tmp_str.front());
tmp_str.pop();
}
std::string opcode = tmp_str.front();
tmp_str.pop();
int src_num = std::stoi(tmp_str.front());
tmp_str.pop();
std::vector<std::string> src_regs;
for (int i = 0; i < src_num; i++) {
src_regs.push_back(tmp_str.front());
tmp_str.pop();
}
int pc_index = 0;
if (block_pc_num.find(warp_id) != block_pc_num.end()) {
if (block_pc_num[warp_id].find(pc) != block_pc_num[warp_id].end()) {
pc_index = block_pc_num[warp_id][pc] + 1;
block_pc_num[warp_id][pc] = pc_index;
} else {
block_pc_num[warp_id][pc] = 0;
}
} else {
block_pc_num[warp_id][pc] = 0;
}
trace_inst m_inst = trace_inst(-1, warp_id, pc, active_mask, dest_num, dest_regs, opcode, src_num, src_regs,
pc_index);
trace_insts.push_back(m_inst);
}
}
}
void trace_reader::read_all_block_sass(const string &benchmark_path, kernel_info &kernelInfo,
std::vector<trace_inst> &trace_insts) const {
std::string file_s = benchmark_path + "kernel-" + std::to_string(m_kernel_id) + ".allsass";
std::ifstream file(file_s);
if(!file.is_open()){
printf("%s does not exist!\n", file_s.c_str());
exit(1);
}
std::string line;
std::map<int, std::map<int, std::map<long long, int> >> block_pc_num; //block,warp,pc,pc_index
while (std::getline(file, line)) {
if (line == "Flexsim version 3 sass trace" || line == "Flexsim kernel info end")
continue;
else if (line.find("kernel_id = ") != std::string::npos) {
std::string str = "kernel_id = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_kernel_id = std::stoi(line);
} else if (line.find("grid_size = ") != std::string::npos) {
std::string str = "grid_size = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_grid_size = std::stoi(line);
} else if (line.find("block_size = ") != std::string::npos) {
std::string str = "block_size = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_block_size = std::stoi(line);
} else if (line.find("shared_mem_bytes = ") != std::string::npos) {
std::string str = "shared_mem_bytes = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_shared_mem_bytes = std::stoi(line);
} else if (line.find("num_registers = ") != std::string::npos) {
std::string str = "num_registers = ";
line = line.replace(line.find(str), str.length(), "");
kernelInfo.m_num_registers = std::stoi(line);
} else {
std::istringstream is(line);
std::string str;
std::queue<std::string> tmp_str;
while (is >> str) {
tmp_str.push(str);
}
int block_id = std::stoi(tmp_str.front());
tmp_str.pop();
int warp_id = std::stoi(tmp_str.front());
tmp_str.pop();
char *stop;
long long pc = std::strtoll(tmp_str.front().c_str(), &stop, 16);
tmp_str.pop();
std::string active_mask = tmp_str.front();
tmp_str.pop();
int dest_num = std::stoi(tmp_str.front());
tmp_str.pop();
std::vector<std::string> dest_regs;
for (int i = 0; i < dest_num; i++) {
dest_regs.push_back(tmp_str.front());
tmp_str.pop();
}
std::string opcode = tmp_str.front();
tmp_str.pop();
int src_num = std::stoi(tmp_str.front());
tmp_str.pop();
std::vector<std::string> src_regs;
for (int i = 0; i < src_num; i++) {
src_regs.push_back(tmp_str.front());
tmp_str.pop();
}
int pc_index = 0;
if (block_pc_num.find(block_id) != block_pc_num.end() &&
block_pc_num[block_id].find(warp_id) != block_pc_num[block_id].end()) { // block
if (block_pc_num[block_id][warp_id].find(pc) != block_pc_num[block_id][warp_id].end()) {
pc_index = block_pc_num[block_id][warp_id][pc] + 1;
block_pc_num[block_id][warp_id][pc] = pc_index;
} else {
block_pc_num[block_id][warp_id][pc] = 0;
}
} else {
block_pc_num[block_id][warp_id][pc] = 0;
}
trace_inst m_inst = trace_inst(block_id, warp_id, pc, active_mask, dest_num, dest_regs, opcode, src_num,
src_regs,
pc_index);
trace_insts.push_back(m_inst);
}
}
}