Logo AND Algorithmique Numérique Distribuée

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