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 "network_ib.hpp"
11 #include "src/surf/HostImpl.hpp"
12 #include "simgrid/sg_config.h"
13 #include "maxmin_private.hpp"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
17 static void IB_create_host_callback(simgrid::s4u::Host& host){
18 using simgrid::surf::NetworkIBModel;
19 using simgrid::surf::IBNode;
22 // pour t->id -> rajouter une nouvelle struct dans le dict, pour stocker les comms actives
23 if(((NetworkIBModel*)surf_network_model)->active_nodes==nullptr)
24 ((NetworkIBModel*)surf_network_model)->active_nodes=xbt_dict_new();
26 IBNode* act = new IBNode(id);
29 xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes,
30 host.name().c_str(), act, nullptr);
34 static void IB_action_state_changed_callback(
35 simgrid::surf::NetworkAction *action,
36 simgrid::surf::Action::State statein, simgrid::surf::Action::State stateout)
38 using simgrid::surf::NetworkIBModel;
39 using simgrid::surf::IBNode;
41 if(statein!=simgrid::surf::Action::State::running || stateout!=simgrid::surf::Action::State::done)
43 std::pair<IBNode*,IBNode*> pair = ((NetworkIBModel*)surf_network_model)->active_comms[action];
44 XBT_DEBUG("IB callback - action %p finished", action);
46 ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, pair.first, pair.second, 1);
48 ((NetworkIBModel*)surf_network_model)->active_comms.erase(action);
53 static void IB_action_init_callback(
54 simgrid::surf::NetworkAction *action, simgrid::routing::NetCard *src, simgrid::routing::NetCard *dst)
56 simgrid::surf::NetworkIBModel* ibModel = (simgrid::surf::NetworkIBModel*)surf_network_model;
58 simgrid::surf::IBNode* act_src= (simgrid::surf::IBNode*) xbt_dict_get_or_null(ibModel->active_nodes, src->name());
59 xbt_assert(act_src, "could not find src node active comms !");
61 simgrid::surf::IBNode* act_dst= (simgrid::surf::IBNode*) xbt_dict_get_or_null(ibModel->active_nodes, dst->name());
62 xbt_assert(act_dst, "could not find dst node active comms !");
64 ibModel->active_comms[action]=std::make_pair(act_src, act_dst);
66 ibModel->updateIBfactors(action, act_src, act_dst, 0);
73 /************************************************************************/
74 /* New model based on MPI contention model for Infiniband platforms */
75 /************************************************************************/
76 /* @Inproceedings{mescal_vienne_phd, */
77 /* author={Jérôme Vienne}, */
78 /* title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
79 /* address={Grenoble FRANCE}, */
83 void surf_network_model_init_IB(void)
85 using simgrid::surf::networkActionStateChangedCallbacks;
87 if (surf_network_model)
90 surf_network_model = new simgrid::surf::NetworkIBModel();
91 xbt_dynar_push(all_existing_models, &surf_network_model);
92 networkActionStateChangedCallbacks.connect(IB_action_state_changed_callback);
93 Link::onCommunicate.connect(IB_action_init_callback);
94 simgrid::s4u::Host::onCreation.connect(IB_create_host_callback);
95 xbt_cfg_setdefault_double("network/weight-S", 8775);
99 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
104 NetworkIBModel::NetworkIBModel()
105 : NetworkSmpiModel() {
107 active_nodes=nullptr;
109 const char* IB_factors_string=xbt_cfg_get_string("smpi/IB-penalty-factors");
110 xbt_dynar_t radical_elements = xbt_str_split(IB_factors_string, ";");
112 surf_parse_assert(xbt_dynar_length(radical_elements)==3,
113 "smpi/IB-penalty-factors should be provided and contain 3 elements, semi-colon separated. Example: 0.965;0.925;1.35");
115 Be = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 0, char *), "First part of smpi/IB-penalty-factors is not numerical: %s");
116 Bs = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 1, char *), "Second part of smpi/IB-penalty-factors is not numerical: %s");
117 ys = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 2, char *), "Third part of smpi/IB-penalty-factors is not numerical: %s");
119 xbt_dynar_free(&radical_elements);
122 NetworkIBModel::~NetworkIBModel()
124 xbt_dict_cursor_t cursor = nullptr;
125 IBNode* instance = nullptr;
126 char *name = nullptr;
127 xbt_dict_foreach(active_nodes, cursor, name, instance)
129 xbt_dict_free(&active_nodes);
132 void NetworkIBModel::computeIBfactors(IBNode *root) {
133 double penalized_bw=0.0;
134 double num_comm_out = (double) root->ActiveCommsUp.size();
135 double max_penalty_out=0.0;
136 //first, compute all outbound penalties to get their max
137 for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
138 double my_penalty_out = 1.0;
141 if((*it)->destination->nbActiveCommsDown > 2)//number of comms sent to the receiving node
142 my_penalty_out = num_comm_out * Bs * ys;
144 my_penalty_out = num_comm_out * Bs;
147 max_penalty_out = std::max(max_penalty_out,my_penalty_out);
150 for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
152 //compute inbound penalty
153 double my_penalty_in = 1.0;
154 int nb_comms = (*it)->destination->nbActiveCommsDown;//total number of incoming comms
156 my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] //number of comm sent to dest by root node
158 * (*it)->destination->ActiveCommsDown.size();//number of different nodes sending to dest
160 double penalty = std::max(my_penalty_in,max_penalty_out);
162 double rate_before_update = (*it)->action->getBound();
163 //save initial rate of the action
164 if((*it)->init_rate==-1)
165 (*it)->init_rate= rate_before_update;
167 penalized_bw= ! num_comm_out ? (*it)->init_rate : (*it)->init_rate /penalty;
169 if (!double_equals(penalized_bw, rate_before_update, sg_surf_precision)){
170 XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,(*it)->destination->id,(*it)->action,penalized_bw, (*it)->action->getBound(), (*it)->init_rate );
171 lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
173 XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id,(*it)->destination->id,(*it)->action,penalized_bw, (*it)->init_rate );
177 XBT_DEBUG("Finished computing IB penalties");
180 void NetworkIBModel::updateIBfactors_rec(IBNode *root, bool* updatedlist) {
181 if(updatedlist[root->id]==0){
182 XBT_DEBUG("IB - Updating rec %d", root->id);
183 computeIBfactors(root);
184 updatedlist[root->id]=1;
185 for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
186 if(updatedlist[(*it)->destination->id]!=1)
187 updateIBfactors_rec((*it)->destination, updatedlist);
189 for (std::map<IBNode*, int>::iterator it= root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
190 if(updatedlist[it->first->id]!=1)
191 updateIBfactors_rec(it->first, updatedlist);
197 void NetworkIBModel::updateIBfactors(NetworkAction *action, IBNode *from, IBNode * to, int remove) {
198 if (from == to)//disregard local comms (should use loopback)
201 bool* updated=(bool*)xbt_malloc0(xbt_dict_size(active_nodes)*sizeof(bool));
202 ActiveComm* comm=nullptr;
204 if(to->ActiveCommsDown[from]==1)
205 to->ActiveCommsDown.erase(from);
207 to->ActiveCommsDown[from]-=1;
209 to->nbActiveCommsDown--;
210 for (std::vector<ActiveComm*>::iterator it= from->ActiveCommsUp.begin();
211 it != from->ActiveCommsUp.end(); ++it) {
212 if((*it)->action==action){
214 from->ActiveCommsUp.erase(it);
222 ActiveComm* comm=new ActiveComm();
224 comm->destination=to;
225 from->ActiveCommsUp.push_back(comm);
227 to->ActiveCommsDown[from]+=1;
228 to->nbActiveCommsDown++;
230 XBT_DEBUG("IB - Updating %d", from->id);
231 updateIBfactors_rec(from, updated);
232 XBT_DEBUG("IB - Finished updating %d", from->id);