-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsched.pl
executable file
·1853 lines (1772 loc) · 62.6 KB
/
sched.pl
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
###################################
# Perl scheduler for ScaffCC
###################################
# By: Jeff Heckey
# Copyright: 2014 UCSB
# NOT INTENDED FOR PUBLIC DISTRIBUTION
###################################
#
# Status (2/Jul/14):
# Added width to RCP
# Status (3/Jun/14):
# Added move counts to metrics output
# Status (26/May/14):
# RCP optimizations round 1
# Status (22/May/14):
# Added "true" printing, which prints the op schedule of each simd region at a given timestep
# Status (13/May/14):
# Added tgate counts, modified output metrics to include more raw data
# Added flags for pretty print
# Minor optimizations (variable localization)
# Status (10/May/14):
# update_moves optimized
# other misc optimizations
# Status (7/May/14):
# find_lp now uses $obj->{ops} to iterate instead of ACPA schedule. ~33% system time speedup
# Status (5/May/14):
# Modified lpfs update ready to use hash instead of arrays (>10x speed up!)
# Status (2/May/14):
# Got rid of moves from stalled SIMD regions
# LPFS will SIMD sched opp regions
# Tested SIMD L=1, K=2; L=1, K=4
# Update 2: regression proofing for reschedule
# Status (1/May/14):
# Improved find_lp algorithm, now much faster
# Status (29/Apr/14):
# Enhancements to work with BWT (div/0 errors)
# Profiled code to speed it up (find_LP() is longest runtime)
# Status (26/Apr/14):
# Added metrics
# Status (25/Apr/14):
# RCP now using update_moves and revised update RCPQ and general clean up
# Update 2: Find next LP for completed SIMD region
# Update 3: Opportunistic SIMD scheduling added
# Status (24/Apr/14):
# Some partial fixes to schedule object for repeated passes.
# Status (23/Apr/14):
# Finished LPFS with moves and cleaned up
# Status (8/Apr/14):
# Finished a first pass of the LPFS scheduling
# Non-optimal - could recalculate longest paths from present point if a path is completed.
# Status (18/Mar/14):
# Contains CPR code for evaluating potential conflicts in SIMD regions
# Partially completed RCP code, in more structured form
#
#TODO:
# Finish clean up for repeat schedules
# Look for movement optimization in LPFS
# Modify to deal with registers
#
#use 5.012;
package Qubit;
sub new {
my $class = shift;
my $qubit = { 'name' => "",
'index' => 0,
'size' => 1,
'loc' => 0, # RCP [-1 = unalloc, 0 = mem, 1-n = simd-n]
'last_op' => -1,
'ops' => [] };
$qubit->{name} = shift;
if ( $qubit->{name} =~ /(.*?)(\d+)/ ) {
$qubit->{index} = $2;
} else {
$qubit->{index} = -1;
}
bless $qubit, $class;
return $qubit;
}
sub add_dep {
my $qubit = shift;
push ( @{ $qubit->{ops} }, shift );
}
sub get_dep {
my $qubit = shift;
my $idx = shift || -1; # return last element by default
return $qubit->{ops}->[$idx];
}
# Fetches qubit at latest timestep
# Return: 1 if qubit needs to be moved, 0 otherwise
sub rcp_fetch_qubit {
my $qubit = shift;
my $ts = shift;
my $simd = shift;
my $op = shift;
$qubit->{last_op}++;
if ( !defined $qubit->{ops}->[ $qubit->{last_op} ] ) {
printf "E: looking at $qubit->{name} out of range\n";
} elsif ( ::refaddr( $op ) != ::refaddr( $qubit->{ops}->[$qubit->{last_op}] ) ) {
printf "E: Out of order scheduling of qubit %s ops '%s' and '%s'\n",
$qubit->{name}, $op->{text}, $qubit->{ops}->[$qubit->{last_op}]->{text};
}
$qubit->{last_ts} = $ts;
if ( $qubit->{loc} != -1 and $qubit->{loc} != $simd ) {
$qubit->{loc} = $simd;
return 1;
}
$qubit->{loc} = $simd;
return 0;
}
# Moves qubit back to memory if it is not used in this timestep
# Return: 1 if qubit returns to memory
sub rcp_store_qubit {
my $qubit = shift;
my $curr_ts = shift;
if ( $qubit->{last_ts} != $curr_ts ) {
$qubit->{loc} = 0; # move to memory
return 1;
}
return 0; # Used in current timestep, so do not move to memory
}
package Op;
my $id = 1;
sub new {
my $class = shift;
my $op = { 'op' => '',
'rank' => 0,
'length' => 1,
'dist' => -1,
'top' => 1,
'bottom' => 1,
'args' => [],
'text' => '',
'id' => 0,
'slack' => 0,
'asap' => {'ts' => -1},
'alap' => {
'ts' => -1,
'sched' => 0
},
'acap' => {
'ts' => -1,
'sched' => 0
},
'lpfs' => {
'tag' => 0,
'path' => 0,
'followed' => 0,
'simd' => -1,
'ts' => -1
},
'rcp' => {
'simd' => -1,
'ts' => -1
},
'p_chaco' => -1,
'in_edges' => [],
'out_edges' => [] };
$op->{op} = shift;
$op->{rank} = shift;
$op->{length} = shift || 1;
$op->{id} = $id++ if ( $op->{op} ne "MOV" );
$op->{text} = "$op->{id}: $op->{op}";
bless $op, $class;
return $op;
}
# Add new args and updated dependencies as needed
sub add_qubit {
my $op = shift;
my $qubit = shift;
push @{ $op->{args} }, $qubit;
if ( my $dep = $qubit->get_dep(-2) ) {
push ( @{ $op->{'in_edges'} }, $dep );
push ( @{ $dep->{'out_edges'} }, $op );
$op->{top} = 0;
$dep->{bottom} = 0;
}
$op->{'text'} .= " $qubit->{name}";
}
sub print_fields {
my $op = shift;
foreach my $field ( @_ ) {
print "$op->{$field} ";
}
print "\n";
}
sub op_ready {
my $op = shift;
my $sched = shift;
my $ready = 1;
foreach my $parent ( @{ $op->{in_edges} } ) {
$ready = 0 if ( ! defined $parent->{$sched}->{ts} or $parent->{$sched}->{ts} == -1 );
}
return $ready;
}
sub get_ready_children {
my $op = shift;
my $sched = shift;
my @ready = ();
foreach my $child ( @{ $op->{out_edges} } ) {
push @ready, $child if ( $child->op_ready($sched) );
}
return @ready;
}
sub take_path {
my $op = shift;
my $path = shift;
$op->{lpfs}->{dist} = 0; # Clear distance for next path finding
$op->{lpfs}->{path} = $path;
$op->{lpfs}->{simd} = $path;
$op->{lpfs}->{followed} = 1;
}
# Fetch op's qubits if needed
# Return: list of qubits that need to move
sub rcp_fetch_op {
my $op = shift;
my $ts = shift;
my $simd = shift;
my @moves = ();
$op->{rcp}->{ts} = $ts;
$op->{rcp}->{simd} = $simd;
foreach my $qubit ( @{ $op->{args} } ) {
push @moves, $qubit if ( $qubit->rcp_fetch_qubit($ts, $simd, $op) );
}
return @moves;
}
# Store op's qubits if needed
# Return: list of qubits that need to move
sub rcp_store_op {
my $op = shift;
my $ts = shift;
my @moves = ();
foreach my $qubit ( @{ $op->{args} } ) {
push @moves, $qubit if ( $qubit->rcp_store_qubit($ts) );
}
return @moves;
}
package Schedule;
sub new {
my $class = shift;
my $obj = ();
$obj->{function} = shift;
bless $obj, $class;
$obj->{twidth} = 0; # cell width for printing
$obj->{threads} = 0; # max number of parallel ops
$obj->{length} = 0;
$obj->{top} = [];
$obj->{bottom} = {};
$obj->{first_op} = 0;
$obj->{last_op} = 0;
$obj->{op_cnt} = 0;
$obj->{qubits} = ();
$obj->{active_qubits} = {};
$obj->{ops} = [];
$obj->{asap} = {threads => 0};
$obj->{alap} = {threads => 0};
$obj->{ss} = $obj->new_ss();
$obj->{lpfs} = $obj->new_lpfs();
$obj->{rcp} = $obj->new_rcp();
return $obj;
}
sub new_ss {
my $obj = shift;
my $k = shift || $::SIMD_K;
my $d = shift || $::SIMD_D;
return {
op_cnt => 0, # counts gates and moves
moves => 0, # counts each move op
mts => 0, # counts only timesteps with moves
len => 0, # total time
tgates => 0, # timesteps that have a T gate
width => 0, # max SIMD regions
simd_k => $k,
simd_d => $d
};
}
sub new_lpfs {
my $obj = shift;
my $k = shift || $::SIMD_K;
my $d = shift || $::SIMD_D;
my $l = shift || $::SIMD_L;
my $refill = shift || $::refill;
my $opp = shift || $::opp;
foreach my $op ( @{ $obj->{ops} } ) { $op->{lpfs}->{followed} = 0; }
return {
op_cnt => 0, # counts gates and moves
moves => 0, # counts each move op
mts => 0, # counts only timesteps with moves
len => 0, # total time
tgates => 0, # timesteps that have a T gate
width => 0, # max SIMD regions
simd_k => $k,
simd_d => $d,
simd_l => $l, # simd regions to allocate only to paths
refill_simd => $refill,
opp_simd => $opp,
path => 1 # starting path id
};
}
sub new_rcp {
my $obj = shift;
my $k = shift || $::SIMD_K;
my $d = shift || $::SIMD_D;
my $w_op = shift || $::w_op;
my $w_dist = shift || $::w_dist;
my $w_slack = shift || $::w_slack;
return {
op_cnt => 0, # counts gates and moves
moves => 0, # counts each move op
mts => 0, # counts only timesteps with moves
len => 0, # last timestep
tgates => 0, # timesteps that have a T gate
width => 0, # max SIMD regions
simd_k => $k,
simd_d => $d,
w_op => $w_op,
w_dist => $w_dist,
w_slack => $w_slack
};
}
# Add an operation to the function
# Operation will be ASAP scheduled
sub add_op {
my $obj = shift;
my $op = shift;
my $time = 0;
my $dist = 1;
push @{ $obj->{ops} }, $op;
if ( $op->{top} ) {
push @{ $obj->{top} }, $op;
$op->{dist} = $dist;
} else {
foreach my $pre ( @{$op->{in_edges}} ) {
print "W: '$pre->{text}'\@$pre->{asap}->{ts} not assigned\n" if ($pre->{asap}->{ts} == -1);
$time = ($time, $pre->{asap}->{ts} + $pre->{length})[$time < $pre->{asap}->{ts} + $pre->{length}];
$dist = ($dist, $pre->{dist} + 1)[$dist < $pre->{dist} + 1];
$pre->{bottom} = 0 if ( $pre->{bottom} );
delete $obj->{bottom}->{$pre};
}
$op->{dist} = $dist;
}
$obj->{bottom}->{$op} = $op if ( $op->{bottom} );
$op->{asap}->{ts} = $time;
$op->{lpfs}->{dist} = $dist;
push @{ $obj->{asap}->{$time} }, $op;
#$obj->{asap}->{threads} = ::max ($obj->{asap}->{threads}, scalar @{ $obj->{asap}->{$time} });
$obj->{first_op} = $op if ( $obj->{asap}->{first_op} == 0 );
$obj->{last_op} = $op;
$obj->{twidth} = ::max ($obj->{twidth}, length $op->{text});
$obj->{length} = ($obj->{length}, $time+$op->{length})[$obj->{length} < $time+$op->{length}];
$obj->{asap}->{op_cnt}++;
$obj->{op_cnt}++;
}
sub draw_graph {
my $obj = shift;
my $file = shift;
my @colors = qw(white red orange yellow green blue indigo violet pink
darkorange gold limegreen cyan navy magenta coral
orangered darkgoldenrod chartreuse skyblue turquoise
plum);
open DOT, ">$file" or die "Unable to open Dot file $file\n";
# Print some header stuff
# Make the function name the root node and an "exit" node
print DOT <<HEADING;
digraph $obj->{function} {
graph [ rankdir=LR ];
Top \t[ label=\"$obj->{function}\", rank=0 ];
Bottom\t[ label=\"Return\", rank=$obj->{length} ];
HEADING
# Loop over {ops} for all intermediate nodes
foreach my $op ( @{ $obj->{ops} } ) {
#print DOT " $op->{id}\t[ label=\"$op->{op}\" ];\n";
print DOT " $op->{id}\t[ label=\"$op->{op}$op->{id}\"";
print DOT ", style=filled, fillcolor=$colors[$op->{lpfs}->{path}]";
print DOT ", rank=$op->{dist}";
print DOT " ];\n";
}
print DOT "\n";
# Loop over all qubits for each edge (safer than trying to infer/rebuild from op perspective)
while ( my ($name, $qubit) = each %{ $obj->{qubits} } ) {
my $last = {'id' => 'Top'};
foreach my $op ( @{ $qubit->{ops} } ) {
print DOT " $last->{id} -> $op->{id}\t[ label=\"$name\" ];\n";
$last = $op;
}
print DOT " $last->{id} -> Bottom\t[ label=\"$name\" ];\n";
}
print DOT "}";
close DOT;
}
# Start by sceduling all of the bottom-most times
# Then recursively call all available dependencies
sub alap {
my $obj = shift;
my @recurse = ();
$obj->{alap}->{op_cnt} = 0;
foreach my $op_ref ( keys $obj->{bottom} ) {
my $op = $obj->{bottom}->{$op_ref};
$op->{alap}->{sched} = 1;
$op->{alap}->{ts} = $obj->{length} - $op->{length};
$op->{slack} = $op->{alap}->{ts} - $op->{asap}->{ts};
print "W: $op->{text} has negative slack $op->{slack} (ASAP: $op->{asap}->{ts}, ALAP: $op->{alap}->{ts})\n" if ( $op->{slack} < 0 );
push @{ $obj->{alap}->{$op->{alap}->{ts}} }, $op;
$obj->{alap}->{threads} = ::max ($obj->{alap}->{threads}, scalar @{ $obj->{alap}->{$op->{alap}->{ts}} });
$obj->{alap}->{op_cnt}++;
foreach my $pre ( @{ $op->{in_edges} } ) {
my $next = 1;
foreach my $dep ( @{ $pre->{out_edges} } ) {
$next &= $dep->{alap}->{sched};
}
if ( $next ) {
$pre->{alap}->{ts} = $obj->{length};
# Search preferred to hash because most lists will be short
# (<3 elements, max <20)
push(@recurse, $pre) unless grep{$_ == $pre} @recurse;
}
}
}
$obj->alap_recurse("alap",@recurse);
if ($obj->{op_cnt} != $obj->{alap}->{op_cnt}) {
print "W: $obj->{function} op counts mismatch (Sched: $obj->{op_cnt}, ALAP: $obj->{alap}->{op_cnt})\n";
}
}
sub alap_recurse {
my $obj = shift;
my $type = shift;
my @recurse = ();
foreach my $op ( @_ ) {
$op->{$type}->{sched} = 1;
foreach my $dep ( @{ $op->{out_edges} } ) {
$op->{$type}->{ts} = min ($op->{$type}->{ts}, $dep->{$type}->{ts} - $op->{length});
}
if ($type eq "alap") {
$op->{slack} = $op->{alap}->{ts} - $op->{asap}->{ts};
print "W: $op->{text} has negative slack $op->{slack} (ASAP: $op->{asap}->{ts}, ALAP: $op->{alap}->{ts})\n" if ( $op->{slack} < 0 );
}
push @{ $obj->{$type}->{$op->{$type}->{ts}} }, $op;
$obj->{$type}->{threads} = ::max ($obj->{$type}->{threads}, scalar @{ $obj->{$type}->{$op->{$type}->{ts}} });
$obj->{$type}->{op_cnt}++;
foreach my $pre ( @{ $op->{in_edges} } ) {
my $next = 1;
foreach my $dep ( @{ $pre->{out_edges} } ) {
$next &= $dep->{$type}->{sched};
}
if ( $next ) {
$pre->{$type}->{ts} = $obj->{length};
# Search preferred to hash because most lists will be short (<3 elements)
push(@recurse, $pre) unless grep{$_ == $pre} @recurse;
}
}
}
$obj->alap_recurse($type,@recurse) if ( scalar @recurse );
}
# As Centered As Possble scheduling
sub acap {
my $obj = shift;
my $type = "acap";
my $mid = $obj->{length} >> 1; # divide by two, without decimal
my @recurse = ();
# Copy second half of ASAP schedule to end of ACAP schedule
for (my $ts=$mid; $ts < $obj->{length}; $ts++) {
foreach my $op ( @{ $obj->{asap}->{$ts} } ) {
push @{ $obj->{$type}->{$ts} }, $op;
$op->{$type}->{ts} = $ts;
$op->{$type}->{sched} = 1;
$obj->{$type}->{threads} = ::max ($obj->{$type}->{threads}, scalar @{ $obj->{$type}->{$ts} });
$obj->{$type}->{op_cnt}++;
# Build recursive list of next operations
foreach my $pre ( @{ $op->{in_edges} } ) {
if ( ! $pre->{acap}->{sched} ) {
my $next = 1;
foreach my $dep ( @{ $pre->{out_edges} } ) {
$next &= $dep->{$type}->{sched};
}
if ( $next ) {
$pre->{$type}->{ts} = $obj->{length};
# Search preferred to hash because most lists will be short
# (<3 elements, max <20) and hashes are expensive in memory
push(@recurse, $pre) unless grep{$_ == $pre} @recurse;
}
}
}
}
}
$obj->alap_recurse($type,@recurse) if ( scalar @recurse );
}
# LPFS Longest Path First Scheduling. Performs a breadth first search to
# find the longest path(s) in the graph and assign those directly to SIMD
# regions. All other operations are scheduled to remaining SIMD regions
# as available.
sub lpfs {
my $obj = shift;
my $ts = 0;
my $op_cnt = 0; # only ops, no moves
my $tgate = 0; # set if T/Tdag found
my $t_cnt = 0; # count of timesteps with a T/Tdag
my $width = 0;
my $ready = {};
my $simds = {};
my $pathsearch = 1; # 0 if all paths are discovered
my $simd_l = $obj->{lpfs}->{simd_l};
my $opp_simd = $obj->{lpfs}->{opp_simd};
my $refill = $obj->{lpfs}->{refill_simd};
# Find longest paths
for ( my $i=1; $pathsearch and $i<=$simd_l; $i++ ) {
@{ $simds->{$i} } = $obj->find_lp( $obj->{top} );
$pathsearch = 0 if ( ! @{ $simds->{$i} } );
}
$ready = $obj->lpfs_init();
while ( ( keys %$ready ) or $op_cnt < $obj->{op_cnt} ) {
$tgate = 0;
if ( $ts > $obj->{op_cnt} ) {
print "E: LPFS timestep $ts > op count $obj->{op_cnt}. Aborting.\n";
return;
}
my $next = {};
# Schedule all assigned paths if ready
foreach my $simd ( 1..$simd_l ) {
if ( $pathsearch and $refill and ! scalar @{ $simds->{$simd} } ) {
@{ $simds->{$simd} } = $obj->find_lp( ref values $ready );
$pathsearch = 0 if ( ! @{ $simds->{$simd} } );
}
my $op = $simds->{$simd}->[0];
if ( $obj->sched_op( "lpfs", $op, $ts, $simd ) ) {
shift @{ $simds->{$simd} };
$width = ($width,$simd)[$width < $simd];
$op_cnt++;
$tgate |= ( $op->{op} =~ /^(?:T|Tdag)$/ );
# add opportunistic scheduling to a scehduled simd region already doing the same op
if ( $opp_simd ) {
foreach ( $obj->lpfs_extract_optype( "lpfs", $op->{op}, $ready ) ) {
$obj->sched_op( "lpfs", $_, $ts, $simd );
$op_cnt++;
}
}
}
}
# Schedule any remaing ready tasks
for ( my $simd=$simd_l+1; $simd<=$obj->{lpfs}->{simd_k}; $simd++ ) {
# Remove all longest path ops from the front
my $scheduled = 0;
while ( ! $scheduled and ( keys $ready ) ) {
my $id = 0;
while ( $id = ::min(keys %$ready) and $ready->{$id}->{followed} ) {
print "W: followed!\n";
delete $ready->{$id};
}
if ( $opp_simd ) {
foreach ( $obj->lpfs_extract_optype( "lpfs", $ready->{$id}->{op}, $ready ) ) {
$obj->sched_op( "lpfs", $_, $ts, $simd );
$width = ($width,$simd)[$width < $simd];
$scheduled = 1;
$op_cnt++;
$tgate |= ( $_->{op} =~ /^(?:T|Tdag)$/ );
}
} else {
$scheduled = $obj->sched_op( "lpfs", $ready->{$id}, $ts, $simd );
if ( $scheduled ) {
$op_cnt++;
$tgate |= ( $ready->{$id}->{op} =~ /^(?:T|Tdag)$/ );
delete $ready->{$id};
$width = ($width,$simd)[$width < $simd];
}
}
}
}
# Update data moves
$obj->{lpfs}->{$ts}->{0} = $obj->update_moves("lpfs", $ts);
# Update ready list
$ready = $obj->lpfs_update_ready( $ts, $ready );
$ts++;
$t_cnt += $tgate;
}
print "E: ops mis-scheduled ($op_cnt out of $obj->{op_cnt})\n" if ( $op_cnt != $obj->{op_cnt} );
$obj->{lpfs}->{len} = $ts;
$obj->{lpfs}->{tgates} = $t_cnt;
$obj->{lpfs}->{width} = $width;
}
sub lpfs_init {
my $obj = shift;
my $ready = {};
$obj->{lpfs}->{op_cnt} = 0;
foreach $op ( @{ $obj->{top} } ) {
$ready->{$op->{id}} = $op if ( ! $op->{lpfs}->{followed} );
}
foreach my $op ( @{ $obj->{ops} } ) {
$op->{lpfs}->{ts} = -1;
$op->{lpfs}->{simd} = -1;
$op->{lpfs}->{dist} = 0;
$op->{lpfs}->{tag} = 0;
}
while ( my ($q, $qubit) = each %{ $obj->{qubits} } ) {
$qubit->{loc} = 0;
$qubit->{last_op} = -1;
}
return $ready;
}
sub lpfs_update_ready {
my $obj = shift;
my $ts = shift;
my $ready = shift;
my $next = {};
foreach my $simd ( keys %{ $obj->{lpfs}->{$ts} } ) {
if ( $simd != 0 ) {
foreach my $op ( @{ $obj->{lpfs}->{$ts}->{$simd} } ) {
foreach my $child ( $op->get_ready_children("lpfs") ) {
$ready->{$child->{id}} = $child if ( ! $child->{lpfs}->{followed} );
}
}
}
}
return $ready;
}
sub lpfs_extract_optype {
my $obj = shift;
my $sched = shift;
my $optype = shift;
my $ready = shift;
my @ret = ();
my $cnt = 0;
foreach $id ( keys %$ready ) {
if ( $ready->{$id}->{op} eq $optype and $cnt < $obj->{$sched}->{simd_d} ) {
push @ret, $ready->{$id};
delete $ready->{$id};
$cnt++;
}
}
return @ret;
}
sub find_lp {
my $obj = shift;
my $top = shift;
return () if ( ! @$top );
my @path = ();
my $id = 0x7fffffff;
my $op = ();
foreach my $op ( @{ $obj->{ops} } ) {
$op->{lpfs}->{dist} = 1 if ( ! $op->{lpfs}->{followed} );
};
foreach $op ( @$top ) {
$id = ($id, $op->{id})[$id > $op->{id}];
};
foreach $op ( @{ $obj->{ops} } ) {
if ( $op->{id} >= $id ) {
if ( $op->{lpfs}->{followed} ) {
$op->{lpfs}->{dist} = 0;
} else {
foreach my $child ( @{ $op->{out_edges} } ) {
my $dist = $op->{lpfs}->{dist} + 1;
$child->{lpfs}->{dist} = ($dist, $child->{lpfs}->{dist})[$dist < $child->{lpfs}->{dist}]
}
}
$path[0] = $op if ( ! $op->{lpfs}->{followed}
and $op->{lpfs}->{dist} > $path[0]->{lpfs}->{dist} );
}
}
return if ( ! @path );
PATH: while ( $path[0]->{lpfs}->{dist} > 1 ) {
my $dist = $path[0]->{lpfs}->{dist} - 1;
$path[0]->take_path( $obj->{lpfs}->{path} );
foreach my $parent ( @{ $path[0]->{in_edges} } ) {
if ( $parent->{lpfs}->{dist} == $dist ) {
unshift @path, $parent;
next PATH;
}
}
print "E: unable to find path from $path[0]->{text} (dist: $path[0]->{lpfs}->{dist})\n";
}
$path[0]->take_path( $obj->{lpfs}->{path} );
$obj->{lpfs}->{path}++;
return @path;
}
sub find_lp_orig {
my $obj = shift;
my $top = shift;
return () if ( ! @$top );
my $next = ();
my $curr = ();
my $tag = 0;
my @path = ();
my $ops = 0;
my $children = 0;
my $parents = 0;
my $acc = 0;
my $maxfan = 0;
@$next = @$top; # copy list instead of modifying
# Reset all distances before recomputing
foreach my $op ( @{ $obj->{ops} } ) {
$op->{lpfs}->{dist} = 1 if ( ! $op->{lpfs}->{followed} );
$op->{lpfs}->{tag} = 0;
};
while ( (my $len = scalar @$next) > 0 ) {
$acc += $len;
$max_fan = ($max_fan, $len)[$max_fan < $len];
$tag++;
$curr = $next;
$next = ();
foreach my $op ( @{ $curr } ) {
$ops++;
my $dist = $op->{lpfs}->{dist} + 1;
foreach my $child ( @{ $op->{out_edges} } ) {
$children++;
if ( ! $child->{lpfs}->{followed} ) {
$child->{lpfs}->{dist} = ($dist, $child->{lpfs}->{dist})[$dist < $child->{lpfs}->{dist}];
}
if ( ! $child->{lpfs}->{tag} ) {
$child->{lpfs}->{tag} = $tag;
push @$next, $child;
}
}
$path[0] = $op if ( ! $op->{lpfs}->{followed}
and $op->{lpfs}->{dist} > $path[0]->{lpfs}->{dist} );
}
print "Total ops: $obj->{op_cnt}; visited ops: $ops; children: $children; parents: $parents\n";
print "Levels: $tag; avg: ".($acc/$tag)."; max: $max_fan\n";
}
return if ( ! @path );
PATH: while ( $path[0]->{lpfs}->{dist} > 1 ) {
my $dist = $path[0]->{lpfs}->{dist} - 1;
$path[0]->take_path( $obj->{lpfs}->{path} );
foreach my $parent ( @{ $path[0]->{in_edges} } ) {
$parents++;
if ( $parent->{lpfs}->{dist} == $dist ) {
unshift @path, $parent;
$dist--;
next PATH;
}
}
print "E: unable to find path from $path[0]->{text} (dist: $path[0]->{lpfs}->{dist})\n";
}
if ( $::DEBUG > 20 ) {
print "Total ops: $obj->{op_cnt}; visited ops: $ops; children: $children; parents: $parents\n";
print "Levels: $tag; avg: ".($acc/$tag)."; max: $max_fan\n";
}
$path[0]->take_path( $obj->{lpfs}->{path} );
$obj->{lpfs}->{path}++;
return @path;
}
sub sched_op {
my $obj = shift;
my $sched = shift;
my $op = shift;
my $ts = shift;
my $simd = shift;
if ( defined $op and $op->op_ready($sched) ) {
push @{ $obj->{$sched}->{$ts}->{$simd} }, $op;
$obj->{$sched}->{op_cnt}++;
$op->{$sched}->{simd} = $simd;
$op->{$sched}->{ts} = $ts;
$op->{$sched}->{followed} = 1 if ( $sched eq "lpfs" );
return 1;
}
return 0;
}
sub extract_optype {
my $obj = shift;
my $sched = shift;
my $optype = shift;
my $ready = shift;
my $i = 0;
# Sort the ready list by optype and scan for start and end of desired optype
@$ready = sort {$a->{op} cmp $b->{op}} @$ready;
while ( $i < scalar @$ready and $ready->[$i]->{op} ne $optype ) { $i++; }
return if ( $i >= scalar @$ready );
my $s = $i;
# Make sure that we don't schedule too many ops to the SIMD region
my $size = scalar @{ $ready->[$i]->{args} };
my $cnt = $size;
while ( $i < scalar @$ready
and $ready->[$i]->{op} eq $optype
and $cnt+$size < $obj->{$sched}->{simd_d} ) {
$i++;
$cnt += $size;
}
my $l = $i - $s;
return splice @$ready, $s, $l;
}
sub update_moves {
my $obj = shift;
my $sched = shift;
my $ts = shift;
my $s = $obj->{$sched};
my $simd = 1;
my $op = ();
my $qubit = ();
my $moves = ();
my $active = $obj->{active_qubits};
my $curr = {};
my $next = {};
my @simd_active = (0) x ($s->{simd_k}+1);
# Get all current
foreach $simd ( 1..$s->{simd_k} ) {
$simd_active[$simd] = 1 if ( scalar @{ $s->{$ts}->{$simd} } );
foreach $op ( @{ $s->{$ts}->{$simd} } ) {
foreach $qubit ( @{ $op->{args} } ) {
$curr->{$qubit->{name}} = $simd;
}
}
}
foreach my $name ( keys %$active ) {
my $src = $active->{$name};
my $dst = $curr->{$name} || 0;
if ( $dst ) {
# qubit is reused, keep it
$next->{$name} = $dst;
delete $curr->{$name}; # remove for fetching
}
if ( ! $dst and ! $simd_active[ $src ] ) {
# Qubit doesn't need to move
$next->{$name} = $src;
}
if ( ( ( $dst ) and $dst != $src )
or ( ( ! $dst ) and $simd_active[ $src ] ) ) {
# Moved into new location or SIMD is active and need to move to memory
$qubit = $obj->{qubits}->{$name};
push @$moves, bless({
'op' => 'MOV',
'src' => $src,
'dst' => $dst,
'text' => "MOV $dst $src $name",
'args' => [$qubit]
},'Op');
$qubit->{loc} = $dst;
$s->{op_cnt}++;
$s->{moves}++;
}
}
foreach my $name ( keys %$curr ) {
# Qubit not active, fetch from mem
my $dst = $curr->{$name};
$next->{$name} = $dst;
$qubit = $obj->{qubits}->{$name};
push @$moves, bless({
'op' => 'MOV',
'src' => 0,
'dst' => $dst,
'text' => "MOV $dst 0 $name",
'args' => [$qubit]
},'Op');
$qubit->{loc} = $dst; # Qubit moved to SIMD
$s->{op_cnt}++;
$s->{moves}++;
}
$s->{mts}++ if ( @$moves );
$obj->{active_qubits} = $next;
@$moves = sort { $a->{text} cmp $b->{text} } @$moves;
return $moves;
}
sub update_moves_hold_for_stall {
my $obj = shift;
my $sched = shift;
my $ts = shift;
my $s = $obj->{$sched};
my $simd = 1;
my $op = ();
my $qubit = ();
my $moves = ();
my $curr = {};
my $prev = {};
foreach $simd ( 1..$s->{simd_k} ) {
my $pts = $ts-1;
# Find previous non-empty timestep for this simd region
while ( $pts >= 0 and ! scalar @{ $s->{$pts}->{$simd} } ) { $pts--; }
# get all previous qubits
if ( $pts >= 0 ) {
foreach $op ( @{ $s->{$pts}->{$simd} } ) {
foreach $qubit ( @{ $op->{args} } ) {
$prev->{$qubit->{name}} = $qubit->{loc};
}
}
}
# get all current qubits
foreach $op ( @{ $s->{$ts}->{$simd} } ) {
foreach $qubit ( @{ $op->{args} } ) {
$curr->{$qubit->{name}} = [ $simd, $qubit->{loc} ]; #dst, src
}
}
}
foreach my $name ( sort keys %$curr ) {
my $src = $curr->{$name}->[1];
my $dst = $curr->{$name}->[0];
$qubit = $obj->{qubits}->{$name};
if ( $src != $dst ) {
push @$moves, bless({
'op' => 'MOV',
'src' => $src,
'dst' => $dst,
'args' => ($qubit),
'text' => "MOV $dst $src $name",
'args' => [$qubit]
},'Op');
$qubit->{loc} = $dst; # Qubit moved to SIMD
$obj->{$sched}->{op_cnt}++;
$obj->{$sched}->{moves}++;
}
delete $prev->{$name} if ( exists $prev->{$name} );
}
foreach my $name ( sort {$b cmp $a} keys %$prev ) {
my $src = $prev->{$name};
my $dst = 0;
$qubit = $obj->{qubits}->{$name};
if ( @{ $s->{$ts}->{$src} } ) {
push @$moves, bless({
'op' => 'MOV',
'src' => $src,
'dst' => $dst,
'args' => ($qubit),
'text' => "MOV $dst $src $name",
'args' => [$qubit]
},'Op');
$qubit->{loc} = $dst; # Qubit moved to SIMD
$obj->{$sched}->{op_cnt}++;
$obj->{$sched}->{moves}++;
}
}
$obj->{$sched}->{mts}++ if ( @$moves );
@$moves = sort { $a->{text} cmp $b->{text} } @$moves;
return $moves;
}
sub update_moves_orig {
my $obj = shift;
my $sched = shift;
my $ts = shift;
my $moves = ();
my $prev = {};
# Get qubits from previous timestep
if ( $ts > 0 ) {
foreach my $simd ( sort keys %{ $obj->{$sched}->{$ts-1} } ) {
if ( $simd > 0 and scalar @{ $obj->{$sched}->{$ts}->{$simd} } ) {
foreach my $op ( @{ $obj->{$sched}->{$ts-1}->{$simd} } ) {
foreach my $qubit ( @{ $op->{args} } ) {
$prev->{$qubit->{name}} = $qubit;
}
}
}
}
}
foreach my $simd ( sort keys %{ $obj->{$sched}->{$ts} } ) {
foreach my $op ( @{ $obj->{$sched}->{$ts}->{$simd} } ) {
foreach my $qubit ( @{ $op->{args} } ) {
delete $prev->{$qubit->{name}}; # remove if used in previous timestep
if ( $qubit->{loc} == -1 ) {
#print STDERR "alloc\n";
$qubit->{loc} = $simd; # Qubit allocated to first SIMD use
} elsif ( $qubit->{loc} != $simd ) {
#print STDERR "moved#\n";
push @$moves, {
'op' => 'MOV',
'src' => $qubit->{loc},
'dst' => $simd,
'args' => ($qubit),
'text' => "MOV $simd $qubit->{loc} $qubit->{name}",
'args' => [$qubit]
};
$qubit->{loc} = $simd; # Qubit moved to SIMD
$obj->{$sched}->{op_cnt}++;
$obj->{$sched}->{moves}++;