1 #ifndef SIMGRID_S4U_TASK_H_
2 #define SIMGRID_S4U_TASK_H_
4 #include <simgrid/s4u/Activity.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <xbt/Extendable.hpp>
14 namespace simgrid::s4u {
17 using TaskPtr = boost::intrusive_ptr<Task>;
18 XBT_PUBLIC void intrusive_ptr_release(Task* o);
19 XBT_PUBLIC void intrusive_ptr_add_ref(Task* o);
21 using ExecTaskPtr = boost::intrusive_ptr<ExecTask>;
23 using CommTaskPtr = boost::intrusive_ptr<CommTask>;
25 using IoTaskPtr = boost::intrusive_ptr<IoTask>;
27 class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
32 int queued_firings_ = 0;
34 bool working_ = false;
36 std::set<Task*> successors_ = {};
37 std::map<Task*, unsigned int> predecessors_ = {};
38 std::atomic_int_fast32_t refcount_{0};
40 bool ready_to_run() const;
41 void receive(Task* source);
43 std::shared_ptr<Token> token_ = nullptr;
44 std::deque<std::map<TaskPtr, std::shared_ptr<Token>>> tokens_received_;
45 ActivityPtr previous_activity_;
46 ActivityPtr current_activity_;
49 explicit Task(const std::string& name);
50 virtual ~Task() = default;
55 void set_current_activity (ActivityPtr a) { current_activity_ = a; }
57 inline static xbt::signal<void(Task*)> on_start;
58 xbt::signal<void(Task*)> on_this_start;
59 inline static xbt::signal<void(Task*)> on_completion;
60 xbt::signal<void(Task*)> on_this_completion;
63 const std::string& get_name() const { return name_; }
64 const char* get_cname() const { return name_.c_str(); }
65 void set_amount(double amount);
66 double get_amount() const { return amount_; }
67 int get_count() const { return count_; }
69 void set_token(std::shared_ptr<Token> token);
70 std::shared_ptr<Token> get_next_token_from(TaskPtr t);
72 void add_successor(TaskPtr t);
73 void remove_successor(TaskPtr t);
74 void remove_all_successors();
75 const std::set<Task*>& get_successors() const { return successors_; }
77 void enqueue_firings(int n);
79 /** Add a callback fired before this task activity starts */
80 void on_this_start_cb(const std::function<void(Task*)>& func){ on_this_start.connect(func); }
81 /** Add a callback fired before a task activity starts.
82 * Triggered after the on_this_start function**/
83 static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
84 /** Add a callback fired before this task activity ends */
85 void on_this_completion_cb(const std::function<void(Task*)>& func) { on_this_completion.connect(func); };
86 /** Add a callback fired after a task activity ends.
87 * Triggered after the on_this_end function, but before sending tokens to successors.**/
88 static void on_completion_cb(const std::function<void(Task*)>& cb) { on_completion.connect(cb); }
91 friend void intrusive_ptr_release(Task* o)
93 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
94 std::atomic_thread_fence(std::memory_order_acquire);
98 friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
102 class CommTask : public Task {
106 explicit CommTask(const std::string& name);
107 void fire() override;
110 static CommTaskPtr init(const std::string& name);
111 static CommTaskPtr init(const std::string& name, double bytes, Host* source, Host* destination);
113 CommTaskPtr set_source(Host* source);
114 Host* get_source() const { return source_; }
115 CommTaskPtr set_destination(Host* destination);
116 Host* get_destination() const { return destination_; }
117 CommTaskPtr set_bytes(double bytes);
118 double get_bytes() const { return get_amount(); }
121 class ExecTask : public Task {
124 explicit ExecTask(const std::string& name);
125 void fire() override;
128 static ExecTaskPtr init(const std::string& name);
129 static ExecTaskPtr init(const std::string& name, double flops, Host* host);
131 ExecTaskPtr set_host(Host* host);
132 Host* get_host() const { return host_; }
133 ExecTaskPtr set_flops(double flops);
134 double get_flops() const { return get_amount(); }
137 class IoTask : public Task {
140 explicit IoTask(const std::string& name);
141 void fire() override;
144 static IoTaskPtr init(const std::string& name);
145 static IoTaskPtr init(const std::string& name, double bytes, Disk* disk, Io::OpType type);
147 IoTaskPtr set_disk(Disk* disk);
148 Disk* get_disk() const { return disk_; }
149 IoTaskPtr set_bytes(double bytes);
150 double get_bytes() const { return get_amount(); }
151 IoTaskPtr set_op_type(Io::OpType type);
152 Io::OpType get_op_type() const { return type_; }
154 } // namespace simgrid::s4u