1 #ifndef SIMGRID_S4U_TASK_H_
2 #define SIMGRID_S4U_TASK_H_
4 #include <simgrid/forward.h>
5 #include <simgrid/s4u/Activity.hpp>
6 #include <simgrid/s4u/Io.hpp>
7 #include <xbt/Extendable.hpp>
14 #include <xbt/asserts.h>
16 namespace simgrid::s4u {
18 class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
21 class XBT_PUBLIC Task {
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}};
32 std::function<std::string()> load_balancing_function_;
34 std::set<Task*> successors_ = {};
35 std::map<Task*, unsigned int> predecessors_ = {};
36 std::atomic_int_fast32_t refcount_{0};
38 bool ready_to_run(std::string instance);
39 void receive(Task* source);
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", {}}};
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;
52 explicit Task(const std::string& name);
53 virtual ~Task() = default;
55 virtual void fire(std::string instance);
56 void complete(std::string instance);
58 void store_activity(ActivityPtr a, const std::string& instance) { current_activities_[instance].push_back(a); }
60 virtual void add_instances(int n);
61 virtual void remove_instances(int n);
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
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
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); }
133 friend void intrusive_ptr_release(Task* o)
135 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
136 std::atomic_thread_fence(std::memory_order_acquire);
140 friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
144 /** CommTask class */
145 class CommTask : public Task {
149 explicit CommTask(const std::string& name);
150 void fire(std::string instance) override;
153 static CommTaskPtr init(const std::string& name);
154 static CommTaskPtr init(const std::string& name, double bytes, Host* source, Host* destination);
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"); }
172 /** ExecTask class */
173 class ExecTask : public Task {
174 std::map<std::string, Host*> host_ = {{"instance_0", nullptr}, {"dispatcher", nullptr}, {"collector", nullptr}};
176 explicit ExecTask(const std::string& name);
177 void fire(std::string instance) override;
180 static ExecTaskPtr init(const std::string& name);
181 static ExecTaskPtr init(const std::string& name, double flops, Host* host);
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;
200 class IoTask : public Task {
203 explicit IoTask(const std::string& name);
204 void fire(std::string instance) override;
207 static IoTaskPtr init(const std::string& name);
208 static IoTaskPtr init(const std::string& name, double bytes, Disk* disk, Io::OpType type);
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_; }
226 } // namespace simgrid::s4u