def render_model(m): src=appBase+"/airfoil_graph_type.xml" (graphTypes,_)=load_graph_types_and_instances(src,src) graphType=graphTypes["airfoil"] nodeType=graphType.device_types["node"] cellType=graphType.device_types["cell"] edgeType=graphType.device_types["edge"] bedgeType=graphType.device_types["bedge"] printerType=graphType.device_types["printer"] graph=GraphInstance("airfoil_inst", graphType, { "gam":m["globals"].gam, "gm1":m["globals"].gm1, "cfl":m["globals"].cfl, "eps":m["globals"].eps, "mach":m["globals"].mach, "alpha":m["globals"].alpha, "qinf":m["globals"].qinf, } ) ################################################################### ################################################################### ## ## Create all devices o_cells=m["cells"] o_nodes=m["nodes"] o_pcell=m["pcell"] o_pedge=m["pedge"] o_pbedge=m["pbedge"] nodes=[ graph.create_device_instance("n{}".format(n.id),nodeType, {"x":n.x,"fanout":0}, None, {"loc":[n.x[0],n.x[1]]} ) for n in m["nodes"] ] def calc_hull(id,map): nodes=[o_nodes[i] for i in map[id]] return [ node.x for node in nodes ] def calc_loc(hull): (xx,yy)=zip(*hull) # Convert from list of pairs to pair of lists return [ sum(xx)/len(xx) , sum(yy)/len(yy) ] def calc_spatial_meta(id,map): hull=calc_hull(id,map) loc=calc_loc(hull) return {"hull":hull,"loc":loc} cells=[ graph.create_device_instance("c{}".format(c.id),cellType, {"id":c.id,"qinit":c.q}, None, calc_spatial_meta(c.id,o_pcell) ) for c in m["cells"] ] edges=[ graph.create_device_instance("e{}".format(e.id),edgeType, {"id":e.id}, None, calc_spatial_meta(e.id,o_pedge) ) for e in m["edges"] ] bedges=[ graph.create_device_instance("be{}".format(be.id),bedgeType, {"bound":int(be.bound),"id":be.id}, None, calc_spatial_meta(be.id,o_pbedge) ) for be in m["bedges"] ] printer=graph.create_device_instance("print",printerType,{"fanin":len(cells),"delta_print":100,"delta_exit":1000}) ################################################################### ################################################################### ## ## Add all edges cell_edges=m["cell_edges"] for (ci,nis) in enumerate(m["pcell"]): dstNode=cells[ci] for (index,ni) in enumerate(nis): srcNode=nodes[ni] srcNode.properties["fanout"]+=1 graph.create_edge_instance(dstNode,"pos_in",srcNode,"pos_out",{"index":index}) graph.create_edge_instance(srcNode,"ack_in",dstNode,"pos_ack_out") for (ci,nis) in enumerate(m["pedge"]): dstNode=edges[ci] for (index,ni) in enumerate(nis): srcNode=nodes[ni] srcNode.properties["fanout"]+=1 graph.create_edge_instance(dstNode,"pos_in",srcNode,"pos_out",{"index":index}) graph.create_edge_instance(srcNode,"ack_in",dstNode,"pos_ack_out") for (ci,nis) in enumerate(m["pbedge"]): dstNode=bedges[ci] for (index,ni) in enumerate(nis): srcNode=nodes[ni] srcNode.properties["fanout"]+=1 graph.create_edge_instance(dstNode,"pos_in",srcNode,"pos_out",{"index":index}) graph.create_edge_instance(srcNode,"ack_in",dstNode,"pos_ack_out") # send (q,adt) from cell to edges that surround it, and send the res back for (ei,cis) in enumerate(m["pecell"]): dstNode=edges[ei] for (index,ci) in enumerate(cis): srcNode=cells[ci] graph.create_edge_instance(dstNode,"q_adt_in",srcNode,"adt_calc",{"index":index}) graph.create_edge_instance(srcNode,"res_inc_in",dstNode,"res_calc_res{}".format(index+1)) for (bei, (ci,) ) in enumerate(m["pbecell"]): dstNode=bedges[bei] srcNode=cells[ci] graph.create_edge_instance(dstNode,"q_adt_in",srcNode,"adt_calc") graph.create_edge_instance(srcNode,"res_inc_in",dstNode,"bres_calc") for c in cells: graph.create_edge_instance(printer,"rms_inc",c,"update") graph.create_edge_instance(c,"rms_ack",printer,"rms_ack") return graph
from graph.core import * from graph.load_xml import load_graph_types_and_instances from graph.build_xml_stream import make_xml_stream_builder import sys import os import math import random import os appBase=os.path.dirname(os.path.realpath(__file__)) src=appBase+"/gals_heat_fix_noedge_graph_type.xml" (graphTypes,graphInstances)=load_graph_types_and_instances(src,src) urand=random.random n=16 maxTime=65 exportDeltaMask=15 if len(sys.argv)>1: n=int(sys.argv[1]) if len(sys.argv)>2: maxTime=int(sys.argv[2]) if len(sys.argv)>3: exportDeltaMask=int(sys.argv[3]) assert math.log2(exportDeltaMask+1).is_integer()
if ia < len(sys.argv): if sys.argv[ia] != "-": sys.stderr.write("Reading graph type from '{}'\n".format(sys.argv[ia])) source = open(sys.argv[ia], "rt") sourcePath = os.path.abspath(sys.argv[1]) ia += 1 dest = sys.stdout destPath = "[graph-cxx-prototype-file]" if ia < len(sys.argv): if sys.argv[ia] != "-": sys.stderr.write("Writing graph types to '{}'\n".format(sys.argv[ia])) dest = open(sys.argv[ia], "wt") destPath = os.path.abspath(sys.argv[ia]) ia += 1 (types, instances) = load_graph_types_and_instances(source, sourcePath) if len(types) != 1: raise RuntimeError("File did not contain exactly one graph type.") graph = None for g in types.values(): graph = g break map = CppTypeMap(graph) print(map.defs)
from graph.core import * from graph.load_xml import load_graph_types_and_instances from graph.save_xml import save_graph import sys import os import math import random urand = random.random src = sys.argv[1] (graphTypes, graphInstances) = load_graph_types_and_instances(src, "<stdin>") Ne = 80 Ni = 20 K = 20 if len(sys.argv) > 2: Ne = int(sys.argv[2]) if len(sys.argv) > 3: Ni = int(sys.argv[3]) if len(sys.argv) > 4: K = int(sys.argv[4]) N = Ne + Ni K = min(N, K) graphType = graphTypes["gals_izhikevich"] neuronType = graphType.device_types["neuron"]
nodeCircles.add( dwg.circle(id=di.id+"-circle",center=loc,r=node_circle_radius,fill="red") ) dwg.viewbox(minX,minY,maxX-minX,maxY-minY) dwg.save() if __name__=="__main__": import mock import argparse parser = argparse.ArgumentParser(description='Generate graph for airfoil.') parser.add_argument('source', type=str, help='Input xml file.') parser.add_argument('-o', dest='output', default="airfoil.svg", help='Where to save the file') parser.add_argument('--no-vectors',default=False,action="store_true") parser.add_argument('--no-edges',default=False,action="store_true") parser.add_argument('--no-nodes',default=False,action="store_true") args = parser.parse_args() (types,instances)=load_graph_types_and_instances(args.source,args.source) if len(instances)!=1: raise "Expected exactly one instance" for i in instances.values(): graph=i break render_model(graph,args.output, no_vectors=args.no_vectors, no_edges=args.no_edges, no_nodes=args.no_nodes)
def apply_checkpoints(graphInstPath: str, checkpointPath: str, eventLogPath: str, maxErrors: int = 10): # Load the graph instance (types, instances) = load_graph_types_and_instances(graphInstPath, graphInstPath) if len(instances) != 1: raise "Not exactly one instance." for i in instances.values(): graphInst = i # Load the checkpoints checkpointTree = etree.parse(checkpointPath) checkpointDoc = checkpointTree.getroot() checkpointGraphsNode = checkpointDoc checkpoints = {} # devId-> { key -> state } for cpsNode in checkpointGraphsNode.findall("p:Checkpoints", ns): sys.stderr.write("Loading checkpoints\n") for cpNode in cpsNode: if cpNode.tag != _cp: raise "Unknown node type in checkpoint file '{}'".format( cpNode.tag) devId = get_attrib(cpNode, "dev") key = get_attrib(cpNode, "key") stateText = cpNode.text state = json.loads("{" + stateText + "}") checkpoints.setdefault(devId, {})[key] = state # Walk through the events sys.stderr.write("Walking events\n") states = {} for di in graphInst.device_instances.values(): states[di.id] = create_default_typed_data(di.device_type.state) # Track the messages in flight, as if there is a global checkpoint we # need to ensure that the event log is in a causal order. # Map of message_id -> unreceived_count # If received_count is negative, then a message has been received, but # has not yet been sent, so we are acausal. # If recieved_count is positive, then there are messages which are sent but not yet delivered. # If 0, then they have matched up, and the entry should be deleted. in_flight = collections.Counter() # Number of messages with a negative receive count # Whenever it is positive, we are acausal acausal_count = 0 class LogSink(LogWriter): def __init__(self): self.gotErrors = 0 def _doCheck(self, dev, key, got): if dev not in checkpoints: sys.stderr.write( "{}, {} : No reference checkpoints found for checkpointed event.\n" .format(e.dev, key)) self.gotErrors += 1 return cps = checkpoints[dev] if key not in cps: sys.stderr.write( "{}, {} : No reference checkpoint event found.\n".format( dev, key)) return ref = cps[key] errors = compare_checkpoint("", ref, got) if len(errors) > 0: for err in errors: sys.stderr.write("{}, {} : {}\n".format(dev, key, err)) sys.stderr.write("ref = {}\n".format( json.dumps(ref, indent=" "))) sys.stderr.write("got = {}\n".format( json.dumps(got, indent=" "))) self.gotErrors += 1 def checkEvent(self, e): #sys.stderr.write("{}\n".format(e.dev)) preState = states[e.dev] postState = e.S states[e.dev] = postState for (pre, key) in e.tags: is_global = key.startswith("global:") if is_global: key = key[len("global:"):] if acausal_count > 0: raise RuntimeError( "Attempt to do global checkpoint when event log is acausal (receive before send)." ) #Always need to check this device got = preState if pre else postState self._doCheck(e.dev, key, got) if is_global: # And only sometimes check everything. If it is a global checkpoint then # we don't worry about pre/post for (dev, state) in states.items(): if dev == e.dev: continue self._doCheck(dev, key, state) if (self.gotErrors >= maxErrors): sys.stderr.write( "More than {} errors. Quitting.".format(maxErrors)) sys.exit(1) def onInitEvent(self, e): self.checkEvent(e) def onSendEvent(self, e): count = in_flight[e.eventId] if count < 0: acausal_count -= 1 count += e.fanout assert count >= 0 if count == 0: del in_flight[e.eventId] else: in_flight[e.eventId] self.checkEvent(e) def onRecvEvent(self, e): count = in_flight[e.sendEventId] if count == 0: acausal_count += 1 count -= 1 if count == 0: del in_flight[e.sendEventId] else: in_flight[e.sendEventId] = count self.checkEvent(e) sink = LogSink() parseEvents(eventLogPath, sink)
source=sys.stdin sourcePath="[XML-file]" if len(sys.argv) != 2: raise RuntimeError("This converts exactly one XML file. Please provide one path to an XML file") sys.stderr.write("Reading graph type from '{}'\n".format(sys.argv[1])) source=open(sys.argv[1],"rt") sourcePath=os.path.abspath(sys.argv[1]) dest=sys.stdout destPath="[XML-file]" # LOAD XML (types,instances)=load_graph_types_and_instances(source, sourcePath, {"p":"https://poets-project.org/schemas/virtual-graph-schema-v2"}, True) assert len(types) == 1, "Only one graph type can be converted at once, this contains {} graph types".format(len(types)) for t in types: graphType = types[t] # CONVERT v2 to v3 # Remove __init__ message type if it exists if "__init__" in graphType.message_types: graphType.message_types.pop("__init__") deviceTypes = graphType.device_types for dt in deviceTypes: if "__init__" in deviceTypes[dt].inputs: initPin = deviceTypes[dt].inputs["__init__"]