1 /* Copyright (c) 2014-2017. The SimGrid Team.
2 *All rights reserved. */
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. */
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>
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
19 static void IB_create_host_callback(simgrid::s4u::Host& host){
20 using simgrid::surf::NetworkIBModel;
21 using simgrid::surf::IBNode;
24 // pour t->id -> rajouter une nouvelle struct dans le dict, pour stocker les comms actives
26 IBNode* act = new IBNode(id);
29 ((NetworkIBModel*)surf_network_model)->active_nodes.insert({host.getName(), act});
32 static void IB_action_state_changed_callback(simgrid::surf::NetworkAction* action)
34 using simgrid::surf::NetworkIBModel;
35 using simgrid::surf::IBNode;
37 if (action->getState() != simgrid::surf::Action::State::done)
39 std::pair<IBNode*,IBNode*> pair = ((NetworkIBModel*)surf_network_model)->active_comms[action];
40 XBT_DEBUG("IB callback - action %p finished", action);
42 ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, pair.first, pair.second, 1);
44 ((NetworkIBModel*)surf_network_model)->active_comms.erase(action);
48 static void IB_action_init_callback(simgrid::surf::NetworkAction* action, simgrid::s4u::Host* src,
49 simgrid::s4u::Host* dst)
51 simgrid::surf::NetworkIBModel* ibModel = (simgrid::surf::NetworkIBModel*)surf_network_model;
52 simgrid::surf::IBNode* act_src;
53 simgrid::surf::IBNode* act_dst;
55 auto asrc = ibModel->active_nodes.find(src->getName());
56 if (asrc != ibModel->active_nodes.end()) {
57 act_src = asrc->second;
59 throw std::out_of_range(std::string("Could not find '") + src->getCname() + "' active comms !");
62 auto adst = ibModel->active_nodes.find(dst->getName());
63 if (adst != ibModel->active_nodes.end()) {
64 act_dst = adst->second;
66 throw std::out_of_range(std::string("Could not find '") + dst->getCname() + "' active comms !");
69 ibModel->active_comms[action]=std::make_pair(act_src, act_dst);
71 ibModel->updateIBfactors(action, act_src, act_dst, 0);
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}, */
88 void surf_network_model_init_IB()
90 if (surf_network_model)
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);
105 NetworkIBModel::NetworkIBModel() : NetworkSmpiModel()
107 std::string IB_factors_string = xbt_cfg_get_string("smpi/IB-penalty-factors");
108 std::vector<std::string> radical_elements;
109 boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
111 surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
112 "elements, semi-colon separated. Example: 0.965;0.925;1.35");
115 Be = std::stod(radical_elements.front());
116 } catch (std::invalid_argument& ia) {
117 throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
121 Bs = std::stod(radical_elements.at(1));
122 } catch (std::invalid_argument& ia) {
123 throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
127 ys = std::stod(radical_elements.back());
128 } catch (std::invalid_argument& ia) {
129 throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
133 NetworkIBModel::~NetworkIBModel()
135 for (auto const& instance : active_nodes)
136 delete instance.second;
139 void NetworkIBModel::computeIBfactors(IBNode* root)
141 double num_comm_out = static_cast<double>(root->ActiveCommsUp.size());
142 double max_penalty_out = 0.0;
143 // first, compute all outbound penalties to get their max
144 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
145 double my_penalty_out = 1.0;
147 if (num_comm_out != 1) {
148 if ((*it)->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
149 my_penalty_out = num_comm_out * Bs * ys;
151 my_penalty_out = num_comm_out * Bs;
154 max_penalty_out = std::max(max_penalty_out, my_penalty_out);
157 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
158 // compute inbound penalty
159 double my_penalty_in = 1.0;
160 int nb_comms = (*it)->destination->nbActiveCommsDown; // total number of incoming comms
162 my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] // number of comm sent to dest by root node
163 * Be * (*it)->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
165 double penalty = std::max(my_penalty_in, max_penalty_out);
167 double rate_before_update = (*it)->action->getBound();
168 // save initial rate of the action
169 if ((*it)->init_rate == -1)
170 (*it)->init_rate = rate_before_update;
172 double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate;
174 if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
175 XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
176 (*it)->destination->id, (*it)->action, penalized_bw, (*it)->action->getBound(), (*it)->init_rate);
177 lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
179 XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, (*it)->destination->id,
180 (*it)->action, penalized_bw, (*it)->init_rate);
183 XBT_DEBUG("Finished computing IB penalties");
186 void NetworkIBModel::updateIBfactors_rec(IBNode* root, std::vector<bool>& updatedlist)
188 if (not updatedlist[root->id]) {
189 XBT_DEBUG("IB - Updating rec %d", root->id);
190 computeIBfactors(root);
191 updatedlist[root->id] = true;
192 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
193 if (not updatedlist[(*it)->destination->id])
194 updateIBfactors_rec((*it)->destination, updatedlist);
196 for (std::map<IBNode*, int>::iterator it = root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
197 if (not updatedlist[it->first->id])
198 updateIBfactors_rec(it->first, updatedlist);
203 void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode* to, int remove)
205 if (from == to) // disregard local comms (should use loopback)
208 ActiveComm* comm = nullptr;
210 if (to->ActiveCommsDown[from] == 1)
211 to->ActiveCommsDown.erase(from);
213 to->ActiveCommsDown[from] -= 1;
215 to->nbActiveCommsDown--;
216 for (std::vector<ActiveComm*>::iterator it = from->ActiveCommsUp.begin(); it != from->ActiveCommsUp.end(); ++it) {
217 if ((*it)->action == action) {
219 from->ActiveCommsUp.erase(it);
226 ActiveComm* comm = new ActiveComm();
227 comm->action = action;
228 comm->destination = to;
229 from->ActiveCommsUp.push_back(comm);
231 to->ActiveCommsDown[from] += 1;
232 to->nbActiveCommsDown++;
234 XBT_DEBUG("IB - Updating %d", from->id);
235 std::vector<bool> updated(active_nodes.size(), false);
236 updateIBfactors_rec(from, updated);
237 XBT_DEBUG("IB - Finished updating %d", from->id);