-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtaFactory.py
1280 lines (929 loc) · 40.4 KB
/
rtaFactory.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
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
import math, os, sys, re
from taskFactory import Task
from combinationGenerator import CombinationGenerator
class RTA:
def __init__(self, params):
required_params = ['num_of_cores']
self.allowed_schedulers = ['RT-Gang', 'RTG-Sync', 'RTG-Synci',
'h1-len-dsc', 'h2-lnr-hyb', 'h3-crt-pth', 'h4-mlt-scr',
'h5-lnr-hyb', 'h6-crt-pth', 'GFTP', 'GFTPi', 'Threaded',
'Threadedi']
for rp in required_params:
assert params.has_key(rp), ("%s is a required parameter "
"for RTA class" % (rp))
setattr(self, rp, params[rp])
self.gang_factory = CombinationGenerator(self.num_of_cores)
return
def __calc_task_scaling_factor(self, task, corunners):
worst_gang, max_demand = self.gang_factory.find_worst_corunner_gang(task, corunners)
scaling_factor = max(1, max_demand / 100.0)
return scaling_factor
def __get_corunners_of_task (self, subject_task, taskset):
corunner_taskset = []
for p in taskset:
for t in taskset[p]:
if t == subject_task:
continue
if (t.h + subject_task.h) <= self.num_of_cores:
corunner_taskset.append(t)
return corunner_taskset
def __scale_taskset(self, taskset):
scaling_factors = {}
for p in taskset:
for t in taskset[p]:
if t.h == self.num_of_cores:
# There cannot be any corunners of this task since it
# requires all the cores
scaling_factors[t] = 1.0
continue
corunner_taskset = self.__get_corunners_of_task(t, taskset)
if corunner_taskset != []:
scaling_factors[t] = self.__calc_task_scaling_factor(t, corunner_taskset)
continue
scaling_factors[t] = 1.0
for p in taskset:
for t in taskset[p]:
t.c *= scaling_factors[t]
return taskset
def __deep_copy_taskset(self, taskset):
copied_taskset = {}
for p in taskset:
copied_taskset[p] = [t.copy() for t in taskset[p]['Real']]
return copied_taskset
def __check_schedulability_gftp(self, tk, hp_tasks):
schedulable = False
rk_prev = tk.c
iIk = self.__calc_task_iIk(tk, hp_tasks)
while (1):
iWk = self.__calc_workload_iWk(rk_prev, hp_tasks)
interferenceFactor = sum([iIk [ti] * iWk [ti] for ti in hp_tasks])
rk_new = tk.c + math.floor(interferenceFactor)
if rk_new == rk_prev or rk_new > tk.p:
if rk_new == rk_prev:
schedulable = True
break
rk_prev = rk_new
return schedulable, rk_new
def __calc_workload_iWk(self, t, hp_tasks):
iWk = {}
for ti in hp_tasks:
delta_i = ti.p - hp_tasks[ti] + ti.c
if t <= delta_i:
iWk[ti] = min(t, ti.c)
else:
iWk[ti] = ti.c + ti.c * \
math.floor(float(t - delta_i) / ti.p) + \
min(ti.c, (t - delta_i) % ti.p)
return iWk
def __calc_task_iIk(self, tk, hp_tasks):
iIk = {}
for ti in hp_tasks:
iIk[ti] = float(min(ti.h, self.num_of_cores - tk.h + 1)) / \
(self.num_of_cores - tk.h + 1)
return iIk
def __get_gftp_scaled_taskset(self, taskset):
gftp_taskset = {}
for p in taskset:
gftp_taskset[p] = [t.copy() for t in taskset[p]['GFTP']]
return gftp_taskset
def run(self, taskset, scheduler, gen_dir = '', debug = False):
pq = []
self.__check_scheduler(scheduler)
if scheduler in ['h1-len-dsc', 'h2-lnr-hyb', 'h3-crt-pth',
'h4-mlt-scr', 'h5-lnr-hyb', 'h6-crt-pth']:
if scheduler == 'h1-len-dsc':
self.__form_virtual_gangs_heuristic_h1(taskset, scheduler, debug)
if scheduler == 'h2-lnr-hyb':
self.__form_virtual_gangs_heuristic_h2(taskset, scheduler, debug)
if scheduler == 'h3-crt-pth':
self.__form_virtual_gangs_heuristic_h3(taskset, scheduler, debug)
if scheduler == 'h4-mlt-scr':
self.__form_virtual_gangs_heuristic_h4(taskset, scheduler, debug)
if scheduler == 'h5-lnr-hyb':
self.__form_virtual_gangs_heuristic_h5(taskset, scheduler, debug)
if scheduler == 'h6-crt-pth':
self.__form_virtual_gangs_heuristic_h6(taskset, scheduler, debug)
if scheduler in ['GFTP', 'GFTPi', 'Threaded', 'Threadedi']:
if scheduler == 'GFTP':
taskset = self.__deep_copy_taskset(taskset)
taskset = self.__scale_taskset(taskset)
for p in taskset:
target_dir = gen_dir + str(p)
if debug:
print
print target_dir
print
assert os.path.exists(target_dir), ('Target folder <%s> '
'for GFTP does not exist.' % (target_dir))
gftp_file = target_dir + '/gftp.txt'
with open(gftp_file, 'w') as fdo:
fdo.write('======== Scaled GFTP Taskset =============\n')
fdo.write('\n'.join([t.__str__() for t in taskset[p]]))
fdo.write('\n')
else:
taskset = self.__deep_copy_taskset(taskset)
if scheduler in ['Threaded', 'Threadedi']:
taskset = self.__split_gangs_into_threads(taskset)
if scheduler == 'Threaded':
taskset = self.__scale_threadset(taskset)
periods = sorted(taskset.keys())
hp_tasks = {}
if debug:
print '===== GFTP'
print ' - Taskset:'
for p in taskset:
print ' * Period=%d' % (p)
print '\n'.join([' + %s' % t for t in taskset[p]])
for p in periods:
tasks = [t.copy() for t in taskset[p]]
# Scheduling jobs of the same period - SJF policy
keys = sorted(list(set([t.c for t in tasks])))
pQueues = {k:[] for k in keys}
# Populate priority-queues
for k in keys:
for t in tasks:
if t.c == k:
pQueues[k].append(t)
for k in keys:
for t in pQueues[k]:
schedulable, responseTime = self.__check_schedulability_gftp(t, hp_tasks)
if not schedulable:
if debug: print ' = Unschedulable!'
return 0
hp_tasks[t] = responseTime
if debug: print ' = Schedulable!'
return 1
if scheduler in ['RT-Gang', 'RTG-Sync', 'RTG-Synci', 'h1-len-dsc',
'h2-lnr-hyb', 'h3-crt-pth', 'h4-mlt-scr', 'h5-lnr-hyb',
'h6-crt-pth']:
pq, sched_test_1 = self.__create_rtg_pq(taskset, scheduler)
# Taskset has failed the preliminary schedulability test
if not sched_test_1:
return 0
# Taskset passed first schedulability test; apply full RTA
idx = 1
for tau in pq[1:]:
hp_tasks = pq[:idx]
schedulable, response_time = self.__check_schedulability(tau,
hp_tasks)
if not schedulable:
return 0
idx += 1
return 1
def __scale_threadset (self, taskset):
sorted_tasks = []
scaling_factors = {}
temp_taskset = [t for p in taskset for t in taskset[p]]
sorted_resource_demands = list(set(sorted([t.r for t in temp_taskset], reverse = True)))
for r in sorted_resource_demands:
for t in temp_taskset:
if t.r == r:
sorted_tasks.append(t)
temp_taskset.remove(t)
for p in taskset:
for t in taskset [p]:
worst_corunners = sorted_tasks[:self.num_of_cores]
if t in worst_corunners:
worst_corunners.remove(t)
else:
del (worst_corunners[-1])
max_demand = t.r + sum([w.r for w in worst_corunners])
scaling_factors[t] = max(1, max_demand / 100.0)
for p in taskset:
for t in taskset[p]:
t.c *= scaling_factors[t]
return taskset
def __split_gangs_into_threads(self, taskset):
threadset = {}
threadIdx = 1
for p in taskset:
threadset[p] = []
for t in taskset [p]:
for th in range(int(t.h)):
thread = Task(threadIdx, t.c, t.p, 1, t.r/t.h)
threadset[p].append(thread)
threadIdx += 1
return threadset
def __form_virtual_gangs_heuristic_h6(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
min_virt_gang = math.ceil(sum([t.h for t in candidate_set]) /
self.num_of_cores)
max_tasks_paths = self.__calc_nodes_in_longest_path(graph)
critical_length = int(max(min_virt_gang, max_tasks_paths))
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
print "\n * max_tasks_path: %d | critical_length: %d" % (
max_tasks_paths, critical_length)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
candidate_list = []
if debug:
print '-' * 50
print 'tk: %s' % (tk)
for tj in pq:
if debug: print 'tj: %s' % (tj)
if tk.h + tj.h > self.num_of_cores:
continue
if self.__are_related(tk, tj, graph):
continue
candidate_list.append(tj)
if debug:
print 'Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
candidate_list = self.__score_candidates_path2(tk, candidate_list, graph, critical_length, debug)
while len(candidate_list) != 0:
tc = self.__get_best_corunner(tk, candidate_list, graph)
if debug: print ' * Pairing: %s' % (tc)
sweep_list.append(tc)
tk = self.__create_virtual_task(tk, tc, vIdx, graph)
if debug: print ' * Vgang: %s' % (tk)
# We cannot pair any more tasks with ti
if tk.h == self.num_of_cores:
break
if tk.members == '':
tk.members = 't%d' % (tk.tid)
self.__scale_virtual_task(tk)
virtual_taskset.append(tk)
vIdx += 1
# Remove paired tasks from pq
for tx in sweep_list:
pq.remove(tx)
if debug:
print "\n[DEBUG] Virtual Set:"
self.__print_pq(virtual_taskset)
print "\nLength = %.2f" % (sum([t.c for t in virtual_taskset]))
print '\n', "-" * 78, '\n'
taskset[period][heuristic] = virtual_taskset
return
def __form_virtual_gangs_heuristic_h3(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
if debug:
print "[DEBUG]<%s> Candidate Set:" % (heuristic)
self.__print_pq(candidate_set)
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
candidate_list = []
if debug:
print '-' * 50
print 'tk: %s' % (tk)
for tj in pq:
if debug: print 'tj: %s' % (tj)
if tk.h + tj.h > self.num_of_cores:
continue
if self.__are_related(tk, tj, graph):
continue
candidate_list.append(tj)
if debug:
print 'Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
candidate_list = self.__score_candidates_path(tk, candidate_list, graph)
if debug:
print 'Ranked Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
while len(candidate_list) != 0:
tc = self.__get_best_corunner(tk, candidate_list, graph)
if debug: print ' * Pairing: %s' % (tc)
sweep_list.append(tc)
tk = self.__create_virtual_task(tk, tc, vIdx, graph)
if debug: print ' * Vgang: %s' % (tk)
# We cannot pair any more tasks with ti
if tk.h == self.num_of_cores:
break
if tk.members == '':
tk.members = 't%d' % (tk.tid)
self.__scale_virtual_task(tk)
virtual_taskset.append(tk)
vIdx += 1
# Remove paired tasks from pq
for tx in sweep_list:
pq.remove(tx)
if debug:
print "\n[DEBUG] Virtual Set:"
self.__print_pq(virtual_taskset)
print "\nLength = %.2f" % (sum([t.c for t in virtual_taskset]))
print '\n', "-" * 78, '\n'
taskset[period][heuristic] = virtual_taskset
return
def __form_virtual_gangs_heuristic_h4(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
num_cores = sum([t.h for t in candidate_set])
total_r = sum([t.r for t in candidate_set])
avg_r_core = total_r / float(num_cores)
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
candidate_list = []
if debug:
print '-' * 50
print 'tk: %s' % (tk)
for tj in pq:
if debug: print 'tj: %s' % (tj)
if tk.h + tj.h > self.num_of_cores:
continue
if self.__are_related(tk, tj, graph):
continue
candidate_list.append(tj)
if debug:
print 'Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
candidate_list = self.__score_candidates_h4(tk, candidate_list, avg_r_core, debug)
while len(candidate_list) != 0:
tc = self.__get_best_corunner(tk, candidate_list, graph)
if debug: print ' * Pairing: %s' % (tc)
sweep_list.append(tc)
tk = self.__create_virtual_task(tk, tc, vIdx, graph)
if debug: print ' * Vgang: %s' % (tk)
# We cannot pair any more tasks with ti
if tk.h == self.num_of_cores:
break
if tk.members == '':
tk.members = 't%d' % (tk.tid)
self.__scale_virtual_task(tk)
virtual_taskset.append(tk)
vIdx += 1
# Remove paired tasks from pq
for tx in sweep_list:
pq.remove(tx)
if debug:
print "\n[DEBUG] Virtual Set:"
self.__print_pq(virtual_taskset)
print "\nLength = %.2f" % (sum([t.c for t in virtual_taskset]))
print '\n', "-" * 78, '\n'
taskset[period][heuristic] = virtual_taskset
return
def __form_virtual_gangs_heuristic_h5(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
candidate_list = []
if debug:
print '-' * 50
print 'tk: %s' % (tk)
for tj in pq:
if debug: print 'tj: %s' % (tj)
if tk.h + tj.h > self.num_of_cores:
continue
if self.__are_related(tk, tj, graph):
continue
candidate_list.append(tj)
if debug:
print 'Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
candidate_list = self.__score_candidates_h5(tk, candidate_list)
if debug:
print 'Ranked Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
while len(candidate_list) != 0:
tc = self.__get_best_corunner(tk, candidate_list, graph)
if debug: print ' * Pairing: %s' % (tc)
sweep_list.append(tc)
tk = self.__create_virtual_task(tk, tc, vIdx, graph)
if debug: print ' * Vgang: %s' % (tk)
# We cannot pair any more tasks with ti
if tk.h == self.num_of_cores:
break
if tk.members == '':
tk.members = 't%d' % (tk.tid)
self.__scale_virtual_task(tk)
virtual_taskset.append(tk)
vIdx += 1
# Remove paired tasks from pq
for tx in sweep_list:
pq.remove(tx)
if debug:
print "\n[DEBUG] Virtual Set:"
self.__print_pq(virtual_taskset)
print "\nLength = %.2f" % (sum([t.c for t in virtual_taskset]))
print '\n', "-" * 78, '\n'
taskset[period][heuristic] = virtual_taskset
return
def __form_virtual_gangs_heuristic_h2(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
candidate_list = []
if debug:
print '-' * 50
print 'tk: %s' % (tk)
for tj in pq:
if debug: print 'tj: %s' % (tj)
if tk.h + tj.h > self.num_of_cores:
continue
if self.__are_related(tk, tj, graph):
continue
candidate_list.append(tj)
if debug:
print 'Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
candidate_list = self.__score_candidates(tk, candidate_list)
if debug:
print 'Ranked Candidates:'
print '\n'.join([' + ' + t.__str__() for t in candidate_list])
while len(candidate_list) != 0:
tc = self.__get_best_corunner(tk, candidate_list, graph)
if debug: print ' * Pairing: %s' % (tc)
sweep_list.append(tc)
tk = self.__create_virtual_task(tk, tc, vIdx, graph)
if debug: print ' * Vgang: %s' % (tk)
# We cannot pair any more tasks with ti
if tk.h == self.num_of_cores:
break
if tk.members == '':
tk.members = 't%d' % (tk.tid)
self.__scale_virtual_task(tk)
virtual_taskset.append(tk)
vIdx += 1
# Remove paired tasks from pq
for tx in sweep_list:
pq.remove(tx)
if debug:
print "\n[DEBUG] Virtual Set:"
self.__print_pq(virtual_taskset)
print "\nLength = %.2f" % (sum([t.c for t in virtual_taskset]))
print '\n', "-" * 78, '\n'
taskset[period][heuristic] = virtual_taskset
return
def __get_best_corunner(self, tk, candidate_list, graph):
tc = candidate_list[0]
rem_cores = self.num_of_cores - tk.h - tc.h
assert rem_cores >= 0, ('-ve remaining cores <%d> after gang '
'formation. \n - tk: %s\n - tc: %s' % (rem_cores, tk, tc))
candidate_list.remove(tc)
tmp_candidate_list = [tx for tx in candidate_list]
for tx in tmp_candidate_list:
if tx.h > rem_cores:
candidate_list.remove(tx)
continue
if self.__are_related(tc, tx, graph):
candidate_list.remove(tx)
return tc
def __calc_nodes_in_longest_path(self, graph):
x = 0
path = []
# Find out all the source nodes in this graph
sources = []
for node in graph:
predecessors = self.__get_predecessors(node.tid, graph)
if predecessors == []:
sources.append(node)
for s in sources:
sub_path = self.__calc_longest_path_from_source(s, graph)
sub_path_len = len(sub_path)
if sub_path_len > x:
path = sub_path
x = sub_path_len
return x
def __calc_paths_longer_than_crit_len(self, graph, crit_len, debug = False):
paths = []
# Find out all the source nodes in this graph
sources = []
for node in graph:
predecessors = self.__get_predecessors(node.tid, graph)
if predecessors == []:
sources.append(node)
for s in sources:
path = self.__calc_longest_path_from_source(s, graph)
path_len = len(path)
if path_len > crit_len:
paths.append(path)
return paths
def __calc_longest_path_from_source(self, s, graph):
path = []
max_nodes_path = []
max_nodes_path_len = 0
for vidx in s.e:
node = self.__get_task_by_tid(vidx, graph)
sub_path = self.__calc_longest_path_from_source(node, graph)
sub_path_len = len(sub_path)
if sub_path_len > max_nodes_path_len:
max_nodes_path = sub_path
max_nodes_path_len = sub_path_len
path = [s] + max_nodes_path
return path
def __score_candidates_path2(self, tk, candidate_list, graph,
critical_length, debug = False):
score_hash = {}
for tp in candidate_list:
# Create a tmp graph containing replicas of the tasks
tmp_graph = [t.copy() for t in graph]
tmp_vidx = max([t.tid for t in tmp_graph]) + 1
tk_copy = [t for t in tmp_graph if t.tid == tk.tid][0]
tp_copy = [t for t in tmp_graph if t.tid == tp.tid][0]
tmp_gang = self.__tmp_create_virtual_task(tk_copy, tp_copy, tmp_vidx, tmp_graph)
paths = self.__calc_paths_longer_than_crit_len(tmp_graph, critical_length, debug)
penalty = self.__calc_penalty(paths, critical_length)
score = tp.c - penalty
while score in score_hash:
score -= 0.001
score_hash[score] = tp
sorted_scores = sorted(score_hash.keys(), reverse = True)
scored_candidate_list = [score_hash[sc] for sc in sorted_scores]
if debug:
if candidate_list != []:
print ' - Scores:'
for sc in sorted_scores:
print ' + %.2f | %s' % (sc, score_hash[sc])
else:
print ' - No candidates!'
return scored_candidate_list
def __calc_penalty(self, paths, critical_length):
penalty = 0
for p in paths:
last_node_idx = len(p) - 1
x = len(p) - critical_length
assert x >= 0, ('Path is shorter than critical length <%d>: \n%s' %
(critical_length, '\n'.join([' + %s' % t for t in path])))
# xa = x
min_possible_len_this_path = sum([t.c for t in p[:x]])
for xa in range(0, x):
xb = x - xa
left_len = 0
right_len = 0
# xa nodes from beginning of p
for node in p[:xa]:
left_len += node.c
# xb nodes from end of p
if xb != 0:
for node in p[-xb:]:
right_len += node.c
path_len = left_len + right_len
if path_len < min_possible_len_this_path:
min_possible_len_this_path = path_len
if min_possible_len_this_path > penalty:
penalty = min_possible_len_this_path
return penalty
def __score_candidates_path(self, tk, candidate_list, graph):
score_hash = {}
for tc in candidate_list:
# Create a tmp graph containing replicas of the tasks
tmp_graph = [t.copy() for t in graph]
tmp_vidx = max([t.tid for t in tmp_graph]) + 1
tk_copy = [t for t in tmp_graph if t.tid == tk.tid][0]
tc_copy = [t for t in tmp_graph if t.tid == tc.tid][0]
tmp_gang = self.__tmp_create_virtual_task(tk_copy, tc_copy, tmp_vidx, tmp_graph)
score = self.__calc_crit_path(tmp_gang, tmp_graph)
while score in score_hash:
score += 1
score_hash[score] = tc
sorted_scores = sorted(score_hash.keys())
scored_candidate_list = [score_hash[sc] for sc in sorted_scores]
return scored_candidate_list
def __calc_crit_path(self, node, graph):
'''
Calculate the length of the 'critical' path for 'node'.
i.e., the longest path through the graph containing 'node'.
'''
left_path = self.__calc_left_crit_path(node, graph, True)
right_path = self.__calc_right_crit_path(node, graph, True)
crit_path_len = node.c + left_path + right_path
return crit_path_len
def __calc_left_crit_path(self, node, graph, first = False):
max_path_length = 0
predecessors = self.__get_predecessors(node.tid, graph)
for p in predecessors:
pt = self.__get_task_by_tid(p, graph)
length = self.__calc_left_crit_path(pt, graph)
if length > max_path_length:
max_path_length = length
if not first:
max_path_length += node.c
return max_path_length
def __calc_right_crit_path(self, node, graph, first = False):
max_path_length = 0
successors = node.e
for s in successors:
st = self.__get_task_by_tid(s, graph)
length = self.__calc_right_crit_path(st, graph)
if length > max_path_length:
max_path_length = length
if not first:
max_path_length += node.c
return max_path_length
def __score_candidates_h4(self, tk, candidate_list, avg_r_core, debug = False):
alpha = 1.0
score_hash = {}
for tp in candidate_list:
penalty = abs((tk.r + tp.r) / (tk.h + tp.h) - avg_r_core) / float(avg_r_core)
score = tp.c / (1 + penalty * alpha)
while score in score_hash:
score -= 0.0001
score_hash[score] = tp
sorted_scores = sorted(score_hash.keys(), reverse = True)
scored_candidate_list = [score_hash[sc] for sc in sorted_scores]
if debug:
print ' - avg_r_core=%.2f' % (avg_r_core)
print ' - Scores:'
for sc in sorted_scores:
print ' + %.2f | %s' % (sc, score_hash[sc])
return scored_candidate_list
def __score_candidates_h5(self, tk, candidate_list, debug = False):
alpha = 2.00
score_hash = {}
for tc in candidate_list:
penalty = tc.r / 100.0
score = tc.c / (1 + penalty * alpha)
while score in score_hash:
score -= 0.001
score_hash[score] = tc
sorted_scores = sorted(score_hash.keys(), reverse = True)
scored_candidate_list = [score_hash[sc] for sc in sorted_scores]
if debug:
if candidate_list != []:
print ' - Scores:'
for sc in sorted_scores:
print ' + %.2f | %s' % (sc, score_hash[sc])
else:
print ' - No candidates!'
return scored_candidate_list
def __score_candidates(self, tk, candidate_list):
score_hash = {}
for tc in candidate_list:
# Assume that we pair tc with tk and evaluate the resulting vgang
vg_demand = tc.r + tk.r
vg_scaled_length = tk.c * max(1.0, vg_demand / 100.0)
score = vg_scaled_length - tc.c
while score in score_hash:
score += 1
score_hash[score] = tc
sorted_scores = sorted(score_hash.keys())
scored_candidate_list = [score_hash[sc] for sc in sorted_scores]
return scored_candidate_list
def __form_virtual_gangs_heuristic_h1(self, taskset, heuristic,
debug = False):
vIdx = sum([len(taskset[p]['Real']) for p in taskset])+ 1
for period in taskset:
virtual_taskset = []
candidate_set = taskset[period]['Real']
pq, graph = self.__create_heuristic_pq(candidate_set, heuristic)
if debug:
print "[DEBUG]<%s> PQ:" % (heuristic)
self.__print_pq(pq)
while len(pq) != 0:
tk = pq.pop(0)
sweep_list = []
for tj in pq:
if tk.h + tj.h > self.num_of_cores:
continue
if debug:
print '=' * 50
print ' - ti:', tk
print ' - tj:', tj
if self.__are_related(tk, tj, graph, debug):
if debug:
print ' - ti=%d and tj=%d are related!' % (tk.tid,
tj.tid)
continue
sweep_list.append(tj)
tk = self.__create_virtual_task(tk, tj, vIdx, graph)