OpenASIP  2.0
ResourceBroker.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 ResourceBroker.cc
26  *
27  * Implementation of ResourceBroker class.
28  *
29  * @author Ari Mets�halme 2006 (ari.metsahalme-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <boost/format.hpp>
34 
35 #include "ResourceBroker.hh"
36 #include "SchedulingResource.hh"
37 #include "MapTools.hh"
38 #include "MachinePart.hh"
39 
41 
42 using std::pair;
43 using std::string;
44 using namespace TTAMachine;
45 
46 /**
47  * Constructor.
48  */
50  unsigned int initiationInterval):
51  initiationInterval_(initiationInterval),
52  resourceMapper_(NULL),
53  brokerName_(name){
54 }
55 
56 /**
57  * Destructor.
58  */
61 }
62 
63 /**
64  * Return true if one of the resources managed by this broker is
65  * suitable for the request contained in the node and can be assigned
66  * to it in given cycle.
67  *
68  * @param cycle Cycle.
69  * @param node Node.
70  * @return True if one of the resources managed by this broker is
71  * suitable for the request contained in the node and can be assigned
72  * to it in given cycle.
73  */
74 bool
76  const TTAMachine::Bus*,
78  const TTAMachine::FunctionUnit*, int,
80  int) const {
81 
82  for (ResourceMap::const_iterator resIter = resMap_.begin();
83  resIter != resMap_.end(); resIter++) {
84 
85  // IUBroker overrides this so no need for immu check
86  const SchedulingResource* res = (*resIter).second;
87  if (res->isAvailable(cycle) && res->canAssign(cycle, node)) {
88  return true;
89  }
90  }
91 
92  return false;
93 }
94 
95 /**
96  * Return one (any) resource managed by this broker that can be
97  * assigned to the given node in the given cycle.
98  *
99  * If no change occurs to the state of the resources, the broker
100  * should always return the same object. If a resource of the type
101  * managed by this broker is already assigned to the node, it is
102  * returned.
103  *
104  * @param cycle Cycle.
105  * @param node Node.
106  * @return One (any) resource managed by this broker that can be
107  * assigned to the given node in the given cycle.
108  * @exception InstanceNotFound If no available resource is found.
109  */
112  const TTAMachine::Bus*,
114  const TTAMachine::FunctionUnit*, int,
115  const TTAMachine::ImmediateUnit*, int)
116  const {
117 
118  for (ResourceMap::const_iterator resIter = resMap_.begin();
119  resIter != resMap_.end(); resIter++) {
120 
121  SchedulingResource* res = (*resIter).second;
122  if (res->isAvailable(cycle)
123  && res->canAssign(cycle, node)) {
124  return *res;
125  }
126  }
127 
128  string msg = "No available resource found.";
129  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
130 }
131 
132 /**
133  * Return all resources managed by this broker that can be assigned to
134  * the given node in the given cycle.
135  *
136  * @return All resources managed by this broker that can be assigned to
137  * the given node in the given cycle.
138  * @param cycle Cycle.
139  * @param node Node.
140  * @note This default implementation always returns an empty set.
141  */
144  const TTAMachine::Bus*,
147  int,
149  int) const {
150  SchedulingResourceSet resourceSet;
151  return resourceSet;
152 }
153 
154 /**
155  * Tells whether the given resource is available for given node at
156  * given cycle.
157  */
159  SchedulingResource& res, const MoveNode& node, int cycle,
160  const TTAMachine::Bus* bus,
161  const TTAMachine::FunctionUnit* srcFU,
162  const TTAMachine::FunctionUnit* dstFU,
163  int immWriteCycle,
164  const TTAMachine::ImmediateUnit* immu,
165  int immRegIndex) const {
167  allAvailableResources(cycle, node, bus, srcFU, dstFU,
168  immWriteCycle, immu, immRegIndex);
169  return resources.hasResource(res);
170 }
171 
172 /**
173  * Return the machine part that models the given resource.
174  *
175  * @param r Scheduling resource.
176  * @return The machine part that models the given resource.
177  * @exception KeyNotFound If no corresponding machine part is found or
178  * r is not one of the primary resources of this broker.
179  */
182  const TTAMachine::MachinePart* machinePart = NULL;
183  try {
184  machinePart = MapTools::keyForValue<const MachinePart*>(resMap_, &r);
185  } catch (const Exception& e) {
186  string msg = "Machine part of for resource ";
187  msg += r.name();
188  msg += " not found!";
189  throw KeyNotFound(__FILE__, __LINE__, __func__, msg);
190  }
191  return *machinePart;
192 }
193 
194 /**
195  * Return true if this broker holds a resource instance that models
196  * given machine part.
197  *
198  * @param mp Machine part.
199  * @return True if this broker holds a resource instance that models
200  * given machine part.
201  */
202 bool
204  return MapTools::containsKey(resMap_, &mp);
205 }
206 
207 /**
208  * Return true if this broker holds given resource instance.
209  *
210  * @param r Scheduling resource.
211  * @return True if this broker holds given resource instance.
212  */
213 bool
215  return MapTools::containsValue(resMap_, &r);
216 }
217 
218 /**
219  * Set the resource mapper for this broker.
220  *
221  * @param mapper Resource mapper.
222  */
223 void
225  resourceMapper_ = &mapper;
226 }
227 
228 /**
229  * Return the resource mapper set for this broker.
230  *
231  * @return The resource mapper set for this broker.
232  */
233 const ResourceMapper&
235  return *resourceMapper_;
236 }
237 
238 
239 /**
240  * Return the instruction index corresponding to cycle.
241  *
242  * If modulo scheduling is not used (ie. initiation interval is 0), then
243  * index is equal to cycle.
244  *
245  * @param cycle Cycle to get instruction index.
246  * @return Return the instruction index for cycle.
247  */
248 unsigned int
249 ResourceBroker::instructionIndex(unsigned int cycle) const {
250 
251  if (initiationInterval_ != 0) {
252  return cycle % initiationInterval_;
253  } else {
254  return cycle;
255  }
256 }
257 
258 /**
259  * Add resource - machine part pair to the broker.
260  *
261  * @param mp Machine part.
262  * @param res Corresponding resource.
263  */
264 void
266  const TTAMachine::MachinePart& mp,
267  SchedulingResource* res) {
268 
269  resMap_.insert(
270  pair<const TTAMachine::MachinePart*, SchedulingResource*>(&mp, res));
271 }
272 
273 /**
274  * Get resources
275  *
276  * @param contents Set to which resources are copied to.
277  */
278 void
279 ResourceBroker::resources(std::set<SchedulingResource*>& contents) {
280  for (ResourceMap::iterator i = resMap_.begin(); i != resMap_.end(); ++i ) {
281  contents.insert(i->second);
282  }
283 }
284 
285 
286 /**
287  * Set initiation interval, if ii = 0 then initiation interval is not used.
288  *
289  * @param ii initiation interval
290  */
291 void
293 {
294  initiationInterval_ = ii;
295 }
296 
297 int
299  return resMap_.size();
300 }
301 
302 bool
304  return false;
305 }
306 
307 bool
309  return false;
310 }
311 
312 bool
314  return false;
315 }
316 
317 bool
319  return false;
320 }
321 
322 /**
323  * Tests if is each resource has correct related and dependent groups.
324  *
325  * @throw ModuleRunTimeError in case resource does not have correct related
326  * or dependent resources.
327  */
328 void
330  ResourceMap::const_iterator itr = resMap_.begin();
331  while (itr != resMap_.end()) {
332  if (!(*itr).second->validateDependentGroups()) {
333  std::string msg = (boost::format(
334  "Broker '%s' failed to validate DependentGroup for '%s'!") %
335  brokerName() % (*itr).second->name()).str();
336  throw ModuleRunTimeError(__FILE__, __LINE__, __func__, msg);
337  }
338  if (!(*itr).second->validateRelatedGroups()){
339  std::string msg = (boost::format(
340  "Broker '%s' failed to validate RelatedGroup for '%s'!") %
341  brokerName() % (*itr).second->name()).str();
342  throw ModuleRunTimeError(__FILE__, __LINE__, __func__, msg);
343  }
344  ++itr;
345  }
346  return;
347 }
348 
349 /**
350  * Returns the name of particular broker.
351  * Used for debugging mainly.
352  *
353  * @return Name of a broker.
354  */
355 std::string
357  return brokerName_;
358 }
359 
360 /**
361  * Clears all resources managed by the broker so that the broker
362  * can be reused.
363  */
364 void
366  // Call clear for all resources.
367  for (ResourceMap::iterator i = resMap_.begin(); i != resMap_.end(); i++) {
368  i->second->clear();
369  }
370  assignedResources_.clear();
371 }
ResourceBroker::~ResourceBroker
virtual ~ResourceBroker()
Definition: ResourceBroker.cc:59
ResourceBroker::allAvailableResources
virtual SchedulingResourceSet allAvailableResources(int cycle, const MoveNode &node, const TTAMachine::Bus *bus, const TTAMachine::FunctionUnit *srcFU, const TTAMachine::FunctionUnit *dstFU, int immWriteCycle, const TTAMachine::ImmediateUnit *immu, int immRegIndex) const
Definition: ResourceBroker.cc:143
ResourceBroker::initiationInterval_
unsigned int initiationInterval_
Definition: ResourceBroker.hh:158
ResourceBroker::resMap_
ResourceMap resMap_
Definition: ResourceBroker.hh:165
MapTools.hh
TTAMachine::Bus
Definition: Bus.hh:53
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
ResourceBroker::hasResource
bool hasResource(const SchedulingResource &r) const
Definition: ResourceBroker.cc:214
MoveNode
Definition: MoveNode.hh:65
ResourceBroker::assignedResources_
MoveResMap assignedResources_
Definition: ResourceBroker.hh:167
ResourceBroker::brokerName
virtual std::string brokerName() const
Definition: ResourceBroker.cc:356
ResourceBroker::addResource
void addResource(const TTAMachine::MachinePart &mp, SchedulingResource *res)
Definition: ResourceBroker.cc:265
ResourceBroker::hasResourceOf
bool hasResourceOf(const TTAMachine::MachinePart &mp) const
Definition: ResourceBroker.cc:203
MachinePart.hh
SchedulingResourceSet
Definition: SchedulingResource.hh:161
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
ResourceBroker::resources
void resources(ResourceSet &contents)
Definition: ResourceBroker.cc:279
ResourceBroker::setResourceMapper
void setResourceMapper(const ResourceMapper &mapper)
Definition: ResourceBroker.cc:224
SchedulingResource
Definition: SchedulingResource.hh:52
SchedulingResource.hh
__func__
#define __func__
Definition: Application.hh:67
ResourceBroker::isAvailable
virtual bool isAvailable(SchedulingResource &des, const MoveNode &node, int cycle, const TTAMachine::Bus *bus, const TTAMachine::FunctionUnit *srcFU, const TTAMachine::FunctionUnit *dstFU, int immWriteCycle, const TTAMachine::ImmediateUnit *immu, int immRegIndex) const
Definition: ResourceBroker.cc:158
ResourceBroker::clear
virtual void clear()
Definition: ResourceBroker.cc:365
TTAMachine::MachinePart
Definition: MachinePart.hh:57
ShortImmPSocketResource.hh
ResourceBroker::setInitiationInterval
virtual void setInitiationInterval(unsigned int cycles)
Definition: ResourceBroker.cc:292
ResourceBroker::validateResources
void validateResources() const
Definition: ResourceBroker.cc:329
ResourceBroker::isAnyResourceAvailable
virtual bool isAnyResourceAvailable(int cycle, const MoveNode &node, const TTAMachine::Bus *bus, const TTAMachine::FunctionUnit *srcFU, const TTAMachine::FunctionUnit *dstFU, int immWriteCycle, const TTAMachine::ImmediateUnit *immu, int immRegIndex) const
Definition: ResourceBroker.cc:75
ResourceBroker::isITemplateBroker
virtual bool isITemplateBroker() const
Definition: ResourceBroker.cc:308
Exception
Definition: Exception.hh:54
ModuleRunTimeError
Definition: Exception.hh:1043
ResourceBroker::availableResource
virtual SchedulingResource & availableResource(int cycle, const MoveNode &node, const TTAMachine::Bus *bus, const TTAMachine::FunctionUnit *srcFU, const TTAMachine::FunctionUnit *dstFU, int immWriteCycle, const TTAMachine::ImmediateUnit *immu, int immRegIndex) const
Definition: ResourceBroker.cc:111
ResourceBroker::isExecutionPipelineBroker
virtual bool isExecutionPipelineBroker() const
Definition: ResourceBroker.cc:318
ResourceBroker::resourceCount
int resourceCount() const
Definition: ResourceBroker.cc:298
ResourceBroker::isIUBroker
virtual bool isIUBroker() const
Definition: ResourceBroker.cc:313
ResourceBroker.hh
SchedulingResource::isAvailable
virtual bool isAvailable(const int cycle) const =0
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
SchedulingResource::canAssign
virtual bool canAssign(const int cycle, const MoveNode &node) const =0
MapTools::containsValue
static bool containsValue(const MapType &aMap, const ValueType &aValue)
ResourceBroker::instructionIndex
unsigned int instructionIndex(unsigned int) const
Definition: ResourceBroker.cc:249
ResourceMapper
Definition: ResourceMapper.hh:51
ResourceBroker::ResourceBroker
ResourceBroker(std::string, unsigned int initiationInterval=0)
Definition: ResourceBroker.cc:49
ResourceBroker::resourceMapper
const ResourceMapper & resourceMapper() const
Definition: ResourceBroker.cc:234
ResourceBroker::machinePartOf
virtual const TTAMachine::MachinePart & machinePartOf(const SchedulingResource &r) const
Definition: ResourceBroker.cc:181
KeyNotFound
Definition: Exception.hh:285
SchedulingResource::name
virtual const std::string & name() const
ResourceBroker::resourceMapper_
const ResourceMapper * resourceMapper_
Definition: ResourceBroker.hh:166
TTAMachine
Definition: Assembler.hh:48
ResourceBroker::isBusBroker
virtual bool isBusBroker() const
Definition: ResourceBroker.cc:303
InstanceNotFound
Definition: Exception.hh:304
ResourceBroker::brokerName_
std::string brokerName_
Definition: ResourceBroker.hh:168
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50