OpenASIP  2.0
CostDBEntryStats.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 CostDBEntryStats.cc
26  *
27  * Implementation of CostDBEntryStats class.
28  *
29  * @author Tommi Rantanen 2003 (tommi.rantanen-no.spam-tut.fi)
30  * @author Jari Mäntyneva 2005 (jari.mantyneva-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include <map>
35 #include <string>
36 
37 #include "Application.hh"
38 #include "CostDBEntryStats.hh"
39 
40 using std::string;
41 using std::map;
42 
43 // warning: is it possible that operation can have one of the
44 // following names?
45 const std::string CostDBEntryStats::ENERGY_ACTIVE = "(active)";
46 const std::string CostDBEntryStats::ENERGY_IDLE = "(idle)";
47 const std::string CostDBEntryStats::ENERGY_READ = "(read)";
48 const std::string CostDBEntryStats::ENERGY_WRITE = "(write)";
49 const std::string CostDBEntryStats::ENERGY_READ_WRITE = "(rd,wr)";
50 
51 
52 /**
53  * Constructor.
54  *
55  * @param areaData area.
56  * @param internalDelayData Components internal delay.
57  */
58 CostDBEntryStats::CostDBEntryStats(double areaData, double internalDelayData) :
59  area_(areaData), delay_(internalDelayData) {
60 }
61 
62 /**
63  * Constructor.
64  *
65  * Combines two statistics into one using coefficient as a weighting
66  * factor on interpolating the statistics. For example, if weighting
67  * factor is 0.4, the first area 100 and the second 200, area of new
68  * statistics will be 140. delay and energy will be handled similarly.
69  *
70  * @param stats1 First statistics.
71  * @param stats2 Second statistics.
72  * @param coefficient Weighting factor.
73  * @exception KeyNotFound Thrown in case that the two statistics won't contain
74  * equal keys.
75  */
77  const CostDBEntryStats& stats1, const CostDBEntryStats& stats2,
78  double coefficient) {
79  area_ = stats1.area() + coefficient * (stats2.area() - stats1.area());
80  delay_ = stats1.delay() + coefficient * (stats2.delay() - stats1.delay());
81 
82  for (EnergyMap::const_iterator i = stats1.energies_.begin();
83  i != stats1.energies_.end(); i++) {
84 
85  string key = (*i).first;
86  double energy1 = (*i).second;
87  double energy2 = stats2.findEnergy(key);
88 
89  double energy = energy1 + coefficient * (energy2 - energy1);
90 
91  addEnergy(key, energy);
92  }
93 
94  // checks that the energy keys are equal in both statistics
95  for (EnergyMap::const_iterator i = stats2.energies_.begin();
96  i != stats2.energies_.end(); i++) {
97 
98  string key = (*i).first;
99  // checks if the energy exist for the key
100  stats1.findEnergy(key);
101  }
102  // check also keys of the other stats
103  for (EnergyMap::const_iterator i = stats1.energies_.begin();
104  i != stats1.energies_.end(); i++) {
105 
106  string key = (*i).first;
107  // checks if the energy exist for the key
108  stats2.findEnergy(key);
109  }
110 
111  for (DelayMap::const_iterator i = stats1.delays_.begin();
112  i != stats1.delays_.end(); i++) {
113 
114  string key = (*i).first;
115  double delay1 = (*i).second;
116  double delay2 = stats2.findDelay(key);
117 
118  double delay = delay1 + coefficient * (delay2 - delay1);
119 
120  addDelay(key, delay);
121  }
122 
123  // checks that the delay keys are equal in both statistics
124  for (DelayMap::const_iterator i = stats2.delays_.begin();
125  i != stats2.delays_.end(); i++) {
126 
127  string key = (*i).first;
128  // checks if the delay exist for the key
129  stats1.findDelay(key);
130  }
131  // check also keys of the other stats
132  for (DelayMap::const_iterator i = stats1.delays_.begin();
133  i != stats1.delays_.end(); i++) {
134 
135  string key = (*i).first;
136  // checks if the delay exist for the key
137  stats2.findDelay(key);
138  }
139 }
140 
141 /**
142  * Destructor.
143  */
145 }
146 
147 /**
148  * Returns the copy of the statistics.
149  *
150  * @return Copy of statistics.
151  */
154 
155  CostDBEntryStats* newStats = createStats();
156 
157  for (EnergyMap::const_iterator i = energies_.begin();
158  i != energies_.end(); i++) {
159 
160  newStats->addEnergy((*i).first, (*i).second);
161  }
162  for (DelayMap::const_iterator i = delays_.begin();
163  i != delays_.end(); i++) {
164 
165  newStats->setDelay((*i).first, (*i).second);
166  }
167 
168  return newStats;
169 }
170 
171 /**
172  * Create correct type of statistics.
173  *
174  * @return Correct type of statistics.
175  */
178 
179  return new CostDBEntryStats(area(), delay());
180 }
181 
182 /**
183  * Returns the energy of an entry in an active cycle.
184  *
185  * @return The energy of an entry in an active cycle.
186  * @exception WrongSubclass Never thrown.
187  * @exception KeyNotFound Thrown if active energy is not set.
188  */
189 double
191  return findEnergy(ENERGY_ACTIVE);
192 }
193 
194 /**
195  * Returns the energy of an entry in an idle cycle.
196  *
197  * @return The energy of an entry in an idle cycle.
198  * @exception WrongSubclass Never thrown by this function.
199  * @exception KeyNotFound Thrown if idle enrgy is not set.
200  */
201 double
203  return findEnergy(ENERGY_IDLE);
204 }
205 
206 /**
207  * Returns the energy of an entry when given operation is executed.
208  *
209  * The function will fail since this class should be used for entries
210  * which do not have operation set as a search key.
211  *
212  * @param name The name of the operation.
213  * @return The energy of an entry when given operation is executed.
214  * @exception WrongSubclass An illegal function was called for this
215  * instance.
216  * @exception KeyNotFound No energy matching the string found.
217  */
218 double
219 CostDBEntryStats::energyOperation(const std::string&) const {
220  throw WrongSubclass(__FILE__, __LINE__,
221  "CostDBEntryStats::energyOperation");
222  return 0.0; // stupid return statement to make compiler quiet
223 }
224 
225 /**
226  * Checks whether the energy exists for the given operation.
227  *
228  * @param name name of the operation.
229  * @return True if the energy exists for the given operation.
230  * @exception WrongSubclass An illegal function was called for this
231  * instance.
232  */
233 bool
234 CostDBEntryStats::hasEnergyOperation(const std::string&) const {
235  throw WrongSubclass(__FILE__, __LINE__,
236  "CostDBEntryStats::hasEnergyOperation");
237  return false; // stupid return statement to make compiler quiet
238 }
239 
240 /**
241  * Returns the read energy of an entry.
242  *
243  * @return The read energy of an entry.
244  * @exception WrongSubclass An illegal function was called for this
245  * instance.
246  * @exception KeyNotFound Never thrown by this function.
247  */
248 double
250  throw WrongSubclass(__FILE__, __LINE__,
251  "CostDBEntryStats::energyRead");
252  return 0.0; // stupid return statement to make compiler quiet
253 }
254 
255 /**
256  * Returns the write energy of an entry.
257  *
258  * @return The write energy of an entry.
259  * @exception WrongSubclass An illegal function was called for this
260  * instance.
261  * @exception KeyNotFound Never thrown by this function.
262  */
263 double
265  throw WrongSubclass(__FILE__, __LINE__,
266  "CostDBEntryStats::energyRead");
267  return 0.0; // stupid return statement to make compiler quiet
268 }
269 
270 /**
271  * Returns the reads and writes energy of an entry.
272  *
273  * @param reads The number of simultaneus reads done for the unit.
274  * @param writes The number of simultaneus writes done for the unit.
275  * @return The reads and writes energy of an entry.
276  * @exception WrongSubclass An illegal function was called for this
277  * instance.
278  * @exception KeyNotFound Never thrown by this function.
279  */
280 double
282  throw WrongSubclass(__FILE__, __LINE__,
283  "CostDBEntryStats::energyReadsWrites");
284  return 0.0; // stupid return statement to make compiler quiet
285 }
286 
287 /**
288  * Checks whether the energy exists for a given key.
289  *
290  * @param key The key.
291  * @return True if energy exists for a given key, otherwise false.
292  */
293 bool
294 CostDBEntryStats::hasEnergy(const std::string& key) const {
295 
296  EnergyMap::const_iterator i = energies_.find(key);
297  return i != energies_.end();
298 }
299 
300 /**
301  * Returns the energy of an entry for a given key.
302  *
303  * @param key The key.
304  * @return The energy of an entry in an active cycle.
305  * @exception KeyNotFound Requested key was not found.
306  */
307 double
308 CostDBEntryStats::findEnergy(const std::string& key) const {
309  EnergyMap::const_iterator i = energies_.find(key);
310  if (i == energies_.end()) {
311  throw KeyNotFound(__FILE__, __LINE__, "CostDBEntryStats::findEnergy");
312  }
313  return i->second;
314 }
315 
316 /**
317  * Set the energy of an entry in an active cycle.
318  *
319  * @param energy The energy of an entry in an active cycle.
320  * @exception WrongSubclass Never thrown by this function.
321  */
322 void
324  EnergyMap::iterator iter = energies_.find(ENERGY_ACTIVE);
325  if (iter != energies_.end()) {
326  energies_.erase(iter);
327  }
328  addEnergy(ENERGY_ACTIVE, energy);
329 }
330 
331 /**
332  * Set the energy of an entry in an idle cycle.
333  *
334  * @param energy The energy of an entry in an idle cycle.
335  * @exception WrongSubclass Never thrown by this function.
336  */
337 void
339  EnergyMap::iterator iter = energies_.find(ENERGY_IDLE);
340  if (iter != energies_.end()) {
341  energies_.erase(iter);
342  }
343  addEnergy(ENERGY_IDLE, energy);
344 }
345 
346 /**
347  * Set the energy of an entry when given operation is executed.
348  *
349  * The function will fail since this class should be used for entries
350  * which do not have operation set as a search key.
351  *
352  * @param name name of the operation.
353  * @param energy The energy of an entry when given operation is executed.
354  * @exception WrongSubclass An illegal function was called for this
355  * instance.
356  */
357 void
358 CostDBEntryStats::setEnergyOperation(const std::string&, double) {
359  throw WrongSubclass(__FILE__, __LINE__,
360  "CostDBEntryStats::setEnergyOperation");
361 }
362 
363 /**
364  * Set the read energy of an entry.
365  *
366  * @param energy The read energy of an entry.
367  * @exception WrongSubclass An illegal function was called for this
368  * instance.
369  */
370 void
372  throw WrongSubclass(__FILE__, __LINE__,
373  "CostDBEntryStats::setEnergyRead");
374 }
375 
376 /**
377  * Set the write energy of an entry.
378  *
379  * @param energy The write energy of an entry.
380  * @exception WrongSubclass An illegal function was called for this
381  * instance.
382  */
383 void
385  throw WrongSubclass(__FILE__, __LINE__,
386  "CostDBEntryStats::setEnergyWrite");
387 }
388 
389 /**
390  * Set the reads and writes energy of an entry.
391  *
392  * @param energy The reads and writes energy of an entry.
393  * @param reads The number of reads of the unit.
394  * @param writes The number of writes of the unit.
395  * @exception WrongSubclass An illegal function was called for this
396  * instance.
397  */
398 void
400  throw WrongSubclass(__FILE__, __LINE__,
401  "CostDBEntryStats::setEnergyReadsWrites");
402 }
403 
404 /**
405  * Set the energy of an entry for a given key.
406  *
407  * Removes the old value.
408  *
409  * @param key The key.
410  * @param energy The energy of an entry for a given key.
411  */
412 void
413 CostDBEntryStats::addEnergy(const std::string& key, double energy) {
414 
415  // if old value for energy is found it is removed first
416  EnergyMap::iterator iter = energies_.find(key);
417  if (iter != energies_.end()) {
418  energies_.erase(iter);
419  }
420  std::pair<const std::string, double> energyKey =
421  std::pair<const std::string, double>(key, energy);
422  energies_.insert(energyKey);
423 }
424 
425 /**
426  * Returns the delay of given port is usage an an input/output.
427  *
428  * @param port The name of the port.
429  * @return The delay of an entry when given port is used.
430  * @exception WrongSubclass Never thrown by this function.
431  * @exception KeyNotFound Thrown if delay for given port is not set.
432  */
433 double
434 CostDBEntryStats::delayPort(const std::string& port) const {
435  return findDelay(port);
436 }
437 
438 /**
439  * Set the delay of an input/output port usage.
440  *
441  * @param port Name of the input/output port.
442  * @param delay The delay of an input/ouput when using the given port.
443  * @exception WrongSubclass Never thrown by this function.
444  */
445 void
446 CostDBEntryStats::setDelay(const std::string& port, double delay) {
447  addDelay(port, delay);
448 }
449 
450 /**
451  * Returns the input/output delay of an entry usign given key as an
452  * input/output port.
453  *
454  * @param key The key.
455  * @return The input/output delay using the given port.
456  * @exception KeyNotFound Requested key was not found.
457  */
458 double
459 CostDBEntryStats::findDelay(const std::string& key) const {
460  DelayMap::const_iterator i = delays_.find(key);
461 
462  if (i == delays_.end()) {
463  throw KeyNotFound(__FILE__, __LINE__, "CostDBEntryStats::findDelay");
464  }
465  return i->second;
466 }
467 
468 /**
469  * Set the delay of an entry for a given key.
470  *
471  * Removes the old value.
472  *
473  * @param key The key.
474  * @param delay The delay of an entry for a given key.
475  */
476 void
477 CostDBEntryStats::addDelay(const std::string& key, double delay) {
478 
479  // if old value for delay is found it is removed first
480  DelayMap::iterator iter = delays_.find(key);
481  if (iter != delays_.end()) {
482  delays_.erase(iter);
483  }
484  std::pair<const std::string, double> delayKey =
485  std::pair<const std::string, double>(key, delay);
486  delays_.insert(delayKey);
487 }
488 
489 /**
490  * Checks whether the delay exists for a given key.
491  *
492  * @param key The key.
493  * @return True if delay exists for a given key, otherwise false.
494  */
495 bool
496 CostDBEntryStats::hasDelay(const std::string& key) const {
497 
498  DelayMap::const_iterator i = delays_.find(key);
499  return i != delays_.end();
500 }
CostDBEntryStats::ENERGY_WRITE
static const std::string ENERGY_WRITE
String for write energy.
Definition: CostDBEntryStats.hh:92
CostDBEntryStats::setEnergyWrite
virtual void setEnergyWrite(double energy)
Definition: CostDBEntryStats.cc:384
CostDBEntryStats::delays_
DelayMap delays_
input and output delays in nanoseconds.
Definition: CostDBEntryStats.hh:107
CostDBEntryStats::ENERGY_READ_WRITE
static const std::string ENERGY_READ_WRITE
String for reads and writes energy.
Definition: CostDBEntryStats.hh:94
CostDBEntryStats::hasEnergy
virtual bool hasEnergy(const std::string &key) const
Definition: CostDBEntryStats.cc:294
CostDBEntryStats::delay
virtual double delay() const
CostDBEntryStats::delay_
double delay_
delay in nanoseconds.
Definition: CostDBEntryStats.hh:105
CostDBEntryStats::ENERGY_IDLE
static const std::string ENERGY_IDLE
String for idle energy.
Definition: CostDBEntryStats.hh:88
CostDBEntryStats.hh
CostDBEntryStats::createStats
virtual CostDBEntryStats * createStats() const
Definition: CostDBEntryStats.cc:177
CostDBEntryStats::energyActive
virtual double energyActive() const
Definition: CostDBEntryStats.cc:190
CostDBEntryStats::energyOperation
virtual double energyOperation(const std::string &name) const
Definition: CostDBEntryStats.cc:219
CostDBEntryStats::hasDelay
virtual bool hasDelay(const std::string &key) const
Definition: CostDBEntryStats.cc:496
WrongSubclass
Definition: Exception.hh:336
CostDBEntryStats::setEnergyRead
virtual void setEnergyRead(double energy)
Definition: CostDBEntryStats.cc:371
CostDBEntryStats::ENERGY_READ
static const std::string ENERGY_READ
String for read energy.
Definition: CostDBEntryStats.hh:90
Application.hh
CostDBEntryStats::setDelay
virtual void setDelay(const std::string &port, double delay)
Definition: CostDBEntryStats.cc:446
CostDBEntryStats::~CostDBEntryStats
virtual ~CostDBEntryStats()
Definition: CostDBEntryStats.cc:144
CostDBEntryStats::addDelay
virtual void addDelay(const std::string &name, double delay)
Definition: CostDBEntryStats.cc:477
CostDBEntryStats::area
virtual double area() const
CostDBEntryStats::copy
virtual CostDBEntryStats * copy() const
Definition: CostDBEntryStats.cc:153
CostDBEntryStats::findDelay
virtual double findDelay(const std::string &key) const
Definition: CostDBEntryStats.cc:459
CostDBEntryStats::setEnergyIdle
virtual void setEnergyIdle(double energy)
Definition: CostDBEntryStats.cc:338
CostDBEntryStats::energies_
EnergyMap energies_
Energies of one specific type of usage of a resource. Usually the map contains energies for active an...
Definition: CostDBEntryStats.hh:112
CostDBEntryStats::addEnergy
virtual void addEnergy(const std::string &name, double energy)
Definition: CostDBEntryStats.cc:413
CostDBEntryStats::energyWrite
virtual double energyWrite() const
Definition: CostDBEntryStats.cc:264
CostDBEntryStats::ENERGY_ACTIVE
static const std::string ENERGY_ACTIVE
String for active energy.
Definition: CostDBEntryStats.hh:86
CostDBEntryStats::setEnergyActive
virtual void setEnergyActive(double energy)
Definition: CostDBEntryStats.cc:323
CostDBEntryStats::findEnergy
virtual double findEnergy(const std::string &key) const
Definition: CostDBEntryStats.cc:308
KeyNotFound
Definition: Exception.hh:285
CostDBEntryStats::setEnergyReadWrite
virtual void setEnergyReadWrite(int reads, int writes, double energy)
Definition: CostDBEntryStats.cc:399
CostDBEntryStats::hasEnergyOperation
virtual bool hasEnergyOperation(const std::string &name) const
Definition: CostDBEntryStats.cc:234
CostDBEntryStats::energyRead
virtual double energyRead() const
Definition: CostDBEntryStats.cc:249
CostDBEntryStats
Definition: CostDBEntryStats.hh:46
CostDBEntryStats::energyIdle
virtual double energyIdle() const
Definition: CostDBEntryStats.cc:202
CostDBEntryStats::area_
double area_
area in gates.
Definition: CostDBEntryStats.hh:103
CostDBEntryStats::CostDBEntryStats
CostDBEntryStats(double areaData, double internalDelayData)
Definition: CostDBEntryStats.cc:58
CostDBEntryStats::setEnergyOperation
virtual void setEnergyOperation(const std::string &name, double energy)
Definition: CostDBEntryStats.cc:358
CostDBEntryStats::energyReadWrite
virtual double energyReadWrite(int reads, int writes) const
Definition: CostDBEntryStats.cc:281
CostDBEntryStats::delayPort
virtual double delayPort(const std::string &port) const
Definition: CostDBEntryStats.cc:434