1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/surf/network_ib.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/surf/HostImpl.hpp"
10 #include "src/surf/xml/platf.hpp"
11 #include "surf/surf.hpp"
13 #include <boost/algorithm/string/classification.hpp>
14 #include <boost/algorithm/string/split.hpp>
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
18 static void IB_create_host_callback(simgrid::s4u::Host const& host)
20 using simgrid::kernel::resource::IBNode;
21 using simgrid::kernel::resource::NetworkIBModel;
24 auto* ibModel = static_cast<NetworkIBModel*>(host.get_netpoint()->get_englobing_zone()->get_network_model());
25 ibModel->active_nodes.emplace(host.get_name(), IBNode(id));
29 static void IB_action_state_changed_callback(simgrid::kernel::resource::NetworkAction& action,
30 simgrid::kernel::resource::Action::State /*previous*/)
32 using simgrid::kernel::resource::IBNode;
34 if (action.get_state() != simgrid::kernel::resource::Action::State::FINISHED)
36 auto* ibModel = static_cast<simgrid::kernel::resource::NetworkIBModel*>(action.get_model());
37 std::pair<IBNode*, IBNode*> pair = ibModel->active_comms[&action];
38 XBT_DEBUG("IB callback - action %p finished", &action);
40 ibModel->updateIBfactors(&action, pair.first, pair.second, 1);
42 ibModel->active_comms.erase(&action);
45 static void IB_action_init_callback(simgrid::kernel::resource::NetworkAction& action)
47 auto* ibModel = static_cast<simgrid::kernel::resource::NetworkIBModel*>(action.get_model());
48 auto* act_src = &ibModel->active_nodes.at(action.get_src().get_name());
49 auto* act_dst = &ibModel->active_nodes.at(action.get_dst().get_name());
51 ibModel->active_comms[&action] = std::make_pair(act_src, act_dst);
52 ibModel->updateIBfactors(&action, act_src, act_dst, 0);
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}, */
69 void surf_network_model_init_IB()
71 /* FIXME[donassolo]: this smells bad, but works
72 * (the constructor saves its pointer in all_existing_models and models_by_type :O).
73 * We need a manager for these models */
74 new simgrid::kernel::resource::NetworkIBModel();
75 simgrid::s4u::Link::on_communication_state_change.connect(IB_action_state_changed_callback);
76 simgrid::s4u::Link::on_communicate.connect(IB_action_init_callback);
77 simgrid::s4u::Host::on_creation.connect(IB_create_host_callback);
78 simgrid::config::set_default<double>("network/weight-S", 8775);
85 NetworkIBModel::NetworkIBModel() : NetworkSmpiModel()
87 /* Do not add this into all_existing_models: our ancestor already does so */
89 std::string IB_factors_string = config::get_value<std::string>("smpi/IB-penalty-factors");
90 std::vector<std::string> radical_elements;
91 boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
93 surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
94 "elements, semi-colon separated. Example: 0.965;0.925;1.35");
97 Be = std::stod(radical_elements.front());
98 } catch (const std::invalid_argument& ia) {
99 throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
103 Bs = std::stod(radical_elements.at(1));
104 } catch (const std::invalid_argument& ia) {
105 throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
109 ys = std::stod(radical_elements.back());
110 } catch (const std::invalid_argument& ia) {
111 throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
115 void NetworkIBModel::computeIBfactors(IBNode* root) const
117 double num_comm_out = root->ActiveCommsUp.size();
118 double max_penalty_out = 0.0;
119 // first, compute all outbound penalties to get their max
120 for (ActiveComm const* comm : root->ActiveCommsUp) {
121 double my_penalty_out = 1.0;
123 if (num_comm_out != 1) {
124 if (comm->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
125 my_penalty_out = num_comm_out * Bs * ys;
127 my_penalty_out = num_comm_out * Bs;
130 max_penalty_out = std::max(max_penalty_out, my_penalty_out);
133 for (ActiveComm* comm : root->ActiveCommsUp) {
134 // compute inbound penalty
135 double my_penalty_in = 1.0;
136 int nb_comms = comm->destination->nbActiveCommsDown; // total number of incoming comms
138 my_penalty_in = (comm->destination->ActiveCommsDown)[root] // number of comm sent to dest by root node
139 * Be * comm->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
141 double penalty = std::max(my_penalty_in, max_penalty_out);
143 double rate_before_update = comm->action->get_bound();
144 // save initial rate of the action
145 if (comm->init_rate == -1)
146 comm->init_rate = rate_before_update;
148 double penalized_bw = num_comm_out ? comm->init_rate / penalty : comm->init_rate;
150 if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
151 XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
152 comm->destination->id, comm->action, penalized_bw, comm->action->get_bound(), comm->init_rate);
153 get_maxmin_system()->update_variable_bound(comm->action->get_variable(), penalized_bw);
155 XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, comm->destination->id,
156 comm->action, penalized_bw, comm->init_rate);
159 XBT_DEBUG("Finished computing IB penalties");
162 void NetworkIBModel::updateIBfactors_rec(IBNode* root, std::vector<bool>& updatedlist) const
164 if (not updatedlist[root->id]) {
165 XBT_DEBUG("IB - Updating rec %d", root->id);
166 computeIBfactors(root);
167 updatedlist[root->id] = true;
168 for (ActiveComm const* comm : root->ActiveCommsUp) {
169 if (not updatedlist[comm->destination->id])
170 updateIBfactors_rec(comm->destination, updatedlist);
172 for (std::map<IBNode*, int>::value_type const& comm : root->ActiveCommsDown) {
173 if (not updatedlist[comm.first->id])
174 updateIBfactors_rec(comm.first, updatedlist);
179 void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode* to, int remove) const
181 if (from == to) // disregard local comms (should use loopback)
185 if (to->ActiveCommsDown[from] == 1)
186 to->ActiveCommsDown.erase(from);
188 to->ActiveCommsDown[from] -= 1;
190 to->nbActiveCommsDown--;
191 auto it = std::find_if(begin(from->ActiveCommsUp), end(from->ActiveCommsUp),
192 [action](const ActiveComm* comm) { return comm->action == action; });
193 if (it != std::end(from->ActiveCommsUp)) {
195 from->ActiveCommsUp.erase(it);
200 auto* comm = new ActiveComm();
201 comm->action = action;
202 comm->destination = to;
203 from->ActiveCommsUp.push_back(comm);
205 to->ActiveCommsDown[from] += 1;
206 to->nbActiveCommsDown++;
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);
213 } // namespace resource
214 } // namespace kernel
215 } // namespace simgrid