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::function<void(Operation*)> end_func_;
52 std::function<void(Operation*)> start_func_;
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 on_this_start(const std::function<void(Operation*)>& func);
71 void on_this_end(const std::function<void(Operation*)>& func);
72 int get_count() const;
74 /** Add a callback fired before an operation activity start.
75 * Triggered after the on_this_start function**/
76 static void on_start_cb(const std::function<void(Operation*)>& cb) { on_start.connect(cb); }
77 /** Add a callback fired after an operation activity end.
78 * Triggered after the on_this_end function, but before
79 * sending tokens to successors.**/
80 static void on_end_cb(const std::function<void(Operation*)>& cb) { on_end.connect(cb); }
83 friend void intrusive_ptr_release(Operation* o)
85 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
86 std::atomic_thread_fence(std::memory_order_acquire);
90 friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
94 class ExecOp : public Operation {
98 explicit ExecOp(const std::string& name);
99 void execute() override;
102 static ExecOpPtr init(const std::string& name);
103 static ExecOpPtr init(const std::string& name, double flops, s4u::Host* host);
104 ExecOpPtr set_host(s4u::Host* host);
105 s4u::Host* get_host() const { return host_; }
106 ExecOpPtr set_flops(double flops);
107 double get_flops() const { return get_amount(); }
108 friend void inline intrusive_ptr_release(ExecOp* e) { intrusive_ptr_release(static_cast<Operation*>(e)); }
109 friend void inline intrusive_ptr_add_ref(ExecOp* e) { intrusive_ptr_add_ref(static_cast<Operation*>(e)); }
112 class CommOp : public Operation {
115 s4u::Host* destination_;
117 explicit CommOp(const std::string& name);
118 void execute() override;
121 static CommOpPtr init(const std::string& name);
122 static CommOpPtr init(const std::string& name, double bytes, s4u::Host* source,
123 s4u::Host* destination);
124 CommOpPtr set_source(s4u::Host* source);
125 s4u::Host* get_source() const { return source_; }
126 CommOpPtr set_destination(s4u::Host* destination);
127 s4u::Host* get_destination() const { return destination_; }
128 CommOpPtr set_bytes(double bytes);
129 double get_bytes() const { return get_amount(); }
130 friend void inline intrusive_ptr_release(CommOp* c) { intrusive_ptr_release(static_cast<Operation*>(c)); }
131 friend void inline intrusive_ptr_add_ref(CommOp* c) { intrusive_ptr_add_ref(static_cast<Operation*>(c)); }
134 } // namespace simgrid::plugins