The Gaudi Framework
master (bdbe7744)
Toggle main menu visibility
Loading...
Searching...
No Matches
graphanalysis.py
Go to the documentation of this file.
1
11
12
from
pygraph.algorithms.accessibility
import
connected_components
13
from
pygraph.algorithms.critical
import
critical_path
14
from
pygraph.algorithms.cycles
import
find_cycle
15
from
pygraph.classes.digraph
import
digraph
16
17
18
def
read_graph_from_json
(filename):
19
data = open(filename).read()
20
workflow = eval(data)
21
gr = digraph()
22
# create all nodes
23
for
algo
in
workflow[
"algorithms"
]:
24
# to differentiate algos from equally named products
25
name = algo[
"name"
] +
"_algo"
26
gr.add_nodes([name])
27
for
input
in
algo[
"inputs"
]:
28
if
input ==
""
:
29
input =
"dummy"
30
if
not
gr.has_node(input):
31
gr.add_nodes([input])
32
if
not
gr.has_edge((input, name)):
33
gr.add_edge((input, name), wt=0)
34
for
output
in
algo[
"outputs"
]:
35
if
output ==
""
:
36
output =
"dummy"
37
if
not
gr.has_node(output):
38
gr.add_nodes([output])
39
if
not
gr.has_edge((name, output)):
40
# the output edge gets the weigth corresponding to the runtime
41
gr.add_edge((name, output), wt=algo[
"runtimes_wall"
][0] * 100)
42
return
gr
43
44
45
def
analyze_and_fix_cycles
(gr):
46
has_loop =
True
47
n_cycles = 0
48
while
has_loop:
49
cycle = find_cycle(gr)
50
if
len(cycle) == 0:
51
has_loop =
False
52
continue
53
n_cycles += 1
54
print(cycle)
55
print(
"Removed loop by deleting edge (%s,%s)"
% (cycle[-1], cycle[0]))
56
gr.del_edge((cycle[-1], cycle[0]))
57
print(
"\nIN TOTAL %i CYCLES\n"
% (n_cycles))
58
return
n_cycles > 0
# whether it needed to fix cycles
59
60
61
def
analyze_connected_componets
(gr):
62
cc = connected_components(gr)
63
cc_size = {}
64
for
i
in
cc.values():
65
cc_size[i] = 0
66
for
k, v
in
cc.iteritems():
67
cc_size[v] = cc_size[v] + 1
68
print(
"Connected components have the following size:"
)
69
# for k,v in cc_size.iteritems():
70
# print "%i : %i" %(k,v)
71
print(
"NUMBER OF CONNECTED COMPONENTS: %i"
% (len(cc_size.keys())))
72
73
74
def
analyze_critical_path
(gr):
75
total_time = 0
76
critical_time = 0
77
cp = critical_path(gr)
78
# calculate total time
79
for
edge
in
gr.edges():
80
total_time += gr.edge_weight(edge)
81
82
# calculate time of critical path
83
edges = [tuple(cp[i : i + 2])
for
i
in
range(0, len(cp))]
84
for
edge
in
edges:
85
critical_time += gr.edge_weight(edge)
86
87
print(
"Total time : %s"
% total_time)
88
print(
"Critical path: %s"
% critical_time)
89
print(
"POSSIBLE SPEEDUP: %s"
% (total_time / critical_time))
90
91
92
def
print_graph_to_json
(gr, filename):
93
algorithms = {}
# still make it known to workflow
94
known_names = set()
95
for
edge
in
gr.edges():
96
if
edge[0].endswith(
"_algo"
):
97
algoname = edge[0].rstrip(
"_algo"
)
98
product = edge[1]
99
reading =
False
100
else
:
101
algoname = edge[1].rstrip(
"_algo"
)
102
product = edge[0]
103
reading =
True
104
105
if
algoname
not
in
known_names:
106
algorithms[algoname] = {
107
"name"
: algoname,
108
"inputs"
: [],
109
"outputs"
: [],
110
"runtimes"
: [1000],
# TODO dummy
111
"runtimes_wall"
: [1000],
# TODO dummy
112
}
113
known_names.add(algoname)
114
if
reading:
115
algorithms[algoname][
"inputs"
].append(product)
116
else
:
117
algorithms[algoname][
"outputs"
].append(product)
118
algorithms[algoname][
"runtimes_wall"
] = [
119
gr.edge_weight(edge) / 100,
120
]
121
out = open(filename,
"w"
)
122
algorithm_list = [item
for
item
in
algorithms.values()]
123
workflow = {
"algorithms"
: algorithm_list}
124
out.write(workflow.__repr__())
125
out.close()
126
127
128
129
if
__name__ ==
"__main__"
:
130
filename =
"Athena.json"
131
gr =
read_graph_from_json
(filename)
132
133
# let's analysis and fix for loops:
134
had_to_fix_cycles =
analyze_and_fix_cycles
(gr)
135
# write file with fixed graph
136
if
had_to_fix_cycles:
137
print_graph_to_json
(gr, filename.replace(
".json"
,
"_loopfixed.json"
))
138
139
# see how many disconnected components are there
140
analyze_connected_componets
(gr)
141
142
# let's check the minimum time required
143
# and print achievable speedup
144
analyze_critical_path
(gr)
graphanalysis.print_graph_to_json
print_graph_to_json(gr, filename)
Definition
graphanalysis.py:92
graphanalysis.analyze_critical_path
analyze_critical_path(gr)
Definition
graphanalysis.py:74
graphanalysis.analyze_and_fix_cycles
analyze_and_fix_cycles(gr)
Definition
graphanalysis.py:45
graphanalysis.read_graph_from_json
read_graph_from_json(filename)
Definition
graphanalysis.py:18
graphanalysis.analyze_connected_componets
analyze_connected_componets(gr)
Definition
graphanalysis.py:61
GaudiHive
options
graphanalysis.py
Generated on
for The Gaudi Framework by
1.17.0