Tenncor
group.hpp
Go to the documentation of this file.
1 
8 #include <boost/uuid/uuid.hpp>
9 #include <boost/uuid/uuid_generators.hpp>
10 #include <boost/uuid/uuid_io.hpp>
11 
12 #include "tag/tag.hpp"
13 
14 #ifndef TAG_GROUP_HPP
15 #define TAG_GROUP_HPP
16 
17 namespace tag
18 {
19 
21 using TensSetT = std::unordered_set<TensKey,TensKeyHash>;
22 
29 struct GroupTag final : public iTag
30 {
31  GroupTag (std::string init_label) : labels_({init_label}) {}
32 
34  size_t tag_id (void) const override
35  {
36  return tag_id_;
37  }
38 
40  void absorb (TagptrT&& other) override
41  {
42  std::set<std::string>& olabels =
43  static_cast<GroupTag*>(other.get())->labels_;
44  labels_.insert(olabels.begin(), olabels.end());
45  }
46 
48  TagRepsT get_tags (void) const override;
49 
50 private:
51  std::set<std::string> labels_;
52 
53  static size_t tag_id_;
54 };
55 
58 struct GroupRegistry final
59 {
60  GroupRegistry (TagRegistry& registry = get_reg()) : tag_reg_(registry) {}
61 
63  void group_tag (teq::TensrefT tens, std::string tag)
64  {
65  tag_reg_.add_tag(tens, TagptrT(new GroupTag(tag)));
66 
67  auto& gtens = groups_[tag];
68  auto it = gtens.find(TensKey(tens.lock().get()));
69  // clear out previous entry that is expired
70  if (gtens.end() != it && it->expired())
71  {
72  gtens.erase(tens.lock().get());
73  }
74  gtens.emplace(tens);
75  }
76 
78  std::unordered_map<std::string,TensSetT> groups_;
79 
82 };
83 
86 
88 const std::string groups_key = get_reg().register_tagr("groups",
89 [](teq::TensrefT ref, std::string tag)
90 {
91  get_group_reg().group_tag(ref, tag);
92 });
93 
96 void recursive_group_tag (teq::TensptrT tens, std::string group,
97  teq::TensSetT stops,
98  GroupRegistry& registry = get_group_reg());
99 
102 using AGroupsT = std::map<std::string,std::unordered_set<std::string>>;
103 
105 using AdjMapT = std::unordered_map<teq::iTensor*,AGroupsT>;
106 
109 void adjacencies (AdjMapT& out, teq::TensptrsT roots,
110  GroupRegistry& registry = get_group_reg());
111 
113 struct Subgraph final : public teq::iTraveler
114 {
115  Subgraph (std::string group) : group_(group) {}
116 
118  void visit (teq::iLeaf* leaf) override
119  {
120  if (false == estd::has(content_, leaf))
121  {
122  content_.emplace(leaf);
123  children_.erase(leaf);
124  }
125  }
126 
128  void visit (teq::iFunctor* func) override
129  {
130  if (false == estd::has(content_, func))
131  {
132  content_.emplace(func);
133  children_.erase(func);
134 
135  auto& children = func->get_children();
136  for (auto& child : children)
137  {
138  auto tens = child.get_tensor();
139  if (false == estd::has(content_, tens.get()))
140  {
141  children_.emplace(tens.get(), tens);
142  }
143  }
144  }
145  }
146 
148  std::string group_;
149 
152 
153  // todo: order subgraphs children somehow
155  std::unordered_map<teq::iTensor*,teq::TensptrT> children_;
156 };
157 
159 using SgraphptrT = std::shared_ptr<Subgraph>;
160 
162 using SubgraphsT = std::unordered_set<SgraphptrT>;
163 
165 using SubgraphAssocsT = std::unordered_map<teq::iTensor*,SubgraphsT>;
166 
170 void beautify_groups (SubgraphAssocsT& out, const AdjMapT& adjs);
171 
174 void filter_head (SubgraphAssocsT& out, const SubgraphAssocsT& assocs);
175 
176 }
177 
178 #endif // TAG_GROUP_HPP
std::unordered_set< teq::iTensor * > TensSetT
Hash set of raw tensor pointers.
Definition: itensor.hpp:63
void visit(teq::iLeaf *leaf) override
Implementation of iTraveler.
Definition: group.hpp:118
void beautify_groups(SubgraphAssocsT &out, const AdjMapT &adjs)
void absorb(TagptrT &&other) override
Implementation of iTag.
Definition: group.hpp:40
virtual const ArgsT & get_children(void) const =0
Return children nodes as a vector of raw pointers.
std::unordered_map< teq::iTensor *, teq::TensptrT > children_
Children of the group.
Definition: group.hpp:155
const std::string groups_key
Identifier of groups tag.
Definition: group.hpp:88
Definition: tag.hpp:25
Subgraph(std::string group)
Definition: group.hpp:115
Interface of iOperation-defined operation node.
Definition: ifunctor.hpp:28
Registry for associating tensors to tag collectives.
Definition: tag.hpp:165
GroupTag(std::string init_label)
Definition: group.hpp:31
std::unordered_map< teq::iTensor *, AGroupsT > AdjMapT
Map tensors to groups.
Definition: group.hpp:105
void visit(teq::iFunctor *func) override
Implementation of iTraveler.
Definition: group.hpp:128
GroupRegistry(TagRegistry &registry=get_reg())
Definition: group.hpp:60
Definition: group.hpp:17
std::unique_ptr< iTag > TagptrT
Unique pointer of tag.
Definition: tag.hpp:40
void adjacencies(AdjMapT &out, teq::TensptrsT roots, GroupRegistry &registry=get_group_reg())
std::map< std::string, std::vector< std::string > > TagRepsT
Map tag key to a series of labels.
Definition: tag.hpp:21
Subgraph group encapsulation.
Definition: group.hpp:113
std::string register_tagr(std::string tag_key, TagrF tagr)
Definition: tag.hpp:232
std::string group_
Group label.
Definition: group.hpp:148
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
size_t tag_id(void) const override
Implementation of iTag.
Definition: group.hpp:34
std::unordered_set< SgraphptrT > SubgraphsT
Set of subgraphs.
Definition: group.hpp:162
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
TagRegistry & tag_reg_
Internal tag registry used to retrieve tensor-group association.
Definition: group.hpp:81
Tensor ref key wrapper.
Definition: tag.hpp:114
TagRegistry & get_reg(void)
Return reference to global tag registry.
void recursive_group_tag(teq::TensptrT tens, std::string group, teq::TensSetT stops, GroupRegistry &registry=get_group_reg())
std::map< std::string, std::unordered_set< std::string > > AGroupsT
Definition: group.hpp:102
void group_tag(teq::TensrefT tens, std::string tag)
Bidirectionally assocate tensor and group.
Definition: group.hpp:63
Definition: group.hpp:58
teq::TensSetT content_
Group content.
Definition: group.hpp:151
void filter_head(SubgraphAssocsT &out, const SubgraphAssocsT &assocs)
void add_tag(teq::TensrefT tens, TagptrT tag)
Add tag to collective referenced by tens.
Definition: tag.hpp:168
GroupRegistry & get_group_reg(void)
Return reference to global group registry.
std::unordered_set< TensKey, TensKeyHash > TensSetT
Tensor ref set.
Definition: group.hpp:21
std::shared_ptr< Subgraph > SgraphptrT
Smart pointer of the subgraph.
Definition: group.hpp:159
TagRepsT get_tags(void) const override
Implementation of iTag.
std::weak_ptr< iTensor > TensrefT
Tensor weak pointers.
Definition: itensor.hpp:54
Definition: group.hpp:29
std::unordered_map< std::string, TensSetT > groups_
Map group label to tensors.
Definition: group.hpp:78
static size_t tag_id_
Definition: group.hpp:53
std::set< std::string > labels_
Definition: group.hpp:51
Leaf of the graph commonly representing the variable in an equation.
Definition: ileaf.hpp:19