Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / Task.hpp
1 #ifndef SIMGRID_S4U_TASK_H_
2 #define SIMGRID_S4U_TASK_H_
3
4 #include <simgrid/forward.h>
5 #include <simgrid/s4u/Activity.hpp>
6 #include <simgrid/s4u/Io.hpp>
7 #include <xbt/Extendable.hpp>
8
9 #include <atomic>
10 #include <deque>
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <xbt/asserts.h>
15
16 namespace simgrid::s4u {
17
18 class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
19
20 /** Task class */
21 class XBT_PUBLIC Task {
22
23   std::string name_;
24
25   std::map<std::string, double> amount_              = {{"instance_0", 0}, {"dispatcher", 0}, {"collector", 0}};
26   std::map<std::string, int> queued_firings_         = {{"instance_0", 0}, {"dispatcher", 0}, {"collector", 0}};
27   std::map<std::string, int> running_instances_      = {{"instance_0", 0}, {"dispatcher", 0}, {"collector", 0}};
28   std::map<std::string, int> count_                  = {{"instance_0", 0}, {"dispatcher", 0}, {"collector", 0}};
29   std::map<std::string, int> parallelism_degree_     = {{"instance_0", 1}, {"dispatcher", 1}, {"collector", 1}};
30   std::map<std::string, int> internal_bytes_to_send_ = {{"instance_0", 0}, {"dispatcher", 0}};
31
32   std::function<std::string()> load_balancing_function_;
33
34   std::set<Task*> successors_                 = {};
35   std::map<Task*, unsigned int> predecessors_ = {};
36   std::atomic_int_fast32_t refcount_{0};
37
38   bool ready_to_run(std::string instance);
39   void receive(Task* source);
40
41   std::shared_ptr<Token> token_ = nullptr;
42   std::map<TaskPtr, std::deque<std::shared_ptr<Token>>> tokens_received_;
43   std::map<std::string, std::deque<ActivityPtr>> current_activities_ = {
44       {"instance_0", {}}, {"dispatcher", {}}, {"collector", {}}};
45
46   inline static xbt::signal<void(Task*)> on_start;
47   xbt::signal<void(Task*)> on_this_start;
48   inline static xbt::signal<void(Task*)> on_completion;
49   xbt::signal<void(Task*)> on_this_completion;
50
51 protected:
52   explicit Task(const std::string& name);
53   virtual ~Task() = default;
54
55   virtual void fire(std::string instance);
56   void complete(std::string instance);
57
58   void store_activity(ActivityPtr a, const std::string& instance) { current_activities_[instance].push_back(a); }
59
60   virtual void add_instances(int n);
61   virtual void remove_instances(int n);
62
63 public:
64   /** @param name The new name of this Task */
65   void set_name(std::string name);
66   /** Retrieves the name of that Task as a C++ string */
67   const std::string& get_name() const { return name_; }
68   /** Retrieves the name of that Task as a C string */
69   const char* get_cname() const { return name_.c_str(); }
70   /** @param amount The new amount of work this instance of this Task has to do
71    *  @note In flops for ExecTasks instances and in bytes for CommTasks instances. In flops for dispatcher and collector
72    * instances */
73   void set_amount(double amount, std::string instance = "instance_0");
74   /** @return Amout of work this instance of this Task has to process */
75   double get_amount(std::string instance = "instance_0") const { return amount_.at(instance); }
76   /** @return Amount of queued firings for this instance of this Task */
77   int get_queued_firings(std::string instance = "instance_0") const { return queued_firings_.at(instance); }
78   /** @return Amount currently running of this instance of this Task */
79   int get_running_count(std::string instance = "instance_0") const { return running_instances_.at(instance); }
80   /** @return Number of times this instance of this Task has been completed */
81   int get_count(std::string instance = "collector") const { return count_.at(instance); }
82   /** @param n The parallelism degree to set
83    *  @brief The parallelism degree defines how many of this instance can run in parallel. */
84   void set_parallelism_degree(int n, std::string instance = "all");
85   /** @return Parallelism degree of this instance of this Task */
86   int get_parallelism_degree(std::string instance = "instance_0") const { return parallelism_degree_.at(instance); }
87   /** @param bytes The amount of bytes this instance has to send to the next instance of this Task
88    *  @note This amount is used when the host is different between the dispatcher and the instance doing the work of the
89    * Task, or between the instance and the collector. */
90   void set_internal_bytes(int bytes, std::string instance = "instance_0");
91   /** @return Amount of bytes this instance of the Task has to send to the next instance */
92   double get_internal_bytes(std::string instance = "instance_0") const { return internal_bytes_to_send_.at(instance); }
93   /** @param func The new balancing function
94    *  @note This function is used by the dispatcher to determine which instance will effectively do the work. This
95    * function must return the name of the instance as a string. The default balancing function always returns
96    * "instance_0" */
97   void set_load_balancing_function(std::function<std::string()> func);
98   /** @param token The new token */
99   void set_token(std::shared_ptr<Token> token);
100   /** @param t A Smart pointer to a Task
101    *  @return Oldest token received by this Task that was sent by Task t */
102   std::shared_ptr<Token> get_token_from(TaskPtr t) const { return tokens_received_.at(t).front(); }
103   /** @param t A Smart pointer to a Task
104    *  @return All tokens received by this Task that were sent by Task t */
105   std::deque<std::shared_ptr<Token>> get_tokens_from(TaskPtr t) const { return tokens_received_.at(t); }
106   /** @param t A Smart pointer to a Task
107    *   @brief Pop the oldest token received by this Task that was sent by Task t */
108   void deque_token_from(TaskPtr t);
109   /** @param t A Smart pointer to a Task
110    *  @brief Add t as a successor of this Task */
111   void add_successor(TaskPtr t);
112   /** @param t A Smart pointer to a Task
113    *  @brief Remove t from the successors of this Task */
114   void remove_successor(TaskPtr t);
115   /** @brief Remove all successors from this Task */
116   void remove_all_successors();
117   /** @return All successors of this Task */
118   const std::set<Task*>& get_successors() const { return successors_; }
119   /** @param n The number of firings to enqueue */
120   void enqueue_firings(int n);
121   /** Add a callback fired before this task activity starts */
122   void on_this_start_cb(const std::function<void(Task*)>& func) { on_this_start.connect(func); }
123   /** Add a callback fired before a task activity starts.
124    * Triggered after the on_this_start function**/
125   static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
126   /** Add a callback fired before this task activity ends */
127   void on_this_completion_cb(const std::function<void(Task*)>& func) { on_this_completion.connect(func); };
128   /** Add a callback fired after a task activity ends.
129    * Triggered after the on_this_end function, but before sending tokens to successors.**/
130   static void on_completion_cb(const std::function<void(Task*)>& cb) { on_completion.connect(cb); }
131
132 #ifndef DOXYGEN
133   friend void intrusive_ptr_release(Task* o)
134   {
135     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
136       std::atomic_thread_fence(std::memory_order_acquire);
137       delete o;
138     }
139   }
140   friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
141 #endif
142 };
143
144 /** CommTask class */
145 class CommTask : public Task {
146   Host* source_;
147   Host* destination_;
148
149   explicit CommTask(const std::string& name);
150   void fire(std::string instance) override;
151
152 public:
153   static CommTaskPtr init(const std::string& name);
154   static CommTaskPtr init(const std::string& name, double bytes, Host* source, Host* destination);
155
156   /** @param source The new source Host of this CommTask
157    *  @return A Smart pointer to this CommTask */
158   CommTaskPtr set_source(Host* source);
159   /** @return A pointer to the source Host of this CommTask */
160   Host* get_source() const { return source_; }
161   /** @param destination The new destination of this CommTask
162    *  @return A Smart pointer to the destination Host of this CommTask */
163   CommTaskPtr set_destination(Host* destination);
164   /** @return A pointer to the destination Host of this CommTask */
165   Host* get_destination() const { return destination_; }
166   /** @param bytes The amount of bytes this CommTask has to send */
167   CommTaskPtr set_bytes(double bytes);
168   /** @return The amout of bytes this CommTask has to send */
169   double get_bytes() const { return get_amount("instance_0"); }
170 };
171
172 /** ExecTask class */
173 class ExecTask : public Task {
174   std::map<std::string, Host*> host_ = {{"instance_0", nullptr}, {"dispatcher", nullptr}, {"collector", nullptr}};
175
176   explicit ExecTask(const std::string& name);
177   void fire(std::string instance) override;
178
179 public:
180   static ExecTaskPtr init(const std::string& name);
181   static ExecTaskPtr init(const std::string& name, double flops, Host* host);
182
183   /** @param host The new host of this instance of this ExecTask
184    *  @return a Smart pointer to this ExecTask */
185   ExecTaskPtr set_host(Host* host, std::string instance = "all");
186   /** @return A pointer to the host of this instance of this ExecTask */
187   Host* get_host(std::string instance = "instance_0") const { return host_.at(instance); }
188   /** @param flops The new amount of flops this instance of this Task has to execute
189    *  @return A Smart pointer to this ExecTask */
190   ExecTaskPtr set_flops(double flops, std::string instance = "instance_0");
191   /** @return The amount of flops this instance of this ExecTask has to execute */
192   double get_flops(std::string instance = "instance_0") const { return get_amount(instance); }
193   /** @param n The number of instances to add to this ExecTask */
194   void add_instances(int n) override;
195   /** @param n The number of isntances to remove from this ExecTask */
196   void remove_instances(int n) override;
197 };
198
199 /** IoTask class */
200 class IoTask : public Task {
201   Disk* disk_;
202   Io::OpType type_;
203   explicit IoTask(const std::string& name);
204   void fire(std::string instance) override;
205
206 public:
207   static IoTaskPtr init(const std::string& name);
208   static IoTaskPtr init(const std::string& name, double bytes, Disk* disk, Io::OpType type);
209
210   /** @param disk The new disk of this IoTask
211    * @return A Smart pointer to this IoTask */
212   IoTaskPtr set_disk(Disk* disk);
213   /** @return A pointer to the disk of this IoTask */
214   Disk* get_disk() const { return disk_; }
215   /** @param bytes The new amount of bytes this IoTask has to write or read
216    *  @return A Smart pointer to this IoTask */
217   IoTaskPtr set_bytes(double bytes);
218   /** @return The amount of bytes this IoTask has to write or read */
219   double get_bytes() const { return get_amount("instance_0"); }
220   /** @param type The type of operation this IoTask has to do
221    *  @return A Smart pointer to this IoTask */
222   IoTaskPtr set_op_type(Io::OpType type);
223   /** @return The type of operation this IoTask has to to */
224   Io::OpType get_op_type() const { return type_; }
225 };
226 } // namespace simgrid::s4u
227 #endif