OpenASIP 2.2
Loading...
Searching...
No Matches
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
42using std::pair;
43using std::string;
44using 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 */
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 */
74bool
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 {
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 */
202bool
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 */
213bool
217
218/**
219 * Set the resource mapper for this broker.
220 *
221 * @param mapper Resource mapper.
222 */
223void
227
228/**
229 * Return the resource mapper set for this broker.
230 *
231 * @return The resource mapper set for this broker.
232 */
233const ResourceMapper&
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 */
248unsigned int
249ResourceBroker::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 */
264void
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 */
278void
279ResourceBroker::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 */
291void
296
297int
299 return resMap_.size();
300}
301
302bool
304 return false;
305}
306
307bool
309 return false;
310}
311
312bool
314 return false;
315}
316
317bool
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 */
328void
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 */
355std::string
357 return brokerName_;
358}
359
360/**
361 * Clears all resources managed by the broker so that the broker
362 * can be reused.
363 */
364void
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}
#define __func__
static KeyType keyForValue(const MapType &aMap, const ValueType &aValue)
static void deleteAllValues(MapType &aMap)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
static bool containsValue(const MapType &aMap, const ValueType &aValue)
void addResource(const TTAMachine::MachinePart &mp, SchedulingResource *res)
virtual bool isIUBroker() const
const ResourceMapper * resourceMapper_
virtual std::string brokerName() const
int resourceCount() const
void validateResources() const
std::string brokerName_
virtual bool isExecutionPipelineBroker() const
MoveResMap assignedResources_
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
unsigned int instructionIndex(unsigned int) const
const ResourceMapper & resourceMapper() const
virtual void setInitiationInterval(unsigned int cycles)
bool hasResource(const SchedulingResource &r) const
ResourceBroker(std::string, unsigned int initiationInterval=0)
virtual ~ResourceBroker()
virtual void clear()
virtual bool isITemplateBroker() const
virtual bool isBusBroker() const
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
void setResourceMapper(const ResourceMapper &mapper)
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
unsigned int initiationInterval_
bool hasResourceOf(const TTAMachine::MachinePart &mp) const
virtual const TTAMachine::MachinePart & machinePartOf(const SchedulingResource &r) const
void resources(ResourceSet &contents)
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
ResourceMap resMap_
virtual bool canAssign(const int cycle, const MoveNode &node) const =0
virtual const std::string & name() const
virtual bool isAvailable(const int cycle) const =0