1 from pygraph.classes.digraph
import digraph
2 from pygraph.algorithms.cycles
import find_cycle
3 from pygraph.algorithms.accessibility
import connected_components
4 from pygraph.algorithms.critical
import critical_path
7 data = open(filename).read()
11 for algo
in workflow[
"algorithms"]:
12 name = algo[
"name"]+
"_algo"
14 for input
in algo[
"inputs"]:
15 if input ==
"": input =
"dummy"
16 if not gr.has_node(input):
18 if not gr.has_edge((input,name)):
19 gr.add_edge((input,name),wt=0)
20 for output
in algo[
"outputs"]:
21 if output ==
"":output =
"dummy"
22 if not gr.has_node(output):
23 gr.add_nodes([output])
24 if not gr.has_edge((name,output)):
26 gr.add_edge((name,output),wt=algo[
"runtimes_wall"][0]*100)
33 cycle = find_cycle(gr)
39 print "Removed loop by deleting edge (%s,%s)" %(cycle[-1],cycle[0])
40 gr.del_edge((cycle[-1],cycle[0]))
41 print "\nIN TOTAL %i CYCLES\n" %(n_cycles)
45 cc = connected_components(gr)
49 for k, v
in cc.iteritems():
50 cc_size[v] = cc_size[v] + 1
51 print "Connected components have the following size:"
54 print "NUMBER OF CONNECTED COMPONENTS: %i" %(len(cc_size.keys()))
60 cp = critical_path(gr)
62 for edge
in gr.edges():
63 total_time += gr.edge_weight(edge)
66 edges = [tuple(cp[i:i+2])
for i
in range(0, len(cp))]
68 critical_time += gr.edge_weight(edge)
70 print "Total time : %s" %total_time
71 print "Critical path: %s" %critical_time
72 print "POSSIBLE SPEEDUP: %s" %(total_time/critical_time)
78 for edge
in gr.edges():
79 if edge[0].endswith(
"_algo"):
80 algoname = edge[0].rstrip(
"_algo")
84 algoname = edge[1].rstrip(
"_algo")
88 if algoname
not in known_names:
89 algorithms[algoname] = {
"name": algoname,
93 "runtimes_wall" : [1000]
95 known_names.add(algoname)
97 algorithms[algoname][
"inputs"].append(product)
99 algorithms[algoname][
"outputs"].append(product)
100 algorithms[algoname][
"runtimes_wall"] = [gr.edge_weight(edge)/100,]
101 out = open(filename,
"w")
102 algorithm_list = [item
for item
in algorithms.values()]
103 workflow = {
"algorithms" : algorithm_list}
104 out.write(workflow.__repr__())
108 if __name__ ==
"__main__":
109 filename =
"Athena.json"
115 if had_to_fix_cycles:
def analyze_connected_componets(gr)
def print_graph_to_json(gr, filename)
def analyze_critical_path(gr)
def analyze_and_fix_cycles(gr)
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
def read_graph_from_json(filename)