mmCEsim 0.3.0
mmWave Channel Estimation Simulation
macro.h
Go to the documentation of this file.
1
12#ifndef _EXPORT_MACRO_H_
13#define _EXPORT_MACRO_H_
14
15#include "export/lang.h"
16#include "log_global.h"
17#include "meta.h"
18#include "utils.h"
19#include <map>
20#include <regex>
21#include <stdexcept>
22#include <string>
23#include <utility>
24#include <vector>
25
26struct XY_Size {
27 XY_Size() = default;
28
29 unsigned Tx = 0;
30 unsigned Ty = 0;
31 unsigned Rx = 0;
32 unsigned Ry = 0;
33
34 unsigned t() const noexcept { return Tx * Ty; }
35 unsigned r() const noexcept { return Rx * Ry; }
36 unsigned _() const noexcept { return t() * r(); }
37
38 std::map<std::string, std::pair<unsigned, unsigned>> nodes_xy;
39
40 bool hasKey(std::string id) const noexcept { return nodes_xy.count(id); }
41
42 std::pair<unsigned, unsigned> xy(std::string id) const {
43 try {
44 return nodes_xy.at(id); // with boundary check
45 } catch (const std::out_of_range& e) {
46 // TODO: better error handling
47 _log.err() << "No '" + id + "' node when replacing macro (`SIZE[<id>]`, `BEAM[<id>]`, `GRID[<id>]`)!"
48 << std::endl;
49 Term::error("No '" + id + "' node when replacing macro (`SIZE[<id>]`, `BEAM[<id>]`, `GRID[<id>]`)!");
50 return { 0, 0 };
51 }
52 }
53
54 unsigned x(std::string id) const { return xy(id).first; }
55 unsigned y(std::string id) const { return xy(id).second; }
56 unsigned x_mul_y(std::string id) const {
57 auto&& x_and_y = this->xy(id);
58 return x_and_y.first * x_and_y.second;
59 }
60};
61
62struct Macro {
63 unsigned job_num = 0;
64 std::vector<unsigned> alg_num;
65 std::vector<std::vector<std::string>> alg_names;
66 std::vector<std::vector<std::string>> alg_params;
67 std::vector<std::vector<std::vector<std::pair<std::string, std::string>>>> alg_custom;
68 std::map<std::string, std::string> custom_priority;
69 std::map<std::string, std::string> custom;
70 std::map<std::string, std::string> custom_in_alg;
71 std::map<std::string, std::string> beamforming;
72 std::string _cascaded_channel;
75
76 bool replaceXY(std::string& r) const;
77
78 std::string replaceMacro(const std::string& s, int job_cnt, int alg_cnt) const;
79
80 private:
81 enum class Type {
82 PRE,
83 USER,
84 USER_PRIORITY,
85 };
86
87 // TODO: This only applies to C++ only now, and needs to be extended.
88 const std::map<std::string, std::pair<std::string, bool>> _constants = {
89 { R"(CHS\.PATHS_NUM)", { "CHS_paths_num", false } },
90 { R"(CHS\.CHANNELS\[(.*\w+.*)\])", { "CHS_channels[$1]", true } },
91 { R"(CHS\.CHANNELS\[(.*\w+.*)\]\.ID)", { "CHS_channels_id[$1]", false } },
92 { R"(CHS\.NODES\[(.*\w+.*)\]\.ID)", { "CHS_nodes_id[$1]", false } },
93 { R"(CHS\.FROM\[(.*\w+.*)\])", { "CHS_from[$1]", false } },
94 { R"(CHS\.FROM\[(.*\w+.*)\]\.INDEX)", { "CHS_from[$1]", false } },
95 { R"(CHS\.FROM\[(.*\w+.*)\]\.ID)", { "CHS_nodes_id[" + constantStr({ "CHS_from[$1]", false }) + "]", false } },
96 { R"(CHS\.TO\[(.*\w+.*)\])", { "CHS_to[$1]", false } },
97 { R"(CHS\.TO\[(.*\w+.*)\]\.INDEX)", { "CHS_to[$1]", false } },
98 { R"(CHS\.TO\[(.*\w+.*)\]\.ID)", { "CHS_nodes_id[" + constantStr({ "CHS_to[$1]", false }) + "]", false } },
99 { R"(CHS\[(.*\w+.*)\]\.JUMPS_NUM)", { "CHS_i_jumps_num[$1]", false } },
100 { R"(CHS\[(.*\w+.*)\]\.SIZE)", { "CHS_i_size[$1]", false } },
101 { R"(CHS\.ALL_CHANNELS\[(.*\w+.*)\]\.INDEX)", { "CHS_all_channels_index[$1]", false } },
102 { R"(CHS\.ALL_CHANNELS\[(.*\w+.*)\])",
103 { "CHS_channels[" + constantStr({ "CHS_all_channels_index[$1]", false }) + "]", true } },
104 { R"(CHS\.ALL_CHANNELS\[(.*\w+.*)\]\.ID)",
105 { "CHS_channels_id[" + constantStr({ "CHS_all_channels_index[$1]", false }) + "]", false } },
106 { R"(CHS\.NUM_CHANNELS_ACC\[(.*\w+.*)\])", { "CHS_num_channels_acc[$1]", false } },
107 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\])",
108 { "CHS_channels[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) + "]",
109 true } },
110 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.ID)",
111 { "CHS_channels_id[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) +
112 "]",
113 false } },
114 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.FROM)",
115 { "CHS_from[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) + "]",
116 false } },
117 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.FROM\.INDEX)",
118 { "CHS_from[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) + "]",
119 false } },
120 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.FROM\.ID)",
121 { "CHS_nodes_id[" +
122 constantStr({ "CHS_from[" +
123 constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) +
124 "]",
125 false }) +
126 "]",
127 false } },
128 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.TO)",
129 { "CHS_to[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) + "]",
130 false } },
131 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.TO\.INDEX)",
132 { "CHS_to[" + constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) + "]",
133 false } },
134 { R"(CHS\[(.*\w+.*)\]\[(.*\w+.*)\]\.TO\.ID)",
135 { "CHS_nodes_id[" +
136 constantStr({ "CHS_to[" +
137 constantStr({ "CHS_all_channels_index[CHS_num_channels_acc[$1] + ($2)]", false }) +
138 "]",
139 false }) +
140 "]",
141 false } },
142 };
143
150 std::string typeName(Type type) const noexcept;
151
152 std::string constantStr(const std::pair<std::string, bool>& v) const;
153
154 public:
164 std::string replace(const std::string& s, const std::string& key, const std::string& val,
165 Type type = Type::PRE) const;
166};
167
168static const Macro macro_none;
169
170inline std::string Macro::typeName(Type type) const noexcept {
171 return type == Type::PRE ? "Predefined" : type == Type::USER ? "User" : "User Priority";
172}
173
174inline std::string Macro::replace(const std::string& s, const std::string& key, const std::string& val,
175 Type type) const {
176 auto r = std::regex_replace(s, std::regex("`" + key + "`"), val);
177 if (r != s) _log.info() << "Macro Replace (" << typeName(type) << "): " << key << " -> " << val << std::endl;
178 return r;
179}
180
181inline std::string Macro::constantStr(const std::pair<std::string, bool>& v) const {
182 if (v.first.empty()) return "";
183 if (lang == Lang::CPP) {
184 if (v.second) return "(*mmCEsim_Consts_"s + v.first + ")";
185 else return "mmCEsim_Consts_"s + v.first;
186 } else return v.first;
187}
188
189#endif
std::ofstream & err()
Write error message to the log file.
Definition: log.h:117
std::ofstream & info()
Write infomation to the log file.
Definition: log.h:107
static void error(const T &msg)
Print error message.
Definition: term.h:178
Definition: type.h:18
Export Languages.
Lang
Export language.
Definition: lang.h:20
@ CPP
C++ with Armadillo library.
Global Access of Log.
Log _log
The shared Log variable.
Definition: log.cpp:53
static const Macro macro_none
Definition: macro.h:168
mmCEsim meta data
type
Definition: version_bump.py:14
Definition: macro.h:62
std::string _cascaded_channel
Definition: macro.h:72
std::string typeName(Type type) const noexcept
Get the name of Type.
Definition: macro.h:170
std::vector< std::vector< std::string > > alg_params
Definition: macro.h:66
std::string constantStr(const std::pair< std::string, bool > &v) const
Definition: macro.h:181
std::vector< unsigned > alg_num
Definition: macro.h:64
std::map< std::string, std::string > beamforming
Definition: macro.h:71
std::map< std::string, std::string > custom_priority
Definition: macro.h:68
const std::map< std::string, std::pair< std::string, bool > > _constants
Definition: macro.h:88
std::vector< std::vector< std::vector< std::pair< std::string, std::string > > > > alg_custom
Definition: macro.h:67
XY_Size _N
Definition: macro.h:73
std::string replace(const std::string &s, const std::string &key, const std::string &val, Type type=Type::PRE) const
RegEx replace.
Definition: macro.h:174
std::map< std::string, std::string > custom
Definition: macro.h:69
std::string replaceMacro(const std::string &s, int job_cnt, int alg_cnt) const
Definition: macro.cpp:73
unsigned job_num
Definition: macro.h:63
XY_Size _G
Definition: macro.h:73
XY_Size _B
Definition: macro.h:73
Lang lang
Definition: macro.h:74
bool replaceXY(std::string &r) const
Definition: macro.cpp:15
std::vector< std::vector< std::string > > alg_names
Definition: macro.h:65
std::map< std::string, std::string > custom_in_alg
Definition: macro.h:70
Definition: macro.h:26
bool hasKey(std::string id) const noexcept
Definition: macro.h:40
unsigned x(std::string id) const
Definition: macro.h:54
unsigned y(std::string id) const
Definition: macro.h:55
unsigned x_mul_y(std::string id) const
Definition: macro.h:56
unsigned Rx
Definition: macro.h:31
unsigned Tx
Definition: macro.h:29
std::pair< unsigned, unsigned > xy(std::string id) const
Definition: macro.h:42
XY_Size()=default
unsigned t() const noexcept
Definition: macro.h:34
unsigned _() const noexcept
Definition: macro.h:36
unsigned Ry
Definition: macro.h:32
unsigned Ty
Definition: macro.h:30
unsigned r() const noexcept
Definition: macro.h:35
std::map< std::string, std::pair< unsigned, unsigned > > nodes_xy
Definition: macro.h:38
Utilities.