Newer
Older
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
import random
import pprint
import sys
# Each insn is a python dictionary (map) with the following fields:
#
# src1 - operand #1 source register id
# src2 - operand #2 source register id
# dst - destination register id
#
# Register id's are just integers. There are 16 architectural
# registers in this simple ISA, with id's 0 to 15.
#
# There are two additional fields which you should fill in as you
# schedule instructions according to their dependences.
#
# consumer_insns - list of insns that consume output of this insn
# depth - depth of insn in the scheduling tree
def gen_insns(n):
# Unique root insn to make sure our dependencies form a tree.
insns = [ {'src1':0, 'src2':0, 'dst':0,
'consumer_insns':[], 'depth':0} ]
live_regs = set([0]) # start out with only 1 live reg
for i in range(n):
insn = {}
insn['src1'] = random.choice(list(live_regs))
insn['src2'] = random.choice(list(live_regs))
insn['dst'] = random.randint(0, 15)
live_regs.add(insn['dst'])
# Used for building dependence tree.
insn['consumer_insns'] = []
# Used for calculating program latency.
insn['depth'] = None
insns.append(insn)
return insns
instr_map = {0:0}
write_instr_counter = 0
def rename(insn):
global write_instr_counter
insn['osrc1'] = insn['src1']
insn['osrc2'] = insn['src2']
insn['odst'] = insn['dst']
if insn['src1'] not in instr_map:
instr_map[insn['src1']] = write_instr_counter
write_instr_counter += 1
insn['src1'] = instr_map[insn['src1']]
if insn['src2'] not in instr_map:
instr_map[insn['src2']] = write_instr_counter
write_instr_counter += 1
insn['src2'] = instr_map[insn['src2']]
write_instr_counter += 1
instr_map[insn['dst']] = write_instr_counter
insn['dst'] = write_instr_counter
def compute_latency(insns):
def find_producer(insns, r, instr):
for i in insns:
if i['dst'] == r:
if instr not in i['consumer_insns']:
i['consumer_insns'].append(instr)
return i['depth']
return -1
lat = 0
for i in insns:
if i['depth'] is not None:
continue
i['depth'] = max(find_producer(insns, i['src1'], i), find_producer(insns, i['src2'], i))+1
lat = max(lat, i['depth'])
return lat + 1
def compute_max_width(insns):
d = {}
for i in insns:
if i['depth'] not in d:
d[i['depth']] = 1
else:
d[i['depth']] += 1
res = 0
for k in d:
res = max(res, d[k])
return res
def compute_max_pregs(insns):
def get_instr_by_depth(insns):
tmp = {}
res = []
max_depth = 0
for i in insns:
if i['depth'] not in tmp:
tmp[i['depth']] = [i]
else:
tmp[i['depth']].append(i)
tmp[i['depth']].sort(key=lambda x : x['number'])
max_depth = max(max_depth, i['depth'])
for i in range(max_depth+1):
res.append(tmp[i])
return res
def get_commit_order(insns):
insns = get_instr_by_depth(insns)
buffer = []
res = []
for i in insns:
buffer = buffer + i
buffer.sort(key=lambda x:x['number'])
tmp = [buffer[0]]
i = 1
while i < len(buffer) and buffer[i-1]['number']+1 == buffer[i]['number']:
tmp.append(buffer[i])
i += 1
res.append(tmp)
if i == len(buffer):
buffer = []
else:
buffer = buffer[i:]
return res
def get_reg_allocation(insns):
by_depth = get_instr_by_depth(insns)
res = []
for i in range(len(by_depth)):
if i == 0:
tmp = set()
else:
tmp = set(res[len(res) - 1])
for ins in by_depth[i]:
tmp.add(ins['src1'])
tmp.add(ins['src2'])
tmp.add(ins['dst'])
res.append(tmp)
return res
def count_reg_usage(insns, reg):
res = 0
for ins in insns:
if reg == ins['src1']:
res += 1
if reg == ins['src2']:
res += 1
if reg == ins['dst']:
res += 1
return res
def get_reg_free(insns):
commit_order = get_commit_order(insns)
not_commited = insns[:]
commited = []
res = []
for i in commit_order:
s = set()
if len(res) != 0:
s = set(res[len(res)-1])
for ins in i:
not_commited.remove(ins)
commited.append(ins)
for ins in i:
if count_reg_usage(not_commited, ins['src1']) == 0:
s.add(ins['src1'])
if count_reg_usage(not_commited, ins['src2']) == 0:
s.add(ins['src2'])
if count_reg_usage(not_commited, ins['dst']) == 0:
s.add(ins['dst'])
res.append(s)
return res
def get_reg_free2(insns):
for i,ins in enumerate(insns):
if ins['odst'] == ins['osrc1']:
ins['free'] = ins['src1']
continue
elif ins['odst'] == ins['osrc2']:
ins['free'] = ins['src2']
continue
elif i > 0:
for j in reversed(insns[:i]):
if j['odst'] == ins['odst']:
ins['free'] = j['dst']
break
elif j['osrc1'] == ins['odst']:
ins['free'] = j['src1']
break
elif j['osrc2'] == ins['odst']:
ins['free'] = j['src2']
break
commit_order = get_commit_order(insns)
res = []
for instructions in commit_order:
tmp = []
if len(res) != 0:
tmp = res[len(res) - 1][:]
for j in instructions:
if 'free' in j:
tmp.append(j['free'])
res.append(tmp)
return res
#return [[i['free'] for i in instctions if 'free' in i] for instctions in commit_order]
for i,ins in enumerate(insns):
ins['number'] = i
reg_free = get_reg_free2(insns)
reg_alloc = get_reg_allocation(insns)
mx = len(reg_alloc[0])
for i in range(1, len(reg_alloc)):
tmp = len(reg_alloc[i]) - len(reg_free[i-1])
mx = max(mx,tmp)
return mx
def main(insns):
# First, rename insns to remove false dependences.
for i in insns:
rename(i)
results = {}
# Compute latency (versus serial).
results['latency'] = compute_latency(insns)
results['serial latency'] = len(insns)
# Compute max machine width used (max number of insns that
# executed in parallel).
results['max machine width used'] = compute_max_width(insns)
# Compute max number of pregs used.
results['max pregs used'] = compute_max_pregs(insns)
return repr(results)
if __name__ == "__main__":
# Edit this line to run with different trace files (or pass a
# filename on the command name).
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/parallel-1tick-6width-7pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/serial-6ticks-1width-3pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/test-3ticks-1width-4pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/test-3ticks-2width-5pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/test-4ticks-2width-7pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/random-3ticks-3width-4pregs.insns"
#filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/random-4ticks-2width-6pregs.insns"
filename = "/Users/svystoro/Downloads/hw3-ilp-scheduler/random-5ticks-4width-6pregs.insns"
if len(sys.argv) > 1:
filename = sys.argv[1]
pprint.pprint(main( eval(open(filename).read()) ))
# Uncomment this line to run with a random trace instead.
# pprint.pprint(main( gen_insns(5) ))
# This code below will dump a random trace of 5 insns to the
# terminal, so you can save it as a file and read it back in later.
# pprint.pprint(gen_insns(5))