Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5ae2e2c3fd9289b5e6959131bc9a51b547c6f9b6
[simgrid.git] / src / plugins / operation.cpp
1 #include <simgrid/Exception.hpp>
2 #include <simgrid/plugins/operation.hpp>
3 #include <simgrid/s4u/Comm.hpp>
4 #include <simgrid/s4u/Exec.hpp>
5
6 #include "src/simgrid/module.hpp"
7
8 SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr)
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Operation, kernel, "Logging specific to the operation plugin");
10
11 namespace simgrid::plugins {
12 Operation::Operation(const std::string& name, double amount) : name_(name), amount_(amount) {}
13
14 std::string Operation::get_name()
15 {
16   return this->name_;
17 }
18
19 void Operation::add_predecessor(Operation* predecessor)
20 {
21   this->predecessors_[predecessor] = 0;
22 }
23
24 bool Operation::ready_to_run() const
25 {
26   if (this->working_ or (this->iteration_count_ != -1 and this->iteration_count_ >= this->iteration_limit_))
27     return false;
28   for (auto const& [key, val] : this->predecessors_)
29     if (val < 1)
30       return false;
31   return true;
32 }
33
34 void Operation::receive(Operation* source)
35 {
36   auto it = this->predecessors_.find(source);
37   it->second++;
38   if (this->ready_to_run())
39     this->execute();
40 }
41
42 void Operation::complete()
43 {
44   working_ = false;
45   call_end();
46   for (auto const& op : this->successors_)
47     op->receive(this);
48   if (ready_to_run())
49     execute();
50 }
51
52 void Operation::consume()
53 {
54   for (auto const& [key, val] : predecessors_)
55     predecessors_[key] = predecessors_[key] > 0 ? predecessors_[key] - 1 : 0;
56 }
57
58 void Operation::call_end()
59 {
60   end_func(this);
61 }
62
63 void Operation::init()
64 {
65   if (Operation::inited_)
66     return;
67   Operation::inited_                      = true;
68   ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
69   simgrid::s4u::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
70     activity.extension<ExtendedAttributeActivity>()->operation_->complete();
71   });
72 }
73
74 void Operation::set_iteration_limit(unsigned int n)
75 {
76   iteration_limit_ = n;
77 }
78
79 void Operation::add_successor(OperationPtr op)
80 {
81   successors_.insert(op.get());
82   op->add_predecessor(this);
83 }
84
85 void Operation::on_end(std::function<void(Operation*)> func)
86 {
87   end_func = func;
88 }
89
90 ExecOp::ExecOp(const std::string& name, double flops, simgrid::s4u::Host* host) : Operation(name, flops), host_(host) {}
91
92 ExecOpPtr ExecOp::create(const std::string& name, double flops, simgrid::s4u::Host* host)
93 {
94   auto op = ExecOpPtr(new ExecOp(name, flops, host));
95   return op;
96 }
97
98 void ExecOp::execute()
99 {
100   iteration_count_++;
101   working_ = true;
102   consume();
103   simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
104   exec->set_name(name_);
105   exec->set_flops_amount(amount_);
106   exec->set_host(host_);
107   exec->start();
108   exec->extension_set(new ExtendedAttributeActivity());
109   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
110   current_activity_                                        = exec;
111 }
112
113 CommOp::CommOp(const std::string& name, double bytes, simgrid::s4u::Host* source, simgrid::s4u::Host* destination)
114     : Operation(name, bytes), source_(source), destination_(destination)
115 {
116 }
117
118 CommOpPtr CommOp::create(const std::string& name, double bytes, simgrid::s4u::Host* source,
119                          simgrid::s4u::Host* destination)
120 {
121   auto op = CommOpPtr(new CommOp(name, bytes, source, destination));
122   return op;
123 }
124
125 void CommOp::execute()
126 {
127   iteration_count_++;
128   working_ = true;
129   consume();
130   simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
131   comm->set_name(name_);
132   comm->set_payload_size(amount_);
133   comm->start();
134   comm->extension_set(new ExtendedAttributeActivity());
135   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
136   current_activity_                                        = comm;
137 }
138 } // namespace simgrid::plugins
139
140 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
141     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
142 bool simgrid::plugins::Operation::inited_ = false;