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 struct ExtendedAttributeActivity {
28 static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
29 Operation* operation_;
35 std::set<Operation*> successors_ = {};
36 std::map<Operation*, unsigned int> predecessors_ = {};
38 void add_predecessor(Operation* predecessor);
39 void remove_predecessor(Operation* predecessor);
40 bool ready_to_run() const;
41 void receive(Operation* source);
47 int queued_execs_ = 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;
57 static xbt::signal<void(Operation*)> on_start;
58 static xbt::signal<void(Operation*)> on_end;
59 std::atomic_int_fast32_t refcount_{0};
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;
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); }
85 friend void intrusive_ptr_release(Operation* o)
87 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
88 std::atomic_thread_fence(std::memory_order_acquire);
92 friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
96 class ExecOp : public Operation {
100 explicit ExecOp(const std::string& name);
101 void execute() override;
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)); }
114 class CommOp : public Operation {
117 s4u::Host* destination_;
119 explicit CommOp(const std::string& name);
120 void execute() override;
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)); }
135 } // namespace simgrid::plugins