1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/exec.h"
7 #include "simgrid/s4u/Actor.hpp"
8 #include "simgrid/s4u/Exec.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_exec, s4u_activity, "S4U asynchronous executions");
16 xbt::signal<void(Actor const&, Exec const&)> Exec::on_start;
17 xbt::signal<void(Actor const&, Exec const&)> Exec::on_completion;
21 pimpl_ = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl());
26 return this->wait_for(-1);
29 Exec* Exec::wait_for(double timeout)
31 if (state_ == State::INITED)
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();
42 int Exec::wait_any_for(std::vector<ExecPtr>* execs, double timeout)
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()); });
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();
56 kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->cancel(); });
57 state_ = State::CANCELED;
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.
65 ExecPtr Exec::set_bound(double bound)
67 xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
71 ExecPtr Exec::set_timeout(double timeout) // XBT_ATTRIB_DEPRECATED_v329
73 xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
78 ExecPtr Exec::set_flops_amount(double flops_amount)
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());
86 ExecPtr Exec::set_flops_amounts(const std::vector<double>& flops_amounts)
88 xbt_assert(state_ == State::INITED, "Cannot change the flops_amounts of an exec after its start");
89 flops_amounts_ = flops_amounts;
94 ExecPtr Exec::set_bytes_amounts(const std::vector<double>& bytes_amounts)
96 xbt_assert(state_ == State::INITED, "Cannot change the bytes_amounts of an exec after its start");
97 bytes_amounts_ = bytes_amounts;
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.
105 Host* Exec::get_host() const
107 return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host();
109 unsigned int Exec::get_host_number() const
111 return static_cast<kernel::activity::ExecImpl*>(pimpl_.get())->get_host_number();
113 double Exec::get_start_time() const
115 return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_start_time();
117 double Exec::get_finish_time() const
119 return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_finish_time();
121 double Exec::get_cost() const
123 return (pimpl_->surf_action_ == nullptr) ? -1 : pimpl_->surf_action_->get_cost();
126 /** @brief Change the execution priority, don't you think?
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.
131 * Currently, this cannot be changed once the exec started. */
132 ExecPtr Exec::set_priority(double priority)
134 xbt_assert(state_ == State::INITED, "Cannot change the priority of an exec after its start");
135 priority_ = priority;
139 /** @brief Change the host on which this activity takes place.
141 * The activity cannot be terminated already (but it may be started). */
142 ExecPtr Exec::set_host(Host* host)
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);
152 ExecPtr Exec::set_hosts(const std::vector<Host*>& hosts)
154 xbt_assert(state_ == State::INITED, "Cannot change the hosts of an exec once it's done (state: %d)", (int)state_);
160 ///////////// SEQUENTIAL EXECUTIONS ////////
164 kernel::actor::simcall([this] {
165 (*boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_))
167 .set_timeout(timeout_)
168 .set_flops_amounts(flops_amounts_)
169 .set_bytes_amounts(bytes_amounts_)
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_)
179 .set_flops_amount(flops_amounts_.front())
182 state_ = State::STARTED;
183 on_start(*Actor::self(), *this);
187 double Exec::get_remaining() const
190 XBT_WARN("Calling get_remaining() on a parallel execution is not allowed. Call get_remaining_ratio() instead.");
191 return get_remaining_ratio();
193 return kernel::actor::simcall(
194 [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_remaining(); });
197 /** @brief Returns the ratio of elements that are still to do
199 * The returned value is between 0 (completely done) and 1 (nothing done yet).
201 double Exec::get_remaining_ratio() const
204 return kernel::actor::simcall(
205 [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_par_remaining_ratio(); });
207 return kernel::actor::simcall(
208 [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_seq_remaining_ratio(); });
212 } // namespace simgrid
213 /* **************************** Public C interface *************************** */
214 void sg_exec_set_bound(sg_exec_t exec, double bound)
216 exec->set_bound(bound);
219 double sg_exec_get_remaining(const_sg_exec_t exec)
221 return exec->get_remaining();
224 void sg_exec_start(sg_exec_t exec)
229 void sg_exec_wait(sg_exec_t exec)
235 void sg_exec_wait_for(sg_exec_t exec, double timeout)
237 exec->wait_for(timeout);