OpenASIP  2.0
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 
38 using std::string;
39 
40 /**
41  * Destructor.
42  */
44 }
45 
46 /**
47  * Constructor.
48  *
49  * @param name Name of resource.
50  */
51 SchedulingResource::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  */
60 int
62  return relatedResourceGroup_.size();
63 }
64 
65 /**
66  * Return number of groups of dependent resources.
67  *
68  * @return Number of groups of dependent resources.
69  */
70 int
72  return dependentResourceGroup_.size();
73 }
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  */
81 void
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  */
100 void
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  */
193 bool
195  const SchedulingResource& sResource) const {
196  return relatedResourceSet_.find(&sResource) !=
197  relatedResourceSet_.end();
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  */
206 bool
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  */
235 void
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  */
250 int
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  */
278 void
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  */
296 bool
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  */
316  resources_.clear();
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  */
327 void
329  std::sort(resources_.begin(), resources_.end(), less_name());
330 }
331 
332 /**
333  * Clears the scheduling resource set.
334  */
335 void
337  resources_.clear();
338 }
339 
340 /**
341  * Returns how many times particular scheduling resource was used
342  *
343  * @return number of times resource was already used
344  */
345 int
347  return useCount_;
348 }
349 
350 /**
351  * Increases use count of Scheduling Resource by 1
352  *
353  */
354 void
356  useCount_++;
357 }
358 
359 /**
360  * Decrease use count of Schedulign Resource by 1
361  *
362  */
363 void
365  useCount_--;
366 }
367 
368 /**
369  * Set initiation interval, if ii = 0 then initiation interval is not used.
370  *
371  * @param ii initiation interval
372  */
373 void
375 {
376  initiationInterval_ = ii;
377 }
378 
379 /**
380  * Get initiation interval, if ii = 0 then initiation interval is not used.
381  *
382  * @return initiation interval
383  */
384 int
386 {
387  return initiationInterval_;
388 }
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  */
396 void
398  useCount_ = 0;
399 }
SchedulingResource::~SchedulingResource
virtual ~SchedulingResource()
Definition: SchedulingResource.cc:43
SchedulingResource::relatedResourceGroup_
SchedulingResourceGroup relatedResourceGroup_
Definition: SchedulingResource.hh:145
SchedulingResource::addToDependentGroup
virtual void addToDependentGroup(const int group, SchedulingResource &resource)
Definition: SchedulingResource.cc:101
SchedulingResource::dependentResourceGroupCount
virtual int dependentResourceGroupCount() const
Definition: SchedulingResource.cc:71
SchedulingResource::clear
virtual void clear()
Definition: SchedulingResource.cc:397
SchedulingResourceSet::hasResource
bool hasResource(SchedulingResource &res)
Definition: SchedulingResource.cc:297
SchedulingResource::hasDependentResource
virtual bool hasDependentResource(const SchedulingResource &sResource) const
Definition: SchedulingResource.cc:207
SchedulingResource::SchedulingResource
SchedulingResource(const std::string &name, const unsigned int ii=0)
Definition: SchedulingResource.cc:51
OutOfRange
Definition: Exception.hh:320
SchedulingResource::initiationInterval_
int initiationInterval_
Definition: SchedulingResource.hh:132
Conversion::toString
static std::string toString(const T &source)
SchedulingResourceSet::clear
void clear()
Definition: SchedulingResource.cc:336
SchedulingResourceSet
Definition: SchedulingResource.hh:161
SchedulingResourceSet::SchedulingResourceSet
SchedulingResourceSet()
Definition: SchedulingResource.cc:222
SchedulingResource::dependentResource
virtual SchedulingResource & dependentResource(const int group, const int index) const
Definition: SchedulingResource.cc:158
SchedulingResource::dependentResourceCount
int dependentResourceCount(const int group) const
SchedulingResourceSet::~SchedulingResourceSet
~SchedulingResourceSet()
Definition: SchedulingResource.cc:227
SchedulingResource::relatedResource
virtual SchedulingResource & relatedResource(const int group, const int index) const
Definition: SchedulingResource.cc:120
SchedulingResource::addToRelatedGroup
virtual void addToRelatedGroup(const int group, SchedulingResource &resource)
Definition: SchedulingResource.cc:82
SchedulingResource::useCount
virtual int useCount() const
Definition: SchedulingResource.cc:346
SchedulingResource
Definition: SchedulingResource.hh:52
Conversion.hh
SchedulingResource.hh
SchedulingResourceSet::operator=
SchedulingResourceSet & operator=(const SchedulingResourceSet &newSet)
Definition: SchedulingResource.cc:315
__func__
#define __func__
Definition: Application.hh:67
SchedulingResourceSet::less_name
Definition: SchedulingResource.hh:177
SchedulingResource::initiationInterval
int initiationInterval() const
Definition: SchedulingResource.cc:385
SchedulingResource::hasRelatedResource
virtual bool hasRelatedResource(const SchedulingResource &sResource) const
Definition: SchedulingResource.cc:194
SchedulingResource::relatedResourceCount
int relatedResourceCount(const int group) const
SchedulingResource::setInitiationInterval
void setInitiationInterval(unsigned int ii)
Definition: SchedulingResource.cc:374
SchedulingResourceSet::remove
void remove(SchedulingResource &resource)
Definition: SchedulingResource.cc:279
SchedulingResourceSet::count
int count() const
Definition: SchedulingResource.cc:251
ObjectAlreadyExists
Definition: Exception.hh:1002
SchedulingResourceSet::resources_
ResourceList resources_
Definition: SchedulingResource.hh:184
SchedulingResource::decreaseUseCount
virtual void decreaseUseCount()
Definition: SchedulingResource.cc:364
ContainerTools::containsValue
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
KeyNotFound
Definition: Exception.hh:285
SchedulingResourceSet::resource
SchedulingResource & resource(int index) const
Definition: SchedulingResource.cc:263
SchedulingResource::name
virtual const std::string & name() const
SchedulingResource::dependentResourceGroup_
SchedulingResourceGroup dependentResourceGroup_
Definition: SchedulingResource.hh:147
SchedulingResource::relatedResourceGroupCount
virtual int relatedResourceGroupCount() const
Definition: SchedulingResource.cc:61
SchedulingResource::useCount_
int useCount_
Definition: SchedulingResource.hh:155
SchedulingResourceSet::insert
void insert(SchedulingResource &resource)
Definition: SchedulingResource.cc:236
SchedulingResource::relatedResourceSet_
SchedulingResourceSet relatedResourceSet_
Definition: SchedulingResource.hh:152
SchedulingResource::increaseUseCount
virtual void increaseUseCount()
Definition: SchedulingResource.cc:355
ContainerTools.hh
SchedulingResourceSet::sort
void sort()
Definition: SchedulingResource.cc:328