Example #1
0
    def test_specify_by_func(self):
        with dl.DeltaGraph() as graph:
            p_1 = dl.placeholder_node_factory()
            p_2 = dl.placeholder_node_factory(p_1)
            p_1.specify_by_node(self.printer_1(p_2))
            p_2.specify_by_func(self.printer_2, allow_const=True)

        for node in graph.nodes:
            self.assertIsInstance(node.body, dl.wiring.PyFuncBody)
Example #2
0
    def test_const_selfloop(self):
        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()
            p.specify_by_node(self.printer_1(p))

        for node in graph.nodes:
            self.assertIsInstance(node.body, dl.wiring.PyFuncBody)
def generate_graph_interactive_input(verbose=False):
    """This function returns instead a cyclical graph:

     -> Checker -> one_shot_run -> HwResetShaper-
    |                                            |
     --------------------------------------------
    In this way we can assert and deassert the reset
    and verify that the migen code extend the reset
    signal for N clock cycles
    """
    if verbose:
        lvl = logging.DEBUG
    else:
        lvl = logging.INFO
    with dl.DeltaGraph() as graph:
        ph = dl.placeholder_node_factory()
        oneshot = one_shot_run.call(status=ph)
        shaper = HwResetShaper(tb_num_iter=50,
                               name='reset_shaper_one_shot',
                               lvl=lvl,
                               vcd_name='reset_shaper_one_shot.vcd')
        shaper_runner = shaper.call(pulse_in=oneshot)
        checker = LengthChecker(5, interactive=True).check_shape(
            shaper_runner.reset_out)
        ph.specify_by_node(checker)

    return (graph, shaper)
    def test_loose_input(self):
        """Loose input wire is bad."""
        s = dl.lib.StateSaver(int)
        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()
            s.transfer(p)

        with self.assertRaises(dl.data_types.DeltaIOError):
            graph.check()
    def test_loose_optional_input(self):
        """Optional input without input does not raise an error."""
        @dl.DeltaBlock(allow_const=False)
        def test_node(a: dl.DOptional(int)) -> dl.Void:
            pass

        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()
            test_node(p)

        self.assertTrue(graph.check())
Example #6
0
    def test_union(self):
        union_mix = dl.Union([
            dl.Int(dl.Size(16)),
            dl.Bool(),
            dl.Str(dl.Size(10)),
            dl.Tuple([
                dl.Int(dl.Size(8)),
                dl.UInt(dl.Size(16)),
                dl.Float(dl.Size(64)),
                dl.Complex(dl.Size(128)),
                dl.Bool(),
                dl.Str(dl.Size(10))
            ]),
            dl.Record(RecATI)
        ])

        @dl.Interactive([("ack_union_mix", bool)],
                        [("out_union_mix", union_mix)])
        def testbench(node: dl.PythonNode):
            node.send(out_union_mix=5)
            assert node.receive("ack_union_mix")

            node.send(out_union_mix=False)
            assert node.receive("ack_union_mix")

            node.send(out_union_mix='abcd')
            assert node.receive("ack_union_mix")

            node.send(out_union_mix=(-5, 10, -1.5, (1.5 + 2.5j), False,
                                     'hello'))
            assert node.receive("ack_union_mix")

            node.send(out_union_mix=RecATI([1, 2], (3.0, 4), 5))
            assert node.receive("ack_union_mix")

            raise DeltaRuntimeExit

        s_union_mix = dl.lib.StateSaver(union_mix, verbose=True)

        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()

            p.specify_by_node(
                testbench.call(s_union_mix.save_and_ack(p.out_union_mix)))

        self.check_executes_graph(
            graph, """\
            saving 5
            saving False
            saving abcd
            saving (-5, 10, -1.5, (1.5+2.5j), False, 'hello')
            saving RecATI(x=[1, 2], y=(3.0, 4), z=5)
            """)
Example #7
0
def get_graph():
    """Return the experiments graph `DeltaGraph` and data store instances.

    Note that the aggregator and commanger files can be provided with
    `vcd_name` which will lead to saving VCD of all signals for further
    debugging.
    """
    result_storage = dl.lib.StateSaver(int)
    cmds_storage = dl.lib.StateSaver(dl.DUInt(dl.DSize(32)))
    hal_template = dl.lib.hal_template

    with dl.DeltaGraph() as graph:
        ph_hal_result = dl.placeholder_node_factory()
        ph_commander = dl.placeholder_node_factory()

        # aggregator node of HAL results
        result_aggregator = Aggregator(
            name="result_aggregator",
            vcd_name=None).call(hal_result=ph_hal_result,
                                shot_completed=ph_commander.shot_completed)

        # commander node to send HAL instructions
        command_sender = Commander(
            name="command_sender",
            vcd_name=None).call(angle=result_aggregator.next_angle)

        hal_result = hal_template.call(hal_command=command_sender.hal_command)

        # local store for experiment results
        result_storage.save(result_aggregator.agg_result)
        cmds_storage.save(command_sender.hal_command)

        # tie up placeholders
        ph_hal_result.specify_by_node(hal_result)
        ph_commander.specify_by_node(command_sender)

        # listen for flag to stop runtime
        experiment_stopper(result_aggregator.completed)

    return graph, result_storage, cmds_storage
Example #8
0
    def test_compound(self):
        tuple_mix = dl.Tuple([
            dl.Int(dl.Size(8)),
            dl.UInt(dl.Size(16)),
            dl.Float(dl.Size(64)),
            dl.Complex(dl.Size(128)),
            dl.Bool(),
            dl.Str(dl.Size(10))
        ])
        array_float = dl.Array(dl.Float(dl.Size(64)), dl.Size(3))
        record_mix = dl.Record(RecATI)

        @dl.Interactive([("ack_tuple_mix", bool), ("ack_array_float", bool),
                         ("ack_record_mix", bool)],
                        [("out_tuple_mix", tuple_mix),
                         ("out_array_float", array_float),
                         ("out_record_mix", record_mix)])
        def testbench(node: dl.PythonNode):
            node.send(out_tuple_mix=(-5, 1000, -100.5, (1.5 + 2.5j), False,
                                     '0123456789'))
            assert node.receive("ack_tuple_mix")

            node.send(out_array_float=[0.5, -0.25, 0.125])
            assert node.receive("ack_array_float")

            node.send(out_record_mix=RecATI([1, 2], (3.0, 4), 5))
            assert node.receive("ack_record_mix")

            raise DeltaRuntimeExit

        s_tuple_mix = dl.lib.StateSaver(tuple_mix, verbose=True)
        s_array_float = dl.lib.StateSaver(array_float, verbose=True)
        s_record_mix = dl.lib.StateSaver(record_mix, verbose=True)

        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()

            p.specify_by_node(
                testbench.call(s_tuple_mix.save_and_ack(p.out_tuple_mix),
                               s_array_float.save_and_ack(p.out_array_float),
                               s_record_mix.save_and_ack(p.out_record_mix)))

        self.check_executes_graph(
            graph, """\
            saving (-5, 1000, -100.5, (1.5+2.5j), False, '0123456789')
            saving [0.5, -0.25, 0.125]
            saving RecATI(x=[1, 2], y=(3.0, 4), z=5)
            """)
Example #9
0
    def test_non_const_node_via_placeholder(self):
        """Non-constant node created via a placeholder has one output reused
        multiple times."""
        with dl.DeltaGraph() as graph:
            output = dl.placeholder_node_factory()
            terminate_non_const(output)
            terminate_non_const(output)
            terminate_non_const(output)

            output.specify_by_node(return_1_non_const())

        self.assertTrue(graph.check())
        self.assertEqual(len(graph.nodes), 5)
        self.assertEqual(
            type(graph.find_node_by_name("return_1_non_const").body),
            dl.wiring.PyFuncBody)
        self.assertEqual(type(graph.find_node_by_name("splitter").body),
                         dl.wiring.PyFuncBody)
Example #10
0
    def setUp(self):
        r"""Build the following graph
        ```
                    add_1
                    /   \
                   ||   ||
                   ||   ||
                    \   /
                   increment ----------> saver
        ```
        """
        self.saver = dl.lib.StateSaver(int)
        with dl.DeltaGraph() as graph:
            add_1_placeholder = dl.placeholder_node_factory()
            incr_node = opt_increment.call(n=add_1_placeholder)
            self.saver.save_and_exit(incr_node.y)
            add_one = add1_or0(incr_node.x)
            add_1_placeholder.specify_by_node(add_one)

        self.graph = graph
        self.rt = dl.DeltaPySimulator(graph)
Example #11
0
    def test_PyMigenBody_python(self):
        @dl.DeltaBlock(allow_const=False)
        def exit_if_6_else_inc(n: int) -> int:
            print(n)
            if n == 6:
                raise DeltaRuntimeExit
            else:
                return n + 1

        with dl.DeltaGraph() as graph:
            ph = dl.placeholder_node_factory()
            c1 = MigenIncrementer().call(i1=ph)
            ex = exit_if_6_else_inc(c1.o1)
            ph.specify_by_node(ex)

        self.check_executes_graph(
            graph, """\
            0
            2
            4
            6
            """)
Example #12
0
    def test_integration(self):
        """Constructs the following graph:
        +---------------------------+
        |             SAVE          |
        |   +-----+    ^            |
        +--->Add  +----+            |
            |to 10|        +----+   |
            |     +-------->    |   |
            +-----+        |ADD +---+
                     2 --->|    |
                           +----+
        """
        s = dl.lib.StateSaver(int, verbose=True)

        with dl.DeltaGraph() as graph:
            add_ph = dl.placeholder_node_factory()
            b = return_2_const()
            int_node = add_until_10.call(num=add_ph)
            add_node = add_non_const(b, int_node.x)
            add_ph.specify_by_node(add_node)
            s.save_and_exit(int_node.y)

        self.check_executes_graph(graph, "saving 11\n")
Example #13
0
    if time_rf >= time_pmt:
        exp_time = time_rf - time_pmt
    else:
        exp_time = math.pow(2, _TIME_RES) + time_rf - time_pmt
    logging.debug(
        f'time result {time_out}, expected time {exp_time}, reset {reset}')
    assert time_out == exp_time
    assert reset == 1


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    with dl.DeltaGraph() as graph:

        # define placeholders
        p0_tb = dl.placeholder_node_factory()

        dut = TimestampChipInterface(
            name="timestamper_interface",
            vcd_name="tb_timestamper_itf.vcd").call(time_in=p0_tb)

        tb = testbench.call(time_out=dut.time_out, reset=dut.counter_reset)

        # resolve placeholders
        p0_tb.specify_by_node(tb)

    # run graph
    print(graph)
    rt = dl.DeltaPySimulator(graph)
    rt.run()
Example #14
0
    def test_primitives(self):
        tuple_int = dl.Tuple([
            dl.Int(dl.Size(8)),
            dl.Int(dl.Size(16)),
            dl.Int(dl.Size(32)),
            dl.Int(dl.Size(64))
        ])
        tuple_uint = dl.Tuple([
            dl.UInt(dl.Size(8)),
            dl.UInt(dl.Size(16)),
            dl.UInt(dl.Size(32)),
            dl.UInt(dl.Size(64))
        ])
        tuple_float = dl.Tuple([dl.Float(dl.Size(32)), dl.Float(dl.Size(64))])
        tuple_complex = dl.Tuple(
            [dl.Complex(dl.Size(64)),
             dl.Complex(dl.Size(128))])
        tuple_bool_char = dl.Tuple([dl.Bool(), dl.Str(dl.Size(1))])

        @dl.Interactive([("ack_int", bool), ("ack_uint", bool),
                         ("ack_float", bool), ("ack_complex", bool),
                         ("ack_bool_char", bool)],
                        [("out_int", tuple_int), ("out_uint", tuple_uint),
                         ("out_float", tuple_float),
                         ("out_complex", tuple_complex),
                         ("out_bool_char", tuple_bool_char)])
        def testbench(node: dl.PythonNode):
            node.send(out_int=(-128, -32768, -2147483648,
                               -9223372036854775808))
            assert node.receive("ack_int")

            node.send(out_int=(127, 32767, 2147483647, 9223372036854775807))
            assert node.receive("ack_int")

            node.send(out_uint=(0, 0, 0, 0))
            assert node.receive("ack_uint")

            node.send(out_uint=(255, 65535, 4294967295, 18446744073709551615))
            assert node.receive("ack_uint")

            # this is just a rough estimate
            node.send(out_float=(1.0000001, 1.000000000000001))
            assert node.receive("ack_float")

            node.send(out_complex=((1.0000001 + 1.0000001j),
                                   (1.000000000000001 + 1.000000000000001j)))
            assert node.receive("ack_complex")

            node.send(out_bool_char=(True, 'a'))
            assert node.receive("ack_bool_char")

            raise DeltaRuntimeExit

        s_int = dl.lib.StateSaver(tuple_int, verbose=True)
        s_uint = dl.lib.StateSaver(tuple_uint, verbose=True)
        s_float = dl.lib.StateSaver(tuple_float, verbose=True)
        s_complex = dl.lib.StateSaver(tuple_complex, verbose=True)
        s_bool_char = dl.lib.StateSaver(tuple_bool_char, verbose=True)

        with dl.DeltaGraph() as graph:
            p = dl.placeholder_node_factory()

            p.specify_by_node(
                testbench.call(s_int.save_and_ack(p.out_int),
                               s_uint.save_and_ack(p.out_uint),
                               s_float.save_and_ack(p.out_float),
                               s_complex.save_and_ack(p.out_complex),
                               s_bool_char.save_and_ack(p.out_bool_char)))

        self.check_executes_graph(
            graph, f"""\
            saving (-128, -32768, -2147483648, -9223372036854775808)
            saving (127, 32767, 2147483647, 9223372036854775807)
            saving (0, 0, 0, 0)
            saving (255, 65535, 4294967295, 18446744073709551615)
            saving ({np.float32(1.0000001)}, 1.000000000000001)
            saving (({np.float32(1.0000001)}+{np.float32(1.0000001)}j), (1.000000000000001+1.000000000000001j))
            saving (True, 'a')
            """)
Example #15
0
    plt.plot(list(data.keys()), data.values(), 'ro')
    plt.savefig('histogram.png')


@dl.DeltaBlock()
def user_interface() -> bool:
    if input('Start Experiment (y/n): ') == 'y':
        return True
    else:
        raise dl.DeltaRuntimeExit


with dl.DeltaGraph() as graph:
    ui = user_interface()
    trig = triggers.call()
    cntr = counter.call(pmt=trig.pmt_trigger, rf=trig.rf_trigger)

    p1_dac_status = dl.placeholder_node_factory()
    p2_dac_voltage = dl.placeholder_node_factory()
    accume = accumulator.call(new_time=cntr,
                              DAC_status=p1_dac_status,
                              DAC_voltage=p2_dac_voltage,
                              experiment_start=ui)
    dac = DAC_control.call(command=accume.DAC_command, params=accume.DAC_param)
    p1_dac_status.specify_by_node(dac.node_status)
    p2_dac_voltage.specify_by_node(dac.return_data)

print(graph)
rt = dl.DeltaPySimulator(graph)
rt.run()