From f374d93ff1cdc470e0134220fac8737a9b914102 Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Wed, 10 Jul 2019 16:15:54 +0200 Subject: [PATCH] Basic pushing of data through inputs/outputs. --- app/playground/main.cpp | 8 +++++++- lib/pls/include/pls/dataflow/dataflow.h | 4 ++++ lib/pls/include/pls/dataflow/graph.h | 15 +++++++++++++++ lib/pls/include/pls/dataflow/internal/input.h | 36 +++++++++++++++++++++++------------- lib/pls/include/pls/dataflow/internal/inputs.h | 35 +++++++++++++++++++++++++++++++++-- lib/pls/include/pls/dataflow/internal/output.h | 10 +++++++--- lib/pls/include/pls/dataflow/internal/outputs.h | 2 +- lib/pls/include/pls/dataflow/internal/token.h | 4 ++++ 8 files changed, 94 insertions(+), 20 deletions(-) diff --git a/app/playground/main.cpp b/app/playground/main.cpp index e35d4a3..b370892 100644 --- a/app/playground/main.cpp +++ b/app/playground/main.cpp @@ -5,9 +5,15 @@ #include #include +#include int main() { using namespace pls::dataflow; - graph, outputs, 8> graph; + graph, outputs, 8> graph; + graph.output<0>().connect(graph.input<0>()); + graph.output<1>().connect(graph.input<1>()); + + graph.input<0>().push_token(pls::dataflow::internal::token()); + graph.input<1>().push_token(pls::dataflow::internal::token()); } diff --git a/lib/pls/include/pls/dataflow/dataflow.h b/lib/pls/include/pls/dataflow/dataflow.h index b9bc7f5..bc9ce86 100644 --- a/lib/pls/include/pls/dataflow/dataflow.h +++ b/lib/pls/include/pls/dataflow/dataflow.h @@ -2,4 +2,8 @@ #ifndef PLS_DATAFLOW_DATAFLOW_H_ #define PLS_DATAFLOW_DATAFLOW_H_ +#include "graph.h" +#include "inputs.h" +#include "outputs.h" + #endif //PLS_DATAFLOW_DATAFLOW_H_ diff --git a/lib/pls/include/pls/dataflow/graph.h b/lib/pls/include/pls/dataflow/graph.h index 1a05a57..46fab38 100644 --- a/lib/pls/include/pls/dataflow/graph.h +++ b/lib/pls/include/pls/dataflow/graph.h @@ -12,7 +12,22 @@ template class graph { using internal_inputs = typename I::template internal_inputs

; using internal_outputs = typename O::template internal_outputs

; + + public: + internal_inputs inputs_; + internal_outputs outputs_; + + template + decltype(outputs_.template get()) output() { + return outputs_.template get(); + } + + template + decltype(inputs_.template get()) input() { + return inputs_.template get(); + } }; + } } diff --git a/lib/pls/include/pls/dataflow/internal/input.h b/lib/pls/include/pls/dataflow/internal/input.h index fba2a54..889351f 100644 --- a/lib/pls/include/pls/dataflow/internal/input.h +++ b/lib/pls/include/pls/dataflow/internal/input.h @@ -2,23 +2,32 @@ #ifndef PLS_DATAFLOW_INTERNAL_INPUT_H_ #define PLS_DATAFLOW_INTERNAL_INPUT_H_ -#include "token.h" +#include #include "pls/internal/base/error_handling.h" +#include "token.h" + namespace pls { namespace dataflow { namespace internal { -template +class push_token_cb { + public: + virtual void token_pushed(int pos, token_color color) = 0; +}; + +template class input { - template + template friend class output; - token token_; - bool filled_; - bool connected_; + std::array, P> tokens_; + bool connected_{false}; + + push_token_cb *cb_{nullptr}; + int input_pos_{0}; void connect() { if (connected_) { @@ -27,15 +36,16 @@ class input { connected_ = true; } - input() : filled_{false}, connected_{false} {}; - - bool has_token(token_color color) const { - return filled_ && token_.color() == color; - } - const token &get_token() const { return token_; } - public: + void push_token(token token) { + tokens_[token.color().get_index(P)] = token; + cb_->token_pushed(input_pos_, token.color()); + } + void set_cb(push_token_cb *cb, int input_pos) { + cb_ = cb; + input_pos_ = input_pos; + } }; } diff --git a/lib/pls/include/pls/dataflow/internal/inputs.h b/lib/pls/include/pls/dataflow/internal/inputs.h index feece2e..5c2218b 100644 --- a/lib/pls/include/pls/dataflow/internal/inputs.h +++ b/lib/pls/include/pls/dataflow/internal/inputs.h @@ -3,6 +3,7 @@ #define PLS_DATAFLOW_INTERNAL_INPUTS_H_ #include +#include #include "input.h" @@ -11,15 +12,45 @@ namespace dataflow { namespace internal { template -class inputs { - using values_type = std::tuple...>; +class inputs : push_token_cb { + using values_type = std::tuple...>; values_type values_; + static constexpr unsigned int num_values = std::tuple_size::value; + + std::array, P> required_inputs_; public: + inputs() { + for (int i = 0; i < P; i++) { + required_inputs_[i] = num_values; + } + init_cb<0, I...>::call(this); + } + + void token_pushed(int /*pos*/, token_color color) override { + int current_required = --required_inputs_[color.get_index(P)]; + if (current_required == 0) { + std::cout << "All Inputs Avaliable" << std::endl; // TODO: Add proper callback in here + } + } + template typename std::tuple_element::type &get() { return std::get(values_); } + +// TODO: Change CB code using proper templating to save method calls during execution... + template + struct init_cb { + static void call(inputs *inputs) { } + }; + template + struct init_cb { + static void call(inputs *inputs) { + inputs->get().set_cb(inputs, POS); + init_cb::call(inputs); + } + }; }; } diff --git a/lib/pls/include/pls/dataflow/internal/output.h b/lib/pls/include/pls/dataflow/internal/output.h index eb19dce..f6f9812 100644 --- a/lib/pls/include/pls/dataflow/internal/output.h +++ b/lib/pls/include/pls/dataflow/internal/output.h @@ -11,15 +11,15 @@ namespace pls { namespace dataflow { namespace internal { -template +template class output { - input *target_; + input *target_; bool connected_; public: output() : target_{nullptr}, connected_{false} {}; - void connect(input &target) { + void connect(input &target) { if (connected_) { PLS_ERROR("Must only connect output once. Please disconnect it before reconnecting.") } @@ -28,6 +28,10 @@ class output { target_ = ⌖ connected_ = true; } + + void push_token(token token) { + target_->push_token(token); + } }; } diff --git a/lib/pls/include/pls/dataflow/internal/outputs.h b/lib/pls/include/pls/dataflow/internal/outputs.h index 1004aba..ca616ea 100644 --- a/lib/pls/include/pls/dataflow/internal/outputs.h +++ b/lib/pls/include/pls/dataflow/internal/outputs.h @@ -12,7 +12,7 @@ namespace internal { template class outputs { - using values_type = std::tuple...>; + using values_type = std::tuple...>; values_type values_; public: diff --git a/lib/pls/include/pls/dataflow/internal/token.h b/lib/pls/include/pls/dataflow/internal/token.h index b0c609a..a6243f6 100644 --- a/lib/pls/include/pls/dataflow/internal/token.h +++ b/lib/pls/include/pls/dataflow/internal/token.h @@ -13,6 +13,10 @@ namespace internal { */ struct token_color { unsigned int clock_; + + int get_index(int parallel_limit) const { + return clock_ % parallel_limit; + } }; template -- libgit2 0.26.0