# HG changeset patch # User Nina Engelhardt # Date 1334150304 -7200 # Node ID 13a3ee4c9608f620fe4bdd5adae6bd5b73f5dc11 # Parent 3092eb030708d695d43a7374167eaf0b016ac98c ucc visualization diff -r 3092eb030708 -r 13a3ee4c9608 scripts/ucc_and_loop_graph_treatment/column_view.py --- a/scripts/ucc_and_loop_graph_treatment/column_view.py Thu Mar 29 18:40:59 2012 +0200 +++ b/scripts/ucc_and_loop_graph_treatment/column_view.py Wed Apr 11 15:18:24 2012 +0200 @@ -8,7 +8,100 @@ __num_idle = 0 #graph.node['start']['numcores'] * 2 -def fig_from_node(graph,node,num_cols,offset,positioning=None): +### saving ### + +def save_computed_column_view(graph,num_cores,total_height,positioning,dependencies=None): + vertical_scale_factor = 10000.0/float(total_height) + __num_cols = 1 + f = sf.Fig(fig_nodes(graph,vertical_scale_factor,1,0,positioning)) + if dependencies != None: + f.d.append(draw_cross_core_deps(graph,num_cores,1,0,positioning,dependencies,vertical_scale_factor)) + #f.trans = "x,{0}*y".format(10000.0/float(total_height)) + w = __num_cols*__column_width*num_cores + c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w)) + c.save() + +def save_comparative_column_view(graph,num_cores,total_height,positioning=None): + if positioning == None: + num_cols = 2 + else: + num_cols = 3 + vertical_scale_factor = 10000.0/float(total_height) + f = sf.Fig() + f.d.append(get_tsc_scale(graph,num_cores,num_cols,0)) + f.d.append(fig_nodes(graph,num_cols,1,positioning=positioning)) + if positioning!=None: + f.d.append(fig_nodes(graph,num_cols,2)) + w = __num_cols*__column_width*num_cores + f.trans = "x,{0}*y".format(10000.0/float(total_height)) + c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w)) + c.save() + +def save_tsc_scale(graph,num_cores,total_height): + nodes = get_tsc_scale(graph,num_cores,1,0,total_height) + w = __column_width*num_cores + c = sf.canvas(nodes.SVG(),width="{0}px".format(w),height="{0}px".format(10000),viewBox='0 0 {0} {1}'.format(w,10000)) + c.save('tsc.svg') + + + +### drawing ### + +### timestamp counter ### + +def get_tsc_scale(graph,num_cores,num_cols,offset): + nodes=sf.Fig() + maxh = 0 + for node in graph: + if node != 'start' and node != 'end': + try: + n , maxh = tsc_fig_from_node(graph,node,num_cols,offset,maxh) + nodes.d.append(n) + except Exception as e: + print node,e + #nodes.trans = "x,{0}*y".format(10000.0/float(total_height)) + return nodes + +def tsc_fig_from_node(graph,node,num_cols,offset,maxh): + tscstarttime = graph.node['start']['tscstarttime'] + core = graph.node[node]['core'] + x1 = num_cols * core * __column_width + offset * __column_width + x2 = x1 + __node_width + y1 = graph.node[node]['Timestamp_start'] - tscstarttime + if graph.node[node].has_key('Timestamp_end'): + y2 = graph.node[node]['Timestamp_end'] - tscstarttime + else: + y2 = y1 + graph.node[node]['weight'] + if node[0]>=2*graph.node['start']['numcores']: + f='yellow' + else: + f='gray' + r = sf.Rect(x1,y1,x2,y2,fill=f) + s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle",font_size=20) + try: + if maxh < y2: + maxh = y2 + except: + pass + return sf.Fig(r,s),maxh + + + +### calculated or annotated positioning ### + + +def fig_nodes(graph,vertical_scale_factor,num_cols,offset,positioning=None): + """Build all nodes.""" + nodes = [] + for node in graph: + if node != 'start' and node != 'end': + try: + nodes.append(fig_from_node(graph,node,num_cols,offset,vertical_scale_factor,positioning)) + except Exception as e: + print node,e + return sf.Fig(*nodes)#,trans="x,{0}*y".format(vertical_scale_factor)) + +def fig_from_node(graph,node,num_cols,offset,vertical_scale_factor,positioning=None): """Build globally positioned node representation.""" core = graph.node[node]['core'] x1 = core * num_cols * __column_width + offset * __column_width @@ -35,113 +128,44 @@ f='red' else: f='gray' - r = sf.Rect(x1,y1,x2,y2,fill=f,fill_opacity="40%") - 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) - s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle",font_size=20) + r = sf.Rect(x1,y1* vertical_scale_factor,x2,y2* vertical_scale_factor,fill=f,fill_opacity="40%") + 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) + s = sf.Text((x1+x2)/2 , (y1+y2)* vertical_scale_factor/2 ,str(node),text_anchor="middle",font_size=20) if graph.node[node].has_key('AssignerInvocation_start') and graph.node[node].has_key('Assigner_start'): h = graph.node[node]['Assigner_start'] - graph.node[node]['AssignerInvocation_start'] - r1 = sf.Rect(x1,y1,x2,y1+h,fill='green',fill_opacity="70%") + r1 = sf.Rect(x1,y1* vertical_scale_factor,x2,(y1+h)* vertical_scale_factor,fill='green',fill_opacity="70%") return sf.Fig(r,r1,r2,s) return sf.Fig(r,r2,s) -def tsc_fig_from_node(graph,node,num_cols,offset,maxh): - tscstarttime = graph.node['start']['tscstarttime'] - core = graph.node[node]['core'] - x1 = num_cols * core * __column_width + offset * __column_width - x2 = x1 + __node_width - y1 = graph.node[node]['Timestamp_start'] - tscstarttime - if graph.node[node].has_key('Timestamp_end'): - y2 = graph.node[node]['Timestamp_end'] - tscstarttime - else: - y2 = y1 + graph.node[node]['weight'] - if node[0]>=2*graph.node['start']['numcores']: - f='yellow' - else: - f='gray' - r = sf.Rect(x1,y1,x2,y2,fill=f) - s = sf.Text((x1+x2)/2 , (y1+y2)/2 ,str(node),text_anchor="middle") - try: - if maxh < y2: - maxh = y2 - except: - pass - return sf.Fig(r,s),maxh -def fig_nodes(graph,vertical_scale_factor,num_cols,offset,positioning=None): - """Build all nodes.""" - nodes = [] - for node in graph: - if node != 'start' and node != 'end': - try: - nodes.append(fig_from_node(graph,node,num_cols,offset,positioning=positioning)) - except Exception as e: - print node,e - return sf.Fig(*nodes)#,trans="x,{0}*y".format(vertical_scale_factor)) -def get_tsc_scale(graph,num_cores,num_cols,offset): - nodes=sf.Fig() - maxh = 0 - for node in graph: - if node != 'start' and node != 'end': - try: - n , maxh = tsc_fig_from_node(graph,node,num_cols,offset,maxh) - nodes.d.append(n) - except Exception as e: - print node,e - #nodes.trans = "x,{0}*y".format(10000.0/float(total_height)) - return nodes +### dependencies ### -def draw_dep(graph,num_cores,num_cols,offset,positioning,dep,color=(0,0,0)): - a,b = dep - core1 = graph.node[a]['core'] - x1 = core1 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width - y1 = positioning[a] - core2 = graph.node[b]['core'] - x2 = core2 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width - y2 = positioning[b] - graph.node[b]['weight'] - 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))) - - -def draw_cross_core_deps(graph,num_cores,num_cols,offset,positioning,dependencies): +def draw_cross_core_deps(graph,num_cores,num_cols,offset,positioning,dependencies,vertical_scale_factor): f = sf.Fig() hue = 0 hue_incr = 1.0/len(dependencies) for depl in dependencies: for a,b in depl: if graph.node[a]['core'] != graph.node[b]['core']: - f.d.append(draw_dep(graph,num_cores,num_cols,offset,positioning,(a,b),colorsys.hsv_to_rgb(hue,1,1))) + f.d.append(draw_dep(graph,num_cores,num_cols,offset,positioning,(a,b),vertical_scale_factor,colorsys.hsv_to_rgb(hue,1,1))) hue = hue + hue_incr return f -def save_comparative_column_view(graph,num_cores,total_height,positioning=None): - if positioning == None: - num_cols = 2 - else: - num_cols = 3 - vertical_scale_factor = 10000.0/float(total_height) - f = sf.Fig() - f.d.append(get_tsc_scale(graph,num_cores,num_cols,0)) - f.d.append(fig_nodes(graph,num_cols,1,positioning=positioning)) - if positioning!=None: - f.d.append(fig_nodes(graph,num_cols,2)) - w = __num_cols*__column_width*num_cores - f.trans = "x,{0}*y".format(10000.0/float(total_height)) - c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w)) - c.save() +def draw_dep(graph,num_cores,num_cols,offset,positioning,dep,vertical_scale_factor,color=(0,0,0)): + a,b = dep + core1 = graph.node[a]['core'] + x1 = core1 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width + y1 = positioning[a] * vertical_scale_factor + core2 = graph.node[b]['core'] + x2 = core2 * num_cols * __column_width + offset * __column_width + 0.5 * __column_width + y2 = (positioning[b] - graph.node[b]['weight']) * vertical_scale_factor + 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))) + -def save_computed_column_view(graph,num_cores,total_height,positioning,dependencies=None): - vertical_scale_factor = 10000.0/float(total_height) - __num_cols = 1 - f = sf.Fig(fig_nodes(graph,vertical_scale_factor,1,0,positioning)) - if dependencies != None: - f.d.append(draw_cross_core_deps(graph,num_cores,1,0,positioning,dependencies)) - f.trans = "x,{0}*y".format(10000.0/float(total_height)) - w = __num_cols*__column_width*num_cores - c = sf.canvas(f.SVG(),width="{0}px".format(w),height="10000px",viewBox='0 0 {0} 10000'.format(w)) - c.save() -def save_tsc_scale(graph,num_cores,total_height): - nodes = get_tsc_scale(graph,num_cores,1,0,total_height) - w = __column_width*num_cores - c = sf.canvas(nodes.SVG(),width="{0}px".format(w),height="{0}px".format(10000),viewBox='0 0 {0} {1}'.format(w,10000)) - c.save('tsc.svg') + + + + + diff -r 3092eb030708 -r 13a3ee4c9608 scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py --- a/scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py Thu Mar 29 18:40:59 2012 +0200 +++ b/scripts/ucc_and_loop_graph_treatment/parse_loop_graph.py Wed Apr 11 15:18:24 2012 +0200 @@ -6,6 +6,84 @@ import matplotlib.pyplot as plt import column_view +def main(): + if len(sys.argv)<3: + if len(sys.argv)==2: + uccfile = open("LoopGraph.{0}".format(sys.argv[1])) + counterfile = open("Counters.{0}.csv".format(sys.argv[1])) + else: + print "Usage: {} SCGfile Counterfile".format(sys.argv[0]) + sys.exit() + else: + uccfile = open(sys.argv[1]) + counterfile = open(sys.argv[2]) + #print sys.getrecursionlimit() + build_cg(uccfile,counterfile) + + +def build_cg(loopfile,counterfile): + d = read_from_file(loopfile) + print "Parsed file", loopfile.name, "and found:" + if d.has_key("unit"): + print len(d["unit"]), "Units" + if d.has_key("ctlDep"): + print len(d["ctlDep"]), "Control Dependencies" + if d.has_key("commDep"): + print len(d["commDep"]), "Communication Dependencies" + if d.has_key("dynDep"): + print len(d["dynDep"]), "Dynamically chosen Dependencies" + if d.has_key("hwDep"): + print len(d["hwDep"]), "Hardware constraints" + g = loopgraph_from_dict(d) + add_attributes_to_nodes_from_file(g,counterfile) + #cont = raw_input("Calculate and show critical path (y/N)? ") + #if not cont.startswith(('y','Y')): + # sys.exit() + lend,predd = annotate_critical_path(g, 'start', 'end') + print "Critical path length:",lend['end'] + critical_path = get_path(predd,'end') + if critical_path == None: + print "No path found!" + nx.draw(g) + else: + #print "Critical path:",critical_path + print "Expected execution time:",path_length(g,critical_path),"cycles" + try: + actual_time = g.node['start']['tscendtime'] - g.node['start']['tscstarttime'] + print "Actual execution time:", actual_time, "cycles" + dif = actual_time - lend['end'] + print "(Difference:", dif, "- Relative Error:", 100*float(dif)/float(lend['end']),"%)" + print g.node['start']['starttimes'] + print g.node['start']['tscstarttimes'] + column_view.save_computed_column_view(g,g.node['start']['numcores'],max(actual_time,lend['end']),lend,[d["commDep"], d["dynDep"], d["ctlDep"]]) + #column_view.save_tsc_scale(g,g.node['start']['numcores'],lend['end']) + return # + print "Node \tCalculated\t Actual\tCore\tDifference\tNon-cumulative part" + drf = 0 + for n in critical_path: + if g.node[n].has_key('AppResponder_end'): + clen = g.node[n]['AppResponder_end'] + elif g.node[n].has_key('AppResponder_start'): + clen = g.node[n]['AppResponder_start'] + else: + print n + if n!='start' and n!='end': + 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) + drf = clen-lend[n] + except KeyError as e: + print e + return # + colors = [] + for node in g.nodes(): + if node in critical_path: + colors.append('r') + else: + colors.append('w') + nx.draw(g, node_color=colors) + plt.show() + + + def read_from_file(filename): d = {} reader = csv.reader(filename) @@ -114,6 +192,8 @@ g.node['start']['tscendtime']=max(tscendtimes.values()) g.node['start']['numcores']=highestcore+1 + + def path_length(graph,path): length = 0 if path==None: @@ -145,6 +225,8 @@ #print nl, ll return longest + + def annotate_critical_path(graph, start, end, lend={}, predd={}): path_to_here = 0 if len(graph.predecessors(end)) == 0: @@ -157,7 +239,7 @@ path_to_here = lend[node] predd[end] = node except KeyError: - print node, nlend + print "Error in annotate_critical_path on node ", node exit(0) try: lend[end] = path_to_here + graph.node[end]['weight'] @@ -166,6 +248,10 @@ exit(0) return lend,predd + + + + def get_path(predd,end): path = [] node = end @@ -175,81 +261,6 @@ path = [node] + path return path -def build_cg(loopfile,counterfile): - d = read_from_file(loopfile) - print "Parsed file", loopfile.name, "and found:" - if d.has_key("unit"): - print len(d["unit"]), "Units" - if d.has_key("ctlDep"): - print len(d["ctlDep"]), "Control Dependencies" - if d.has_key("commDep"): - print len(d["commDep"]), "Communication Dependencies" - if d.has_key("dynDep"): - print len(d["dynDep"]), "Dynamically chosen Dependencies" - if d.has_key("hwDep"): - print len(d["hwDep"]), "Hardware constraints" - g = loopgraph_from_dict(d) - add_attributes_to_nodes_from_file(g,counterfile) - #cont = raw_input("Calculate and show critical path (y/N)? ") - #if not cont.startswith(('y','Y')): - # sys.exit() - lend,predd = annotate_critical_path(g, 'start', 'end') - print "Critical path length:",lend['end'] - critical_path = get_path(predd,'end') - if critical_path == None: - print "No path found!" - nx.draw(g) - else: - #print "Critical path:",critical_path - print "Expected execution time:",path_length(g,critical_path),"cycles" - try: - actual_time = g.node['start']['tscendtime'] - g.node['start']['tscstarttime'] - print "Actual execution time:", actual_time, "cycles" - dif = actual_time - lend['end'] - print "(Difference:", dif, "- Relative Error:", 100*float(dif)/float(lend['end']),"%)" - print g.node['start']['starttimes'] - print g.node['start']['tscstarttimes'] - column_view.save_computed_column_view(g,g.node['start']['numcores'],max(actual_time,lend['end']),lend,[d["commDep"], d["dynDep"], d["ctlDep"]]) - #column_view.save_tsc_scale(g,g.node['start']['numcores'],lend['end']) - return # - print "Node \tCalculated\t Actual\tCore\tDifference\tNon-cumulative part" - drf = 0 - for n in critical_path: - if g.node[n].has_key('AppResponder_end'): - clen = g.node[n]['AppResponder_end'] - elif g.node[n].has_key('AppResponder_start'): - clen = g.node[n]['AppResponder_start'] - else: - print n - if n!='start' and n!='end': - 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) - drf = clen-lend[n] - except KeyError as e: - print e - return # - colors = [] - for node in g.nodes(): - if node in critical_path: - colors.append('r') - else: - colors.append('w') - nx.draw(g, node_color=colors) - plt.show() - - -def main(): - if len(sys.argv)<3: - if len(sys.argv)==2: - uccfile = open("LoopGraph.{0}".format(sys.argv[1])) - counterfile = open("Counters.{0}.csv".format(sys.argv[1])) - else: - print "Usage: {} SCGfile Counterfile".format(sys.argv[0]) - sys.exit() - else: - uccfile = open(sys.argv[1]) - counterfile = open(sys.argv[2]) - print sys.getrecursionlimit() - build_cg(uccfile,counterfile) if __name__ == "__main__": diff -r 3092eb030708 -r 13a3ee4c9608 scripts/ucc_and_loop_graph_treatment/parse_ucc.py --- a/scripts/ucc_and_loop_graph_treatment/parse_ucc.py Thu Mar 29 18:40:59 2012 +0200 +++ b/scripts/ucc_and_loop_graph_treatment/parse_ucc.py Wed Apr 11 15:18:24 2012 +0200 @@ -4,6 +4,41 @@ import csv import networkx as nx import matplotlib.pyplot as plt +import svgfig as sf + +aliases = {} + +def main(): + sys.setrecursionlimit(10000) + if len(sys.argv)<1: + sys.exit() + uccfile = open(sys.argv[1]) + d = read_from_file(uccfile) + g = uccgraph_from_dict(d) + print "Parsed UCC from file", sys.argv[1], "and found:" + if d.has_key("unit"): + print len(d["unit"]), "Units" + if d.has_key("ctlDep"): + print len(d["ctlDep"]), "Control Dependencies" + if d.has_key("commDep"): + print len(d["commDep"]), "Communication Dependencies" + if d.has_key("NtoN"): + print len(d["NtoN"]), "N to N communicating groups" + #print nx.simple_cycles(g) + try: + counterfile = open(sys.argv[2]) + lend, rankd = rank_nodes_with_weight(g,'start','end', counterfile) + save_ucc_with_weight(g, lend, rankd) + except Exception as e: + #raise + print "Failed to process unit length information, generating no-length graph." + lend,rankd = rank_nodes(g,'start','end') + print "Highest rank =", lend['end'] + save_ucc_as_svg(g,rankd) + #nx.draw(g) + #plt.show() + +########################## INPUT FNS ########################## def read_from_file(filename): d = {} @@ -13,60 +48,270 @@ if row[0] == "unit": if not d.has_key("unit"): # not key in d: d["unit"] = [] - d["unit"].append( ( int(row[1]),int(row[2]) ) ) + unit = ( int(row[1]),int(row[2]) ) + #the following supposes that units appear in order, and that all units appear before any constraints. + if not aliases.has_key(unit): + d["unit"].append( unit ) + aliases[unit]=unit + if int(row[4]) == 1: + aliases[unit[0],unit[1]+1]=aliases[unit] if row[0] == "ctlDep": if not d.has_key("ctlDep"): d["ctlDep"] = [] - d["ctlDep"].append( ( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) ) ) + node1 = aliases[(int(row[1]),int(row[2]))] + node2 = aliases[(int(row[3]),int(row[4]))] + if node1 != node2: + d["ctlDep"].append( ( node1 , node2 ) ) if row[0] == "commDep": if not d.has_key("commDep"): d["commDep"] = [] - d["commDep"].append(( (int(row[1]),int(row[2])) , (int(row[3]),int(row[4])) )) + node1 = aliases[(int(row[1]),int(row[2]))] + node2 = aliases[(int(row[3]),int(row[4]))] + if node1 != node2: + d["commDep"].append(( node1 , node2 )) if row[0] == "NtoN": if not d.has_key("NtoN"): d["NtoN"] = [] n = int(row[1]) senders = zip(map(int,row[2:2+2*n:2]),map(int,row[3:2+2*n:2])) receivers = zip(map(int,row[2*n+2:4*n+2:2]),map(int,row[2*n+3:4*n+2:2])) + asenders = list(set([aliases[s] for s in senders])) + areceivers = list(set([aliases[s] for s in receivers])) #print "Senders:",senders, "=",len(senders) #print "Receivers:",receivers, "=",len(receivers) - d["NtoN"].append((senders,receivers)) + d["NtoN"].append((asenders,areceivers)) except Exception as e: print e continue return d def uccgraph_from_dict(d): - g = nx.Graph() + g = nx.DiGraph() + g.add_node("start") + g.add_node("end") g.add_nodes_from(d["unit"]) g.add_edges_from(d["ctlDep"]) g.add_edges_from(d["commDep"]) - for senders,receivers in d["NtoN"]: - g.add_node("NtoN0") + for i,(senders,receivers) in enumerate(d["NtoN"]): + g.add_node("NtoN"+str(i)) for node in senders: g.add_edge(node,"NtoN0") for node in receivers: g.add_edge("NtoN0",node) + for node in g: + if node != "start" and node != "end": + if len(g.predecessors(node)) == 0: + g.add_edge("start",node) + if len(g.successors(node)) == 0: + g.add_edge(node,"end") return g -def main(): - if len(sys.argv)<1: - sys.exit() - uccfile = open(sys.argv[1]) - d = read_from_file(uccfile) - print "Parsed UCC from file", sys.argv[1], "and found:" - if d.has_key("unit"): - print len(d["unit"]), "Units" - if d.has_key("ctlDep"): - print len(d["ctlDep"]), "Control Dependencies" - if d.has_key("commDep"): - print len(d["commDep"]), "Communication Dependencies" - if d.has_key("NtoN"): - print len(d["NtoN"]), "N to N communicating groups" - g = uccgraph_from_dict(d) - nx.draw_spectral(g) - plt.show() +########################## NON-WEIGHTED UCC ########################## +############ ranking ############ + +def rank_nodes(graph, start, end, lend={}, rankd={}): + path_to_here = 0 + if len(graph.predecessors(end)) == 0: + assert end == start + for node in graph.predecessors(end): + if not lend.has_key(node): + lend,rankd = rank_nodes(graph, start, node, lend, rankd) + try: + if lend[node] > path_to_here: + path_to_here = lend[node] + except KeyError: + print node + exit(0) + lend[end] = path_to_here + 1 + if rankd.has_key(lend[end]): + rankd[lend[end]].append(end) + else: + rankd[lend[end]] = [end] + return lend,rankd + +############ drawing ############ + +__rank_height = 100 +__index_width = 100 +__node_radius = 40 + + +def save_ucc_as_svg(graph,rankd): + node_positions = {} + nodes = draw_nodes(rankd,node_positions) + edges = draw_edges(graph,node_positions) + f = sf.Fig(nodes,edges) + w = (max([len(r) for r in rankd.values()]) + 1) * __index_width + h = (max(rankd.keys()) + 1) * __rank_height + c = sf.canvas(f.SVG(),width="{0}px".format(w),height="{0}px".format(h),viewBox='0 0 {0} {1}'.format(w,h)) + c.save("UCC.svg") + +def draw_nodes(rankd,node_positions): + f = sf.Fig() + for rank in rankd.keys(): + for index, node in enumerate(rankd[rank]): + f.d.append(draw_node(node,rank,index,node_positions)) + return f + +def draw_node(node,rank,index,node_positions): + cx = (index + 1)*__index_width + cy = (rank + 1)*__rank_height + node_positions[node] = (cx,cy) + r = __node_radius + c = sf.Ellipse(cx,cy,0,r,r,stroke='black',fill='yellow') + t = sf.Text(cx,cy,str(node),font_size=20) + return sf.Fig(c,t) + + +def draw_edges(graph,node_positions): + f = sf.Fig() + for edge in nx.edges(graph): + f.d.append(draw_edge(edge,node_positions)) + return f + +def draw_edge(edge,node_positions): + node1,node2 = edge + x1,y1 = node_positions[node1] + x2,y2 = node_positions[node2] + return sf.Line(x1,y1,x2,y2,style="stroke:rgb(0,0,0);stroke-width:3") + + + + +########################## WEIGHTED UCC ########################## + + +############ ranking ############ + +def rank_nodes_with_weight(graph, start, end, filename): + add_weights_to_nodes(graph, filename) + return do_recursive_ranking(graph, start, end) + +def add_weights_to_nodes(g, filename): + counterreader = (csv.reader)(filename,skipinitialspace=True) + events = {} + inversealiases = {} + for row in counterreader: + if row[0] == "event": + n = (int(row[2]),int(row[3])) + if not events.has_key(n): + events[n]={} + events[n][row[1]] = int(row[4]) + if not inversealiases.has_key(aliases[n]): + inversealiases[aliases[n]]=set() + inversealiases[aliases[n]].add(n) + for n in g: + try: + weight = 0 + if inversealiases.has_key(n): + for a in inversealiases[n]: + if events[n].has_key('Assigner_start') and events[n].has_key('AssignerInvocation_start'): + weight += events[n]['Assigner_start'] - events[n]['AssignerInvocation_start'] + if events[n].has_key('Assigner_end') and events[n].has_key('Assigner_start'): + weight += events[n]['Assigner_end'] - events[n]['Assigner_start'] + if events[n].has_key('Work_start') and events[n].has_key('Assigner_end'): + weight += events[n]['Work_start'] - events[n]['Assigner_end'] + if events[n].has_key('Work_end') and events[n].has_key('Work_start'): + weight += events[n]['Work_end'] - events[n]['Work_start'] + if events[n].has_key('AppResponderInvocation_start') and events[n].has_key('Work_end'): + weight += events[n]['AppResponderInvocation_start'] - events[n]['Work_end'] + if events[n].has_key('AppResponder_start') and events[n].has_key('AppResponderInvocation_start'): + weight += events[n]['AppResponder_start'] - events[n]['AppResponderInvocation_start'] + if events[n].has_key('AppResponder_end') and events[n].has_key('AppResponder_start'): + weight += events[n]['AppResponder_end'] - events[n]['AppResponder_start'] + if events[n].has_key('NextAssigner_start') and events[n].has_key('AppResponder_end'): + weight += events[n]['NextAssigner_start'] - events[n]['AppResponder_end'] + g.node[n]['weight'] = weight #+ weight/10 + except Exception as e: + print n + raise + + +def do_recursive_ranking(graph, start, end, lend={}, rankd={}): + path_to_here = 0 + if len(graph.predecessors(end)) == 0: + assert end == start + for node in graph.predecessors(end): + if not lend.has_key(node): + lend, rankd = do_recursive_ranking(graph, start, node, lend, rankd) + try: + if lend[node] > path_to_here: + path_to_here = lend[node] + except KeyError: + print "Error in annotate_critical_path on node ", node + exit(0) + try: + lend[end] = path_to_here + graph.node[end]['weight'] + if rankd.has_key(path_to_here): + rankd[path_to_here].append(end) + else: + rankd[path_to_here] = [end] + except KeyError: + print end, "has no attribute 'weight'" + exit(0) + return lend, rankd + + +############ drawing ############ + +__column_width = 110 +__node_width = 100 + +def save_ucc_with_weight(graph, lend, rankd): + node_positions = {} + scale_factor = 10000.0/float(lend['end']) + nodes, max_cols = draw_nodes_with_weight(graph, lend, rankd, node_positions, scale_factor) + edges = draw_edges_with_weight(graph,node_positions) + f = sf.Fig(nodes,edges) + #f.trans = "x,{0}*y".format(10000.0/float(lend['end'])) + w = (max_cols +1) * __column_width + h = 10000 + c = sf.canvas(f.SVG(),width="{0}px".format(w),height="{0}px".format(h),viewBox='0 0 {0} {1}'.format(w,h)) + c.save("UCC.svg") + +def draw_nodes_with_weight(graph, lend, rankd, node_positions, scale_factor=1.0): + f = sf.Fig() + columnoccupancy = {} + for rank in sorted(rankd.keys()): + for node in rankd[rank]: + f.d.append(draw_node_with_weight(graph, node, lend, rankd, columnoccupancy, node_positions, scale_factor)) + return f, max(columnoccupancy.keys()) + +def draw_node_with_weight(graph, node, lend, rankd, columnoccupancy, node_positions, scale_factor=1.0): + node_end = lend[node] + node_start = node_end - graph.node[node]['weight'] + column = 0 + while columnoccupancy.has_key(column) and columnoccupancy[column] > node_start: + column = column + 1 + columnoccupancy[column] = node_end + x1 = column * __column_width + y1 = node_start*scale_factor + x2 = x1 + __node_width + y2 = node_end*scale_factor + if y1 == y2: + y2 = y1+1 + node_positions[node] = (x1,y1,x2,y2) + r = sf.Rect(x1,y1,x2,y2,stroke='black',fill='yellow') + t = sf.Text((x1+x2)/2.0 , (y1+y2)/2.0 ,str(node),text_anchor="middle",font_size=20) + return sf.Fig(r,t) + +def draw_edges_with_weight(graph,node_positions): + f = sf.Fig() + for edge in nx.edges(graph): + f.d.append(draw_edge_with_weight(edge,node_positions)) + return f + +def draw_edge_with_weight(edge,node_positions): + node1,node2 = edge + x1_1,y1_1,x2_1,y2_1 = node_positions[node1] + x1_2,y1_2,x2_2,y2_2 = node_positions[node2] + m1 = (x1_1 + x2_1) / 2 + m2 = (x1_2 + x2_2) / 2 + return sf.Line(m1,y2_1,m2,y1_2,style="stroke:rgb(0,0,0);stroke-width:3") + + +########################## if __name__ == "__main__": main()