torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Trace.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 Trace class.
18 
19 #ifndef TORC_ROUTER_TRACE_HPP
20 #define TORC_ROUTER_TRACE_HPP
21 
24 #include <set>
25 #include <iostream>
26 
29 
30 namespace torc {
31 namespace router {
32 
33  /// \brief Provides path extraction from usage information in a DDB instance..
34  /// \details The tracer provides functions that recover a set of connected routing resources
35  /// to build up complete or partial nets from the device usage information.
36  class Trace {
37  // types
38  /// \brief Imported type name
45  protected:
46  // members
47  /// \brief Database reference.
48  DDB& mDB;
49  /// \brief ArcUsage reference.
51 
52  /// \brief TraceNode representing the starting point for this trace.
54  /// \brief Vector of net sink nodes.
56  /// \brief Vector of net source nodes.
58  /// \brief Vector of net branch nodes.
60 
61  /// \brief Vector of all TraceNode pointers.
63  /// \brief Map of Tilewires to owning TraceNode.
64  std::map<Tilewire, TraceNode*> mTilewireToTraceNode;
65  /// \brief Map of TraceNode pointers to Arc that connects them
66  std::map<TraceNode*, std::map<TraceNode*, Arc> > mTraceNodesToArc;
67  /// \brief Vector of all arcs, populated on a getArcs call.
69 
70  public:
71  /// \brief Enumeration for Trace modes.
74 
75  public:
76  // constructor
77  /// \brief Public Constructor
78  Trace(DDB& inDB, Tilewire inTilewire, ETraceMode inTraceMode = eTraceFullNet)
79  : mDB(inDB), mArcUsage(inDB.getArcUsage()) {
80  mInitialNode = createNode(inTilewire);
81  switch (inTraceMode) {
82  case eTraceFullNet:
83  case eTraceToSinks:
84  case eTraceToBranch:
85  case eTraceToSources:
86  case eTraceSinglePath:
87  break;
88  default:
89  std::cout << "Invalid Trace mode setting." << std::endl;
90  return;
91  }
92  traceWorker(mInitialNode, inTraceMode);
93  }
94  /// \brief Destructor.
95  ~Trace() {
96  std::cout << "Destroying Trace!" << std::endl;
97  TraceNodePtrVector::iterator p;
98  TraceNodePtrVector::iterator e = mAllNodes.end();
99  for (p = mAllNodes.begin(); p != e; p++) {
100  delete *p;
101  }
102  mAllNodes.clear();
103  mSinks.clear();
104  mSources.clear();
105  mBranchPoints.clear();
106  //delete mInitialNode;
107  }
108  /// \brief Get trace sink nodes.
110  return mSinks;
111  }
112  /// \brief Get trace source nodes.
114  return mSources;
115  }
116  /// \brief Get trace branch point nodes.
118  return mBranchPoints;
119  }
120  /// \brief Get all Arcs found during the trace.
122  mArcVector.clear();
123  std::map<TraceNode*, std::map<TraceNode*, Arc> >::iterator outer_p;
124  std::map<TraceNode*, std::map<TraceNode*, Arc> >::iterator outer_end;
125  std::map<TraceNode*, Arc>::iterator inner_p;
126  std::map<TraceNode*, Arc>::iterator inner_end;
127  outer_end = mTraceNodesToArc.end();
128  for (outer_p = mTraceNodesToArc.begin(); outer_p != outer_end; outer_p++) {
129  std::map<TraceNode*, Arc> innermap = outer_p->second;
130  inner_end = innermap.end();
131  for (inner_p = innermap.begin(); inner_p != inner_end; inner_p++) {
132  mArcVector.push_back(inner_p->second);
133  //std::cout << mArcVector.back() << std::endl;
134  }
135  }
136  //std::cout << mArcVector.size() << std::endl;
137  return mArcVector;
138  }
139  protected:
140  /// \brief Recursively traces from the specified TraceNode in the specified mode.
141  void traceWorker(TraceNode* inNode, ETraceMode inMode) {
142  TraceNodePtrVector activeArcs;
143 
144  //std::cout << "\t## traceWorker ## " << inNode->getTilewire() << std::endl;
145 
146  ArcVector outArcs;
147  ArcVector inArcs;
148  TraceNodePtrVector activeSinks;
149  TraceNodePtrVector activeSources;
150  Tilewire nodeTilewire = inNode->getTilewire();
151 
152  unsigned int childCount = 0;
153  unsigned int parentCount = 0;
154  mDB.expandSegmentSinks(nodeTilewire, outArcs);
155  for (unsigned int i = 0 ; i < outArcs.size(); i++) {
156  if (mArcUsage.isUsed(outArcs[i]))
157  childCount++;
158  }
159  mDB.expandSegmentSources(nodeTilewire, inArcs);
160  for (unsigned int i = 0; i < inArcs.size(); i++) {
161  if (mArcUsage.isUsed(inArcs[i]))
162  parentCount++;
163  }
164  bool isBranch;
165  if (childCount == 0) {
166  mSinks.push_back(inNode);
167  std::cout << "FOUND A SINK NODE: " << nodeTilewire << std::endl;
168  }
169  if (parentCount == 0) {
170  mSources.push_back(inNode);
171  std::cout << "FOUND A SOURCE NODE: " << nodeTilewire << std::endl;
172  }
173  if (childCount > 1 || parentCount > 1) {
174  mBranchPoints.push_back(inNode);
175  std::cout << "FOUND A BRANCH NODE: " << nodeTilewire
176  << " children: " << childCount << " parents: " << parentCount << std::endl;
177  isBranch = true;
178  } else {
179  isBranch = false;
180  }
181  if (childCount == 1 && parentCount == 1) {
182  //std::cout << "NODE: " << inNode->getTilewire() << std::endl;
183  }
184 
185  // mode selection
186  bool traceSinks = inMode != eTraceToSources;
187  bool traceSources = inMode != eTraceToSinks;
188  if (inMode == eTraceToBranch || inMode == eTraceSinglePath) {
189  traceSinks = !isBranch;
190  }
191  if (inMode == eTraceToBranch || inMode == eTraceSinglePath) {
192  traceSources = !isBranch;
193  }
194 
195  // node creation
196  if (traceSinks) { // only trace to sources doesn't look at sinks
197  for (unsigned int i = 0; i < outArcs.size(); i++) {
198  const Tilewire& sinkTilewire = outArcs[i].getSinkTilewire();
199  // if the arc is used we need to check this out
200  if (mArcUsage.isUsed(outArcs[i])) {
201  //std::cout << "\tOUT ARC USED " << outArcs[i] << std::endl;
202  // get the node
203  TraceNode* arcNode = getNode(sinkTilewire);
204  if (arcNode == 0) {
205  // node doesn't exist, create it
206  arcNode = createNode(sinkTilewire);
207  // store it for traversal
208  activeSinks.push_back(arcNode);
209  }
210  if (findArc(inNode, arcNode) == Arc()) {
211  // arc does not exist so add it
212  inNode->addChild(arcNode);
213  arcNode->addParent(inNode);
214  } else {
215  // arc already traversed, don't do it again
216  continue;
217  }
218  // record this arc in the trace
219  mTraceNodesToArc[inNode][arcNode] = outArcs[i];
220  }
221  }
222  }
223  if (traceSources) { // only trace to sinks doesn't look at sources
224  for (unsigned int i = 0; i < inArcs.size(); i++) {
225  const Tilewire& sourceTilewire = inArcs[i].getSourceTilewire();
226  // if the arc is used we need to check this out
227  if (mArcUsage.isUsed(inArcs[i])) {
228  //std::cout << "\tIN ARC USED " << inArcs[i] << std::endl;
229  // get the node
230  TraceNode* arcNode = getNode(sourceTilewire);
231  if (arcNode == 0) {
232  // node doesn't exist, create it
233  arcNode = createNode(sourceTilewire);
234  // store it for traversal
235  activeSources.push_back(arcNode);
236  }
237  if (findArc(arcNode, inNode) == Arc()) {
238  // arc does not exist so add it
239  arcNode->addChild(inNode);
240  inNode->addParent(arcNode);
241  } else {
242  // arc already traversed, don't do it again
243  continue;
244  }
245  // record this arc in the trace
246  mTraceNodesToArc[arcNode][inNode] = inArcs[i];
247  }
248  }
249  }
250 
251  // recursive calls
252  if (traceSinks) {
253  for (unsigned int i = 0; i < activeSinks.size(); i++) {
254  if (inMode == eTraceToBranch)
255  traceWorker(activeSinks[i], eTraceToSinks);
256  else
257  traceWorker(activeSinks[i], inMode);
258  }
259  }
260  if (traceSources) { // only trace to sinks doesn't look at sources
261  for (unsigned int i = 0; i < activeSources.size(); i++) {
262  traceWorker(activeSources[i], inMode);
263  }
264  }
265  }
266 
267 
268  // functions
269  /// \brief Normalize depth of nodes.
270  void normalizeDepth(TraceNode* inNode) {
272  TraceNode::TraceNodePtrList wavefront;
273  TraceNode* node;
274  std::set<Tilewire> nodesVisited;
275 
276  // not needed anymore since we save sources
277  //findTop(parents, inNode);
278 
279  for (unsigned int i = 0; i < parents.size(); i++) {
280  parents[i]->setDepth(0);
281  std::cout << "PARENT " << parents[i]->getTilewire() << std::endl;
282  nodesVisited.insert(parents[i]->getTilewire());
283  for (unsigned int j = 0; j < parents[i]->getNumChildren(); j++) {
284  wavefront.push_back(parents[i]->getChild(j));
285  }
286  }
287  while (wavefront.size() != 0) {
288  node = wavefront.front();
289  wavefront.pop_front();
290 
291  Tilewire tw = node->getTilewire();
292  if (nodesVisited.find(tw) == nodesVisited.end()) {
293  nodesVisited.insert(node->getTilewire());
294  } else {
295  std::cout << "Already visited " << tw << std::endl;
296  }
297 
298  if (node->getNumParents() == 0) {
299  std::cout << "SOURCE NODE" << std::endl;
300  node->setDepth(0);
301  } else {
302  for (unsigned int i = 0; i < node->getNumParents(); i++) {
303  std::cout << "PARENT DEPTH " << node->getParent(i)->getDepth() << std::endl;
304  if (node->getParent(i)->getDepth() + 1 > node->getDepth()) {
305  node->setDepth(node->getParent(i)->getDepth() + 1);
306  }
307  }
308  }
309  if (node->getNumChildren() == 0) {
310  std::cout << "SINK NODE" << std::endl;
311  }
312  for (unsigned int i = 0; i < node->getNumChildren(); i++) {
313  wavefront.push_back(node->getChild(i));
314  }
315  }
316 
317  }
318  /// \brief Create a TraceNode and update auxiliary structures.
320  std::map<Tilewire, TraceNode*>::iterator it;
321  it = mTilewireToTraceNode.find(inTilewire);
322  if (it != mTilewireToTraceNode.end()) {
323  return 0;
324  }
325  TilewireVector segmentTilewires;
326  mDB.expandSegment(inTilewire, segmentTilewires);
327  TraceNode* newNode = new TraceNode(inTilewire);
328  mAllNodes.push_back(newNode);
329  for (unsigned int i = 0; i < segmentTilewires.size(); i++) {
330  mTilewireToTraceNode.insert(
331  std::pair<Tilewire, TraceNode*>(segmentTilewires[i], newNode));
332  }
333  return newNode;
334  }
335  /// \brief Get a TraceNode based on its owning Tilewire.
336  TraceNode* getNode(Tilewire inTilewire) {
337  std::map<Tilewire, TraceNode*>::iterator it;
338  it = mTilewireToTraceNode.find(inTilewire);
339  if (it == mTilewireToTraceNode.end()) {
340  return 0;
341  } else {
342  return it->second;
343  }
344  }
345  /// \brief Find a traced Arc from the nodes that it connects
346  Arc findArc(TraceNode* source, TraceNode* sink) {
347  // returns the Arc connecting the two nodes if the arc exists.
348  std::map<TraceNode*, std::map<TraceNode*, Arc> >::iterator outer_p;
349  std::map<TraceNode*, std::map<TraceNode*, Arc> >::iterator outer_end;
350  std::map<TraceNode*, Arc>::iterator inner_p;
351  std::map<TraceNode*, Arc>::iterator inner_end;
352  outer_end = mTraceNodesToArc.end();
353  outer_p = mTraceNodesToArc.find(source);
354  if (outer_p != outer_end) {
355  std::map<TraceNode*, Arc> innermap = outer_p->second;
356  inner_end = innermap.end();
357  inner_p = innermap.find(sink);
358  if (inner_p != inner_end) {
359  return inner_p->second;
360  }
361  }
362  return Arc();
363  }
364  }; // class Trace
365 } // namespace router
366 } // namespace torc
367 
368 #endif // TORC_ROUTER_TRACE_HPP
TraceNodePtrVector & getSources()
Get trace source nodes.
Definition: Trace.hpp:113
TraceNode * createNode(Tilewire inTilewire)
Create a TraceNode and update auxiliary structures.
Definition: Trace.hpp:319
Encapsulation of an arc between two tilewires.
Definition: Arc.hpp:28
std::vector< Tilewire > TilewireVector
Vector of Tilewire objects.
Definition: Tilewire.hpp:101
Tilewire getTilewire()
Get the Tilewire associated with this node.
Definition: TraceNode.hpp:130
Device database, including complete wiring and logic support.
Definition: DDB.hpp:42
void addParent(TraceNode *newParent)
Add parent to the node.
Definition: TraceNode.hpp:150
TraceNode * getChild(boost::uint32_t index)
Get a child by index, returns 0 for invalid index.
Definition: TraceNode.hpp:162
architecture::DDB DDB
Imported type name.
Definition: Trace.hpp:39
void traceWorker(TraceNode *inNode, ETraceMode inMode)
Recursively traces from the specified TraceNode in the specified mode.
Definition: Trace.hpp:141
std::map< TraceNode *, std::map< TraceNode *, Arc > > mTraceNodesToArc
Map of TraceNode pointers to Arc that connects them.
Definition: Trace.hpp:66
TraceNode * getParent(boost::uint32_t index)
Get a parent by index, returns 0 for invalid index.
Definition: TraceNode.hpp:167
architecture::TilewireVector TilewireVector
Definition: Trace.hpp:43
Header for the TraceNode class.
architecture::ArcVector ArcVector
Definition: Trace.hpp:44
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
std::vector< TraceNode * > TraceNodePtrVector
Vector of TraceNode pointer.
Definition: TraceNode.hpp:192
TraceNodePtrVector & getBranchPoints()
Get trace branch point nodes.
Definition: Trace.hpp:117
boost::int32_t getDepth() const
Get the depth of this node from the furthest node with no parent.
Definition: TraceNode.hpp:133
TraceNode * getNode(Tilewire inTilewire)
Get a TraceNode based on its owning Tilewire.
Definition: Trace.hpp:336
ArcUsage & mArcUsage
ArcUsage reference.
Definition: Trace.hpp:50
std::vector< Arc > ArcVector
Vector of Arc objects.
Definition: Arc.hpp:78
TraceNodePtrVector mBranchPoints
Vector of net branch nodes.
Definition: Trace.hpp:59
Header for Boost.Test helper functions.
ArcVector & getArcs()
Get all Arcs found during the trace.
Definition: Trace.hpp:121
void setDepth(boost::int32_t inDepth)
Set the depth of this node.
Definition: TraceNode.hpp:135
Header for torc::physical output stream helpers.
std::map< Tilewire, TraceNode * > mTilewireToTraceNode
Map of Tilewires to owning TraceNode.
Definition: Trace.hpp:64
Provides path extraction from usage information in a DDB instance..
Definition: Trace.hpp:36
TraceNodePtrVector mSources
Vector of net source nodes.
Definition: Trace.hpp:57
Encapsulation of a device tile and wire pair.
Definition: Tilewire.hpp:39
architecture::Tilewire Tilewire
Definition: Trace.hpp:41
Encapsulation the design arc usage.
Definition: ArcUsage.hpp:38
void expandSegmentSources(const Tilewire &inTilewire, ArcVector &outSources, EExpandDirection inExpandDirection=eExpandDirectionNone, bool inUseTied=true, bool inUseRegular=true, bool inUseIrregular=true, bool inUseRoutethrough=true)
Expands all source arcs for the given tilewire's segment.
Definition: DDB.cpp:336
void normalizeDepth(TraceNode *inNode)
Normalize depth of nodes.
Definition: Trace.hpp:270
TraceNodePtrVector mSinks
Vector of net sink nodes.
Definition: Trace.hpp:55
std::vector< TraceNode * > TraceNodePtrVector
Vector of TraceNode pointers.
Definition: TraceNode.hpp:57
architecture::Arc Arc
Definition: Trace.hpp:42
ArcVector mArcVector
Vector of all arcs, populated on a getArcs call.
Definition: Trace.hpp:68
boost::uint32_t getNumParents()
Get the number of parents.
Definition: TraceNode.hpp:158
Arc findArc(TraceNode *source, TraceNode *sink)
Find a traced Arc from the nodes that it connects.
Definition: Trace.hpp:346
TraceNodePtrVector mAllNodes
Vector of all TraceNode pointers.
Definition: Trace.hpp:62
Trace(DDB &inDB, Tilewire inTilewire, ETraceMode inTraceMode=eTraceFullNet)
Public Constructor.
Definition: Trace.hpp:78
ETraceMode
Enumeration for Trace modes.
Definition: Trace.hpp:72
~Trace()
Destructor.
Definition: Trace.hpp:95
TraceNode * mInitialNode
TraceNode representing the starting point for this trace.
Definition: Trace.hpp:53
Header for the DDB class.
boost::uint32_t getNumChildren()
Get the number of children.
Definition: TraceNode.hpp:154
DDB & mDB
Database reference.
Definition: Trace.hpp:48
bool isUsed(const Arc &inArc)
Determines whether the specified arc is in use.
Definition: ArcUsage.hpp:209
std::list< TraceNode * > TraceNodePtrList
List of TraceNode pointers.
Definition: TraceNode.hpp:59
An object that holds more complete path information for routing and tracing.
Definition: TraceNode.hpp:44
architecture::ArcUsage ArcUsage
Definition: Trace.hpp:40
void addChild(TraceNode *newChild)
brief Add child to the node.
Definition: TraceNode.hpp:145
void expandSegment(const Tilewire &inTilewire, TilewireVector &outTilewires, EExpandDirection inExpandDirection=eExpandDirectionNone)
Expands the given tilewire's segment.
Definition: DDB.cpp:154
TraceNodePtrVector & getSinks()
Get trace sink nodes.
Definition: Trace.hpp:109