1 /* Copyright (c) 2014-2015. 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;
56 act_src = ibModel->active_nodes.at(src->getName());
57 } catch (std::out_of_range& unfound) {
58 throw std::out_of_range(std::string("Could not find '") + src->getCname() + "' active comms !");
62 act_dst = ibModel->active_nodes.at(dst->getName());
63 } catch (std::out_of_range& unfound) {
64 throw std::out_of_range(std::string("Could not find '") + dst->getCname() + "' active comms !");
67 ibModel->active_comms[action]=std::make_pair(act_src, act_dst);
69 ibModel->updateIBfactors(action, act_src, act_dst, 0);
76 /************************************************************************/
77 /* New model based on MPI contention model for Infiniband platforms */
78 /************************************************************************/
79 /* @Inproceedings{mescal_vienne_phd, */
80 /* author={Jérôme Vienne}, */
81 /* title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
82 /* address={Grenoble FRANCE}, */
86 void surf_network_model_init_IB()
88 if (surf_network_model)
91 surf_network_model = new simgrid::surf::NetworkIBModel();
92 all_existing_models->push_back(surf_network_model);
93 simgrid::s4u::Link::onCommunicationStateChange.connect(IB_action_state_changed_callback);
94 simgrid::s4u::Link::onCommunicate.connect(IB_action_init_callback);
95 simgrid::s4u::Host::onCreation.connect(IB_create_host_callback);
96 xbt_cfg_setdefault_double("network/weight-S", 8775);
103 NetworkIBModel::NetworkIBModel() : NetworkSmpiModel()
106 const char* IB_factors_string = xbt_cfg_get_string("smpi/IB-penalty-factors");
107 std::vector<std::string> radical_elements;
108 boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
110 surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
111 "elements, semi-colon separated. Example: 0.965;0.925;1.35");
114 Be = std::stod(radical_elements.front());
115 } catch (std::invalid_argument& ia) {
116 throw std::invalid_argument(std::string("First part of smpi/IB-penalty-factors is not numerical:") + ia.what());
120 Bs = std::stod(radical_elements.at(1));
121 } catch (std::invalid_argument& ia) {
122 throw std::invalid_argument(std::string("Second part of smpi/IB-penalty-factors is not numerical:") + ia.what());
126 ys = std::stod(radical_elements.back());
127 } catch (std::invalid_argument& ia) {
128 throw std::invalid_argument(std::string("Third part of smpi/IB-penalty-factors is not numerical:") + ia.what());
132 NetworkIBModel::~NetworkIBModel()
134 for (auto instance : active_nodes)
135 delete instance.second;
138 void NetworkIBModel::computeIBfactors(IBNode* root)
140 double num_comm_out = static_cast<double>(root->ActiveCommsUp.size());
141 double max_penalty_out = 0.0;
142 // first, compute all outbound penalties to get their max
143 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
144 double my_penalty_out = 1.0;
146 if (num_comm_out != 1) {
147 if ((*it)->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
148 my_penalty_out = num_comm_out * Bs * ys;
150 my_penalty_out = num_comm_out * Bs;
153 max_penalty_out = std::max(max_penalty_out, my_penalty_out);
156 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
157 // compute inbound penalty
158 double my_penalty_in = 1.0;
159 int nb_comms = (*it)->destination->nbActiveCommsDown; // total number of incoming comms
161 my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] // number of comm sent to dest by root node
162 * Be * (*it)->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
164 double penalty = std::max(my_penalty_in, max_penalty_out);
166 double rate_before_update = (*it)->action->getBound();
167 // save initial rate of the action
168 if ((*it)->init_rate == -1)
169 (*it)->init_rate = rate_before_update;
171 double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate;
173 if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
174 XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
175 (*it)->destination->id, (*it)->action, penalized_bw, (*it)->action->getBound(), (*it)->init_rate);
176 lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
178 XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, (*it)->destination->id,
179 (*it)->action, penalized_bw, (*it)->init_rate);
182 XBT_DEBUG("Finished computing IB penalties");
185 void NetworkIBModel::updateIBfactors_rec(IBNode* root, bool* updatedlist)
187 if (updatedlist[root->id] == 0) {
188 XBT_DEBUG("IB - Updating rec %d", root->id);
189 computeIBfactors(root);
190 updatedlist[root->id] = 1;
191 for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
192 if (updatedlist[(*it)->destination->id] != 1)
193 updateIBfactors_rec((*it)->destination, updatedlist);
195 for (std::map<IBNode*, int>::iterator it = root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
196 if (updatedlist[it->first->id] != 1)
197 updateIBfactors_rec(it->first, updatedlist);
202 void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode* to, int remove)
204 if (from == to) // disregard local comms (should use loopback)
207 bool* updated = (bool*)xbt_malloc0(active_nodes.size() * sizeof(bool));
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 updateIBfactors_rec(from, updated);
236 XBT_DEBUG("IB - Finished updating %d", from->id);