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 
27 namespace armarx
28 {
29  bool cprs::And::shouldAllocateComputingPower(const Ice::Current&)
30  {
31  return std::all_of(requestStrategies.begin(), requestStrategies.end(),
32  [](ComputingPowerRequestStrategyBasePtr & s)
33  {
34  return s->shouldAllocateComputingPower();
35  }
36  );
37  }
38 
40  {
41  if (skipping)
42  {
43  allocatedLastTime = Clock::now();
44  }
45  else
46  {
47  allocatedLastTime += std::chrono::seconds {timeDeltaInSeconds};
48  }
49  }
50 
52  {
53  if (skipping)
54  {
55  allocateNextTime = nodeCount + nodeCountDelta;
56  }
57  else
58  {
59  allocateNextTime += nodeCountDelta;
60  }
61  }
62 
63  void cprs::TaskStatus::updateNodeCount(Ice::Long count, const Ice::Current&)
64  {
65  for (auto& strat : strategyPerTaskStatus)
66  {
67  strat.second->updateNodeCount(count);
68  }
69  }
70 
71  void cprs::TaskStatus::updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current&)
72  {
73  const auto it = strategyPerTaskStatus.find(newStatus);
74  current = (it == strategyPerTaskStatus.end()) ? nullptr : it->second.get();
75 
76  for (auto& strat : strategyPerTaskStatus)
77  {
78  strat.second->updateTaskStatus(newStatus);
79  }
80  }
81 
83  {
84  for (auto& strat : strategyPerTaskStatus)
85  {
86  strat.second->allocatedComputingPower();
87  }
88  }
89 
90  void cprs::TaskStatus::updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current&)
91  {
92  for (auto& strat : strategyPerTaskStatus)
93  {
94  strat.second->updateNodeCreations(nodesCreated, tries);
95  }
96  }
97 
98  void cprs::NoNodeCreated::updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current&)
99  {
100  for (std::size_t i = 0; i < static_cast<std::size_t>(tries); ++i)
101  {
102  backlog.at((currentBacklogIndex + i) % backlogSize) = i < static_cast<std::size_t>(nodesCreated) ? 0 : 1;
103  }
104 
105  currentBacklogIndex = (currentBacklogIndex + tries) % backlogSize;
106  }
107 
109  {
110  const auto perc = std::accumulate(backlog.begin(), backlog.end(), 1.f) / backlog.size();
111  const auto usedTimeDelta = timeDeltaInSeconds / (1.f + sigma * perc);
112  ARMARX_CHECK_EXPRESSION(usedTimeDelta >= 0);
113 
114  if (skipping)
115  {
116  allocatedLastTime = Clock::now();
117  }
118  else
119  {
120  allocatedLastTime += std::chrono::seconds {static_cast<std::size_t>(usedTimeDelta)};
121  }
122  }
123 
125  {
126  for (auto& s : requestStrategies)
127  {
128  s->setCurrentStateAsInitialState();
129  }
130  }
131 
133  {
134  for (auto& s : requestStrategies)
135  {
136  s->updateNodeCount(count);
137  }
138  }
139 
140  void cprs::CompoundedRequestStrategy::updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current&)
141  {
142  for (auto& s : requestStrategies)
143  {
144  s->updateTaskStatus(newStatus);
145  }
146  }
147 
149  {
150  for (auto& s : requestStrategies)
151  {
152  s->allocatedComputingPower();
153  }
154  }
155 
156  void cprs::CompoundedRequestStrategy::updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current&)
157  {
158  for (auto& s : requestStrategies)
159  {
160  s->updateNodeCreations(nodesCreated, tries);
161  }
162  }
163 
164 }
ComputingPowerRequestStrategy.h
armarx::cprs::CompoundedRequestStrategy::updateTaskStatus
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:140
armarx::cprs::And::shouldAllocateComputingPower
bool shouldAllocateComputingPower(const ::Ice::Current &=Ice::emptyCurrent) override
Returns true if all compounded strategies return true.
Definition: ComputingPowerRequestStrategy.cpp:29
armarx::cprs::TaskStatus::updateNodeCount
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:63
armarx::cprs::TaskStatus::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:90
armarx::cprs::CompoundedRequestStrategy::updateNodeCount
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:132
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::cprs::CompoundedRequestStrategy::setCurrentStateAsInitialState
void setCurrentStateAsInitialState(const ::Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:124
armarx::cprs::NoNodeCreated::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Definition: ComputingPowerRequestStrategy.cpp:108
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::cprs::TaskStatus::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:82
armarx::cprs::ElapsedTime::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next time to allocate depending on the flag skipping.
Definition: ComputingPowerRequestStrategy.cpp:39
armarx::cprs::CompoundedRequestStrategy::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:156
armarx::cprs::TaskStatus::updateTaskStatus
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Switches the current strategy and passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:71
armarx::cprs::CompoundedRequestStrategy::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:148
armarx::cprs::TotalNodeCount::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next creation count to allocate depending on the flag skipping.
Definition: ComputingPowerRequestStrategy.cpp:51
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::cprs::NoNodeCreated::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Updates the number of failed node creations.
Definition: ComputingPowerRequestStrategy.cpp:98
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:23