Tenncor
matcher.hpp
Go to the documentation of this file.
1 
10 #include "teq/traveler.hpp"
11 
12 #include "tag/group.hpp"
13 
14 #include "opt/ivoter.hpp"
15 
16 #ifndef OPT_MATCHER_HPP
17 #define OPT_MATCHER_HPP
18 
19 namespace opt
20 {
21 
23 const std::string group_prefix = "group:";
24 
42 struct Matcher final : public teq::iTraveler
43 {
44  Matcher (void) = default;
45 
46  Matcher (const VoterPool& voters) : voters_(voters) {}
47 
49  void visit (teq::iLeaf* leaf) override
50  {
51  if (false == estd::has(candidates_, leaf))
52  {
53  if (tag::get_property_reg().has_property(leaf, tag::immutable_tag))
54  {
55  std::string const_str = leaf->to_string();
56  CandsT cands = {
57  {Symbol{CAND_TYPE::CONST, const_str}, CtxsT{}}
58  };
59  if (is_scalar(leaf))
60  {
61  // match against scalar maps
62  if (estd::has(voters_.immutables_, const_str))
63  {
64  cands.emplace(
65  Symbol{CAND_TYPE::SCALAR, const_str}, CtxsT{});
66  }
67  }
68  candidates_.emplace(leaf, cands);
69  }
70  else
71  {
72  candidates_.emplace(leaf, CandsT{});
73  }
74  }
75  }
76 
78  void visit (teq::iFunctor* func) override
79  {
80  if (false == estd::has(candidates_, func))
81  {
82  auto& children = func->get_children();
83  for (auto& child : children)
84  {
85  child.get_tensor()->accept(*this);
86  }
87 
88  if (std::all_of(children.begin(), children.end(),
89  [this](const teq::FuncArg& child) -> bool
90  {
91  auto ctens = child.get_tensor().get();
92  return estd::has(this->candidates_[ctens],
93  Symbol{CAND_TYPE::CONST, ctens->to_string()});
94  }))
95  {
96  // all children are constants
97  // therefore mark this as constant
98  std::string const_str = func->to_string();
99  candidates_.emplace(func, CandsT{
100  {Symbol{CAND_TYPE::CONST, const_str}, CtxsT{}},
101  });
102 
103  // mark as scalar if func's children are scalar
104  // in order to propagate scalar info to parents
105  if (scalarize_)
106  {
107  if (std::all_of(children.begin(), children.end(),
108  [this](const teq::FuncArg& child) -> bool
109  {
110  auto ctens = child.get_tensor().get();
111  std::string scalar_str = scalarize_(ctens);
112  return estd::has(this->candidates_[ctens],
113  Symbol{CAND_TYPE::SCALAR, scalar_str});
114  }))
115  {
116  std::string scalar_str = scalarize_(func);
117  candidates_[func].emplace(
118  Symbol{CAND_TYPE::SCALAR, scalar_str}, CtxsT{});
119  }
120  }
121  return;
122  }
123 
124  CandsT out_cands;
125  // functor
126  std::string opname = func->get_opcode().name_;
127  auto it = voters_.branches_.find(opname);
128  if (voters_.branches_.end() != it)
129  {
130  CandArgsT args;
131  args.reserve(children.size());
132  for (auto& child : children)
133  {
134  auto ctens = child.get_tensor();
135  args.push_back(CandArg{
136  ctens,
137  candidates_[ctens.get()],
138  child.get_shaper(),
139  child.get_coorder(),
140  });
141  }
142  out_cands = it->second->inspect(args);
143  }
144 
145  // do the same for functors that are the "head" of groups
146  tag::SubgraphsT sgs;
147  if (estd::get(sgs, group_head_, func))
148  {
149  // look for candidates in each of the potential subgraphs
150  for (tag::SgraphptrT sg : sgs)
151  {
152  auto bit = voters_.branches_.find(
153  group_prefix + sg->group_);
154  if (voters_.branches_.end() != bit)
155  {
156  // todo: store sg->children_ as teq::ArgsT
157  CandArgsT args;
158  args.reserve(children.size());
159  for (auto& sgcpair : sg->children_)
160  {
161  auto ctens = sgcpair.second;
162  args.push_back(CandArg{
163  ctens,
164  candidates_[sgcpair.first],
166  teq::CoordptrT(),
167  });
168  }
169  CandsT group_cands = bit->second->inspect(args);
170  out_cands.insert(
171  group_cands.begin(), group_cands.end());
172  }
173  }
174  }
175 
176  candidates_.emplace(func, out_cands);
177  }
178  }
179 
182 
184  std::unordered_map<teq::iTensor*,CandsT> candidates_;
185 
188 
190  std::function<std::string(teq::iTensor*)> scalarize_;
191 };
192 
193 }
194 
195 #endif // OPT_MATCHER_HPP
VoterPool voters_
Conversion voters to identify candidates.
Definition: matcher.hpp:181
args
Definition: csv_to_png.py:105
Convert to a non-scalar constant.
Definition: candidate.hpp:37
void visit(teq::iFunctor *func) override
Implementation of iTraveler.
Definition: matcher.hpp:78
Matcher(void)=default
virtual const ArgsT & get_children(void) const =0
Return children nodes as a vector of raw pointers.
Interface of iOperation-defined operation node.
Definition: ifunctor.hpp:28
std::unordered_map< std::string, VotptrT > branches_
Map voter identifier to associated branch voters.
Definition: ivoter.hpp:256
std::shared_ptr< iCoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:106
std::string name_
String representation of operation.
Definition: ifunctor.hpp:21
Definition: candidate.hpp:19
std::unordered_set< ContexT, boost::hash< ContexT > > CtxsT
Set of contexts that serve as a candidates of a conversion rule.
Definition: candidate.hpp:29
std::vector< CandArg > CandArgsT
Vector of candidate arguments.
Definition: candidate.hpp:95
Coordinate mapper and tensor pair.
Definition: funcarg.hpp:21
CoordptrT identity
Identity matrix instance.
virtual Opcode get_opcode(void) const =0
Return operation encoding.
Interface to travel through graph, treating iLeaf and iFunctor differently.
Definition: itensor.hpp:24
std::unordered_map< teq::iTensor *, SubgraphsT > SubgraphAssocsT
Root of the subgraph associated with the subgraph representations.
Definition: group.hpp:165
std::unordered_map< Symbol, CtxsT, SymbolHash > CandsT
Map of convers symbols to its potential candidate conversion rules.
Definition: candidate.hpp:76
Encapsulation of match output argument.
Definition: candidate.hpp:79
std::function< std::string(teq::iTensor *)> scalarize_
Function that returns constant representation of tensor.
Definition: matcher.hpp:190
std::unordered_set< SgraphptrT > SubgraphsT
Set of subgraphs.
Definition: group.hpp:162
Matcher(const VoterPool &voters)
Definition: matcher.hpp:46
bool is_scalar(teq::iLeaf *leaf)
Return true if leaf contains a scalar.
std::unordered_set< std::string > immutables_
Set of immutable ids under rule tree.
Definition: ivoter.hpp:253
tag::SubgraphAssocsT group_head_
Root of grouped subgraphs.
Definition: matcher.hpp:187
Generic representation of a conversion rule.
Definition: candidate.hpp:45
virtual std::string to_string(void) const =0
Return the string representation of the tensor.
PropertyRegistry & get_property_reg(void)
Return reference to global property registry.
const std::string immutable_tag
Identifier for immutable property.
Definition: prop.hpp:20
std::unordered_map< teq::iTensor *, CandsT > candidates_
Map real TEQ tensors to candidates identified as TEQ graph is visited.
Definition: matcher.hpp:184
void visit(teq::iLeaf *leaf) override
Implementation of iTraveler.
Definition: matcher.hpp:49
std::shared_ptr< Subgraph > SgraphptrT
Smart pointer of the subgraph.
Definition: group.hpp:159
Definition: matcher.hpp:42
Definitive scalar constant.
Definition: def.h:29
const std::string group_prefix
String to prefix group types.
Definition: matcher.hpp:23
Parsed representation of a rule tree.
Definition: ivoter.hpp:250
Leaf of the graph commonly representing the variable in an equation.
Definition: ileaf.hpp:19