Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factor Activity::wait() and wait_for() through CRTP.
[simgrid.git] / src / s4u / s4u_Exec.cpp
1 /* Copyright (c) 2006-2021. 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/Exception.hpp"
7 #include "simgrid/exec.h"
8 #include "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Exec.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/kernel/actor/SimcallObserver.hpp"
13 #include "xbt/log.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_exec, s4u_activity, "S4U asynchronous executions");
16
17 namespace simgrid {
18 namespace s4u {
19 xbt::signal<void(Exec const&)> Exec::on_start;
20 xbt::signal<void(Exec const&)> Exec::on_completion;
21
22 Exec::Exec(kernel::activity::ExecImplPtr pimpl)
23 {
24   pimpl_ = pimpl;
25 }
26
27 void Exec::complete(Activity::State state)
28 {
29   Activity::complete(state);
30   on_completion(*this);
31 }
32
33 ExecPtr Exec::init()
34 {
35   auto pimpl = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl());
36   return ExecPtr(pimpl->get_iface());
37 }
38
39 Exec* Exec::start()
40 {
41   kernel::actor::simcall([this] {
42     (*boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_))
43         .set_name(get_name())
44         .set_tracing_category(get_tracing_category())
45         .start();
46   });
47
48   if (suspended_)
49     pimpl_->suspend();
50
51   state_      = State::STARTED;
52   start_time_ = pimpl_->surf_action_->get_start_time();
53   on_start(*this);
54   return this;
55 }
56
57 int Exec::wait_any_for(std::vector<ExecPtr>* execs, double timeout)
58 {
59   std::vector<kernel::activity::ExecImpl*> rexecs(execs->size());
60   std::transform(begin(*execs), end(*execs), begin(rexecs),
61                  [](const ExecPtr& exec) { return static_cast<kernel::activity::ExecImpl*>(exec->pimpl_.get()); });
62
63   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
64   kernel::actor::ExecutionWaitanySimcall observer{issuer, rexecs, timeout};
65   int changed_pos = kernel::actor::simcall_blocking(
66       [&observer] {
67         kernel::activity::ExecImpl::wait_any_for(observer.get_issuer(), observer.get_execs(), observer.get_timeout());
68       },
69       &observer);
70   if (changed_pos != -1)
71     execs->at(changed_pos)->complete(State::FINISHED);
72   return changed_pos;
73 }
74
75 /** @brief change the execution bound
76  * This means changing the maximal amount of flops per second that it may consume, regardless of what the host may
77  * deliver. Currently, this cannot be changed once the exec started.
78  */
79 ExecPtr Exec::set_bound(double bound)
80 {
81   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
82              "Cannot change the bound of an exec after its start");
83   kernel::actor::simcall(
84       [this, bound] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_bound(bound); });
85   return this;
86 }
87
88 /** @brief  Change the execution priority, don't you think?
89  *
90  * An execution with twice the priority will get twice the amount of flops when the resource is shared.
91  * The default priority is 1.
92  *
93  * Currently, this cannot be changed once the exec started. */
94 ExecPtr Exec::set_priority(double priority)
95 {
96   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
97              "Cannot change the priority of an exec after its start");
98   kernel::actor::simcall([this, priority] {
99     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_sharing_penalty(1. / priority);
100   });
101   return this;
102 }
103
104 ExecPtr Exec::set_timeout(double timeout) // XBT_ATTRIB_DEPRECATED_v329
105 {
106   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
107              "Cannot change the bound of an exec after its start");
108   kernel::actor::simcall(
109       [this, timeout] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_timeout(timeout); });
110   return this;
111 }
112
113 ExecPtr Exec::set_flops_amount(double flops_amount)
114 {
115   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
116       "Cannot change the flop_amount of an exec after its start");
117   kernel::actor::simcall([this, flops_amount] {
118     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_flops_amount(flops_amount);
119   });
120   Activity::set_remaining(flops_amount);
121   return this;
122 }
123
124 ExecPtr Exec::set_flops_amounts(const std::vector<double>& flops_amounts)
125 {
126   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
127       "Cannot change the flops_amounts of an exec after its start");
128   kernel::actor::simcall([this, flops_amounts] {
129     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_flops_amounts(flops_amounts);
130   });
131   parallel_      = true;
132   return this;
133 }
134
135 ExecPtr Exec::set_bytes_amounts(const std::vector<double>& bytes_amounts)
136 {
137   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
138       "Cannot change the bytes_amounts of an exec after its start");
139   kernel::actor::simcall([this, bytes_amounts] {
140     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_bytes_amounts(bytes_amounts);
141   });
142   parallel_      = true;
143   return this;
144 }
145
146 /** @brief Retrieve the host on which this activity takes place.
147  *  If it runs on more than one host, only the first host is returned.
148  */
149 Host* Exec::get_host() const
150 {
151   return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host();
152 }
153 unsigned int Exec::get_host_number() const
154 {
155   return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host_number();
156 }
157
158 /** @brief Change the host on which this activity takes place.
159  *
160  * The activity cannot be terminated already (but it may be started). */
161 ExecPtr Exec::set_host(Host* host)
162 {
163   xbt_assert(state_ == State::INITED || state_ == State::STARTING || state_ == State::STARTED,
164              "Cannot change the host of an exec once it's done (state: %s)", to_c_str(state_));
165
166   if (state_ == State::STARTED)
167     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->migrate(host);
168
169   kernel::actor::simcall(
170       [this, host] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_host(host); });
171
172   if (state_ == State::STARTING)
173   // Setting the host may allow to start the activity, let's try
174     vetoable_start();
175
176   return this;
177 }
178
179 ExecPtr Exec::set_hosts(const std::vector<Host*>& hosts)
180 {
181   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
182              "Cannot change the hosts of an exec once it's done (state: %s)", to_c_str(state_));
183
184   kernel::actor::simcall(
185       [this, hosts] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_hosts(hosts); });
186   parallel_ = true;
187
188   // Setting the host may allow to start the activity, let's try
189   if (state_ == State::STARTING)
190      vetoable_start();
191
192   return this;
193 }
194
195 double Exec::get_cost() const
196 {
197   return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_cost();
198 }
199
200 double Exec::get_remaining() const
201 {
202   if (is_parallel()) {
203     XBT_WARN("Calling get_remaining() on a parallel execution is not allowed. Call get_remaining_ratio() instead.");
204     return get_remaining_ratio();
205   } else
206     return kernel::actor::simcall(
207         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_remaining(); });
208 }
209
210 /** @brief Returns the ratio of elements that are still to do
211  *
212  * The returned value is between 0 (completely done) and 1 (nothing done yet).
213  */
214 double Exec::get_remaining_ratio() const
215 {
216   if (is_parallel())
217     return kernel::actor::simcall(
218         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_par_remaining_ratio(); });
219   else
220     return kernel::actor::simcall(
221         [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_seq_remaining_ratio(); });
222 }
223
224 bool Exec::is_assigned() const
225 {
226   return not boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_hosts().empty();
227 }
228 } // namespace s4u
229 } // namespace simgrid
230
231 /* **************************** Public C interface *************************** */
232 void sg_exec_set_bound(sg_exec_t exec, double bound)
233 {
234   exec->set_bound(bound);
235 }
236
237 const char* sg_exec_get_name(const_sg_exec_t exec)
238 {
239   return exec->get_cname();
240 }
241
242 void sg_exec_set_name(sg_exec_t exec, const char* name)
243 {
244   exec->set_name(name);
245 }
246
247 void sg_exec_set_host(sg_exec_t exec, sg_host_t new_host)
248 {
249   exec->set_host(new_host);
250 }
251
252 double sg_exec_get_remaining(const_sg_exec_t exec)
253 {
254   return exec->get_remaining();
255 }
256
257 double sg_exec_get_remaining_ratio(const_sg_exec_t exec)
258 {
259   return exec->get_remaining_ratio();
260 }
261
262 void sg_exec_start(sg_exec_t exec)
263 {
264   exec->vetoable_start();
265 }
266
267 void sg_exec_cancel(sg_exec_t exec)
268 {
269   exec->cancel();
270   exec->unref();
271 }
272
273 int sg_exec_test(sg_exec_t exec)
274 {
275   bool finished = exec->test();
276   if (finished)
277     exec->unref();
278   return finished;
279 }
280
281 sg_error_t sg_exec_wait(sg_exec_t exec)
282 {
283   sg_error_t status = SG_OK;
284
285   simgrid::s4u::ExecPtr s4u_exec(exec, false);
286   try {
287     s4u_exec->wait_for(-1);
288   } catch (const simgrid::TimeoutException&) {
289     status = SG_ERROR_TIMEOUT;
290   } catch (const simgrid::CancelException&) {
291     status = SG_ERROR_CANCELED;
292   } catch (const simgrid::HostFailureException&) {
293     status = SG_ERROR_HOST;
294   }
295   return status;
296 }
297
298 sg_error_t sg_exec_wait_for(sg_exec_t exec, double timeout)
299 {
300   sg_error_t status = SG_OK;
301
302   simgrid::s4u::ExecPtr s4u_exec(exec, false);
303   try {
304     s4u_exec->wait_for(timeout);
305   } catch (const simgrid::TimeoutException&) {
306     status = SG_ERROR_TIMEOUT;
307   } catch (const simgrid::CancelException&) {
308     status = SG_ERROR_CANCELED;
309   } catch (const simgrid::HostFailureException&) {
310     status = SG_ERROR_HOST;
311   }
312   return status;
313 }
314
315 int sg_exec_wait_any(sg_exec_t* execs, size_t count)
316 {
317   return sg_exec_wait_any_for(execs, count, -1);
318 }
319
320 int sg_exec_wait_any_for(sg_exec_t* execs, size_t count, double timeout)
321 {
322   std::vector<simgrid::s4u::ExecPtr> s4u_execs;
323   for (unsigned int i = 0; i < count; i++)
324     s4u_execs.emplace_back(execs[i], false);
325
326   int pos = simgrid::s4u::Exec::wait_any_for(&s4u_execs, timeout);
327   for (unsigned i = 0; i < count; i++) {
328     if (pos != -1 && static_cast<unsigned>(pos) != i)
329       s4u_execs[i]->add_ref();
330   }
331   return pos;
332 }