1 #ifndef SIMGRID_PLUGINS_OPERATION_H_
2 #define SIMGRID_PLUGINS_OPERATION_H_
4 #include <simgrid/s4u/Activity.hpp>
5 #include <xbt/Extendable.hpp>
12 namespace simgrid::plugins {
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);
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);
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);
27 class ExtendedAttributeActivity {
29 static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
30 Operation* operation_;
32 ExtendedAttributeActivity(){};
38 std::set<Operation*> successors_ = {};
39 std::map<Operation*, unsigned int> predecessors_ = {};
41 void add_predecessor(Operation* predecessor);
42 void remove_predecessor(Operation* predecessor);
43 bool ready_to_run() const;
44 void receive(Operation* source);
50 int queued_execs_ = 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;
60 static xbt::signal<void(Operation*)> on_start;
61 static xbt::signal<void(Operation*)> on_end;
62 std::atomic_int_fast32_t refcount_{0};
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);
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); }
86 friend void intrusive_ptr_release(Operation* o)
88 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
89 std::atomic_thread_fence(std::memory_order_acquire);
93 friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
97 class ExecOp : public Operation {
101 ExecOp(const std::string& name);
102 void execute() override;
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)); }
115 class CommOp : public Operation {
118 s4u::Host* destination_;
120 CommOp(const std::string& name);
121 void execute() override;
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)); }
137 } // namespace simgrid::plugins