Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ports to be defined as class member variables #28

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 67 additions & 34 deletions bench/bm_case1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,69 @@ namespace fg = fair::graph;
inline constexpr std::size_t N_ITER = 10;
inline constexpr std::size_t N_SAMPLES = gr::util::round_up(1'000'000, 1024);

template<typename T, int addend, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
class add
: public fg::node<add<T, addend, N_MIN, N_MAX>, fg::IN<T, "in", N_MIN, N_MAX>, fg::OUT<T, "out", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
public:
template<fair::meta::t_or_simd<T> V>
[[nodiscard]] constexpr V
process_one(V a) const noexcept {
return a + addend;
}
};

template<typename T, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
class multiply
: public fg::node<multiply<T, N_MIN, N_MAX>, fg::IN<T, "in", N_MIN, N_MAX>, fg::OUT<T, "out", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
//
// This defines a new node type that is parametrised only on the type
// (single typename template parameter)
//
// It defines its ports as member variables, so it needs to be
// enabled for reflection using the ENABLE_REFLECTION_FOR_TEMPLATE
// macro as can be seen immediately after the class is defined.
//
template<typename T>
class multiply : public fg::node<multiply<T>> {
T _factor = static_cast<T>(1.0f);

public:
fg::IN<T> in;
fg::OUT<T> out;

multiply() = delete;

explicit multiply(T factor, std::string name = fair::graph::this_source_location()) : _factor(
factor) { this->set_name(name); }
factor) { this->set_name(name); }

template<fair::meta::t_or_simd<T> V>
[[nodiscard]] constexpr V
process_one(V a) const noexcept {
return a * _factor;
}
};
ENABLE_REFLECTION_FOR_TEMPLATE(multiply, in, out);


//
// This defines a new node type that is parametrised on several
// template parameters.
//
// It defines its ports as member variables, so it needs to be
// enabled for reflection using the ENABLE_REFLECTION_FOR_TEMPLATE_FULL
// macro because it has several different template parameters.
//
template<typename T, int addend>
class add : public fg::node<add<T, addend>> {
public:
fg::IN<T> in;
fg::OUT<T> out;

template<typename T, char op, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
template<fair::meta::t_or_simd<T> V>
[[nodiscard]] constexpr V
process_one(V a) const noexcept {
return a + addend;
}
};
ENABLE_REFLECTION_FOR_TEMPLATE_FULL((typename T, int addend), (add<T, addend>), in, out);


//
// This defines a new node type that which doesn't define ports
// as member variables, but as template parameters to the fg::node
// base class template.
//
// It doesn't need to be enabled for reflection.
//
template<typename T, char op>
class gen_operation_SIMD
: public fg::node<gen_operation_SIMD<T, op, N_MIN, N_MAX>, fg::IN<T, "in", N_MIN, N_MAX>, fg::OUT<T, "out", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
: public fg::node<gen_operation_SIMD<T, op>, fg::IN<T, 0, N_MAX, "in">, fg::OUT<T, 0, N_MAX, "out">> {
T _value = static_cast<T>(1.0f);

public:
Expand Down Expand Up @@ -119,17 +150,20 @@ class gen_operation_SIMD
return fair::graph::work_return_t::OK;
}
};
// no reflection needed because ports are defined via the fg::node<...> base class

template<typename T, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
using multiply_SIMD = gen_operation_SIMD<T, '*', N_MIN, N_MAX>;
template<typename T>
using multiply_SIMD = gen_operation_SIMD<T, '*'>;

template<typename T, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
using add_SIMD = gen_operation_SIMD<T, '+', N_MIN, N_MAX>;
template<typename T>
using add_SIMD = gen_operation_SIMD<T, '+'>;

template<typename T, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX, bool use_bulk_operation = false, bool use_memcopy = true>
class copy
: public fg::node<copy<T, N_MIN, N_MAX, use_bulk_operation, use_memcopy>, fg::IN<T, "in", N_MIN, N_MAX>, fg::OUT<T, "out", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
class copy : public fg::node<copy<T, N_MIN, N_MAX, use_bulk_operation, use_memcopy>> {
public:
fg::IN<T, N_MIN, N_MAX> in;
fg::OUT<T, N_MIN, N_MAX> out;

template<fair::meta::t_or_simd<T> V>
[[nodiscard]] constexpr T
process_one(V a) const noexcept {
Expand Down Expand Up @@ -174,7 +208,7 @@ class copy
return fair::graph::work_return_t::OK;
}
};

ENABLE_REFLECTION_FOR_TEMPLATE_FULL((typename T, std::size_t N_MIN, std::size_t N_MAX, bool use_bulk_operation, bool use_memcopy), (copy<T, N_MIN, N_MAX, use_bulk_operation, use_memcopy>), in, out);

namespace detail {
template<typename T>
Expand All @@ -191,8 +225,8 @@ namespace detail {
namespace stdx = vir::stdx;

template<typename From, typename To, std::size_t N_MIN = 0 /* SIMD size */, std::size_t N_MAX = N_MAX>
class convert : public fg::node<convert<From, To, N_MIN, N_MAX>, fg::IN<From, "in", N_MIN, N_MAX>,
fg::OUT<To, "out", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
class convert : public fg::node<convert<From, To, N_MIN, N_MAX>, fg::IN<From, N_MIN, N_MAX, "in">,
fg::OUT<To, N_MIN, N_MAX, "out">> {
static_assert(stdx::is_simd_v<From> != stdx::is_simd_v<To>, "either input xor output must be SIMD capable");
constexpr static std::size_t from_simd_size = detail::simd_size<From>();
constexpr static std::size_t to_simd_size = detail::simd_size<To>();
Expand Down Expand Up @@ -302,9 +336,7 @@ inline const boost::ut::suite _constexpr_bm = [] {
}

{
auto merged_node = merge<"out", "in">(
merge<"out", "in">(test::source<float>(N_SAMPLES), test::cascade<10, copy<float>>(copy<float>())),
test::sink<float>());
auto merged_node = merge<"out", "in">(merge<"out", "in">(test::source<float>(N_SAMPLES), test::cascade<10, copy<float>>(copy<float>())), test::sink<float>());
"constexpr src->copy^10->sink"_benchmark.repeat<N_ITER>(N_SAMPLES) = [&merged_node]() {
test::n_samples_produced = 0LU;
test::n_samples_consumed = 0LU;
Expand Down Expand Up @@ -402,8 +434,8 @@ inline const boost::ut::suite _runtime_tests = [] {
auto &sink = flow_graph.make_node<test::sink<float>>();
auto &cpy = flow_graph.make_node<copy<float>>();

expect(eq(fg::connection_result_t::SUCCESS, connect<"out">(src).to<"in">(cpy)));
expect(eq(fg::connection_result_t::SUCCESS, connect<"out">(cpy).to<"in">(sink)));
expect(eq(fg::connection_result_t::SUCCESS, connect(src, &test::source<float>::out).to<"in">(cpy)));
expect(eq(fg::connection_result_t::SUCCESS, connect<"out">(cpy).to(sink, &test::sink<float>::in)));

"runtime src->copy->sink"_benchmark.repeat<N_ITER>(N_SAMPLES) = [&flow_graph]() {
test::n_samples_produced = 0LU;
Expand All @@ -421,15 +453,16 @@ inline const boost::ut::suite _runtime_tests = [] {
auto &src = flow_graph.make_node<test::source<float>>(N_SAMPLES);
auto &sink = flow_graph.make_node<test::sink<float>>();

std::vector<copy<float, 0, N_MAX, true, true>*> cpy(10);
using copy = ::copy<float, 0, N_MAX, true, true>;
std::vector<copy*> cpy(10);
for (std::size_t i = 0; i < cpy.size(); i++) {
cpy[i] = std::addressof(flow_graph.make_node<copy<float, 0, N_MAX, true, true>>());
cpy[i] = std::addressof(flow_graph.make_node<copy>());
cpy[i]->set_name(fmt::format("copy {} at {}", i, fair::graph::this_source_location()));

if (i == 0) {
expect(eq(fg::connection_result_t::SUCCESS, connect<"out">(src).to<"in">(*cpy[i])));
} else {
expect(eq(fg::connection_result_t::SUCCESS, connect<"out">(*cpy[i - 1]).to<"in">(*cpy[i])));
expect(eq(fg::connection_result_t::SUCCESS, connect(*cpy[i - 1], &copy::out).to(*cpy[i], &copy::in)));
}
}

Expand Down
30 changes: 19 additions & 11 deletions bench/bm_test_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
#define GRAPH_PROTOTYPE_BM_TEST_HELPER_HPP

#include <graph.hpp>
#include <merged_node.hpp>

inline constexpr std::size_t N_MAX = std::numeric_limits<std::size_t>::max();

namespace test {
namespace fg = fair::graph;
inline static std::size_t n_samples_produced = 0LU;

template<typename T, std::size_t min = 0, std::size_t count = N_MAX, bool use_bulk_operation = true>
class source : public fg::node<source<T, min, count>, fg::OUT<T, "out">, fg::limits<min, count>> {
std::size_t _n_samples_max;
namespace fg = fair::graph;
using namespace fair::literals;

inline static std::size_t n_samples_produced = 0_UZ;

template<typename T, std::size_t min = 0_UZ, std::size_t count = N_MAX, bool use_bulk_operation = true>
class source : public fg::node<source<T, min, count>> {
public:
std::size_t _n_samples_max;
fg::OUT<T> out;

source() = delete;

source(std::size_t n_samples) : _n_samples_max(n_samples) {}
Expand All @@ -34,7 +37,7 @@ class source : public fg::node<source<T, min, count>, fg::OUT<T, "out">, fg::lim

if constexpr (use_bulk_operation) {
std::size_t n_write = std::clamp(n_to_publish, 0UL, std::min(writer.available(), port.max_buffer_size()));
if (n_write == 0) {
if (n_write == 0_UZ) {
return fair::graph::work_return_t::INSUFFICIENT_INPUT_ITEMS;
}

Expand All @@ -47,7 +50,7 @@ class source : public fg::node<source<T, min, count>, fg::OUT<T, "out">, fg::lim
n_write);
} else {
auto [data, token] = writer.get(1);
if (data.size() == 0) {
if (data.size() == 0_UZ) {
return fair::graph::work_return_t::ERROR;
}
data[0] = process_one();
Expand All @@ -60,11 +63,13 @@ class source : public fg::node<source<T, min, count>, fg::OUT<T, "out">, fg::lim
}
};

inline static std::size_t n_samples_consumed = 0LU;
inline static std::size_t n_samples_consumed = 0_UZ;

template<typename T, std::size_t N_MIN = 0, std::size_t N_MAX = N_MAX>
class sink : public fg::node<sink<T, N_MIN, N_MAX>, fg::IN<T, "in", N_MIN, N_MAX>, fg::limits<N_MIN, N_MAX>> {
template<typename T, std::size_t N_MIN = 0_UZ, std::size_t N_MAX = N_MAX>
class sink : public fg::node<sink<T, N_MIN, N_MAX>> {
public:
fg::IN<T, N_MIN, N_MAX> in;

template<fair::meta::t_or_simd<T> V>
[[nodiscard]] constexpr auto
process_one(V a) const noexcept {
Expand All @@ -86,4 +91,7 @@ cascade(

} // namespace test

ENABLE_REFLECTION_FOR_TEMPLATE_FULL((typename T, std::size_t min, std::size_t count, bool use_bulk_operation), (test::source<T, min, count, use_bulk_operation>), out);
ENABLE_REFLECTION_FOR_TEMPLATE_FULL((typename T, std::size_t N_MIN, std::size_t N_MAX), (test::sink<T, N_MIN, N_MAX>), in);

#endif // GRAPH_PROTOTYPE_BM_TEST_HELPER_HPP
Loading