flow/network: refactor graph
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 8 Jun 2012 20:49:49 +0000 (22:49 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 8 Jun 2012 20:49:49 +0000 (22:49 +0200)
examples/dataflow.py
examples/dataflow_dma.py
examples/dataflow_sim.py
migen/flow/composer.py
migen/flow/network.py

index aab603f9a0ea8dcdfa317b7d92c383d3dec62123..5c3d5fc0e268e33767db2ec0d27a84e9a33d7cd1 100644 (file)
@@ -6,7 +6,7 @@ from migen.flow.ala import *
 from migen.flow.network import *
 from migen.flow.composer import *
 
-g = nx.MultiDiGraph()
+g = DataFlowGraph()
 a1 = make_composable(g, Add(BV(16)))
 a2 = make_composable(g, Add(BV(16)))
 a3 = make_composable(g, Add(BV(16)))
index 183c79ffcf893a5e3c5bee1962cf5ef48804d8f1..b5f24a7cff485fed3e1b9df9f7b2c0dc2061ab86 100644 (file)
@@ -1,4 +1,3 @@
-import networkx as nx
 from random import Random
 
 from migen.fhdl import verilog
@@ -43,9 +42,9 @@ def test_reader():
        adrgen = SimActor(adrgen_gen(), ("address", Source, [("a", BV(30))]))
        reader = dma_wishbone.Reader()
        dumper = SimActor(dumper_gen(), ("data", Sink, [("d", BV(32))]))
-       g = nx.MultiDiGraph()
-       add_connection(g, adrgen, reader)
-       add_connection(g, reader, dumper)
+       g = DataFlowGraph()
+       g.add_connection(adrgen, reader)
+       g.add_connection(reader, dumper)
        comp = CompositeActor(g)
        
        peripheral = MyPeripheral()
@@ -73,8 +72,8 @@ def test_writer():
        print("*** Testing writer")
        trgen = SimActor(trgen_gen(), ("address_data", Source, [("a", BV(30)), ("d", BV(32))]))
        writer = dma_wishbone.Writer()
-       g = nx.MultiDiGraph()
-       add_connection(g, trgen, writer)
+       g = DataFlowGraph()
+       g.add_connection(trgen, writer)
        comp = CompositeActor(g)
        
        peripheral = MyPeripheral()
index 3426a4fc9bbf9a0f6684b041b8233c5c660afba2..98d79652ee0074b26710bb906b751656f0131b3d 100644 (file)
@@ -1,5 +1,3 @@
-import networkx as nx
-
 from migen.fhdl.structure import *
 from migen.flow.actor import *
 from migen.flow.network import *
@@ -21,8 +19,8 @@ def sink_gen():
 def main():
        source = SimActor(source_gen(), ("source", Source, [("value", BV(32))]))
        sink = SimActor(sink_gen(), ("sink", Sink, [("value", BV(32))]))
-       g = nx.MultiDiGraph()
-       add_connection(g, source, sink)
+       g = DataFlowGraph()
+       g.add_connection(source, sink)
        comp = CompositeActor(g)
        def end_simulation(s):
                s.interrupt = source.done
index fe63d4da20d00579cc2b3a9859e61876de0697d9..b9237dfb73d31eb819dadee25ffef4a27cb07b33 100644 (file)
@@ -1,5 +1,3 @@
-import networkx as nx
-
 from migen.flow.actor import *
 from migen.flow.ala import *
 from migen.flow.plumbing import *
@@ -16,9 +14,9 @@ def _simple_binary(a, b, actor_class):
        signed = signal_self.bv.signed and signal_other.bv.signed
        actor = actor_class(BV(width, signed))
        combinator = Combinator(actor.token("operands").layout(), ["a"], ["b"])
-       add_connection(a.dfg, combinator, actor)
-       add_connection(a.dfg, a.actor, combinator, a.endp, "sink0")
-       add_connection(a.dfg, b.actor, combinator, b.endp, "sink1")
+       a.dfg.add_connection(combinator, actor)
+       a.dfg.add_connection(a.actor, combinator, a.endp, "sink0")
+       a.dfg.add_connection(b.actor, combinator, b.endp, "sink1")
        return make_composable(a.dfg, actor)
 
 class ComposableSource():
index 5eb98ac7fd4ac19899d15adbfa6f10972415cdce..60038443626c9fad340e8a8c8a8eba0dbe0b5839 100644 (file)
@@ -1,9 +1,21 @@
-import networkx as nx
+from networkx import MultiDiGraph
 
 from migen.fhdl.structure import *
 from migen.flow.actor import *
 from migen.corelogic.misc import optree
 
+class DataFlowGraph(MultiDiGraph):
+       def add_connection(self, source_node, sink_node, source_ep=None, sink_ep=None):
+               if source_ep is None:
+                       source_eps = source_node.sources()
+                       assert(len(source_eps) == 1)
+                       source_ep = source_eps[0]
+               if sink_ep is None:
+                       sink_eps = sink_node.sinks()
+                       assert(len(sink_eps) == 1)
+                       sink_ep = sink_eps[0]
+               self.add_edge(source_node, sink_node, source=source_ep, sink=sink_ep)
+
 class CompositeActor(Actor):
        def __init__(self, dfg): # TODO: endpoints
                self.dfg = dfg
@@ -16,14 +28,3 @@ class CompositeActor(Actor):
                others = sum([node.get_fragment() for node in self.dfg], Fragment())
                busy = Fragment([self.busy.eq(optree("|", [node.busy for node in self.dfg]))])
                return this + others + busy
-
-def add_connection(dfg, source_node, sink_node, source_ep=None, sink_ep=None):
-       if source_ep is None:
-               source_eps = source_node.sources()
-               assert(len(source_eps) == 1)
-               source_ep = source_eps[0]
-       if sink_ep is None:
-               sink_eps = sink_node.sinks()
-               assert(len(sink_eps) == 1)
-               sink_ep = sink_eps[0]
-       dfg.add_edge(source_node, sink_node, source=source_ep, sink=sink_ep)