MicroModelicaCCompiler  4.5.3
model_matrix_gen.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 <string>
23 #include <map>
24 
25 #include <ir/compute_deps.hpp>
26 #include <ir/statement.hpp>
27 #include <ir/node_selector.hpp>
28 #include <deps/sbg_graph/deps_graph.hpp>
29 #include <util/symbol_table.hpp>
30 
31 namespace MicroModelica {
32 namespace IR {
33 
34 namespace MATRIX {
35 typedef enum { Normal = 0, Transpose = 1 } Mode;
36 
37 typedef enum { Alloc = 0, Init = 2 } Method;
38 
39 typedef std::map<int ,AST_ExpressionList> UserDefMatrixExps;
40 
41 template <typename S>
42 class MatrixConfig {
43  public:
44  MatrixConfig<S>(std::string cont, std::vector<std::string> n, std::vector<std::string> ac, std::vector<std::string> comp, S sel)
45  : container(cont), names(n), access(ac), component(comp), search(IR::STATEMENT::LHS), selector(sel), user_def(), use_component({true,true}) {};
46  MatrixConfig<S>(std::string cont, std::vector<std::string> n, std::vector<std::string> ac, std::vector<std::string> comp,
48  : container(cont), names(n), access(ac), component(comp), search(s), selector(sel), user_def(ud), use_component({true, true}){};
49  MatrixConfig<S>(std::string cont, std::vector<std::string> n, std::vector<std::string> ac, std::vector<std::string> comp,
50  std::vector<bool> use_comp, IR::STATEMENT::AssignTerm s, UserDefMatrixExps ud, S sel)
51  : container(cont), names(n), access(ac), component(comp), search(s), selector(sel), user_def(ud), use_component(use_comp){};
52  MatrixConfig<S>(std::string cont, std::vector<std::string> n, std::vector<std::string> ac, std::vector<std::string> comp,
54  : container(cont), names(n), access(ac), component(comp), search(s), selector(sel), user_def(), use_component({true,true}) {};
55  MatrixConfig<S>(){};
56  std::string container;
57  std::vector<std::string> names;
58  std::vector<std::string> access;
59  std::vector<std::string> component;
61  S selector;
63  std::vector<bool> use_component;
64 };
65 
66 typedef MatrixConfig<Deps::EQSelector> EQMatrixConfig;
67 
68 typedef MatrixConfig<Deps::EVSelector> EVMatrixConfig;
69 
70 } // namespace MATRIX
71 
72 struct ModelMatrixDef {
73  std::string alloc[2];
74  std::string init[2];
75 };
76 
77 struct MatrixCode {
78  vector<std::string> begin;
79  vector<std::string> end;
80  vector<std::string> code;
81 };
82 
83 template <typename NT, typename N, typename Config>
84 class ModelMatrixGenerator {
85  public:
87  ~ModelMatrixGenerator() = default;
88 
89  void init(SB::Deps::SetVertex vertex);
90  void end();
91  void postProcess(SB::Deps::SetVertex vertex);
92  void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep);
93  void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, SB::Deps::SetVertex gen_vertex);
94  void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift);
95  void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::PWLMap use_map, SB::Deps::LMapExp use_map_exp,
96  Expression use_exp, SB::PWLMap def_map, SB::Deps::LMapExp def_map_exp, SB::Set intersection);
97  void initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge edge);
99 
100  void setup(Config config);
101  Config config();
102 
103  protected:
104  void addCode(MatrixCode dep_code, std::stringstream& code);
105  std::string component(MATRIX::Method method, MATRIX::Mode mode) const;
107 
109  int _tabs;
112  Config _config;
113 };
114 
115 } // namespace IR
116 } // namespace MicroModelica
MicroModelica::IR::ModelMatrixGenerator::postProcess
void postProcess(SB::Deps::SetVertex vertex)
Definition: model_matrix_gen.cpp:186
MicroModelica::IR::MATRIX::Normal
@ Normal
Definition: model_matrix_gen.hpp:103
MicroModelica::IR::ModelMatrixGenerator::_deps
DepsMap _deps
Definition: model_matrix_gen.hpp:144
MicroModelica::IR::STATEMENT::LHS
@ LHS
Definition: statement.hpp:51
MicroModelica::IR::MatrixCode::begin
vector< std::string > begin
Definition: model_matrix_gen.hpp:112
MicroModelica::IR::ModelMatrixGenerator::_config
Config _config
Definition: model_matrix_gen.hpp:146
MicroModelica::IR::ModelMatrixGenerator::_tabs
int _tabs
Definition: model_matrix_gen.hpp:143
MicroModelica::IR::ModelMatrixDef::init
std::string init[2]
Definition: model_matrix_gen.hpp:108
MicroModelica::IR::MATRIX::MatrixConfig::search
IR::STATEMENT::AssignTerm search
Definition: model_matrix_gen.hpp:111
MicroModelica::IR::ModelMatrixGenerator::init
void init(SB::Deps::SetVertex vertex)
Definition: model_matrix_gen.cpp:199
MicroModelica::IR::DepsMap
std::map< std::string, DepsData > DepsMap
Definition: compute_deps.hpp:159
compute_deps.hpp
MicroModelica::IR::ModelMatrixGenerator::ModelMatrixGenerator
ModelMatrixGenerator()
Definition: model_matrix_gen.cpp:59
symbol_table.hpp
MicroModelica::IR::MATRIX::UserDefMatrixExps
std::map< int,AST_ExpressionList > UserDefMatrixExps
Definition: model_matrix_gen.hpp:90
MicroModelica::IR::MatrixCode::code
vector< std::string > code
Definition: model_matrix_gen.hpp:114
MicroModelica::IR::ModelMatrixGenerator::addCode
void addCode(MatrixCode dep_code, std::stringstream &code)
Definition: model_matrix_gen.cpp:76
MicroModelica::IR::MATRIX::Method
Method
Definition: model_matrix_gen.hpp:88
MicroModelica::IR::MATRIX::MatrixConfig::component
std::vector< std::string > component
Definition: model_matrix_gen.hpp:110
MicroModelica::IR::ModelMatrixDef
Definition: model_matrix_gen.hpp:106
MicroModelica::IR::MatrixCode
Definition: model_matrix_gen.hpp:111
MicroModelica::IR::ModelMatrixGenerator::end
void end()
Definition: model_matrix_gen.cpp:204
MicroModelica::IR::MatrixCode::end
vector< std::string > end
Definition: model_matrix_gen.hpp:113
MicroModelica::IR::Expression
Definition: expression.hpp:64
node_selector.hpp
MicroModelica::IR::MATRIX::Transpose
@ Transpose
Definition: model_matrix_gen.hpp:103
MicroModelica::IR::ModelMatrixGenerator::setup
void setup(Config config)
Definition: model_matrix_gen.cpp:64
MicroModelica::IR::MATRIX::MatrixConfig::use_component
std::vector< bool > use_component
Definition: model_matrix_gen.hpp:114
MicroModelica::IR::MATRIX::EVMatrixConfig
MatrixConfig< Deps::EVSelector > EVMatrixConfig
Definition: model_matrix_gen.hpp:119
MicroModelica::IR::ModelMatrixGenerator::def
ModelMatrixDef def()
Definition: model_matrix_gen.cpp:260
MicroModelica::IR::STATEMENT::AssignTerm
AssignTerm
Definition: statement.hpp:51
MicroModelica::IR::MATRIX::Mode
Mode
Definition: model_matrix_gen.hpp:86
MicroModelica::IR::ModelMatrixGenerator::_post_process_eval
bool _post_process_eval
Definition: model_matrix_gen.hpp:145
MicroModelica::IR::MATRIX::MatrixConfig::names
std::vector< std::string > names
Definition: model_matrix_gen.hpp:108
MicroModelica::IR::ModelMatrixGenerator::printMatrix
void printMatrix(MATRIX::Method method, MATRIX::Mode mode)
Definition: model_matrix_gen.cpp:104
MicroModelica::IR::ModelMatrixDef::alloc
std::string alloc[2]
Definition: model_matrix_gen.hpp:107
MicroModelica::IR::MATRIX::EQMatrixConfig
MatrixConfig< Deps::EQSelector > EQMatrixConfig
Definition: model_matrix_gen.hpp:117
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::MATRIX::MatrixConfig::container
std::string container
Definition: model_matrix_gen.hpp:106
MicroModelica::IR::ModelMatrixGenerator::_model_matrix_def
ModelMatrixDef _model_matrix_def
Definition: model_matrix_gen.hpp:142
MicroModelica::IR::ModelMatrixGenerator::config
Config config()
Definition: model_matrix_gen.cpp:70
MicroModelica::IR::ModelMatrixGenerator::visitF
void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep)
Definition: model_matrix_gen.cpp:209
MicroModelica::IR::MATRIX::MatrixConfig::access
std::vector< std::string > access
Definition: model_matrix_gen.hpp:109
MicroModelica::IR::ModelMatrixGenerator::~ModelMatrixGenerator
~ModelMatrixGenerator()=default
MicroModelica::IR::ModelMatrixGenerator::component
std::string component(MATRIX::Method method, MATRIX::Mode mode) const
Definition: model_matrix_gen.cpp:89
MicroModelica::IR::MATRIX::MatrixConfig::selector
S selector
Definition: model_matrix_gen.hpp:112
MicroModelica::IR::MATRIX::MatrixConfig::user_def
UserDefMatrixExps user_def
Definition: model_matrix_gen.hpp:113
MicroModelica::IR::ModelMatrixGenerator::visitG
void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift)
Definition: model_matrix_gen.cpp:228
MicroModelica::IR::ModelMatrixGenerator::initG
void initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge edge)
Definition: model_matrix_gen.cpp:255
statement.hpp
MicroModelica::IR::MATRIX::Alloc
@ Alloc
Definition: model_matrix_gen.hpp:88
MicroModelica::IR::MATRIX::Init
@ Init
Definition: model_matrix_gen.hpp:88