9 #include "estd/estd.hpp" 10 #include "estd/range.hpp" 12 #include "teq/ileaf.hpp" 15 #ifndef TEQ_TRAVELER_HPP 16 #define TEQ_TRAVELER_HPP 29 if (
false == estd::has(
visited_, leaf))
39 if (
false == estd::has(
visited_, func))
62 graphsize_.emplace(leaf, estd::NumRange<size_t>());
71 size_t nchildren = children.size();
72 std::vector<size_t> max_heights;
73 std::vector<size_t> min_heights;
74 max_heights.reserve(nchildren);
75 min_heights.reserve(nchildren);
76 for (
auto& child : children)
78 iTensor* tens = child.get_tensor().get();
80 estd::NumRange<size_t> range = estd::must_getf(
graphsize_, tens,
81 "GraphStat failed to visit child `%s` of functor `%s`",
83 max_heights.push_back(range.upper_);
84 min_heights.push_back(range.lower_);
86 size_t max_height = 1;
87 size_t min_height = 1;
88 auto max_it = std::max_element(
89 max_heights.begin(), max_heights.end());
90 auto min_it = std::min_element(
91 min_heights.begin(), min_heights.end());
92 if (max_heights.end() != max_it)
94 max_height += *max_it;
96 if (min_heights.end() != max_it)
98 min_height += *min_it;
100 graphsize_.emplace(func, estd::NumRange<size_t>(min_height, max_height));
105 std::unordered_map<iTensor*,estd::NumRange<size_t>>
graphsize_;
109 using ParentMapT = std::unordered_map<iTensor*,std::vector<size_t>>;
126 if (
false == estd::has(
parents_, func))
129 size_t n = children.size();
130 std::unordered_set<size_t> path;
131 for (
size_t i = 0; i < n; ++i)
133 TensptrT tens = children[i].get_tensor();
141 if (estd::has(
parents_, tens.get()))
147 if (
false == path.empty())
149 parents_[func] = std::vector<size_t>(path.begin(), path.end());
173 if (
false == estd::has(
parents_, func))
176 for (
size_t i = 0, n = children.size(); i < n; ++i)
178 auto& child = children[i];
179 auto tens = child.get_tensor();
181 parents_[tens.get()][func].push_back(i);
193 using OwnerMapT = std::unordered_map<iTensor*,TensrefT>;
210 std::vector<size_t> root_heights;
211 root_heights.reserve(roots.size());
212 std::transform(roots.begin(), roots.end(),
213 std::back_inserter(root_heights),
219 size_t maxheight = *std::max_element(
220 root_heights.begin(), root_heights.end());
221 funcs_ = std::vector<std::unordered_set<iFunctor*>>(maxheight);
225 auto tens = gpair.first;
226 size_t height = gpair.second.upper_;
229 leaves_.emplace(static_cast<iLeaf*>(tens));
233 funcs_[height - 1].emplace(static_cast<iFunctor*>(tens));
242 std::vector<std::unordered_set<iFunctor*>>
funcs_;
247 #endif // TEQ_TRAVELER_HPP void visit(iFunctor *func) override
Implementation of iTraveler.
Definition: traveler.hpp:66
std::unordered_map< iTensor *, estd::NumRange< size_t > > graphsize_
Definition: traveler.hpp:105
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::vector< FuncArg > ArgsT
Type of functor arguments.
Definition: funcarg.hpp:101
std::unordered_map< iTensor *, ParentMapT > parents_
Definition: traveler.hpp:189
void visit(iFunctor *func) override
Implementation of iTraveler.
Definition: traveler.hpp:171
void visit(iLeaf *leaf) override
Implementation of iTraveler.
Definition: traveler.hpp:60
std::unordered_set< iTensor * > visited_
Set of tensors visited.
Definition: traveler.hpp:53
Traveler that for each child tracks the relationship to all parents.
Definition: traveler.hpp:162
HeightMatrix(const TensptrsT &roots)
Definition: traveler.hpp:202
Definition: traveler.hpp:116
Traveler that maps each tensor to its subtree's maximum depth.
Definition: traveler.hpp:57
virtual void visit_leaf(iLeaf *leaf)=0
Do something during unique visit to leaf.
PathFinder(const iTensor *target)
Definition: traveler.hpp:118
void visit(iFunctor *func) override
Implementation of iTraveler.
Definition: traveler.hpp:124
Interface to travel through graph, treating iLeaf and iFunctor differently.
Definition: itensor.hpp:24
std::unordered_map< iTensor *, TensrefT > OwnerMapT
Map between tensor and its corresponding smart pointer.
Definition: traveler.hpp:193
const iTensor * target_
Target of tensor all paths are travelling to.
Definition: traveler.hpp:155
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
std::vector< TensptrT > TensptrsT
Vector of tensor smart pointers.
Definition: itensor.hpp:60
void visit(iLeaf *leaf) override
Implementation of iTraveler.
Definition: traveler.hpp:165
std::vector< std::unordered_set< iFunctor * > > funcs_
Functors of subgraph ordered by ascending maximum height.
Definition: traveler.hpp:242
std::unordered_map< iTensor *, std::vector< size_t > > ParentMapT
Map tensors to indices of children.
Definition: traveler.hpp:109
Leaves set and sets of functors ordered by height (by ascending order)
Definition: traveler.hpp:200
void visit(iLeaf *leaf) override
Implementation of iTraveler.
Definition: traveler.hpp:27
virtual std::string to_string(void) const =0
Return the string representation of the tensor.
Interface of traversible and differentiable nodes with shape information.
Definition: itensor.hpp:36
virtual void accept(iTraveler &visiter)=0
Obtain concrete information on either leaf or functor implementations.
Extremely generic traveler that visits every node in the graph once.
Definition: traveler.hpp:22
void visit(iLeaf *leaf) override
Implementation of iTraveler.
Definition: traveler.hpp:121
virtual ~OnceTraveler(void)=default
ParentMapT parents_
Map of parent to child indices that lead to target tensor.
Definition: traveler.hpp:158
std::unordered_set< iLeaf * > leaves_
Leaves of specified subroot.
Definition: traveler.hpp:239
void visit(iFunctor *func) override
Implementation of iTraveler.
Definition: traveler.hpp:37
virtual void visit_func(iFunctor *func)=0
Do something during unique visit to functor.
OwnerMapT track_owners(TensptrsT roots)
Leaf of the graph commonly representing the variable in an equation.
Definition: ileaf.hpp:19