Logo AND Algorithmique Numérique Distribuée

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