Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
939543da2ab1a346548f3988cb76a6fc3ab263fe
[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_   = [](Operation*) {};
48   std::function<void(Operation*)> start_func_ = [](Operation*) {};
49   Operation(const std::string& name, double amount);
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, double flops, simgrid::s4u::Host* host);
81   void execute();
82
83 public:
84   static ExecOpPtr create(const std::string& name, double flops, simgrid::s4u::Host* host);
85   void set_host(simgrid::s4u::Host* host);
86 };
87
88 class CommOp : public Operation {
89 private:
90   simgrid::s4u::Host* source_;
91   simgrid::s4u::Host* destination_;
92
93   CommOp(const std::string& name, double bytes, simgrid::s4u::Host* source, simgrid::s4u::Host* destination);
94   void execute();
95
96 public:
97   static CommOpPtr create(const std::string& name, double bytes, simgrid::s4u::Host* source,
98                           simgrid::s4u::Host* destination);
99   void set_source(simgrid::s4u::Host* source);
100   void set_destination(simgrid::s4u::Host* destination);
101 };
102 } // namespace simgrid::plugins
103 #endif