-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sim.py
92 lines (81 loc) · 2.54 KB
/
Sim.py
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
from Predictor import Predictor
from Perceptron import Perceptron
from NeuralNetwork import NeuralNetwork
from SharedNeuralNetwork import SharedNeuralNetwork
from SharedHistoryPerceptron import SharedHistoryPerceptron
import sys
import time
"""
Evaluates perceptron branch predictor on given an input file path and N, the number of perceptrons
The input file should be as follows:
- each line represents a unique tuple of (address, taken/not taken)
- each line should contain 2 numbers separated by a comma
- taken and not taken should be represented as 1 and 0 respectively
- example test.txt
"""
if __name__ == '__main__':
# Input file path
filepath = sys.argv[1]
# predictor type; one of {"perceptron", "nn"}
pred_type = sys.argv[2].lower()
# Number of predictors
N = int(sys.argv[3])
# History length
L = int(sys.argv[4])
# output (optional)
dump = False
print(sys.argv)
if len(sys.argv) == 6:
dump = True
dumpfile = sys.argv[5]
print("dumping output to", dumpfile)
pred_class = Perceptron
if pred_type == "nn":
pred_class = NeuralNetwork
elif pred_type == "snn":
pred_class = SharedNeuralNetwork
elif pred_type == "shp":
pred_class = SharedHistoryPerceptron
# print("Predictor:", pred_class)
# initialize
p = Predictor(pred_class, N, L)
p_taken, p_not_taken = 0, 0
hits = 0
total = 0
start_time = time.time()
t = int(time.time())
# read test
f = open(filepath, "r")
lines = f.readlines()
nlines = len(lines)
f.close()
# main loop
for l in lines:
total += 1
addr, x = l.split(",")
addr, x = int(addr) % N, int(x) * 2 - 1 # translates 0s and 1s to -1s and 1s
y = p.predict(addr)
if y == 1:
p_taken += 1
else:
p_not_taken += 1
if x == y:
hits += 1
p.train(addr, x)
# Print progress
if t != int(time.time()):
t = int(time.time())
# print("accuracy:", hits / total, "...", total, "/", nlines, " done... Elapsed time:", int(t - start_time), "s.")
# print stats
output = print
if dump:
f = open(dumpfile, "a")
output = f.write
output("File:" + str(filepath) + "\n")
output("Predictor:" + str(pred_class) + "\n")
output("accuracy:" + str(hits / total) + "\n")
output("predicted taken:" + str(p_taken) + "\n")
output("predicted not taken:" + str(p_not_taken) + "\n")
# dump files
if dump:
f.close()