Skip to content

Commit 883320f

Browse files
committed
changing scheduler so mutations are executed sequentially
1 parent 0b2eb68 commit 883320f

29 files changed

+284
-149
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ endif()
99

1010
project(${LIB_TARGET} LANGUAGES CXX VERSION 0.0.1)
1111

12-
# require C++17
13-
set(CMAKE_CXX_STANDARD 17)
12+
# require C++20
13+
set(CMAKE_CXX_STANDARD 20)
1414
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1515
set(CMAKE_CXX_EXTENSIONS OFF)
1616

examples/CMakeLists.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
include_directories(
2-
"${PROJECT_SOURCE_DIR}/include"
3-
)
1+
include_directories("${PROJECT_SOURCE_DIR}/include")
42

5-
add_custom_target(examples)
6-
add_subdirectory(count)
7-
add_subdirectory(ports)
8-
add_subdirectory(hello)
9-
add_subdirectory(power_train)
10-
add_subdirectory(multiport_mutation)
3+
add_custom_target(examples) add_subdirectory(count) add_subdirectory(ports) add_subdirectory(hello)
4+
add_subdirectory(power_train) add_subdirectory(multiport_mutation)

examples/count/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
add_executable(count EXCLUDE_FROM_ALL main.cc)
2-
target_link_libraries(count reactor-cpp)
3-
add_dependencies(examples count)
1+
add_executable(count EXCLUDE_FROM_ALL main.cc) target_link_libraries(count reactor - cpp)
2+
add_dependencies(examples count)

examples/hello/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
add_executable(hello EXCLUDE_FROM_ALL main.cc)
2-
target_link_libraries(hello reactor-cpp)
3-
add_dependencies(examples hello)
1+
add_executable(hello EXCLUDE_FROM_ALL main.cc) target_link_libraries(hello reactor - cpp)
2+
add_dependencies(examples hello)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
add_executable(mutation_multiports main.cc)
2-
target_link_libraries(mutation_multiports reactor-cpp)
3-
add_dependencies(examples mutation_multiports)
1+
add_executable(mutation_multiports main.cc) target_link_libraries(mutation_multiports reactor - cpp)
2+
add_dependencies(examples mutation_multiports)

examples/multiport_mutation/consumer.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ using namespace std::chrono_literals;
1717

1818
class Consumer final : public Reactor { // NOLINT
1919
class Inner : public Scope {
20-
Inner(Reactor* reactor, std::size_t index)
21-
: Scope(reactor)
20+
Inner(Reaction* reaction, std::size_t index)
21+
: Scope(reaction)
2222
, index_(index) {}
2323
std::size_t index_ = 0;
2424

@@ -30,12 +30,12 @@ class Consumer final : public Reactor { // NOLINT
3030
};
3131

3232
Inner _lf_inner;
33-
Reaction handle{"handle", 1, this, [this]() { _lf_inner.reaction_1(this->in); }};
33+
Reaction handle{"handle", 1, false, this, [this]() { _lf_inner.reaction_1(this->in); }};
3434

3535
public:
3636
Consumer(const std::string& name, Environment* env, std::size_t index)
3737
: Reactor(name, env)
38-
, _lf_inner(this, index) {
38+
, _lf_inner(&handle, index) {
3939
std::cout << "creating instance of consumer" << '\n';
4040
}
4141
~Consumer() override { std::cout << "Consumer Object is deleted" << '\n'; };

examples/multiport_mutation/load_balancer.hh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,34 @@ using namespace std::chrono_literals;
1717

1818
class LoadBalancer final : public Reactor { // NOLINT
1919
class Inner : public MutableScope {
20-
explicit Inner(Reactor* reactor)
21-
: MutableScope(reactor) {}
20+
explicit Inner(Reaction* reaction)
21+
: MutableScope(reaction) {}
2222

2323
// reaction bodies
2424
static void reaction_1(const Input<unsigned>& inbound, Output<unsigned>& scale_bank,
2525
Multiport<Output<unsigned>>& outbound) {
26-
if (std::rand() % 15 == 0) { // NOLINT
26+
if (std::rand() % 15 == 0) { // NOLINT
27+
28+
std::cout << "triggering mutation" << std::endl;
2729
scale_bank.set(std::rand() % 20 + 1); // NOLINT
2830
}
2931
const unsigned outbound_port = std::rand() % outbound.size(); // NOLINT
32+
33+
std::cout << "forwarding to: " << outbound_port << std::endl;
3034
outbound[outbound_port].set(inbound.get());
3135
}
3236

3337
friend LoadBalancer;
3438
};
3539

3640
Inner _lf_inner;
37-
Reaction process{"process", 1, this, [this]() { Inner::reaction_1(this->inbound, this->scale_bank, this->out); }};
41+
Reaction process{"process", 1, false, this,
42+
[this]() { Inner::reaction_1(this->inbound, this->scale_bank, this->out); }};
3843

3944
public:
4045
LoadBalancer(const std::string& name, Environment* env)
4146
: Reactor(name, env)
42-
, _lf_inner(this) {
47+
, _lf_inner(&process) {
4348
out.reserve(4);
4449
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
4550
out.create_new_port();

examples/multiport_mutation/main.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ class Deployment final : public Reactor { // NOLINT
2222
std::unique_ptr<LoadBalancer> load_balancer_;
2323
std::vector<std::unique_ptr<Consumer>> consumers_;
2424

25-
Reaction scale_bank{"scale_bank", 1, this,
26-
[this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }};
25+
Reaction scale_bank{"scale_bank", 1, true, this, [this]() {
26+
this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);
27+
//
28+
}};
2729

30+
class InnerNonMutable : public Scope {};
2831
class Inner : public MutableScope {
2932
int state = 0;
3033

3134
public:
32-
explicit Inner(Reactor* reactor)
33-
: MutableScope(reactor) {}
35+
explicit Inner(Reaction* reaction)
36+
: MutableScope(reaction) {}
3437
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank,
3538
ModifableMultiport<Output<unsigned>>& load_balancer) {
39+
std::cout << "mutation reaction is being executed" << std::endl;
3640
std::size_t new_size = *scale.get();
3741

3842
std::function lambda = [](Environment* env, std::size_t index) {
@@ -41,11 +45,11 @@ class Deployment final : public Reactor { // NOLINT
4145
};
4246

4347
std::function get_input_port = [](const std::unique_ptr<Consumer>& consumer) { return &consumer->in; };
48+
4449
const auto rescale = std::make_shared<ResizeMultiportToBank<unsigned, Consumer>>(
45-
&load_balancer, &reactor_bank, get_input_port, lambda, new_size);
50+
reaction_, &load_balancer, &reactor_bank, get_input_port, lambda, new_size);
4651

4752
add_to_transaction(rescale);
48-
4953
commit_transaction(true);
5054
}
5155

@@ -57,7 +61,7 @@ class Deployment final : public Reactor { // NOLINT
5761
public:
5862
Deployment(const std::string& name, Environment* env)
5963
: Reactor(name, env)
60-
, _inner(this)
64+
, _inner(&scale_bank)
6165
, producer_(std::make_unique<Producer>("producer", environment()))
6266
, load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
6367
std::cout << "creating instance of deployment" << '\n';

examples/multiport_mutation/multiport_to_bank.hh

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,57 @@ template <class PortType, class ReactorType> class ResizeMultiportToBank : publi
3333
std::size_t new_size_ = 0;
3434

3535
public:
36-
ResizeMultiportToBank(ModifableMultiport<Output<PortType>>* multiport,
36+
ResizeMultiportToBank(Reaction* reaction, ModifableMultiport<Output<PortType>>* multiport,
3737
std::vector<std::unique_ptr<ReactorType>>* bank,
3838
std::function<Input<PortType>*(const std::unique_ptr<ReactorType>&)> get_input_port,
3939
std::function<std::unique_ptr<ReactorType>(Environment* env, std::size_t index)> create_lambda,
4040
std::size_t new_size)
41-
: multiport_(multiport)
41+
: Mutation(reaction)
42+
, multiport_(multiport)
4243
, bank_(bank)
4344
, get_input_port_(get_input_port)
4445
, create_lambda_(create_lambda)
4546
, new_size_(new_size) {}
4647

4748
~ResizeMultiportToBank() = default;
48-
auto run() -> MutationResult {
49+
auto run() -> MutationResult override {
4950
if (multiport_->size() != bank_->size()) {
5051
return NotMatchingBankSize;
5152
}
5253
auto old_size = multiport_->size();
5354

5455
if (new_size_ > old_size) {
55-
auto change_multiport_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_);
56+
auto change_multiport_size =
57+
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(reaction_, multiport_, new_size_);
5658

5759
change_multiport_size->run();
5860

5961
auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>(
60-
bank_, (*bank_)[0]->environment(), new_size_, create_lambda_);
62+
reaction_, bank_, new_size_, create_lambda_);
6163

6264
change_bank_size->run();
6365

6466
for (auto i = old_size; i < new_size_; i++) {
6567
auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>(
66-
&(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), true);
68+
reaction_, &(*multiport_)[i], get_input_port_((*bank_)[i]), true);
6769

6870
add_conn->run();
6971
}
7072
} else if (new_size_ < old_size) {
7173
for (auto i = old_size - 1; i >= new_size_; i--) {
7274
auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>(
73-
&(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), false);
75+
reaction_, &(*multiport_)[i], get_input_port_((*bank_)[i]), false);
7476

7577
add_conn->run();
7678
}
7779

78-
auto change_multiport_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_);
80+
auto change_multiport_size =
81+
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(reaction_, multiport_, new_size_);
7982

8083
change_multiport_size->run();
8184

8285
auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>(
83-
bank_, (*bank_)[0]->environment(), new_size_, create_lambda_);
86+
reaction_, bank_, new_size_, create_lambda_);
8487

8588
change_bank_size->run();
8689
}

examples/multiport_mutation/producer.hh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ using namespace std::chrono_literals;
1717
class Producer final : public Reactor { // NOLINT
1818
private:
1919
Timer timer{"timer", this, 1s, 1s};
20-
Reaction r_timer{"r_timer", 1, this, [this]() { _lf_inner.reaction_1(this->value); }};
20+
Reaction r_timer{"r_timer", 1, false, this, [this]() { _lf_inner.reaction_1(this->value); }};
2121

2222
class Inner : public Scope {
2323
unsigned int counter_ = 0;
2424

25-
void reaction_1([[maybe_unused]] Output<unsigned>& out) {
26-
// std::cout << "producing value:" << counter_ << "\n";
27-
out.set(counter_++);
28-
}
25+
void reaction_1([[maybe_unused]] Output<unsigned>& out) { out.set(counter_++); }
2926

30-
explicit Inner(Reactor* reactor)
31-
: Scope(reactor) {}
27+
explicit Inner(Reaction* reaction)
28+
: Scope(reaction) {}
3229

3330
friend Producer;
3431
};
@@ -38,7 +35,7 @@ private:
3835
public:
3936
Producer(const std::string& name, Environment* env)
4037
: Reactor(name, env)
41-
, _lf_inner(this) {
38+
, _lf_inner(&r_timer) {
4239
std::cout << "creating instance of producer\n";
4340
}
4441
Producer() = delete;

0 commit comments

Comments
 (0)