Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Const again.
[simgrid.git] / src / s4u / s4u_Exec.cpp
1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/exec.h"
7 #include "simgrid/s4u/Actor.hpp"
8 #include "simgrid/s4u/Exec.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_exec, s4u_activity, "S4U asynchronous executions");
13
14 namespace simgrid {
15 namespace s4u {
16 xbt::signal<void(Actor const&, Exec const&)> Exec::on_start;
17 xbt::signal<void(Actor const&, Exec const&)> Exec::on_completion;
18
19 Exec::Exec()
20 {
21   pimpl_ = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl());
22 }
23
24 Exec* Exec::wait()
25 {
26   return this->wait_for(-1);
27 }
28
29 Exec* Exec::wait_for(double timeout)
30 {
31   if (state_ == State::INITED)
32     vetoable_start();
33
34   kernel::actor::ActorImpl* issuer = Actor::self()->get_impl();
35   kernel::actor::simcall_blocking<void>([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
36   state_ = State::FINISHED;
37   on_completion(*Actor::self(), *this);
38   this->release_dependencies();
39   return this;
40 }
41
42 int Exec::wait_any_for(std::vector<ExecPtr>* execs, double timeout)
43 {
44   std::unique_ptr<kernel::activity::ExecImpl* []> rexecs(new kernel::activity::ExecImpl*[execs->size()]);
45   std::transform(begin(*execs), end(*execs), rexecs.get(),
46                  [](const ExecPtr& exec) { return static_cast<kernel::activity::ExecImpl*>(exec->pimpl_.get()); });
47
48   int changed_pos = simcall_execution_waitany_for(rexecs.get(), execs->size(), timeout);
49   if (changed_pos != -1)
50     execs->at(changed_pos)->release_dependencies();
51   return changed_pos;
52 }
53
54 Exec* Exec::cancel()
55 {
56   kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->cancel(); });
57   state_ = State::CANCELED;
58   return this;
59 }
60
61 /** @brief change the execution bound
62  * This means changing the maximal amount of flops per second that it may consume, regardless of what the host may
63  * deliver. Currently, this cannot be changed once the exec started.
64  */
65 ExecPtr Exec::set_bound(double bound)
66 {
67   xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
68   bound_ = bound;
69   return this;
70 }
71 ExecPtr Exec::set_timeout(double timeout) // XBT_ATTRIB_DEPRECATED_v329
72 {
73   xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
74   timeout_ = timeout;
75   return this;
76 }
77
78 ExecPtr Exec::set_flops_amount(double flops_amount)
79 {
80   xbt_assert(state_ == State::INITED, "Cannot change the flop_amount of an exec after its start");
81   flops_amounts_.assign(1, flops_amount);
82   Activity::set_remaining(flops_amounts_.front());
83   return this;
84 }
85
86 ExecPtr Exec::set_flops_amounts(const std::vector<double>& flops_amounts)
87 {
88   xbt_assert(state_ == State::INITED, "Cannot change the flops_amounts of an exec after its start");
89   flops_amounts_ = flops_amounts;
90   parallel_      = true;
91   return this;
92 }
93
94 ExecPtr Exec::set_bytes_amounts(const std::vector<double>& bytes_amounts)
95 {
96   xbt_assert(state_ == State::INITED, "Cannot change the bytes_amounts of an exec after its start");
97   bytes_amounts_ = bytes_amounts;
98   parallel_      = true;
99   return this;
100 }
101
102 /** @brief Retrieve the host on which this activity takes place.
103  *  If it runs on more than one host, only the first host is returned.
104  */
105 Host* Exec::get_host() const
106 {
107   return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host();
108 }
109 unsigned int Exec::get_host_number() const
110 {
111   return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host_number();
112 }
113 double Exec::get_start_time() const
114 {
115   return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_start_time();
116 }
117 double Exec::get_finish_time() const
118 {
119   return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_finish_time();
120 }
121 double Exec::get_cost() const
122 {
123   return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_cost();
124 }
125
126 /** @brief  Change the execution priority, don't you think?
127  *
128  * An execution with twice the priority will get twice the amount of flops when the resource is shared.
129  * The default priority is 1.
130  *
131  * Currently, this cannot be changed once the exec started. */
132 ExecPtr Exec::set_priority(double priority)
133 {
134   xbt_assert(state_ == State::INITED, "Cannot change the priority of an exec after its start");
135   priority_ = priority;
136   return this;
137 }
138
139 /** @brief Change the host on which this activity takes place.
140  *
141  * The activity cannot be terminated already (but it may be started). */
142 ExecPtr Exec::set_host(Host* host)
143 {
144   xbt_assert(state_ == State::INITED || state_ == State::STARTED,
145              "Cannot change the host of an exec once it's done (state: %d)", (int)state_);
146   if (state_ == State::STARTED)
147     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->migrate(host);
148   boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_host(host);
149   return this;
150 }
151
152 ExecPtr Exec::set_hosts(const std::vector<Host*>& hosts)
153 {
154   xbt_assert(state_ == State::INITED, "Cannot change the hosts of an exec once it's done (state: %d)", (int)state_);
155   hosts_    = hosts;
156   parallel_ = true;
157   return this;
158 }
159
160 ///////////// SEQUENTIAL EXECUTIONS ////////
161 Exec* Exec::start()
162 {
163   if (is_parallel())
164     kernel::actor::simcall([this] {
165       (*boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_))
166           .set_hosts(hosts_)
167           .set_timeout(timeout_)
168           .set_flops_amounts(flops_amounts_)
169           .set_bytes_amounts(bytes_amounts_)
170           .start();
171     });
172   else
173     kernel::actor::simcall([this] {
174       (*boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_))
175           .set_name(get_name())
176           .set_tracing_category(get_tracing_category())
177           .set_sharing_penalty(1. / priority_)
178           .set_bound(bound_)
179           .set_flops_amount(flops_amounts_.front())
180           .start();
181     });
182   state_ = State::STARTED;
183   on_start(*Actor::self(), *this);
184   return this;
185 }
186
187 double Exec::get_remaining() const
188 {
189   if (is_parallel()) {
190     XBT_WARN("Calling get_remaining() on a parallel execution is not allowed. Call get_remaining_ratio() instead.");
191     return get_remaining_ratio();
192   } else
193     return kernel::actor::simcall(
194         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_remaining(); });
195 }
196
197 /** @brief Returns the ratio of elements that are still to do
198  *
199  * The returned value is between 0 (completely done) and 1 (nothing done yet).
200  */
201 double Exec::get_remaining_ratio() const
202 {
203   if (is_parallel())
204     return kernel::actor::simcall(
205         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_par_remaining_ratio(); });
206   else
207     return kernel::actor::simcall(
208         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_seq_remaining_ratio(); });
209 }
210
211 } // namespace s4u
212 } // namespace simgrid
213 /* **************************** Public C interface *************************** */
214 void sg_exec_set_bound(sg_exec_t exec, double bound)
215 {
216   exec->set_bound(bound);
217 }
218
219 double sg_exec_get_remaining(const_sg_exec_t exec)
220 {
221   return exec->get_remaining();
222 }
223
224 void sg_exec_start(sg_exec_t exec)
225 {
226   exec->start();
227 }
228
229 void sg_exec_wait(sg_exec_t exec)
230 {
231   exec->wait_for(-1);
232   exec->unref();
233 }
234
235 void sg_exec_wait_for(sg_exec_t exec, double timeout)
236 {
237   exec->wait_for(timeout);
238   exec->unref();
239 }