Skip to content

Commit

Permalink
Introducing message port handling to blocks and graphs
Browse files Browse the repository at this point in the history
- Added a default message port pair to gr::Block with
  example messages for setting properties, and lifetime control
  of blocks;
- Additional message port pair for gr::Graph to communicate
  with the blocks it owns -- used for message propagation into
  the graph and for exporting messages outward;
- Blocks handle the messages with (optionally) type-tagged
  ports -- when using named ports (or another way of port type
  differentiation, the processMessages handlers are chosen during
  compile-time and there is no need to check which port a message
  came on during runtime);
- Messages can be targeted to a specified unique_name. The '/'
  is used for separating unique_name of parent graph(s) and the block.
  '*' (or an empty target) is used to denote a message that is meant
  for all blocks;
- Since messages are arbitrary property maps, some convenience
  functions and definitions are provided;

TODO:
- Write more detailed tests
- Remove debugging output
- Check graph<->children connection issues
  • Loading branch information
ivan-cukic committed Jan 25, 2024
1 parent 174e462 commit d93c67a
Show file tree
Hide file tree
Showing 32 changed files with 1,144 additions and 335 deletions.
18 changes: 9 additions & 9 deletions algorithm/include/gnuradio-4.0/algorithm/filter/FilterTool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ calculateResponse(double frequency, const PoleZeroLocations &value) {
}
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
calculateFilterButterworth(std::size_t order) {
// Butterworth design criteria: https://en.wikipedia.org/wiki/Butterworth_filter
// place poles equally spaced along the lhs unit half-circle starting with
Expand All @@ -509,7 +509,7 @@ calculateFilterButterworth(std::size_t order) {
return ret;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
calculateFilterBessel(std::size_t order) {
// pole location data: Steve Winder, "Filter Design," Newnes Press, 1998.
using C = std::complex<double>;
Expand Down Expand Up @@ -539,7 +539,7 @@ calculateFilterBessel(std::size_t order) {
}
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
calculateFilterChebyshevType1(std::size_t order, double rippleDb = 0.1) {
// Cauer, W. "The realization of impedances of prescribed frequency dependence." Annalen der Physik 401.2 (1930): 157-229.
// see also: https://en.wikipedia.org/wiki/Chebyshev_filter
Expand Down Expand Up @@ -590,7 +590,7 @@ calculateFilterChebyshevType2(std::size_t numPoles, double stopBandDb = 40) {
return ret;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
analogToDigitalTransform(const PoleZeroLocations &analogPoleZeros, double samplingRate) {
// see: https://en.wikipedia.org/wiki/Bilinear_transform#Discrete-time_approximation
const double twoFs = 2. * samplingRate;
Expand Down Expand Up @@ -703,7 +703,7 @@ expandRootsToPolynomial(Range &&roots, std::size_t desiredOrder) {
return coefficients;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
lowPassProtoToLowPass(const PoleZeroLocations &lowPassProto, FilterParameters parameters) {
PoleZeroLocations lowPass = lowPassProto;

Expand All @@ -714,7 +714,7 @@ lowPassProtoToLowPass(const PoleZeroLocations &lowPassProto, FilterParameters pa
return lowPass;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
lowPassProtoToHighPass(const PoleZeroLocations &lowPassProto, FilterParameters parameters) {
PoleZeroLocations highPass = lowPassProto;

Expand All @@ -734,7 +734,7 @@ lowPassProtoToHighPass(const PoleZeroLocations &lowPassProto, FilterParameters p
return highPass;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
lowPassProtoToBandPass(const PoleZeroLocations &lowPassProto, FilterParameters parameters) {
constexpr double epsilon = 1e-10;
const double omega0 = 2. * std::numbers::pi * std::sqrt(parameters.fLow * parameters.fHigh); // centre frequency
Expand Down Expand Up @@ -793,7 +793,7 @@ lowPassProtoToBandPass(const PoleZeroLocations &lowPassProto, FilterParameters p
return bandPass;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
lowPassProtoToBandStop(const PoleZeroLocations &lowPassProto, FilterParameters parameters) {
constexpr double epsilon = 1e-10;
const double omega0 = 2. * std::numbers::pi * std::sqrt(parameters.fLow * parameters.fHigh); // centre frequency
Expand Down Expand Up @@ -850,7 +850,7 @@ lowPassProtoToBandStop(const PoleZeroLocations &lowPassProto, FilterParameters p
return bandStop;
}

[[nodiscard]] inline constexpr PoleZeroLocations
[[nodiscard]] inline PoleZeroLocations
designAnalogFilter(const Type filterType, FilterParameters params, const Design filterDesign = Design::BUTTERWORTH) {
// step 1: continuous-time analog prototype
PoleZeroLocations analogPoleZeros;
Expand Down
9 changes: 6 additions & 3 deletions blocks/basic/include/gnuradio-4.0/basic/common_blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class multi_adder : public gr::BlockModel {
protected:
using TPortIn = gr::PortIn<T>;
std::vector<TPortIn> _input_ports;
gr::PortOut<T> _output_port;
gr::PortOut<T> _output_port;

protected:
using setting_map = std::map<std::string, int, std::less<>>;
Expand Down Expand Up @@ -196,6 +196,9 @@ class multi_adder : public gr::BlockModel {
return { requested_work, available_samples, gr::work::Status::OK };
}

virtual void

Check notice on line 199 in blocks/basic/include/gnuradio-4.0/basic/common_blocks.hpp

View check run for this annotation

codefactor.io / CodeFactor

blocks/basic/include/gnuradio-4.0/basic/common_blocks.hpp#L199

"virtual" is redundant since function is already declared as "override" (readability/inheritance)
processScheduledMessages() override {}

void *
raw() override {
return this;
Expand Down Expand Up @@ -234,8 +237,8 @@ void
registerBuiltinBlocks(Registry *registry) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
GP_REGISTER_NODE_RUNTIME(registry, builtin_multiply, double, float);
GP_REGISTER_NODE_RUNTIME(registry, builtin_counter, double, float);
GP_REGISTER_BLOCK_RUNTIME(registry, builtin_multiply, double, float);
GP_REGISTER_BLOCK_RUNTIME(registry, builtin_counter, double, float);
#pragma GCC diagnostic pop
}

Expand Down
2 changes: 1 addition & 1 deletion blocks/basic/test/qa_Selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct repeated_source : public gr::Block<repeated_source<T>> {
}

if (remaining_events_count != 0) {
auto &port = gr::outputPort<0>(this);
auto &port = gr::outputPort<0, gr::traits::port::port_kind::Stream>(this);
auto &writer = port.streamWriter();
auto data = writer.reserve_output_range(1UZ);

Expand Down
28 changes: 14 additions & 14 deletions core/benchmarks/bm-nosonar_node_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ class gen_operation_SIMD : public gr::Block<gen_operation_SIMD<T, op>, gr::PortI

gr::work::Result
work(std::size_t requested_work) noexcept {
auto &out_port = outputPort<0>(this);
auto &in_port = inputPort<0>(this);
auto &out_port = outputPort<0, gr::traits::port::port_kind::Stream>(this);
auto &in_port = inputPort<0, gr::traits::port::port_kind::Stream>(this);

auto &reader = in_port.streamReader();
auto &writer = out_port.streamWriter();
Expand Down Expand Up @@ -281,8 +281,8 @@ class copy : public gr::Block<copy<T, N_MIN, N_MAX, use_bulk_operation, use_memc

gr::work::Result
work(std::size_t requested_work) noexcept { // TODO - make this an alternate version to 'processOne'
auto &out_port = out;
auto &in_port = in;
auto &out_port = out;
auto &in_port = in;

auto &reader = in_port.streamReader();
auto &writer = out_port.streamWriter();
Expand Down Expand Up @@ -344,8 +344,8 @@ class convert : public gr::Block<convert<From, To, N_MIN, N_MAX>, gr::PortInName
gr::work::Status
work() noexcept {
using namespace stdx;
auto &out_port = outputPort<"out">(this);
auto &in_port = inputPort<"in">(this);
auto &out_port = outputPort<"out">(this);
auto &in_port = inputPort<"in">(this);

auto &reader = in_port.streamReader();
auto &writer = out_port.streamWriter();
Expand All @@ -363,7 +363,7 @@ class convert : public gr::Block<convert<From, To, N_MIN, N_MAX>, gr::PortInName
const auto objects_to_write = stdx::is_simd_v<To> ? n_simd_to_convert : scalars_to_convert;
const auto objects_to_read = stdx::is_simd_v<From> ? n_simd_to_convert : scalars_to_convert;

auto return_value = gr::work::Status::OK;
auto return_value = gr::work::Status::OK;
writer.publish( //
[&](std::span<To> output) {
const auto input = reader.get();
Expand Down Expand Up @@ -518,7 +518,7 @@ inline const boost::ut::suite _runtime_tests = [] {
auto &src = testGraph.emplaceBlock<test::source<float>>(N_SAMPLES);
auto &sink = testGraph.emplaceBlock<test::sink<float>>();

using copy = ::copy<float, 1, N_MAX, true, true>;
using copy = ::copy<float, 1, N_MAX, true, true>;
std::vector<copy *> cpy(10);
for (std::size_t i = 0; i < cpy.size(); i++) {
cpy[i] = std::addressof(testGraph.emplaceBlock<copy>({ { "name", fmt::format("copy {} at {}", i, gr::this_source_location()) } }));
Expand Down Expand Up @@ -576,9 +576,9 @@ inline const boost::ut::suite _runtime_tests = [] {
templated_cascaded_test(static_cast<int>(2.0), "runtime src->mult(2.0)->div(2.0)->add(-1)->sink - int");

constexpr auto templated_cascaded_test_10 = []<typename T>(T factor, const char *test_name) {
gr::Graph testGraph;
auto &src = testGraph.emplaceBlock<test::source<T>>(N_SAMPLES);
auto &sink = testGraph.emplaceBlock<test::sink<T>>();
gr::Graph testGraph;
auto &src = testGraph.emplaceBlock<test::source<T>>(N_SAMPLES);
auto &sink = testGraph.emplaceBlock<test::sink<T>>();

std::vector<multiply<T> *> mult1;
std::vector<divide<T> *> div1;
Expand Down Expand Up @@ -643,9 +643,9 @@ inline const boost::ut::suite _simd_tests = [] {
}

{
gr::Graph testGraph;
auto &src = testGraph.emplaceBlock<test::source<float>>(N_SAMPLES);
auto &sink = testGraph.emplaceBlock<test::sink<float>>();
gr::Graph testGraph;
auto &src = testGraph.emplaceBlock<test::source<float>>(N_SAMPLES);
auto &sink = testGraph.emplaceBlock<test::sink<float>>();

std::vector<multiply_SIMD<float> *> mult1;
std::vector<multiply_SIMD<float> *> mult2;
Expand Down
Loading

0 comments on commit d93c67a

Please sign in to comment.