OpenASIP  2.0
RemoveUnconnectedComponents.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file RemoveUnconnectedComponents.cc
26  *
27  * Explorer plugin that removes unconnected ports from units or creates
28  * connections to these ports in case of a FUs. Also removes unconnected
29  * buses. If all ports from a unit are removed, removes also the unit.
30  *
31  *
32  * @author Esa Määttä 2007 (esa.maatta-no.spam-tut.fi)
33  * @note rating: red
34  */
35 
36 #include <string>
37 #include <vector>
38 
40 #include "DSDBManager.hh"
41 #include "Machine.hh"
42 #include "MachineTester.hh"
43 #include "Exception.hh"
44 #include "StringTools.hh"
45 #include "Conversion.hh"
46 
48 
49 using std::endl;
50 using namespace TTAMachine;
51 
52 /**
53  * Explorer plugin that removes unconnected ports from units or creates
54  * connections to these ports in case of a FUs. Also removes unconnected
55  * buses. If all ports from a unit are removed, removes also the unit.
56  */
58  PLUGIN_DESCRIPTION("Removes unconnected components from a configuration.");
59 
61  allowRemoval_(false) {
62 
63  // compulsory parameters
64  // no compulsory parameters
65 
66  // parameters that have a default value
67  addParameter(allowRemovalPN_, BOOL, false,
68  Conversion::toString(allowRemoval_));
69  }
70 
71 
72  virtual bool requiresStartingPointArchitecture() const { return true; }
73  virtual bool producesArchitecture() const { return true; }
74  virtual bool requiresHDB() const { return false; }
75  virtual bool requiresSimulationData() const { return false; }
76  virtual bool requiresApplication() const { return false; }
77 
78  /**
79  * Removes unconnected components from a configuration.
80  *
81  * Explorer plugin that removes unconnected ports from units or creates
82  * connections to these ports in case of a FUs. Also removes unconnected
83  * buses. If all ports from a unit are removed, removes also the unit.
84  * Removes also unconnected FUs. First unconnected sockets are removed.
85  *
86  * Supported parameters:
87  * - allow_remove, Allows the RFs port removal and portless RFs removal.
88  *
89  * @param configurationID Configuration to optimize.
90  */
91  virtual std::vector<RowID>
92  explore(const RowID& configurationID, const unsigned int&) {
93 
94  std::vector<RowID> result;
95 
96  readParameters();
97 
98  try {
99  DSDBManager& dsdb = db();
101  dsdb.configuration(configurationID);
102 
103  // get the machine belonging to the configuration
104  Machine* mach = NULL;
105  try {
106  mach = dsdb.architecture(conf.architectureID);
107  } catch (const Exception& e) {
108  return result;
109  }
110 
111  // removes sockets not connected to any bus
112  removeSockets(*mach);
113 
114  // removes FUs not connected to any socket by any port
115  std::vector<std::string> removedFUNames;
116  removeUnconnectedFUs(*mach, removedFUNames);
117 
118  // checks that every FU port is connected to a socket
119  checkFUPorts(*mach);
120 
121  // register files are removed only if allow_remove parameter is
122  // given, else connections to sockets are made for RFs
123  std::vector<std::string> removedRFNames;
124  checkRFPorts(*mach, removedRFNames);
125 
126  // removes buses that have no connections to any socket
127  checkBuses(*mach);
128 
130  if (conf.hasImplementation) {
131  newConf.hasImplementation = true;
133  dsdb.implementation(conf.implementationID);
134  // removes removed RFs from idf
135  while (!removedRFNames.empty()) {
136  idf->removeRFImplementation(removedRFNames.at(
137  removedRFNames.size()-1));
138  removedRFNames.pop_back();
139  }
140  // removes removed FUs from idf
141  while (!removedFUNames.empty()) {
142  idf->removeFUImplementation(removedFUNames.at(
143  removedFUNames.size()-1));
144  removedFUNames.pop_back();
145  }
146  newConf.implementationID =
147  dsdb.addImplementation(*idf, 0, 0);
148  } else {
149  newConf.hasImplementation = false;
150  }
151  newConf.architectureID = dsdb.addArchitecture(*mach);
152 
153  RowID confID = dsdb.addConfiguration(newConf);
154  result.push_back(confID);
155 
156  } catch (const Exception& e) {
157  std::ostringstream msg(std::ostringstream::out);
158  msg << "Error while using RemoveUnconnectedComponents:"
159  << endl << e.errorMessage() << endl;
160  verboseLog(msg.str());
161  return result;
162  }
163  return result;
164  }
165 
166 private:
167  static const std::string allowRemovalPN_;
168  /// parameter allow removal of unused ports and RFs without ports.
170 
171 
172  /**
173  * Reads the parameters given to the plugin.
174  */
175  void readParameters() {
176  readOptionalParameter(allowRemovalPN_, allowRemoval_);
177  }
178 
179 
180  /**
181  * Removes totally unconnected FUs.
182  *
183  * @param mach Machine which unconnected FUs are removed.
184  * @param removedFUNames Names of FU units that were removed from machine
185  */
187  std::vector<std::string>& removedFUNames) {
188 
189  //MachineTester& tester = mach.machineTester();
190  Machine::SocketNavigator socketNav = mach.socketNavigator();
191 
193  for (int i = 0; i < FUNav.count(); ++i) {
194  FunctionUnit* FU = FUNav.item(i);
195  bool noConnections = true;
196  for (int j = 0; j < FU->portCount(); ++j) {
197  Port* port = FU->port(j);
198  // if no connections
199  if (port->socketCount() > 0) {
200  noConnections = false;
201  break;
202  }
203  }
204  // remove FU if it did not have any connections
205  if (noConnections) {
206  mach.removeFunctionUnit(*FU);
207  removedFUNames.push_back(FU->name());
208  --i;
209  }
210  }
211  }
212 
213  /**
214  * Checks that every FU port has at least one connection.
215  *
216  * Adds an new connection to some available socket if port has no
217  * connections
218  *
219  * @param mach Machine which FU ports are checked.
220  */
222  MachineTester& tester = mach.machineTester();
223  Machine::SocketNavigator socketNav = mach.socketNavigator();
224 
226  for (int i = 0; i < FUNav.count(); ++i) {
227  FunctionUnit* FU = FUNav.item(i);
228  for (int j = 0; j < FU->portCount(); ++j) {
229  Port* port = FU->port(j);
230  // if no connections, make a connection to some socket
231  if (port->socketCount() < 1) {
232  Socket* socket = NULL;
233  // find a socket where FU port can be connected
234  for (int soc = 0; i < socketNav.count(); ++soc) {
235  socket = socketNav.item(soc);
236  if (tester.canConnect(*socket, *port)) {
237  port->attachSocket(*socket);
238  break;
239  }
240  }
241  }
242  }
243  }
244  }
245 
246  /**
247  * Checks that every RF port has connections.
248  *
249  * Removes every port of a RF that has no connections to sockets if
250  * allowRemoval_ is true else makes connection to available socket.
251  * If RF ends up having no ports, removes the RF from machine.
252  *
253  * @param mach Machine which RF ports are checked.
254  * @param removedRFNames Names of RF units that were removed from machine
255  * content only added not read in this function.
256  */
258  std::vector<std::string>& removedRFNames) {
259 
260  MachineTester& tester = mach.machineTester();
261  Machine::SocketNavigator socketNav = mach.socketNavigator();
262 
264  for (int i = 0; i < RFNav.count(); ++i) {
265  RegisterFile* RF = RFNav.item(i);
266  for (int j = 0; j < RF->portCount(); ++j) {
267  Port* port = RF->port(j);
268  // if no connections
269  if (port->socketCount() < 1) {
270  if (allowRemoval_) {
271  delete port;
272  port = NULL;
273  --j;
274  if (RF->portCount() < 1) {
275  mach.removeRegisterFile(*RF);
276  removedRFNames.push_back(RF->name());
277  --i;
278  }
279  } else {
280  Socket* socket = NULL;
281  // find a socket where RF port can be connected
282  for (int soc = 0; i < socketNav.count(); ++soc) {
283  socket = socketNav.item(soc);
284  if (tester.canConnect(*socket, *port)) {
285  port->attachSocket(*socket);
286  break;
287  }
288  }
289  }
290  }
291  }
292  }
293  }
294 
295  /**
296  * Checks buses and removes the ones that have no connections to sockets.
297  *
298  * @param mach Machine which buses are checked.
299  */
301  Machine::SocketNavigator socketNav = mach.socketNavigator();
302  Machine::BusNavigator busNav = mach.busNavigator();
303  for (int i = 0; i < busNav.count(); ++i) {
304  bool isConnected = false;
305  for (int j = 0; j < socketNav.count(); ++j) {
306  if ((busNav.item(i))->isConnectedTo(
307  *socketNav.item(j))) {
308  isConnected = true;
309  }
310  }
311  // if not connected to any socket remove bus
312  if (!isConnected) {
313  mach.removeBus(*busNav.item(i));
314  }
315  }
316  }
317 
318  /**
319  * Removes sockets that are not needed in the machine.
320  *
321  * @param mach Machine which extra sockets are removed.
322  */
324 
325  // remove not connected sockets
326  MachineResourceModifier modifier;
327  std::list<std::string> removedSocketNames;
328  modifier.removeNotConnectedSockets(mach, removedSocketNames);
329  }
330 };
331 
332 const std::string
334 
TTAMachine::Machine::removeFunctionUnit
virtual void removeFunctionUnit(FunctionUnit &unit)
Definition: Machine.cc:530
RemoveUnconnectedComponents::explore
virtual std::vector< RowID > explore(const RowID &configurationID, const unsigned int &)
Definition: RemoveUnconnectedComponents.cc:92
IDF::MachineImplementation::removeFUImplementation
void removeFUImplementation(const std::string &unitName)
Definition: MachineImplementation.cc:634
MachineTester::canConnect
virtual bool canConnect(const TTAMachine::Socket &socket, const TTAMachine::Segment &segment)
Definition: MachineTester.cc:86
RemoveUnconnectedComponents
Definition: RemoveUnconnectedComponents.cc:57
IDF::MachineImplementation::removeRFImplementation
void removeRFImplementation(const std::string &unitName)
Definition: MachineImplementation.cc:659
FU
const string FU
Definition: IDFSerializer.cc:64
Exception.hh
RemoveUnconnectedComponents::requiresHDB
virtual bool requiresHDB() const
Definition: RemoveUnconnectedComponents.cc:74
DSDBManager::architecture
TTAMachine::Machine * architecture(RowID id) const
Definition: DSDBManager.cc:807
RemoveUnconnectedComponents::requiresSimulationData
virtual bool requiresSimulationData() const
Definition: RemoveUnconnectedComponents.cc:75
DSDBManager::MachineConfiguration::hasImplementation
bool hasImplementation
Definition: DSDBManager.hh:80
TTAMachine::Machine::removeBus
virtual void removeBus(Bus &bus)
Definition: Machine.cc:477
DesignSpaceExplorerPlugin
Definition: DesignSpaceExplorerPlugin.hh:55
RemoveUnconnectedComponents::removeUnconnectedFUs
void removeUnconnectedFUs(TTAMachine::Machine &mach, std::vector< std::string > &removedFUNames)
Definition: RemoveUnconnectedComponents.cc:186
RowID
int RowID
Type definition of row ID in relational databases.
Definition: DBTypes.hh:37
MachineResourceModifier
Definition: MachineResourceModifier.hh:49
RemoveUnconnectedComponents::allowRemoval_
bool allowRemoval_
parameter allow removal of unused ports and RFs without ports.
Definition: RemoveUnconnectedComponents.cc:169
TTAMachine::Machine::Navigator::count
int count() const
Conversion::toString
static std::string toString(const T &source)
DesignSpaceExplorerPlugin.hh
verboseLog
#define verboseLog(text)
Definition: Application.hh:115
BOOL
@ BOOL
Definition: ExplorerPluginParameter.hh:40
TTAMachine::Port::socketCount
virtual int socketCount() const
Definition: Port.cc:375
StringTools.hh
RemoveUnconnectedComponents::checkBuses
void checkBuses(TTAMachine::Machine &mach)
Definition: RemoveUnconnectedComponents.cc:300
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::Port::attachSocket
virtual void attachSocket(Socket &socket)
Definition: Port.cc:191
DSDBManager::MachineConfiguration::implementationID
RowID implementationID
Definition: DSDBManager.hh:81
RemoveUnconnectedComponents::producesArchitecture
virtual bool producesArchitecture() const
Definition: RemoveUnconnectedComponents.cc:73
DSDBManager::MachineConfiguration
Definition: DSDBManager.hh:78
PLUGIN_DESCRIPTION
const std::string PLUGIN_DESCRIPTION
Definition: DefaultICDecoderPlugin.cc:917
EXPORT_DESIGN_SPACE_EXPLORER_PLUGIN
#define EXPORT_DESIGN_SPACE_EXPLORER_PLUGIN(PLUGIN_NAME__)
Definition: DesignSpaceExplorerPlugin.hh:125
Conversion.hh
TTAMachine::Machine::machineTester
MachineTester & machineTester() const
Definition: Machine.cc:671
TTAMachine::Port
Definition: Port.hh:54
TTAMachine::Machine::functionUnitNavigator
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition: Machine.cc:380
TTAMachine::Socket
Definition: Socket.hh:53
TTAMachine::Machine::removeRegisterFile
virtual void removeRegisterFile(RegisterFile &unit)
Definition: Machine.cc:554
Machine.hh
Exception
Definition: Exception.hh:54
DSDBManager
Definition: DSDBManager.hh:76
TTAMachine::Machine::socketNavigator
virtual SocketNavigator socketNavigator() const
Definition: Machine.cc:368
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
DSDBManager.hh
RemoveUnconnectedComponents::checkRFPorts
void checkRFPorts(TTAMachine::Machine &mach, std::vector< std::string > &removedRFNames)
Definition: RemoveUnconnectedComponents.cc:257
DSDBManager::addConfiguration
RowID addConfiguration(const MachineConfiguration &conf)
Definition: DSDBManager.cc:299
MachineTester.hh
DSDBManager::addArchitecture
RowID addArchitecture(const TTAMachine::Machine &mom)
Definition: DSDBManager.cc:191
TTAMachine::Machine::registerFileNavigator
virtual RegisterFileNavigator registerFileNavigator() const
Definition: Machine.cc:450
DSDBManager::configuration
MachineConfiguration configuration(RowID id) const
Definition: DSDBManager.cc:361
RemoveUnconnectedComponents::requiresApplication
virtual bool requiresApplication() const
Definition: RemoveUnconnectedComponents.cc:76
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
RemoveUnconnectedComponents::checkFUPorts
void checkFUPorts(TTAMachine::Machine &mach)
Definition: RemoveUnconnectedComponents.cc:221
TTAMachine::Machine::busNavigator
virtual BusNavigator busNavigator() const
Definition: Machine.cc:356
RemoveUnconnectedComponents::readParameters
void readParameters()
Definition: RemoveUnconnectedComponents.cc:175
DSDBManager::implementation
IDF::MachineImplementation * implementation(RowID id) const
Definition: DSDBManager.cc:887
RemoveUnconnectedComponents::RemoveUnconnectedComponents
RemoveUnconnectedComponents()
Definition: RemoveUnconnectedComponents.cc:60
MachineResourceModifier.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAMachine
Definition: Assembler.hh:48
RemoveUnconnectedComponents::removeSockets
void removeSockets(TTAMachine::Machine &mach)
Definition: RemoveUnconnectedComponents.cc:323
RemoveUnconnectedComponents::requiresStartingPointArchitecture
virtual bool requiresStartingPointArchitecture() const
Definition: RemoveUnconnectedComponents.cc:72
MachineTester
Definition: MachineTester.hh:46
RemoveUnconnectedComponents::allowRemovalPN_
static const std::string allowRemovalPN_
Definition: RemoveUnconnectedComponents.cc:167
DSDBManager::MachineConfiguration::architectureID
RowID architectureID
Definition: DSDBManager.hh:79
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
DSDBManager::addImplementation
RowID addImplementation(const IDF::MachineImplementation &impl, double longestPathDelay, CostEstimator::AreaInGates area)
Definition: DSDBManager.cc:252
IDF::MachineImplementation
Definition: MachineImplementation.hh:54
RF
const string RF
Definition: IDFSerializer.cc:68
MachineResourceModifier::removeNotConnectedSockets
void removeNotConnectedSockets(TTAMachine::Machine &mach, std::list< std::string > &removedSocketNames)
Definition: MachineResourceModifier.cc:244
TTAMachine::Machine
Definition: Machine.hh:73