torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CombinationalPath.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 CombinationalPath class.
18 
19 #ifndef TORC_PACKER_COMBINATIONALPATH_HPP
20 #define TORC_PACKER_COMBINATIONALPATH_HPP
21 
22 #include "torc/physical/Net.hpp"
25 #include "torc/physical/Design.hpp"
26 #include <iostream>
27 #include <vector>
28 
29 namespace torc {
30 namespace physical {
31 
32  /// \brief Routing net.
33 
35  // friends
36  /// \brief The Factory class has direct access to our internals.
37  friend class RcFactory;
38  protected:
39  // types
40  /// \brief Imported type name.
42 
43  // members
44  /// \brief Vector of routing net shared pointers.
46 
47  // constructors
48  /// \brief Protected constructor.
49  /// \param original net
50  /// \param inNetType The net power type.
52  public:
53 
54  /// \brief Constant iterator to Routing Net shared pointers.
55  typedef RoutingNetSharedPtrVector::const_iterator RoutingNetSharedPtrConstIterator;
56  /// \brief Non-constant iterator to Routing Net shared pointers.
57  typedef RoutingNetSharedPtrVector::iterator RoutingNetSharedPtrIterator;
58 
59  /// \brief Find a net by name.
60  /// \param inName The net name to look for.
61  /// \returns an iterator to the specified net, or netsEnd() if the name was not found.
63  NameComparator predicate(inName);
64  return std::find_if(routingNetsBegin(), routingNetsEnd(), predicate);
65  }
66  /// \brief Add a net to the set.
67  /// \param inRoutingNetPtr The net to add.
68  /// \returns true if the net was added, or false if a net with the same name already exists
69  /// in the circuit.
70  bool addRoutingNet(RoutingNetSharedPtr& inRoutingNetPtr) {
71  /// \todo Acquire mutex.
73  RoutingNetSharedPtrIterator result = findRoutingNet(inRoutingNetPtr->getName());
74  if(result != e) return false;
75  mRoutingNets.push_back(inRoutingNetPtr);
76  return true;
77  /// \todo Release mutex.
78  }
79  /// \brief Remove a net from the circuit.
80  /// \param inRoutingNetPtr The net to remove.
81  /// \returns true if the net was removed, or false if the net did not exist.
82  bool removeRoutingNet(RoutingNetSharedPtr& inRoutingNetPtr) {
83  /// \todo Acquire mutex.
85  RoutingNetSharedPtrIterator result = findRoutingNet(inRoutingNetPtr->getName());
86  if(result == e) return false;
87  mRoutingNets.erase(result);
88  /// \todo Release mutex.
89  return true;
90  }
91 
92  // functions
93  /// \brief Returns instance pin type
95 
96  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
97  if(netPtr->containsSource(instancePinPtr))
98  return OutputP;
99  return InputP;
100  }
101  /// \brief Set combinational counts for all nets
102  bool setPatchCounts(DesignSharedPtr inDesignPtr){
103 
104  Circuit::NetSharedPtrConstIterator pn = inDesignPtr->netsBegin();
105  Circuit::NetSharedPtrConstIterator en = inDesignPtr->netsEnd();
106  while(pn < en){
107  NetSharedPtr netPtr = *pn++;
108  RoutingNetSharedPtr routingNetPtr(new RoutingNet(netPtr));
109  mRoutingNets.push_back(routingNetPtr);
110  }
111  Circuit::InstanceSharedPtrConstIterator pi = inDesignPtr->instancesBegin();
112  Circuit::InstanceSharedPtrConstIterator ei = inDesignPtr->instancesEnd();
113  // set nets connected to flipflops
114  while(pi < ei) {
115  InstanceSharedPtr instance1Ptr = *pi++;
116  if((instance1Ptr->getType()=="DFF")||(instance1Ptr->getType()=="IOB")){
117  Instance::InstancePinSharedPtrConstIterator ipp = instance1Ptr->pinsBegin();
118  Instance::InstancePinSharedPtrConstIterator ipe = instance1Ptr->pinsEnd();
119  while(ipp != ipe) {
120  InstancePinSharedPtr instancePinPtr = ipp->second;
121  if(getInstancePinType(instancePinPtr) == InputP){
122  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
123  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
124  Net::InstancePinSharedPtrConstIterator sop = netPtr->sourcesBegin();
125  Net::InstancePinSharedPtrConstIterator soe = netPtr->sourcesEnd();
126  while(sop < soe){
127  InstancePinSharedPtr instPin = *sop++;
128  routingNetPtr->setPathCount(instPin, 1);
129  }
130  }
131  ++ipp;
132  }
133  }
134  }
135  bool repeatNeeded = true;
136  size_t maxTotalCount = 0;
137  while(repeatNeeded){
138  if(maxTotalCount>inDesignPtr->getInstanceCount()){
139  cout<<"Combinational Loop !!!!!"<<endl;;
140  break;
141  }
142  repeatNeeded = false;
143  pi = inDesignPtr->instancesBegin();
144  ei = inDesignPtr->instancesEnd();
145 
146  // set nets connected to other gates
147  while(pi < ei) {
148  InstanceSharedPtr instance1Ptr = *pi++;
149  if((instance1Ptr->getType()!= "DFF")&&(instance1Ptr->getType()!= "IOB")){
150  size_t maxCount = 0;
151  Instance::InstancePinSharedPtrConstIterator ipp = instance1Ptr->pinsBegin();
152  Instance::InstancePinSharedPtrConstIterator ipe = instance1Ptr->pinsEnd();
153  while(ipp != ipe) {
154  InstancePinSharedPtr instancePinPtr = ipp->second;
155  if(getInstancePinType(instancePinPtr) == OutputP){
156  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
157  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
158  Net::InstancePinSharedPtrConstIterator sop = netPtr->sourcesBegin();
159  Net::InstancePinSharedPtrConstIterator soe = netPtr->sourcesEnd();
160  while(sop < soe){
161  InstancePinSharedPtr instPin = *sop++;
162  size_t currCount = routingNetPtr->getPathCount(instPin);
163  if(currCount>maxCount)
164  maxCount = currCount;
165  if(maxTotalCount<maxCount)
166  maxTotalCount=maxCount;
167  }
168  }
169  ++ipp;
170  }
171  ipp = instance1Ptr->pinsBegin();
172  ipe = instance1Ptr->pinsEnd();
173  while(ipp != ipe) {
174  InstancePinSharedPtr instancePinPtr = ipp->second;
175  if(getInstancePinType(instancePinPtr) == InputP){
176  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
177  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
178  if(routingNetPtr->getPathCount(instancePinPtr)<(maxCount+1)){
179  routingNetPtr->setPathCount(instancePinPtr,maxCount+1);
180  repeatNeeded = true;
181  }
182  Net::InstancePinSharedPtrConstIterator sop = netPtr->sourcesBegin();
183  Net::InstancePinSharedPtrConstIterator soe = netPtr->sourcesEnd();
184  while(sop < soe){
185  InstancePinSharedPtr instPin = *sop++;
186  size_t currCount = routingNetPtr->getPathCount(instPin);
187  if(currCount<(maxCount+1)){
188  routingNetPtr->setPathCount(instPin, maxCount+1);
189  maxCount = currCount;
190  if(maxTotalCount<maxCount)
191  maxTotalCount=maxCount;
192  repeatNeeded = true;
193  }
194  }
195  }
196  ++ipp;
197  }
198  }
199  }
200  }
201  pn = inDesignPtr->netsBegin();
202  en = inDesignPtr->netsEnd();
203  while(pn < en){
204  NetSharedPtr netPtr = *pn++;
205 
206  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
207  Net::InstancePinSharedPtrConstIterator sop = netPtr->sourcesBegin();
208  Net::InstancePinSharedPtrConstIterator soe = netPtr->sourcesEnd();
209  size_t setCount = 0;
210  while(sop<soe){
211  if(setCount<routingNetPtr->getPathCount(*sop))
212  setCount = routingNetPtr->getPathCount(*sop);
213  ++sop;
214  }
215 
216  Net::InstancePinSharedPtrConstIterator sip = netPtr->sinksBegin();
217  Net::InstancePinSharedPtrConstIterator sie = netPtr->sinksEnd();
218  while(sip!=sie){
219  if(routingNetPtr->getPathCount(*sip)==0)
220  routingNetPtr->setPathCount(*sip, setCount);
221  ++sip;
222  }
223  }
224 
225 
226  repeatNeeded = true;
227 
228  while(repeatNeeded){
229  repeatNeeded = false;
230  pi = inDesignPtr->instancesBegin();
231  ei = inDesignPtr->instancesEnd();
232 
233  // set nets connected to other gates
234  while(pi < ei) {
235  InstanceSharedPtr instance1Ptr = *pi++;
236  if((instance1Ptr->getType()!= "DFF")&&(instance1Ptr->getType()!= "IOB")){
237  size_t maxCount = 0;
238  Instance::InstancePinSharedPtrConstIterator ipp = instance1Ptr->pinsBegin();
239  Instance::InstancePinSharedPtrConstIterator ipe = instance1Ptr->pinsEnd();
240  while(ipp != ipe) {
241  InstancePinSharedPtr instancePinPtr = ipp->second;
242  if(getInstancePinType(instancePinPtr) == InputP){
243  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
244  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
245  size_t currCount = routingNetPtr->getPathCount(instancePinPtr);
246  if(currCount>maxCount)
247  maxCount = currCount;
248  }
249  ++ipp;
250  }
251 
252  ipp = instance1Ptr->pinsBegin();
253  ipe = instance1Ptr->pinsEnd();
254  while(ipp != ipe) {
255  InstancePinSharedPtr instancePinPtr = ipp->second;
256  if(getInstancePinType(instancePinPtr) == OutputP){
257  NetSharedPtr netPtr = instancePinPtr->getParentWeakPtr().lock();
258  RoutingNetSharedPtr routingNetPtr = *findRoutingNet(netPtr->getName());
259  if(routingNetPtr->getPathCount(instancePinPtr)<(maxCount)){
260  routingNetPtr->setPathCount(instancePinPtr,maxCount);
261  repeatNeeded = true;
262  size_t minDiff = 100000000;
263  Net::InstancePinSharedPtrConstIterator sop = netPtr->sinksBegin();
264  Net::InstancePinSharedPtrConstIterator soe = netPtr->sinksEnd();
265  while(sop < soe){
266  InstancePinSharedPtr instPin = *sop++;
267  size_t currCount = maxCount - routingNetPtr->getPathCount(instPin);
268  if(currCount<minDiff){
269  minDiff = currCount;
270  }
271  }
272  sop = netPtr->sinksBegin();
273  soe = netPtr->sinksEnd();
274  while(sop < soe){
275  InstancePinSharedPtr instPin = *sop++;
276  size_t currCount = routingNetPtr->getPathCount(instPin);
277  routingNetPtr->setPathCount(instPin,currCount+minDiff);
278  }
279  }
280  }
281  ++ipp;
282  }
283  }
284  }
285  }
286  return true;
287  }
288 
289  /// \brief Returns the begin constant iterator for routingNets.
291  /// \brief Returns the end constant iterator for routingNets.
293  /// \brief Returns the begin non-constant iterator for routingNets.
295  /// \brief Returns the end non-constant iterator for routingNets.
297  /// \brief Returns the number of routingNets in the circuit.
298  size_t getRoutingNetCount(void) const { return mRoutingNets.size(); }
299  };
300 
301 
302  /// \brief Shared pointer encapsulation of a CombinationalPath.
303  typedef boost::shared_ptr<CombinationalPath> CombinationalPathSharedPtr;
304 
305  /// \brief Vector of CombinationalPath shared pointers.
306  typedef std::vector<CombinationalPathSharedPtr> CombinationalPathSharedPtrVector;
307 
308 } // namespace physical
309 } // namespace torc
310 
311 #endif // TORC_PACKER_COMBINATIONALPATH_HPP
RoutingNetSharedPtrVector mRoutingNets
Vector of routing net shared pointers.
CombinationalPath()
Protected constructor.
size_t getRoutingNetCount(void) const
Returns the number of routingNets in the circuit.
RoutingNetSharedPtrVector::iterator RoutingNetSharedPtrIterator
Non-constant iterator to Routing Net shared pointers.
bool addRoutingNet(RoutingNetSharedPtr &inRoutingNetPtr)
Add a net to the set.
bool removeRoutingNet(RoutingNetSharedPtr &inRoutingNetPtr)
Remove a net from the circuit.
RoutingNetSharedPtrConstIterator routingNetsEnd(void) const
Returns the end constant iterator for routingNets.
InstanceSharedPtrVector::const_iterator InstanceSharedPtrConstIterator
Constant iterator to Instance shared pointers.
Definition: Circuit.hpp:72
Header for the RoutingNet class.
std::string string
Imported type name.
RcFactory class for physical netlist elements.
Definition: RcFactory.hpp:34
RoutingNetSharedPtrIterator routingNetsEnd(void)
Returns the end non-constant iterator for routingNets.
std::vector< CombinationalPathSharedPtr > CombinationalPathSharedPtrVector
Vector of CombinationalPath shared pointers.
PinType getInstancePinType(InstancePinSharedPtr instancePinPtr)
Returns instance pin type.
boost::shared_ptr< class InstancePin > InstancePinSharedPtr
Shared pointer encapsulation of an InstancePin.
std::string string
std::vector< RoutingNetSharedPtr > RoutingNetSharedPtrVector
Vector of RoutingNet shared pointers.
Definition: RoutingNet.hpp:134
Comparator class to serve as a predicate when searching for names.
Definition: Named.hpp:61
Header for the Design class.
RoutingNetSharedPtrIterator routingNetsBegin(void)
Returns the begin non-constant iterator for routingNets.
InstancePinMap::const_iterator InstancePinSharedPtrConstIterator
Constant iterator to InstancePin shared pointers.
boost::shared_ptr< Net > NetSharedPtr
Shared pointer encapsulation of a Net.
RoutingNetSharedPtrConstIterator routingNetsBegin(void) const
Returns the begin constant iterator for routingNets.
RoutingNetSharedPtrVector::const_iterator RoutingNetSharedPtrConstIterator
Constant iterator to Routing Net shared pointers.
boost::shared_ptr< Instance > InstanceSharedPtr
Shared pointer encapsulation of an Instance.
boost::shared_ptr< Design > DesignSharedPtr
Shared pointer encapsulation of a Design.
InstancePinSharedPtrVector::const_iterator InstancePinSharedPtrConstIterator
Constant iterator to InstancePin shared pointer objects.
Header for the Net class.
boost::shared_ptr< RoutingNet > RoutingNetSharedPtr
Shared pointer encapsulation of a RoutingNet.
Definition: RoutingNet.hpp:131
boost::shared_ptr< CombinationalPath > CombinationalPathSharedPtr
Shared pointer encapsulation of a CombinationalPath.
RoutingNetSharedPtrIterator findRoutingNet(const string &inName)
Find a net by name.
bool setPatchCounts(DesignSharedPtr inDesignPtr)
Set combinational counts for all nets.
NetSharedPtrVector::const_iterator NetSharedPtrConstIterator
Constant iterator to Net shared pointers.
Definition: Circuit.hpp:76