diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-03-24 16:54:11 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 11:49:13 -0800 |
commit | efb1d5a4729ea892ea03b8d0265aae9e8fadfff1 (patch) | |
tree | 23cd4dbf85c635d381aaf7b0f6e9bd15eeca08fc /host/include | |
parent | 44f9bca2c5b561fa3f5f08d0d616c91d2142cbf9 (diff) | |
download | uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.tar.gz uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.tar.bz2 uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.zip |
rfnoc: Add properties, nodes, and accessors
Adds the following classes:
- uhd::rfnoc::node_t, the base class for RFNoC nodes
- uhd::rfnoc::node_accessor_t, a class to access private properties
- uhd::rfnoc::res_source_info, a struct that identifies where properties
come from
- uhd::rfnoc::property_t, and property_base_t (its parent)
- uhd::rfnoc::prop_accessor_t, a class to access properties
Add always dirty property (dirtifier).
Also adds unit tests for properties.
Diffstat (limited to 'host/include')
-rw-r--r-- | host/include/uhd/rfnoc/CMakeLists.txt | 3 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/dirtifier.hpp | 54 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/node.hpp | 206 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/node.ipp | 78 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/property.hpp | 257 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/property.ipp | 25 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/res_source_info.hpp | 89 |
7 files changed, 712 insertions, 0 deletions
diff --git a/host/include/uhd/rfnoc/CMakeLists.txt b/host/include/uhd/rfnoc/CMakeLists.txt index 7b5b69f4b..bc5d7f054 100644 --- a/host/include/uhd/rfnoc/CMakeLists.txt +++ b/host/include/uhd/rfnoc/CMakeLists.txt @@ -13,9 +13,12 @@ if(ENABLE_RFNOC) blockdef.hpp block_id.hpp constants.hpp + dirtifier.hpp graph.hpp node_ctrl_base.hpp node_ctrl_base.ipp + node.hpp + node.ipp rate_node_ctrl.hpp scalar_node_ctrl.hpp sink_block_ctrl_base.hpp diff --git a/host/include/uhd/rfnoc/dirtifier.hpp b/host/include/uhd/rfnoc/dirtifier.hpp new file mode 100644 index 000000000..cc4430d3d --- /dev/null +++ b/host/include/uhd/rfnoc/dirtifier.hpp @@ -0,0 +1,54 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_DIRTIFYIER_HPP +#define INCLUDED_LIBUHD_DIRTIFYIER_HPP + +#include <uhd/rfnoc/property.hpp> + +namespace uhd { namespace rfnoc { + +/*! This is a special class for property that is always dirty. This is useful + * to force property resolutions in certain cases. + * + * Note: This has nothing to do with 'dirtify' in the CGI/graphics sense. + */ +class dirtifier_t : public property_base_t +{ +public: + dirtifier_t() + : property_base_t("__ALWAYS_DIRTY__", res_source_info(res_source_info::FRAMEWORK)) + { + // nop + } + + //! This property is always dirty + bool is_dirty() const { return true; } + + //! This property is never equal to anything else + bool equal(property_base_t*) const { return false; } + + //! Always dirty, so this can be called as often as we like + void force_dirty() {} + +private: + //! This property cannot be marked clean, but nothing happens if you try + void mark_clean() {} + + //! The value from this property cannot be forwarded + void forward(property_base_t*) + { + throw uhd::type_error("Cannot forward to or from dirtifier property!"); + } + + //! This property never has the same type as another type + bool is_type_equal(property_base_t*) const { return false; } +}; + +}} /* namespace uhd::rfnoc */ + +#endif /* INCLUDED_LIBUHD_DIRTIFYIER_HPP */ + diff --git a/host/include/uhd/rfnoc/node.hpp b/host/include/uhd/rfnoc/node.hpp new file mode 100644 index 000000000..5f8a15f4f --- /dev/null +++ b/host/include/uhd/rfnoc/node.hpp @@ -0,0 +1,206 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_RFNOC_NODE_HPP +#define INCLUDED_LIBUHD_RFNOC_NODE_HPP + +#include <uhd/rfnoc/property.hpp> +#include <uhd/utils/scope_exit.hpp> +#include <uhd/utils/log.hpp> +#include <unordered_map> +#include <unordered_set> +#include <boost/optional.hpp> +#include <functional> +#include <memory> +#include <mutex> +#include <string> +#include <tuple> +#include <vector> + +namespace uhd { namespace rfnoc { + +/*! The base class for all nodes within an RFNoC graph + * + * The block supports the following types of data access: + * - High-level property access + * - Action execution + */ +class UHD_API node_t +{ +public: + using resolver_fn_t = std::function<void(void)>; + + /****************************************** + * Basic Operations + ******************************************/ + //! Return a unique identifier string for this node. In every RFNoC graph, + // no two nodes cannot have the same ID. + // + // \returns The unique ID as a string + virtual std::string get_unique_id() const; + + /*! Return the number of input ports for this block. + * + * This function needs to be overridden. + * + * \return noc_id The number of ports + */ + virtual size_t get_num_input_ports() const = 0; + + /*! Return the number of output ports for this block. + * + * This function needs to be overridden. + * + * \return noc_id The number of ports + */ + virtual size_t get_num_output_ports() const = 0; + + /****************************************** + * Property Specific + ******************************************/ + + /*! Return the names of all possible user properties that can be + * accessed for this block. + * + * Note that the type of the property is not auto-detectable. + * + * \returns A vector of all possible IDs of user properties supported by + * this block. + */ + std::vector<std::string> get_property_ids() const; + + /*! Set a specific user property that belongs to this block. + * + * Setting a user property will trigger a property resolution. This means + * that changing this block can have effects on other nodes. + * + * If the property does not exist, or if the property can be determined to + * be of a different type than \p prop_data_t due to the usage of runtime + * type information (RTTI), a lookup_error is thrown. + * + * \param prop_data_t The data type of the property + * \param id The identifier of the property to write. To find out which + * values of \p id are valid, call get_property_ids() + * \param instance The instance number of this property + * \param val The new value of the property. + */ + template <typename prop_data_t> + void set_property( + const std::string& id, const prop_data_t& val, const size_t instance = 0); + + /*! Get the value of a specific block argument. \p The type of an argument + * must be known at compile time. + * + * If the property does not exist, or if the property can be determined to + * be of a different type than \p prop_data_t due to the usage of runtime + * type information (RTTI), a lookup_error is thrown. + * + * Note: Despite this being a "getter", this function is not declared const. + * This is because internally, it can resolve properties, which may cause + * changes within the object. + * + * \param prop_data_t The data type of the property + * \param id The identifier of the property to write. + * \param instance The instance number of this property + * \return The value of the property. + * \throws uhd::lookup_error if the property can't be found. + */ + template <typename prop_data_t> + const prop_data_t& get_property( + const std::string& id, const size_t instance = 0) /* mutable */; + + /****************************************** + * Action Specific + ******************************************/ + // TBW + +protected: + /****************************************** + * Internal Registration Functions + ******************************************/ + + /*! Register a property for this block + * + * \param prop A reference to the property + * + * \throws uhd::key_error if another property with the same ID and source + * type is already registered + */ + void register_property(property_base_t* prop); + + /*! Add a resolver function to this block. A resolver function is used to + * reconcile state changes in the block, and is triggered by a + * user or other upstream/downstream blocks. A block may have multiple + * resolvers. + * + * NOTE: Multiple resolvers may share properties for reading and a + * resolver may read multiple properties + * NOTE: The framework will perform run-time validation to + * ensure read/write property access is not violated. This means the + * resolver function can only read values that are specified in the + * \p inputs list, and only write values that are specified in the + * \p outputs list. + * + * \param inputs The properties that this resolver will read + * \param outputs The properties that this resolver will write to + * \param resolver_fn The resolver function + */ + void add_property_resolver(std::set<property_base_t*>&& inputs, + std::set<property_base_t*>&& outputs, + resolver_fn_t&& resolver_fn); + + /*! Handle a request to perform an action. The default action handler + * ignores user action and forwards port actions. + * + * \param handler The function that is called to handle the action + */ + //void register_action_handler(std::function< + //void(const action_info& info, const res_source_info& src) + //> handler); + + /****************************************** + * Internal action forwarding + ******************************************/ + // TBW + // + +private: + + /*! Return a reference to a property, if it exists. + * + * \returns A reference to the property, if it exists, or nullptr otherwise + */ + property_base_t* _find_property( + res_source_info src_info, const std::string& id) const; + + /*! RAII-Style property access + */ + uhd::utils::scope_exit::uptr _request_property_access( + property_base_t* prop, property_base_t::access_t access) const; + + /****** Attributes *******************************************************/ + //! Mutex to lock access to the property registry + mutable std::mutex _prop_mutex; + + //! Stores a reference to every registered property (Property Registry) + std::unordered_map<res_source_info::source_t, + std::vector<property_base_t*>, + std::hash<size_t> > + _props; + + using property_resolver_t = std::tuple<std::set<property_base_t*>, + std::set<property_base_t*>, + resolver_fn_t> ; + //! Stores the list of property resolvers + std::vector<property_resolver_t> _prop_resolvers; + +}; // class node_t + +}} /* namespace uhd::rfnoc */ + +#include <uhd/rfnoc/node.ipp> + +#endif /* INCLUDED_LIBUHD_RFNOC_NODE_HPP */ diff --git a/host/include/uhd/rfnoc/node.ipp b/host/include/uhd/rfnoc/node.ipp new file mode 100644 index 000000000..7de69063c --- /dev/null +++ b/host/include/uhd/rfnoc/node.ipp @@ -0,0 +1,78 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_RFNOC_NODE_IPP +#define INCLUDED_LIBUHD_RFNOC_NODE_IPP + +#include <boost/format.hpp> +#include <boost/units/detail/utility.hpp> + +namespace { + +template <typename prop_data_t> +uhd::rfnoc::property_t<prop_data_t>* _assert_prop( + uhd::rfnoc::property_base_t* prop_base_ptr, + const std::string& node_id, + const std::string& prop_id) +{ + // First check if the pointer is valid at all: + if (prop_base_ptr == nullptr) { + throw uhd::lookup_error( + str(boost::format("[%s] Unknown user property: `%s'") % node_id % prop_id)); + } + + // Next, check if we can cast the pointer to the desired type: + auto prop_ptr = + dynamic_cast<uhd::rfnoc::property_t<prop_data_t>*>(prop_base_ptr); + if (!prop_ptr) { + throw uhd::type_error(str( + boost::format( + "[%s] Found user property `%s', but could not cast to requested type `%s'!") + % node_id % prop_id % boost::units::detail::demangle(typeid(prop_data_t).name()) )); + } + + // All is good, we now return the raw pointer that has been validated. + return prop_ptr; +} + +} // namespace + +namespace uhd { namespace rfnoc { + +template <typename prop_data_t> +void node_t::set_property( + const std::string& id, const prop_data_t& val, const size_t instance) +{ + res_source_info src_info{res_source_info::USER, instance}; + UHD_LOG_TRACE(get_unique_id(), "Setting property `" << id << "`"); + auto prop_ptr = + _assert_prop<prop_data_t>(_find_property(src_info, id), get_unique_id(), id); + { + auto prop_access = _request_property_access(prop_ptr, property_base_t::RW); + prop_ptr->set(val); + } + + // resolve_all() TODO +} + +template <typename prop_data_t> +const prop_data_t& node_t::get_property(const std::string& id, const size_t instance) +{ + res_source_info src_info{res_source_info::USER, instance}; + + // resolve_all() TODO + auto prop_ptr = _assert_prop<prop_data_t>( + _find_property(src_info, id), get_unique_id(), id); + + auto prop_access = _request_property_access(prop_ptr, property_base_t::RO); + return prop_ptr->get(); +} + + +}} /* namespace uhd::rfnoc */ + +#endif /* INCLUDED_LIBUHD_RFNOC_NODE_IPP */ + diff --git a/host/include/uhd/rfnoc/property.hpp b/host/include/uhd/rfnoc/property.hpp new file mode 100644 index 000000000..a85c978e9 --- /dev/null +++ b/host/include/uhd/rfnoc/property.hpp @@ -0,0 +1,257 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_PROPERTY_HPP +#define INCLUDED_LIBUHD_PROPERTY_HPP + +#include <uhd/exception.hpp> +#include <uhd/rfnoc/res_source_info.hpp> +#include <uhd/utils/dirty_tracked.hpp> +#include <string> +#include <memory> + + +namespace uhd { namespace rfnoc { + +// Forward declaration, separates includes +class prop_accessor_t; + +/*! Base class for properties + * + */ +class UHD_API property_base_t +{ +public: + enum access_t { + NONE, //!< Neither reading nor writing to this property is permitted + RO = 0x1, //!< Read-Only + RW = 0x3, //!< Read-Write + RWLOCKED = 0x5 //!< Write is locked. This lets you call set(), but only if the value is unchanged. + }; + + property_base_t(const std::string& id, const res_source_info& source_info) + : _id(id), _source_info(source_info) + { + } + + //! Gets the ID (name) of this property + const std::string& get_id() const + { + return _id; + } + + //! Return the source info for this property + const res_source_info& get_src_info() const + { + return _source_info; + } + + //! Query this property's dirty flag. + // + // If it's true, that means this property was recently changed, but changes + // have not propagated yet and still need resolving. + virtual bool is_dirty() const = 0; + + //! Returns true if this property can be read. + bool read_access_granted() const + { + return static_cast<uint8_t>(_access_mode) & 0x1; + } + + //! Returns true if this property can be written to. + bool write_access_granted() const + { + return static_cast<uint8_t>(_access_mode) & 0x2; + } + + //! Return the current access mode + access_t get_access_mode() const + { + return _access_mode; + } + + //! Return true if rhs has the same type and value + virtual bool equal(property_base_t* rhs) const = 0; + + //! Create a copy of this property + // + // The copy must have the same type, value, and ID. However, it is often + // desirable to have a new source information, so that can be overridden. + // + // The cleanliness state of \p original is not preserved. The new property + // will have the same cleanliness state as any other new property. + // + virtual std::unique_ptr<property_base_t> clone(res_source_info) + { + throw uhd::not_implemented_error("Cloning is not available for this property."); + } + + virtual void force_dirty() = 0; + +private: + friend class prop_accessor_t; + + //! Reset the dirty bit. See also dirty_tracked::mark_clean() + virtual void mark_clean() = 0; + + //! Forward the value of this property to another one + virtual void forward(property_base_t* next_prop) = 0; + + //! Compare property types + // + // Note: This uses RTTI to evaluate type equality + virtual bool is_type_equal(property_base_t* other_prop) const = 0; + + /*** Attributes **********************************************************/ + //! Stores an ID string for this property. They don't need to be unique. + const std::string _id; + + //! Stores the source info for this property. + const res_source_info _source_info; + + //! Access mode. Note that the prop_accessor_t is the only one who can + // write this. + access_t _access_mode = RO; +}; + +/*! + * An encapsulation class for a block property. + */ +template <typename data_t> +class property_t : public property_base_t +{ +public: + //! We want to be good C++ citizens + using value_type = data_t; + + property_t(const std::string& id, data_t&& value, const res_source_info& source_info); + + property_t( + const std::string& id, const data_t& value, const res_source_info& source_info); + + property_t(const property_t<data_t>& prop) = default; + + //! Returns the dirty state of this property + // + // If true, this means the value was recently changed, but it wasn't marked + // clean yet. + bool is_dirty() const + { + return _data.is_dirty(); + } + + bool equal(property_base_t* rhs) const + { + if (!is_type_equal(rhs)) { + return false; + } + return get() == dynamic_cast<property_t<data_t>*>(rhs)->get(); + } + + std::unique_ptr<property_base_t> clone(res_source_info new_src_info) override + { + return std::unique_ptr<property_base_t>(new property_t<data_t>( + get_id(), get(), new_src_info)); + } + + //! Returns the source info for the property + // const res_source_info& get_src_info() const = 0; + + //! Set the value of this property + // + // \throws uhd::access_error if the current access mode is not RW or RWLOCKED + // \throws uhd::resolve_error if the property is RWLOCKED but the new value + // doesn't match + void set(const data_t& value) + { + if (write_access_granted()) { + _data = value; + } else if (get_access_mode() == RWLOCKED) { + if (_data.get() != value) { + throw uhd::resolve_error(std::string("Attempting to overwrite property `") + + get_id() + "@" + get_src_info().to_string() + + "' with a new value after it was locked!"); + } + } else { + throw uhd::access_error(std::string("Attempting to write to property `") + + get_id() + "' without access privileges!"); + } + } + + void force_dirty() + { + if (write_access_granted()) { + _data.force_dirty(); + } else if (get_access_mode() == RWLOCKED) { + if (!_data.is_dirty()) { + throw uhd::resolve_error(std::string("Attempting to overwrite property `") + + get_id() + + "' with dirty flag after it was locked!"); + } + } else { + throw uhd::access_error(std::string("Attempting to flag dirty property `") + + get_id() + "' without access privileges!"); + } + } + + //! Get the value of this property + const data_t& get() const + { + if (read_access_granted()) { + return _data; + } + throw uhd::access_error(std::string("Attempting to read property `") + get_id() + + "' without access privileges!"); + } + + operator const data_t&() const + { + return get(); + } + + bool operator==(const data_t& rhs) + { + return get() == rhs; + } + + property_t<data_t>& operator=(const data_t& value) + { + set(value); + return *this; + } + +private: + void mark_clean() + { + _data.mark_clean(); + } + + void forward(property_base_t* next_prop) + { + property_t<data_t>* prop_ptr = dynamic_cast<property_t<data_t>*>(next_prop); + if (prop_ptr == nullptr) { + throw uhd::type_error(std::string("Unable to cast property ") + + next_prop->get_id() + " to the same type as property " + + get_id()); + } + + prop_ptr->set(get()); + } + + bool is_type_equal(property_base_t* other_prop) const + { + return dynamic_cast<property_t<data_t>*>(other_prop) != nullptr; + } + + dirty_tracked<data_t> _data; +}; // class property_t + +}} /* namespace uhd::rfnoc */ + +#include <uhd/rfnoc/property.ipp> + +#endif /* INCLUDED_LIBUHD_PROPERTY_HPP */ + diff --git a/host/include/uhd/rfnoc/property.ipp b/host/include/uhd/rfnoc/property.ipp new file mode 100644 index 000000000..101aa5d7e --- /dev/null +++ b/host/include/uhd/rfnoc/property.ipp @@ -0,0 +1,25 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_PROPERTY_IPP +#define INCLUDED_LIBUHD_PROPERTY_IPP + +template <typename data_t> +uhd::rfnoc::property_t<data_t>::property_t(const std::string& id, data_t&& data, const uhd::rfnoc::res_source_info& source_info) + : uhd::rfnoc::property_base_t(id, source_info), _data(std::forward<data_t>(data)) +{ + // nop +} + +template <typename data_t> +uhd::rfnoc::property_t<data_t>::property_t(const std::string& id, const data_t& data, const uhd::rfnoc::res_source_info& source_info) + : uhd::rfnoc::property_base_t(id, source_info), _data(data) +{ + // nop +} + +#endif /* INCLUDED_LIBUHD_PROPERTY_IPP */ + diff --git a/host/include/uhd/rfnoc/res_source_info.hpp b/host/include/uhd/rfnoc/res_source_info.hpp new file mode 100644 index 000000000..37371009d --- /dev/null +++ b/host/include/uhd/rfnoc/res_source_info.hpp @@ -0,0 +1,89 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_RES_SRC_INFO_HPP +#define INCLUDED_LIBUHD_RES_SRC_INFO_HPP + +#include <uhd/exception.hpp> +#include <cstdint> +#include <string> + +namespace uhd { namespace rfnoc { + +/*! Describes the source of a particular resource (property or action) +*/ +struct res_source_info { + /*! Source type + */ + enum source_t { + USER, ///< The user API sources this resource + INPUT_EDGE, ///< An input edge sources this resource + OUTPUT_EDGE, ///< An input edge sources this resource + FRAMEWORK ///< This is a special resource, only accessed by the framework + }; + + // No default ctor: The source type must be specified + res_source_info() = delete; + + res_source_info(source_t source_type, size_t instance_ = 0) + : type(source_type), instance(instance_) + { + // nop + } + + //! The type of source (user or edge) + source_t type; + + //! The instance of the source. For resource that is sourced by a edge, it + // corresponds to the port number + size_t instance = 0; + + bool operator==(const res_source_info& rhs) const + { + return rhs.type == type && rhs.instance == instance; + } + + //! Returns a string representation of the source + std::string to_string() const + { + const std::string type_repr = type == USER ? "USER" : + type == INPUT_EDGE ? "INPUT_EDGE" : + type == OUTPUT_EDGE ? "OUTPUT_EDGE" : "INVALID"; + return type_repr + ":" + std::to_string(instance); + } + + /*! Convenience function to invert the type value if it's an edge. + * + * - Will assert that the edge is actually either INPUT_EDGE or OUTPUT_EDGE + * - Then, returns the opposite of what it was before + * + * \throws uhd::assertion_error if \p edge_direction is neither INPUT_EDGE + * nor OUTPUT_EDGE + */ + static source_t invert_edge(const source_t edge_direction) + { + UHD_ASSERT_THROW(edge_direction == INPUT_EDGE || edge_direction == OUTPUT_EDGE); + return edge_direction == INPUT_EDGE ? OUTPUT_EDGE : INPUT_EDGE; + } +}; + +}} /* namespace uhd::rfnoc */ + +namespace std { +template <> +struct hash<uhd::rfnoc::res_source_info> +{ + size_t operator()(const uhd::rfnoc::res_source_info& src_info) const + { + const size_t hash_type = std::hash<size_t>{}(src_info.type); + const size_t hash_inst = std::hash<size_t>{}(src_info.instance); + return hash_type ^ (hash_inst << 1); + } +}; +} // namespace std + +#endif /* INCLUDED_LIBUHD_RES_SRC_INFO_HPP */ + |