aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests/rfnoc_detailgraph_test.cpp
blob: 6273430e6cc1f8fe55991dcf367a94b76ab66f30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/rfnoc/node.hpp>
#include <uhd/utils/log.hpp>
#include <uhdlib/rfnoc/node_accessor.hpp>
#include <uhdlib/rfnoc/prop_accessor.hpp>
#include <uhdlib/rfnoc/graph.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>

#include "rfnoc_graph_mock_nodes.hpp"

using uhd::rfnoc::detail::graph_t;
using namespace uhd::rfnoc;

namespace uhd { namespace rfnoc { namespace detail {

/*! Helper class to access internals of detail::graph
 *
 * This is basically a cheat code to get around the 'private' part of graph_t.
 */
class graph_accessor_t
{
public:
    using vertex_descriptor = graph_t::rfnoc_graph_t::vertex_descriptor;

    graph_accessor_t(graph_t* graph_ptr) : _graph_ptr(graph_ptr)
    { /* nop */
    }

    graph_t::rfnoc_graph_t& get_graph()
    {
        return _graph_ptr->_graph;
    }

    template <typename VertexIterator>
    graph_t::node_ref_t get_node_ref_from_iterator(VertexIterator it)
    {
        return boost::get(graph_t::vertex_property_t(), get_graph(), *it);
    }

    auto find_neighbour(vertex_descriptor origin, res_source_info port_info)
    {
        return _graph_ptr->_find_neighbour(origin, port_info);
    }

    auto find_dirty_nodes()
    {
        return _graph_ptr->_find_dirty_nodes();
    }

    auto get_topo_sorted_nodes()
    {
        return _graph_ptr->_vertices_to_nodes(_graph_ptr->_get_topo_sorted_nodes());
    }
private:
    graph_t* _graph_ptr;
};

}}};

BOOST_AUTO_TEST_CASE(test_graph)
{
    graph_t graph{};
    uhd::rfnoc::detail::graph_accessor_t graph_accessor(&graph);
    node_accessor_t node_accessor{};

    auto& bgl_graph = graph_accessor.get_graph();

    // Define some mock nodes:
    // Source radio
    mock_radio_node_t mock_rx_radio(0);
    // Sink radio
    mock_radio_node_t mock_tx_radio(1);

    // These init calls would normally be done by the framework
    node_accessor.init_props(&mock_rx_radio);
    node_accessor.init_props(&mock_tx_radio);

    // In this simple graph, all connections are identical from an edge info
    // perspective, so we're lazy and share an edge_info object:
    uhd::rfnoc::detail::graph_t::graph_edge_t edge_info;
    edge_info.src_port                    = 0;
    edge_info.dst_port                    = 0;
    edge_info.property_propagation_active = true;
    edge_info.edge = uhd::rfnoc::detail::graph_t::graph_edge_t::DYNAMIC;

    // Now create the graph:
    graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info);

    // A whole bunch of low-level checks first:
    BOOST_CHECK_EQUAL(boost::num_vertices(bgl_graph), 2);
    auto vertex_iterators = boost::vertices(bgl_graph);
    auto vertex_iterator  = vertex_iterators.first;
    auto rx_descriptor    = *vertex_iterator;
    graph_t::node_ref_t node_ref =
        graph_accessor.get_node_ref_from_iterator(vertex_iterator++);
    BOOST_CHECK_EQUAL(node_ref->get_unique_id(), mock_rx_radio.get_unique_id());
    auto tx_descriptor = *vertex_iterator;
    node_ref           = graph_accessor.get_node_ref_from_iterator(vertex_iterator++);
    BOOST_CHECK_EQUAL(node_ref->get_unique_id(), mock_tx_radio.get_unique_id());
    BOOST_CHECK(vertex_iterator == vertex_iterators.second);

    auto rx_neighbour_info =
        graph_accessor.find_neighbour(rx_descriptor, {res_source_info::OUTPUT_EDGE, 0});
    BOOST_REQUIRE(rx_neighbour_info.first);
    BOOST_CHECK_EQUAL(
        rx_neighbour_info.first->get_unique_id(), mock_tx_radio.get_unique_id());
    BOOST_CHECK(std::tie(rx_neighbour_info.second.src_port,
                    rx_neighbour_info.second.dst_port,
                    rx_neighbour_info.second.property_propagation_active)
                == std::tie(edge_info.src_port,
                       edge_info.dst_port,
                       edge_info.property_propagation_active));

    auto tx_neighbour_info =
        graph_accessor.find_neighbour(tx_descriptor, {res_source_info::INPUT_EDGE, 0});
    BOOST_REQUIRE(tx_neighbour_info.first);
    BOOST_CHECK_EQUAL(
        tx_neighbour_info.first->get_unique_id(), mock_rx_radio.get_unique_id());
    BOOST_CHECK(std::tie(tx_neighbour_info.second.src_port,
                    tx_neighbour_info.second.dst_port,
                    tx_neighbour_info.second.property_propagation_active)
                == std::tie(edge_info.src_port,
                       edge_info.dst_port,
                       edge_info.property_propagation_active));

    auto rx_upstream_neighbour_info =
        graph_accessor.find_neighbour(rx_descriptor, {res_source_info::INPUT_EDGE, 0});
    BOOST_CHECK(rx_upstream_neighbour_info.first == nullptr);
    auto tx_downstream_neighbour_info =
        graph_accessor.find_neighbour(tx_descriptor, {res_source_info::OUTPUT_EDGE, 0});
    BOOST_CHECK(tx_downstream_neighbour_info.first == nullptr);
    auto rx_wrongport_neighbour_info =
        graph_accessor.find_neighbour(rx_descriptor, {res_source_info::OUTPUT_EDGE, 1});
    BOOST_CHECK(rx_wrongport_neighbour_info.first == nullptr);
    auto tx_wrongport_neighbour_info =
        graph_accessor.find_neighbour(tx_descriptor, {res_source_info::INPUT_EDGE, 1});
    BOOST_CHECK(tx_wrongport_neighbour_info.first == nullptr);

    // Check there are no dirty nodes (init_props() will clean them all)
    BOOST_CHECK_EQUAL(graph_accessor.find_dirty_nodes().empty(), true);

    auto topo_sorted_nodes = graph_accessor.get_topo_sorted_nodes();
    BOOST_CHECK_EQUAL(topo_sorted_nodes.size(), 2);
    BOOST_CHECK_EQUAL(
        topo_sorted_nodes.at(0)->get_unique_id(), mock_rx_radio.get_unique_id());

    // Now initialize the graph (will force a call to resolve_all_properties())
    graph.initialize();

    // This will be ignored
    graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info);
    BOOST_CHECK_EQUAL(boost::num_vertices(bgl_graph), 2);

    // Now attempt illegal connections (they must all fail)
    edge_info.src_port = 1;
    edge_info.dst_port = 0;
    BOOST_REQUIRE_THROW(
        graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info), uhd::rfnoc_error);
    edge_info.src_port = 0;
    edge_info.dst_port = 1;
    BOOST_REQUIRE_THROW(
        graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info), uhd::rfnoc_error);
    edge_info.src_port                    = 0;
    edge_info.dst_port                    = 0;
    edge_info.property_propagation_active = false;
    BOOST_REQUIRE_THROW(
        graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info), uhd::rfnoc_error);
}

BOOST_AUTO_TEST_CASE(test_graph_unresolvable)
{
    graph_t graph{};
    node_accessor_t node_accessor{};

    // Define some mock nodes:
    // Source radio
    mock_radio_node_t mock_rx_radio(0);
    // Sink radio
    mock_radio_node_t mock_tx_radio(1);

    // These init calls would normally be done by the framework
    node_accessor.init_props(&mock_rx_radio);
    node_accessor.init_props(&mock_tx_radio);

    // In this simple graph, all connections are identical from an edge info
    // perspective, so we're lazy and share an edge_info object:
    uhd::rfnoc::detail::graph_t::graph_edge_t edge_info(
        0, 0, graph_t::graph_edge_t::DYNAMIC, true);

    // Now create the graph and commit:
    graph.connect(&mock_rx_radio, &mock_tx_radio, edge_info);
    graph.initialize();

    // Now set a property that will cause the graph to fail to resolve:
    BOOST_REQUIRE_THROW(mock_tx_radio.set_property<double>("master_clock_rate", 100e6, 0),
        uhd::resolve_error);

    // Now we add a back-edge
    edge_info.src_port                    = 0;
    edge_info.dst_port                    = 0;
    edge_info.property_propagation_active = false;
    graph.connect(&mock_tx_radio, &mock_rx_radio, edge_info);
    UHD_LOG_INFO("TEST", "Testing back edge error path");
    mock_tx_radio.disable_samp_out_resolver = true;
    // The set_property would be valid if we hadn't futzed with the back-edge
    BOOST_REQUIRE_THROW(mock_tx_radio.set_property<double>("master_clock_rate", 200e6, 0),
        uhd::resolve_error);
    UHD_LOG_INFO("TEST", "^^^ Expected ERROR here.");
}