Logo AND Algorithmique Numérique Distribuée

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