VMS/2__runs_and_data

changeset 22:13a3ee4c9608

ucc visualization
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Wed, 11 Apr 2012 15:18:24 +0200
parents 3092eb030708
children 8336ea9c3153
files scripts/ucc_and_loop_graph_treatment/column_view.py scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py scripts/ucc_and_loop_graph_treatment/parse_ucc.py
diffstat 3 files changed, 472 insertions(+), 192 deletions(-) [+]
line diff
     1.1 --- a/scripts/ucc_and_loop_graph_treatment/column_view.py	Thu Mar 29 18:40:59 2012 +0200
     1.2 +++ b/scripts/ucc_and_loop_graph_treatment/column_view.py	Wed Apr 11 15:18:24 2012 +0200
     1.3 @@ -8,7 +8,100 @@
     1.4  
     1.5  __num_idle = 0 #graph.node['start']['numcores'] * 2
     1.6  
     1.7 -def fig_from_node(graph,node,num_cols,offset,positioning=None):
     1.8 +### saving ###
     1.9 +
    1.10 +def save_computed_column_view(graph,num_cores,total_height,positioning,dependencies=None):
    1.11 +	vertical_scale_factor = 10000.0/float(total_height)
    1.12 +	__num_cols = 1
    1.13 +	f = sf.Fig(fig_nodes(graph,vertical_scale_factor,1,0,positioning))
    1.14 +	if dependencies != None:
    1.15 +		f.d.append(draw_cross_core_deps(graph,num_cores,1,0,positioning,dependencies,vertical_scale_factor))
    1.16 +	#f.trans = "x,{0}*y".format(10000.0/float(total_height))
    1.17 +	w = __num_cols*__column_width*num_cores
    1.18 +	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w))
    1.19 +	c.save()
    1.20 +
    1.21 +def save_comparative_column_view(graph,num_cores,total_height,positioning=None):
    1.22 +	if positioning == None:
    1.23 +		num_cols = 2
    1.24 +	else:
    1.25 +		num_cols = 3
    1.26 +	vertical_scale_factor = 10000.0/float(total_height)
    1.27 +	f = sf.Fig()
    1.28 +	f.d.append(get_tsc_scale(graph,num_cores,num_cols,0))
    1.29 +	f.d.append(fig_nodes(graph,num_cols,1,positioning=positioning))
    1.30 +	if positioning!=None:
    1.31 +		f.d.append(fig_nodes(graph,num_cols,2))
    1.32 +	w = __num_cols*__column_width*num_cores
    1.33 +	f.trans = "x,{0}*y".format(10000.0/float(total_height))
    1.34 +	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w))
    1.35 +	c.save()
    1.36 +
    1.37 +def save_tsc_scale(graph,num_cores,total_height):
    1.38 +	nodes = get_tsc_scale(graph,num_cores,1,0,total_height)
    1.39 +	w = __column_width*num_cores
    1.40 +	c = sf.canvas(nodes.SVG(),width="{0}px".format(w),height="{0}px".format(10000),viewBox='0 0 {0} {1}'.format(w,10000))
    1.41 +	c.save('tsc.svg')
    1.42 +
    1.43 +
    1.44 +
    1.45 +### drawing ###
    1.46 +
    1.47 +### timestamp counter ###
    1.48 +
    1.49 +def get_tsc_scale(graph,num_cores,num_cols,offset):
    1.50 +	nodes=sf.Fig()
    1.51 +	maxh = 0
    1.52 +	for node in graph:
    1.53 +		if node != 'start' and node != 'end':
    1.54 +			try:
    1.55 +				n , maxh = tsc_fig_from_node(graph,node,num_cols,offset,maxh)
    1.56 +				nodes.d.append(n)
    1.57 +			except Exception as e:
    1.58 +				print node,e
    1.59 +	#nodes.trans = "x,{0}*y".format(10000.0/float(total_height))
    1.60 +	return nodes
    1.61 +
    1.62 +def tsc_fig_from_node(graph,node,num_cols,offset,maxh):
    1.63 +	tscstarttime = graph.node['start']['tscstarttime']
    1.64 +	core = graph.node[node]['core']
    1.65 +	x1 = num_cols * core * __column_width + offset * __column_width
    1.66 +	x2 = x1 + __node_width
    1.67 +	y1 = graph.node[node]['Timestamp_start'] - tscstarttime
    1.68 +	if graph.node[node].has_key('Timestamp_end'):
    1.69 +		y2 = graph.node[node]['Timestamp_end'] - tscstarttime
    1.70 +	else:
    1.71 +		y2 = y1 + graph.node[node]['weight']
    1.72 +	if node[0]>=2*graph.node['start']['numcores']:
    1.73 +		f='yellow'
    1.74 +	else:
    1.75 +		f='gray'
    1.76 +	r = sf.Rect(x1,y1,x2,y2,fill=f)
    1.77 +	s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle",font_size=20)
    1.78 +	try:
    1.79 +		if maxh < y2:
    1.80 +			maxh = y2
    1.81 +	except:
    1.82 +		pass
    1.83 +	return sf.Fig(r,s),maxh
    1.84 +
    1.85 +
    1.86 +
    1.87 +### calculated or annotated positioning ###
    1.88 +
    1.89 +
    1.90 +def fig_nodes(graph,vertical_scale_factor,num_cols,offset,positioning=None):
    1.91 +	"""Build all nodes."""
    1.92 +	nodes = []
    1.93 +	for node in graph:
    1.94 +		if node != 'start' and node != 'end':
    1.95 +			try:
    1.96 +				nodes.append(fig_from_node(graph,node,num_cols,offset,vertical_scale_factor,positioning))
    1.97 +			except Exception as e:
    1.98 +				print node,e
    1.99 +	return sf.Fig(*nodes)#,trans="x,{0}*y".format(vertical_scale_factor))
   1.100 +
   1.101 +def fig_from_node(graph,node,num_cols,offset,vertical_scale_factor,positioning=None):
   1.102  	"""Build globally positioned node representation."""
   1.103  	core = graph.node[node]['core']
   1.104  	x1 = core * num_cols * __column_width + offset * __column_width
   1.105 @@ -35,113 +128,44 @@
   1.106  			f='red'
   1.107  	else:
   1.108  		f='gray'
   1.109 -	r = sf.Rect(x1,y1,x2,y2,fill=f,fill_opacity="40%")
   1.110 -	r2 = sf.Rect(x1,y1+graph.node[node]['work_offset'],x2,y1+graph.node[node]['work_offset']+graph.node[node]['Work_end']-graph.node[node]['Work_start'],fill=f, stroke_opacity=0)
   1.111 -	s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle",font_size=20)
   1.112 +	r = sf.Rect(x1,y1* vertical_scale_factor,x2,y2* vertical_scale_factor,fill=f,fill_opacity="40%")
   1.113 +	r2 = sf.Rect(x1,(y1+graph.node[node]['work_offset'])* vertical_scale_factor,x2,(y1+graph.node[node]['work_offset']+graph.node[node]['Work_end']-graph.node[node]['Work_start'])* vertical_scale_factor,fill=f, stroke_opacity=0)
   1.114 +	s = sf.Text((x1+x2)/2 , (y1+y2)* vertical_scale_factor/2 ,str(node),text_anchor="middle",font_size=20)
   1.115  	if graph.node[node].has_key('AssignerInvocation_start') and graph.node[node].has_key('Assigner_start'):
   1.116  		h = graph.node[node]['Assigner_start'] - graph.node[node]['AssignerInvocation_start']
   1.117 -		r1 = sf.Rect(x1,y1,x2,y1+h,fill='green',fill_opacity="70%")
   1.118 +		r1 = sf.Rect(x1,y1* vertical_scale_factor,x2,(y1+h)* vertical_scale_factor,fill='green',fill_opacity="70%")
   1.119  		return sf.Fig(r,r1,r2,s)
   1.120  	return sf.Fig(r,r2,s)
   1.121  
   1.122 -def tsc_fig_from_node(graph,node,num_cols,offset,maxh):
   1.123 -	tscstarttime = graph.node['start']['tscstarttime']
   1.124 -	core = graph.node[node]['core']
   1.125 -	x1 = num_cols * core * __column_width + offset * __column_width
   1.126 -	x2 = x1 + __node_width
   1.127 -	y1 = graph.node[node]['Timestamp_start'] - tscstarttime
   1.128 -	if graph.node[node].has_key('Timestamp_end'):
   1.129 -		y2 = graph.node[node]['Timestamp_end'] - tscstarttime
   1.130 -	else:
   1.131 -		y2 = y1 + graph.node[node]['weight']
   1.132 -	if node[0]>=2*graph.node['start']['numcores']:
   1.133 -		f='yellow'
   1.134 -	else:
   1.135 -		f='gray'
   1.136 -	r = sf.Rect(x1,y1,x2,y2,fill=f)
   1.137 -	s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle")
   1.138 -	try:
   1.139 -		if maxh < y2:
   1.140 -			maxh = y2
   1.141 -	except:
   1.142 -		pass
   1.143 -	return sf.Fig(r,s),maxh
   1.144  
   1.145 -def fig_nodes(graph,vertical_scale_factor,num_cols,offset,positioning=None):
   1.146 -	"""Build all nodes."""
   1.147 -	nodes = []
   1.148 -	for node in graph:
   1.149 -		if node != 'start' and node != 'end':
   1.150 -			try:
   1.151 -				nodes.append(fig_from_node(graph,node,num_cols,offset,positioning=positioning))
   1.152 -			except Exception as e:
   1.153 -				print node,e
   1.154 -	return sf.Fig(*nodes)#,trans="x,{0}*y".format(vertical_scale_factor))
   1.155  
   1.156 -def get_tsc_scale(graph,num_cores,num_cols,offset):
   1.157 -	nodes=sf.Fig()
   1.158 -	maxh = 0
   1.159 -	for node in graph:
   1.160 -		if node != 'start' and node != 'end':
   1.161 -			try:
   1.162 -				n , maxh = tsc_fig_from_node(graph,node,num_cols,offset,maxh)
   1.163 -				nodes.d.append(n)
   1.164 -			except Exception as e:
   1.165 -				print node,e
   1.166 -	#nodes.trans = "x,{0}*y".format(10000.0/float(total_height))
   1.167 -	return nodes
   1.168 +### dependencies ###
   1.169  
   1.170 -def draw_dep(graph,num_cores,num_cols,offset,positioning,dep,color=(0,0,0)):
   1.171 -	a,b = dep
   1.172 -	core1 = graph.node[a]['core']
   1.173 -	x1 = core1 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width
   1.174 -	y1 = positioning[a]
   1.175 -	core2 = graph.node[b]['core']
   1.176 -	x2 = core2 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width
   1.177 -	y2 = positioning[b] - graph.node[b]['weight']
   1.178 -	return sf.Line(x1,y1,x2,y2,style="stroke:rgb({},{},{});stroke-width:3".format(int(color[0]*255),int(color[1]*255),int(color[2]*255)))
   1.179 -	
   1.180 -
   1.181 -def draw_cross_core_deps(graph,num_cores,num_cols,offset,positioning,dependencies):
   1.182 +def draw_cross_core_deps(graph,num_cores,num_cols,offset,positioning,dependencies,vertical_scale_factor):
   1.183  	f = sf.Fig()
   1.184  	hue = 0
   1.185  	hue_incr = 1.0/len(dependencies)
   1.186  	for depl in dependencies:
   1.187  		for a,b in depl:
   1.188  			if graph.node[a]['core'] != graph.node[b]['core']:
   1.189 -				f.d.append(draw_dep(graph,num_cores,num_cols,offset,positioning,(a,b),colorsys.hsv_to_rgb(hue,1,1)))
   1.190 +				f.d.append(draw_dep(graph,num_cores,num_cols,offset,positioning,(a,b),vertical_scale_factor,colorsys.hsv_to_rgb(hue,1,1)))
   1.191  		hue = hue + hue_incr
   1.192  	return f
   1.193  
   1.194 -def save_comparative_column_view(graph,num_cores,total_height,positioning=None):
   1.195 -	if positioning == None:
   1.196 -		num_cols = 2
   1.197 -	else:
   1.198 -		num_cols = 3
   1.199 -	vertical_scale_factor = 10000.0/float(total_height)
   1.200 -	f = sf.Fig()
   1.201 -	f.d.append(get_tsc_scale(graph,num_cores,num_cols,0))
   1.202 -	f.d.append(fig_nodes(graph,num_cols,1,positioning=positioning))
   1.203 -	if positioning!=None:
   1.204 -		f.d.append(fig_nodes(graph,num_cols,2))
   1.205 -	w = __num_cols*__column_width*num_cores
   1.206 -	f.trans = "x,{0}*y".format(10000.0/float(total_height))
   1.207 -	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w))
   1.208 -	c.save()
   1.209 +def draw_dep(graph,num_cores,num_cols,offset,positioning,dep,vertical_scale_factor,color=(0,0,0)):
   1.210 +	a,b = dep
   1.211 +	core1 = graph.node[a]['core']
   1.212 +	x1 = core1 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width
   1.213 +	y1 = positioning[a] * vertical_scale_factor
   1.214 +	core2 = graph.node[b]['core']
   1.215 +	x2 = core2 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width
   1.216 +	y2 = (positioning[b] - graph.node[b]['weight']) * vertical_scale_factor
   1.217 +	return sf.Line(x1,y1,x2,y2,style="stroke:rgb({0},{1},{2});stroke-width:3".format(int(color[0]*255),int(color[1]*255),int(color[2]*255)))
   1.218 +	
   1.219  
   1.220 -def save_computed_column_view(graph,num_cores,total_height,positioning,dependencies=None):
   1.221 -	vertical_scale_factor = 10000.0/float(total_height)
   1.222 -	__num_cols = 1
   1.223 -	f = sf.Fig(fig_nodes(graph,vertical_scale_factor,1,0,positioning))
   1.224 -	if dependencies != None:
   1.225 -		f.d.append(draw_cross_core_deps(graph,num_cores,1,0,positioning,dependencies))
   1.226 -	f.trans = "x,{0}*y".format(10000.0/float(total_height))
   1.227 -	w = __num_cols*__column_width*num_cores
   1.228 -	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w))
   1.229 -	c.save()
   1.230  
   1.231 -def save_tsc_scale(graph,num_cores,total_height):
   1.232 -	nodes = get_tsc_scale(graph,num_cores,1,0,total_height)
   1.233 -	w = __column_width*num_cores
   1.234 -	c = sf.canvas(nodes.SVG(),width="{0}px".format(w),height="{0}px".format(10000),viewBox='0 0 {0} {1}'.format(w,10000))
   1.235 -	c.save('tsc.svg')
   1.236 +
   1.237 +
   1.238 +
   1.239 +
   1.240 +
     2.1 --- a/scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py	Thu Mar 29 18:40:59 2012 +0200
     2.2 +++ b/scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py	Wed Apr 11 15:18:24 2012 +0200
     2.3 @@ -6,6 +6,84 @@
     2.4  import matplotlib.pyplot as plt
     2.5  import column_view
     2.6  
     2.7 +def main():
     2.8 +	if len(sys.argv)<3:
     2.9 +		if len(sys.argv)==2:
    2.10 +			uccfile = open("LoopGraph.{0}".format(sys.argv[1]))
    2.11 +			counterfile = open("Counters.{0}.csv".format(sys.argv[1]))
    2.12 +		else:
    2.13 +			print "Usage: {} SCGfile Counterfile".format(sys.argv[0])
    2.14 +			sys.exit()
    2.15 +	else:
    2.16 +		uccfile = open(sys.argv[1])
    2.17 +		counterfile = open(sys.argv[2])
    2.18 +	#print sys.getrecursionlimit()
    2.19 +	build_cg(uccfile,counterfile)
    2.20 +
    2.21 +
    2.22 +def build_cg(loopfile,counterfile):
    2.23 +	d = read_from_file(loopfile)
    2.24 +	print "Parsed file", loopfile.name, "and found:"
    2.25 +	if d.has_key("unit"):
    2.26 +		print len(d["unit"]), "Units"
    2.27 +	if d.has_key("ctlDep"):
    2.28 +		print len(d["ctlDep"]), "Control Dependencies"
    2.29 +	if d.has_key("commDep"):
    2.30 +		print len(d["commDep"]), "Communication Dependencies"
    2.31 +	if d.has_key("dynDep"):
    2.32 +		print len(d["dynDep"]), "Dynamically chosen Dependencies"
    2.33 +	if d.has_key("hwDep"):
    2.34 +		print len(d["hwDep"]), "Hardware constraints"
    2.35 +	g = loopgraph_from_dict(d)
    2.36 +	add_attributes_to_nodes_from_file(g,counterfile)
    2.37 +	#cont = raw_input("Calculate and show critical path (y/N)? ")
    2.38 +	#if not cont.startswith(('y','Y')):
    2.39 +	#	sys.exit()
    2.40 +	lend,predd = annotate_critical_path(g, 'start', 'end')
    2.41 +	print "Critical path length:",lend['end']
    2.42 +	critical_path = get_path(predd,'end')
    2.43 +	if critical_path == None:
    2.44 +		print "No path found!"
    2.45 +		nx.draw(g)
    2.46 +	else:
    2.47 +		#print "Critical path:",critical_path
    2.48 +		print "Expected execution time:",path_length(g,critical_path),"cycles"
    2.49 +		try:
    2.50 +			actual_time = g.node['start']['tscendtime'] - g.node['start']['tscstarttime']
    2.51 +			print "Actual execution time:", actual_time, "cycles"
    2.52 +			dif = actual_time - lend['end']
    2.53 +			print "(Difference:", dif, "- Relative Error:", 100*float(dif)/float(lend['end']),"%)"
    2.54 +			print g.node['start']['starttimes']
    2.55 +			print g.node['start']['tscstarttimes']
    2.56 +			column_view.save_computed_column_view(g,g.node['start']['numcores'],max(actual_time,lend['end']),lend,[d["commDep"], d["dynDep"], d["ctlDep"]])
    2.57 +			#column_view.save_tsc_scale(g,g.node['start']['numcores'],lend['end'])
    2.58 +			return #
    2.59 +			print "Node   \tCalculated\t    Actual\tCore\tDifference\tNon-cumulative part"
    2.60 +			drf = 0
    2.61 +			for n in critical_path:
    2.62 +				if g.node[n].has_key('AppResponder_end'):
    2.63 +					clen = g.node[n]['AppResponder_end']
    2.64 +				elif g.node[n].has_key('AppResponder_start'):
    2.65 +					clen = g.node[n]['AppResponder_start']
    2.66 +				else:
    2.67 +					print n
    2.68 +				if n!='start' and n!='end':
    2.69 +					print "{}\t{:10}\t{:10}\t({})\t{:10}\t{:10}".format(n,lend[n],clen,g.node[n]['core'],clen-lend[n],clen-lend[n]-drf)
    2.70 +					drf = clen-lend[n]
    2.71 +		except KeyError as e:
    2.72 +			print e
    2.73 +		return #
    2.74 +		colors = []
    2.75 +		for node in g.nodes():
    2.76 +			if node in critical_path:
    2.77 +				colors.append('r')
    2.78 +			else:
    2.79 +				colors.append('w')
    2.80 +		nx.draw(g, node_color=colors)
    2.81 +	plt.show()
    2.82 +
    2.83 +
    2.84 +
    2.85  def read_from_file(filename):
    2.86  	d = {}
    2.87  	reader = csv.reader(filename)
    2.88 @@ -114,6 +192,8 @@
    2.89  	g.node['start']['tscendtime']=max(tscendtimes.values())
    2.90  	g.node['start']['numcores']=highestcore+1
    2.91  
    2.92 +
    2.93 +
    2.94  def path_length(graph,path):
    2.95  	length = 0
    2.96  	if path==None:
    2.97 @@ -145,6 +225,8 @@
    2.98  					#print nl, ll
    2.99  	return longest
   2.100  
   2.101 +
   2.102 +
   2.103  def annotate_critical_path(graph, start, end, lend={}, predd={}):
   2.104  	path_to_here = 0
   2.105  	if len(graph.predecessors(end)) == 0:
   2.106 @@ -157,7 +239,7 @@
   2.107  				path_to_here = lend[node]
   2.108  				predd[end] = node 
   2.109  		except KeyError:
   2.110 -			print node, nlend
   2.111 +			print "Error in annotate_critical_path on node ", node
   2.112  			exit(0)
   2.113  	try:
   2.114  		lend[end] = path_to_here + graph.node[end]['weight']
   2.115 @@ -166,6 +248,10 @@
   2.116  		exit(0)
   2.117  	return lend,predd
   2.118  
   2.119 +
   2.120 +
   2.121 +
   2.122 +	
   2.123  def get_path(predd,end):
   2.124  	path = []
   2.125  	node = end
   2.126 @@ -175,81 +261,6 @@
   2.127  	path = [node] + path
   2.128  	return path
   2.129  
   2.130 -def build_cg(loopfile,counterfile):
   2.131 -	d = read_from_file(loopfile)
   2.132 -	print "Parsed file", loopfile.name, "and found:"
   2.133 -	if d.has_key("unit"):
   2.134 -		print len(d["unit"]), "Units"
   2.135 -	if d.has_key("ctlDep"):
   2.136 -		print len(d["ctlDep"]), "Control Dependencies"
   2.137 -	if d.has_key("commDep"):
   2.138 -		print len(d["commDep"]), "Communication Dependencies"
   2.139 -	if d.has_key("dynDep"):
   2.140 -		print len(d["dynDep"]), "Dynamically chosen Dependencies"
   2.141 -	if d.has_key("hwDep"):
   2.142 -		print len(d["hwDep"]), "Hardware constraints"
   2.143 -	g = loopgraph_from_dict(d)
   2.144 -	add_attributes_to_nodes_from_file(g,counterfile)
   2.145 -	#cont = raw_input("Calculate and show critical path (y/N)? ")
   2.146 -	#if not cont.startswith(('y','Y')):
   2.147 -	#	sys.exit()
   2.148 -	lend,predd = annotate_critical_path(g, 'start', 'end')
   2.149 -	print "Critical path length:",lend['end']
   2.150 -	critical_path = get_path(predd,'end')
   2.151 -	if critical_path == None:
   2.152 -		print "No path found!"
   2.153 -		nx.draw(g)
   2.154 -	else:
   2.155 -		#print "Critical path:",critical_path
   2.156 -		print "Expected execution time:",path_length(g,critical_path),"cycles"
   2.157 -		try:
   2.158 -			actual_time = g.node['start']['tscendtime'] - g.node['start']['tscstarttime']
   2.159 -			print "Actual execution time:", actual_time, "cycles"
   2.160 -			dif = actual_time - lend['end']
   2.161 -			print "(Difference:", dif, "- Relative Error:", 100*float(dif)/float(lend['end']),"%)"
   2.162 -			print g.node['start']['starttimes']
   2.163 -			print g.node['start']['tscstarttimes']
   2.164 -			column_view.save_computed_column_view(g,g.node['start']['numcores'],max(actual_time,lend['end']),lend,[d["commDep"], d["dynDep"], d["ctlDep"]])
   2.165 -			#column_view.save_tsc_scale(g,g.node['start']['numcores'],lend['end'])
   2.166 -			return #
   2.167 -			print "Node   \tCalculated\t    Actual\tCore\tDifference\tNon-cumulative part"
   2.168 -			drf = 0
   2.169 -			for n in critical_path:
   2.170 -				if g.node[n].has_key('AppResponder_end'):
   2.171 -					clen = g.node[n]['AppResponder_end']
   2.172 -				elif g.node[n].has_key('AppResponder_start'):
   2.173 -					clen = g.node[n]['AppResponder_start']
   2.174 -				else:
   2.175 -					print n
   2.176 -				if n!='start' and n!='end':
   2.177 -					print "{}\t{:10}\t{:10}\t({})\t{:10}\t{:10}".format(n,lend[n],clen,g.node[n]['core'],clen-lend[n],clen-lend[n]-drf)
   2.178 -					drf = clen-lend[n]
   2.179 -		except KeyError as e:
   2.180 -			print e
   2.181 -		return #
   2.182 -		colors = []
   2.183 -		for node in g.nodes():
   2.184 -			if node in critical_path:
   2.185 -				colors.append('r')
   2.186 -			else:
   2.187 -				colors.append('w')
   2.188 -		nx.draw(g, node_color=colors)
   2.189 -	plt.show()
   2.190 -	
   2.191 -
   2.192 -def main():
   2.193 -	if len(sys.argv)<3:
   2.194 -		if len(sys.argv)==2:
   2.195 -			uccfile = open("LoopGraph.{0}".format(sys.argv[1]))
   2.196 -			counterfile = open("Counters.{0}.csv".format(sys.argv[1]))
   2.197 -		else:
   2.198 -			print "Usage: {} SCGfile Counterfile".format(sys.argv[0])
   2.199 -			sys.exit()
   2.200 -	else:
   2.201 -		uccfile = open(sys.argv[1])
   2.202 -		counterfile = open(sys.argv[2])
   2.203 -	print sys.getrecursionlimit()
   2.204 -	build_cg(uccfile,counterfile)
   2.205  
   2.206  
   2.207  if __name__ == "__main__":
     3.1 --- a/scripts/ucc_and_loop_graph_treatment/parse_ucc.py	Thu Mar 29 18:40:59 2012 +0200
     3.2 +++ b/scripts/ucc_and_loop_graph_treatment/parse_ucc.py	Wed Apr 11 15:18:24 2012 +0200
     3.3 @@ -4,6 +4,41 @@
     3.4  import csv
     3.5  import networkx as nx
     3.6  import matplotlib.pyplot as plt
     3.7 +import svgfig as sf
     3.8 +
     3.9 +aliases = {}
    3.10 +
    3.11 +def main():
    3.12 +	sys.setrecursionlimit(10000)
    3.13 +	if len(sys.argv)<1:
    3.14 +		sys.exit()
    3.15 +	uccfile = open(sys.argv[1])
    3.16 +	d = read_from_file(uccfile)
    3.17 +	g = uccgraph_from_dict(d)
    3.18 +	print "Parsed UCC from file", sys.argv[1], "and found:"
    3.19 +	if d.has_key("unit"):
    3.20 +		print len(d["unit"]), "Units"
    3.21 +	if d.has_key("ctlDep"):
    3.22 +		print len(d["ctlDep"]), "Control Dependencies"
    3.23 +	if d.has_key("commDep"):
    3.24 +		print len(d["commDep"]), "Communication Dependencies"
    3.25 +	if d.has_key("NtoN"):
    3.26 +		print len(d["NtoN"]), "N to N communicating groups"
    3.27 +	#print nx.simple_cycles(g)
    3.28 +	try:
    3.29 +		counterfile = open(sys.argv[2])
    3.30 +		lend, rankd = rank_nodes_with_weight(g,'start','end', counterfile)
    3.31 +		save_ucc_with_weight(g, lend, rankd)
    3.32 +	except Exception as e:
    3.33 +		#raise
    3.34 +		print "Failed to process unit length information, generating no-length graph."
    3.35 +		lend,rankd = rank_nodes(g,'start','end')
    3.36 +		print "Highest rank =", lend['end']
    3.37 +		save_ucc_as_svg(g,rankd)
    3.38 +	#nx.draw(g)
    3.39 +	#plt.show()
    3.40 +
    3.41 +########################## INPUT FNS ##########################
    3.42  
    3.43  def read_from_file(filename):
    3.44  	d = {}
    3.45 @@ -13,60 +48,270 @@
    3.46  			if row[0] == "unit":
    3.47  				if not d.has_key("unit"): # not key in d:
    3.48  					d["unit"] = []
    3.49 -				d["unit"].append( ( int(row[1]),int(row[2]) ) )
    3.50 +				unit = ( int(row[1]),int(row[2]) )
    3.51 +				#the following supposes that units appear in order, and that all units appear before any constraints.
    3.52 +				if not aliases.has_key(unit):
    3.53 +					d["unit"].append( unit )
    3.54 +					aliases[unit]=unit
    3.55 +				if int(row[4]) == 1:
    3.56 +					aliases[unit[0],unit[1]+1]=aliases[unit]
    3.57  			if row[0] == "ctlDep":
    3.58  				if not d.has_key("ctlDep"):
    3.59  					d["ctlDep"] = []
    3.60 -				d["ctlDep"].append( ( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ) )
    3.61 +				node1 = aliases[(int(row[1]),int(row[2]))]
    3.62 +				node2 = aliases[(int(row[3]),int(row[4]))]
    3.63 +				if node1 != node2:
    3.64 +					d["ctlDep"].append( ( node1 , node2 ) )
    3.65  			if row[0] == "commDep":
    3.66  				if not d.has_key("commDep"):
    3.67  					d["commDep"] = []
    3.68 -				d["commDep"].append(( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ))
    3.69 +				node1 = aliases[(int(row[1]),int(row[2]))]
    3.70 +				node2 = aliases[(int(row[3]),int(row[4]))]
    3.71 +				if node1 != node2:
    3.72 +					d["commDep"].append(( node1 , node2 ))
    3.73  			if row[0] == "NtoN":
    3.74  				if not d.has_key("NtoN"):
    3.75  					d["NtoN"] = []
    3.76  				n = int(row[1])
    3.77  				senders = zip(map(int,row[2:2+2*n:2]),map(int,row[3:2+2*n:2]))
    3.78  				receivers = zip(map(int,row[2*n+2:4*n+2:2]),map(int,row[2*n+3:4*n+2:2]))
    3.79 +				asenders = list(set([aliases[s] for s in senders]))
    3.80 +				areceivers = list(set([aliases[s] for s in receivers]))
    3.81  				#print "Senders:",senders, "=",len(senders)
    3.82  				#print "Receivers:",receivers, "=",len(receivers)
    3.83 -				d["NtoN"].append((senders,receivers))
    3.84 +				d["NtoN"].append((asenders,areceivers))
    3.85  		except Exception as e:
    3.86  			print e
    3.87  			continue
    3.88  	return d
    3.89  
    3.90  def uccgraph_from_dict(d):
    3.91 -	g = nx.Graph()
    3.92 +	g = nx.DiGraph()
    3.93 +	g.add_node("start")
    3.94 +	g.add_node("end")
    3.95  	g.add_nodes_from(d["unit"])
    3.96  	g.add_edges_from(d["ctlDep"])
    3.97  	g.add_edges_from(d["commDep"])
    3.98 -	for senders,receivers in d["NtoN"]:
    3.99 -		g.add_node("NtoN0")
   3.100 +	for i,(senders,receivers) in enumerate(d["NtoN"]):
   3.101 +		g.add_node("NtoN"+str(i))
   3.102  		for node in senders:
   3.103  			g.add_edge(node,"NtoN0")
   3.104  		for node in receivers:
   3.105  			g.add_edge("NtoN0",node)
   3.106 +	for node in g:
   3.107 +		if node != "start" and node != "end":
   3.108 +			if len(g.predecessors(node)) == 0:
   3.109 +				g.add_edge("start",node)
   3.110 +			if len(g.successors(node)) == 0:
   3.111 +				g.add_edge(node,"end")
   3.112  	return g
   3.113  
   3.114 -def main():
   3.115 -	if len(sys.argv)<1:
   3.116 -		sys.exit()
   3.117 -	uccfile = open(sys.argv[1])
   3.118 -	d = read_from_file(uccfile)
   3.119 -	print "Parsed UCC from file", sys.argv[1], "and found:"
   3.120 -	if d.has_key("unit"):
   3.121 -		print len(d["unit"]), "Units"
   3.122 -	if d.has_key("ctlDep"):
   3.123 -		print len(d["ctlDep"]), "Control Dependencies"
   3.124 -	if d.has_key("commDep"):
   3.125 -		print len(d["commDep"]), "Communication Dependencies"
   3.126 -	if d.has_key("NtoN"):
   3.127 -		print len(d["NtoN"]), "N to N communicating groups"
   3.128 -	g = uccgraph_from_dict(d)
   3.129 -	nx.draw_spectral(g)
   3.130 -	plt.show()
   3.131 +########################## NON-WEIGHTED UCC ##########################
   3.132  
   3.133 +############ ranking ############ 
   3.134 +
   3.135 +def rank_nodes(graph, start, end, lend={}, rankd={}):
   3.136 +	path_to_here = 0
   3.137 +	if len(graph.predecessors(end)) == 0:
   3.138 +		assert end == start
   3.139 +	for node in graph.predecessors(end):
   3.140 +		if not lend.has_key(node):
   3.141 +			lend,rankd = rank_nodes(graph, start, node, lend, rankd)
   3.142 +		try:
   3.143 +			if lend[node] > path_to_here:
   3.144 +				path_to_here = lend[node]
   3.145 +		except KeyError:
   3.146 +			print node
   3.147 +			exit(0)
   3.148 +	lend[end] = path_to_here + 1
   3.149 +	if rankd.has_key(lend[end]):
   3.150 +		rankd[lend[end]].append(end)
   3.151 +	else:
   3.152 +		rankd[lend[end]] = [end]
   3.153 +	return lend,rankd
   3.154 +
   3.155 +############ drawing ############
   3.156 +
   3.157 +__rank_height = 100
   3.158 +__index_width = 100
   3.159 +__node_radius = 40
   3.160 +	
   3.161 +
   3.162 +def save_ucc_as_svg(graph,rankd):
   3.163 +	node_positions = {}
   3.164 +	nodes = draw_nodes(rankd,node_positions)
   3.165 +	edges = draw_edges(graph,node_positions)
   3.166 +	f = sf.Fig(nodes,edges)
   3.167 +	w = (max([len(r) for r in rankd.values()]) + 1) * __index_width
   3.168 +	h = (max(rankd.keys()) + 1) * __rank_height
   3.169 +	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="{0}px".format(h),viewBox='0 0 {0} {1}'.format(w,h))
   3.170 +	c.save("UCC.svg")
   3.171 +
   3.172 +def draw_nodes(rankd,node_positions):
   3.173 +	f = sf.Fig()
   3.174 +	for rank in rankd.keys():
   3.175 +		for index, node in enumerate(rankd[rank]):
   3.176 +			f.d.append(draw_node(node,rank,index,node_positions))
   3.177 +	return f
   3.178 +
   3.179 +def draw_node(node,rank,index,node_positions):
   3.180 +	cx = (index + 1)*__index_width
   3.181 +	cy = (rank + 1)*__rank_height
   3.182 +	node_positions[node] = (cx,cy)
   3.183 +	r = __node_radius
   3.184 +	c = sf.Ellipse(cx,cy,0,r,r,stroke='black',fill='yellow')
   3.185 +	t = sf.Text(cx,cy,str(node),font_size=20)
   3.186 +	return sf.Fig(c,t)
   3.187 +
   3.188 +
   3.189 +def draw_edges(graph,node_positions):
   3.190 +	f = sf.Fig()
   3.191 +	for edge in nx.edges(graph):
   3.192 +		f.d.append(draw_edge(edge,node_positions))
   3.193 +	return f
   3.194 +
   3.195 +def draw_edge(edge,node_positions):
   3.196 +	node1,node2 = edge
   3.197 +	x1,y1 = node_positions[node1]
   3.198 +	x2,y2 = node_positions[node2]
   3.199 +	return sf.Line(x1,y1,x2,y2,style="stroke:rgb(0,0,0);stroke-width:3")
   3.200 +
   3.201 +
   3.202 +
   3.203 +
   3.204 +########################## WEIGHTED UCC ##########################
   3.205 +
   3.206 +
   3.207 +############ ranking ############ 
   3.208 +
   3.209 +def rank_nodes_with_weight(graph, start, end, filename):
   3.210 +	add_weights_to_nodes(graph, filename)
   3.211 +	return do_recursive_ranking(graph, start, end)
   3.212 +
   3.213 +def add_weights_to_nodes(g, filename):
   3.214 +	counterreader = (csv.reader)(filename,skipinitialspace=True)
   3.215 +	events = {}
   3.216 +	inversealiases = {}
   3.217 +	for row in counterreader:
   3.218 +		if row[0] == "event":
   3.219 +			n = (int(row[2]),int(row[3]))
   3.220 +			if not events.has_key(n):
   3.221 +				events[n]={}
   3.222 +			events[n][row[1]] = int(row[4])
   3.223 +			if not inversealiases.has_key(aliases[n]):
   3.224 +				inversealiases[aliases[n]]=set()
   3.225 +			inversealiases[aliases[n]].add(n)
   3.226 +	for n in g:
   3.227 +		try:
   3.228 +			weight = 0
   3.229 +			if inversealiases.has_key(n):
   3.230 +				for a in inversealiases[n]:				
   3.231 +					if events[n].has_key('Assigner_start') and events[n].has_key('AssignerInvocation_start'):
   3.232 +						weight += events[n]['Assigner_start'] - events[n]['AssignerInvocation_start']
   3.233 +					if events[n].has_key('Assigner_end') and events[n].has_key('Assigner_start'):
   3.234 +						weight += events[n]['Assigner_end'] - events[n]['Assigner_start']
   3.235 +					if events[n].has_key('Work_start') and events[n].has_key('Assigner_end'):
   3.236 +						weight += events[n]['Work_start'] - events[n]['Assigner_end']
   3.237 +					if events[n].has_key('Work_end') and events[n].has_key('Work_start'):
   3.238 +						weight += events[n]['Work_end'] - events[n]['Work_start']
   3.239 +					if events[n].has_key('AppResponderInvocation_start') and events[n].has_key('Work_end'):
   3.240 +						weight += events[n]['AppResponderInvocation_start'] - events[n]['Work_end']
   3.241 +					if events[n].has_key('AppResponder_start') and events[n].has_key('AppResponderInvocation_start'):
   3.242 +						weight += events[n]['AppResponder_start'] - events[n]['AppResponderInvocation_start']
   3.243 +					if events[n].has_key('AppResponder_end') and events[n].has_key('AppResponder_start'):
   3.244 +						weight += events[n]['AppResponder_end'] - events[n]['AppResponder_start'] 
   3.245 +					if events[n].has_key('NextAssigner_start') and events[n].has_key('AppResponder_end'):
   3.246 +						weight += events[n]['NextAssigner_start'] - events[n]['AppResponder_end'] 
   3.247 +			g.node[n]['weight'] = weight #+ weight/10
   3.248 +		except Exception as e:
   3.249 +			print n
   3.250 +			raise
   3.251 +
   3.252 +
   3.253 +def do_recursive_ranking(graph, start, end, lend={}, rankd={}):
   3.254 +	path_to_here = 0
   3.255 +	if len(graph.predecessors(end)) == 0:
   3.256 +		assert end == start
   3.257 +	for node in graph.predecessors(end):
   3.258 +		if not lend.has_key(node):
   3.259 +			lend, rankd = do_recursive_ranking(graph, start, node, lend, rankd)
   3.260 +		try:
   3.261 +			if lend[node] > path_to_here:
   3.262 +				path_to_here = lend[node]
   3.263 +		except KeyError:
   3.264 +			print "Error in annotate_critical_path on node ", node
   3.265 +			exit(0)
   3.266 +	try:
   3.267 +		lend[end] = path_to_here + graph.node[end]['weight']
   3.268 +		if rankd.has_key(path_to_here):
   3.269 +			rankd[path_to_here].append(end)
   3.270 +		else:
   3.271 +			rankd[path_to_here] = [end]
   3.272 +	except KeyError:
   3.273 +		print end, "has no attribute 'weight'"
   3.274 +		exit(0)
   3.275 +	return lend, rankd
   3.276 +
   3.277 +
   3.278 +############ drawing ############
   3.279 +
   3.280 +__column_width = 110
   3.281 +__node_width = 100
   3.282 +
   3.283 +def save_ucc_with_weight(graph, lend, rankd):
   3.284 +	node_positions = {}
   3.285 +	scale_factor = 10000.0/float(lend['end'])
   3.286 +	nodes, max_cols = draw_nodes_with_weight(graph, lend, rankd, node_positions, scale_factor)
   3.287 +	edges = draw_edges_with_weight(graph,node_positions)
   3.288 +	f = sf.Fig(nodes,edges)
   3.289 +	#f.trans = "x,{0}*y".format(10000.0/float(lend['end']))
   3.290 +	w = (max_cols +1) * __column_width
   3.291 +	h = 10000
   3.292 +	c = sf.canvas(f.SVG(),width="{0}px".format(w),height="{0}px".format(h),viewBox='0 0 {0} {1}'.format(w,h))
   3.293 +	c.save("UCC.svg")
   3.294 +
   3.295 +def draw_nodes_with_weight(graph, lend, rankd, node_positions, scale_factor=1.0):
   3.296 +	f = sf.Fig()
   3.297 +	columnoccupancy = {}
   3.298 +	for rank in sorted(rankd.keys()):
   3.299 +		for node in rankd[rank]:
   3.300 +			f.d.append(draw_node_with_weight(graph, node, lend, rankd, columnoccupancy, node_positions, scale_factor))
   3.301 +	return f, max(columnoccupancy.keys())
   3.302 +
   3.303 +def draw_node_with_weight(graph, node, lend, rankd, columnoccupancy, node_positions, scale_factor=1.0):
   3.304 +	node_end = lend[node]
   3.305 +	node_start = node_end - graph.node[node]['weight']
   3.306 +	column = 0
   3.307 +	while columnoccupancy.has_key(column) and columnoccupancy[column] > node_start:
   3.308 +		column = column + 1
   3.309 +	columnoccupancy[column] = node_end
   3.310 +	x1 = column * __column_width
   3.311 +	y1 = node_start*scale_factor
   3.312 +	x2 = x1 + __node_width
   3.313 +	y2 = node_end*scale_factor
   3.314 +	if y1 == y2:
   3.315 +		y2 = y1+1
   3.316 +	node_positions[node] = (x1,y1,x2,y2)
   3.317 +	r = sf.Rect(x1,y1,x2,y2,stroke='black',fill='yellow')
   3.318 +	t = sf.Text((x1+x2)/2.0 , (y1+y2)/2.0 ,str(node),text_anchor="middle",font_size=20)
   3.319 +	return sf.Fig(r,t)
   3.320 +
   3.321 +def draw_edges_with_weight(graph,node_positions):
   3.322 +	f = sf.Fig()
   3.323 +	for edge in nx.edges(graph):
   3.324 +		f.d.append(draw_edge_with_weight(edge,node_positions))
   3.325 +	return f
   3.326 +
   3.327 +def draw_edge_with_weight(edge,node_positions):
   3.328 +	node1,node2 = edge
   3.329 +	x1_1,y1_1,x2_1,y2_1 = node_positions[node1]
   3.330 +	x1_2,y1_2,x2_2,y2_2 = node_positions[node2]
   3.331 +	m1 = (x1_1 + x2_1) / 2
   3.332 +	m2 = (x1_2 + x2_2) / 2
   3.333 +	return sf.Line(m1,y2_1,m2,y1_2,style="stroke:rgb(0,0,0);stroke-width:3")
   3.334 +
   3.335 +
   3.336 +########################## 
   3.337  
   3.338  if __name__ == "__main__":
   3.339      main()