Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
47d7ef6302dce0a5f06cdfed1033198481ba363e
[simgrid.git] / src / surf / network_ib.cpp
1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/surf/network_ib.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/surf/HostImpl.hpp"
11 #include "src/surf/xml/platf.hpp"
12 #include "surf/surf.hpp"
13
14 #include <boost/algorithm/string/classification.hpp>
15 #include <boost/algorithm/string/split.hpp>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
18
19 /*********
20  * Model *
21  *********/
22
23 /************************************************************************/
24 /* New model based on MPI contention model for Infiniband platforms */
25 /************************************************************************/
26 /* @Inproceedings{mescal_vienne_phd, */
27 /*  author={Jérôme Vienne}, */
28 /*  title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
29 /*  address={Grenoble FRANCE}, */
30 /*  month=june, */
31 /*  year={2010} */
32 /*  } */
33 void surf_network_model_init_IB()
34 {
35   using simgrid::kernel::resource::NetworkIBModel;
36
37   auto net_model = std::make_shared<NetworkIBModel>("Network_IB");
38   auto* engine   = simgrid::kernel::EngineImpl::get_instance();
39   engine->add_model(net_model);
40   engine->get_netzone_root()->set_network_model(net_model);
41
42   simgrid::s4u::Link::on_communication_state_change.connect(NetworkIBModel::IB_action_state_changed_callback);
43   simgrid::s4u::Link::on_communicate.connect(NetworkIBModel::IB_action_init_callback);
44   simgrid::s4u::Host::on_creation.connect(NetworkIBModel::IB_create_host_callback);
45   simgrid::config::set_default<double>("network/weight-S", 8775);
46 }
47
48 namespace simgrid {
49 namespace kernel {
50 namespace resource {
51
52 void NetworkIBModel::IB_create_host_callback(s4u::Host const& host)
53 {
54   static int id = 0;
55   auto* ibModel = static_cast<NetworkIBModel*>(host.get_netpoint()->get_englobing_zone()->get_network_model().get());
56   ibModel->active_nodes.emplace(host.get_name(), IBNode(id));
57   id++;
58 }
59
60 void NetworkIBModel::IB_action_state_changed_callback(NetworkAction& action, Action::State /*previous*/)
61 {
62   if (action.get_state() != Action::State::FINISHED)
63     return;
64   auto* ibModel                    = static_cast<NetworkIBModel*>(action.get_model());
65   std::pair<IBNode*, IBNode*> pair = ibModel->active_comms[&action];
66
67   XBT_DEBUG("IB callback - action %p finished", &action);
68   ibModel->update_IB_factors(&action, pair.first, pair.second, 1);
69   ibModel->active_comms.erase(&action);
70 }
71
72 void NetworkIBModel::IB_action_init_callback(NetworkAction& action)
73 {
74   auto* ibModel = static_cast<NetworkIBModel*>(action.get_model());
75   auto* act_src = &ibModel->active_nodes.at(action.get_src().get_name());
76   auto* act_dst = &ibModel->active_nodes.at(action.get_dst().get_name());
77
78   ibModel->active_comms[&action] = std::make_pair(act_src, act_dst);
79   ibModel->update_IB_factors(&action, act_src, act_dst, 0);
80 }
81
82 NetworkIBModel::NetworkIBModel(const std::string& name) : NetworkSmpiModel(name)
83 {
84   std::string IB_factors_string = config::get_value<std::string>("smpi/IB-penalty-factors");
85   std::vector<std::string> radical_elements;
86   boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
87
88   surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
89                                                   "elements, semi-colon separated. Example: 0.965;0.925;1.35");
90
91   try {
92     Be_ = std::stod(radical_elements.front());
93   } catch (const std::invalid_argument& ia) {
94     throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
95   }
96
97   try {
98     Bs_ = std::stod(radical_elements.at(1));
99   } catch (const std::invalid_argument& ia) {
100     throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
101   }
102
103   try {
104     ys_ = std::stod(radical_elements.back());
105   } catch (const std::invalid_argument& ia) {
106     throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
107   }
108 }
109
110 void NetworkIBModel::compute_IB_factors(IBNode* root) const
111 {
112   size_t num_comm_out    = root->active_comms_up_.size();
113   double max_penalty_out = 0.0;
114   // first, compute all outbound penalties to get their max
115   for (ActiveComm const* comm : root->active_comms_up_) {
116     double my_penalty_out = 1.0;
117
118     if (num_comm_out != 1) {
119       if (comm->destination->nb_active_comms_down_ > 2) // number of comms sent to the receiving node
120         my_penalty_out = num_comm_out * Bs_ * ys_;
121       else
122         my_penalty_out = num_comm_out * Bs_;
123     }
124
125     max_penalty_out = std::max(max_penalty_out, my_penalty_out);
126   }
127
128   for (ActiveComm* comm : root->active_comms_up_) {
129     // compute inbound penalty
130     double my_penalty_in = 1.0;
131     int nb_comms         = comm->destination->nb_active_comms_down_; // total number of incoming comms
132     if (nb_comms != 1)
133       my_penalty_in = (comm->destination->active_comms_down_)[root]         // number of comm sent to dest by root node
134                       * Be_ * comm->destination->active_comms_down_.size(); // number of different nodes sending to dest
135
136     double penalty = std::max(my_penalty_in, max_penalty_out);
137
138     double rate_before_update = comm->action->get_bound();
139     // save initial rate of the action
140     if (comm->init_rate == -1)
141       comm->init_rate = rate_before_update;
142
143     double penalized_bw = num_comm_out ? comm->init_rate / penalty : comm->init_rate;
144
145     if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
146       XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id_,
147                 comm->destination->id_, comm->action, penalized_bw, comm->action->get_bound(), comm->init_rate);
148       get_maxmin_system()->update_variable_bound(comm->action->get_variable(), penalized_bw);
149     } else {
150       XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id_, comm->destination->id_,
151                 comm->action, penalized_bw, comm->init_rate);
152     }
153   }
154   XBT_DEBUG("Finished computing IB penalties");
155 }
156
157 void NetworkIBModel::update_IB_factors_rec(IBNode* root, std::vector<bool>& updatedlist) const
158 {
159   if (not updatedlist[root->id_]) {
160     XBT_DEBUG("IB - Updating rec %d", root->id_);
161     compute_IB_factors(root);
162     updatedlist[root->id_] = true;
163     for (ActiveComm const* comm : root->active_comms_up_) {
164       if (not updatedlist[comm->destination->id_])
165         update_IB_factors_rec(comm->destination, updatedlist);
166     }
167     for (std::map<IBNode*, int>::value_type const& comm : root->active_comms_down_) {
168       if (not updatedlist[comm.first->id_])
169         update_IB_factors_rec(comm.first, updatedlist);
170     }
171   }
172 }
173
174 void NetworkIBModel::update_IB_factors(NetworkAction* action, IBNode* from, IBNode* to, int remove) const
175 {
176   if (from == to) // disregard local comms (should use loopback)
177     return;
178
179   if (remove) {
180     if (to->active_comms_down_[from] == 1)
181       to->active_comms_down_.erase(from);
182     else
183       to->active_comms_down_[from] -= 1;
184
185     to->nb_active_comms_down_--;
186     auto it = std::find_if(begin(from->active_comms_up_), end(from->active_comms_up_),
187                            [action](const ActiveComm* comm) { return comm->action == action; });
188     if (it != std::end(from->active_comms_up_)) {
189       delete *it;
190       from->active_comms_up_.erase(it);
191     }
192     action->unref();
193   } else {
194     action->ref();
195     auto* comm        = new ActiveComm();
196     comm->action      = action;
197     comm->destination = to;
198     from->active_comms_up_.push_back(comm);
199
200     to->active_comms_down_[from] += 1;
201     to->nb_active_comms_down_++;
202   }
203   XBT_DEBUG("IB - Updating %d", from->id_);
204   std::vector<bool> updated(active_nodes.size(), false);
205   update_IB_factors_rec(from, updated);
206   XBT_DEBUG("IB - Finished updating %d", from->id_);
207 }
208 } // namespace resource
209 } // namespace kernel
210 } // namespace simgrid