OpenASIP  2.0
EstimatorCmdLineUI.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 EstimatorCmdLineUI.cc
26  *
27  * Implementation of estimate.
28  *
29  * The command line user interface of cost estimator.
30  *
31  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
32  * @note rating: red
33  */
34 
35 #include <iostream>
36 #include <cmath>
37 
39 #include "Machine.hh"
40 #include "ADFSerializer.hh"
41 #include "MachineImplementation.hh"
42 #include "IDFSerializer.hh"
43 #include "Program.hh"
44 #include "NullProgram.hh"
45 #include "Binary.hh"
46 #include "FileSystem.hh"
47 #include "BinaryStream.hh"
48 #include "BinaryReader.hh"
49 #include "TPEFProgramFactory.hh"
50 #include "ExecutionTrace.hh"
51 #include "Estimator.hh"
52 
53 using namespace CostEstimator;
54 
55 /// this is set to true in case energy estimation can be performed with the
56 /// given input files
57 bool energyEstimation = false;
58 /// the architecture definition of the estimated processor
60 /// the implementation definition of the estimated processor
62 /// the estimated program is stored in this variable (in case energy estimation
63 /// is wanted
65 /// the execution trace database
67 
68 /**
69  * Loads the input files using the command line arguments.
70  *
71  * @param options The command line arguments.
72  * @return True in case there was no errors in the arguments.
73  */
74 bool
76 
77  if (options.numberOfArguments() < 2) {
78  std::cerr << "ADF and IDF are required." << std::endl;
79  return false;
80  }
81  else if (options.numberOfArguments() > 2) {
82  std::cerr << "Illegal command line arguments." << std::endl;
83  return false;
84  }
85 
86  std::string adfFile = options.argument(1);
87  std::string idfFile = options.argument(2);
88 
89  try {
90  ADFSerializer serializer;
91  serializer.setSourceFile(adfFile);
92  machine = serializer.readMachine();
93  } catch (const Exception& e) {
94  std::cerr << "Error while loading ADF. " << e.errorMessage()
95  << std::endl;
96  return false;
97  }
98 
99  try {
100  IDF::IDFSerializer serializer;
101  serializer.setSourceFile(idfFile);
103  } catch (const Exception& e) {
104  std::cerr << "Error while loading IDF. " << e.errorMessage()
105  << std::endl;
106  return false;
107  }
108 
109  if (options.TPEF() != "") {
110 
111  if (options.traceDB() == "") {
112  std::cerr << "Also TraceDB is required for energy estimation."
113  << std::endl;
114  return false;
115  }
116 
117  TPEF::Binary* tpef = NULL;
118  try {
119  const std::string tpefFileName = options.TPEF();
120  if (!FileSystem::fileExists(tpefFileName)) {
121  std::cerr << "Error while loading TPEF. "
122  << "Cannot open file '" << tpefFileName << "'."
123  << std::endl;
124  delete machine;
125  machine = NULL;
126  delete implementation;
127  implementation = NULL;
128  return EXIT_FAILURE;
129  }
130  TPEF::BinaryStream binaryStream(tpefFileName);
131 
132  // read first to a TPEF Handler Module
133  tpef = TPEF::BinaryReader::readBinary(binaryStream);
134 
135  assert(tpef != NULL);
136  assert(machine != NULL);
137 
138  // convert the loaded TPEF to POM
139  TTAProgram::TPEFProgramFactory factory(*tpef, *machine);
140  program = factory.build();
141  delete tpef;
142  tpef = NULL;
143  } catch (const Exception& e) {
144  std::cerr << "Error while loading TPEF. " << e.errorMessage()
145  << std::endl;
146  delete tpef;
147  tpef = NULL;
148  return false;
149  }
150  }
151 
152  if (options.traceDB() != "") {
153 
154  const std::string traceDBFileName = options.traceDB();
155  if (!FileSystem::fileExists(traceDBFileName)) {
156  std::cerr << "Error while loading TraceDB. "
157  << "Cannot open file '" << traceDBFileName << "'."
158  << std::endl;
159  return false;
160  }
161 
162  try {
163  trace = ExecutionTrace::open(traceDBFileName);
164  } catch (const Exception& e) {
165  std::cerr << "Error while loading TraceDB. " << e.errorMessage()
166  << std::endl;
167  return false;
168  }
169  if (program == NULL)
171  energyEstimation = true;
172  }
173  return true;
174 }
175 
176 /**
177  * Frees all allocated input resources.
178  */
179 void cleanup() {
180 
181  delete machine;
182  machine = NULL;
183  delete implementation;
184  implementation = NULL;
185 
186  if (energyEstimation) {
187  delete program;
188  program = NULL;
189  delete trace;
190  trace = NULL;
191  }
192 }
193 
194 /**
195  * Main function.
196  *
197  * Parses the command line and executes cost estimation functionality.
198  *
199  * @param argc The command line argument count.
200  * @param argv The command line arguments (passed to the interpreter).
201  * @return The return status.
202  */
203 int
204 main(int argc, char* argv[]) {
205 
207 
209  try {
210  options->parse(argv, argc);
212  } catch (ParserStopRequest) {
213  return EXIT_SUCCESS;
214  } catch (const IllegalCommandLine& i) {
215  std::cerr << i.errorMessage() << std::endl;
216  return EXIT_FAILURE;
217  }
218 
219  if (!loadInputs(*options)) {
220  cleanup();
221  return EXIT_FAILURE;
222  }
223 
224  Estimator estimator;
225  if (options->totalArea() || !options->runOnlyEstimations()) {
226  CostEstimator::AreaInGates totalArea = -1;
227  try {
228  totalArea = round(estimator.totalArea(*machine, *implementation));
229  } catch (const Exception& e) {
230  std::cerr << "estimation failed: " + e.errorMessage()
231  << std::endl;
232  }
233 
234  if (totalArea >= 0) {
235  std::cout << "total area: " << totalArea << " gates"
236  << std::endl;
237  }
238  }
239 
240  if (options->longestPath() || !options->runOnlyEstimations()) {
241  std::cout << "delay of the longest path: ";
242  try {
243  std::cout
244  << round(estimator.longestPath(*machine, *implementation))
245  << " ns" << std::endl;
246  } catch (const Exception& e) {
247  std::cerr << "estimation failed: " + e.errorMessage()
248  << std::endl;
249  }
250  }
251 
252  if (energyEstimation &&
253  (options->totalEnergy() || !options->runOnlyEstimations())) {
254  std::cout << "total consumed energy: ";
255  try {
256  std::cout
257  << estimator.totalEnergy(
259  << " mJ" << std::endl;
260  } catch (const Exception& e) {
261  std::cerr << "estimation failed: " + e.errorMessage()
262  << std::endl;
263  }
264  }
265 
266  cleanup();
267  return EXIT_SUCCESS;
268 }
TTAProgram::Program
Definition: Program.hh:63
BinaryReader.hh
CostEstimator::Estimator::totalArea
AreaInGates totalArea(const TTAMachine::Machine &machine, const IDF::MachineImplementation &machineImplementation)
area estimation functions
Definition: Estimator.cc:84
FileSystem.hh
ParserStopRequest
Definition: Exception.hh:491
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
implementation
IDF::MachineImplementation * implementation
the implementation definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:61
TPEF::Binary
Definition: Binary.hh:49
IDF::IDFSerializer::readMachineImplementation
MachineImplementation * readMachineImplementation()
Definition: IDFSerializer.cc:148
CostEstimator::Estimator::totalEnergy
EnergyInMilliJoules totalEnergy(const TTAMachine::Machine &machine, const IDF::MachineImplementation &machineImplementation, const TTAProgram::Program &program, const ExecutionTrace &traceDB)
energy estimation functions
Definition: Estimator.cc:433
CostEstimator::Estimator::longestPath
DelayInNanoSeconds longestPath(const TTAMachine::Machine &machine, const IDF::MachineImplementation &machineImplementation)
delay estimation functions
Definition: Estimator.cc:915
TPEF::BinaryStream
Definition: BinaryStream.hh:59
ExecutionTrace::open
void open()
Definition: ExecutionTrace.cc:169
energyEstimation
bool energyEstimation
this is set to true in case energy estimation can be performed with the given input files
Definition: EstimatorCmdLineUI.cc:57
CostEstimator::Estimator
Definition: Estimator.hh:85
Application::setCmdLineOptions
static void setCmdLineOptions(CmdLineOptions *options_)
Definition: Application.cc:381
IllegalCommandLine
Definition: Exception.hh:438
CostEstimator::AreaInGates
double AreaInGates
type for area values in equivalent gates
Definition: CostEstimatorTypes.hh:35
EstimatorCmdLineOptions
Definition: EstimatorCmdLineOptions.hh:43
Estimator.hh
loadInputs
bool loadInputs(EstimatorCmdLineOptions &options)
Definition: EstimatorCmdLineUI.cc:75
TTAProgram::TPEFProgramFactory
Definition: TPEFProgramFactory.hh:87
TTAProgram::NullProgram::instance
static NullProgram & instance()
Definition: NullProgram.cc:72
assert
#define assert(condition)
Definition: Application.hh:86
ExecutionTrace
Definition: ExecutionTrace.hh:56
ADFSerializer
Definition: ADFSerializer.hh:49
TTAProgram::TPEFProgramFactory::build
Program * build()
Definition: TPEFProgramFactory.cc:200
program
TTAProgram::Program * program
the estimated program is stored in this variable (in case energy estimation is wanted
Definition: EstimatorCmdLineUI.cc:64
main
int main(int argc, char *argv[])
Definition: EstimatorCmdLineUI.cc:204
Machine.hh
Exception
Definition: Exception.hh:54
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
TPEFProgramFactory.hh
ADFSerializer.hh
Program.hh
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Application::initialize
static void initialize()
Definition: Application.cc:99
trace
ExecutionTrace * trace
the execution trace database
Definition: EstimatorCmdLineUI.cc:66
cleanup
void cleanup()
Definition: EstimatorCmdLineUI.cc:179
EstimatorCmdLineOptions.hh
BinaryStream.hh
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
IDF::IDFSerializer
Definition: IDFSerializer.hh:45
NullProgram.hh
CostEstimator
Definition: CostEstimationPlugin.cc:36
CmdLineParser::argument
virtual std::string argument(int index) const
IDF::MachineImplementation
Definition: MachineImplementation.hh:54
MachineImplementation.hh
TPEF::BinaryReader::readBinary
static Binary * readBinary(BinaryStream &stream)
Definition: BinaryReader.cc:88
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
ExecutionTrace.hh
IDFSerializer.hh