1 /* Copyright (c) 2014-2023. 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 <simgrid/kernel/routing/NetPoint.hpp>
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/activity/CommImpl.hpp"
11 #include "src/kernel/resource/HostImpl.hpp"
12 #include "src/kernel/resource/models/network_ib.hpp"
14 #include <boost/algorithm/string/classification.hpp>
15 #include <boost/algorithm/string/split.hpp>
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
23 /************************************************************************/
24 /* New model based on MPI contention model for Infiniband platforms */
25 /************************************************************************/
26 /* @Inproceedings{mescal_vienne_phd, */
27 /* author={Jérôme Vienne}, */
28 /* title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
29 /* address={Grenoble FRANCE}, */
33 SIMGRID_REGISTER_NETWORK_MODEL(
35 "Realistic network model specifically tailored for HPC settings, with Infiniband contention model as described in "
36 "http://mescal.imag.fr/membres/jean-marc.vincent/index.html/PhD/Vienne.pdf",
38 using simgrid::kernel::resource::NetworkIBModel;
40 auto net_model = std::make_shared<NetworkIBModel>("Network_IB");
41 auto* engine = simgrid::kernel::EngineImpl::get_instance();
42 engine->add_model(net_model);
43 engine->get_netzone_root()->set_network_model(net_model);
45 simgrid::s4u::Link::on_communication_state_change_cb(NetworkIBModel::IB_action_state_changed_callback);
46 simgrid::kernel::activity::CommImpl::on_start.connect(NetworkIBModel::IB_comm_start_callback);
47 simgrid::s4u::Host::on_creation_cb(NetworkIBModel::IB_create_host_callback);
48 simgrid::config::set_default<double>("network/weight-S", 8775);
51 namespace simgrid::kernel::resource {
53 void NetworkIBModel::IB_create_host_callback(s4u::Host const& host)
56 auto* ibModel = static_cast<NetworkIBModel*>(host.get_netpoint()->get_englobing_zone()->get_network_model().get());
57 ibModel->active_nodes.try_emplace(host.get_name(), id);
61 void NetworkIBModel::IB_action_state_changed_callback(NetworkAction& action, Action::State /*previous*/)
63 if (action.get_state() != Action::State::FINISHED)
65 auto* ibModel = static_cast<NetworkIBModel*>(action.get_model());
66 auto [src, dst] = ibModel->active_comms[&action];
68 XBT_DEBUG("IB callback - action %p finished", &action);
69 ibModel->update_IB_factors(&action, src, dst, 1);
70 ibModel->active_comms.erase(&action);
73 void NetworkIBModel::IB_comm_start_callback(const activity::CommImpl& comm)
75 auto* action = static_cast<NetworkAction*>(comm.surf_action_);
76 auto* ibModel = static_cast<NetworkIBModel*>(action->get_model());
77 auto* act_src = &ibModel->active_nodes.at(action->get_src().get_name());
78 auto* act_dst = &ibModel->active_nodes.at(action->get_dst().get_name());
80 ibModel->active_comms[action] = std::make_pair(act_src, act_dst);
81 ibModel->update_IB_factors(action, act_src, act_dst, 0);
84 NetworkIBModel::NetworkIBModel(const std::string& name) : NetworkCm02Model(name)
86 std::string IB_factors_string = config::get_value<std::string>("smpi/IB-penalty-factors");
87 std::vector<std::string> radical_elements;
88 boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
90 xbt_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
91 "elements, semi-colon separated. Example: 0.965;0.925;1.35");
94 Be_ = std::stod(radical_elements.front());
95 } catch (const std::invalid_argument& ia) {
96 throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
100 Bs_ = std::stod(radical_elements.at(1));
101 } catch (const std::invalid_argument& ia) {
102 throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
106 ys_ = std::stod(radical_elements.back());
107 } catch (const std::invalid_argument& ia) {
108 throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
112 void NetworkIBModel::compute_IB_factors(IBNode* root) const
114 size_t num_comm_out = root->active_comms_up_.size();
115 double max_penalty_out = 0.0;
116 // first, compute all outbound penalties to get their max
117 for (ActiveComm const* comm : root->active_comms_up_) {
118 double my_penalty_out = 1.0;
120 if (num_comm_out != 1) {
121 if (comm->destination->nb_active_comms_down_ > 2) // number of comms sent to the receiving node
122 my_penalty_out = num_comm_out * Bs_ * ys_;
124 my_penalty_out = num_comm_out * Bs_;
127 max_penalty_out = std::max(max_penalty_out, my_penalty_out);
130 for (ActiveComm* comm : root->active_comms_up_) {
131 // compute inbound penalty
132 double my_penalty_in = 1.0;
133 if (comm->destination->nb_active_comms_down_ != 1) // total number of incoming comms
134 my_penalty_in = (comm->destination->active_comms_down_)[root] // number of comm sent to dest by root node
135 * Be_ * comm->destination->active_comms_down_.size(); // number of different nodes sending to dest
137 double penalty = std::max(my_penalty_in, max_penalty_out);
139 double rate_before_update = comm->action->get_bound();
140 // save initial rate of the action
141 if (comm->init_rate == -1)
142 comm->init_rate = rate_before_update;
144 double penalized_bw = num_comm_out ? comm->init_rate / penalty : comm->init_rate;
146 if (not double_equals(penalized_bw, rate_before_update, sg_precision_timing)) {
147 XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id_,
148 comm->destination->id_, comm->action, penalized_bw, comm->action->get_bound(), comm->init_rate);
149 get_maxmin_system()->update_variable_bound(comm->action->get_variable(), penalized_bw);
151 XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id_, comm->destination->id_,
152 comm->action, penalized_bw, comm->init_rate);
155 XBT_DEBUG("Finished computing IB penalties");
158 void NetworkIBModel::update_IB_factors_rec(IBNode* root, std::vector<bool>& updatedlist) const
160 if (not updatedlist[root->id_]) {
161 XBT_DEBUG("IB - Updating rec %d", root->id_);
162 compute_IB_factors(root);
163 updatedlist[root->id_] = true;
164 for (ActiveComm const* comm : root->active_comms_up_) {
165 if (not updatedlist[comm->destination->id_])
166 update_IB_factors_rec(comm->destination, updatedlist);
168 for (auto const& [comm, _] : root->active_comms_down_) {
169 if (not updatedlist[comm->id_])
170 update_IB_factors_rec(comm, updatedlist);
175 void NetworkIBModel::update_IB_factors(NetworkAction* action, IBNode* from, IBNode* to, int remove) const
177 if (from == to) // disregard local comms (should use loopback)
181 if (to->active_comms_down_[from] == 1)
182 to->active_comms_down_.erase(from);
184 to->active_comms_down_[from] -= 1;
186 to->nb_active_comms_down_--;
187 if (auto it = std::find_if(begin(from->active_comms_up_), end(from->active_comms_up_),
188 [action](const ActiveComm* comm) { return comm->action == action; });
189 it != std::end(from->active_comms_up_)) {
191 from->active_comms_up_.erase(it);
196 auto* comm = new ActiveComm();
197 comm->action = action;
198 comm->destination = to;
199 from->active_comms_up_.push_back(comm);
201 to->active_comms_down_[from] += 1;
202 to->nb_active_comms_down_++;
204 XBT_DEBUG("IB - Updating %d", from->id_);
205 std::vector<bool> updated(active_nodes.size(), false);
206 update_IB_factors_rec(from, updated);
207 XBT_DEBUG("IB - Finished updating %d", from->id_);
209 } // namespace simgrid::kernel::resource