VMS/2__runs_and_data

changeset 17:2021f42cc672

scheduling consequence graph construction script
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 10 Feb 2012 16:15:55 +0100
parents eea8cf5846c7
children c946cba3fda0
files scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py scripts/ucc_and_loop_graph_treatment/parse_ucc.py
diffstat 2 files changed, 175 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py	Fri Feb 10 16:15:55 2012 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +import sys
     1.7 +import csv
     1.8 +import networkx as nx
     1.9 +import matplotlib.pyplot as plt
    1.10 +
    1.11 +def read_from_file(filename):
    1.12 +	d = {}
    1.13 +	reader = csv.reader(filename)
    1.14 +	for row in reader:
    1.15 +		try:
    1.16 +			if row[0] == "unit":
    1.17 +				if not d.has_key("unit"): # not key in d:
    1.18 +					d["unit"] = []
    1.19 +				d["unit"].append( ( int(row[1]),int(row[2]) ) )
    1.20 +			if row[0] == "ctlDep":
    1.21 +				if not d.has_key("ctlDep"):
    1.22 +					d["ctlDep"] = []
    1.23 +				d["ctlDep"].append( ( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ) )
    1.24 +			if row[0] == "commDep":
    1.25 +				if not d.has_key("commDep"):
    1.26 +					d["commDep"] = []
    1.27 +				d["commDep"].append(( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ))
    1.28 +			if row[0] == "dynDep":
    1.29 +				if not d.has_key("dynDep"):
    1.30 +					d["dynDep"] = []
    1.31 +				d["dynDep"].append(( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ))
    1.32 +			if row[0] == "hwDep":
    1.33 +				if not d.has_key("hwDep"):
    1.34 +					d["hwDep"] = []
    1.35 +				d["hwDep"].append(( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ))
    1.36 +		except Exception as e:
    1.37 +			print e
    1.38 +			continue
    1.39 +	return d
    1.40 +
    1.41 +def add_attributes_to_nodes_from_file(g,filename):
    1.42 +	counterreader = (csv.reader)(filename,skipinitialspace=True)
    1.43 +	for row in counterreader:
    1.44 +		if row[0] == "event":
    1.45 +			n = (int(row[2]),int(row[3]))
    1.46 +			g.node[n][row[1]] = int(row[4])
    1.47 +	g.node["start"]['weight'] = 0
    1.48 +	g.node["end"]['weight'] = 0
    1.49 +	for n in g:
    1.50 +		try:
    1.51 +			if n!="start" and n!="end":
    1.52 +				g.node[n]['weight'] = g.node[n]['CoreLoop_afterWork'] - g.node[n]['CoreLoop_beforeWork'] + g.node[n]['MasterLoop_afterReqHdlr'] - g.node[n]['MasterLoop_beforeReqHdlr'] + g.node[n]['MasterLoop_afterAssign'] - g.node[n]['MasterLoop_beforeAssign']
    1.53 +		except KeyError as e:
    1.54 +			print e,n
    1.55 +
    1.56 +
    1.57 +#	counterreader = (csv.reader)(filename)
    1.58 +#	for row in counterreader:
    1.59 +#		n = (int(row[0]),int(row[1]))
    1.60 +#		g.node[n]['suspend_point'] = row[2]
    1.61 +#		g.node[n]['schedule_check_cycles'] = int(row[4])-int(row[19])
    1.62 +#		g.node[n]['sync_cycles'] = int(row[25])-int(row[24])
    1.63 +#		g.node[n]['assign_cycles'] = int(row[10])-int(row[7])
    1.64 +#		g.node[n]['work_comm_cycles'] = int(row[16])-int(row[13])
    1.65 +#		g.node[n]['status_cycles'] = int(row[22])-int(row[16])
    1.66 +#		g.node[n]['weight'] = g.node[n]['schedule_check_cycles'] + g.node[n]['sync_cycles'] + g.node[n]['assign_cycles'] + g.node[n]['work_comm_cycles'] + g.node[n]['status_cycles']
    1.67 +
    1.68 +def loopgraph_from_dict(d):
    1.69 +	g = nx.DiGraph()
    1.70 +	g.add_node("start")
    1.71 +	g.add_node("end")
    1.72 +	if d.has_key("unit"):
    1.73 +		g.add_nodes_from(d["unit"])
    1.74 +	if d.has_key("ctlDep"):
    1.75 +		g.add_edges_from(d["ctlDep"])
    1.76 +	if d.has_key("commDep"):
    1.77 +		g.add_edges_from(d["commDep"])
    1.78 +	if d.has_key("dynDep"):
    1.79 +		g.add_edges_from(d["dynDep"])
    1.80 +	if d.has_key("hwDep"):
    1.81 +		g.add_edges_from(d["hwDep"])
    1.82 +	for node in g:
    1.83 +		if node != "start" and node != "end":
    1.84 +			if len(g.predecessors(node)) == 0:
    1.85 +				g.add_edge("start",node)
    1.86 +			if len(g.successors(node)) == 0:
    1.87 +				g.add_edge(node,"end")
    1.88 +	return g
    1.89 +
    1.90 +def path_length(graph,path):
    1.91 +	length = 0
    1.92 +	for node in path:
    1.93 +		try:
    1.94 +			length += graph.node[node]['weight']
    1.95 +		except KeyError as e:
    1.96 +			pass
    1.97 +	return length
    1.98 +	
    1.99 +	
   1.100 +def find_critical_path(graph, start, end, path=[]):
   1.101 +	path = path + [end]
   1.102 +	if start == end:
   1.103 +		return path
   1.104 +	if not graph.has_node(end):
   1.105 +		return None
   1.106 +	longest = None
   1.107 +	for node in graph.predecessors(end):
   1.108 +		if node not in path:
   1.109 +			newpath = find_critical_path(graph, start, node, path)
   1.110 +			if newpath:
   1.111 +				if not longest or path_length(graph,newpath) > path_length(graph,longest):
   1.112 +					longest = newpath
   1.113 +	return longest
   1.114 +
   1.115 +def build_cg(loopfile,counterfile):
   1.116 +	d = read_from_file(loopfile)
   1.117 +	print "Parsed file", loopfile.name, "and found:"
   1.118 +	if d.has_key("unit"):
   1.119 +		print len(d["unit"]), "Units"
   1.120 +	if d.has_key("ctlDep"):
   1.121 +		print len(d["ctlDep"]), "Control Dependencies"
   1.122 +	if d.has_key("commDep"):
   1.123 +		print len(d["commDep"]), "Communication Dependencies"
   1.124 +	if d.has_key("dynDep"):
   1.125 +		print len(d["dynDep"]), "Dynamically chosen Dependencies"
   1.126 +	if d.has_key("hwDep"):
   1.127 +		print len(d["hwDep"]), "Hardware constraints"
   1.128 +	g = loopgraph_from_dict(d)
   1.129 +	add_attributes_to_nodes_from_file(g,counterfile)
   1.130 +	critical_path = find_critical_path(g,"start","end")
   1.131 +	if critical_path == None:
   1.132 +		print "No path found!"
   1.133 +		nx.draw(g)
   1.134 +	else:
   1.135 +		print "Expected execution time:\t",path_length(g,critical_path),"cycles"
   1.136 +		try:
   1.137 +			print "Actual execution time:\t", g.node[(2,98)]['MasterLoop_beforeReqHdlr'] - g.node[(2,1)]['MasterLoop_beforeAssign'], "cycles"
   1.138 +		except KeyError:
   1.139 +			pass
   1.140 +		colors = []
   1.141 +		for node in g.nodes():
   1.142 +			if node in critical_path:
   1.143 +				colors.append('r')
   1.144 +			else:
   1.145 +				colors.append('w')
   1.146 +		nx.draw(g, node_color=colors)
   1.147 +	plt.show()
   1.148 +	
   1.149 +
   1.150 +def main():
   1.151 +	if len(sys.argv)<2:
   1.152 +		sys.exit()
   1.153 +	uccfile = open(sys.argv[1])
   1.154 +	counterfile = open(sys.argv[2])
   1.155 +	build_cg(uccfile,counterfile)
   1.156 +
   1.157 +
   1.158 +if __name__ == "__main__":
   1.159 +    main()
     2.1 --- a/scripts/ucc_and_loop_graph_treatment/parse_ucc.py	Mon Feb 06 16:58:25 2012 +0100
     2.2 +++ b/scripts/ucc_and_loop_graph_treatment/parse_ucc.py	Fri Feb 10 16:15:55 2012 +0100
     2.3 @@ -2,7 +2,8 @@
     2.4  
     2.5  import sys
     2.6  import csv
     2.7 -
     2.8 +import networkx as nx
     2.9 +import matplotlib.pyplot as plt
    2.10  
    2.11  def read_from_file(filename):
    2.12  	d = {}
    2.13 @@ -35,6 +36,19 @@
    2.14  			continue
    2.15  	return d
    2.16  
    2.17 +def uccgraph_from_dict(d):
    2.18 +	g = nx.Graph()
    2.19 +	g.add_nodes_from(d["unit"])
    2.20 +	g.add_edges_from(d["ctlDep"])
    2.21 +	g.add_edges_from(d["commDep"])
    2.22 +	for senders,receivers in d["NtoN"]:
    2.23 +		g.add_node("NtoN0")
    2.24 +		for node in senders:
    2.25 +			g.add_edge(node,"NtoN0")
    2.26 +		for node in receivers:
    2.27 +			g.add_edge("NtoN0",node)
    2.28 +	return g
    2.29 +
    2.30  def main():
    2.31  	if len(sys.argv)<1:
    2.32  		sys.exit()
    2.33 @@ -49,6 +63,10 @@
    2.34  		print len(d["commDep"]), "Communication Dependencies"
    2.35  	if d.has_key("NtoN"):
    2.36  		print len(d["NtoN"]), "N to N communicating groups"
    2.37 +	g = uccgraph_from_dict(d)
    2.38 +	nx.draw_spectral(g)
    2.39 +	plt.show()
    2.40 +
    2.41  
    2.42  if __name__ == "__main__":
    2.43      main()