forked from lanl/gs_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs_patterns.h
344 lines (278 loc) · 10.6 KB
/
gs_patterns.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once
#include <exception>
#include <string>
#include <cstring>
#include <vector>
//symbol lookup options
#if !defined(SYMBOLS_ONLY)
#define SYMBOLS_ONLY 1 //Filter out instructions that have no symbol
#endif
//triggers
#define PERSAMPLE 10000000
//info
#define CLSIZE (64) //cacheline bytes
#define NBUFS (1LL<<10) //trace reading buffer size
#define IWINDOW (1024) //number of iaddrs per window
#define NGS (8096) //max number for gathers and scatters
#define OBOUNDS (512) //histogram positive max
#define OBOUNDS_ALLOC (2*OBOUNDS + 3)
//patterns
#define USTRIDES 1024 //Threshold for number of accesses
#define NSTRIDES 15 //Threshold for number of unique distances
#define OUTTHRESH (0.5) //Threshold for percentage of distances at boundaries of histogram
#define NTOP (10) //Final gather / scatters to keep
#define INITIAL_PSIZE (1<<15)
#define MAX_PSIZE (1<<30) //Max number of indices recorded per gather/scatter
#define MAX_LINE_LENGTH 1024
#if !defined(VBITS)
# define VBITS (512L)
# define VBYTES (VBITS/8)
#endif
namespace gs_patterns
{
typedef uintptr_t addr_t;
typedef enum { GATHER=0, SCATTER } mem_access_type;
typedef enum { VECTOR=0, CTA } mem_instr_type;
class GSError : public std::exception
{
public:
GSError (const std::string & reason) : _reason(reason) { }
~GSError() {}
const char * what() const noexcept override { return _reason.c_str(); }
private:
std::string _reason;
};
class GSFileError : public GSError
{
public:
GSFileError (const std::string & reason) : GSError(reason) { }
~GSFileError() {}
};
class GSDataError : public GSError
{
public:
GSDataError (const std::string & reason) : GSError(reason) { }
~GSDataError() {}
};
class GSAllocError : public GSError
{
public:
GSAllocError (const std::string & reason) : GSError(reason) { }
~GSAllocError() {}
};
class InstrAddrAdapter
{
public:
InstrAddrAdapter() { }
virtual ~InstrAddrAdapter() { }
virtual bool is_valid() const = 0;
virtual bool is_mem_instr() const = 0;
virtual bool is_other_instr() const = 0;
virtual mem_access_type get_mem_access_type() const = 0;
virtual mem_instr_type get_mem_instr_type() const = 0;
virtual size_t get_size() const = 0;
virtual addr_t get_base_addr() const = 0;
virtual addr_t get_address() const = 0;
virtual addr_t get_iaddr() const = 0;
virtual addr_t get_maddr() const = 0;
virtual unsigned short get_type() const = 0; // must be 0 for GATHER, 1 for SCATTER !!
virtual int64_t get_max_access_size() const = 0;
virtual bool is_gather() const
{ return (is_valid() && is_mem_instr() && GATHER == get_mem_access_type()) ? true : false; }
virtual bool is_scatter() const
{ return (is_valid() && is_mem_instr() && SCATTER == get_mem_access_type()) ? true : false; }
virtual void output(std::ostream & os) const = 0;
};
std::ostream & operator<<(std::ostream & os, const InstrAddrAdapter & ia);
class Metrics
{
public:
Metrics(mem_access_type mType) : _mType(mType), _pattern_sizes(NTOP)
{
try
{
for (int j = 0; j < NTOP; j++) {
patterns[j] = new int64_t[INITIAL_PSIZE];
_pattern_sizes[j] = INITIAL_PSIZE;
}
}
catch (const std::exception & ex)
{
throw GSAllocError("Could not allocate patterns for " + type_as_string() + "! due to: " + ex.what());
}
}
~Metrics()
{
for (int i = 0; i < NTOP; i++) {
delete [] patterns[i];
}
delete [] srcline;
}
size_t get_pattern_size(int pattern_index) {
return _pattern_sizes[pattern_index];
}
bool grow(int pattern_index) {
try {
size_t old_size = _pattern_sizes[pattern_index];
size_t new_size = old_size * 2;
if (new_size > MAX_PSIZE) {
return false;
}
int64_t *tmp = new int64_t[new_size];
memcpy(tmp, patterns[pattern_index], old_size * sizeof(int64_t));
delete [] patterns[pattern_index];
patterns[pattern_index] = tmp;
_pattern_sizes[pattern_index] = new_size;
return true;
}
catch (...) {
return false;
}
}
Metrics(const Metrics &) = delete;
Metrics & operator=(const Metrics & right) = delete;
std::string type_as_string() { return !_mType ? "GATHER" : "SCATTER"; }
std::string getName() { return !_mType ? "Gather" : "Scatter"; }
std::string getShortName() { return !_mType ? "G" : "S"; }
std::string getShortNameLower() { return !_mType ? "g" : "s"; }
auto get_srcline() { return srcline[_mType]; }
int ntop = 0;
int64_t iaddrs_nosym = 0;
int64_t indices_nosym = 0;
int64_t iaddrs_sym = 0;
int64_t indices_sym = 0;
double cnt = 0.0;
int offset[NTOP] = {0};
int size[NTOP] = {0};
addr_t tot[NTOP] = {0};
addr_t top[NTOP] = {0};
addr_t top_idx[NTOP] = {0};
int64_t* patterns[NTOP] = {0};
private:
char (*srcline)[NGS][MAX_LINE_LENGTH] = new char[2][NGS][MAX_LINE_LENGTH];
mem_access_type _mType;
std::vector<size_t> _pattern_sizes;
};
class InstrInfo
{
public:
InstrInfo(mem_access_type mType) : _mType(mType) { }
~InstrInfo() {
delete [] iaddrs;
delete [] icnt;
delete [] occ;
}
InstrInfo(const InstrInfo &) = delete;
InstrInfo & operator=(const InstrInfo & right) = delete;
addr_t* get_iaddrs() { return iaddrs[_mType]; }
int64_t* get_icnt() { return icnt[_mType]; }
int64_t* get_occ() { return occ[_mType]; }
private:
addr_t (*iaddrs)[NGS] = new addr_t[2][NGS];
int64_t (*icnt)[NGS] = new int64_t[2][NGS]; //vector instances
int64_t (*occ)[NGS] = new int64_t[2][NGS]; //load/store instances
mem_access_type _mType;
};
class TraceInfo // Stats
{
public:
/// TODO: need a reset method to zero out counters
uint64_t opcodes = 0;
uint64_t opcodes_mem = 0;
uint64_t addrs = 0;
uint64_t other = 0;
int64_t ngs = 0;
int64_t trace_lines = 0;
bool did_opcode = false; // revist this ---------------
double other_cnt = 0.0;
double gather_score = 0.0;
double gather_occ_avg = 0.0;
double scatter_occ_avg = 0.0;
uint64_t mcnt = 0;
};
template <std::size_t MAX_ACCESS_SIZE>
class InstrWindow
{
public:
InstrWindow() {
// First dimension is 0=GATHER/1=SCATTER
_w_iaddrs = new int64_t[2][IWINDOW];
_w_bytes = new int64_t[2][IWINDOW];
_w_maddr = new int64_t[2][IWINDOW][MAX_ACCESS_SIZE];
_w_cnt = new int64_t[2][IWINDOW];
init();
}
virtual ~InstrWindow() {
delete [] _w_iaddrs;
delete [] _w_bytes;
delete [] _w_maddr;
delete [] _w_cnt;
}
void init() {
for (int w = 0; w < 2; w++) {
for (int i = 0; i < IWINDOW; i++) {
_w_iaddrs[w][i] = -1;
_w_bytes[w][i] = 0;
_w_cnt[w][i] = 0;
for (uint64_t j = 0; j < MAX_ACCESS_SIZE; j++)
_w_maddr[w][i][j] = -1;
}
}
}
void reset(int w) {
for (int i = 0; i < IWINDOW; i++) {
_w_iaddrs[w][i] = -1;
_w_bytes[w][i] = 0;
_w_cnt[w][i] = 0;
for (uint64_t j = 0; j < MAX_ACCESS_SIZE; j++)
_w_maddr[w][i][j] = -1;
}
}
void reset() {
for (int w = 0; w < 2; w++) {
reset(w);
}
}
InstrWindow(const InstrWindow &) = delete;
InstrWindow & operator=(const InstrWindow & right) = delete;
int64_t & w_iaddrs(int32_t i, int32_t j) { return _w_iaddrs[i][j]; }
int64_t & w_bytes(int32_t i, int32_t j) { return _w_bytes[i][j]; }
int64_t & w_maddr(int32_t i, int32_t j, int32_t k) { return _w_maddr[i][j][k]; }
int64_t & w_cnt(int32_t i, int32_t j) { return _w_cnt[i][j]; }
addr_t & get_iaddr() { return iaddr; }
int64_t & get_maddr_prev() { return maddr_prev; }
int64_t & get_maddr() { return maddr; }
private:
// First dimension is 0=GATHER/1=SCATTER
int64_t (*_w_iaddrs)[IWINDOW];
int64_t (*_w_bytes)[IWINDOW];
int64_t (*_w_maddr)[IWINDOW][MAX_ACCESS_SIZE];
int64_t (*_w_cnt)[IWINDOW];
// State which must be carried with each call to handle a trace
addr_t iaddr;
int64_t maddr_prev;
int64_t maddr;
};
template <std::size_t MAX_ACCESS_SIZE>
class MemPatterns
{
public:
MemPatterns() { }
virtual ~MemPatterns() { };
MemPatterns(const MemPatterns &) = delete;
MemPatterns & operator=(const MemPatterns &) = delete;
virtual void handle_trace_entry(const InstrAddrAdapter & ia) = 0;
virtual void generate_patterns() = 0;
virtual Metrics & get_metrics(mem_access_type) = 0;
virtual InstrInfo & get_iinfo(mem_access_type) = 0;
virtual Metrics & get_gather_metrics() = 0;
virtual Metrics & get_scatter_metrics() = 0;
virtual InstrInfo & get_gather_iinfo() = 0;
virtual InstrInfo & get_scatter_iinfo() = 0;
virtual TraceInfo & get_trace_info() = 0;
virtual InstrWindow<MAX_ACCESS_SIZE> &
get_instr_window() = 0;
virtual void set_log_level(int8_t ll) = 0;
virtual int8_t get_log_level() = 0;
};
} // namespace gs_patterns