Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
60ba49aced8ddf143c34f27c6a25c3b388786947
[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 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Exec::on_start;
16 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Exec::on_completion;
17
18 Exec* Exec::start()
19 {
20   pimpl_ = simcall_execution_start(name_, tracing_category_, flops_amount_, 1. / priority_, bound_, host_);
21   state_ = State::STARTED;
22   on_start(Actor::self());
23   return this;
24 }
25
26 Exec* Exec::cancel()
27 {
28   simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::ExecImpl*>(pimpl_.get())->cancel(); });
29   state_ = State::CANCELED;
30   return this;
31 }
32
33 Exec* Exec::wait()
34 {
35   if (state_ == State::INITED)
36     start();
37   simcall_execution_wait(pimpl_);
38   state_ = State::FINISHED;
39   on_completion(Actor::self());
40   return this;
41 }
42
43 Exec* Exec::wait_for(double)
44 {
45   THROW_UNIMPLEMENTED;
46   return this;
47 }
48
49 /** @brief Returns whether the state of the exec is finished */
50 bool Exec::test()
51 {
52   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
53
54   if (state_ == State::FINISHED)
55     return true;
56
57   if (state_ == State::INITED)
58     this->start();
59
60   if (simcall_execution_test(pimpl_)) {
61     state_ = State::FINISHED;
62     return true;
63   }
64
65   return false;
66 }
67
68 /** @brief  Change the execution priority, don't you think?
69  *
70  * An execution with twice the priority will get twice the amount of flops when the resource is shared.
71  * The default priority is 1.
72  *
73  * Currently, this cannot be changed once the exec started. */
74 ExecPtr Exec::set_priority(double priority)
75 {
76   xbt_assert(state_ == State::INITED, "Cannot change the priority of an exec after its start");
77   priority_ = priority;
78   return this;
79 }
80
81 /** @brief change the execution bound, ie the maximal amount of flops per second that it may consume, regardless of what
82  * the host may deliver
83  *
84  * Currently, this cannot be changed once the exec started. */
85 ExecPtr Exec::set_bound(double bound)
86 {
87   xbt_assert(state_ == State::INITED, "Cannot change the bound of an exec after its start");
88   bound_ = bound;
89   return this;
90 }
91
92 /** @brief Change the host on which this activity takes place.
93  *
94  * The activity cannot be terminated already (but it may be started). */
95 ExecPtr Exec::set_host(Host* host)
96 {
97   xbt_assert(state_ == State::INITED || state_ == State::STARTED,
98              "Cannot change the host of an exec once it's done (state: %d)", (int)state_);
99   if (state_ == State::STARTED)
100     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->migrate(host);
101   host_ = host;
102   return this;
103 }
104
105 ExecPtr Exec::set_name(std::string name)
106 {
107   xbt_assert(state_ == State::INITED, "Cannot change the name of an exec after its start");
108   name_ = name;
109   return this;
110 }
111
112 ExecPtr Exec::set_tracing_category(std::string category)
113 {
114   xbt_assert(state_ == State::INITED, "Cannot change the tracing category of an exec after its start");
115   tracing_category_ = category;
116   return this;
117 }
118
119 /** @brief Retrieve the host on which this activity takes place. */
120 Host* Exec::get_host()
121 {
122   return host_;
123 }
124
125 /** @brief Returns the amount of flops that remain to be done */
126 double Exec::get_remaining()
127 {
128   return simgrid::simix::simcall(
129       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->get_remaining(); });
130 }
131
132 /** @brief Returns the ratio of elements that are still to do
133  *
134  * The returned value is between 0 (completely done) and 1 (nothing done yet).
135  */
136 double Exec::get_remaining_ratio()
137 {
138   return simgrid::simix::simcall([this]() {
139     return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->get_remaining_ratio();
140   });
141 }
142
143 void intrusive_ptr_release(simgrid::s4u::Exec* e)
144 {
145   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
146     std::atomic_thread_fence(std::memory_order_acquire);
147     delete e;
148   }
149 }
150
151 void intrusive_ptr_add_ref(simgrid::s4u::Exec* e)
152 {
153   e->refcount_.fetch_add(1, std::memory_order_relaxed);
154 }
155 } // namespace s4u
156 } // namespace simgrid