Logo AND Algorithmique Numérique Distribuée

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