1 #ifndef SIMGRID_PLUGINS_OPERATION_H_
2 #define SIMGRID_PLUGINS_OPERATION_H_
4 #include <simgrid/s4u/Activity.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <xbt/Extendable.hpp>
13 namespace simgrid::plugins {
16 using OperationPtr = boost::intrusive_ptr<Operation>;
17 XBT_PUBLIC void intrusive_ptr_release(Operation* o);
18 XBT_PUBLIC void intrusive_ptr_add_ref(Operation* o);
20 using ExecOpPtr = boost::intrusive_ptr<ExecOp>;
21 XBT_PUBLIC void intrusive_ptr_release(ExecOp* e);
22 XBT_PUBLIC void intrusive_ptr_add_ref(ExecOp* e);
24 using CommOpPtr = boost::intrusive_ptr<CommOp>;
25 XBT_PUBLIC void intrusive_ptr_release(CommOp* c);
26 XBT_PUBLIC void intrusive_ptr_add_ref(CommOp* c);
28 using IoOpPtr = boost::intrusive_ptr<IoOp>;
29 XBT_PUBLIC void intrusive_ptr_release(IoOp* i);
30 XBT_PUBLIC void intrusive_ptr_add_ref(IoOp* i);
32 struct ExtendedAttributeActivity {
33 static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
34 Operation* operation_;
39 std::set<Operation*> successors_ = {};
40 std::map<Operation*, unsigned int> predecessors_ = {};
42 void add_predecessor(Operation* predecessor);
43 void remove_predecessor(Operation* predecessor);
44 bool ready_to_run() const;
45 void receive(Operation* source);
51 int queued_execs_ = 0;
53 bool working_ = false;
54 s4u::ActivityPtr current_activity_;
55 std::vector<std::function<void(Operation*)>> end_func_handlers_;
56 std::vector<std::function<void(Operation*)>> start_func_handlers_;
57 explicit Operation(const std::string& name);
58 virtual ~Operation() = default;
59 virtual void execute() = 0;
61 static xbt::signal<void(Operation*)> on_start;
62 static xbt::signal<void(Operation*)> on_end;
63 std::atomic_int_fast32_t refcount_{0};
67 const std::string& get_name() const { return name_; }
68 const char* get_cname() const { return name_.c_str(); }
69 void enqueue_execs(int n);
70 void set_amount(double amount);
71 double get_amount() const { return amount_; }
72 void add_successor(OperationPtr op);
73 void remove_successor(OperationPtr op);
74 void remove_all_successors();
75 const std::set<Operation*>& get_successors() const { return successors_ ;}
76 void on_this_start(const std::function<void(Operation*)>& func);
77 void on_this_end(const std::function<void(Operation*)>& func);
78 int get_count() const;
80 /** Add a callback fired before an operation activity start.
81 * Triggered after the on_this_start function**/
82 static void on_start_cb(const std::function<void(Operation*)>& cb) { on_start.connect(cb); }
83 /** Add a callback fired after an operation activity end.
84 * Triggered after the on_this_end function, but before
85 * sending tokens to successors.**/
86 static void on_end_cb(const std::function<void(Operation*)>& cb) { on_end.connect(cb); }
89 friend void intrusive_ptr_release(Operation* o)
91 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
92 std::atomic_thread_fence(std::memory_order_acquire);
96 friend void intrusive_ptr_add_ref(Operation* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
100 class ExecOp : public Operation {
103 explicit ExecOp(const std::string& name);
104 void execute() override;
107 static ExecOpPtr init(const std::string& name);
108 static ExecOpPtr init(const std::string& name, double flops, s4u::Host* host);
109 ExecOpPtr set_host(s4u::Host* host);
110 s4u::Host* get_host() const { return host_; }
111 ExecOpPtr set_flops(double flops);
112 double get_flops() const { return get_amount(); }
113 friend void inline intrusive_ptr_release(ExecOp* e) { intrusive_ptr_release(static_cast<Operation*>(e)); }
114 friend void inline intrusive_ptr_add_ref(ExecOp* e) { intrusive_ptr_add_ref(static_cast<Operation*>(e)); }
117 class CommOp : public Operation {
119 s4u::Host* destination_;
121 explicit CommOp(const std::string& name);
122 void execute() override;
125 static CommOpPtr init(const std::string& name);
126 static CommOpPtr init(const std::string& name, double bytes, s4u::Host* source,
127 s4u::Host* destination);
128 CommOpPtr set_source(s4u::Host* source);
129 s4u::Host* get_source() const { return source_; }
130 CommOpPtr set_destination(s4u::Host* destination);
131 s4u::Host* get_destination() const { return destination_; }
132 CommOpPtr set_bytes(double bytes);
133 double get_bytes() const { return get_amount(); }
134 friend void inline intrusive_ptr_release(CommOp* c) { intrusive_ptr_release(static_cast<Operation*>(c)); }
135 friend void inline intrusive_ptr_add_ref(CommOp* c) { intrusive_ptr_add_ref(static_cast<Operation*>(c)); }
138 class IoOp : public Operation {
140 s4u::Io::OpType type_;
141 explicit IoOp(const std::string& name);
142 void execute() override;
145 static IoOpPtr init(const std::string& name);
146 static IoOpPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
147 IoOpPtr set_disk(s4u::Disk* disk);
148 s4u::Disk* get_disk() const { return disk_; }
149 IoOpPtr set_bytes(double bytes);
150 double get_bytes() { return get_amount(); }
151 IoOpPtr set_op_type(s4u::Io::OpType type);
153 friend void inline intrusive_ptr_release(IoOp* i) { intrusive_ptr_release(static_cast<Operation*>(i)); }
154 friend void inline intrusive_ptr_add_ref(IoOp* i) { intrusive_ptr_add_ref(static_cast<Operation*>(i)); }
156 } // namespace simgrid::plugins