Lodestar
An integrated real-time control package in C++
Executor.hpp
1 //
2 // Created by Hamza El-Kebir on 12/25/21.
3 //
4 
5 #ifndef LODESTAR_EXECUTOR_HPP
6 #define LODESTAR_EXECUTOR_HPP
7 
8 #include "Lodestar/blocks/BlockPack.hpp"
9 #include "Lodestar/blocks/aux/StronglyConnectedComponents.hpp"
10 #include <Lodestar/cli/ExecuteCommand.hpp>
11 
12 #include <utility>
13 #include <algorithm>
14 #include <regex>
15 
16 #define FMT_HEADER_ONLY
17 
18 #include <fmt/format.h>
19 
20 namespace ls {
21  namespace blocks {
22  namespace aux {
23  class Executor {
24  public:
25  Executor() : blockPack(), executionOrder(1)
26  {}
27 
28  Executor(ls::blocks::BlockPack bp) : blockPack(::std::move(bp)),
29  executionOrder(bp.blocks)
30  {}
31 
32  // FIXME: For some reason, these two functions cannot be put in the source file, since the copy
33  // assignment will not work otherwise. Segfault with __memmove_avx_unaligned_erms is thrown in both GCC
34  // and Clang in that case.
35  void computeExecutionOrder()
36  {
37  blockPack.makeGraph();
38  executionOrder = blockPack.blocks;
39  components = StronglyConnectedComponents::findComponents(blockPack.graph);
40 
41  ::std::stable_sort(executionOrder.begin(),
42  executionOrder.end(),
43  [&](
44  BlockProto *a,
45  BlockProto *b) {
46  return order(a, b);
47  });
48 
49  ::std::reverse(executionOrder.begin(),
50  executionOrder.end());
51  }
52 
53  void applyExecutionOrder()
54  {
55  int i = 0;
56  for (auto &blk: executionOrder) {
57  blk->setPriority(i);
58  i++;
59  }
60  }
61 
62  void resolveExecutionOrder()
63  {
64  computeExecutionOrder();
65  applyExecutionOrder();
66  }
67 
68  bool order(BlockProto *blk1, BlockProto *blk2) const;
69 
70  // NOTE: Yet another segfault here if this were defined in the source file.
71  void trigger()
72  {
73  for (auto blk: executionOrder)
74  blk->trigger();
75  }
76 
77  ls::blocks::BlockPack blockPack;
78  ::std::vector<BlockProto *> executionOrder{};
80 
81  void
82  makeDotFile(::std::stringstream &ss, bool lineLabels = true,
83  bool slotLabels = false, float rankSep = 2,
84  float nodeSep = 2);
85 
86  void
87  makeSimpleDotFile(::std::stringstream &ss, bool lineLabels = true,
88  bool slotLabels = false, float rankSep = 2,
89  float nodeSep = 2);
90 
91  ::std::string
92  getAsciiGraph(bool lineLabels = true,
93  bool slotLabels = false, float rankSep = 2,
94  float nodeSep = 2);
95 
96  unsigned long getComponentSize() const;
97 
98  protected:
99  static void
100  replace(::std::string &str, const ::std::string &from,
101  const ::std::string &to)
102  {
103  str = ::std::regex_replace(str, ::std::regex(from), to);
104 // size_t start_pos = str.find(from);
105 // if (start_pos == ::std::string::npos)
106 // return false;
107 // str.replace(start_pos, from.length(), to);
108 // return true;
109  }
110 
111  };
112  }
113  }
114 }
115 
116 
117 #endif //LODESTAR_EXECUTOR_HPP
ls::blocks::aux::Executor
Definition: Executor.hpp:23
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::blocks::BlockPack
Definition: BlockPack.hpp:17
ls::blocks::aux::StronglyConnectedComponents::SCCResult
Definition: StronglyConnectedComponents.hpp:18
ls::blocks::BlockProto
Definition: BlockProto.hpp:20