Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
properly remove the old signals and prevent MC to fire Comm::on_this_completion
[simgrid.git] / include / simgrid / plugins / operation.hpp
1 #ifndef SIMGRID_PLUGINS_OPERATION_H_
2 #define SIMGRID_PLUGINS_OPERATION_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 Operation;
16 using OperationPtr = boost::intrusive_ptr<Operation>;
17 XBT_PUBLIC void intrusive_ptr_release(Operation* o);
18 XBT_PUBLIC void intrusive_ptr_add_ref(Operation* o);
19 class ExecOp;
20 using ExecOpPtr = boost::intrusive_ptr<ExecOp>;
21 XBT_PUBLIC void intrusive_ptr_release(ExecOp* e);
22 XBT_PUBLIC void intrusive_ptr_add_ref(ExecOp* e);
23 class CommOp;
24 using CommOpPtr =  boost::intrusive_ptr<CommOp>;
25 XBT_PUBLIC void intrusive_ptr_release(CommOp* c);
26 XBT_PUBLIC void intrusive_ptr_add_ref(CommOp* c);
27 class IoOp;
28 using IoOpPtr =  boost::intrusive_ptr<IoOp>;
29 XBT_PUBLIC void intrusive_ptr_release(IoOp* i);
30 XBT_PUBLIC void intrusive_ptr_add_ref(IoOp* i);
31
32 struct ExtendedAttributeActivity {
33   static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
34   Operation* operation_;
35 };
36
37 class Operation {
38   static bool inited_;
39   std::set<Operation*> successors_                 = {};
40   std::map<Operation*, unsigned int> predecessors_ = {};
41
42   void add_predecessor(Operation* predecessor);
43   void remove_predecessor(Operation* predecessor);
44   bool ready_to_run() const;
45   void receive(Operation* source);
46   void complete();
47
48 protected:
49   std::string name_;
50   double amount_;
51   int queued_execs_ = 0;
52   int count_        = 0;
53   bool working_     = false;
54   s4u::ActivityPtr current_activity_;
55   std::vector<std::function<void(Operation*)>> end_func_handlers_;
56   std::vector<std::function<void(Operation*)>> start_func_handlers_;
57   explicit Operation(const std::string& name);
58   virtual ~Operation()   = default;
59   virtual void execute() = 0;
60
61   static xbt::signal<void(Operation*)> on_start;
62   static xbt::signal<void(Operation*)> on_end;
63   std::atomic_int_fast32_t refcount_{0};
64
65 public:
66   static void init();
67   const std::string& get_name() const { return name_; }
68   const char* get_cname() const { return name_.c_str(); }
69   void enqueue_execs(int n);
70   void set_amount(double amount);
71   double get_amount() const { return amount_; }
72   void add_successor(OperationPtr op);
73   void remove_successor(OperationPtr op);
74   void remove_all_successors();
75   const std::set<Operation*>& get_successors() const { return successors_ ;}
76   void on_this_start(const std::function<void(Operation*)>& func);
77   void on_this_end(const std::function<void(Operation*)>& func);
78   int get_count() const;
79
80   /** Add a callback fired before an operation activity start.
81    * Triggered after the on_this_start function**/
82   static void on_start_cb(const std::function<void(Operation*)>& cb) { on_start.connect(cb); }
83   /** Add a callback fired after an operation activity end.
84    * Triggered after the on_this_end function, but before
85    * sending tokens to successors.**/
86   static void on_end_cb(const std::function<void(Operation*)>& cb) { on_end.connect(cb); }
87
88 #ifndef DOXYGEN
89   friend void intrusive_ptr_release(Operation* o)
90   {
91     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
92       std::atomic_thread_fence(std::memory_order_acquire);
93       delete o;
94     }
95   }
96   friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
97 #endif
98 };
99
100 class ExecOp : public Operation {
101   s4u::Host* host_;
102
103   explicit ExecOp(const std::string& name);
104   void execute() override;
105
106 public:
107   static ExecOpPtr init(const std::string& name);
108   static ExecOpPtr init(const std::string& name, double flops, s4u::Host* host);
109   ExecOpPtr set_host(s4u::Host* host);
110   s4u::Host* get_host() const { return host_; }
111   ExecOpPtr set_flops(double flops);
112   double get_flops() const { return get_amount(); }
113   friend void inline intrusive_ptr_release(ExecOp* e) { intrusive_ptr_release(static_cast<Operation*>(e)); }
114   friend void inline intrusive_ptr_add_ref(ExecOp* e) { intrusive_ptr_add_ref(static_cast<Operation*>(e)); }
115 };
116
117 class CommOp : public Operation {
118   s4u::Host* source_;
119   s4u::Host* destination_;
120
121   explicit CommOp(const std::string& name);
122   void execute() override;
123
124 public:
125   static CommOpPtr init(const std::string& name);
126   static CommOpPtr init(const std::string& name, double bytes, s4u::Host* source,
127                         s4u::Host* destination);
128   CommOpPtr set_source(s4u::Host* source);
129   s4u::Host* get_source() const { return source_; }
130   CommOpPtr set_destination(s4u::Host* destination);
131   s4u::Host* get_destination() const { return destination_; }
132   CommOpPtr set_bytes(double bytes);
133   double get_bytes() const { return get_amount(); }
134   friend void inline intrusive_ptr_release(CommOp* c) { intrusive_ptr_release(static_cast<Operation*>(c)); }
135   friend void inline intrusive_ptr_add_ref(CommOp* c) { intrusive_ptr_add_ref(static_cast<Operation*>(c)); }
136 };
137
138 class IoOp : public Operation {
139   s4u::Disk* disk_;
140   s4u::Io::OpType type_;
141   explicit IoOp(const std::string& name);
142   void execute() override;
143
144 public:
145   static IoOpPtr init(const std::string& name);
146   static IoOpPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
147   IoOpPtr set_disk(s4u::Disk* disk);
148   s4u::Disk* get_disk() const { return disk_; }
149   IoOpPtr set_bytes(double bytes);
150   double get_bytes() { return get_amount(); }
151   IoOpPtr set_op_type(s4u::Io::OpType type);
152   s4u::Io::OpType get_op_type() { return type_; }
153
154   friend void inline intrusive_ptr_release(IoOp* i) { intrusive_ptr_release(static_cast<Operation*>(i)); }
155   friend void inline intrusive_ptr_add_ref(IoOp* i) { intrusive_ptr_add_ref(static_cast<Operation*>(i)); }
156 };
157 } // namespace simgrid::plugins
158 #endif