Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0f2f0e664ffbb7f1f2ad69cf5e7654b207eba01e
[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 <map>
8 #include <memory>
9 #include <set>
10
11 namespace simgrid::plugins {
12
13 class Operation;
14 using OperationPtr = std::shared_ptr<Operation>;
15 class ExecOp;
16 using ExecOpPtr = std::shared_ptr<ExecOp>;
17 class CommOp;
18 using CommOpPtr = std::shared_ptr<CommOp>;
19
20 class ExtendedAttributeActivity {
21 public:
22   static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
23   Operation* operation_;
24
25   ExtendedAttributeActivity(){};
26 };
27
28 class Operation {
29 private:
30   static bool inited_;
31   std::set<Operation*> successors_                 = {};
32   std::map<Operation*, unsigned int> predecessors_ = {};
33
34   void add_predecessor(Operation* predecessor);
35   void remove_predecessor(Operation* predecessor);
36   bool ready_to_run() const;
37   void receive(Operation* source);
38   void complete();
39
40 protected:
41   std::string name_;
42   double amount_;
43   int queued_execs_ = 0;
44   int count_        = 0;
45   bool working_     = false;
46   simgrid::s4u::ActivityPtr current_activity_;
47   std::function<void(Operation*)> end_func_;
48   std::function<void(Operation*)> start_func_;
49   Operation(const std::string& name);
50   virtual ~Operation()   = default;
51   virtual void execute() = 0;
52
53   static xbt::signal<void(Operation*)> on_start;
54   static xbt::signal<void(Operation*)> on_end;
55
56 public:
57   static void init();
58   std::string get_name();
59   void enqueue_execs(int n);
60   void set_amount(double amount);
61   void add_successor(OperationPtr op);
62   void remove_successor(OperationPtr op);
63   void on_this_start(std::function<void(Operation*)> func);
64   void on_this_end(std::function<void(Operation*)> func);
65   int get_count();
66
67   /** Add a callback fired before an operation activity start.
68    * Triggered after the on_this_start function**/
69   static void on_start_cb(const std::function<void(Operation*)>& cb) { on_start.connect(cb); }
70   /** Add a callback fired after an operation activity end.
71    * Triggered after the on_this_end function, but before
72    * sending tokens to successors.**/
73   static void on_end_cb(const std::function<void(Operation*)>& cb) { on_end.connect(cb); }
74 };
75
76 class ExecOp : public Operation {
77 private:
78   simgrid::s4u::Host* host_;
79
80   ExecOp(const std::string& name);
81   void execute() override;
82
83 public:
84   static ExecOpPtr init(const std::string& name);
85   static ExecOpPtr init(const std::string& name, double flops, simgrid::s4u::Host* host);
86   void set_host(simgrid::s4u::Host* host);
87   void set_flops(double flops);
88 };
89
90 class CommOp : public Operation {
91 private:
92   simgrid::s4u::Host* source_;
93   simgrid::s4u::Host* destination_;
94
95   CommOp(const std::string& name);
96   void execute() override;
97
98 public:
99   static CommOpPtr init(const std::string& name);
100   static CommOpPtr init(const std::string& name, double bytes, simgrid::s4u::Host* source,
101                         simgrid::s4u::Host* destination);
102   void set_source(simgrid::s4u::Host* source);
103   void set_destination(simgrid::s4u::Host* destination);
104   void set_bytes(double bytes);
105 };
106 } // namespace simgrid::plugins
107 #endif