-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
142 lines (116 loc) · 3.66 KB
/
utils.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
from enum import Enum
class TaskState(Enum):
running = 1
ready = 2
terminated = 3
class TaskType(Enum):
ROOT = 1
COMPUTATION = 2
TRANSFER = 3
END = 4
def parse_type(string_t):
if string_t == "ROOT":
return TaskType.ROOT
if string_t == "COMPUTATION":
return TaskType.COMPUTATION
if string_t == "TRANSFER":
return TaskType.TRANSFER
if string_t == "END":
return TaskType.END
return -1
def get_string(index):
if index == TaskType.ROOT:
return "ROOT"
if index == TaskType.COMPUTATION:
return "COMPUTATION"
if index == TaskType.TRANSFER:
return "TRANSFER"
if index == TaskType.END:
return "END"
return ""
class Task:
"""
@brief : non-preemptable task
@param task_id : unique task id
@param memory, flops : the cost of the node depending on its type
"""
def __init__(self, task_id, task_type, memory=0, flops=0):
self.task_id = task_id
self.task_type = task_type
self.time = self.get_time(task_type, memory, flops)
#print(str(self.time)+ " " + str(self.task_id) + " " + TaskType.get_string(self.task_type))
self.state = TaskState.ready
self.static_level = 0
if self.task_type == TaskType.END:
self.static_level = self.time
# Task start time and earliest start time
self.start = 0
self.est = 0
def get_time(self, task_type, memory, flops):
if task_type == TaskType.COMPUTATION:
return flops / (10000 * 1024 * 1024)
if task_type == TaskType.TRANSFER:
return memory / (3200 * 1024 * 1024);
return 0;
def __repr__(self):
return TaskType.get_string(self.task_type) + " id " + str(self.task_id)
def finish(self):
return self.start + self.time
def __str__(self):
return self.__repr__()
def set_finish(self, finish):
self.finish = finish
def listify(dag, visited=set()):
if not visited:
visited = set()
res = [dag.task]
visited.add(dag)
for kid in dag.kids:
if kid not in visited:
res.extend(listify(kid, visited))
visited.add(kid)
return res
class DAG:
"""
@brief : graph
@param kids : dag kids
@param kids_indices : unique ids of kids
"""
def __init__(self):
self.parent = []
self.task = None
self.kids = []
self.kids_indices = []
self.inserted_indices = []
self.static_level = 0
def __repr__(self):
return str(self.task)
def clear_time(self):
self.task.time = 0
for kid in self.kids:
kid.clear_time()
def insert(self, task, kids_indices, child):
# who is the parent of this task
if not (task.task_id in self.kids_indices):
for kid in self.kids:
child = kid.insert(task, kids_indices, child)
return child
if child == None:
child = DAG()
child.task = task
child.kids = []
child.kids_indices = kids_indices
#verify if child is already here
if task.task_id in self.inserted_indices:
return child
#print("parent " + str(self.task.task_id) + " added child " + str(task.task_id))
child.parent += [self]
self.kids += [child]
self.inserted_indices += [child.task.task_id]
return child
def print(self):
for kid in self.kids:
if kid.task:
kid.task.print()
kid.print()
print(len(self.kids))