Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Start to use Curiously Recurring Template Patterns
[simgrid.git] / src / s4u / s4u_Exec.cpp
1 /* Copyright (c) 2006-2019. 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/s4u/Actor.hpp"
7 #include "simgrid/s4u/Exec.hpp"
8 #include "src/kernel/activity/ExecImpl.hpp"
9 #include "xbt/log.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_exec, s4u_activity, "S4U asynchronous executions");
12
13 namespace simgrid {
14 namespace s4u {
15 xbt::signal<void(ActorPtr)> Exec::on_start;
16 xbt::signal<void(ActorPtr)> Exec::on_completion;
17
18 Exec::Exec()
19 {
20   pimpl_ = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl(get_name(), get_tracing_category()));
21 }
22
23 bool Exec::test()
24 {
25   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
26
27   if (state_ == State::FINISHED)
28     return true;
29
30   if (state_ == State::INITED)
31     this->start();
32
33   if (simcall_execution_test(pimpl_)) {
34     state_ = State::FINISHED;
35     return true;
36   }
37
38   return false;
39 }
40
41 Exec* Exec::wait()
42 {
43   if (state_ == State::INITED)
44     start();
45   simcall_execution_wait(pimpl_);
46   state_ = State::FINISHED;
47   on_completion(Actor::self());
48   return this;
49 }
50
51 Exec* Exec::wait_for(double)
52 {
53   THROW_UNIMPLEMENTED;
54 }
55
56 Exec* Exec::cancel()
57 {
58   simix::simcall([this] { boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->cancel(); });
59   state_ = State::CANCELED;
60   return this;
61 }
62
63 void intrusive_ptr_release(simgrid::s4u::Exec* e)
64 {
65   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
66     std::atomic_thread_fence(std::memory_order_acquire);
67     delete e;
68   }
69 }
70
71 void intrusive_ptr_add_ref(simgrid::s4u::Exec* e)
72 {
73   e->refcount_.fetch_add(1, std::memory_order_relaxed);
74 }
75
76 /** @brief change the execution bound
77  * This means changing the maximal amount of flops per second that it may consume, regardless of what the host may
78  * deliver. Currently, this cannot be changed once the exec started.
79  */
80 ExecPtr Exec::set_bound(double bound)
81 {
82   xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
83   bound_ = bound;
84   return this;
85 }
86 ExecPtr Exec::set_timeout(double timeout)
87 {
88   xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
89   timeout_ = timeout;
90   return this;
91 }
92
93 /** @brief  Change the execution priority, don't you think?
94  *
95  * An execution with twice the priority will get twice the amount of flops when the resource is shared.
96  * The default priority is 1.
97  *
98  * Currently, this cannot be changed once the exec started. */
99 ExecPtr Exec::set_priority(double priority)
100 {
101   xbt_assert(state_ == State::INITED, "Cannot change the priority of an exec after its start");
102   priority_ = priority;
103   return this;
104 }
105
106 ///////////// SEQUENTIAL EXECUTIONS ////////
107 ExecSeq::ExecSeq(sg_host_t host, double flops_amount) : Exec(), flops_amount_(flops_amount)
108 {
109   Activity::set_remaining(flops_amount_);
110   boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->set_host(host);
111 }
112
113 Exec* ExecSeq::start()
114 {
115   simix::simcall([this] {
116     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->start(flops_amount_, 1. / priority_, bound_);
117   });
118   state_ = State::STARTED;
119   on_start(Actor::self());
120   return this;
121 }
122
123 /** @brief Returns whether the state of the exec is finished */
124 /** @brief Change the host on which this activity takes place.
125  *
126  * The activity cannot be terminated already (but it may be started). */
127 ExecPtr ExecSeq::set_host(Host* host)
128 {
129   xbt_assert(state_ == State::INITED || state_ == State::STARTED,
130              "Cannot change the host of an exec once it's done (state: %d)", (int)state_);
131   if (state_ == State::STARTED)
132     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->migrate(host);
133   host_ = host;
134   boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->host_ = host;
135   return this;
136 }
137
138
139 /** @brief Retrieve the host on which this activity takes place. */
140 Host* ExecSeq::get_host()
141 {
142   return host_;
143 }
144
145 /** @brief Returns the amount of flops that remain to be done */
146 double ExecSeq::get_remaining()
147 {
148   return simgrid::simix::simcall(
149       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->get_remaining(); });
150 }
151
152 /** @brief Returns the ratio of elements that are still to do
153  *
154  * The returned value is between 0 (completely done) and 1 (nothing done yet).
155  */
156 double ExecSeq::get_remaining_ratio()
157 {
158   return simgrid::simix::simcall([this]() {
159     return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->get_seq_remaining_ratio();
160   });
161 }
162
163 ///////////// PARALLEL EXECUTIONS ////////
164 ExecPar::ExecPar(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
165                  const std::vector<double>& bytes_amounts)
166     : Exec(), hosts_(hosts), flops_amounts_(flops_amounts), bytes_amounts_(bytes_amounts)
167 {
168   // For parallel executions, we need a special host to run the timeout detector.
169   host_                                                                          = hosts.front();
170   boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->host_ = host_;
171 }
172
173 Exec* ExecPar::start()
174 {
175   simix::simcall([this] {
176     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->set_timeout(timeout_);
177     boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->start(hosts_, flops_amounts_, bytes_amounts_);
178   });
179   state_ = State::STARTED;
180   on_start(Actor::self());
181   return this;
182 }
183 double ExecPar::get_remaining_ratio()
184 {
185   return simix::simcall(
186       [this]() { return boost::static_pointer_cast<kernel::activity::ExecImpl>(pimpl_)->get_par_remaining_ratio(); });
187 }
188
189 double ExecPar::get_remaining()
190 {
191   XBT_WARN("Calling get_remaining() on a parallel execution is not allowed. Call get_remaining_ratio() instead.");
192   return get_remaining_ratio();
193 }
194 } // namespace s4u
195 } // namespace simgrid