Logo AND Algorithmique Numérique Distribuée

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