-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_compare.py
More file actions
131 lines (99 loc) · 4.2 KB
/
Copy pathcache_compare.py
File metadata and controls
131 lines (99 loc) · 4.2 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
import sys
import copy
from core.parser import parse_assembly
from pipeline.controller import Pipeline
from core.cache import Cache
def compare_caches(filename):
try:
instructions, data_segment = parse_assembly(filename)
except Exception as e:
print(f"Error: {e}")
return
configs = [
{"size": 32, "assoc": 1, "name": "32B 1-way"},
{"size": 64, "assoc": 1, "name": "64B direct-mapped"},
{"size": 128, "assoc": 1, "name": "128B direct-mapped"},
{"size": 128, "assoc": 2, "name": "128B 2-way"},
{"size": 256, "assoc": 2, "name": "256B 2-way"},
{"size": 512, "assoc": 2, "name": "512B 2-way"},
{"size": 512, "assoc": 4, "name": "512B 4-way"},
{"size": 1024, "assoc": 2, "name": "1024B 2-way"},
{"size": 1024, "assoc": 4, "name": "1024B 4-way"},
]
print(f"\n{'=' * 70}")
print(f" CACHE CONFIGURATION COMPARISON")
print(f"{'=' * 70}")
print(f"\nProgram: {filename}")
print(f"Instructions: {len(instructions)}")
if data_segment:
print(f"Data segment: {len(data_segment)} words")
print(f"\nTesting {len(configs)} cache configurations...\n")
print(f"{'Configuration':<25} {'Hit Rate':<12} {'Latency':<12} {'Speedup':<10} {'AMAT'}")
print("-" * 70)
baseline_latency = None
results = []
for config in configs:
cache = Cache(
size=config['size'],
line_size=16,
associativity=config['assoc'],
write_policy='write-back'
)
instr_copy = copy.deepcopy(instructions)
pipeline = Pipeline(instr_copy, cache=cache, verbose=False)
for address, value in data_segment.items():
pipeline.memory.data[address >> 2] = value
pipeline.run()
cache_stats = cache.get_stats()
mem_stats = pipeline.memory.get_stats()
if baseline_latency is None:
baseline_latency = mem_stats['total_latency']
speedup_str = "baseline"
else:
speedup = baseline_latency / mem_stats['total_latency']
speedup_str = f"{speedup:.2f}"
# Store results
results.append({
'config': config,
'hit_rate': cache_stats['hit_rate'],
'latency': mem_stats['total_latency'],
'amat': cache_stats['amat'],
'speedup': speedup_str
})
print(f"{config['name']:<25} {cache_stats['hit_rate'] * 100:>6.1f}% "
f"{mem_stats['total_latency']:>6} cy {speedup_str:>8} "
f"{cache_stats['amat']:>6.2f} cy")
# Analysis
print(f"\n{'=' * 70}")
print(" ANALYSIS")
print(f"{'=' * 70}\n")
# best config
best_by_hit_rate = max(results, key=lambda x: x['hit_rate'])
best_by_speedup = max(results, key=lambda x: float(x['speedup'].rstrip('x')) if x['speedup'] != 'baseline' else 0)
print(f"Best hit rate: {best_by_hit_rate['config']['name']} ({best_by_hit_rate['hit_rate'] * 100:.1f}%)")
if best_by_speedup['speedup'] != 'baseline':
print(f"Best speedup: {best_by_speedup['config']['name']} ({best_by_speedup['speedup']})")
print(f"\nRECOMMENDATIONS:")
max_improvement_idx = 0
max_improvement = 0
for i in range(1, len(results)):
prev_latency = results[i - 1]['latency']
curr_latency = results[i]['latency']
improvement = prev_latency - curr_latency
if improvement > max_improvement:
max_improvement = improvement
max_improvement_idx = i
if max_improvement_idx > 0:
recommended = results[max_improvement_idx]
speedup = results[0]['latency'] / recommended['latency']
print(f" {recommended['config']['name']}: Best cost/performance ratio")
print(f" ({speedup:.2f}x speedup over baseline, {recommended['hit_rate'] * 100:.0f}% hit rate)")
print(f" Beyond this: diminishing returns (< 10% additional speedup)")
else:
print(f" Smallest cache sufficient for this workload, we can really pick almost anything and it wouldn't matter")
print()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 cache_compare.py <file.s>")
sys.exit(1)
compare_caches(sys.argv[1])