forked from JCMoure/RVCAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
669 lines (511 loc) · 23.7 KB
/
Copy pathscheduler.py
File metadata and controls
669 lines (511 loc) · 23.7 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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
from copy import copy
import json
from .window import Window, InstrState
from .program import Program
from .processor import Processor, _processor
from .instruction import Instruction, MemType
from . import exec_graph as ex
global _scheduler
class Scheduler:
def __init__(self, processor: Processor) -> None:
self.processor = processor
def load_program(self, program: Program, iterations: int=3, window_size: int=100) -> None:
self.program = program
self.program.load_program()
self.processor.reset()
self.iterations = iterations
self.window_size= window_size
self.window = Window(window_size)
self.n = iterations*self.program.n
self.pc = 0
self.cycles = 0
self.DepEdges = program.generate_dependence_info()
def next_cycle(self) -> int:
xw = self.processor.stages["execute"]
rw = self.processor.stages["retire"]
sched = self.processor.sched != "greedy"
issue_queue = {}
used_ports = {port:False for port in self.processor.ports}
MM_access = -1
for window_idx in range(self.window.count):
instr = self.window[window_idx]
static_idx = instr.s_idx
dynamic_idx = instr.d_idx
if instr.state == InstrState.WRITE_BACK:
if rw:
if window_idx == 0 or self.window[window_idx-1].state == InstrState.RETIRE:
instr.state = InstrState.RETIRE
instr.substate = InstrState.NONE
rw -= 1
else:
instr.substate = InstrState.WAIT_RETIRE
else:
instr.substate = InstrState.WAIT_RETIRE
elif instr.state == InstrState.EXECUTE:
instr.latency -= 1
if instr.latency == 0:
if instr.substate == InstrState.NONE and instr.memory != MemType.NONE:
# Memory instruction: Cache access
mType = 0 if instr.memory == MemType.LOAD else 1
instr.latency, result, MM_access = self.processor.cache_access(mType, instr.memAddr, self.cycles)
instr.exec_lat += instr.latency # add extra latency in case of cache miss
if instr.latency > 0:
if result == 1:
instr.substate = InstrState.WAIT_CACHE_MISS
else:
instr.substate = InstrState.WAIT_CACHE_2ND
else:
instr.state = InstrState.WRITE_BACK
instr.substate = InstrState.NONE
elif instr.state == InstrState.DISPATCH:
if instr.substate in [InstrState.NONE, InstrState.WAIT_DATA]:
# need to check data dependencies
instr.substate = InstrState.NONE
for dependence_offset in self.DepEdges[static_idx]:
instr_dep = self.window.get_instr( dynamic_idx - dependence_offset )
if not instr_dep: # instruction is no longer in the window
continue
else:
if instr_dep.state not in [InstrState.WRITE_BACK, InstrState.RETIRE]:
instr.substate = InstrState.WAIT_DATA
break
if instr.substate != InstrState.WAIT_DATA:
instr_type = self.program[static_idx].type
resource = self.processor.get_resource(instr_type)
latency, ports = resource
if not sched: # Greedy scheduling algorithm
if xw:
for port in ports:
if not used_ports[port]:
used_ports[port] = True
instr.exec_cycle = self.cycles
instr.latency = latency
instr.exec_lat += latency
instr.state = InstrState.EXECUTE
instr.substate = InstrState.NONE
instr.port_used = port
xw -= 1
break
if instr.state == InstrState.DISPATCH:
instr.substate = InstrState.WAIT_RESOURCE
instr.exec_lat += 1
else:
instr.substate = InstrState.WAIT_BANDWIDTH
instr.exec_lat += 1
else: # Optimal scheduling: first priority is age, second is using most ports
issue_queue[window_idx] = ports
instr.latency = latency
elif instr.state != InstrState.RETIRE:
print("ERROR")
if sched: # Improved scheduling algorithm
issd_isps = ex.old_priority(issue_queue)
for w_idx in issue_queue:
instr = self.window[w_idx]
instr.substate = InstrState.NONE
if xw:
if w_idx in issd_isps:
port = issd_isps[w_idx]
instr.port_used = port
used_ports[port] = True
instr.exec_cycle = self.cycles
instr.state = InstrState.EXECUTE
instr.exec_lat += instr.latency
xw -= 1
else:
instr.substate = InstrState.WAIT_RESOURCE
instr.exec_lat += 1
else:
instr.substate = InstrState.WAIT_BANDWIDTH
instr.exec_lat += 1
retires = self.processor.stages["retire"] - rw
return retires, used_ports, MM_access
def dispatch(self):
dw = self.processor.stages["dispatch"]
while dw and not self.window.is_full():
static_idx = self.pc % self.program.n
instr = self.program[static_idx]
if self.processor.cache != None:
instr_mem = instr.memory
addr = instr.nextaddr
if instr_mem != MemType.NONE:
instr.nextaddr = addr + instr.stride
instr.count +=1
if instr.count == instr.N:
instr.count = 0
instr.nextaddr = instr.addr
else:
addr = -1
else:
instr_mem = MemType.NONE
addr = -1
self.window.push(self.cycles, self.pc, static_idx, instr_mem, addr)
self.pc += 1
dw -= 1
self.cycles += 1
def generate_timeline_state ( self, dynamic_idx, stages, critical_path):
# Decode Stage
decode_stage = stages[0].value + ' '
stages.pop(0)
if len(critical_path) > 0:
node = critical_path[-1]
idx = node[0] // 3
stage= node[0] % 3
if idx == dynamic_idx and stage == 0:
if node[1] == 1:
decode_stage = "\033[91m" + decode_stage + "\033[0m"
critical_path.pop(-1)
while stages[0] == InstrState.WAIT_DATA:
decode_stage += stages[0].value + ' '
stages.pop(0)
# Execute Stage
execute_stage = ""
count = 0
if len(critical_path) > 0:
node = critical_path[-1]
idx = node[0] // 3
stage= node[0] % 3
if idx == dynamic_idx and stage == 1:
count = node[1]
execute_stage = "\033[91m"
critical_path.pop(-1)
while stages[0] != InstrState.RETIRE:
execute_stage += stages[0].value + ' '
stages.pop(0)
count = count-1
if count == 0:
execute_stage += "\033[0m"
# Retire Stage
retire_stage = "R "
if len(critical_path) > 0:
node = critical_path[-1]
idx = node[0] // 3
stage= node[0] % 3
if idx == dynamic_idx and stage == 2:
critical_path.pop(-1)
if node[1] == 1:
retire_stage = "\033[91m" + "R "+ "\033[0m"
return decode_stage + execute_stage + retire_stage
def generate_timeline(self):
rw = self.processor.stages["retire"]
timeline = {i:[] for i in range(self.n + self.window.size + rw)}
port_timeline = {port:[] for port in self.processor.ports}
MM_timeline = []
INSTR_Info = []
ExecGraph = ex.generate_execution_graph( self.program, self.n, self.window_size, self.DepEdges )
retired = 0
self.cycles = 0
last_ret_cycle = 0
last_disp_cycle = 0
while retired < self.n:
retires, used_ports, MM_access = self.next_cycle()
if MM_access >= 0:
MM_timeline.append(MM_access+1)
for port, used in used_ports.items():
port_timeline[port].append(used)
for i in range(retires):
r_instr = self.window[i]
dynamic_idx = r_instr.d_idx
if dynamic_idx != retired:
print("ERROR\n")
disp_latency = r_instr.disp_cycle - last_disp_cycle
last_disp_cycle = r_instr.disp_cycle
ret_latency = self.cycles - last_ret_cycle
last_ret_cycle = self.cycles
exec_latency = r_instr.exec_lat
INSTR_Info.append([r_instr.exec_cycle, r_instr.port_used, r_instr.memAddr])
timeline[ dynamic_idx ].append((self.cycles, r_instr.state))
ex.exec_graph_update ( ExecGraph, dynamic_idx, disp_latency, exec_latency, ret_latency )
retired += 1
if retired >= self.n:
break
self.window.pop(retires)
self.dispatch()
for instr in self.window:
if instr.substate != InstrState.NONE:
timeline[instr.d_idx].append((self.cycles, instr.substate))
else:
timeline[instr.d_idx].append((self.cycles, instr.state))
critical_path = ex.longest_path(ExecGraph)
return timeline, port_timeline, MM_timeline, INSTR_Info, critical_path
def compute_results( self, usage, cycles_per_iter, name ):
out = ""
if usage >= 0.98:
out += f"\033[96m"
v1_str = f"{100*usage:0.2f}"
v2_str = f"{usage*cycles_per_iter:0.2f}"
out += f" {name:^10}:\t{v1_str:^10}\t{v2_str:^12}\n"
if usage >= 0.98:
out += f"\033[0m"
return out
def format_analysis(self) -> str:
retired = 0
self.cycles = 0
last_ret_cycle = 0
last_disp_cycle = 0
ports = self.processor.ports
port_usage = {port:0 for port in ports}
ExecGraph = ex.generate_execution_graph( self.program, self.n, self.window_size, self.DepEdges )
while retired < self.n:
retires, used_ports, _ = self.next_cycle()
for port in ports:
if used_ports[port]:
port_usage[port] += 1
for i in range(retires):
r_instr = self.window[i]
dynamic_idx = r_instr.d_idx
if dynamic_idx != retired:
print("ERROR\n")
disp_latency = r_instr.disp_cycle - last_disp_cycle
last_disp_cycle = r_instr.disp_cycle
ret_latency = self.cycles - last_ret_cycle
last_ret_cycle = self.cycles
exec_latency = r_instr.exec_lat
ex.exec_graph_update ( ExecGraph, dynamic_idx, disp_latency, exec_latency, ret_latency )
retired += 1
if retired >= self.n:
break
self.window.pop(retires)
self.dispatch()
critical_path = ex.longest_path(ExecGraph)
cycles_per_iter = self.cycles / self.iterations
IPC = self.n / self.cycles
out = f"**** Performance Results ****\n\n"
out += f"Total Iterations= {self.iterations}, "
out += f"Total Instructions= {self.n}, "
out += f"Total cycles= {self.cycles}, "
out += f"IPC= {IPC:0.2f}\n\n"
value1_str = f"{cycles_per_iter:0.2f}"
out += f" Resource \t Usage(%) \t Cycles/iter.\n"
out += f" --------- \t----------\t ------------\n"
out += f" PROGRAM :\t \t{value1_str:^12}\n"
dw_usage = IPC / self.processor.stages["dispatch"]
xw_usage = IPC / self.processor.stages["execute"]
rw_usage = IPC / self.processor.stages["retire"]
out += self.compute_results( dw_usage, cycles_per_iter, "dispatch")
out += self.compute_results( xw_usage, cycles_per_iter, "execute" )
out += self.compute_results( rw_usage, cycles_per_iter, "retire" )
for port in ports:
usage = port_usage[port]/self.cycles
name = f"Port {port}"
out += self.compute_results( usage, cycles_per_iter, name)
if self.processor.cache != None:
MM_usage, MM_Rd_usage, RdMisses, WrMisses = self.processor.cache.statistics(self.cycles)
out += self.compute_results( MM_usage, cycles_per_iter, "MM total BW")
out += self.compute_results( MM_Rd_usage, cycles_per_iter, "MM read BW")
v_str = f"{RdMisses/self.iterations:0.2f}"
out += f" Read Misses:\t \t {v_str:^10}\n"
v_str = f"{WrMisses/self.iterations:0.2f}"
out += f" Write Misses:\t \t {v_str:^10}\n"
out += f"\n Critical Path\n -------------\n"
out += ex.critical_path_statistics(self.program, critical_path)
return out
def format_timeline(self, niters: int = 3):
global_n = self.n
self.n = niters * self.program.n
timeline, _, MM_timeline, INSTR_Info, critical_path = self.generate_timeline()
pad_iteration = len(str(self.iterations))
pad_i = len(str(self.program.n))
pad = pad_iteration + pad_i + 5
out_cycles = f"{' '*pad}{' '.join([str(c_i%10) for c_i in range(self.cycles)])}\n"
port_timeline = {}
for port in self.processor.ports:
port_timeline[port] = [False for i in range(self.cycles)]
Cycle = 0
usage = " "
for use_time in MM_timeline:
if use_time >= self.cycles:
break
usage += " "*(use_time-Cycle-1)+"# "
Cycle = use_time
name = "MM"
out_MM = f"{name:{pad_iteration+pad_i+2}} {usage}\n"
out_timeline = ""
pad_list = []
for i, cycles in timeline.items():
if not cycles or i >= self.n:
break
iteration = i // self.program.n
i_mod = i % self.program.n
if i_mod == 0:
pad_list.append([cycles[0][0],0])
pad_list[iteration][1] = cycles[0][0] + len(cycles)
for i, cycles in timeline.items():
if not cycles or i >= self.n:
break
iteration = i // self.program.n
i_mod = i % self.program.n
init_pad = pad_list[iteration][0] - 1
if init_pad < 0:
init_pad = 0
medium_pad= cycles[0][0]-init_pad
end_pad = pad_list[iteration][1] - len(cycles) - (init_pad+medium_pad)
stages = self.generate_timeline_state( i, [s for _,s in cycles], critical_path)
out_timeline += f"{' '*init_pad}[{iteration:{pad_iteration}},{i_mod:{pad_i}}]{' '*medium_pad}"
out_timeline += f"{stages}{' '*end_pad} "
out_timeline += f"{self.program.instr_str(i_mod)}"
out_timeline += f" (P.{INSTR_Info[i][1]})"
port_timeline[ INSTR_Info[i][1] ][ INSTR_Info[i][0] ] = True
if iteration == 0:
out_timeline += f" {self.program.instr_type_str(i_mod)}"
if INSTR_Info[i][2] >= 0:
out_timeline += f" [Addr= {INSTR_Info[i][2]}]"
out_timeline += "\n"
out_Ports = ""
for port, cycles in port_timeline.items():
usage = " ".join(["X" if used else " " for used in cycles])
out_Ports += f"P.{port:{pad_iteration+pad_i+2}} {usage}\n"
self.n = global_n
return out_cycles + out_Ports + out_MM + "\n" + out_cycles + out_timeline
def format_timeline2(self, niters: int = 3) -> str:
global_n = self.n
self.n = niters * self.program.n
timeline, port_timeline, MM_timeline, INSTR_Info, critical_path = self.generate_timeline()
pad_iteration = len(str(self.iterations))
pad_i = len(str(self.program.n))
pad = pad_iteration + pad_i + 5
out = f"{' '*pad}{' '.join([str(c_i%10) for c_i in range(self.cycles)])}\n"
for port, cycles in port_timeline.items():
usage = " ".join(["X" if used else " " for used in cycles])
out += f"P.{port:{pad_iteration+pad_i+2}} {usage}\n"
Cycle = 0
usage = " "
for use_time in MM_timeline:
if use_time >= self.cycles:
break
usage += " "*(use_time-Cycle-1)+"# "
Cycle = use_time
name = "MM"
out += f"{name:{pad_iteration+pad_i+2}} {usage}\n"
out += f"\n{' '*pad}{' '.join([str(c_i%10) for c_i in range(self.cycles)])}\n"
pad_list = []
for i, cycles in timeline.items():
if not cycles or i >= self.n:
break
iteration = i // self.program.n
i_mod = i % self.program.n
if i_mod == 0:
pad_list.append([cycles[0][0],0])
pad_list[iteration][1] = cycles[0][0] + len(cycles)
for i, cycles in timeline.items():
if not cycles or i >= self.n:
break
iteration = i // self.program.n
i_mod = i % self.program.n
init_pad = pad_list[iteration][0] - 1
if init_pad < 0:
init_pad = 0
medium_pad= cycles[0][0]-init_pad
end_pad = pad_list[iteration][1] - len(cycles) - (init_pad+medium_pad)
stages = self.generate_timeline_state( i, [s for _,s in cycles], critical_path)
out += f"{' '*init_pad}[{iteration:{pad_iteration}},{i_mod:{pad_i}}]{' '*medium_pad}"
out += f"{stages}{' '*end_pad} "
out += f"{self.program.instr_str(i_mod)}"
out += f" ({INSTR_Info[i][3]})"
if iteration == 0:
out += f" {self.program.instr_type_str(i_mod)}"
if INSTR_Info[i][4] >= 0:
out += f" [Addr= {INSTR_Info[i][4]}]"
out += "\n"
self.n = global_n
return out
def format_memtrace(self) -> str:
N_LOADs = 0
N_STOREs = 0
Trace = ""
TotalIter = self.n // self.program.n
Sz = self.program.n
for i in range(self.n):
instr = self.program[i % Sz]
instr_mem = instr.memory
if instr_mem != MemType.NONE: # Memory instruction
addr = instr.nextaddr
instr.nextaddr = addr + instr.stride
instr.count += 1
if instr.count == instr.N:
instr.count = 0
instr.nextaddr = instr.addr
mType = 0 if instr.memory == MemType.LOAD else 1
if mType == 0:
N_LOADs += 1
else:
N_STOREs += 1
pre, post = "", ""
blk = ""
if self.processor.cache != None: # Cache Access
lat, result, _ = self.processor.cache_access(mType, addr, i)
if lat > 0:
if result == 1: # Primary Cache Miss
block_number= addr // self.processor.blkSize
pre = "\033[91m"
blk = f"({block_number})"
post = f"\033[0m"
# else: Secondary Cache Miss
txt_addr = f"{addr}{blk}"
Trace += f"{pre}{txt_addr:7}{post}, "
if (N_LOADs + N_STOREs) % 12 == 0:
Trace += "\n"
out = f"**** Memory Trace of Execution ****\n"
out += f" Iterations= {TotalIter}, Instructions= {self.n}, "
v_str = f"{N_LOADs/TotalIter:0.2f}"
out += f"LOADs/Iter.= {v_str:^4}, "
v_str = f"{N_STOREs/TotalIter:0.2f}"
out += f"STOREs/Iter.= {v_str:^4}"
if self.processor.cache != None:
_, _, RdMisses, WrMisses = self.processor.cache.statistics(1)
v_str = f"{RdMisses/TotalIter:0.2f}"
out += f", \033[96m Rd Misses/Iter.= {v_str:^4}, "
v_str = f"{WrMisses/TotalIter:0.2f}"
out += f" Wr Misses/Iter.= {v_str:^4} \033[0m"
out += f"\n .... Address trace ....\n{Trace}\n"
return out
def format_analysis_json(self) -> str:
retired = 0
self.cycles = 0
last_ret_cycle = 0
last_disp_cycle = 0
ports = self.processor.ports
port_usage = {port:0 for port in ports}
ExecGraph = ex.generate_execution_graph( self.program, self.n, self.window_size, self.DepEdges )
while retired < self.n:
retires, used_ports, _ = self.next_cycle()
for port in ports:
if used_ports[port]:
port_usage[port] += 1
for i in range(retires):
r_instr = self.window[i]
dynamic_idx = r_instr.d_idx
if dynamic_idx != retired:
print("ERROR\n")
disp_latency = r_instr.disp_cycle - last_disp_cycle
last_disp_cycle = r_instr.disp_cycle
ret_latency = self.cycles - last_ret_cycle
last_ret_cycle = self.cycles
exec_latency = r_instr.exec_lat
ex.exec_graph_update ( ExecGraph, dynamic_idx, disp_latency, exec_latency, ret_latency )
retired += 1
if retired >= self.n:
break
self.window.pop(retires)
self.dispatch()
critical_path = ex.longest_path(ExecGraph)
cycles_per_iter = self.cycles / self.iterations
IPC = self.n / self.cycles
out = {}
out["total_iterations"] = self.iterations
out["total_instructions"] = self.n
out["total_cycles"] = self.cycles
out["ipc"] = IPC
out["cycles_per_iteration"] = cycles_per_iter
out["ports"] = {}
for port in ports:
usage = port_usage[port]/self.cycles
out["ports"][str(port)] = usage*100
if self.processor.cache != None:
MM_usage, MM_Rd_usage, RdMisses, WrMisses = self.processor.cache.statistics(self.cycles)
out["MM_usage"] = MM_usage
out["MM_read_usage"] = MM_Rd_usage
out["read_misses"] = RdMisses
out["write_misses"] = WrMisses
out["critical_path"] = ex.critical_path_statistics_json(self.program, critical_path)
return json.dumps(out)
_scheduler = Scheduler(_processor)