MicroModelicaCCompiler  4.5.3
compute_deps.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <ir/equation.hpp>
23 #include <deps/sbg_graph/deps_graph.hpp>
24 
25 namespace MicroModelica {
26 namespace IR {
27 
28 struct QSSModelConfig {
30  SB::Deps::Graph graph;
31 };
32 
33 struct DefAlgDepsUse {
34  DefAlgDepsUse(Equation e, SB::Deps::VariableDep var_dep)
35  {
36  eq = e;
37  use = var_dep.mapF();
38  range = var_dep.mapF().wholeDom();
39  exp = var_dep.exp();
40  offset = var_dep.eqOffset();
41  recursive = var_dep.isRecursive();
42  use_map = var_dep.nMap();
43  def_map = SB::Deps::LMapExp();
44  }
45  DefAlgDepsUse(Equation e, SB::PWLMap pwl_def_map, Expression use_exp, SB::Deps::LMapExp use_map_exp, SB::Deps::LMapExp def_map_exp,
46  int off, bool rec = false)
47  {
48  eq = e;
49  use = pwl_def_map;
50  range = pwl_def_map.wholeDom();
51  exp = use_exp;
52  recursive = rec;
53  use_map = use_map_exp;
54  def_map = def_map_exp;
55  offset = off;
56  }
57  DefAlgDepsUse(const DefAlgDepsUse& other)
58  {
59  eq = other.eq;
60  use = other.use;
61  range = other.range;
62  exp = other.exp;
63  recursive = other.recursive;
64  use_map = other.use_map;
65  def_map = other.def_map;
66  offset = other.offset;
67  }
69  SB::PWLMap use;
70  SB::Deps::LMapExp use_map;
71  SB::Deps::LMapExp def_map;
72  SB::Set range;
74  int offset;
75  bool recursive;
76 };
77 
78 struct CompDef {
79  bool operator()(const DefAlgDepsUse& lhs, const DefAlgDepsUse& rhs) const
80  {
81  if (lhs.eq.id() != rhs.eq.id()) {
82  if (lhs.use_map.constantExp() && rhs.use_map.constantExp()) {
83  return lhs.eq.id() < rhs.eq.id();
84  }
85  if (lhs.use_map.constantExp() && !rhs.use_map.constantExp()) {
86  return lhs.use_map.constants() < rhs.use_map.appliedInitValues();
87  }
88  if (!lhs.use_map.constantExp() && rhs.use_map.constantExp()) {
89  return lhs.use_map.appliedInitValues() < rhs.use_map.constants();
90  }
91  if (!lhs.use_map.constantExp() && !rhs.use_map.constantExp()) {
92  return lhs.use_map.appliedInitValues() < rhs.use_map.appliedInitValues();
93  }
94  assert(false);
95  return false;
96  } else {
97  return lhs.use_map < rhs.use_map;
98  }
99  }
100 };
101 
102 typedef set<DefAlgDepsUse, CompDef> AlgDeps;
103 
104 typedef std::map<int, AlgDeps> AlgDepsMap;
105 
106 struct PrintedDep {
107  SB::Set range;
108  int id;
109  SB::Deps::LMapExp use_map;
110  SB::Deps::LMapExp eq_use_map;
111 };
112 
113 typedef list<PrintedDep> PrintedDeps;
114 
115 struct DepData {
116  DepData(int id_exp, SB::Deps::VariableDep var_dep_exp) : id(id_exp), var_dep(var_dep_exp), from_alg(false) {}
117  DepData(int id_exp, SB::Deps::VariableDep var_dep_exp, bool from_alg_exp) : id(id_exp), var_dep(var_dep_exp), from_alg(from_alg_exp) {}
118  int id;
119  SB::Deps::VariableDep var_dep;
120  bool from_alg;
121 };
122 
123 typedef list<DepData> DepsData;
124 
125 typedef std::map<std::string, DepsData> DepsMap;
126 
127 string addAlgDeps(Equation eq, SB::Deps::LMapExp eq_use, std::map<int, AlgDeps> der_deps, std::map<int, AlgDeps> alg_deps,
128  PrintedDeps& printed_deps, bool comes_from_rec = false);
129 
130 void insertAlg(AlgDepsMap& map, int id, DefAlgDepsUse new_dep);
131 
132 Expression getUseExp(Util::Variable variable, DepData dep_data);
133 
134 bool checkEventRange(Index index, Range range);
135 
136 std::vector<std::string> getVariables(Index index, Range range);
137 
138 template <typename N>
139 Option<Range> getUseRange(Util::Variable variable, DepData dep_data, N node, bool event = false, bool path_recursive_deps = false)
140 {
141  Expression use_exp = dep_data.var_dep.exp();
142  const bool SCALAR_EXP = dep_data.var_dep.nMap().constantExp();
143  if (variable.isScalar()) {
144  return Option<Range>();
145  }
147  if (variable.isArray() && path_recursive_deps) {
148  return Range(variable);
149  }
150  if (dep_data.var_dep.isRecursive()) {
151  return Range(dep_data.var_dep.var());
152  }
153  if (SCALAR_EXP) {
154  return Option<Range>();
155  }
156  Index use_idx(use_exp);
157  std::vector<std::string> var_names = (use_idx.isConstant() && node.range()) ? node.range()->getIndexes() : use_idx.variables();
160  if (dep_data.var_dep.equations().size() == 1 && dep_data.var_dep.variables().size() > 1) {
161  return Range(dep_data.var_dep.variables(), dep_data.var_dep.varOffset(), var_names);
162  } else if (dep_data.var_dep.equations().size() > 1 && dep_data.var_dep.variables().size() == 1) {
163  return Range(dep_data.var_dep.equations(), dep_data.var_dep.eqOffset(), var_names);
164  }
165  if (!SCALAR_EXP) {
166  if (dep_data.from_alg) {
167  return Range(dep_data.var_dep.equations(), dep_data.var_dep.eqOffset(), use_idx.variables(), node.range());
168  }
169  if (event) {
170  if (!node.range()) {
171  return Range(dep_data.var_dep.variables(), dep_data.var_dep.varOffset(), var_names);
172  } else if (!checkEventRange(use_idx, node.range().get())) {
173  var_names = getVariables(use_idx, node.range().get());
174  return Range(dep_data.var_dep.equations(), dep_data.var_dep.eqOffset(), var_names);
175  }
176  }
177  return node.range();
178  }
179  assert(false);
180  return Option<Range>();
181 }
182 
183 Option<Range> getUseRange(Util::Variable variable, DepData dep_data);
184 
185 bool findDep(DepsMap deps, DepData dep_data, bool multiple_nodes = false);
186 
187 bool checkAlgRecursiveDeps(Equation eq, AlgDepsMap alg_deps, AlgDepsMap deps);
188 
189 } // namespace IR
190 } // namespace MicroModelica
MicroModelica::IR::DefAlgDepsUse::offset
int offset
Definition: compute_deps.hpp:108
MicroModelica::IR::Range
Definition: index.hpp:164
MicroModelica::IR::DepData::var_dep
SB::Deps::VariableDep var_dep
Definition: compute_deps.hpp:153
MicroModelica::IR::addAlgDeps
string addAlgDeps(Equation eq, SB::Deps::LMapExp eq_use, AlgDepsMap alg_deps, AlgDepsMap deps, PrintedDeps &printed_deps, bool comes_from_rec)
Definition: compute_deps.cpp:123
MicroModelica::Util::Variable
Definition: symbol_table.hpp:75
MicroModelica::IR::DefAlgDepsUse::use
SB::PWLMap use
Definition: compute_deps.hpp:103
MicroModelica::IR::findDep
bool findDep(DepsMap deps, DepData dep_data, bool multiple_nodes)
Definition: compute_deps.cpp:65
MicroModelica::IR::PrintedDep
Definition: compute_deps.hpp:140
MicroModelica::IR::DefAlgDepsUse::eq
Equation eq
Definition: compute_deps.hpp:102
MicroModelica::IR::getUseExp
Expression getUseExp(Variable variable, DepData dep_data)
Definition: compute_deps.cpp:48
MicroModelica::IR::DepsMap
std::map< std::string, DepsData > DepsMap
Definition: compute_deps.hpp:159
MicroModelica::IR::insertAlg
void insertAlg(AlgDepsMap &map, int id, DefAlgDepsUse new_dep)
Definition: compute_deps.cpp:187
MicroModelica::IR::DefAlgDepsUse::range
SB::Set range
Definition: compute_deps.hpp:106
MicroModelica::IR::EquationTable
ModelTable< int, Equation > EquationTable
Definition: equation.hpp:169
MicroModelica::IR::PrintedDep::eq_use_map
SB::Deps::LMapExp eq_use_map
Definition: compute_deps.hpp:144
MicroModelica::IR::DepData::from_alg
bool from_alg
Definition: compute_deps.hpp:154
Option
Definition: util_types.hpp:32
MicroModelica::Util::Variable::isArray
bool isArray() const
Definition: symbol_table.hpp:130
MicroModelica::IR::AlgDeps
set< DefAlgDepsUse, CompDef > AlgDeps
Definition: compute_deps.hpp:136
MicroModelica::IR::AlgDepsMap
std::map< int, AlgDeps > AlgDepsMap
Definition: compute_deps.hpp:138
MicroModelica::IR::QSSModelConfig::graph
SB::Deps::Graph graph
Definition: compute_deps.hpp:81
MicroModelica::Util::Variable::isScalar
bool isScalar() const
Definition: symbol_table.hpp:131
MicroModelica::IR::Expression
Definition: expression.hpp:64
MicroModelica::IR::CompDef
Definition: compute_deps.hpp:112
MicroModelica::IR::checkEventRange
bool checkEventRange(Index index, Range range)
Definition: compute_deps.cpp:194
MicroModelica::IR::DepData::DepData
DepData(int id_exp, SB::Deps::VariableDep var_dep_exp)
Definition: compute_deps.hpp:150
MicroModelica::IR::QSSModelConfig::eqs
EquationTable eqs
Definition: compute_deps.hpp:80
MicroModelica::IR::PrintedDep::use_map
SB::Deps::LMapExp use_map
Definition: compute_deps.hpp:143
MicroModelica::IR::PrintedDeps
list< PrintedDep > PrintedDeps
Definition: compute_deps.hpp:147
MicroModelica::IR::getVariables
vector< string > getVariables(Index index, Range range)
Definition: compute_deps.cpp:213
MicroModelica::IR::PrintedDep::range
SB::Set range
Definition: compute_deps.hpp:141
MicroModelica::IR::Equation
Definition: equation.hpp:67
MicroModelica::IR::DefAlgDepsUse::def_map
SB::Deps::LMapExp def_map
Definition: compute_deps.hpp:105
equation.hpp
MicroModelica::IR::DefAlgDepsUse::exp
Expression exp
Definition: compute_deps.hpp:107
MicroModelica::IR::DefAlgDepsUse
Definition: compute_deps.hpp:67
MicroModelica::IR::Index
Definition: index.hpp:92
MicroModelica::IR::DepsData
list< DepData > DepsData
Definition: compute_deps.hpp:157
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::DepData::id
int id
Definition: compute_deps.hpp:152
MicroModelica::IR::DefAlgDepsUse::recursive
bool recursive
Definition: compute_deps.hpp:109
MicroModelica::IR::DefAlgDepsUse::use_map
SB::Deps::LMapExp use_map
Definition: compute_deps.hpp:104
MicroModelica::IR::DefAlgDepsUse::DefAlgDepsUse
DefAlgDepsUse(Equation e, SB::Deps::VariableDep var_dep)
Definition: compute_deps.hpp:68
MicroModelica::IR::getUseRange
Option< Range > getUseRange(Util::Variable variable, DepData dep_data, N node, bool event=false, bool path_recursive_deps=false)
Definition: compute_deps.hpp:173
MicroModelica::IR::DepData
Definition: compute_deps.hpp:149
MicroModelica::IR::checkAlgRecursiveDeps
bool checkAlgRecursiveDeps(Equation eq, AlgDepsMap alg_deps, AlgDepsMap deps)
Definition: compute_deps.cpp:105
MicroModelica::IR::CompDef::operator()
bool operator()(const DefAlgDepsUse &lhs, const DefAlgDepsUse &rhs) const
Definition: compute_deps.hpp:113
MicroModelica::IR::PrintedDep::id
int id
Definition: compute_deps.hpp:142