Logo AND Algorithmique Numérique Distribuée

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