Logo AND Algorithmique Numérique Distribuée

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