ComputingPowerRequestStrategy.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package RobotComponents
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl.txt
22 * GNU General Public License
23 */
24
26
27namespace armarx
28{
29 bool
31 {
32 return std::all_of(requestStrategies.begin(),
33 requestStrategies.end(),
34 [](ComputingPowerRequestStrategyBasePtr& s)
35 { return s->shouldAllocateComputingPower(); });
36 }
37
38 void
40 {
41 if (skipping)
42 {
44 }
45 else
46 {
47 allocatedLastTime += std::chrono::seconds{timeDeltaInSeconds};
48 }
49 }
50
51 void
53 {
54 if (skipping)
55 {
56 allocateNextTime = nodeCount + nodeCountDelta;
57 }
58 else
59 {
60 allocateNextTime += nodeCountDelta;
61 }
62 }
63
64 void
65 cprs::TaskStatus::updateNodeCount(Ice::Long count, const Ice::Current&)
66 {
67 for (auto& strat : strategyPerTaskStatus)
68 {
69 strat.second->updateNodeCount(count);
70 }
71 }
72
73 void
74 cprs::TaskStatus::updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current&)
75 {
76 const auto it = strategyPerTaskStatus.find(newStatus);
77 current = (it == strategyPerTaskStatus.end()) ? nullptr : it->second.get();
78
79 for (auto& strat : strategyPerTaskStatus)
80 {
81 strat.second->updateTaskStatus(newStatus);
82 }
83 }
84
85 void
87 {
88 for (auto& strat : strategyPerTaskStatus)
89 {
90 strat.second->allocatedComputingPower();
91 }
92 }
93
94 void
96 Ice::Long tries,
97 const Ice::Current&)
98 {
99 for (auto& strat : strategyPerTaskStatus)
100 {
101 strat.second->updateNodeCreations(nodesCreated, tries);
102 }
103 }
104
105 void
107 Ice::Long tries,
108 const Ice::Current&)
109 {
110 for (std::size_t i = 0; i < static_cast<std::size_t>(tries); ++i)
111 {
112 backlog.at((currentBacklogIndex + i) % backlogSize) =
113 i < static_cast<std::size_t>(nodesCreated) ? 0 : 1;
114 }
115
116 currentBacklogIndex = (currentBacklogIndex + tries) % backlogSize;
117 }
118
119 void
121 {
122 const auto perc = std::accumulate(backlog.begin(), backlog.end(), 1.f) / backlog.size();
123 const auto usedTimeDelta = timeDeltaInSeconds / (1.f + sigma * perc);
124 ARMARX_CHECK_EXPRESSION(usedTimeDelta >= 0);
125
126 if (skipping)
127 {
129 }
130 else
131 {
132 allocatedLastTime += std::chrono::seconds{static_cast<std::size_t>(usedTimeDelta)};
133 }
134 }
135
136 void
138 {
139 for (auto& s : requestStrategies)
140 {
141 s->setCurrentStateAsInitialState();
142 }
143 }
144
145 void
146 cprs::CompoundedRequestStrategy::updateNodeCount(Ice::Long count, const Ice::Current&)
147 {
148 for (auto& s : requestStrategies)
149 {
150 s->updateNodeCount(count);
151 }
152 }
153
154 void
155 cprs::CompoundedRequestStrategy::updateTaskStatus(armarx::TaskStatus::Status newStatus,
156 const Ice::Current&)
157 {
158 for (auto& s : requestStrategies)
159 {
160 s->updateTaskStatus(newStatus);
161 }
162 }
163
164 void
166 {
167 for (auto& s : requestStrategies)
168 {
169 s->allocatedComputingPower();
170 }
171 }
172
173 void
175 Ice::Long tries,
176 const Ice::Current&)
177 {
178 for (auto& s : requestStrategies)
179 {
180 s->updateNodeCreations(nodesCreated, tries);
181 }
182 }
183
184} // namespace armarx
DateTime now() const
Current date/time of the clock.
Definition Clock.cpp:22
bool shouldAllocateComputingPower(const ::Ice::Current &=Ice::emptyCurrent) override
Returns true if all compounded strategies return true.
void setCurrentStateAsInitialState(const ::Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next time to allocate depending on the flag skipping.
TimePoint allocatedLastTime
The time point when more power should be allocated again.
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Updates the number of failed node creations.
std::size_t currentBacklogIndex
The current index in the backlog.
std::vector< char > backlog
The backlog.
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Switches the current strategy and passes this call to all sub strategies.
ComputingPowerRequestStrategyBase * current
The current strategy in use.
Ice::Long nodeCount
The current node count.
Ice::Long allocateNextTime
The count when to allocate more power.
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next creation count to allocate depending on the flag skipping.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
This file offers overloads of toIce() and fromIce() functions for STL container types.