-
Notifications
You must be signed in to change notification settings - Fork 6
/
rnddag.py
415 lines (322 loc) · 12 KB
/
rnddag.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Randomized Multi-DAG Task Generator
# Xiaotian Dai
# Real-Time Systems Group
# University of York, UK
# -------------------------------------------------------------------------------
import os
import json
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout, to_agraph
import pygraphviz as pgv
from random import seed, randint, random
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Class: DAGTaskset
class DAGTaskset:
def __init__(self):
self.rnd_seed = randint(1, 1000)
self.util = 0
self.task_number = 0
self.tasks = []
def gen(self, u, n):
# generate tasks
# generate utilizations
# generate periods
# generate execution times
pass
def add(self, tau):
# add a task to taskset
pass
def remove(self, tau):
# remove a task from taskset
pass
def dump(self):
# dump tasksets into a json file
pass
def load(self):
# load tasksets from a json file
pass
# Class: DAG (Directed Acyclic Graph Task)
class DAG:
def __init__(self, i=0, U=-1, T=-1, W=-1):
# parameters (or use default)
self.task_num = i
self.name = 'Tau_{:d}'.format(i)
self.U = U
self.T = T
self.W = W
self.L = -1 # needs to be computed later
# configs for gen_nfj()
self.depth = 5
self.p_fork = 0.3
self.p_join = 0.8
self.fork_n_min = 2
self.fork_n_max = 4
def __str__(self):
A = nx.nx_agraph.to_agraph(self.G)
return A.__str__()
def get_graph(self):
return self.G
def get_number_of_nodes(self):
return self.G.number_of_nodes()
def get_number_of_edges(self):
return self.G.number_of_edges()
def gen(self, algorithm):
if algorithm == "nfj":
self.gen_rnd_nfj()
elif algorithm == "rnd":
self.gen_rnd()
else:
self.gen_rnd_legacy()
def gen_rnd_legacy(self):
# data structures
nodes = [] # nodes in all layers (in form of shape decomposition)
nodes_parent = [] # nodes that can be parents
nodes_parent_childless = [] # nodes without child
nodes_orphan = [] # nodes without any parent
# initial a new graph
G = nx.DiGraph(Index=self.task_num, U=self.U, T=self.T, W=self.W)
# add the root node
n = 1
G.add_node(n, rank=0)
nodes.append([n])
nodes_parent.append(n)
n = n + 1
# random and remove the source and the sink node
layer_num_this = randint(self.layer_num_min - 2, self.layer_num_max - 2)
# generate layer by layer
for k in range(layer_num_this):
# randomised nodes in each layer
m = randint(1, self.parallelism)
nodes_t = []
for _ in range(m):
nodes_t.append(n)
nodes_orphan.append(n)
G.add_node(n, rank=k+1)
n = n + 1
nodes.append(nodes_t)
# initially assume all parents are childless
nodes_parent_childless[:] = nodes_parent_childless[:] + nodes_parent[:]
# iterates all nodes in the current layout
for i in nodes[k+1]:
for ii in nodes_parent:
# add connections
if random() < self.connect_prob:
G.add_edge(ii, i)
if i in nodes_orphan:
nodes_orphan.remove(i)
if ii in nodes_parent_childless:
nodes_parent_childless.remove(ii)
# add all childs as candidate parents for the next layer
nodes_parent[:] = nodes[k+1]
# connect all orphan to the root node
for i in nodes_orphan:
nodes_orphan.remove(i)
G.add_edge(1, i)
# if i in nodes_parent:
# nodes_parent.remove(i)
# Dealing with the final layer
# connect everything together to a final node
for i in nodes_parent:
G.add_edge(i, n)
for i in nodes_parent_childless:
G.add_edge(i, n)
# connect all orphan to the root node
for i in nodes_orphan:
nodes_orphan.remove(i)
G.add_edge(1, i)
# (optional) mutate a node to be conditional
# G.add_node('2', style='filled', fillcolor='red', shape='diamond')
#print(nodes)
#print(nodes_orphan)
# return the graph
self.G = G
def gen_rnd(self, parallelism=8, layer_num_min=5, layer_num_max=12, connect_prob=0.5):
# data structures
nodes = [] # nodes in all layers (in form of shape decomposition)
nodes_parent = [] # nodes that can be parents
nodes_parent_childless = [] # nodes without child
nodes_orphan = [] # nodes without any parent
# initial a new graph
G = nx.DiGraph(Index=self.task_num, U=self.U, T=self.T, W=self.W)
# add the root node
n = 1
G.add_node(n, rank=0)
nodes.append([n])
nodes_parent.append(n)
n = n + 1
# random and remove the source and the sink node
layer_num_this = randint(layer_num_min - 2, layer_num_max - 2)
# generate layer by layer
for k in range(layer_num_this):
# randomised nodes in each layer
m = randint(1, parallelism)
nodes_t = []
for _ in range(m):
nodes_t.append(n)
nodes_orphan.append(n)
G.add_node(n, rank=k+1)
n = n + 1
nodes.append(nodes_t)
# initially assume all parents are childless
nodes_parent_childless[:] = nodes_parent_childless[:] + nodes_parent[:]
# iterates all nodes in the current layout
for i in nodes[k+1]:
for ii in nodes_parent:
# add connections
if random() < connect_prob:
G.add_edge(ii, i)
if i in nodes_orphan.copy():
nodes_orphan.remove(i)
if ii in nodes_parent_childless.copy():
nodes_parent_childless.remove(ii)
# add all childs as candidate parents for the next layer
nodes_parent[:] = nodes[k+1]
# connect all orphan to the root node
for i in nodes_orphan.copy():
G.add_edge(1, i)
nodes_orphan.remove(i)
# if i in nodes_parent:
# nodes_parent.remove(i)
# Dealing with the final layer
# connect everything together to a final node
for i in nodes_parent:
G.add_edge(i, n)
for i in nodes_parent_childless:
G.add_edge(i, n)
# connect all orphan to the root node
for i in nodes_orphan.copy():
G.add_edge(1, i)
nodes_orphan.remove(i)
# (optional) mutate a node to be conditional
# G.add_node('2', style='filled', fillcolor='red', shape='diamond')
#print(nodes)
#print(nodes_orphan)
# return the graph
self.G = G
def gen_nfj(self):
""" Generate Nested Fork-Join DAG
"""
# data structures
nodes = [] # nodes in all layers (in form of shape decomposition)
nodes_parent = [] # nodes that can become parent
ancestor_dict = {} # dict stores traces of nodes' all ancestors
# initial a new graph
G = nx.DiGraph(Index=self.task_num, U=self.U, T=self.T, W=self.W)
n = 1
r = 0
G.add_node(n, rank=r)
nodes.append([n])
nodes_parent.append(n)
n = n + 1
r = r + 1
ancestor_dict[1] = []
layer_num = max(self.depth - 2, 0)
# I. fork phase
for i in range(layer_num):
nodes_parent_next = []
fork_happened = False
for node_p in nodes_parent:
if random() < self.p_fork or node_p == 1:
kk = randint(self.fork_n_min, self.fork_n_max)
for i in range(kk):
G.add_node(n, rank=r)
G.add_edge(node_p, n, label="n/a")
nodes_parent_next.append(n)
ancestor_dict[n] = ancestor_dict[node_p] + [node_p]
fork_happened = True
n = n + 1
else:
nodes_parent_next.append(node_p)
if fork_happened:
r = r + 1
nodes_parent = nodes_parent_next
# print(ancestor_dict)
# II. join phase
# table contains all ancestors and nodes list, with ancestor as the key
# it is a reverse of ancestor_dict
table = {}
for i in nodes_parent:
for j in ancestor_dict[i]:
ret = table.get(j, None)
if ret == None:
table[j] = [i]
else:
table[j] = table[j] + [i]
# print(table)
# start to join
join_list = []
for node_p in nodes_parent:
if random() < self.p_join:
join_list.append(node_p)
# print(join_list)
# connect edges if all ancestor constraints are satisfied.
joined_happened = False
for i in sorted(table.keys()):
v = table[i]
# print(v)
if set(v).issubset(set(join_list)):
G.add_node(n, rank=r)
nodes_parent.append(n)
for cc in v:
G.add_edge(cc, n, label="n/a")
# remove from join list & parent list
join_list.remove(cc)
nodes_parent.remove(cc)
joined_happened = True
n = n + 1
if joined_happened:
r = r + 1
# connect all terminal nodes to the sink
if len(nodes_parent) > 1:
G.add_node(n, rank=r)
for i in nodes_parent:
G.add_edge(i, n)
# return the generated graph
self.G = G
def config(self):
pass
def print_data(self):
#print(self.G.graph)
print(self.G.nodes.data())
print(self.G.edges.data())
def save(self, basefolder="./data/"):
# layout graph
A = nx.nx_agraph.to_agraph(self.G)
#print("G", self.G.graph)
#print(A)
A.layout(prog='dot')
# create basefolder (if not exists)
if not os.path.exists(basefolder):
os.makedirs(basefolder)
# save graph (png)
A.draw(basefolder + self.name + '.png', format="png")
# save graph (gpickle)
nx.write_gpickle(self.G, basefolder + self.name + '.gpickle')
# save graph (gml)
nx.write_gml(self.G, basefolder + self.name + '.gml')
def load(self, basefolder="./data/"):
pass
def plot(self, basefolder="./data"):
""" The current version of plot draws the generated png file to benefit
from AGraph (Graphviz) layout functions.
"""
img = mpimg.imread(basefolder + self.name + '.png')
ypixels, xpixels, bands = img.shape
dpi = 100.
xinch = xpixels / dpi
yinch = ypixels / dpi
# plot and save in the same size as the original
plt.figure(figsize=(xinch, yinch))
ax = plt.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[])
ax.imshow(img, interpolation='none')
# plt.show(block=False)
plt.show()
if __name__ == "__main__":
G = DAG()
G.gen("nfj")
G.save(basefolder="./")
G.plot(basefolder="./")