Tenncor
teq_csv.hpp
Go to the documentation of this file.
1 
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <utility>
12 
13 #include "teq/ileaf.hpp"
14 #include "teq/ifunctor.hpp"
15 
16 #include "estd/estd.hpp"
17 
18 #include "dbg/stream/teq.hpp"
19 
20 #ifndef DBG_TEQ_CSV_HPP
21 #define DBG_TEQ_CSV_HPP
22 
24 const char label_delim = ':';
25 
26 static void multiline_replace (std::string& multiline)
27 {
28  size_t i = 0;
29  char nline = '\n';
30  while ((i = multiline.find(nline, i)) != std::string::npos)
31  {
32  multiline.replace(i, 1, "\\");
33  }
34 }
35 
38 {
39  VARIABLE = 0,
42 };
43 
45 using GetTypeF = std::function<NODE_TYPE(teq::iFunctor*)>;
46 
48 struct CSVEquation final : public teq::iTraveler
49 {
50  CSVEquation (GetTypeF get_ftype =
51  [](teq::iFunctor* func) { return FUNCTOR; }) :
52  get_ftype_(get_ftype) {}
53 
55  void visit (teq::iLeaf* leaf) override
56  {
57  if (estd::has(nodes_, leaf))
58  {
59  return;
60  }
61  std::string label;
62  auto it = labels_.find(leaf);
63  if (labels_.end() != it)
64  {
65  label = it->second + "=";
66  }
67  label += leaf->to_string();
68  if (showshape_)
69  {
70  label += leaf->shape().to_string();
71  }
72  nodes_.emplace(leaf, Node{
73  label,
74  VARIABLE,
75  nodes_.size(),
76  });
77  }
78 
80  void visit (teq::iFunctor* func) override
81  {
82  if (estd::has(nodes_, func))
83  {
84  return;
85  }
86  std::string funcstr;
87  auto it = labels_.find(func);
88  if (labels_.end() != it)
89  {
90  funcstr = it->second + "=";
91  }
92  funcstr += func->to_string();
93  if (showshape_)
94  {
95  funcstr += func->shape().to_string();
96  }
97  nodes_.emplace(func, Node{
98  funcstr,
99  get_ftype_(func),
100  nodes_.size(),
101  });
102  auto& children = func->get_children();
103  for (size_t i = 0, n = children.size(); i < n; ++i)
104  {
105  const teq::FuncArg& child = children[i];
106  auto coorder = child.get_coorder().get();
107  auto tens = child.get_tensor().get();
108  if (teq::is_identity(coorder))
109  {
110  coorder = nullptr;
111  }
112  else
113  {
114  std::string coordstr = coorder->to_string();
115  multiline_replace(coordstr);
116  coorders_.emplace(coorder, coordstr);
117  }
118  edges_.push_back(Edge{
119  func,
120  tens,
121  coorder,
122  fmts::to_string(i),
123  });
124  tens->accept(*this);
125  }
126  }
127 
129  void to_stream (std::ostream& out)
130  {
131  size_t nnodes = nodes_.size();
132  for (size_t i = 0, nedges = edges_.size(); i < nedges; ++i)
133  {
134  const Edge& edge = edges_[i];
135  auto& parent_node = nodes_[edge.func_];
136  auto& child_node = nodes_[edge.child_];
137  std::string color = child_node.ntype_ == CACHED_FUNC ?
138  "red" : "white";
139  if (nullptr == edge.coorder_)
140  {
141  out << parent_node.id_ << label_delim
142  << parent_node.label_ << ','
143  << child_node.id_ << label_delim
144  << child_node.label_ << ','
145  << edge.edge_label_ << ','
146  << color << '\n';
147  }
148  else
149  {
150  out << parent_node.id_ << label_delim
151  << parent_node.label_ << ','
152  << nnodes + i << label_delim
153  << coorders_[edge.coorder_] << ','
154  << edge.edge_label_ << ','
155  << color << '\n';
156 
157  out << nnodes + i << label_delim
158  << coorders_[edge.coorder_] << ','
159  << child_node.id_ << label_delim
160  << child_node.label_ << ','
161  << edge.edge_label_ << ','
162  << color << '\n';
163  }
164  }
165  }
166 
168  bool showshape_ = false;
169 
172 
173 private:
174  struct Edge
175  {
177 
179 
181 
182  std::string edge_label_;
183  };
184 
185  struct Node
186  {
187  std::string label_;
188 
190 
191  size_t id_;
192  };
193 
194  std::vector<Edge> edges_;
195 
196  std::unordered_map<teq::iTensor*,Node> nodes_;
197 
198  std::unordered_map<teq::iCoordMap*,std::string> coorders_;
199 
201 };
202 
203 #endif // DBG_TEQ_CSV_HPP
std::vector< Edge > edges_
Definition: teq_csv.hpp:194
const char label_delim
CSV delimiter.
Definition: teq_csv.hpp:24
size_t id_
Definition: teq_csv.hpp:191
std::string to_string(void) const
Return string representation of shape.
Definition: shape.hpp:148
virtual const ArgsT & get_children(void) const =0
Return children nodes as a vector of raw pointers.
std::unordered_map< teq::iCoordMap *, std::string > coorders_
Definition: teq_csv.hpp:198
Interface of iOperation-defined operation node.
Definition: ifunctor.hpp:28
bool showshape_
Print every tensor&#39;s shape if true, otherwise don&#39;t.
Definition: teq_csv.hpp:168
LabelsMapT labels_
For every label associated with a tensor, show LABEL=value in the tree.
Definition: teq_csv.hpp:171
NODE_TYPE ntype_
Definition: teq_csv.hpp:189
Definition: teq_csv.hpp:41
TensptrT get_tensor(void) const
Return tensor being mapped.
Definition: funcarg.hpp:61
NODE_TYPE
Type of the tensors.
Definition: teq_csv.hpp:37
Interface for transforming coordinates and reversing the coordinate.
Definition: coord.hpp:20
CoordptrT get_coorder(void) const
Return coord map for coordinates.
Definition: funcarg.hpp:80
Coordinate mapper and tensor pair.
Definition: funcarg.hpp:21
void visit(teq::iFunctor *func) override
Implementation of iTraveler.
Definition: teq_csv.hpp:80
teq::iFunctor * func_
Definition: teq_csv.hpp:176
Definition: teq_csv.hpp:39
Interface to travel through graph, treating iLeaf and iFunctor differently.
Definition: itensor.hpp:24
GetTypeF get_ftype_
Definition: teq_csv.hpp:200
teq::iCoordMap * coorder_
Definition: teq_csv.hpp:180
void to_stream(std::ostream &out)
Stream visited graphs to out.
Definition: teq_csv.hpp:129
std::unordered_map< teq::iTensor *, Node > nodes_
Definition: teq_csv.hpp:196
std::unordered_map< teq::iTensor *, std::string > LabelsMapT
Map tensor to label.
Definition: teq.hpp:20
void visit(teq::iLeaf *leaf) override
Implementation of iTraveler.
Definition: teq_csv.hpp:55
std::string to_string(teq::CoordptrT c)
Return brief hashable string representation of coordinate mapper.
teq::iTensor * child_
Definition: teq_csv.hpp:178
Definition: teq_csv.hpp:40
std::string edge_label_
Definition: teq_csv.hpp:182
std::function< NODE_TYPE(teq::iFunctor *)> GetTypeF
Function that identify functors by NODE_TYPE.
Definition: teq_csv.hpp:45
virtual std::string to_string(void) const =0
Return the string representation of the tensor.
Use CSVEquation to render teq::TensptrT graph to output csv edges.
Definition: teq_csv.hpp:48
CSVEquation(GetTypeF get_ftype=[](teq::iFunctor *func) { return FUNCTOR;})
Definition: teq_csv.hpp:50
Interface of traversible and differentiable nodes with shape information.
Definition: itensor.hpp:36
std::string label_
Definition: teq_csv.hpp:187
Definition: teq_csv.hpp:185
static void multiline_replace(std::string &multiline)
Definition: teq_csv.hpp:26
Definition: teq_csv.hpp:174
Leaf of the graph commonly representing the variable in an equation.
Definition: ileaf.hpp:19
bool is_identity(iCoordMap *coorder)
Checks if the coord mapper is an identity mapper.
virtual const Shape & shape(void) const =0
Return the shape held by this tensor.