Logo AND Algorithmique Numérique Distribuée

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