OpenASIP  2.0
ResourceVectorSet.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 ResourceVectorSet.cc
26  *
27  * Definition of ResourceVectorSet class.
28  *
29  * @author Pekka J��skel�inen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <algorithm>
34 
35 #include "ResourceVectorSet.hh"
36 #include "Application.hh"
37 #include "ExecutionPipeline.hh"
38 #include "PipelineElement.hh"
39 #include "HWOperation.hh"
40 #include "StringTools.hh"
41 #include "AssocTools.hh"
42 #include "FunctionUnit.hh"
43 
44 namespace TTAMachine {
45 
46 /**
47  * Builds the resource vectors for the given FU.
48  *
49  * Resource vectors are built for all operations in the FU.
50  *
51  * @param functionUnit The function unit to build the resource vectors for.
52  * @exception InvalidData In case the function unit is incomplete to build
53  * resource vectors for (e.g., missing operand-port bindings).
54  */
56  const TTAMachine::FunctionUnit& functionUnit)
57  : width_(0) {
58  try {
59  // add the port usages of each operation and resource usages
60  for (int i = 0; i < functionUnit.operationCount(); ++i) {
61  const TTAMachine::HWOperation* op = functionUnit.operation(i);
62  ResourceVector* rv =
63  new ResourceVector(*op->pipeline());
65  width_ = std::max(width_, rv->width());
66  }
67  } catch (const Exception& e) {
68  InvalidData io(
69  __FILE__, __LINE__, __func__,
70  "Error building the resource vectors");
71  io.setCause(e);
72  throw io;
73  }
74 }
75 
76 /**
77  * Destructor.
78  */
81 }
82 
83 /**
84  * Returns the resource vector associated with the given operation.
85  *
86  * @param operationName The operation name.
87  * @return The resource vector.
88  * @exception KeyNotFound If the operation is not found.
89  */
90 const ResourceVector&
91 ResourceVectorSet::resourceVector(const std::string& operationName) const {
92  const std::string opName = StringTools::stringToUpper(operationName);
93  if (!AssocTools::containsKey(vectors_, opName)) {
94  std::string message = "No resource vector found for operation " +
95  operationName + ".";
96  throw KeyNotFound(
97  __FILE__, __LINE__, __func__, message);
98  }
99  return *(*vectors_.find(opName)).second;
100 }
101 
102 /**
103  * Returns the count of resource vectors in the set.
104  *
105  * @return The count.
106  */
107 std::size_t
109  return vectors_.size();
110 }
111 
112 /**
113  * Returns the resource vector at the given index.
114  *
115  * @param index The index.
116  * @return The resource vector.
117  */
118 const ResourceVector&
119 ResourceVectorSet::resourceVector(std::size_t index) const {
120 
121  int counter = index;
122  for (ResourceVectorIndex::const_iterator i = vectors_.begin();
123  i != vectors_.end(); ++i) {
124  if (counter == 0)
125  return *((*i).second);
126  --counter;
127  }
128  // should never get here
129  return *((*vectors_.end()).second);
130 }
131 
132 /**
133  * Returns the name of the operation of the resource vector at the given index.
134  *
135  * @param index The index.
136  * @return The operation name.
137  */
138 std::string
139 ResourceVectorSet::operationName(std::size_t index) const {
140 
141  int counter = index;
142  for (ResourceVectorIndex::const_iterator i = vectors_.begin();
143  i != vectors_.end(); ++i) {
144  if (counter == 0)
145  return (*i).first;
146  --counter;
147  }
148  // should never get here
149  return (*vectors_.end()).first;
150 }
151 
152 /**
153  * Returns the index of the operation with the given name.
154  *
155  * @param operationName The name of the operation.
156  * @return Index of the operation.
157  * @exception KeyNotFound If the operation is not found.
158  */
159 std::size_t
160 ResourceVectorSet::operationIndex(const std::string& operationName) const {
161  int counter = 0;
162  for (ResourceVectorIndex::const_iterator i = vectors_.begin();
163  i != vectors_.end(); ++i) {
164  if ((*i).first == operationName)
165  return counter;
166  ++counter;
167  }
168  throw KeyNotFound(__FILE__, __LINE__, __func__, "Operation not found.");
169 }
170 
171 /**
172  * Returns the width of the longest resource vector in the resource vector set.
173  *
174  * @return The width.
175  */
176 std::size_t
178  return width_;
179 }
180 
181 
182 /**
183  * Compares two ResourceVectorSets.
184  *
185  * @param rightHand Right hand operand.
186  * @return True is the two sets match false otherwise.
187  */
188 bool
190 
191  if (resourceVectorCount() != rightHand.resourceVectorCount()) {
192  return false;
193  }
194  ResourceVectorIndex::const_iterator iter = vectors_.begin();
195  for (; iter != vectors_.end(); iter++) {
196  try {
197  if (!((*(*iter).second) ==
198  rightHand.resourceVector((*iter).first))) {
199  return false;
200  }
201  } catch (KeyNotFound& e) {
202  return false;
203  }
204  }
205  return true;
206 }
207 }
TTAMachine::ResourceVectorSet::width
std::size_t width() const
Definition: ResourceVectorSet.cc:177
TTAMachine::HWOperation
Definition: HWOperation.hh:52
ExecutionPipeline.hh
TTAMachine::ResourceVectorSet::ResourceVectorSet
ResourceVectorSet(const TTAMachine::FunctionUnit &functionUnit)
Definition: ResourceVectorSet.cc:55
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
ResourceVectorSet.hh
TTAMachine::ResourceVectorSet
Definition: ResourceVectorSet.hh:47
Exception::setCause
void setCause(const Exception &cause)
Definition: Exception.cc:75
TTAMachine::ResourceVector::width
std::size_t width() const
Definition: ResourceVector.cc:141
TTAMachine::ResourceVector
Definition: ResourceVector.hh:53
TTAMachine::ResourceVectorSet::operator==
bool operator==(const ResourceVectorSet &rightHand) const
Definition: ResourceVectorSet.cc:189
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
StringTools.hh
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::ResourceVectorSet::vectors_
ResourceVectorIndex vectors_
Storage for the resource vectors.
Definition: ResourceVectorSet.hh:72
HWOperation.hh
TTAMachine::HWOperation::name
const std::string & name() const
Definition: HWOperation.cc:141
InvalidData
Definition: Exception.hh:149
TTAMachine::ResourceVectorSet::operationIndex
std::size_t operationIndex(const std::string &operationName) const
Definition: ResourceVectorSet.cc:160
TTAMachine::ResourceVectorSet::resourceVector
const ResourceVector & resourceVector(const std::string &operationName) const
Definition: ResourceVectorSet.cc:91
Application.hh
__func__
#define __func__
Definition: Application.hh:67
TTAMachine::FunctionUnit::operationCount
virtual int operationCount() const
Definition: FunctionUnit.cc:419
Exception
Definition: Exception.hh:54
TTAMachine::ResourceVectorSet::resourceVectorCount
std::size_t resourceVectorCount() const
Definition: ResourceVectorSet.cc:108
AssocTools.hh
PipelineElement.hh
TTAMachine::HWOperation::pipeline
ExecutionPipeline * pipeline() const
Definition: HWOperation.cc:201
KeyNotFound
Definition: Exception.hh:285
TTAMachine::FunctionUnit::operation
virtual HWOperation * operation(const std::string &name) const
Definition: FunctionUnit.cc:363
TTAMachine::ResourceVectorSet::operationName
std::string operationName(std::size_t index) const
Definition: ResourceVectorSet.cc:139
TTAMachine::ResourceVectorSet::~ResourceVectorSet
virtual ~ResourceVectorSet()
Definition: ResourceVectorSet.cc:79
TTAMachine
Definition: Assembler.hh:48
AssocTools::deleteAllValues
static void deleteAllValues(ContainerType &aMap)
TTAMachine::ResourceVectorSet::width_
std::size_t width_
Width of the longest resource vector.
Definition: ResourceVectorSet.hh:74
FunctionUnit.hh