Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / surf / network_ib.cpp
1 /* Copyright (c) 2014-2015. 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/maxmin_private.hpp"
12 #include "src/surf/network_ib.hpp"
13 #include "src/surf/xml/platf.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   try {
56     act_src = ibModel->active_nodes.at(src->getName());
57   } catch (std::out_of_range& unfound) {
58     throw std::out_of_range(std::string("Could not find '") + src->getName() + "' active comms !");
59   }
60
61   try {
62     act_dst = ibModel->active_nodes.at(dst->getName());
63   } catch (std::out_of_range& unfound) {
64     throw std::out_of_range(std::string("Could not find '") + dst->getName() + "' 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   haveGap_                      = false;
106   const char* IB_factors_string = xbt_cfg_get_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 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->getBound();
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->getBound(), (*it)->init_rate);
176       lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), 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, bool* updatedlist)
186 {
187   if (updatedlist[root->id] == 0) {
188     XBT_DEBUG("IB - Updating rec %d", root->id);
189     computeIBfactors(root);
190     updatedlist[root->id] = 1;
191     for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
192       if (updatedlist[(*it)->destination->id] != 1)
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 (updatedlist[it->first->id] != 1)
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   bool* updated    = (bool*)xbt_malloc0(active_nodes.size() * sizeof(bool));
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   updateIBfactors_rec(from, updated);
236   XBT_DEBUG("IB - Finished updating %d", from->id);
237   if (comm)
238     delete comm;
239   xbt_free(updated);
240 }
241 }
242 }