Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert the last two simdag examples. Simdag can die
[simgrid.git] / include / simgrid / s4u / Activity.hpp
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 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
8
9 #include <algorithm>
10 #include <atomic>
11 #include <set>
12 #include <simgrid/forward.h>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 #include <xbt/Extendable.hpp>
17 #include <xbt/asserts.h>
18 #include <xbt/signal.hpp>
19 #include <xbt/utility.hpp>
20
21 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
22
23 namespace simgrid {
24 namespace s4u {
25
26 /** @brief Activities
27  *
28  * This class is the ancestor of every activities that an actor can undertake.
29  * That is, activities are all the things that do take time to the actor in the simulated world.
30  */
31 class XBT_PUBLIC Activity : public xbt::Extendable<Activity> {
32   friend Comm;
33   friend Exec;
34   friend Io;
35 #ifndef DOXYGEN
36   friend std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
37   friend std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
38 #endif
39
40 public:
41   // enum class State { ... }
42   XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
43
44   virtual bool is_assigned() const = 0;
45   virtual bool dependencies_solved() const { return dependencies_.empty(); }
46   virtual unsigned long is_waited_by() const { return successors_.size(); }
47   const std::set<ActivityPtr>& get_dependencies() const { return dependencies_; }
48   const std::vector<ActivityPtr>& get_successors() const { return successors_; }
49
50 protected:
51   Activity()  = default;
52   virtual ~Activity() = default;
53   void destroy();
54
55   void release_dependencies()
56   {
57     while (not successors_.empty()) {
58       ActivityPtr b = successors_.back();
59       XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
60       b->dependencies_.erase(this);
61       if (b->dependencies_solved()) {
62         b->vetoable_start();
63       }
64       successors_.pop_back();
65     }
66   }
67
68   void add_successor(ActivityPtr a)
69   {
70     if(this == a)
71       throw std::invalid_argument("Cannot be its own successor");
72     auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
73     if (p != successors_.end())
74       throw std::invalid_argument("Dependency already exists");
75
76     successors_.push_back(a);
77     a->dependencies_.insert({this});
78   }
79
80   void remove_successor(ActivityPtr a)
81   {
82     if(this == a)
83       throw std::invalid_argument("Cannot ask to remove itself from successors list");
84
85     auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
86     if (p != successors_.end()){
87       successors_.erase(p);
88       a->dependencies_.erase({this});
89     } else
90       throw std::invalid_argument("Dependency does not exist. Can not be removed.");
91   }
92
93   static std::set<Activity*>* vetoed_activities_;
94
95 public:
96   /*! Signal fired each time that the activity fails to start because of a veto (e.g., unsolved dependency or no
97    * resource assigned) */
98   static xbt::signal<void(Activity&)> on_veto;
99   /*! Signal fired when theactivity completes  (either normally, cancelled or failed) */
100   static xbt::signal<void(Activity&)> on_completion;
101
102   void vetoable_start()
103   {
104     state_ = State::STARTING;
105     if (dependencies_solved() && is_assigned()) {
106       XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
107       start();
108     } else {
109       if (vetoed_activities_ != nullptr)
110         vetoed_activities_->insert(this);
111       on_veto(*this);
112     }
113   }
114
115   void complete(Activity::State state)
116   {
117     state_ = state;
118     on_completion(*this);
119     if (state == State::FINISHED)
120       release_dependencies();
121   }
122
123   static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
124   static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
125
126 #ifndef DOXYGEN
127   Activity(Activity const&) = delete;
128   Activity& operator=(Activity const&) = delete;
129 #endif
130
131   /** Starts a previously created activity.
132    *
133    * This function is optional: you can call wait() even if you didn't call start()
134    */
135   virtual Activity* start() = 0;
136   /** Blocks the current actor until the activity is terminated */
137   Activity* wait() { return wait_for(-1.0); }
138   /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
139    *  Raises: timeout exception.*/
140   Activity* wait_for(double timeout);
141   /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
142    * Raises: timeout exception. */
143   void wait_until(double time_limit);
144
145   /** Cancel that activity */
146   Activity* cancel();
147   /** Retrieve the current state of the activity */
148   Activity::State get_state() const { return state_; }
149   /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
150   const char* get_state_str() const;
151   void set_state(Activity::State state) { state_ = state; }
152   /** Tests whether the given activity is terminated yet. */
153   virtual bool test();
154
155   /** Blocks the progression of this activity until it gets resumed */
156   virtual Activity* suspend();
157   /** Unblock the progression of this activity if it was suspended previously */
158   virtual Activity* resume();
159   /** Whether or not the progression of this activity is blocked */
160   bool is_suspended() const { return suspended_; }
161
162   virtual const char* get_cname() const       = 0;
163   virtual const std::string& get_name() const = 0;
164
165   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
166   virtual double get_remaining() const;
167   /** Set the [remaining] amount of work that this Activity will entail
168    *
169    * It is forbidden to change the amount of work once the Activity is started */
170   Activity* set_remaining(double remains);
171
172   double get_start_time() const;
173   double get_finish_time() const;
174   void mark() { marked_ = true; }
175   bool is_marked() const { return marked_; }
176
177   /** Returns the internal implementation of this Activity */
178   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
179
180 #ifndef DOXYGEN
181   friend void intrusive_ptr_release(Activity* a)
182   {
183     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
184       std::atomic_thread_fence(std::memory_order_acquire);
185       delete a;
186     }
187   }
188   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
189 #endif
190   Activity* add_ref()
191   {
192     intrusive_ptr_add_ref(this);
193     return this;
194   }
195   void unref() { intrusive_ptr_release(this); }
196
197 private:
198   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
199   Activity::State state_                   = Activity::State::INITED;
200   double remains_                          = 0;
201   bool suspended_                          = false;
202   bool marked_                             = false;
203   std::vector<ActivityPtr> successors_;
204   std::set<ActivityPtr> dependencies_;
205   std::atomic_int_fast32_t refcount_{0};
206 };
207
208 template <class AnyActivity> class Activity_T : public Activity {
209   std::string name_             = "unnamed";
210   std::string tracing_category_ = "";
211   void* user_data_              = nullptr;
212
213 public:
214   AnyActivity* add_successor(ActivityPtr a)
215   {
216     Activity::add_successor(a);
217     return static_cast<AnyActivity*>(this);
218   }
219   AnyActivity* remove_successor(ActivityPtr a)
220   {
221     Activity::remove_successor(a);
222     return static_cast<AnyActivity*>(this);
223   }
224   AnyActivity* set_name(const std::string& name)
225   {
226     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
227     name_ = name;
228     return static_cast<AnyActivity*>(this);
229   }
230   const std::string& get_name() const override { return name_; }
231   const char* get_cname() const override { return name_.c_str(); }
232
233   AnyActivity* set_tracing_category(const std::string& category)
234   {
235     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
236     tracing_category_ = category;
237     return static_cast<AnyActivity*>(this);
238   }
239   const std::string& get_tracing_category() const { return tracing_category_; }
240
241   AnyActivity* set_user_data(void* data)
242   {
243     user_data_ = data;
244     return static_cast<AnyActivity*>(this);
245   }
246
247   void* get_user_data() const { return user_data_; }
248
249   AnyActivity* vetoable_start()
250   {
251     Activity::vetoable_start();
252     return static_cast<AnyActivity*>(this);
253   }
254
255   AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
256   AnyActivity* wait() { return wait_for(-1.0); }
257   virtual AnyActivity* wait_for(double timeout) { return static_cast<AnyActivity*>(Activity::wait_for(timeout)); }
258
259 #ifndef DOXYGEN
260   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
261    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
262    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
263    */
264   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
265   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
266 #endif
267 };
268
269 } // namespace s4u
270 } // namespace simgrid
271
272 #endif /* SIMGRID_S4U_ACTIVITY_HPP */