From 3f32dcdef8bcdbbee72635fe47737cacfbc161ee Mon Sep 17 00:00:00 2001 From: Hugo Winbladh <hugwi268@student.liu.se> Date: Sun, 27 Aug 2023 20:31:08 +0000 Subject: [PATCH] Add a sink operation to avoid dangling nodes --- b_asic/core_operations.py | 43 +++++++++++++++++++++++++++++++++++- test/test_core_operations.py | 15 +++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/b_asic/core_operations.py b/b_asic/core_operations.py index 17e762e7..e731a480 100644 --- a/b_asic/core_operations.py +++ b/b_asic/core_operations.py @@ -73,7 +73,6 @@ class Constant(AbstractOperation): def __str__(self) -> str: return f"{self.value}" - class Addition(AbstractOperation): """ Binary addition operation. @@ -1263,3 +1262,45 @@ class Shift(AbstractOperation): if not isinstance(value, int): raise TypeError("value must be an int") self.set_param("value", value) + +class Sink(AbstractOperation): + r""" + Sink operation. + + Used for ignoring the output from another operation to avoid dangling output nodes. + + Parameters + ========== + + name : Name, optional + Operation name. + """ + + _execution_time = 0 + is_linear = True + + def __init__(self, name: Name = ""): + """Construct a Sink operation.""" + super().__init__( + input_count=1, + output_count=0, + name=name, + latency_offsets={"in0": 0}, + ) + + @classmethod + def type_name(cls) -> TypeName: + return TypeName("sink") + + def evaluate(self): + raise NotImplementedError + + @property + def latency(self) -> int: + return self.latency_offsets["in0"] + + def __repr__(self) -> str: + return "Sink()" + + def __str__(self) -> str: + return "sink" diff --git a/test/test_core_operations.py b/test/test_core_operations.py index 40f15f2f..002e38c9 100644 --- a/test/test_core_operations.py +++ b/test/test_core_operations.py @@ -20,6 +20,8 @@ from b_asic import ( SquareRoot, Subtraction, SymmetricTwoportAdaptor, + Sink, + SFG, ) @@ -404,3 +406,16 @@ class TestDepends: bfly1 = Butterfly() assert set(bfly1.inputs_required_for_output(0)) == {0, 1} assert set(bfly1.inputs_required_for_output(1)) == {0, 1} + +class TestSink: + def test_create_sfg_with_sink(self): + bfly = Butterfly() + sfg = bfly.to_sfg() + s = Sink() + sfg1 = sfg.replace_operation(s, "out0") + sfg2 = SFG(sfg1.input_operations, sfg1.output_operations[1:]) + + assert sfg2.output_count == 1 + assert sfg2.input_count == 2 + + assert sfg.evaluate_output(1, [0,1]) == sfg2.evaluate_output(0, [0,1]) -- GitLab