Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add simgrid::plugins::Token. switch from get_tokens to get_next_token_from
[simgrid.git] / include / simgrid / plugins / task.hpp
1 #ifndef SIMGRID_PLUGINS_TASK_H_
2 #define SIMGRID_PLUGINS_TASK_H_
3
4 #include <simgrid/s4u/Activity.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <xbt/Extendable.hpp>
7
8 #include <atomic>
9 #include <deque>
10 #include <map>
11 #include <memory>
12 #include <set>
13
14 namespace simgrid::plugins {
15
16 class Task;
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);
20 class ExecTask;
21 using ExecTaskPtr = boost::intrusive_ptr<ExecTask>;
22 class CommTask;
23 using CommTaskPtr = boost::intrusive_ptr<CommTask>;
24 class IoTask;
25 using IoTaskPtr = boost::intrusive_ptr<IoTask>;
26
27 struct ExtendedAttributeActivity {
28   static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
29   Task* task_;
30 };
31
32 class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
33
34 class Task {
35   std::set<Task*> successors_                 = {};
36   std::map<Task*, unsigned int> predecessors_ = {};
37
38   bool ready_to_run() const;
39   void receive(Task* source);
40   void complete();
41
42 protected:
43   std::string name_;
44   double amount_;
45   std::shared_ptr<Token> token_ = nullptr;
46   std::deque<std::map<TaskPtr, std::shared_ptr<Token>>> tokens_received_;
47   int queued_execs_ = 0;
48   int count_        = 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;
57
58   static xbt::signal<void(Task*)> on_start;
59   static xbt::signal<void(Task*)> on_end;
60   std::atomic_int_fast32_t refcount_{0};
61
62 public:
63   static void init();
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;
78
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); }
86
87 #ifndef DOXYGEN
88   friend void intrusive_ptr_release(Task* o)
89   {
90     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
91       std::atomic_thread_fence(std::memory_order_acquire);
92       delete o;
93     }
94   }
95   friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
96 #endif
97 };
98
99 class ExecTask : public Task {
100   s4u::Host* host_;
101
102   explicit ExecTask(const std::string& name);
103   void fire() override;
104
105 public:
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(); }
112 };
113
114 class CommTask : public Task {
115   s4u::Host* source_;
116   s4u::Host* destination_;
117
118   explicit CommTask(const std::string& name);
119   void fire() override;
120
121 public:
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(); }
130 };
131
132 class IoTask : public Task {
133   s4u::Disk* disk_;
134   s4u::Io::OpType type_;
135   explicit IoTask(const std::string& name);
136   void fire() override;
137
138 public:
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_; }
147 };
148 } // namespace simgrid::plugins
149 #endif