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;
33 std::set<Task*> successors_ = {};
34 std::map<Task*, unsigned int> predecessors_ = {};
36 bool ready_to_run() const;
37 void receive(Task* source);
43 std::shared_ptr<void> token_ = NULL;
44 std::deque<std::map<TaskPtr, std::shared_ptr<void>>> tokens_received_;
45 int queued_execs_ = 0;
47 bool working_ = false;
48 s4u::ActivityPtr previous_activity_;
49 s4u::ActivityPtr current_activity_;
50 xbt::signal<void(Task*)> on_this_start_;
51 xbt::signal<void(Task*)> on_this_end_;
52 explicit Task(const std::string& name);
53 virtual ~Task() = default;
54 virtual void fire() = 0;
56 static xbt::signal<void(Task*)> on_start;
57 static xbt::signal<void(Task*)> on_end;
58 std::atomic_int_fast32_t refcount_{0};
62 const std::string& get_name() const { return name_; }
63 const char* get_cname() const { return name_.c_str(); }
64 void enqueue_execs(int n);
65 void set_amount(double amount);
66 double get_amount() const { return amount_; }
67 void set_token(std::shared_ptr<void> token);
68 std::map<TaskPtr, std::shared_ptr<void>> get_tokens() const;
69 void add_successor(TaskPtr t);
70 void remove_successor(TaskPtr t);
71 void remove_all_successors();
72 const std::set<Task*>& get_successors() const { return successors_; }
73 void on_this_start_cb(const std::function<void(Task*)>& func);
74 void on_this_end_cb(const std::function<void(Task*)>& func);
75 int get_count() const;
77 /** Add a callback fired before a task activity start.
78 * Triggered after the on_this_start function**/
79 static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
80 /** Add a callback fired after a task activity end.
81 * Triggered after the on_this_end function, but before
82 * sending tokens to successors.**/
83 static void on_end_cb(const std::function<void(Task*)>& cb) { on_end.connect(cb); }
86 friend void intrusive_ptr_release(Task* o)
88 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
89 std::atomic_thread_fence(std::memory_order_acquire);
93 friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
97 class ExecTask : public Task {
100 explicit ExecTask(const std::string& name);
101 void fire() override;
104 static ExecTaskPtr init(const std::string& name);
105 static ExecTaskPtr init(const std::string& name, double flops, s4u::Host* host);
106 ExecTaskPtr set_host(s4u::Host* host);
107 s4u::Host* get_host() const { return host_; }
108 ExecTaskPtr set_flops(double flops);
109 double get_flops() const { return get_amount(); }
112 class CommTask : public Task {
114 s4u::Host* destination_;
116 explicit CommTask(const std::string& name);
117 void fire() override;
120 static CommTaskPtr init(const std::string& name);
121 static CommTaskPtr init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination);
122 CommTaskPtr set_source(s4u::Host* source);
123 s4u::Host* get_source() const { return source_; }
124 CommTaskPtr set_destination(s4u::Host* destination);
125 s4u::Host* get_destination() const { return destination_; }
126 CommTaskPtr set_bytes(double bytes);
127 double get_bytes() const { return get_amount(); }
130 class IoTask : public Task {
132 s4u::Io::OpType type_;
133 explicit IoTask(const std::string& name);
134 void fire() override;
137 static IoTaskPtr init(const std::string& name);
138 static IoTaskPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
139 IoTaskPtr set_disk(s4u::Disk* disk);
140 s4u::Disk* get_disk() const { return disk_; }
141 IoTaskPtr set_bytes(double bytes);
142 double get_bytes() const { return get_amount(); }
143 IoTaskPtr set_op_type(s4u::Io::OpType type);
144 s4u::Io::OpType get_op_type() const { return type_; }
146 } // namespace simgrid::plugins