1 #ifndef SIMGRID_PLUGINS_TASK_H_
2 #define SIMGRID_PLUGINS_TASK_H_
4 #include <simgrid/s4u/Activity.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <xbt/Extendable.hpp>
14 namespace simgrid::plugins {
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 struct ExtendedAttributeActivity {
28 static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
32 class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
35 std::set<Task*> successors_ = {};
36 std::map<Task*, unsigned int> predecessors_ = {};
38 bool ready_to_run() const;
39 void receive(Task* source);
45 std::shared_ptr<Token> token_ = nullptr;
46 std::deque<std::map<TaskPtr, std::shared_ptr<Token>>> tokens_received_;
47 int queued_execs_ = 0;
49 bool working_ = false;
50 s4u::ActivityPtr previous_activity_;
51 s4u::ActivityPtr current_activity_;
52 xbt::signal<void(Task*)> on_this_start_;
53 xbt::signal<void(Task*)> on_this_end_;
54 explicit Task(const std::string& name);
55 virtual ~Task() = default;
56 virtual void fire() = 0;
58 static xbt::signal<void(Task*)> on_start;
59 static xbt::signal<void(Task*)> on_end;
60 std::atomic_int_fast32_t refcount_{0};
64 const std::string& get_name() const { return name_; }
65 const char* get_cname() const { return name_.c_str(); }
66 void enqueue_execs(int n);
67 void set_amount(double amount);
68 double get_amount() const { return amount_; }
69 void set_token(std::shared_ptr<Token> token);
70 std::shared_ptr<Token> get_next_token_from(TaskPtr t);
71 void add_successor(TaskPtr t);
72 void remove_successor(TaskPtr t);
73 void remove_all_successors();
74 const std::set<Task*>& get_successors() const { return successors_; }
75 void on_this_start_cb(const std::function<void(Task*)>& func);
76 void on_this_end_cb(const std::function<void(Task*)>& func);
77 int get_count() const;
79 /** Add a callback fired before a task activity start.
80 * Triggered after the on_this_start function**/
81 static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
82 /** Add a callback fired after a task activity end.
83 * Triggered after the on_this_end function, but before
84 * sending tokens to successors.**/
85 static void on_end_cb(const std::function<void(Task*)>& cb) { on_end.connect(cb); }
88 friend void intrusive_ptr_release(Task* o)
90 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
91 std::atomic_thread_fence(std::memory_order_acquire);
95 friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
99 class ExecTask : public Task {
102 explicit ExecTask(const std::string& name);
103 void fire() override;
106 static ExecTaskPtr init(const std::string& name);
107 static ExecTaskPtr init(const std::string& name, double flops, s4u::Host* host);
108 ExecTaskPtr set_host(s4u::Host* host);
109 s4u::Host* get_host() const { return host_; }
110 ExecTaskPtr set_flops(double flops);
111 double get_flops() const { return get_amount(); }
114 class CommTask : public Task {
116 s4u::Host* destination_;
118 explicit CommTask(const std::string& name);
119 void fire() override;
122 static CommTaskPtr init(const std::string& name);
123 static CommTaskPtr init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination);
124 CommTaskPtr set_source(s4u::Host* source);
125 s4u::Host* get_source() const { return source_; }
126 CommTaskPtr set_destination(s4u::Host* destination);
127 s4u::Host* get_destination() const { return destination_; }
128 CommTaskPtr set_bytes(double bytes);
129 double get_bytes() const { return get_amount(); }
132 class IoTask : public Task {
134 s4u::Io::OpType type_;
135 explicit IoTask(const std::string& name);
136 void fire() override;
139 static IoTaskPtr init(const std::string& name);
140 static IoTaskPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
141 IoTaskPtr set_disk(s4u::Disk* disk);
142 s4u::Disk* get_disk() const { return disk_; }
143 IoTaskPtr set_bytes(double bytes);
144 double get_bytes() const { return get_amount(); }
145 IoTaskPtr set_op_type(s4u::Io::OpType type);
146 s4u::Io::OpType get_op_type() const { return type_; }
148 } // namespace simgrid::plugins