CGRA-ME
Mapping.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #ifndef __MAPPING_H___
12 #define __MAPPING_H___
13 
14 #include <vector>
15 #include <iostream>
16 #include <unordered_map>
17 #include <unordered_set>
18 
19 class Mapping;
20 
21 #include <CGRA/CGRA.h>
22 #include <CGRA/ConfigStore.h>
23 #include <CGRA/MRRG.h>
24 #include <CGRA/OpGraph.h>
25 
26 enum class MappingStatus : int {
28 };
29 std::ostream& operator<<(std::ostream& os, MappingStatus ms);
30 
31 class Mapping
32 {
33  public:
34  Mapping(std::shared_ptr<CGRA> cgra, int II, std::shared_ptr<OpGraph> opgraph);
35  ~Mapping();
36 
37  bool isMapped() const { return status == MappingStatus::success; }
38  bool isTimeout() const { return status == MappingStatus::timeout; }
39  MappingStatus getStatus() const { return status; }
40  void setStatus(MappingStatus new_status) { status = new_status; }
41 
42  CGRA * getCGRA() const;
43  auto getCGRA_shared() const { return cgra; }
44  int getII() const;
45 
46  // accessor functions to the mapping
47  std::map<OpGraph::NodeDescriptor,std::vector<MRRG::NodeDescriptor>>& getMapping() { return mapping; }
48  const std::map<OpGraph::NodeDescriptor,std::vector<MRRG::NodeDescriptor>>& getMapping() const { return mapping; }
49 
50  bool hasMapping(OpGraph::NodeDescriptor key) const { return mapping.find(key) != end(mapping); }
51  void assertHasMapping(OpGraph::NodeDescriptor key) const { if (not hasMapping(key)) { make_and_throw<cgrame_error>([&](auto&&s) { s << "op " << key << " not mapped"; }); } }
52 
53  bool hasSingleMapping(OpGraph::NodeDescriptor key) const { hasMapping(key); return mapping.at(key).size() == 1; }
54  void assertHasSingleMapping(OpGraph::NodeDescriptor key) const { assertHasMapping(key); if (not hasSingleMapping(key)) { make_and_throw<cgrame_error>([&](auto&&s) { s << "op " << key << " has multiple mappings"; }); } }
55 
57  const std::vector<MRRG::NodeDescriptor>& getMappingList(OpGraph::NodeDescriptor key) const { return mapping.at(key); }
58 
59  void setMapping(std::map<OpGraph::NodeDescriptor,std::vector<MRRG::NodeDescriptor>> mapping);
60  void setNodeMapping(OpGraph::NodeDescriptor, std::vector<MRRG::NodeDescriptor> mapping);
61 
64 
65  /*
66  * Restore this object to the just-constructed state.
67  * No mappings, and not mapped, but other data is left alone.
68  */
69  void clear();
70 
71  // Result printing function
72  void outputMapping(std::ostream & o = std::cout) const { outputMapping(*opgraph, o); }
73  void outputMapping(const OpGraph& opgraph, std::ostream & o) const;
74  void outputDetailedMapping(std::ostream & o = std::cout) const;
75  void outputValMapping(OpGraphVal* val, std::ostream & o = std::cout) const;
76 
77  void check() const;
78 
79  // mapping verification
81 
82  OpGraph& getOpGraph() { return *opgraph; }
83  const OpGraph& getOpGraph() const { return *opgraph; }
84 
85  template<typename F>
86  void forEachMappingList(F&& f) const {
87  for (const auto& og_node_and_mappings : mapping) {
88  std::forward<F>(f)(og_node_and_mappings.first, og_node_and_mappings.second);
89  }
90  }
91 
92  template<typename F>
93  void forEachMapping(F&& f) const {
94  return forEachMappingList([&](auto&& og_node, auto&& mappings) {
95  for (auto& mrrg_node : mappings) {
96  std::forward<F>(f)(og_node, mrrg_node);
97  }
98  });
99  }
100 
101  private:
102  std::map<OpGraph::NodeDescriptor,std::vector<MRRG::NodeDescriptor>> mapping = {};
103  // TODO: add fanout-details
104  std::shared_ptr<CGRA> cgra;
105  int II;
106  std::shared_ptr<OpGraph> opgraph;
107 
109  public:
111 };
112 
119  const Mapping& src,
120  const std::unordered_map<OpGraph::NodeDescriptor, OpGraph::NodeDescriptor>& forward_mappings
121 );
122 
123 
124 /******** Graph of the Mapping ************/
127 };
128 
130 public:
131  struct NodeDescriptor { std::ptrdiff_t id = 0;
132  bool operator==(const NodeDescriptor& rhs) const {
133  return this->id == rhs.id;
134  }
135  bool operator< (const NodeDescriptor& rhs) const {
136  return this->id < rhs.id;
137  }
138  };
139 
141  std::size_t operator()(const NodeDescriptor& ndesc) const {
142  return std::hash<decltype(NodeDescriptor::id)> {}(ndesc.id);
143  }
144  };
145 
146  using ToMRRGVertexMap = std::unordered_map<NodeDescriptor, MRRG::NodeDescriptor, NodeDescriptorHash>;
147 
148  MappingGraph() = default;
149  MappingGraph(const MappingGraph& src) = default;
150  MappingGraph(MappingGraph&&) = default;
151 
152  MappingGraph& operator=(const MappingGraph& rhs) = default;
153  MappingGraph& operator=(MappingGraph&&) = default;
154 
159  std::pair<NodeDescriptor,bool> insert(MappingGraphNode node);
160  std::pair<NodeDescriptor,bool> insert(NodeDescriptor fanin, MappingGraphNode node) { return insertMultiFanin(std::array<NodeDescriptor,1>{{fanin}}, std::move(node)); }
161 
163  return nodes.find(node) != nodes.end();
164  }
165  bool contains(std::ptrdiff_t id) {
166  return nodes.find({id}) != nodes.end();
167  }
168 
169  template<typename NodeDescList = std::vector<NodeDescriptor>>
170  std::pair<NodeDescriptor,bool> insertMultiFanin(NodeDescList fanins, MappingGraphNode node) {
171  auto ir = insert(std::move(node));
172  if (ir.second) for (auto& fanin : fanins) link(fanin, ir.first);
173  return ir;
174  }
175 
184 
189 
194  MappingGraphNode& getNodeRef(NodeDescriptor ndesc);
195  const MappingGraphNode& getNodeRef(NodeDescriptor ndesc) const;
196 
201  auto& fanout(NodeDescriptor ndesc) const { return fanout_lists.at(ndesc); }
202 
207  auto& fanin(const NodeDescriptor ndesc) const { return fanin_lists.at(ndesc); }
208 
214  struct VerifyMessage {
215  enum class Type : char { Info, Warning, Error, };
217  std::string message;
218  VerifyMessage(Type t, std::string s) : type(t), message(s) {}
219  };
220 
221  std::vector<VerifyMessage> verify(const MRRG& mrrg, const ToMRRGVertexMap& toMRRG) const;
222 
227  void printDot(std::ostream& os, const MRRG& mrrg, const OpGraph& opgraph, const ToMRRGVertexMap& toMRRG, const ConfigStore& archAttributes) const;
228 
232  std::ptrdiff_t size() const;
233 
234  auto& allNodes() const { return nodes; }
235 
236 private:
238  std::unordered_map<NodeDescriptor, MappingGraphNode, NodeDescriptorHash> nodes = {};
239  std::unordered_set<int> deletedIds = {};
240 
241  std::unordered_map<NodeDescriptor, std::vector<NodeDescriptor>, NodeDescriptorHash> fanout_lists = {};
242  std::unordered_map<NodeDescriptor, std::vector<NodeDescriptor>, NodeDescriptorHash> fanin_lists = {};
243 
244 };
245 
248  using FromMRRG = std::unordered_map<MRRG::NodeDescriptor, MappingGraph::NodeDescriptor>;
249 
253 };
254 
259 
265 
266 using MappingGraphCycleAssignment = std::unordered_map<MappingGraph::NodeDescriptor, int, MappingGraph::NodeDescriptorHash>;
270 std::pair<bool, MappingGraphCycleAssignment> latencyCheck(
271  const MappingGraph& mapping, const MRRG& mrrg,
273 );
274 
275 #endif
Mapping::Mapping
Mapping(std::shared_ptr< CGRA > cgra, int II, std::shared_ptr< OpGraph > opgraph)
Definition: Mapping.cpp:37
Mapping::II
int II
Definition: Mapping.h:105
MappingGraph::insert
std::pair< NodeDescriptor, bool > insert(NodeDescriptor fanin, MappingGraphNode node)
Definition: Mapping.h:160
CreateMappingGraphResult::mg
MappingGraph mg
Definition: Mapping.h:250
MappingGraph::VerifyMessage::Type
Type
Definition: Mapping.h:215
withRemappedOps
Mapping withRemappedOps(const Mapping &src, const std::unordered_map< OpGraph::NodeDescriptor, OpGraph::NodeDescriptor > &forward_mappings)
Definition: Mapping.cpp:288
OpGraph.h
MappingGraph::fanin
auto & fanin(const NodeDescriptor ndesc) const
Definition: Mapping.h:207
MRRG
Definition: MRRG.h:216
Mapping::check
void check() const
Definition: Mapping.cpp:284
Mapping::getSingleMapping
MRRG::NodeDescriptor getSingleMapping(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:56
Mapping::getMapping
std::map< OpGraph::NodeDescriptor, std::vector< MRRG::NodeDescriptor > > & getMapping()
Definition: Mapping.h:47
MappingGraph::deletedIds
std::unordered_set< int > deletedIds
Definition: Mapping.h:239
operator<<
std::ostream & operator<<(std::ostream &os, MappingStatus ms)
Definition: Mapping.cpp:28
createMappingGraph
CreateMappingGraphResult createMappingGraph(const MRRG &mg, const Mapping &m)
Definition: Mapping.cpp:468
CreateMappingGraphResult::ToMRRG
MappingGraph::ToMRRGVertexMap ToMRRG
Definition: Mapping.h:247
Mapping::solve_time_in_seconds
double solve_time_in_seconds
Definition: Mapping.h:110
ConfigStore.h
MappingGraph::VerifyMessage::message
std::string message
Definition: Mapping.h:217
MappingGraph::NodeDescriptorHash
Definition: Mapping.h:140
CGRA
Definition: CGRA.h:76
Mapping::getCGRA_shared
auto getCGRA_shared() const
Definition: Mapping.h:43
MappingGraph::NodeDescriptor::operator==
bool operator==(const NodeDescriptor &rhs) const
Definition: Mapping.h:132
Mapping::getOpGraph
OpGraph & getOpGraph()
Definition: Mapping.h:82
Mapping::opgraph
std::shared_ptr< OpGraph > opgraph
Definition: Mapping.h:106
Mapping::getMappingList
const std::vector< MRRG::NodeDescriptor > & getMappingList(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:57
Mapping::outputMapping
void outputMapping(std::ostream &o=std::cout) const
Definition: Mapping.h:72
Mapping::setMapping
void setMapping(std::map< OpGraph::NodeDescriptor, std::vector< MRRG::NodeDescriptor >> mapping)
Definition: Mapping.cpp:86
ConfigStore
Definition: ConfigStore.h:76
Mapping::assertHasMapping
void assertHasMapping(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:51
MappingGraphNode::op_node_desc
OpGraph::NodeDescriptor op_node_desc
Definition: Mapping.h:126
Mapping::getStatus
MappingStatus getStatus() const
Definition: Mapping.h:39
MappingGraph::printDot
void printDot(std::ostream &os, const MRRG &mrrg, const OpGraph &opgraph, const ToMRRGVertexMap &toMRRG, const ConfigStore &archAttributes) const
Definition: Mapping.cpp:419
MappingGraph::NodeDescriptor::id
std::ptrdiff_t id
Definition: Mapping.h:131
MappingGraph::VerifyMessage
Definition: Mapping.h:214
MappingGraph::fanout_lists
std::unordered_map< NodeDescriptor, std::vector< NodeDescriptor >, NodeDescriptorHash > fanout_lists
Definition: Mapping.h:241
MappingGraph
Definition: Mapping.h:129
Mapping::setNodeMapping
void setNodeMapping(OpGraph::NodeDescriptor, std::vector< MRRG::NodeDescriptor > mapping)
Definition: Mapping.cpp:91
Mapping::mapMRRGNode
void mapMRRGNode(OpGraph::NodeDescriptor, MRRG::NodeDescriptor node)
Definition: Mapping.cpp:58
MappingGraphNode
Definition: Mapping.h:125
MappingGraph::allNodes
auto & allNodes() const
Definition: Mapping.h:234
CreateMappingGraphResult
Definition: Mapping.h:246
Mapping::hasMapping
bool hasMapping(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:50
Mapping
Definition: Mapping.h:31
Mapping::getCGRA
CGRA * getCGRA() const
Definition: Mapping.cpp:48
MappingGraph::VerifyMessage::type
Type type
Definition: Mapping.h:216
MappingGraph::erase
void erase(MappingGraph::NodeDescriptor n)
Definition: Mapping.cpp:340
Mapping::assertHasSingleMapping
void assertHasSingleMapping(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:54
Mapping::outputValMapping
void outputValMapping(OpGraphVal *val, std::ostream &o=std::cout) const
Definition: Mapping.cpp:266
MappingGraph::NodeDescriptor
Definition: Mapping.h:131
Mapping::status
MappingStatus status
Definition: Mapping.h:108
MappingGraph::operator=
MappingGraph & operator=(const MappingGraph &rhs)=default
Mapping::verifyOpGraphMappingConnectivity
bool verifyOpGraphMappingConnectivity()
Definition: Mapping.cpp:122
MappingStatus::timeout
@ timeout
MappingGraph::NodeDescriptorHash::operator()
std::size_t operator()(const NodeDescriptor &ndesc) const
Definition: Mapping.h:141
MappingGraph::fanin_lists
std::unordered_map< NodeDescriptor, std::vector< NodeDescriptor >, NodeDescriptorHash > fanin_lists
Definition: Mapping.h:242
MappingGraph::VerifyMessage::Type::Error
@ Error
MappingStatus::failure
@ failure
CreateMappingGraphResult::FromMRRG
std::unordered_map< MRRG::NodeDescriptor, MappingGraph::NodeDescriptor > FromMRRG
Definition: Mapping.h:248
MappingGraph::VerifyMessage::VerifyMessage
VerifyMessage(Type t, std::string s)
Definition: Mapping.h:218
MappingGraph::VerifyMessage::Type::Info
@ Info
MappingGraph::contains
bool contains(std::ptrdiff_t id)
Definition: Mapping.h:165
MappingGraph::fanout
auto & fanout(NodeDescriptor ndesc) const
Definition: Mapping.h:201
MappingGraph::contains
bool contains(NodeDescriptor node)
Definition: Mapping.h:162
CreateMappingGraphResult::fromMRRG
FromMRRG fromMRRG
Definition: Mapping.h:251
OpGraphNode
Definition: OpGraph.h:113
Mapping::cgra
std::shared_ptr< CGRA > cgra
Definition: Mapping.h:104
Mapping::forEachMappingList
void forEachMappingList(F &&f) const
Definition: Mapping.h:86
Mapping::hasSingleMapping
bool hasSingleMapping(OpGraph::NodeDescriptor key) const
Definition: Mapping.h:53
Mapping::getII
int getII() const
Definition: Mapping.cpp:53
MappingStatus
MappingStatus
Definition: Mapping.h:26
CGRA.h
MappingGraph::ToMRRGVertexMap
std::unordered_map< NodeDescriptor, MRRG::NodeDescriptor, NodeDescriptorHash > ToMRRGVertexMap
Definition: Mapping.h:146
OpGraphVal
Definition: OpGraph.h:178
end
auto end(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:138
MappingGraph::VerifyMessage::Type::Warning
@ Warning
MRRGNode
Definition: MRRG.h:60
Mapping::mapping
std::map< OpGraph::NodeDescriptor, std::vector< MRRG::NodeDescriptor > > mapping
Definition: Mapping.h:102
Mapping::clear
void clear()
Definition: Mapping.cpp:95
MappingGraph::verify
std::vector< VerifyMessage > verify(const MRRG &mrrg, const ToMRRGVertexMap &toMRRG) const
Definition: Mapping.cpp:367
MappingGraph::nextId
NodeDescriptor nextId
Definition: Mapping.h:237
MappingGraph::size
std::ptrdiff_t size() const
Definition: Mapping.cpp:313
MappingGraph::link
void link(MappingGraph::NodeDescriptor driver, MappingGraph::NodeDescriptor fanout)
Definition: Mapping.cpp:317
MappingStatus::success
@ success
Mapping::outputDetailedMapping
void outputDetailedMapping(std::ostream &o=std::cout) const
Definition: Mapping.cpp:243
Mapping::getOpGraph
const OpGraph & getOpGraph() const
Definition: Mapping.h:83
Mapping::forEachMapping
void forEachMapping(F &&f) const
Definition: Mapping.h:93
MappingGraph::getNodeRef
MappingGraphNode & getNodeRef(NodeDescriptor ndesc)
Definition: Mapping.cpp:359
MappingGraph::insertMultiFanin
std::pair< NodeDescriptor, bool > insertMultiFanin(NodeDescList fanins, MappingGraphNode node)
Definition: Mapping.h:170
MappingGraph::insert
std::pair< NodeDescriptor, bool > insert(MappingGraphNode node)
Definition: Mapping.cpp:303
Mapping::isMapped
bool isMapped() const
Definition: Mapping.h:37
MappingGraph::MappingGraph
MappingGraph()=default
Mapping::~Mapping
~Mapping()
Definition: Mapping.cpp:44
CreateMappingGraphResult::toMRRG
ToMRRG toMRRG
Definition: Mapping.h:252
removeIsolatedRoutingNodes
MappingGraph removeIsolatedRoutingNodes(const MappingGraph &mapping, const MRRG &mrrg, const MappingGraph::ToMRRGVertexMap &toMRRG)
Definition: Mapping.cpp:504
MappingGraph::NodeDescriptor::operator<
bool operator<(const NodeDescriptor &rhs) const
Definition: Mapping.h:135
Mapping::unmapMRRGNode
void unmapMRRGNode(OpGraph::NodeDescriptor, MRRG::NodeDescriptor node)
Definition: Mapping.cpp:70
MRRG.h
Mapping::isTimeout
bool isTimeout() const
Definition: Mapping.h:38
MappingGraph::unlink
void unlink(MappingGraph::NodeDescriptor driver, MappingGraph::NodeDescriptor fanout)
Definition: Mapping.cpp:327
OpGraph
Definition: OpGraph.h:215
latencyCheck
std::pair< bool, MappingGraphCycleAssignment > latencyCheck(const MappingGraph &mapping, const MRRG &mrrg, const CreateMappingGraphResult::ToMRRG &toMRRG)
Definition: Mapping.cpp:582
MappingGraph::nodes
std::unordered_map< NodeDescriptor, MappingGraphNode, NodeDescriptorHash > nodes
Definition: Mapping.h:238
Mapping::setStatus
void setStatus(MappingStatus new_status)
Definition: Mapping.h:40
Mapping::getMapping
const std::map< OpGraph::NodeDescriptor, std::vector< MRRG::NodeDescriptor > > & getMapping() const
Definition: Mapping.h:48
MappingGraphCycleAssignment
std::unordered_map< MappingGraph::NodeDescriptor, int, MappingGraph::NodeDescriptorHash > MappingGraphCycleAssignment
Definition: Mapping.h:266