OpenASIP 2.2
Loading...
Searching...
No Matches
SchedulingResource.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 SchedulingResource.cc
26 *
27 * Implementation of SchedulingResource class.
28 *
29 * @author Vladimir Guzma 2006 (vladimir.guzma-no.spam-tut.fi)
30 * @note rating: red
31 */
32#include <algorithm>
33
34#include "SchedulingResource.hh"
35#include "ContainerTools.hh"
36#include "Conversion.hh"
37
38using std::string;
39
40/**
41 * Destructor.
42 */
45
46/**
47 * Constructor.
48 *
49 * @param name Name of resource.
50 */
51SchedulingResource::SchedulingResource(const std::string& name, const unsigned int ii) :
52 initiationInterval_(ii), name_(name), useCount_(0) {
53}
54
55/**
56 * Return number of groups of related resources.
57 *
58 * @return Number of groups of related resources.
59 */
60int
64
65/**
66 * Return number of groups of dependent resources.
67 *
68 * @return Number of groups of dependent resources.
69 */
70int
74
75/**
76 * Add resource to group of related resources.
77 *
78 * @param group Group to which resource add.
79 * @param resource Resource to add to group.
80 */
81void
83 const int group,
84 SchedulingResource& resource) {
85
86 relatedResourceSet_.insert(&resource);
87
88 if (group >= relatedResourceGroupCount()) {
89 relatedResourceGroup_.resize(group+1);
90 }
91 relatedResourceGroup_.at(group).push_back(&resource);
92}
93
94/**
95 * Add resource to group of dependent resources.
96 *
97 * @param group Group to which resource add.
98 * @param resource Resource to add to group.
99 */
100void
102 const int group,
103 SchedulingResource& resource) {
104
105 if (group >= dependentResourceGroupCount()) {
106 dependentResourceGroup_.resize(group+1);
107 }
108 dependentResourceGroup_.at(group).push_back(&resource);
109}
110
111/**
112 * Return related resource from group with position.
113 *
114 * @param group Group from which to return resource.
115 * @param index Index of resource in particular group.
116 * @return Reference to SchedulingResource [group][index].
117 * @exception OutOfRange When group or index requested does not exist.
118 */
121 const int group,
122 const int index) const {
123
124 if (group < relatedResourceGroupCount()) {
125 if (index < relatedResourceCount(group)) {
126 return *relatedResourceGroup_.at(group).at(index);
127 } else {
128 std::string msg = "Requested related resource [";
129 msg += Conversion::toString(index);
130 msg += "] does not exists";
131 msg += " in group [";
132 msg += Conversion::toString(group);
133 msg += "] of ";
134 msg += name();
135 msg += ". Group count is: ";
137 msg += ".";
138 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
139 }
140 } else {
141 std::string msg = "Requested related resource group ";
142 msg += Conversion::toString(group);
143 msg += " does not exists in ";
144 msg += name();
145 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
146 }
147}
148
149/**
150 * Return dependent resource from group with position.
151 *
152 * @param group Group from which to return resource.
153 * @param index Index of resource in particular group.
154 * @return Reference to SchedulingResource [group][index].
155 * @exception OutOfRange When group or index requested does not exist.
156 */
159 const int group,
160 const int index) const {
161
162 if (group < dependentResourceGroupCount()) {
163 if (index < dependentResourceCount(group)) {
164 return *dependentResourceGroup_.at(group).at(index);
165 } else {
166 std::string msg = "Requested dependent resource [";
167 msg += Conversion::toString(index);
168 msg += "] does not exists";
169 msg += " in group [";
170 msg += Conversion::toString(group);
171 msg += "] of ";
172 msg += name();
173 msg += ". Group count is: ";
175 msg += ".";
176 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
177 }
178 } else {
179 std::string msg = "Requested dependent resource group ";
180 msg += Conversion::toString(group);
181 msg += " does not exists in ";
182 msg += name();
183 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
184 }
185}
186
187/**
188 * Return true if resource contains given related resource.
189 *
190 * @param sResource Resource to test.
191 * @return True if resource contains given related resource.
192 */
193bool
195 const SchedulingResource& sResource) const {
196 return relatedResourceSet_.find(&sResource) !=
198}
199
200/**
201 * Return true if resource contains given dependent resource.
202 *
203 * @param sResource Resource to test.
204 * @return True if resource contains given dependent resource.
205 */
206bool
208 const SchedulingResource& sResource) const {
209 for (int i = 0; i < dependentResourceGroupCount();i++) {
210 for (int j = 0, count = dependentResourceCount(i); j < count; j++) {
211 if (dependentResource(i, j).name() == sResource.name()) {
212 return true;
213 }
214 }
215 }
216 return false;
217}
218
219/**
220 * Constructor.
221 */
223
224/**
225 * Destructor.
226 */
228
229/**
230 * Insert a scheduling resource in the set.
231 *
232 * @param resource Resource to insert.
233 * @exception ObjectAlreadyExists if the resource is already in the set.
234 */
235void
237
239 throw ObjectAlreadyExists(__FILE__, __LINE__, __func__);
240 } else {
241 resources_.push_back(&resource);
242 }
243}
244
245/**
246 * Return the number of resources in the set.
247 *
248 * @return The number of resources in the set.
249 */
250int
252 return resources_.size();
253}
254
255/**
256 * Return the resource at the given position.
257 *
258 * @param index Position of resource.
259 * @return The resource at the given position.
260 * @exception OutOfRange If the given position exceeds number of resources.
261 */
264
265 if (index < 0 || index >= static_cast<int>(resources_.size())) {
266 throw OutOfRange(__FILE__, __LINE__, __func__);
267 } else {
268 return *resources_.at(index);
269 }
270}
271
272/**
273 * Remove a resource from the set.
274 *
275 * @param resource Scheduling resource to be removed.
276 * @exception KeyNotFound If given resource is not found in the set.
277 */
278void
280
281 ResourceList::iterator iter = resources_.begin();
282 while (iter != resources_.end()) {
283 if ((*iter) == &resource) {
284 resources_.erase(iter);
285 return;
286 }
287 iter++;
288 }
289 string msg = "Resource not found in resource set.";
290 throw KeyNotFound(__FILE__, __LINE__, __func__, msg);
291}
292
293/**
294 * Tells whether the set has the given resource.
295 */
296bool
298 ResourceList::iterator iter = resources_.begin();
299 while (iter != resources_.end()) {
300 if ((*iter) == &resource) {
301 return true;
302 }
303 iter++;
304 }
305 return false;
306}
307
308/**
309 * Assignment operator.
310 *
311 * @param newSet Set to assign resources from.
312 * @return This set with newly assigned contents.
313 */
317 for (int i = 0; i < newSet.count(); i++) {
318 insert(newSet.resource(i));
319 }
320 return *this;
321}
322
323/**
324 * Sort the content of Scheduling Resource Set by the names of the
325 * resources.
326 */
327void
329 std::sort(resources_.begin(), resources_.end(), less_name());
330}
331
332/**
333 * Clears the scheduling resource set.
334 */
335void
339
340/**
341 * Returns how many times particular scheduling resource was used
342 *
343 * @return number of times resource was already used
344 */
345int
347 return useCount_;
348}
349
350/**
351 * Increases use count of Scheduling Resource by 1
352 *
353 */
354void
358
359/**
360 * Decrease use count of Schedulign Resource by 1
361 *
362 */
363void
367
368/**
369 * Set initiation interval, if ii = 0 then initiation interval is not used.
370 *
371 * @param ii initiation interval
372 */
373void
378
379/**
380 * Get initiation interval, if ii = 0 then initiation interval is not used.
381 *
382 * @return initiation interval
383 */
384int
389
390/**
391 * Clears bookkeeping of the scheduling resource.
392 *
393 * After this call the state of the resource should be identical to a
394 * newly-created and initialized resource.
395 */
396void
#define __func__
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
static std::string toString(const T &source)
void remove(SchedulingResource &resource)
SchedulingResource & resource(int index) const
SchedulingResourceSet & operator=(const SchedulingResourceSet &newSet)
bool hasResource(SchedulingResource &res)
void insert(SchedulingResource &resource)
virtual void addToDependentGroup(const int group, SchedulingResource &resource)
virtual void increaseUseCount()
virtual SchedulingResource & relatedResource(const int group, const int index) const
int relatedResourceCount(const int group) const
void setInitiationInterval(unsigned int ii)
virtual SchedulingResource & dependentResource(const int group, const int index) const
virtual void addToRelatedGroup(const int group, SchedulingResource &resource)
virtual const std::string & name() const
virtual bool hasDependentResource(const SchedulingResource &sResource) const
virtual void decreaseUseCount()
virtual bool hasRelatedResource(const SchedulingResource &sResource) const
SchedulingResourceGroup dependentResourceGroup_
int dependentResourceCount(const int group) const
virtual int relatedResourceGroupCount() const
virtual int useCount() const
SchedulingResource(const std::string &name, const unsigned int ii=0)
SchedulingResourceGroup relatedResourceGroup_
virtual int dependentResourceGroupCount() const
SchedulingResourceSet relatedResourceSet_