CGRA-ME
MRRGProcedures.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 
16 #ifndef MRRG_PROCEDURES__H_
17 #define MRRG_PROCEDURES__H_
18 
19 #include <CGRA/MRRG.h>
23 #include <CGRA/Mapping.h>
24 
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 
29 /*******************
30  * Begin forward declarations and simple/small types
31  *******************/
32 
33 struct InterMRRGMap;
36 struct NodeExclusions;
37 
39  std::vector<MRRG::NodeDescriptor> function_nodes = {};
40  std::vector<MRRG::NodeDescriptor> routing_nodes = {};
41 };
42 
44  std::vector<std::vector<MRRG::NodeDescriptor>> function_nodes = {};
45  std::vector<std::vector<MRRG::NodeDescriptor>> routing_nodes = {};
46 };
47 
49  struct NodeInfo {
51  int distance;
52 
53  operator MRRG::NodeDescriptor() const { return node; }
54  bool operator==(const MRRG::NodeDescriptor& ndesc) const { return node == ndesc; }
55  };
56  using NodeInfoList = std::unordered_map<MRRG::NodeDescriptor, NodeInfo>;
57 
59  const NodeInfo& getInfoFor(MRRG::NodeDescriptor src, MRRG::NodeDescriptor dest) const { return neighbours.at(src).at(dest); }
60  friend std::ostream& operator<<(std::ostream& os, const FunctionalUnitNeighbours& fu_neighbours);
61 
62  std::unordered_map<MRRG::NodeDescriptor, NodeInfoList> neighbours = {};
63 
64  auto begin() const { return neighbours.begin(); }
65  auto end() const { return neighbours.end(); }
66  auto begin() { return neighbours.begin(); }
67  auto end() { return neighbours.end(); }
68 };
69 
71 };
72 
73 /*******************
74  * Begin function prototypes
75  *******************/
76 
81 
86 
90 std::unordered_map<OpGraph::OpDescriptor, std::vector<MRRG::NodeDescriptor>> findAllCompatibleFUs(const OpGraph& opgraph, const MRRG& base_mrrg);
91 
100 FunctionalUnitNeighbours::NodeInfoList findNeighbourFUs(const MRRG& mrrg, MRRG::NodeDescriptor source, int min_neighbours_to_find);
101 
102 template<typename NodeList = std::vector<MRRG::NodeDescriptor>, typename NEIGHBOUR_FINDER = FunctionalUnitNeighbours::NodeInfoList(const MRRG&, MRRG::NodeDescriptor,int)>
103 FunctionalUnitNeighbours findAllNeighbourFUs(const MRRG& mrrg, const NodeList& sources, int min_neighbours_to_find = -1, NEIGHBOUR_FINDER&& neighbour_finder = findNeighbourFUs) {
105  for(auto& n : sources) result.neighbours.emplace(n, std::forward<NEIGHBOUR_FINDER>(neighbour_finder)(mrrg, n, min_neighbours_to_find));
106  return result;
107 }
108 
109 inline auto makeNClosestNeighbourFinder(int min_neighbours) {
110  return [=](const MRRG& mrrg, const std::vector<MRRG::NodeDescriptor>& all_compatible_nodes_list) -> FunctionalUnitNeighbours {
111  auto result = findAllNeighbourFUs(mrrg, all_compatible_nodes_list, min_neighbours);
112  // std::cout << result << std::endl;
113  return result;
114  };
115 }
116 
129 std::vector<std::vector<MRRG::NodeDescriptor>> findNRoutingPathsBetween(
130  int num_paths, const MRRG& mrrg, MRRG::NodeDescriptor source, MRRG::NodeDescriptor sink,
131  std::pair<int,int> cycle_trip_count_min_max, const OperandTag& operand_tag,
132  MRRGProcedureCacheHandle* cache_handle
133 );
134 
148 std::vector<std::vector<MRRG::NodeDescriptor>> mergeMRRGWalks(
149  const MRRG& mrrg, const std::vector<MrrgNodeSpan>& walks);
150 
154 inline auto mergeMRRGWalks(
155  const MRRG& mrrg, const std::vector<std::vector<MRRG::NodeDescriptor>>& walks
156 ) {
157  std::vector<MrrgNodeSpan> with_pointers;
158  with_pointers.reserve(walks.size());
159  auto to_span = [](auto& e) -> MrrgNodeSpan { return {e.data(), e.size()}; };
160  std::transform(walks.begin(), walks.end(), std::back_inserter(with_pointers), to_span);
161  return mergeMRRGWalks(mrrg, with_pointers);
162 }
163 
174 int tripCountOfWalk(const MRRG& mrrg, const std::vector<MRRG::NodeDescriptor>& walk);
175 
180 bool nodeIsCompatibleWithOperand(const MRRG& mrrg, MRRG::NodeDescriptor node, const OperandTag& operand_tag);
181 
187 bool walkIsCompatibleWithOperand(const MRRG& mrrg, const std::vector<MRRG::NodeDescriptor>& walk, const OperandTag& operand_tag);
188 
193 void recordNodeMerge(
194  MRRGTransformationResult& transformation_result, const MRRG& src_mrrg,
196 );
197 
203 
209 
210 /*******************
211  * Simple node/graph functions
212  *******************/
213 
214 bool isNodeFanoutOf(const MRRG& mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc);
215 bool isNodeFaninOf(const MRRG& mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc);
216 void assertNodeIsFanoutOf(const MRRG& mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc, const char* action_name = " -- ");
217 void assertNodeIsFaninOf(const MRRG& mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc, const char* action_name = " -- ");
218 void assertNodeIsExcusiveFanout(const MRRG& mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc, const char* action_name = " -- ");
219 void assertNodeIsExcusiveFanin(const MRRG& mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc, const char* action_name = " -- ");
220 void assertHasNoOps(const MRRGNode& n, const char* action_name = "");
221 
222 void assertHasSingleFanout(const MRRGNode& n, const char* action_name);
223 
227 void mergeNodeConnections(MRRG& mrrg, MRRG::NodeDescriptor into_ndesc, MRRG::NodeDescriptor from_ndesc);
228 
234 
241 
242 /*******************
243  * Begin type definitions
244  *******************/
245 
246 struct InterMRRGMap {
247  using NodeSet = std::unordered_set<MRRG::NodeDescriptor>;
248 
249  // Get the nodes in the tranformed MRRG that correspond to node `n` in the src MRRG
251 
252  // Get the nodes in the src MRRG that correspond to node `n` in the transformed MRRG
254 
256  forward_map[old_node].emplace(new_node);
257  reverse_map[new_node].emplace(old_node);
258  }
259 
260  template<typename NodeList = std::vector<MRRG::NodeDescriptor>>
261  void addMappingMulti(MRRG::NodeDescriptor old_node, const NodeList& new_nodes) { for (auto& nn : new_nodes) { addMapping(old_node, nn); } }
262  template<typename NodeList = std::vector<MRRG::NodeDescriptor>>
263  void addMappingMulti(const NodeList& old_nodes, MRRG::NodeDescriptor new_node) { for (auto& on : old_nodes) { addMapping(on, new_node); } }
264 
265  // Adds mapping from node in src MRRG to its copy in the transformed MRRG.
266  void addAllByMatchingProperties(const MRRG& src_mrrg, const MRRG& transformed_mrrg);
267 
269 
270  bool operator==(const InterMRRGMap& rhs) const { return forward_map == rhs.forward_map && reverse_map == rhs.reverse_map; }
271  friend std::ostream& operator<<(std::ostream& os, const InterMRRGMap& imm);
272 private:
273  std::unordered_map<MRRG::NodeDescriptor, NodeSet> forward_map = {};
274  std::unordered_map<MRRG::NodeDescriptor, NodeSet> reverse_map = {};
276 };
277 
281 };
282 
283 struct FindNeighbourFUsVisitor : DoNothingGraphVisitor<MRRG::NodeDescriptor> {
284  std::unordered_set<MRRG::NodeDescriptor> discovered_fu_nodes = {};
285 
287  return discovered_fu_nodes.find(v) != end(discovered_fu_nodes);
288  }
289 
291  if (fanout->type == MRRG_NODE_FUNCTION || fanout->type == MRRG_NODE_ROUTING_FUNCTION) {
292  discovered_fu_nodes.insert(fanout);
293  }
294  }
295 };
296 
301 Mapping transformToOriginalMRRG(const Mapping & map, const MRRGTransformationResult & transformation);
302 
303 Mapping removeMuxExNodeInsertion(const Mapping & map, const MRRGTransformationResult & transformation);
304 
305 #endif /* MRRG_PROCEDURES__H_ */
makeNClosestNeighbourFinder
auto makeNClosestNeighbourFinder(int min_neighbours)
Definition: MRRGProcedures.h:109
MRRG_NODE_ROUTING_FUNCTION
@ MRRG_NODE_ROUTING_FUNCTION
Definition: MRRG.h:47
MRRGNodeClassLists::function_nodes
std::vector< MRRG::NodeDescriptor > function_nodes
Definition: MRRGProcedures.h:39
isNodeFaninOf
bool isNodeFaninOf(const MRRG &mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc)
Definition: MRRGProcedures.cpp:511
assertNodeIsExcusiveFanin
void assertNodeIsExcusiveFanin(const MRRG &mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc, const char *action_name=" -- ")
Definition: MRRGProcedures.cpp:547
findNeighbourFUs
FunctionalUnitNeighbours::NodeInfoList findNeighbourFUs(const MRRG &mrrg, MRRG::NodeDescriptor source, int min_neighbours_to_find)
Definition: MRRGProcedures.cpp:194
muxExNodeInsertion
MRRGTransformationResult muxExNodeInsertion(const MRRG &src_mrrg)
Definition: MRRGProcedures.cpp:458
MRRGTransformationResult::mapping
InterMRRGMap mapping
Definition: MRRGProcedures.h:280
OperandTag
std::string OperandTag
Definition: OpGraph.h:81
nodeIsCompatibleWithOperand
bool nodeIsCompatibleWithOperand(const MRRG &mrrg, MRRG::NodeDescriptor node, const OperandTag &operand_tag)
Definition: MRRGProcedures.cpp:391
InterMRRGMap
Definition: MRRGProcedures.h:246
MRRG
Definition: MRRG.h:216
InterMRRGMap::operator==
bool operator==(const InterMRRGMap &rhs) const
Definition: MRRGProcedures.h:272
MRRG::NodeDescriptor
const MRRGNode * NodeDescriptor
Definition: MRRG.h:219
FunctionalUnitNeighbours::begin
auto begin() const
Definition: MRRGProcedures.h:64
MRRGNode::type
MRRGNode_Type type
Definition: MRRG.h:107
InterMRRGMap::forward_map
std::unordered_map< MRRG::NodeDescriptor, NodeSet > forward_map
Definition: MRRGProcedures.h:275
mergeNodePropertiesOfExclusiveFanoutToExclusiveFanin
void mergeNodePropertiesOfExclusiveFanoutToExclusiveFanin(MRRG &mrrg, MRRG::NodeDescriptor into_ndesc, MRRG::NodeDescriptor from_ndesc)
Definition: MRRGProcedures.cpp:583
FunctionalUnitNeighbours::NodeInfo::operator==
bool operator==(const MRRG::NodeDescriptor &ndesc) const
Definition: MRRGProcedures.h:54
MRRGNodeClassListsByCycle::routing_nodes
std::vector< std::vector< MRRG::NodeDescriptor > > routing_nodes
Definition: MRRGProcedures.h:45
FindNeighbourFUsVisitor::discovered_fu_nodes
std::unordered_set< MRRG::NodeDescriptor > discovered_fu_nodes
Definition: MRRGProcedures.h:284
GraphAlgorithmHelpers.h
FindNeighbourFUsVisitor
Definition: MRRGProcedures.h:283
InterMRRGMap::empty_node_list
NodeSet empty_node_list
Definition: MRRGProcedures.h:277
recordNodeMerge
void recordNodeMerge(MRRGTransformationResult &transformation_result, const MRRG &src_mrrg, MRRG::NodeDescriptor into, MRRG::NodeDescriptor from)
MrrgNodeSpan::data
MRRG::NodeDescriptor const * data() const
Definition: MRRG.h:376
removeMuxExNodeInsertion
Mapping removeMuxExNodeInsertion(const Mapping &map, const MRRGTransformationResult &transformation)
assertNodeIsExcusiveFanout
void assertNodeIsExcusiveFanout(const MRRG &mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc, const char *action_name=" -- ")
Definition: MRRGProcedures.cpp:539
FunctionalUnitNeighbours::operator<<
friend std::ostream & operator<<(std::ostream &os, const FunctionalUnitNeighbours &fu_neighbours)
Definition: MRRGProcedures.cpp:38
findNRoutingPathsBetween
std::vector< std::vector< MRRG::NodeDescriptor > > findNRoutingPathsBetween(int num_paths, const MRRG &mrrg, MRRG::NodeDescriptor source, MRRG::NodeDescriptor sink, std::pair< int, int > cycle_trip_count_min_max, const OperandTag &operand_tag, MRRGProcedureCacheHandle *cache_handle)
Definition: MRRGProcedures.cpp:248
assertHasNoOps
void assertHasNoOps(const MRRGNode &n, const char *action_name="")
Definition: MRRGProcedures.cpp:555
MRRGProcedureCache.h
MRRGTransformationResult::transformed_mrrg
MRRG transformed_mrrg
Definition: MRRGProcedures.h:279
FunctionalUnitNeighbours::NodeInfo::node
MRRG::NodeDescriptor node
Definition: MRRGProcedures.h:50
FindNeighbourFUsVisitor::onExamineEdge
void onExamineEdge(const MRRG::NodeDescriptor &, const MRRG::NodeDescriptor &fanout)
Definition: MRRGProcedures.h:290
walkIsCompatibleWithOperand
bool walkIsCompatibleWithOperand(const MRRG &mrrg, const std::vector< MRRG::NodeDescriptor > &walk, const OperandTag &operand_tag)
Definition: MRRGProcedures.cpp:396
transformToOriginalMRRG
Mapping transformToOriginalMRRG(const Mapping &map, const MRRGTransformationResult &transformation)
Definition: MRRGProcedures.cpp:607
Mapping
Definition: Mapping.h:31
assertNodeIsFaninOf
void assertNodeIsFaninOf(const MRRG &mrrg, MRRG::NodeDescriptor driven_ndesc, MRRG::NodeDescriptor test_ndesc, const char *action_name=" -- ")
Definition: MRRGProcedures.cpp:533
tripCountOfWalk
int tripCountOfWalk(const MRRG &mrrg, const std::vector< MRRG::NodeDescriptor > &walk)
Definition: MRRGProcedures.cpp:379
InterMRRGMap::newNodesForOldNode
const NodeSet & newNodesForOldNode(MRRG::NodeDescriptor n) const
Definition: MRRGProcedures.h:252
FunctionalUnitNeighbours::begin
auto begin()
Definition: MRRGProcedures.h:66
FunctionalUnitNeighbours::getInfoFor
const NodeInfo & getInfoFor(MRRG::NodeDescriptor src, MRRG::NodeDescriptor dest) const
Definition: MRRGProcedures.h:59
InterMRRGMap::oldNodesForNewNode
const NodeSet & oldNodesForNewNode(MRRG::NodeDescriptor n) const
Definition: MRRGProcedures.h:255
MRRG_NODE_FUNCTION
@ MRRG_NODE_FUNCTION
Definition: MRRG.h:46
mergeMRRGWalks
std::vector< std::vector< MRRG::NodeDescriptor > > mergeMRRGWalks(const MRRG &mrrg, const std::vector< MrrgNodeSpan > &walks)
Definition: MRRGProcedures.cpp:296
InterMRRGMap::addMapping
void addMapping(MRRG::NodeDescriptor old_node, MRRG::NodeDescriptor new_node)
Definition: MRRGProcedures.h:257
isNodeFanoutOf
bool isNodeFanoutOf(const MRRG &mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc)
Definition: MRRGProcedures.cpp:495
FindNeighbourFUsVisitor::shouldIgnore
bool shouldIgnore(const MRRG::NodeDescriptor &v)
Definition: MRRGProcedures.h:286
FunctionalUnitNeighbours
Definition: MRRGProcedures.h:48
reduceLosslessly
MRRGTransformationResult reduceLosslessly(const MRRG &src_mrrg, const MRRGTransformFlags &flags)
Definition: MRRGProcedures.cpp:420
collapseSingleFaninFanoutNodeIntoFanin
void collapseSingleFaninFanoutNodeIntoFanin(MRRG &mrrg, MRRG::NodeDescriptor into_ndesc, MRRG::NodeDescriptor from_ndesc)
FunctionalUnitNeighbours::isReachableFrom
bool isReachableFrom(MRRG::NodeDescriptor src, MRRG::NodeDescriptor dest) const
Definition: MRRGProcedures.cpp:28
InterMRRGMap::addAllByMatchingProperties
void addAllByMatchingProperties(const MRRG &src_mrrg, const MRRG &transformed_mrrg)
Definition: MRRGProcedures.cpp:51
DoNothingGraphVisitor
Definition: GraphAlgorithmHelpers.h:49
Collections.h
end
auto end(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:138
assertNodeIsFanoutOf
void assertNodeIsFanoutOf(const MRRG &mrrg, MRRG::NodeDescriptor driver_ndesc, MRRG::NodeDescriptor test_ndesc, const char *action_name=" -- ")
Definition: MRRGProcedures.cpp:527
InterMRRGMap::reverse_map
std::unordered_map< MRRG::NodeDescriptor, NodeSet > reverse_map
Definition: MRRGProcedures.h:276
MRRGNodeClassLists::routing_nodes
std::vector< MRRG::NodeDescriptor > routing_nodes
Definition: MRRGProcedures.h:40
MRRGNode
Definition: MRRG.h:60
findAllCompatibleFUs
std::unordered_map< OpGraph::OpDescriptor, std::vector< MRRG::NodeDescriptor > > findAllCompatibleFUs(const OpGraph &opgraph, const MRRG &base_mrrg)
Definition: MRRGProcedures.cpp:230
value_for_key_or
auto value_for_key_or(ASSOCIATIVE_COLLECTION &assoc_collection, const KEY &key, DEFAULT_VALUE &default_value) -> std::conditional_t< std::is_lvalue_reference< DEFAULT_VALUE >::value, decltype(false ? default_value :assoc_collection.find(key) ->second)&, decltype(false ? default_value :assoc_collection.find(key) ->second) >
Definition: Collections.h:339
FunctionalUnitNeighbours::end
auto end()
Definition: MRRGProcedures.h:67
Mapping.h
MRRGNodeClassListsByCycle::function_nodes
std::vector< std::vector< MRRG::NodeDescriptor > > function_nodes
Definition: MRRGProcedures.h:44
InterMRRGMap::mergeDescriptors
void mergeDescriptors(MRRG::NodeDescriptor from, MRRG::NodeDescriptor into)
Definition: MRRGProcedures.cpp:73
FunctionalUnitNeighbours::NodeInfoList
std::unordered_map< MRRG::NodeDescriptor, NodeInfo > NodeInfoList
Definition: MRRGProcedures.h:56
computeNodeClassListsByCycle
MRRGNodeClassListsByCycle computeNodeClassListsByCycle(const MRRG &mrrg)
Definition: MRRGProcedures.cpp:138
findAllNeighbourFUs
FunctionalUnitNeighbours findAllNeighbourFUs(const MRRG &mrrg, const NodeList &sources, int min_neighbours_to_find=-1, NEIGHBOUR_FINDER &&neighbour_finder=findNeighbourFUs)
Definition: MRRGProcedures.h:103
FunctionalUnitNeighbours::neighbours
std::unordered_map< MRRG::NodeDescriptor, NodeInfoList > neighbours
Definition: MRRGProcedures.h:62
InterMRRGMap::addMappingMulti
void addMappingMulti(MRRG::NodeDescriptor old_node, const NodeList &new_nodes)
Definition: MRRGProcedures.h:263
MRRGProcedureCacheHandle
Definition: MRRGProcedureCache.h:20
MRRGNodeClassLists
Definition: MRRGProcedures.h:38
computeNodeClassLists
MRRGNodeClassLists computeNodeClassLists(const MRRG &mrrg)
Definition: MRRGProcedures.cpp:169
FunctionalUnitNeighbours::end
auto end() const
Definition: MRRGProcedures.h:65
MRRGNodeClassListsByCycle
Definition: MRRGProcedures.h:43
FunctionalUnitNeighbours::NodeInfo::distance
int distance
Definition: MRRGProcedures.h:51
assertHasSingleFanout
void assertHasSingleFanout(const MRRGNode &n, const char *action_name)
Definition: MRRGProcedures.cpp:561
MRRGTransformFlags
Definition: MRRGProcedures.h:70
InterMRRGMap::NodeSet
std::unordered_set< MRRG::NodeDescriptor > NodeSet
Definition: MRRGProcedures.h:249
MRRG.h
OpGraph
Definition: OpGraph.h:215
InterMRRGMap::operator<<
friend std::ostream & operator<<(std::ostream &os, const InterMRRGMap &imm)
Definition: MRRGProcedures.cpp:98
FunctionalUnitNeighbours::NodeInfo
Definition: MRRGProcedures.h:49
MrrgNodeSpan
Definition: MRRG.h:372
mergeNodeConnections
void mergeNodeConnections(MRRG &mrrg, MRRG::NodeDescriptor into_ndesc, MRRG::NodeDescriptor from_ndesc)
Definition: MRRGProcedures.cpp:567
MRRGTransformationResult
Definition: MRRGProcedures.h:278