Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dict --
[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/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   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   haveGap_                      = false;
108   const char* IB_factors_string = xbt_cfg_get_string("smpi/IB-penalty-factors");
109   std::vector<std::string> radical_elements;
110   boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
111
112   surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
113                                                   "elements, semi-colon separated. Example: 0.965;0.925;1.35");
114
115   try {
116     Be = std::stod(radical_elements.front());
117   } catch (std::invalid_argument& ia) {
118     throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
119   }
120
121   try {
122     Bs = std::stod(radical_elements.at(1));
123   } catch (std::invalid_argument& ia) {
124     throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
125   }
126
127   try {
128     ys = std::stod(radical_elements.back());
129   } catch (std::invalid_argument& ia) {
130     throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
131   }
132 }
133
134 NetworkIBModel::~NetworkIBModel()
135 {
136   for (auto instance : active_nodes)
137     delete instance.second;
138 }
139
140 void NetworkIBModel::computeIBfactors(IBNode* root)
141 {
142   double num_comm_out    = static_cast<double>(root->ActiveCommsUp.size());
143   double max_penalty_out = 0.0;
144   // first, compute all outbound penalties to get their max
145   for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
146     double my_penalty_out = 1.0;
147
148     if (num_comm_out != 1) {
149       if ((*it)->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
150         my_penalty_out = num_comm_out * Bs * ys;
151       else
152         my_penalty_out = num_comm_out * Bs;
153     }
154
155     max_penalty_out = std::max(max_penalty_out, my_penalty_out);
156   }
157
158   for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
159     // compute inbound penalty
160     double my_penalty_in = 1.0;
161     int nb_comms         = (*it)->destination->nbActiveCommsDown; // total number of incoming comms
162     if (nb_comms != 1)
163       my_penalty_in = ((*it)->destination->ActiveCommsDown)[root]        // number of comm sent to dest by root node
164                       * Be * (*it)->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
165
166     double penalty = std::max(my_penalty_in, max_penalty_out);
167
168     double rate_before_update = (*it)->action->getBound();
169     // save initial rate of the action
170     if ((*it)->init_rate == -1)
171       (*it)->init_rate = rate_before_update;
172
173     double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate;
174
175     if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
176       XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
177                 (*it)->destination->id, (*it)->action, penalized_bw, (*it)->action->getBound(), (*it)->init_rate);
178       lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
179     } else {
180       XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, (*it)->destination->id,
181                 (*it)->action, penalized_bw, (*it)->init_rate);
182     }
183   }
184   XBT_DEBUG("Finished computing IB penalties");
185 }
186
187 void NetworkIBModel::updateIBfactors_rec(IBNode* root, bool* updatedlist)
188 {
189   if (updatedlist[root->id] == 0) {
190     XBT_DEBUG("IB - Updating rec %d", root->id);
191     computeIBfactors(root);
192     updatedlist[root->id] = 1;
193     for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
194       if (updatedlist[(*it)->destination->id] != 1)
195         updateIBfactors_rec((*it)->destination, updatedlist);
196     }
197     for (std::map<IBNode*, int>::iterator it = root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
198       if (updatedlist[it->first->id] != 1)
199         updateIBfactors_rec(it->first, updatedlist);
200     }
201   }
202 }
203
204 void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode* to, int remove)
205 {
206   if (from == to) // disregard local comms (should use loopback)
207     return;
208
209   bool* updated    = (bool*)xbt_malloc0(active_nodes.size() * sizeof(bool));
210   ActiveComm* comm = nullptr;
211   if (remove) {
212     if (to->ActiveCommsDown[from] == 1)
213       to->ActiveCommsDown.erase(from);
214     else
215       to->ActiveCommsDown[from] -= 1;
216
217     to->nbActiveCommsDown--;
218     for (std::vector<ActiveComm*>::iterator it = from->ActiveCommsUp.begin(); it != from->ActiveCommsUp.end(); ++it) {
219       if ((*it)->action == action) {
220         comm = (*it);
221         from->ActiveCommsUp.erase(it);
222         break;
223       }
224     }
225     action->unref();
226   } else {
227     action->ref();
228     ActiveComm* comm  = new ActiveComm();
229     comm->action      = action;
230     comm->destination = to;
231     from->ActiveCommsUp.push_back(comm);
232
233     to->ActiveCommsDown[from] += 1;
234     to->nbActiveCommsDown++;
235   }
236   XBT_DEBUG("IB - Updating %d", from->id);
237   updateIBfactors_rec(from, updated);
238   XBT_DEBUG("IB - Finished updating %d", from->id);
239   if (comm)
240     delete comm;
241   xbt_free(updated);
242 }
243 }
244 }