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