torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Circuit.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 Circuit class.
18 
19 #ifndef TORC_PHYSICAL_CIRCUIT_HPP
20 #define TORC_PHYSICAL_CIRCUIT_HPP
21 
22 #include "torc/physical/Named.hpp"
27 #include "torc/physical/Net.hpp"
29 #include <map>
30 
31 namespace torc {
32 namespace physical {
33 
34  /// \brief Circuit composed of instances and nets.
35  /// \details This class serves as a base class for Design and Module classes, and cannot be
36  /// instantiated. A circuit is essentially a container for instances and their connecting
37  /// nets.
38  /// \warning Do not attempt to sort or otherwise manipulate the order of the instances and
39  /// nets. An internal container maps instance or net names to corresponding vector indexes
40  /// for fast lookup, and those indexes updated whenever and instance or net is removed.
41  /// Sorting or changing the order of the instances and nets will result in incorrect
42  /// results from findInstance() and findNet(), along with data corruption if
43  /// removeInstance() or removeNet() are subsequently called.
44  /// \todo May want to replace the instance and net vector and map combinations with a Boost
45  /// multi-index.
46  class Circuit : public Named, public Progeny<class Circuit>, public ConfigMap,
47  public common::Annotated, protected Progenitor<class Circuit> {
48  protected:
49  // types
50  /// \brief Imported type name.
52  /// \brief Imported type name
53  typedef boost::int64_t int64_t;
54  /// \brief Map from an element name to a vector index.
55  typedef std::map<std::string, int64_t> NameToIndexMap;
56  // members
57  /// \brief Vector of Instance shared pointers for the circuit.
59  /// \brief Instance name to index mapping for this circuit.
61  /// \brief Vector of Net shared pointers for the circuit.
63  /// \brief Net name to index mapping for this circuit.
65  // constructors
66  /// \brief Protected constructor. Circuit objects cannot be instantiated.
67  /// \param inName The circuit name.
68  Circuit(const string& inName) : Named(inName), ConfigMap(), Progenitor<class Circuit>() {}
69  public:
70  // types
71  /// \brief Constant iterator to Instance shared pointers.
72  typedef InstanceSharedPtrVector::const_iterator InstanceSharedPtrConstIterator;
73  /// \brief Non-constant iterator to Instance shared pointers.
74  typedef InstanceSharedPtrVector::iterator InstanceSharedPtrIterator;
75  /// \brief Constant iterator to Net shared pointers.
76  typedef NetSharedPtrVector::const_iterator NetSharedPtrConstIterator;
77  /// \brief Non-constant iterator to Net shared pointers.
78  typedef NetSharedPtrVector::iterator NetSharedPtrIterator;
79  // functions
80  /// \brief Find a circuit instance by name.
81  /// \param inName The instance name to look for.
82  /// \returns an iterator for the specified instance, or instancesEnd() if the name was not
83  /// found.
84  InstanceSharedPtrIterator findInstance(const string& inName) {
85  NameToIndexMap::iterator result = mInstanceMap.find(inName);
86  NameToIndexMap::iterator e = mInstanceMap.end();
87  if(result == e) return instancesEnd();
88  return instancesBegin() + result->second;
89  }
90  /// \brief Add an instance to the circuit.
91  /// \param inInstancePtr The instance to add.
92  /// \returns true if the instance was added, or false if an instance with the same name
93  /// already exists in the circuit.
94  bool addInstance(InstanceSharedPtr& inInstancePtr) {
95  /// \todo Acquire mutex.
96  const std::string& name = inInstancePtr->getName();
99  if(result != e) return false;
100  inInstancePtr->setParentWeakPtr(mParentWeakPtr);
101  mInstanceMap[name] = mInstances.size();
102  mInstances.push_back(inInstancePtr);
103  return true;
104  /// \todo Release mutex.
105  }
106  /// \brief Remove an instance from the circuit.
107  /// \param inInstancePtr The instance to remove.
108  /// \returns true if the instance was removed, or false if the instance did not exist.
109  bool removeInstance(InstanceSharedPtr& inInstancePtr) {
110  /// \todo Acquire mutex.
111  const std::string& name = inInstancePtr->getName();
112  NameToIndexMap::iterator result = mInstanceMap.find(name);
113  NameToIndexMap::iterator e = mInstanceMap.end();
114  if(result == e) return false;
115  inInstancePtr->resetParentWeakPtr();
116  // fix up the map indexes
117  NameToIndexMap::mapped_type target = result->second;
118  NameToIndexMap::iterator mp = mInstanceMap.begin();
119  NameToIndexMap::iterator me = mInstanceMap.end();
120  while(mp != me) {
121  NameToIndexMap::mapped_type& index = mp->second;
122  if(index > target) index--;
123  mp++;
124  }
125  mInstances.erase(mInstances.begin() + target);
126  mInstanceMap.erase(result);
127  /// \todo Release mutex.
128  return true;
129  }
130  /// \brief Unplace the circuit by discarding placement information for each instance.
131  void unplace(void) {
132  /// \todo Acquire mutex.
135  while(p < e) (*p++)->unplace();
136  /// \todo Release mutex.
137  }
138  /// \brief Find a circuit net by name.
139  /// \param inName The net name to look for.
140  /// \returns an iterator to the specified net, or netsEnd() if the name was not found.
141  NetSharedPtrIterator findNet(const string& inName) {
142  NameToIndexMap::iterator result = mNetMap.find(inName);
143  NameToIndexMap::iterator e = mNetMap.end();
144  if(result == e) return netsEnd();
145  return netsBegin() + result->second;
146  }
147  /// \brief Add a net to the circuit.
148  /// \param inNetPtr The net to add.
149  /// \returns true if the net was added, or false if a net with the same name already exists
150  /// in the circuit.
151  bool addNet(NetSharedPtr& inNetPtr) {
152  /// \todo Acquire mutex.
153  const std::string& name = inNetPtr->getName();
154  NetSharedPtrIterator e = mNets.end();
155  NetSharedPtrIterator result = findNet(name);
156  if(result != e) return false;
157  inNetPtr->setParentWeakPtr(mParentWeakPtr);
158  mNetMap[name] = mNets.size();
159  mNets.push_back(inNetPtr);
160  return true;
161  /// \todo Release mutex.
162  }
163  /// \brief Remove a net from the circuit.
164  /// \param inNetPtr The net to remove.
165  /// \returns true if the net was removed, or false if the net did not exist.
166  bool removeNet(NetSharedPtr& inNetPtr) {
167  /// \todo Acquire mutex.
168  const std::string& name = inNetPtr->getName();
169  NameToIndexMap::iterator result = mNetMap.find(name);
170  NameToIndexMap::iterator e = mNetMap.end();
171  if(result == e) return false;
172  inNetPtr->resetParentWeakPtr();
173  // fix up the map indexes
174  NameToIndexMap::mapped_type target = result->second;
175  NameToIndexMap::iterator mp = mNetMap.begin();
176  NameToIndexMap::iterator me = mNetMap.end();
177  while(mp != me) {
178  NameToIndexMap::mapped_type& index = mp->second;
179  if(index > target) index--;
180  mp++;
181  }
182  mNets.erase(mNets.begin() + target);
183  mNetMap.erase(result);
184  /// \todo Release mutex.
185  return true;
186  }
187  /// \brief Unroute the circuit by discarding routing information for each net.
188  void unroute(void) {
189  /// \todo Acquire mutex.
192  while(p < e) (*p++)->unroute();
193  /// \todo Release mutex.
194  }
195  // iterators
196  /// \brief Returns the begin constant iterator for instances.
198  /// \brief Returns the end constant iterator for instances.
200  /// \brief Returns the begin non-constant iterator for instances.
202  /// \brief Returns the end non-constant iterator for instances.
204  /// \brief Returns the number of instances in the circuit.
205  size_t getInstanceCount(void) const { return mInstances.size(); }
206  /// \brief Returns the begin constant iterator for nets.
207  NetSharedPtrConstIterator netsBegin(void) const { return mNets.begin(); }
208  /// \brief Returns the end constant iterator for nets.
209  NetSharedPtrConstIterator netsEnd(void) const { return mNets.end(); }
210  /// \brief Returns the begin non-constant iterator for nets.
211  NetSharedPtrIterator netsBegin(void) { return mNets.begin(); }
212  /// \brief Returns the end non-constant iterator for nets.
213  NetSharedPtrIterator netsEnd(void) { return mNets.end(); }
214  /// \brief Returns the number of nets in the circuit.
215  size_t getNetCount(void) const { return mNets.size(); }
216  };
217 
218  /// \brief Shared pointer encapsulation of a Circuit.
219  typedef boost::shared_ptr<Circuit> CircuitSharedPtr;
220 
221  /// \brief Weak pointer encapsulation of a Circuit.
222  typedef boost::weak_ptr<Circuit> CircuitWeakPtr;
223 
224  /// \brief Vector of Circuit shared pointers.
225  typedef std::vector<CircuitSharedPtr> CircuitSharedPtrVector;
226 
227 } // namespace physical
228 } // namespace torc
229 
230 #endif // TORC_PHYSICAL_CIRCUIT_HPP
boost::weak_ptr< Circuit > CircuitWeakPtr
Weak pointer encapsulation of a Circuit.
Definition: Circuit.hpp:222
Header for the ConfigMap class.
std::vector< InstanceSharedPtr > InstanceSharedPtrVector
Vector of Instance shared pointers.
InstanceSharedPtrIterator instancesEnd(void)
Returns the end non-constant iterator for instances.
Definition: Circuit.hpp:203
NetSharedPtrVector mNets
Vector of Net shared pointers for the circuit.
Definition: Circuit.hpp:62
std::vector< CircuitSharedPtr > CircuitSharedPtrVector
Vector of Circuit shared pointers.
Definition: Circuit.hpp:225
size_t getNetCount(void) const
Returns the number of nets in the circuit.
Definition: Circuit.hpp:215
Header for the Instance class.
InstanceSharedPtrVector::const_iterator InstanceSharedPtrConstIterator
Constant iterator to Instance shared pointers.
Definition: Circuit.hpp:72
NameToIndexMap mInstanceMap
Instance name to index mapping for this circuit.
Definition: Circuit.hpp:60
size_t getInstanceCount(void) const
Returns the number of instances in the circuit.
Definition: Circuit.hpp:205
bool addNet(NetSharedPtr &inNetPtr)
Add a net to the circuit.
Definition: Circuit.hpp:151
WeakPtrType mParentWeakPtr
Weak pointer to the parent.
Definition: Progeny.hpp:38
Concept for any object that can be named.
Definition: Named.hpp:36
std::string string
Header for the Progenitor class.
Header for the Annotated class.
InstanceSharedPtrIterator findInstance(const string &inName)
Find a circuit instance by name.
Definition: Circuit.hpp:84
Header for the Named class.
Circuit(const string &inName)
Protected constructor. Circuit objects cannot be instantiated.
Definition: Circuit.hpp:68
NetSharedPtrConstIterator netsEnd(void) const
Returns the end constant iterator for nets.
Definition: Circuit.hpp:209
std::vector< NetSharedPtr > NetSharedPtrVector
Vector of Net shared pointers.
boost::shared_ptr< Net > NetSharedPtr
Shared pointer encapsulation of a Net.
NetSharedPtrIterator findNet(const string &inName)
Find a circuit net by name.
Definition: Circuit.hpp:141
boost::shared_ptr< Circuit > CircuitSharedPtr
Shared pointer encapsulation of a Circuit.
Definition: Circuit.hpp:219
boost::shared_ptr< Instance > InstanceSharedPtr
Shared pointer encapsulation of an Instance.
Concept for any object that may have children.
Definition: Progenitor.hpp:36
NetSharedPtrVector::iterator NetSharedPtrIterator
Non-constant iterator to Net shared pointers.
Definition: Circuit.hpp:78
Concept for any object that may have a parent.
Definition: Progeny.hpp:29
Header for the Progeny class.
bool addInstance(InstanceSharedPtr &inInstancePtr)
Add an instance to the circuit.
Definition: Circuit.hpp:94
bool removeNet(NetSharedPtr &inNetPtr)
Remove a net from the circuit.
Definition: Circuit.hpp:166
bool removeInstance(InstanceSharedPtr &inInstancePtr)
Remove an instance from the circuit.
Definition: Circuit.hpp:109
Configuration setting map.
Definition: ConfigMap.hpp:39
Header for the Net class.
InstanceSharedPtrIterator instancesBegin(void)
Returns the begin non-constant iterator for instances.
Definition: Circuit.hpp:201
NetSharedPtrIterator netsBegin(void)
Returns the begin non-constant iterator for nets.
Definition: Circuit.hpp:211
Circuit composed of instances and nets.
Definition: Circuit.hpp:46
boost::int64_t int64_t
Imported type name.
Definition: Circuit.hpp:53
void unroute(void)
Unroute the circuit by discarding routing information for each net.
Definition: Circuit.hpp:188
std::map< std::string, int64_t > NameToIndexMap
Map from an element name to a vector index.
Definition: Circuit.hpp:55
void unplace(void)
Unplace the circuit by discarding placement information for each instance.
Definition: Circuit.hpp:131
InstanceSharedPtrConstIterator instancesBegin(void) const
Returns the begin constant iterator for instances.
Definition: Circuit.hpp:197
NetSharedPtrIterator netsEnd(void)
Returns the end non-constant iterator for nets.
Definition: Circuit.hpp:213
std::string string
Imported type name.
Definition: Circuit.hpp:51
InstanceSharedPtrVector mInstances
Vector of Instance shared pointers for the circuit.
Definition: Circuit.hpp:58
Concept for any object that can be annotated.
Definition: Annotated.hpp:29
NetSharedPtrVector::const_iterator NetSharedPtrConstIterator
Constant iterator to Net shared pointers.
Definition: Circuit.hpp:76
InstanceSharedPtrVector::iterator InstanceSharedPtrIterator
Non-constant iterator to Instance shared pointers.
Definition: Circuit.hpp:74
NameToIndexMap mNetMap
Net name to index mapping for this circuit.
Definition: Circuit.hpp:64
NetSharedPtrConstIterator netsBegin(void) const
Returns the begin constant iterator for nets.
Definition: Circuit.hpp:207
InstanceSharedPtrConstIterator instancesEnd(void) const
Returns the end constant iterator for instances.
Definition: Circuit.hpp:199