-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAffinityAnalysis.cpp
More file actions
357 lines (273 loc) · 9.29 KB
/
AffinityAnalysis.cpp
File metadata and controls
357 lines (273 loc) · 9.29 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
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
#include "AffinityAnalysis.hpp"
/*
Class: Affinity
Public member functions: <Analysis>, <remove_entry>,
<getLayouts>, and <trace_hash_access>
*/
std::ostream& Analysis::err(std::cerr);
Analysis::Analysis() {
srand(time(NULL));
// get prepared for the first analysis_set_sampling round
analysis_count_down = analysis_sampling_period;
current_stage_fn = &Analysis::sample_stage;
current_stage = SAMPLE_STAGE;
trace_stage_count = 0;
const char* e;
if ((e = getenv("ST_HASH_DEBUG")) && atoi(e) == 1)
DEBUG = true;
if ((e = getenv("ST_HASH_PRINT")) && atoi(e) == 1)
PRINT = true;
}
/*
Function: remove_entry
Removes a <entry_t> element from the analysis entirely.
Effects:
<entry_index> is added to <delete_set> to allow
for it's lazy deletion from <affinity_map>.
<entry_index> is removed from <window_list>, <analysis_vec>,
<analysis_set>, and <timestamp_map> if applicable. This doesn't
need to happen simply ignore <entry_index> while generating
pairings. Still remove form <analysis_set> though; maybe.
*/
void Analysis::remove_entry(entry_index_t entry_index) {
/*
entry_t entry(entry_index);
for(auto w = singlFreq.begin(); w != singlFreq.end();) {
auto entry_a = w->first;
if(entry_a = entry)
w = singlFreq.erase(w);
}
*/
remove_set.emplace(entry_index);
}
/*
Function: modify_entry
Changes the uid of an entry_t in the analysis from o to n.
Effects:
Modifies <analysis_vec>, <analysis_set>, <timestamp_map>,
<decay_map>, and <window_list> changing <entry_mappings>[i].first
to <entry_mappings>[i].second for all i.
void Analysis::modify_entries(std::vector<std::pair<entry_index_t, entry_index_t>> entry_mappings) {
for(const auto &p : entry_mappings) {
}
}
*/
/*
void new_ids(std::unordered_map<entry_t, entry_t> mapping) {
for(auto& entry : jointFreq) {
auto& a = entry.first.first;
auto& b = entry.first.second;
if(mapping.find(a) != mapping.end())
a = mapping.find(a);
if(mapping.find(b) != mapping.end())
b = mapping.find(b);
}
for(auto& entry : singlFreq) {
auto& a = entry.first;
if(mapping.find(a) != mapping.end())
a = mapping.find(a);
}
}
*/
std::vector<layout_t> Analysis::getLayouts() {
std::vector<layout_t> layouts;
// Clear frequency information.
// TODO: move back to transition_stage().
singlFreq.clear();
jointFreq.clear();
for (auto layout_pair : layout_map)
if (!layout_pair.second->dumped) {
layouts.push_back(*layout_pair.second);
// layout_os << *layout_pair.second;
layout_pair.second->dumped = true;
}
return layouts;
}
void Analysis::trace_hash_access(entry_index_t entry_index) {
// Switch to new stage if the current has ended.
if (!analysis_count_down--)
transition_stage();
// Process entry according to current stage.
(this->*current_stage_fn)(entry_index);
}
/*
Class: Analysis
Private member functions: <trace_stage>, <sample_stage>,
<reorder_stage>, <transition_stage>, <update_affinity>,
and <add_compress_update>
*/
/*
Function: transition_stages
Effects:
Changes <current_stage> and <current_stage_fn> to
the current stage.
*/
void Analysis::transition_stage() {
switch (current_stage) {
case SAMPLE_STAGE: { // -> TRACE_STAGE
if (DEBUG) {
err << "End of the sampling stage, analyzed entries are:\n";
for (const auto& a_entry : analysis_vec)
err << a_entry << "\n";
err << "\n";
}
// For constant time lookup.
for (const auto& e : analysis_vec)
analysis_set.insert(e);
analysis_count_down = analysis_staging_period;
current_stage_fn = &Analysis::trace_stage;
current_stage = TRACE_STAGE;
} break;
case TRACE_STAGE: { // -> SAMPLE_STAGE
if (DEBUG) {
err << "Analysis stage finishes now!\n";
err.flush();
}
// Clear the exluded entry set and the analysis entry set
analysis_vec.clear();
analysis_set.clear();
window_list.clear();
if (trace_stage_count++ % analysis_reordering_period == 0) {
reorder_stage();
if (DEBUG) {
err << "Exiting reordering phase\n";
err.flush();
}
// Clear frequency information.
// Moved to getLayouts() temporarily
// singlFreq.clear();
// jointFreq.clear();
}
// TODO: Change analysis_set to new ids. and manage reordering stages.
analysis_count_down = analysis_sampling_period;
current_stage_fn = &Analysis::sample_stage;
current_stage = SAMPLE_STAGE;
} break;
}
}
/*
Function: sample_stage
Effects:
*/
void Analysis::sample_stage(entry_index_t entry_index) {
entry_t entry(entry_index);
// Fill in the analysis vector first
if (analysis_vec.size() < analysis_set_size) {
analysis_vec.push_back(entry);
// Reservoir sampling
} else {
int r = rand() % (analysis_sampling_period - analysis_count_down);
if (r < analysis_set_size)
analysis_vec[r] = entry;
}
}
/*
Function: trace_stage
Effects:
*/
void Analysis::trace_stage(entry_index_t entry_index) {
entry_t entry(entry_index);
add_compress_update(entry, (analysis_set.find(entry) != analysis_set.end()));
}
/*
Function: reorder_stage
A reorder stage generates and caches a layout based on
affinities generated during the trace stage. During this
stage pair-wise affinities of objects are also decreased
if an object in the pair hasn't been accessed since the
last reorder stage via <decay_map>. Beyond this
Effects:
Removes <delete_set> elements from <affinity_map> then
clears <delete_set>.
Removes <entry_t> elements from <afffinity_map> based on
their decay due to <decay_map>.
Removes unused entires from <decay_map>.
Generates <layout_map>.
*/
void Analysis::reorder_stage() {
layout_map.clear();
std::vector<affinity_pair_t> all_affinity_pairs = get_affinity_pairs();
std::sort(all_affinity_pairs.rbegin(), all_affinity_pairs.rend());
for (const auto& affinity_pair : all_affinity_pairs) {
auto lLayoutPair = layout_map.emplace(std::piecewise_construct,
std::forward_as_tuple(affinity_pair.lentry),
std::forward_as_tuple((layout_t*)NULL));
if (lLayoutPair.second)
lLayoutPair.first->second = new layout_t(affinity_pair.lentry);
auto rLayoutPair = layout_map.emplace(std::piecewise_construct,
std::forward_as_tuple(affinity_pair.rentry),
std::forward_as_tuple((layout_t*)NULL));
if (rLayoutPair.second)
rLayoutPair.first->second = new layout_t(affinity_pair.rentry);
if (rLayoutPair.first->second != lLayoutPair.first->second) {
if (rLayoutPair.first->second->size() > lLayoutPair.first->second->size())
rLayoutPair.first->second->merge(layout_map, lLayoutPair.first->second);
else
lLayoutPair.first->second->merge(layout_map, rLayoutPair.first->second);
}
}
}
std::vector<affinity_pair_t> Analysis::get_affinity_pairs() {
std::vector<affinity_pair_t> all_affinity_pairs;
all_affinity_pairs.reserve(jointFreq.size());
for (const auto& wcount_pair : jointFreq) {
const auto& entry_pair = wcount_pair.first;
const auto& window_hist = wcount_pair.second;
// Lazily remove datum.
//if(remove_set.find(entry_pair.first) || remove_set.find(entry_pair.second)) {
// continue;
//}
avalue_t affinity = 0;
for (wsize_t w_ind = 0; w_ind < max_fpdist_ind; w_ind += 1) {
if (window_hist[w_ind] != 0) {
if (std::max(singlFreq[entry_pair.first][w_ind], singlFreq[entry_pair.second][w_ind]) == 0)
std::cout << "OVERFLOW!\n";
avalue_t weight = (max_fpdist_ind - w_ind);
float p = (float)window_hist[w_ind] /
(float)std::max(singlFreq[entry_pair.first][w_ind], singlFreq[entry_pair.second][w_ind]);
affinity += (avalue_t)(p * weight);
}
}
if (affinity > 0)
all_affinity_pairs.emplace_back(entry_pair.first, entry_pair.second, affinity);
}
if (DEBUG) {
std::sort(all_affinity_pairs.begin(), all_affinity_pairs.end());
std::reverse(all_affinity_pairs.begin(), all_affinity_pairs.end());
err << "Printing affinity pairs:\n";
for (auto &pair : all_affinity_pairs)
err << pair << " ";
err.flush();
}
remove_set.clear();
return all_affinity_pairs;
}
inline wsize_t log2_ceil(const wsize_t in) {
return ((sizeof(unsigned int) * 8) - __builtin_clz(in)) - 2;
}
void Analysis::add_compress_update(const entry_t& entry, bool analysis) {
// Add or update window information for analyzed entry.
if(analysis) {
auto &window = window_list[entry];
window.length = 1;
}
// decay_map[entry] = std::make_pair(true, 0);
for(auto w = window_list.begin(); w != window_list.end();) {
auto &owner = w->first;
auto &window = w->second;
// Don't add to window that starts with same entry.
if(owner == entry) {
++w;
continue;
}
window.length += 1;
wsize_t window_ind = log2_ceil(window.length);
singlFreq[entry][window_ind] += 1;
jointFreq[entry_pair_t(entry, owner)][window_ind] += 1;
if(window.length == max_fpdist + 1) {
w = window_list.erase(w);
} else {
++w;
}
}
}