Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Dec 17, 2024
1 parent 9e1b6ed commit 165c235
Show file tree
Hide file tree
Showing 28 changed files with 257 additions and 313 deletions.
1 change: 0 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ add_subdirectory(ports)
add_subdirectory(hello)
add_subdirectory(power_train)
add_subdirectory(multiport_mutation)
add_subdirectory(unit_tests_mutations)
6 changes: 3 additions & 3 deletions examples/count/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using namespace reactor;
using namespace std::chrono_literals;

class Count : public Reactor {
class Count final : public Reactor {
private:
// actions
Timer timer{"timer", this};
LogicalAction<int> counter{"counter", this};

// reactions_
Reaction r_init{"r_init", 1, false, this, [this]() { this->init(); }};
Reaction r_counter{"r_counter", 2, false, this, [this]() { this->print_count(); }};
Reaction r_init{"r_init", 1, this, [this]() { this->init(); }};
Reaction r_counter{"r_counter", 2, this, [this]() { this->print_count(); }};

public:
explicit Count(Environment* env)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace reactor;
using namespace std::chrono_literals;

class Hello : public Reactor {
class Hello final : public Reactor {
private:
// actions
Timer timer{"timer", this, 1s, 2s};
Expand Down
49 changes: 21 additions & 28 deletions examples/multiport_mutation/consumer.hh
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
//
// Created by tanneberger on 11/17/24.
//

#ifndef CONSUMER_HH
#define CONSUMER_HH
#ifndef CONSUMER_HH //NOLINT
#define CONSUMER_HH //NOLINT

#include <reactor-cpp/reactor-cpp.hh>

using namespace reactor;
using namespace std::chrono_literals;

class Consumer : public Reactor {
private:
class Inner: public Scope {
Inner(Reactor* reactor, std::size_t index) : Scope(reactor), index_(index) {}
std::size_t index_;

[[maybe_unused]] const Inner& __lf_inner = *this;
class Consumer final: public Reactor { // NOLINT
class Inner : public Scope {
Inner(Reactor* reactor, std::size_t index)
: Scope(reactor)
, index_(index) {}
std::size_t index_ = 0;

void reaction_1([[maybe_unused]] const Input<unsigned>& in) {
std::cout << "consumer: " << index_ << " received value:" << *in.get() << std::endl;
void reaction_1(const Input<unsigned>& in) const {
std::cout << "consumer: " << index_ << " received value:" << *in.get() << '\n';
}

friend Consumer;
};

Inner __lf_inner;
Reaction handle{"handle", 1, this, [this]() { __lf_inner.reaction_1(this->in); }};
Inner _lf_inner;
Reaction handle{"handle", 1, this, [this]() { _lf_inner.reaction_1(this->in); }};

public:
Consumer(const std::string& name, Environment* env, std::size_t index) : Reactor(name, env), __lf_inner(this, index) {
std::cout << "creating instance of consumer" << std::endl;
Consumer(const std::string& name, Environment* env, std::size_t index)
: Reactor(name, env)
, _lf_inner(this, index) {
std::cout << "creating instance of consumer" << '\n';
}
~Consumer() override {
std::cout << "Consumer Object is deleted" << std::endl;
};
~Consumer() override { std::cout << "Consumer Object is deleted" << '\n'; };

Input<unsigned> in{"in", this};
Input<unsigned> in{"in", this}; //NOLINT

void assemble() override {
handle.declare_trigger(&in);
}
void assemble() override { handle.declare_trigger(&in); }
};


#endif //CONSUMER_HH
#endif // CONSUMER_HH
65 changes: 33 additions & 32 deletions examples/multiport_mutation/load_balancer.hh
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//
// Created by tanneberger on 11/17/24.
//

#ifndef LOAD_BALANCER_HH
#define LOAD_BALANCER_HH
#ifndef LOAD_BALANCER_HH //NOLINT
#define LOAD_BALANCER_HH //NOLINT

#include <reactor-cpp/reactor-cpp.hh>

Expand All @@ -13,29 +9,31 @@
using namespace reactor;
using namespace std::chrono_literals;

class LoadBalancer : public Reactor {
private:
class Inner: public MutableScope {
Inner(Reactor* reactor) : MutableScope(reactor) {}
[[maybe_unused]] const Inner& __lf_inner = *this;
class LoadBalancer final: public Reactor {
class Inner : public MutableScope {
explicit Inner(Reactor* reactor)
: MutableScope(reactor) {}

// reaction bodies
void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action, Multiport<Output<unsigned>>& outbound) {
if (rand() % 30 == 0) {
scale_action.schedule(rand() % 20 + 1);
static void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action,
Multiport<Output<unsigned>>& outbound) {
if (std::rand() % 30 == 0) { //NOLINT
scale_action.schedule(std::rand() % 20 + 1); //NOLINT
}
unsigned sel = rand() % outbound.size();
std::cout << "Sending out to:" << sel << std::endl;
const unsigned sel = std::rand() % outbound.size(); //NOLINT
std::cout << "Sending out to:" << sel << '\n';
outbound[sel].set(inbound.get());
}

void reaction_2(ModifableMultiport<Output<unsigned>>&outbound, [[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
void reaction_2(ModifableMultiport<Output<unsigned>>& outbound,
[[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
ModifableMultiport<Output<unsigned>>* temp = &outbound;
std::size_t new_size = *scale.get();

auto antideps = (outbound[0]).anti_dependencies();

auto change_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
const auto change_size =
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);

add_to_transaction(change_size);

Expand All @@ -47,37 +45,40 @@ private:
friend LoadBalancer;
};

Inner __lf_inner;
Reaction process{"process", 2, this, [this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }};
Reaction scale{"scale", 1, this, [this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};
Inner _lf_inner;
Reaction process{"process", 2, this,
[this]() { Inner::reaction_1(this->inbound, this->scale_action, this->out); }};
Reaction scale{"scale", 1, this,
[this]() { _lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};

public:
LoadBalancer(const std::string& name, Environment* env)
: Reactor(name, env), __lf_inner(this) {
std::cout << "creating instance of load balancer" << std::endl;
: Reactor(name, env)
, _lf_inner(this) {
std::cout << "creating instance of load balancer" << '\n';
out.reserve(4);
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
out.emplace_back(_lf_port_name, this);
}
}
~LoadBalancer() override = default;


LogicalAction<unsigned> scale_action{"scale", this, 1us};
ModifableMultiport<Output<unsigned>> out{"out"};
LogicalAction<unsigned> scale_action{"scale", this, 1us}; //NOLINT
ModifableMultiport<Output<unsigned>> out{"out"}; //NOLINT
Input<unsigned> inbound{"inbound", this}; // NOLINT
Output<unsigned> scale_bank{"scale_bank", this};
Output<unsigned> scale_bank{"scale_bank", this}; //NOLINT

void assemble() override {
std::cout << "assemble LoadBalancer\n";
for (auto& __lf_port : out) {
process.declare_antidependency(&__lf_port);
for (auto& _lf_port : out) {
process.declare_antidependency(&_lf_port);
}
process.declare_trigger(&inbound);
scale.declare_trigger(&scale_action);
scale.declare_antidependency(&scale_bank);
}
};



#endif //LOAD_BALANCER_HH
#endif // LOAD_BALANCER_HH
72 changes: 34 additions & 38 deletions examples/multiport_mutation/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,88 @@
#include <reactor-cpp/mutations/bank.hh>
#include <reactor-cpp/mutations/connection.hh>

#include "../../lib/mutation/bank.cc"
#include "../../lib/mutation/connection.cc"
#include "./consumer.hh"
#include "./load_balancer.hh"
#include "./producer.hh"
#include "../../lib/mutation/bank.cc"
#include "../../lib/mutation/connection.cc"
#include <reactor-cpp/reactor-cpp.hh>

class Deployment : public Reactor {
class Deployment final : public Reactor { //NOLINT
std::unique_ptr<Producer> producer_;
std::unique_ptr<LoadBalancer> load_balancer_;
std::vector<std::unique_ptr<Consumer>> consumers_;

Reaction scale_bank{"scale_bank", 1, this, [this](){this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);}};
Reaction scale_bank{"scale_bank", 1, this,
[this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }};

public:

class Inner: public MutableScope {
class Inner : public MutableScope {
int state = 0;
[[maybe_unused]] const Inner& __lf_inner = *this;
public:

Inner(Reactor* reactor) : MutableScope(reactor) {}
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank, ModifableMultiport<Output<unsigned>>& load_balancer) {
public:
Inner(Reactor* reactor)
: MutableScope(reactor) {}
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank,
ModifableMultiport<Output<unsigned>>& load_balancer) {
std::size_t new_size = *scale.get();
std::size_t old_size = reactor_bank.size();

std::function<std::unique_ptr<Consumer>(Reactor*, std::size_t)> lambda = [](Reactor* reactor, std::size_t index) {
std::string __lf_inst_name = "consumer_" + std::to_string(index);
return std::make_unique<Consumer>(__lf_inst_name, reactor->environment(), index);
std::function lambda = [](Reactor* reactor, std::size_t index) {
std::string _lf_inst_name = "consumer_" + std::to_string(index);
return std::make_unique<Consumer>(_lf_inst_name, reactor->environment(), index);
};
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(&reactor_bank, this->reactor_, new_size, lambda);

auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(
&reactor_bank, this->reactor_, new_size, lambda);

add_to_transaction(change_size);

// old topology
commit_transaction();
// new topology

if (old_size > new_size) {

for (auto i = 0; i < old_size - new_size; i++) {
}
} else {
std::cout << "load_balancer size:" << load_balancer.size() << " bank size: " << reactor_bank.size() << std::endl;
if (old_size < new_size) {
for (auto i = 0; i < new_size; i++) {
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
add_to_transaction(add_conn);
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(
&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
add_to_transaction(add_conn);
}
commit_transaction(true);
}

std::cout << "new bank size:" << reactor_bank.size() << std::endl;
commit_transaction(true);
}

friend LoadBalancer;
};

Inner __inner;
Inner _inner;

Deployment(const std::string& name, Environment* env) : Reactor(name, env), __inner(this),
producer_(std::make_unique<Producer>("producer", environment())),
load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
std::cout << "creating instance of deployment" << std::endl;
Deployment(const std::string& name, Environment* env)
: Reactor(name, env)
, _inner(this)
, producer_(std::make_unique<Producer>("producer", environment()))
, load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
std::cout << "creating instance of deployment" << '\n';
consumers_.reserve(4);
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
std::string __lf_inst_name = "consumer_" + std::to_string(__lf_idx);
consumers_.push_back(std::make_unique<Consumer>(__lf_inst_name, environment(), __lf_idx));
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx);
consumers_.push_back(std::make_unique<Consumer>(_lf_inst_name, environment(), _lf_idx));
}
}
~Deployment() override = default;

Input<unsigned> scale{"scale", this};

void assemble() override {
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
environment()->draw_connection(load_balancer_->out[__lf_idx], consumers_[__lf_idx]->in, ConnectionProperties{});
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{});
environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{});
}
environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{});
scale_bank.declare_trigger(&this->scale);
}
};


auto main() -> int {
//srand(time(nullptr));
Environment env{4, true};
auto deployment = std::make_unique<Deployment>("c1", &env);
env.optimize();
Expand Down
Loading

0 comments on commit 165c235

Please sign in to comment.