Logo AND Algorithmique Numérique Distribuée

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