structuring.py 1.6 KB
Newer Older
1 2
from itertools import count

Sebastien Bourdeauducq's avatar
Sebastien Bourdeauducq committed
3 4 5
import networkx as nx
import matplotlib.pyplot as plt

6 7 8 9 10
from migen.flow.network import *
from migen.actorlib import structuring
from migen.actorlib.sim import *
from migen.sim.generic import Simulator
from migen.sim.icarus import Runner
Sebastien Bourdeauducq's avatar
Sebastien Bourdeauducq committed
11
from migen.flow import perftools
12 13 14 15

pack_factor = 5

def source_gen():
16
	for i in count(0):
17 18 19 20 21 22 23 24 25 26
		yield Token("source", {"value": i})

def sink_gen():
	while True:
		t = Token("sink")
		yield t
		print(t.value["value"])

def main():
	base_layout = [("value", BV(32))]
27 28
	packed_layout = structuring.pack_layout(base_layout, pack_factor)
	rawbits_layout = [("value", BV(32*pack_factor))]
29 30
	
	source = ActorNode(SimActor(source_gen(), ("source", Source, base_layout)))
31 32 33
	sink = ActorNode(SimActor(sink_gen(), ("sink", Sink, base_layout)))
	
	# A tortuous way of passing integer tokens.
34
	packer = ActorNode(structuring.Pack(base_layout, pack_factor))
35 36
	to_raw = ActorNode(structuring.Cast(packed_layout, rawbits_layout))
	from_raw = ActorNode(structuring.Cast(rawbits_layout, packed_layout))
37 38 39 40
	unpacker = ActorNode(structuring.Unpack(pack_factor, base_layout))
	
	g = DataFlowGraph()
	g.add_connection(source, packer)
41 42 43
	g.add_connection(packer, to_raw)
	g.add_connection(to_raw, from_raw)
	g.add_connection(from_raw, unpacker)
44 45
	g.add_connection(unpacker, sink)
	comp = CompositeActor(g)
Sebastien Bourdeauducq's avatar
Sebastien Bourdeauducq committed
46
	reporter = perftools.DFGReporter(g)
47
	
Sebastien Bourdeauducq's avatar
Sebastien Bourdeauducq committed
48
	fragment = comp.get_fragment() + reporter.get_fragment()
49
	sim = Simulator(fragment, Runner())
Sebastien Bourdeauducq's avatar
Sebastien Bourdeauducq committed
50 51 52 53 54 55 56
	sim.run(1000)
	
	g_layout = nx.spectral_layout(g)
	nx.draw(g, g_layout)
	nx.draw_networkx_edge_labels(g, g_layout, reporter.get_edge_labels())
	plt.show()

57 58

main()