Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop using default mpi_call
[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 <map>
10 #include <memory>
11 #include <set>
12
13 namespace simgrid::plugins {
14
15 class Task;
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);
19 class ExecTask;
20 using ExecTaskPtr = boost::intrusive_ptr<ExecTask>;
21 class CommTask;
22 using CommTaskPtr = boost::intrusive_ptr<CommTask>;
23 class IoTask;
24 using IoTaskPtr = boost::intrusive_ptr<IoTask>;
25
26 struct ExtendedAttributeActivity {
27   static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
28   Task* task_;
29 };
30
31 class Task {
32   std::set<Task*> successors_                 = {};
33   std::map<Task*, unsigned int> predecessors_ = {};
34
35   bool ready_to_run() const;
36   void receive(Task* source);
37   void complete();
38
39 protected:
40   std::string name_;
41   double amount_;
42   int queued_execs_ = 0;
43   int count_        = 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;
52
53   static xbt::signal<void(Task*)> on_start;
54   static xbt::signal<void(Task*)> on_end;
55   std::atomic_int_fast32_t refcount_{0};
56
57 public:
58   static void init();
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;
71
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); }
79
80 #ifndef DOXYGEN
81   friend void intrusive_ptr_release(Task* o)
82   {
83     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
84       std::atomic_thread_fence(std::memory_order_acquire);
85       delete o;
86     }
87   }
88   friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
89 #endif
90 };
91
92 class ExecTask : public Task {
93   s4u::Host* host_;
94
95   explicit ExecTask(const std::string& name);
96   void fire() override;
97
98 public:
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(); }
105 };
106
107 class CommTask : public Task {
108   s4u::Host* source_;
109   s4u::Host* destination_;
110
111   explicit CommTask(const std::string& name);
112   void fire() override;
113
114 public:
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(); }
123 };
124
125 class IoTask : public Task {
126   s4u::Disk* disk_;
127   s4u::Io::OpType type_;
128   explicit IoTask(const std::string& name);
129   void fire() override;
130
131 public:
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_; }
140 };
141 } // namespace simgrid::plugins
142 #endif