-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReuseDistance.cpp
468 lines (389 loc) · 12 KB
/
ReuseDistance.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* This file is part of the ReuseDistance tool.
*
* Copyright (c) 2012, University of California Regents
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ReuseDistance.hpp>
using namespace std;
//#define REUSE_DEBUG
#ifdef REUSE_DEBUG
#define debug_assert(...) assert(__VA_ARGS__)
#else
#define debug_assert(...)
#endif
inline uint64_t uint64abs(uint64_t a){
if (a < 0x8000000000000000L){
return a;
} else {
return 0 - a;
}
}
void ReuseDistance::Init(uint64_t w, uint64_t b){
capacity = w;
binindividual = b;
maxtracking = capacity;
current = 0;
sequence = 1;
window = newtree234();
assert(window);
mwindow.clear();
assert(ReuseDistance::Infinity == NULL && "NULL is non-zero!?");
}
ReuseDistance::ReuseDistance(uint64_t w, uint64_t b){
ReuseDistance::Init(w, b);
}
ReuseDistance::ReuseDistance(uint64_t w){
ReuseDistance::Init(w, DefaultBinIndividual);
}
ReuseDistance::~ReuseDistance(){
for (reuse_map_type<uint64_t, ReuseStats*>::const_iterator it = stats.begin(); it != stats.end(); it++){
uint64_t id = it->first;
delete stats[id];
}
debug_assert(current == count234(window));
while (current){
delete (ReuseEntry*)delpos234(window, 0);
current--;
}
freetree234(window);
}
uint64_t ReuseStats::GetMissCount(){
return distcounts[invalid];
}
void ReuseDistance::GetIndices(std::vector<uint64_t>& ids){
assert(ids.size() == 0);
for (reuse_map_type<uint64_t, ReuseStats*>::const_iterator it = stats.begin(); it != stats.end(); it++){
uint64_t id = it->first;
ids.push_back(id);
}
}
void ReuseDistance::GetActiveAddresses(std::vector<uint64_t>& addrs){
assert(addrs.size() == 0);
debug_assert(current == count234(window));
for (int i = 0; i < current; i++){
ReuseEntry* r = index234(window, i);
addrs.push_back(r->address);
}
}
void ReuseDistance::Print(bool annotate){
Print(cout, annotate);
}
void ReuseDistance::Process(ReuseEntry* rs, uint64_t count){
for (uint32_t i = 0; i < count; i++){
Process(rs[i]);
}
}
void ReuseDistance::Process(vector<ReuseEntry> rs){
for (vector<ReuseEntry>::const_iterator it = rs.begin(); it != rs.end(); it++){
ReuseEntry r = *it;
Process(r);
}
}
void ReuseDistance::Process(vector<ReuseEntry*> rs){
for (vector<ReuseEntry*>::const_iterator it = rs.begin(); it != rs.end(); it++){
ReuseEntry* r = *it;
Process((*r));
}
}
void ReuseDistance::SkipAddresses(uint64_t amount){
sequence += amount;
// flush the window completely
while (current){
delete delpos234(window, 0);
current--;
}
mwindow.clear();
assert(mwindow.size() == 0);
assert(count234(window) == 0);
}
void ReuseDistance::Process(ReuseEntry& r){
uint64_t addr = r.address;
uint64_t id = r.id;
uint64_t mres = mwindow.count(addr);
ReuseStats* stats = GetStats(id, true);
int dist = 0;
ReuseEntry* result;
if (mres){
mres = mwindow[addr];
ReuseEntry key;
key.address = addr;
key.__seq = mres;
result = findrelpos234(window, &key, &dist);
debug_assert(result);
if (capacity != ReuseDistance::Infinity){
debug_assert(current - dist <= capacity);
}
stats->Update(current - dist);
} else {
stats->Update(ReuseDistance::Infinity);
}
// recycle a slot when possible
ReuseEntry* slot = NULL;
if (mres || (capacity != ReuseDistance::Infinity && current >= capacity)){
slot = (ReuseEntry*)delpos234(window, dist);
debug_assert(mwindow[slot->address]);
mwindow.erase(slot->address);
debug_assert(count234(window) == mwindow.size());
} else {
slot = new ReuseEntry();
current++;
}
mwindow[addr] = sequence;
slot->__seq = sequence;
slot->address = addr;
add234(window, slot);
debug_assert(count234(window) == mwindow.size());
debug_assert(mwindow.size() <= current);
sequence++;
}
void ReuseDistance::PrintFormat(ostream& f){
f << "# "
<< Describe() << "STATS"
<< TAB << "<window_size>"
<< TAB << "<bin_indiv>"
<< TAB << "<max_track>"
<< TAB << "<id_count>"
<< TAB << "<tot_access>"
<< TAB << "<tot_miss>"
<< ENDL;
f << "# "
<< TAB << Describe() << "ID"
<< TAB << "<id>"
<< TAB << "<id_access>"
<< TAB << "<id_miss>"
<< ENDL;
}
void ReuseStats::PrintFormat(ostream& f){
f << "# "
<< TAB
<< TAB << "<bin_lower_bound>"
<< TAB << "<bin_upper_bound>"
<< TAB << "<bin_count>"
<< ENDL;
}
void ReuseDistance::Print(ostream& f, bool annotate){
vector<uint64_t> keys;
for (reuse_map_type<uint64_t, ReuseStats*>::const_iterator it = stats.begin(); it != stats.end(); it++){
keys.push_back(it->first);
}
sort(keys.begin(), keys.end());
uint64_t tot = 0, mis = 0;
for (vector<uint64_t>::const_iterator it = keys.begin(); it != keys.end(); it++){
uint64_t id = (*it);
ReuseStats* r = (ReuseStats*)stats[id];
tot += r->GetAccessCount();
mis += r->GetMissCount();
}
if (annotate){
ReuseDistance::PrintFormat(f);
ReuseStats::PrintFormat(f);
}
f << Describe() << "STATS"
<< TAB << dec << capacity
<< TAB << binindividual
<< TAB << maxtracking
<< TAB << keys.size()
<< TAB << tot
<< TAB << mis
<< ENDL;
for (vector<uint64_t>::const_iterator it = keys.begin(); it != keys.end(); it++){
uint64_t id = (*it);
ReuseStats* r = (ReuseStats*)stats[id];
f << TAB << Describe() << "ID"
<< TAB << dec << id
<< TAB << r->GetAccessCount()
<< TAB << r->GetMissCount()
<< ENDL;
r->Print(f);
}
}
ReuseStats* ReuseDistance::GetStats(uint64_t id, bool gen){
ReuseStats* s = stats[id];
if (s == NULL && gen){
s = new ReuseStats(id, binindividual, capacity, ReuseDistance::Infinity);
stats[id] = s;
}
return s;
}
// this should be fast as possible. This code is from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
static const uint64_t b[] = {0x2L, 0xCL, 0xF0L, 0xFF00L, 0xFFFF0000L, 0xFFFFFFFF00000000L};
static const uint32_t S[] = {1, 2, 4, 8, 16, 32};
inline uint64_t ShaveBitsPwr2(uint64_t val){
val -= 1;
register uint64_t r = 0; // result of log2(v) will go here
for (int32_t i = 5; i >= 0; i--){
if (val & b[i]){
val = val >> S[i];
r |= S[i];
}
}
return (2 << r);
}
uint64_t ReuseStats::GetBin(uint64_t value){
// not a valid value
if (value == invalid){
return invalid;
}
// outside of tracking window, also invalid
else if (maxtracking != ReuseDistance::Infinity && value > maxtracking){
return invalid;
}
// valid but not tracked individually
else if (binindividual != ReuseDistance::Infinity && value > binindividual){
return ShaveBitsPwr2(value);
}
// valid and tracked individually
return value;
}
ReuseStats* ReuseDistance::GetStats(uint64_t id){
return GetStats(id, false);
}
uint64_t ReuseStats::GetAccessCount(){
return accesses;
}
uint64_t ReuseStats::GetMaximumDistance(){
uint64_t max = 0;
for (reuse_map_type<uint64_t, uint64_t>::const_iterator it = distcounts.begin(); it != distcounts.end(); it++){
uint64_t d = it->first;
if (d > max){
max = d;
}
}
return max;
}
void ReuseStats::Update(uint64_t dist){
distcounts[GetBin(dist)] += 1;
accesses++;
}
uint64_t ReuseStats::CountDistance(uint64_t d){
if (distcounts.count(d) == 0){
return 0;
}
return distcounts[d];
}
void ReuseStats::GetSortedDistances(vector<uint64_t>& dkeys){
assert(dkeys.size() == 0 && "dkeys must be an empty vector");
for (reuse_map_type<uint64_t, uint64_t>::const_iterator it = distcounts.begin(); it != distcounts.end(); it++){
uint64_t d = it->first;
dkeys.push_back(d);
}
sort(dkeys.begin(), dkeys.end());
}
void ReuseStats::Print(ostream& f, bool annotate){
vector<uint64_t> keys;
GetSortedDistances(keys);
if (annotate){
ReuseStats::PrintFormat(f);
}
for (vector<uint64_t>::const_iterator it = keys.begin(); it != keys.end(); it++){
uint64_t d = *it;
if (d == invalid) continue;
debug_assert(distcounts.count(d) > 0);
uint32_t cnt = distcounts[d];
debug_assert(cnt > 0);
if (cnt > 0){
uint64_t p = d / 2 + 1;
if (binindividual == ReuseDistance::Infinity || d <= binindividual){
p = d;
}
f << TAB
<< TAB << dec << p
<< TAB << d
<< TAB << cnt
<< ENDL;
}
}
}
void SpatialLocality::Init(uint64_t size, uint64_t bin, uint64_t max){
sequence = 1;
capacity = size;
binindividual = bin;
maxtracking = max;
assert(capacity > 0 && capacity != ReuseDistance::Infinity && "window size must be a finite, positive value");
assert((maxtracking == INFINITY_REUSE || maxtracking >= binindividual) && "max tracking must be at least as large as individual binning");
}
ReuseStats* SpatialLocality::GetStats(uint64_t id, bool gen){
ReuseStats* s = stats[id];
if (s == NULL && gen){
s = new ReuseStats(id, binindividual, maxtracking, SpatialLocality::Invalid);
stats[id] = s;
}
return s;
}
void SpatialLocality::Process(ReuseEntry& r){
uint64_t addr = r.address;
uint64_t id = r.id;
ReuseStats* stats = GetStats(id, true);
debug_assert(stats);
// find the address closest to addr
uint64_t bestdiff = SpatialLocality::Invalid;
if (awindow.size() > 0){
map<uint64_t, uint64_t>::const_iterator it = awindow.upper_bound(addr);
if (it == awindow.end()){
it--;
}
// only need to check the values immediately equal, >, and < than addr
for (uint32_t i = 0; i < 3; i++, it--){
uint64_t cur = it->first;
uint64_t seq = it->second;
uint64_t diff = uint64abs(cur - addr);
if (diff < bestdiff){
bestdiff = diff;
}
if (it == awindow.begin()){
break;
}
}
}
stats->Update(bestdiff);
// remove the oldest address in the window
if (swindow.size() > capacity){
uint64_t a = swindow.front();
swindow.pop_front();
uint64_t v = awindow[a];
if (v > 1){
awindow[a] = v - 1;
} else {
awindow.erase(a);
}
}
// insert the newest address into the window
awindow[addr]++;
swindow.push_back(addr);
}
void SpatialLocality::SkipAddresses(uint64_t amount){
// flush the window completely
while (swindow.size()){
uint64_t a = swindow.front();
swindow.pop_front();
uint64_t v = awindow[a];
if (v > 1){
awindow[a] = v - 1;
} else {
awindow.erase(a);
}
}
assert(awindow.size() == 0);
assert(swindow.size() == 0);
}
void SpatialLocality::GetActiveAddresses(std::vector<uint64_t>& addrs){
assert(addrs.size() == 0);
for (map<uint64_t, uint64_t>::const_iterator it = awindow.begin(); it != awindow.end(); it++){
uint64_t addr = it->first;
addrs.push_back(addr);
}
}