-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
86 lines (75 loc) · 3.69 KB
/
visualization.py
File metadata and controls
86 lines (75 loc) · 3.69 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
print "Visualization"
rowlen = 80
def visualize_item(row, start, end, makespan, task):
scale = (1.0 * rowlen) / makespan
scaledstart = int(start * scale)
scaledend = min(rowlen-1, int(end * scale))
row[scaledstart] = "|"
for i in range(scaledstart+1, scaledend):
local_idx = i - (scaledstart+1)
if local_idx < len(task):
row[i] = task[local_idx]
else:
row[i] = "."
#row[scaledstart + 1:min(scaledend, len(task))] = list(task)[:min(len(task), scaledend-scaledstart)]
row[scaledend] = "|"
def visualize_schedules(schedules, timeline, jobs, resources):
if len(schedules) != len(timeline):
print "visualize_schedules() error: schedule and timeline mismatch"
return
#for each resource build a list of tasks, each cell with start time and end time
exec_dict = {}
for i in range(len(schedules)):
schedule = schedules[i]
for (resource, tasks) in schedule.items():
if not exec_dict.has_key(resource):
exec_dict[resource] = []
start = timeline[i]
#TODO relax the following constraint to allow better packing:
for task in tasks:
duration = jobs[task]['time']
cores = jobs[task]['cores']
end = start + duration
exec_unit = {'start': start, 'end': end, 'cores': cores, 'task': task}
exec_dict[resource].append(exec_unit)
makespan = 0
# Calculate latest execution time for each resource
for (resource, dictlist) in exec_dict.items():
# sort in ascending start time order
maxtime = max(dictlist, key=lambda x: x['end'])['end'] #max end time for all the exec items in the list
makespan = max(maxtime, makespan)
# for each resource draw the execution line
for (resource, exec_list) in exec_dict.items():
# sort in ascending start time order
sorted(exec_list, key=lambda x: x['start'])
numcores = resources[resource]
cores_availability = [[0, x] for x in range(numcores)] #list of times at which each core is available
cores_vis = [[" "] * rowlen for x in range(numcores)]
cores_utilization = [0 for x in range(numcores)]
for exec_item_idx in range(0, len(exec_list)):
cores_required = exec_list[exec_item_idx]['cores']
for core_idx in range(cores_required):
cores_availability.sort()
next_avail = cores_availability[0][0]
if next_avail > exec_list[exec_item_idx]['start']:
print "Visualization error - temporal contention, resouce: " + resource + " time: " + str(exec_list[exec_item_idx])
else:
row = cores_vis[cores_availability[0][1]]
start = exec_list[exec_item_idx]['start']
end = exec_list[exec_item_idx]['end']
task = exec_list[exec_item_idx]['task']
visualize_item(row, start, end, makespan, task)
cores_utilization[cores_availability[0][1]] += end - start
cores_availability[0][0] = exec_list[exec_item_idx]['end']
print "\t\t" + "%-10s" % (resource + ":")
for i in range(numcores):
perc_utilization = (100.0*cores_utilization[i])/makespan
print "\t\t\t" + "core%-4s" % (str(i) + ":") + ''.join(cores_vis[i]) + \
" " + str(int(perc_utilization)) + "%"
xaxis = ""
axis_interval = 5
scale = (1.0 * rowlen) / makespan
for i in range(0, rowlen+axis_interval, axis_interval):
xaxis += "%-5s" % str(int(i/scale))
print "\t\t\t\t " + xaxis
print "Makespan: " + str(makespan)