OpenASIP  2.0
GrowMachine.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 GrowMachine.cc
26  *
27  * Explorer plugin that adds resources until cycle count doesn't go down
28  * anymore.
29  *
30  * @author Jari Mäntyneva 2007 (jari.mantyneva-no.spam-tut.fi)
31  * @author Esa Määttä 2008 (esa.maatta-no.spam-tut.fi)
32  * @note rating: red
33  */
34 
35 #include <vector>
36 #include <set>
37 #include <string>
38 
40 
41 #include "DSDBManager.hh"
42 #include "Machine.hh"
43 
45 #include "DesignSpaceExplorer.hh"
46 #include "CostEstimates.hh"
47 #include "SimulatorConstants.hh"
48 
49 #include "Conversion.hh"
50 
51 using namespace TTAProgram;
52 using namespace TTAMachine;
53 using namespace HDB;
54 
55 /**
56  * Explorer plugin that adds resources until cycle count doesn't go down
57  * anymore.
58  *
59  * Supported parameters:
60  */
62  PLUGIN_DESCRIPTION("Adds resources until cycle count doesn't go down anymore.");
63 
65  superiority_(10) {
66 
67  // compulsory parameters
68  // no compulsory parameters
69 
70  // parameters that have a default value
71  addParameter(superiorityPN_, UINT, false,
72  Conversion::toString(superiority_));
73  }
74 
75  virtual bool requiresStartingPointArchitecture() const { return true; }
76  virtual bool producesArchitecture() const { return true; }
77  virtual bool requiresHDB() const { return true; }
78  virtual bool requiresSimulationData() const { return false; }
79 
80  /**
81  * Optimizes the architecture in regards of the cycle count.
82  *
83  * Optimizes the architecture by growing it until cycle count doesn't go
84  * down anymore.
85  *
86  * @TODO: average cycle count lowering, or lowest/largest lowering
87  * percentage among apps now it's largest lowering among apps.
88  * @TODO: parametrize machine growing, example how many buses to add
89  * each step.
90  *
91  * @return Configurations (including only adf) generated by CycleOptimizer.
92  * Best result is at the top of the list.
93  */
94  virtual std::vector<RowID>
95  explore(const RowID& configurationID, const unsigned int&) {
96  std::vector<RowID> result;
97 
98  readParameters();
99 
100  TTAMachine::Machine* adf = NULL;
101  DSDBManager::MachineConfiguration configuration;
102  DSDBManager& dsdb = db();
103 
104  // get the architecture from the database
105  try {
106  configuration = dsdb.configuration(configurationID);
107  adf = dsdb.architecture(configuration.architectureID);
108  } catch (const KeyNotFound& e) {
109  debugLog(std::string("Fetching architecture from DSDB failed in "
110  "GrowMachine. ")
111  + e.errorMessage() + std::string(" ")
112  + e.errorMessageStack());
113  result.push_back(configurationID);
114  delete adf;
115  return result;
116  }
117 
118  // evaluate to get current cycle counts
119  DesignSpaceExplorer explorer;
120  explorer.setDSDB(dsdb);
121  CostEstimates estimates;
123  startConf.architectureID = dsdb.addArchitecture(*adf);
124  startConf.hasImplementation = false;
125  try {
126  bool estimate = false;
127  if (!explorer.evaluate(startConf, estimates, estimate)) {
128  debugLog(std::string("Evaluate failed in GrowMachine."));
129  result.push_back(configurationID);
130  delete adf;
131  adf = NULL;
132  return result;
133  }
134  } catch (const Exception& e) {
135  debugLog(std::string("Error in GrowMachine: ")
136  + e.errorMessage() + std::string(" ")
137  + e.errorMessageStack());
138  result.push_back(configurationID);
139  delete adf;
140  adf = NULL;
141  return result;
142  }
143 
144  std::vector<ClockCycleCount> cycleCounts =
145  db().cycleCounts(startConf);
146 
147 
148  if (cycleCounts.size() < 1) {
149  std::ostringstream msg(std::ostringstream::out);
150  msg << "GrowMachine error: Couldn't evaluate cycle "
151  << "counts for applications, correct_simulation_output"
152  << " probably missing." << std::endl;
153  verboseLog(msg.str());
154  result.push_back(configurationID);
155  delete adf;
156  adf = NULL;
157  return result;
158  }
159 
160  // all applications minimum cycle count
161  ClockCycleCount currentMinCycles = cycleCounts.at(0);
162  for (int i = 1; i < (int)cycleCounts.size(); i++) {
163  if (cycleCounts.at(i) < currentMinCycles) {
164  currentMinCycles = cycleCounts.at(i);
165  }
166  }
167 
168  ClockCycleCount prevMinCycles = 0;
169  MachineResourceModifier modifier;
170  std::map<ClockCycleCount, RowID> resultMap;
171  do {
172  prevMinCycles = currentMinCycles;
173  try {
174  // These parameters passed to the modifier can be changed.
175  // They tell how many units of same type are added each time.
176  modifier.addBusesByAmount(8, *adf);
177  modifier.increaseAllRFsThatDiffersByAmount(1, *adf);
178  modifier.increaseAllFUsThatDiffersByAmount(1, *adf);
179  // @TODO immediate unit addition
180 
181  DSDBManager::MachineConfiguration newConfiguration;
182  try {
183  newConfiguration.architectureID = dsdb.addArchitecture(*adf);
184  } catch (const RelationalDBException& e) {
185  // Error occurred while adding adf to the dsdb, adf
186  // probably too big
187  break;
188  }
189  newConfiguration.hasImplementation = false;
190  RowID confID = dsdb.addConfiguration(newConfiguration);
191  CostEstimates newEstimates;
192 
193  // evaluate to get new cycle counts
194  if (explorer.evaluate(newConfiguration, newEstimates, false)) {
195  // resets the currentMinCycles
196  std::vector<ClockCycleCount> newCycleCounts =
197  db().cycleCounts(newConfiguration);
198 
199  currentMinCycles = newCycleCounts.at(0);
200  for (int i = 1; i < (int)newCycleCounts.size(); i++) {
201  if (newCycleCounts.at(i) < currentMinCycles) {
202  currentMinCycles = newCycleCounts.at(i);
203  }
204  }
205 
206  if (Application::verboseLevel() > 0) {
207  std::ostringstream msg(std::ostringstream::out);
208  msg << "GrowMachine produced config: "
209  << confID << ", with cycle count: "
210  << currentMinCycles << " ("
211  << calculateImprovement(currentMinCycles,
212  prevMinCycles)
213  << "% better)";
214  verboseLog(msg.str())
215  }
216 
217  if (checkSuperiority(currentMinCycles, prevMinCycles)) {
218  // only add config if it meets the superiority
219  // requirements regarding clock cycles
220  resultMap[currentMinCycles] = confID;
221  } else {
222  break;
223  }
224  } else {
225  // evaluating failed
226  debugLog("GrowMachine: Evaluating config with id: "
227  + Conversion::toString(confID)
228  + " failed. This is probably a bug.");
229  break;
230  }
231 
232  } catch (const Exception& e) {
233  debugLog(std::string("Error in GrowMachine: ")
234  + e.errorMessage() + std::string(" ")
235  + e.errorMessageStack());
236  result.push_back(configurationID);
237  delete adf;
238  adf = NULL;
239  return result;
240  }
241  } while (true);
242 
243  std::map<ClockCycleCount, RowID>::const_iterator mapIter =
244  resultMap.begin();
245  for (; mapIter != resultMap.end(); mapIter++) {
246  result.push_back((*mapIter).second);
247  }
248  if (result.empty()) {
249  // if now new configuration created return the old
250  verboseLogC("GrowMachine could not generate new configs.", 2)
251  result.push_back(configurationID);
252  }
253  delete adf;
254  adf = NULL;
255  return result;
256  }
257 
258 private:
259  // parameter names
260  static const std::string superiorityPN_;
261 
262  // parameters
263  /// Percentage value of how much faster schedules are wanted until cycle
264  /// count optimization is stopped.
265  unsigned int superiority_;
266 
267  /**
268  * Reads the parameters given to the plugin.
269  */
270  void readParameters() {
271  // optional parameters
272  readOptionalParameter(superiorityPN_, superiority_);
273  }
274 
275 
276  /**
277  * Checks whether cycle counts have been lowered enough.
278  *
279  * @param newCC The new minimum cycle count.
280  * @param oldCC The old minimum cycle count.
281  */
282  inline bool checkSuperiority(
283  const ClockCycleCount& newCC,
284  const ClockCycleCount& oldCC) const {
285 
286  if ((newCC < oldCC) &&
287  (((static_cast<double>(superiority_) / 100) * oldCC) <
288  (oldCC - newCC))) {
289  return true;
290  }
291  return false;
292  }
293 
294  /**
295  * Calculates the percentage of improvement in cycle count
296  *
297  * @param newCC The new minimum cycle count.
298  * @param oldCC The old minimum cycle count.
299  */
300  inline double calculateImprovement(
301  const ClockCycleCount& newCC,
302  const ClockCycleCount& oldCC) const {
303 
304  return (1.0 -(static_cast<double>(newCC)/oldCC))*100.0;
305  }
306 
307 };
308 
309 const std::string GrowMachine::superiorityPN_("superiority");
310 
DesignSpaceExplorer::evaluate
virtual bool evaluate(const DSDBManager::MachineConfiguration &configuration, CostEstimates &results=dummyEstimate_, bool estimate=false)
Definition: DesignSpaceExplorer.cc:133
TTAProgram
Definition: Estimator.hh:65
GrowMachine::producesArchitecture
virtual bool producesArchitecture() const
Definition: GrowMachine.cc:76
GrowMachine::requiresSimulationData
virtual bool requiresSimulationData() const
Definition: GrowMachine.cc:78
HDB
Definition: CostDatabase.hh:49
CostEstimates
Definition: CostEstimates.hh:57
RelationalDBException
Definition: Exception.hh:692
MachineResourceModifier::increaseAllRFsThatDiffersByAmount
void increaseAllRFsThatDiffersByAmount(int registersToAdd, TTAMachine::Machine &mach)
Definition: MachineResourceModifier.cc:311
UINT
@ UINT
Definition: ExplorerPluginParameter.hh:40
DSDBManager::architecture
TTAMachine::Machine * architecture(RowID id) const
Definition: DSDBManager.cc:807
DSDBManager::MachineConfiguration::hasImplementation
bool hasImplementation
Definition: DSDBManager.hh:80
GrowMachine
Definition: GrowMachine.cc:61
DesignSpaceExplorerPlugin
Definition: DesignSpaceExplorerPlugin.hh:55
RowID
int RowID
Type definition of row ID in relational databases.
Definition: DBTypes.hh:37
MachineResourceModifier
Definition: MachineResourceModifier.hh:49
Application::verboseLevel
static int verboseLevel()
Definition: Application.hh:176
GrowMachine::GrowMachine
GrowMachine()
Definition: GrowMachine.cc:64
Conversion::toString
static std::string toString(const T &source)
DesignSpaceExplorerPlugin.hh
verboseLog
#define verboseLog(text)
Definition: Application.hh:115
SimulatorConstants.hh
DSDBManager::MachineConfiguration
Definition: DSDBManager.hh:78
PLUGIN_DESCRIPTION
const std::string PLUGIN_DESCRIPTION
Definition: DefaultICDecoderPlugin.cc:917
GrowMachine::readParameters
void readParameters()
Definition: GrowMachine.cc:270
EXPORT_DESIGN_SPACE_EXPLORER_PLUGIN
#define EXPORT_DESIGN_SPACE_EXPLORER_PLUGIN(PLUGIN_NAME__)
Definition: DesignSpaceExplorerPlugin.hh:125
GrowMachine::checkSuperiority
bool checkSuperiority(const ClockCycleCount &newCC, const ClockCycleCount &oldCC) const
Definition: GrowMachine.cc:282
verboseLogC
#define verboseLogC(text, neededVerbosity)
Definition: Application.hh:110
Conversion.hh
GrowMachine::requiresHDB
virtual bool requiresHDB() const
Definition: GrowMachine.cc:77
CostEstimates.hh
GrowMachine::superiority_
unsigned int superiority_
Percentage value of how much faster schedules are wanted until cycle count optimization is stopped.
Definition: GrowMachine.cc:265
Exception::errorMessageStack
std::string errorMessageStack(bool messagesOnly=false) const
Definition: Exception.cc:138
Machine.hh
Exception
Definition: Exception.hh:54
DSDBManager
Definition: DSDBManager.hh:76
MachineResourceModifier::increaseAllFUsThatDiffersByAmount
void increaseAllFUsThatDiffersByAmount(int moreFUs, TTAMachine::Machine &mach)
Definition: MachineResourceModifier.cc:451
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
DSDBManager.hh
DSDBManager::addConfiguration
RowID addConfiguration(const MachineConfiguration &conf)
Definition: DSDBManager.cc:299
GrowMachine::requiresStartingPointArchitecture
virtual bool requiresStartingPointArchitecture() const
Definition: GrowMachine.cc:75
GrowMachine::superiorityPN_
static const std::string superiorityPN_
Definition: GrowMachine.cc:260
MachineResourceModifier::addBusesByAmount
void addBusesByAmount(int busesToAdd, TTAMachine::Machine &mach)
Definition: MachineResourceModifier.cc:67
DSDBManager::addArchitecture
RowID addArchitecture(const TTAMachine::Machine &mom)
Definition: DSDBManager.cc:191
GrowMachine::calculateImprovement
double calculateImprovement(const ClockCycleCount &newCC, const ClockCycleCount &oldCC) const
Definition: GrowMachine.cc:300
DSDBManager::configuration
MachineConfiguration configuration(RowID id) const
Definition: DSDBManager.cc:361
DesignSpaceExplorer.hh
DesignSpaceExplorer::setDSDB
virtual void setDSDB(DSDBManager &dsdb)
Definition: DesignSpaceExplorer.cc:107
MachineResourceModifier.hh
ClockCycleCount
CycleCount ClockCycleCount
Alias for ClockCycleCount.
Definition: SimulatorConstants.hh:57
DesignSpaceExplorer
Definition: DesignSpaceExplorer.hh:70
KeyNotFound
Definition: Exception.hh:285
TTAMachine
Definition: Assembler.hh:48
debugLog
#define debugLog(text)
Definition: Application.hh:95
DSDBManager::MachineConfiguration::architectureID
RowID architectureID
Definition: DSDBManager.hh:79
TTAMachine::Machine
Definition: Machine.hh:73
GrowMachine::explore
virtual std::vector< RowID > explore(const RowID &configurationID, const unsigned int &)
Definition: GrowMachine.cc:95