torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetRouterHeuristic.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the Heuristic class.
18 
19 #ifndef TORC_ROUTER_NETROUTERHEURISTIC_HPP
20 #define TORC_ROUTER_NETROUTERHEURISTIC_HPP
21 
27 #include <set>
28 #include <iostream>
29 #include <algorithm>
30 #include <queue>
31 #include <stdlib.h>
32 #include <boost/cstdint.hpp>
33 #include <boost/timer.hpp>
34 #include <boost/unordered_map.hpp>
35 #include <boost/functional/hash.hpp>
36 #include <boost/integer_traits.hpp>
37 
38 namespace torc {
39 namespace router {
40 
41  /// \brief Provides net routing based on the Nillson graphsearch algorithm.
42  /// \details The router can either return a vector of nodes or directly populate DDB usage.
44  // types
45  /// \brief Imported type names
57 
58  protected:
59  // members
60  /// \brief ArcUsage reference.
62  /// \brief Tiles reference.
63  const Tiles& mTiles;
64  /// \brief Target sink tilewire.
66  /// \brief Target sink tile information.
68  /// \brief Target row coordinate
69  boost::int32_t mRow;
70  /// \brief Target column coordinate
71  boost::int32_t mCol;
72 
73  /// \brief Segment tilewire buffer.
75  /// \brief Arc buffer.
77 
78 
79 
80  public:
81  // constructor
82  /// \brief Public Constructor
84  mArcUsage(inDB.getArcUsage()), mTiles(inDB.getTiles()) {}
85  /// \brief Destructor.
87 
88  /// \brief Set the current routing target
89  virtual void setSink(const Tilewire& inSink) {
91  mTargetSink = inSink;
94  }
95  /// \brief Calculate the node cost based on distance to the sink and path length
96  virtual void nodeCost(RouteNode& inNode) {
97  Tilewire sinkTilewire = inNode.getSinkTilewire();
98  boost::int32_t bestDistance = boost::integer_traits<boost::int32_t>::max();
99  boost::int32_t distance = boost::integer_traits<boost::int32_t>::max();
100  boost::int32_t cost = 0;
101  if (sinkTilewire == mTargetSink) {
102  inNode.setCost(0);
103  return;
104  }
105  mArcsBuf.clear();
106  mDB.expandSegmentSinks(sinkTilewire, mArcsBuf);
107  if (mArcsBuf.size() == 0) {
108  inNode.setCost(bestDistance);
109  return;
110  }
111  ArcVector::iterator p;
112  ArcVector::iterator e = mArcsBuf.end();
113  for (p = mArcsBuf.begin(); p != e; p++) {
114  if (mArcUsage.isUsed(*p)) continue;
115  if (sinkTilewire == mTargetSink) {
116  inNode.setCost(0);
117  return;
118  }
119  distance = distanceToSink(p->getSinkTilewire());
120  if (distance < bestDistance) bestDistance = distance;
121  }
122  if (bestDistance == boost::integer_traits<boost::int32_t>::max()) {
123  inNode.setCost(bestDistance);
124  return;
125  }
126  cost += bestDistance; // heuristic cost
127  /// \todo provide an additional field in RouteNode to store path cost
128  if (inNode.getParent() == NULL)
129  std::cout << "NULL PARENT" << std::endl;
130  else
131  cost += inNode.getParent()->getCost(); //add cost to parent;
132  inNode.setCost(cost);
133 
134  }
135  /// \brief
136  virtual void nodeCostInitial(RouteNode& inNode) {
137  Tilewire sinkTilewire = inNode.getSinkTilewire();
138  boost::int32_t bestDistance = boost::integer_traits<boost::int32_t>::max();
139  boost::int32_t distance = boost::integer_traits<boost::int32_t>::max();
140  boost::int32_t cost = 0;
141  if (sinkTilewire == mTargetSink) {
142  inNode.setCost(0);
143  return;
144  }
145  mArcsBuf.clear();
146  mDB.expandSegmentSinks(sinkTilewire, mArcsBuf);
147  if (mArcsBuf.size() == 0) {
148  inNode.setCost(bestDistance);
149  return;
150  }
151  ArcVector::iterator p;
152  ArcVector::iterator e = mArcsBuf.end();
153  for (p = mArcsBuf.begin(); p != e; p++) {
154  if (mArcUsage.isUsed(*p)) continue;
155  if (sinkTilewire == mTargetSink) {
156  inNode.setCost(0);
157  return;
158  }
159  distance = distanceToSink(p->getSinkTilewire());
160  if (distance < bestDistance) bestDistance = distance;
161  }
162  if (bestDistance == boost::integer_traits<boost::int32_t>::max()) {
163  inNode.setCost(bestDistance);
164  return;
165  }
166  cost += bestDistance; // heuristic cost
167  inNode.setCost(cost);
168  }
169  /// \brief Reorder the Sinks based on this heuristic.
170  virtual void reorderSinks(const Tilewire& inSource, TilewireVector& inSinks) {
171  // do nothing for now
172  }
173  /// \brief Heuristic handling of expansion of a node.
174  virtual void expandSegmentSinks(const Tilewire& inTilewire, ArcVector& outArcs) {
175  ArcVector tempArcs;
176 
177  const architecture::TileInfo& tileInfo = mTiles.getTileInfo(inTilewire.getTileIndex());
178  const architecture::WireInfo& wireInfo = mTiles.getWireInfo(
179  tileInfo.getTypeIndex(), inTilewire.getWireIndex());
180  if (wireInfo.isInput()) return;
181 
182  mDB.expandSegmentSinks(inTilewire, tempArcs, DDB::eExpandDirectionNone,
183  true, true, true, false);
184  unsigned int s = tempArcs.size();
185  if (s == 0) return;
186  unsigned int t = inTilewire.getTileIndex() % s;
187  for (unsigned int i = t; i < s; i++) {
188  outArcs.push_back(tempArcs[i]);
189  }
190  for (unsigned int i = 0; i < t; i++) {
191  outArcs.push_back(tempArcs[i]);
192  }
193  }
194 
195  protected:
196  virtual boost::int32_t distanceToSink(const Tilewire& inTilewire) {
197  const TileInfo* tileInfo = &mTiles.getTileInfo(inTilewire.getTileIndex());
198  boost::int32_t distance = 0;
199  boost::int32_t iRow = tileInfo->getRow();
200  boost::int32_t iCol = tileInfo->getCol();
201  distance += iRow < mRow ? mRow - iRow : iRow - mRow;
202  distance += iCol < mCol ? mCol - iCol : iCol - mCol;
203  return distance;
204  }
205 
206  virtual boost::int32_t clkDistanceToSink(const Tilewire& inTilewire) {
207  return -100;
208  }
209 
210  boost::int32_t distanceFromParent(const Tilewire& inParent, const Tilewire& inCurrent) {
211  const TileInfo* parentInfo = &mTiles.getTileInfo(inParent.getTileIndex());
212  const TileInfo* currentInfo = &mTiles.getTileInfo(inCurrent.getTileIndex());
213 
214  boost::int32_t distance = 0;
215  boost::int32_t pRow = parentInfo->getRow();
216  boost::int32_t pCol = parentInfo->getCol();
217  boost::int32_t cRow = currentInfo->getRow();
218  boost::int32_t cCol = currentInfo->getCol();
219  distance += pRow < cRow ? cRow - pRow : pRow - cRow;
220  distance += pCol < cCol ? cCol - pCol : pCol - cCol;
221  return distance;
222  }
223  }; // class Heuristic
224 
225 
226 } // namespace router
227 } // namespace torc
228 
229 #endif // TORC_ROUTER_NETROUTERHEURISTIC_HPP
const WireIndex & getWireIndex(void) const
Returns the wire index.
Definition: Tilewire.hpp:66
const TileCol & getCol(void) const
Returns the column for this tile.
Definition: TileInfo.hpp:96
Encapsulation of a tile row in an unsigned 16-bit integer.
Encapsulation of an arc between two tilewires.
Definition: Arc.hpp:28
std::vector< Tilewire > TilewireVector
Vector of Tilewire objects.
Definition: Tilewire.hpp:101
const TileRow & getRow(void) const
Returns the row for this tile.
Definition: TileInfo.hpp:94
ArcUsage & mArcUsage
ArcUsage reference.
const TileInfo * mSinkTileInfo
Target sink tile information.
Encapsulation of a tile column in an unsigned 16-bit integer.
RouteNode * getParent() const
Get the node's parent.
Definition: RouteNode.hpp:98
architecture::xilinx::TileCol TileCol
Device database, including complete wiring and logic support.
Definition: DDB.hpp:42
architecture::DDB DDB
Imported type names.
architecture::xilinx::TileRow TileRow
const TileInfo & getTileInfo(TileIndex inTileIndex) const
Returns the TileInfo object for the specified tile.
Definition: Tiles.hpp:137
Encapsulation the design wire usage.
Definition: WireUsage.hpp:35
bool isInput(void) const
Returns true if the wire is a logic input.
Definition: WireInfo.hpp:130
Header for the RouteNode class.
virtual void nodeCostInitial(RouteNode &inNode)
const Array< const WireInfo > & getWireInfo(TileTypeIndex inTileTypeIndex) const
Returns the WireInfo array for the specified tile type.
Definition: Tiles.hpp:140
virtual boost::int32_t distanceToSink(const Tilewire &inTilewire)
void expandSegmentSinks(const Tilewire &inTilewire, ArcVector &outSinks, EExpandDirection inExpandDirection=eExpandDirectionNone, bool inUseTied=true, bool inUseRegular=true, bool inUseIrregular=true, bool inUseRoutethrough=true)
Expands all sink arcs for the given tilewire's segment.
Definition: DDB.cpp:314
architecture::TilewireVector TilewireVector
virtual void nodeCost(RouteNode &inNode)
Calculate the node cost based on distance to the sink and path length.
boost::int32_t distanceFromParent(const Tilewire &inParent, const Tilewire &inCurrent)
std::vector< Arc > ArcVector
Vector of Arc objects.
Definition: Arc.hpp:78
const Tilewire & getSinkTilewire() const
Get the sink Tilewire.
Definition: RouteNode.hpp:84
void setCost(boost::int32_t inHeuristicCost)
Set the heuristic node cost.
Definition: RouteNode.hpp:88
Header for torc::physical output stream helpers.
const Tiles & mTiles
Tiles reference.
Tilewire mTargetSink
Target sink tilewire.
Encapsulation of a device tile and wire pair.
Definition: Tilewire.hpp:39
Encapsulation the design arc usage.
Definition: ArcUsage.hpp:38
Tile map, tile type, and wire information for the family and device.
Definition: Tiles.hpp:36
boost::int32_t mRow
Target row coordinate.
Encapsulation of a tile within a device tile map.
Definition: TileInfo.hpp:33
const TileTypeIndex & getTypeIndex(void) const
Returns the tile type index for this tile.
Definition: TileInfo.hpp:92
Provides net routing based on the Nillson graphsearch algorithm.
virtual void setSink(const Tilewire &inSink)
Set the current routing target.
Header for the HeuristicBase class.
An object that holds an arc and path information for routing.
Definition: RouteNode.hpp:40
virtual void reorderSinks(const Tilewire &inSource, TilewireVector &inSinks)
Reorder the Sinks based on this heuristic.
Provides the interface for net routers.
NetRouterHeuristic(DDB &inDB)
Public Constructor.
Header for the DDB class.
const TileIndex & getTileIndex(void) const
Returns the tile index.
Definition: Tilewire.hpp:64
Encapsulation of a wire within a tile type.
Definition: WireInfo.hpp:36
bool isUsed(const Arc &inArc)
Determines whether the specified arc is in use.
Definition: ArcUsage.hpp:209
virtual boost::int32_t clkDistanceToSink(const Tilewire &inTilewire)
TilewireVector mSegmentBuf
Segment tilewire buffer.
boost::int32_t mCol
Target column coordinate.
virtual void expandSegmentSinks(const Tilewire &inTilewire, ArcVector &outArcs)
Heuristic handling of expansion of a node.
const boost::int32_t getCost() const
Get the heuristic node cost.
Definition: RouteNode.hpp:86
Device database types for Xilinx architectures.