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>
13 namespace simgrid::plugins {
16 using TaskPtr = boost::intrusive_ptr<Task>;
17 XBT_PUBLIC void intrusive_ptr_release(Task* o);
18 XBT_PUBLIC void intrusive_ptr_add_ref(Task* o);
20 using ExecTaskPtr = boost::intrusive_ptr<ExecTask>;
22 using CommTaskPtr = boost::intrusive_ptr<CommTask>;
24 using IoTaskPtr = boost::intrusive_ptr<IoTask>;
26 struct ExtendedAttributeActivity {
27 static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
32 std::set<Task*> successors_ = {};
33 std::map<Task*, unsigned int> predecessors_ = {};
35 bool ready_to_run() const;
36 void receive(Task* source);
42 int queued_execs_ = 0;
44 bool working_ = false;
45 s4u::ActivityPtr previous_activity_;
46 s4u::ActivityPtr current_activity_;
47 xbt::signal<void(Task*)> on_this_start_;
48 xbt::signal<void(Task*)> on_this_end_;
49 explicit Task(const std::string& name);
50 virtual ~Task() = default;
51 virtual void fire() = 0;
53 static xbt::signal<void(Task*)> on_start;
54 static xbt::signal<void(Task*)> on_end;
55 std::atomic_int_fast32_t refcount_{0};
59 const std::string& get_name() const { return name_; }
60 const char* get_cname() const { return name_.c_str(); }
61 void enqueue_execs(int n);
62 void set_amount(double amount);
63 double get_amount() const { return amount_; }
64 void add_successor(TaskPtr t);
65 void remove_successor(TaskPtr t);
66 void remove_all_successors();
67 const std::set<Task*>& get_successors() const { return successors_; }
68 void on_this_start_cb(const std::function<void(Task*)>& func);
69 void on_this_end_cb(const std::function<void(Task*)>& func);
70 int get_count() const;
72 /** Add a callback fired before a task activity start.
73 * Triggered after the on_this_start function**/
74 static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
75 /** Add a callback fired after a task activity end.
76 * Triggered after the on_this_end function, but before
77 * sending tokens to successors.**/
78 static void on_end_cb(const std::function<void(Task*)>& cb) { on_end.connect(cb); }
81 friend void intrusive_ptr_release(Task* o)
83 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
84 std::atomic_thread_fence(std::memory_order_acquire);
88 friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
92 class ExecTask : public Task {
95 explicit ExecTask(const std::string& name);
99 static ExecTaskPtr init(const std::string& name);
100 static ExecTaskPtr init(const std::string& name, double flops, s4u::Host* host);
101 ExecTaskPtr set_host(s4u::Host* host);
102 s4u::Host* get_host() const { return host_; }
103 ExecTaskPtr set_flops(double flops);
104 double get_flops() const { return get_amount(); }
107 class CommTask : public Task {
109 s4u::Host* destination_;
111 explicit CommTask(const std::string& name);
112 void fire() override;
115 static CommTaskPtr init(const std::string& name);
116 static CommTaskPtr init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination);
117 CommTaskPtr set_source(s4u::Host* source);
118 s4u::Host* get_source() const { return source_; }
119 CommTaskPtr set_destination(s4u::Host* destination);
120 s4u::Host* get_destination() const { return destination_; }
121 CommTaskPtr set_bytes(double bytes);
122 double get_bytes() const { return get_amount(); }
125 class IoTask : public Task {
127 s4u::Io::OpType type_;
128 explicit IoTask(const std::string& name);
129 void fire() override;
132 static IoTaskPtr init(const std::string& name);
133 static IoTaskPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
134 IoTaskPtr set_disk(s4u::Disk* disk);
135 s4u::Disk* get_disk() const { return disk_; }
136 IoTaskPtr set_bytes(double bytes);
137 double get_bytes() const { return get_amount(); }
138 IoTaskPtr set_op_type(s4u::Io::OpType type);
139 s4u::Io::OpType get_op_type() const { return type_; }
141 } // namespace simgrid::plugins