Skip to content

Commit 044d291

Browse files
committed
fixed ordering problem in assemble
1 parent 30158d4 commit 044d291

File tree

5 files changed

+51
-64
lines changed

5 files changed

+51
-64
lines changed

include/reactor-cpp/environment.hh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ public:
9292

9393
void optimize();
9494
void expand_and_merge();
95-
void strip_and_optimize();
9695

9796
void register_reactor(Reactor* reactor);
98-
void register_port(BasePort* port) noexcept;
9997
void register_input_action(BaseAction* action);
10098
void assemble();
10199
auto startup() -> std::thread;

include/reactor-cpp/graph.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ public:
164164
}
165165
}
166166

167-
auto to_mermaid() const noexcept -> std::string {
167+
[[nodiscard]] auto to_mermaid() const noexcept -> std::string {
168168
std::size_t index{0};
169169
std::map<E, std::string> name_map{};
170170
std::string mermaid_string = "graph TD;\n";
171171

172172
auto name_resolver = [&](E object) -> std::string {
173-
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz";
173+
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz"; //NOLINT
174174
if (name_map.find(object) == std::end(name_map)) {
175175
name_map[object] = names[index];
176176
index++;

include/reactor-cpp/port.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public:
9393
[[nodiscard]] inline auto has_dependencies() const noexcept -> bool { return !dependencies_.empty(); }
9494
[[nodiscard]] inline auto has_anti_dependencies() const noexcept -> bool { return !anti_dependencies_.empty(); }
9595
[[nodiscard]] inline auto has_triggers() const noexcept -> bool { return !triggers_.empty(); }
96+
[[nodiscard]] inline auto rating() const noexcept -> std::size_t { return triggers_.size() + dependencies_.size(); }
9697

9798
[[nodiscard]] inline auto inward_binding() const noexcept -> BasePort* { return inward_binding_; }
9899
[[nodiscard]] inline auto outward_bindings() const noexcept -> const auto& { return outward_bindings_; }

lib/environment.cc

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ void Environment::register_input_action(BaseAction* action) {
6363
}
6464

6565
void Environment::optimize() {
66-
#ifdef GRAPH_OPTIMIZATIONS
66+
#if GRAPH_OPTIMIZATIONS
6767
constexpr bool enable_optimizations = true;
6868
#else
6969
constexpr bool enable_optimizations = false;
7070
#endif
71+
log::Debug() << "Opimizations:" << enable_optimizations;
7172
if constexpr (enable_optimizations) {
73+
log::Debug() << graph_.to_mermaid();
7274
expand_and_merge();
73-
strip_and_optimize();
75+
log::Debug() << optimized_graph_.to_mermaid();
7476
} else {
7577
// no optimizations
7678
optimized_graph_ = graph_;
@@ -149,70 +151,53 @@ void Environment::expand_and_merge() {
149151
auto spanning_tree = graph_.naive_spanning_tree(source);
150152
for (auto& path : spanning_tree) {
151153
ConnectionProperties merged_properties{};
152-
auto* current_source = source;
153154

154-
for (auto element : path) {
155-
auto property = element.first;
155+
std::reverse(path.begin(), path.end());
156156

157-
auto return_type =
158-
construction_table[std::pair<ConnectionType, ConnectionType>(merged_properties.type_, property.type_)];
157+
auto* previous_element = std::begin(path)->second;
158+
for (auto it = std::begin(path); it != std::end(path); ++it) {
159+
if (std::next(it) == std::end(path)) {
160+
it->second = source;
161+
} else {
162+
it->second = std::next(it)->second;
163+
}
164+
}
159165

160-
// invalid will split the connections
161-
if (return_type == Invalid) {
162-
// first add connection until this point
163-
optimized_graph_.add_edge(current_source, element.second, merged_properties);
166+
auto current_rating = previous_element->rating();
164167

165-
// updating the source of the connection and resetting the properties
166-
current_source = element.second;
167-
merged_properties = property;
168+
for (auto element : path) {
169+
auto property = element.first;
170+
current_rating += element.second->rating();
168171

169-
} else {
170-
// merging the connections
171-
merged_properties.type_ = return_type;
172+
if (current_rating > 0) {
173+
auto return_type =
174+
construction_table[std::pair<ConnectionType, ConnectionType>(merged_properties.type_, property.type_)];
175+
// invalid will split the connections
176+
if (return_type == Invalid) {
177+
// first add connection until this point
178+
optimized_graph_.add_edge(element.second, previous_element, merged_properties);
172179

173-
// adding up delays
174-
merged_properties.delay_ += property.delay_;
180+
// updating the source of the connection and resetting the properties
181+
previous_element = element.second;
182+
merged_properties = property;
175183

176-
// updating target enclave if not nullptr
177-
merged_properties.enclave_ = (property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;
184+
} else {
185+
// merging the connections
186+
merged_properties.type_ = return_type;
178187

179-
optimized_graph_.add_edge(current_source, element.second, merged_properties);
180-
}
181-
}
182-
}
183-
}
184-
}
188+
// adding up delays
189+
merged_properties.delay_ += property.delay_;
185190

186-
void Environment::strip_and_optimize() {
187-
Graph<BasePort*, ConnectionProperties> striped_graph{};
188-
189-
auto nodes = optimized_graph_.get_nodes();
190-
std::vector<BasePort*> has_downstream_reactions{};
191-
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_downstream_reactions),
192-
[](BasePort* port) { return port->has_anti_dependencies(); });
193-
194-
std::vector<BasePort*> has_triggers{};
195-
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_triggers),
196-
[](BasePort* port) { return port->has_dependencies(); });
197-
198-
for (auto downstream : has_downstream_reactions) {
199-
for (auto upstream : has_triggers) {
200-
if (upstream != downstream) {
201-
auto optional_path = optimized_graph_.shortest_path(downstream, upstream);
202-
203-
if (optional_path.has_value()) {
204-
auto best_path = optional_path.value();
205-
auto source = downstream;
206-
for (auto hop : best_path) {
207-
striped_graph.add_edge(source, hop.second, hop.first);
208-
source = hop.second;
191+
// updating target enclave if not nullptr
192+
merged_properties.enclave_ =
193+
(property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;
194+
195+
optimized_graph_.add_edge(element.second, previous_element, merged_properties);
209196
}
210197
}
211198
}
212199
}
213200
}
214-
215-
optimized_graph_ = striped_graph;
216201
}
217202

218203
void recursive_assemble(Reactor* container) { // NOLINT
@@ -233,11 +218,17 @@ void Environment::assemble() { // NOLINT
233218
recursive_assemble(reactor);
234219
}
235220

236-
log::Debug() << "start optimization on port graph";
237-
this->optimize();
221+
// this assembles all the contained environments aka enclaves
222+
for (auto* env : contained_environments_) {
223+
env->assemble();
224+
}
238225

239-
log::Debug() << "instantiating port graph declaration";
240226
if (top_environment_ == nullptr || top_environment_ == this) {
227+
log::Debug() << "start optimization on port graph";
228+
this->optimize();
229+
230+
log::Debug() << "instantiating port graph declaration";
231+
241232
log::Debug() << "graph: ";
242233
log::Debug() << optimized_graph_;
243234

@@ -291,11 +282,6 @@ void Environment::assemble() { // NOLINT
291282
}
292283

293284
calculate_indexes();
294-
295-
// this assembles all the contained environments aka enclaves
296-
for (auto* env : contained_environments_) {
297-
env->assemble();
298-
}
299285
}
300286

301287
void Environment::build_dependency_graph(Reactor* reactor) { // NOLINT

lib/port.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ void BasePort::register_dependency(Reaction* reaction, bool is_trigger) noexcept
3030
"Dependent output ports must belong to a contained reactor");
3131
}
3232

33+
log::Debug() << "registering dependency for : " << this->fqn() << " with reaction: " << reaction->fqn()
34+
<< " is trigger: " << is_trigger;
3335
[[maybe_unused]] bool result = dependencies_.insert(reaction).second;
3436
reactor_assert(result);
3537
if (is_trigger) {

0 commit comments

Comments
 (0)