Skip to content

Commit

Permalink
fixed ordering problem in assemble
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Aug 21, 2023
1 parent 30158d4 commit 044d291
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 64 deletions.
2 changes: 0 additions & 2 deletions include/reactor-cpp/environment.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ public:

void optimize();
void expand_and_merge();
void strip_and_optimize();

void register_reactor(Reactor* reactor);
void register_port(BasePort* port) noexcept;
void register_input_action(BaseAction* action);
void assemble();
auto startup() -> std::thread;
Expand Down
4 changes: 2 additions & 2 deletions include/reactor-cpp/graph.hh
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ public:
}
}

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

auto name_resolver = [&](E object) -> std::string {
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz";
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz"; //NOLINT
if (name_map.find(object) == std::end(name_map)) {
name_map[object] = names[index];
index++;
Expand Down
1 change: 1 addition & 0 deletions include/reactor-cpp/port.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public:
[[nodiscard]] inline auto has_dependencies() const noexcept -> bool { return !dependencies_.empty(); }
[[nodiscard]] inline auto has_anti_dependencies() const noexcept -> bool { return !anti_dependencies_.empty(); }
[[nodiscard]] inline auto has_triggers() const noexcept -> bool { return !triggers_.empty(); }
[[nodiscard]] inline auto rating() const noexcept -> std::size_t { return triggers_.size() + dependencies_.size(); }

[[nodiscard]] inline auto inward_binding() const noexcept -> BasePort* { return inward_binding_; }
[[nodiscard]] inline auto outward_bindings() const noexcept -> const auto& { return outward_bindings_; }
Expand Down
106 changes: 46 additions & 60 deletions lib/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ void Environment::register_input_action(BaseAction* action) {
}

void Environment::optimize() {
#ifdef GRAPH_OPTIMIZATIONS
#if GRAPH_OPTIMIZATIONS
constexpr bool enable_optimizations = true;
#else
constexpr bool enable_optimizations = false;
#endif
log::Debug() << "Opimizations:" << enable_optimizations;
if constexpr (enable_optimizations) {
log::Debug() << graph_.to_mermaid();
expand_and_merge();
strip_and_optimize();
log::Debug() << optimized_graph_.to_mermaid();
} else {
// no optimizations
optimized_graph_ = graph_;
Expand Down Expand Up @@ -149,70 +151,53 @@ void Environment::expand_and_merge() {
auto spanning_tree = graph_.naive_spanning_tree(source);
for (auto& path : spanning_tree) {
ConnectionProperties merged_properties{};
auto* current_source = source;

for (auto element : path) {
auto property = element.first;
std::reverse(path.begin(), path.end());

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

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

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

} else {
// merging the connections
merged_properties.type_ = return_type;
if (current_rating > 0) {
auto return_type =
construction_table[std::pair<ConnectionType, ConnectionType>(merged_properties.type_, property.type_)];
// invalid will split the connections
if (return_type == Invalid) {
// first add connection until this point
optimized_graph_.add_edge(element.second, previous_element, merged_properties);

// adding up delays
merged_properties.delay_ += property.delay_;
// updating the source of the connection and resetting the properties
previous_element = element.second;
merged_properties = property;

// updating target enclave if not nullptr
merged_properties.enclave_ = (property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;
} else {
// merging the connections
merged_properties.type_ = return_type;

optimized_graph_.add_edge(current_source, element.second, merged_properties);
}
}
}
}
}
// adding up delays
merged_properties.delay_ += property.delay_;

void Environment::strip_and_optimize() {
Graph<BasePort*, ConnectionProperties> striped_graph{};

auto nodes = optimized_graph_.get_nodes();
std::vector<BasePort*> has_downstream_reactions{};
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_downstream_reactions),
[](BasePort* port) { return port->has_anti_dependencies(); });

std::vector<BasePort*> has_triggers{};
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_triggers),
[](BasePort* port) { return port->has_dependencies(); });

for (auto downstream : has_downstream_reactions) {
for (auto upstream : has_triggers) {
if (upstream != downstream) {
auto optional_path = optimized_graph_.shortest_path(downstream, upstream);

if (optional_path.has_value()) {
auto best_path = optional_path.value();
auto source = downstream;
for (auto hop : best_path) {
striped_graph.add_edge(source, hop.second, hop.first);
source = hop.second;
// updating target enclave if not nullptr
merged_properties.enclave_ =
(property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;

optimized_graph_.add_edge(element.second, previous_element, merged_properties);
}
}
}
}
}

optimized_graph_ = striped_graph;
}

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

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

log::Debug() << "instantiating port graph declaration";
if (top_environment_ == nullptr || top_environment_ == this) {
log::Debug() << "start optimization on port graph";
this->optimize();

log::Debug() << "instantiating port graph declaration";

log::Debug() << "graph: ";
log::Debug() << optimized_graph_;

Expand Down Expand Up @@ -291,11 +282,6 @@ void Environment::assemble() { // NOLINT
}

calculate_indexes();

// this assembles all the contained environments aka enclaves
for (auto* env : contained_environments_) {
env->assemble();
}
}

void Environment::build_dependency_graph(Reactor* reactor) { // NOLINT
Expand Down
2 changes: 2 additions & 0 deletions lib/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void BasePort::register_dependency(Reaction* reaction, bool is_trigger) noexcept
"Dependent output ports must belong to a contained reactor");
}

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

0 comments on commit 044d291

Please sign in to comment.